mpcio.tcc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // T is the type being stored
  2. // N is a type whose "name" static member is a string naming the type
  3. // so that we can report something useful to the user if they try
  4. // to read a type that we don't have any more values for
  5. template<typename T, typename N>
  6. PreCompStorage<T,N>::PreCompStorage(unsigned player, ProcessingMode mode,
  7. const char *filenameprefix, unsigned thread_num) :
  8. name(N::name), depth(0)
  9. {
  10. init(player, mode, filenameprefix, thread_num);
  11. }
  12. template<typename T, typename N>
  13. void PreCompStorage<T,N>::init(unsigned player, ProcessingMode mode,
  14. const char *filenameprefix, unsigned thread_num, nbits_t depth,
  15. nbits_t width)
  16. {
  17. if (mode != MODE_ONLINE) return;
  18. std::string filename(filenameprefix);
  19. char suffix[20];
  20. if (depth) {
  21. this->depth = depth;
  22. this->width = width;
  23. sprintf(suffix, "%02d.p%d.t%u", depth, player%10, thread_num);
  24. } else {
  25. sprintf(suffix, ".p%d.t%u", player%10, thread_num);
  26. }
  27. filename.append(suffix);
  28. storage.open(filename);
  29. // It's OK if not every file exists; so don't worry about checking
  30. // for errors here. We'll report an error in get() if we actually
  31. // try to use a value for which we don't have a precomputed file.
  32. count = 0;
  33. }
  34. template<typename T, typename N>
  35. void PreCompStorage<T,N>::get(T& nextval)
  36. {
  37. storage >> nextval;
  38. if (!storage.good()) {
  39. std::cerr << "Failed to read precomputed value from " << name;
  40. if (depth) {
  41. std::cerr << (int)depth;
  42. }
  43. if (width > 1) {
  44. std::cerr << "." << (int)width;
  45. }
  46. std::cerr << " storage\n";
  47. exit(1);
  48. }
  49. ++count;
  50. }
  51. // Only computational peers call this; the server should be calling
  52. // rdpfpair() at the same time
  53. template <nbits_t WIDTH>
  54. RDPFTriple<WIDTH> MPCTIO::rdpftriple(yield_t &yield, nbits_t depth,
  55. bool incremental, bool keep_expansion)
  56. {
  57. assert(mpcio.player < 2);
  58. RDPFTriple<WIDTH> val;
  59. MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
  60. if (mpcio.mode == MODE_ONLINE) {
  61. if (incremental) {
  62. std::get<WIDTH-1>(mpcpio.irdpftriples)
  63. [thread_num][depth-1].get(val);
  64. } else {
  65. std::get<WIDTH-1>(mpcpio.rdpftriples)
  66. [thread_num][depth-1].get(val);
  67. }
  68. } else {
  69. val = RDPFTriple<WIDTH>(*this, yield, depth,
  70. incremental, keep_expansion);
  71. iostream_server() <<
  72. val.dpf[(mpcio.player == 0) ? 1 : 2];
  73. if (incremental) {
  74. std::get<WIDTH-1>(mpcpio.irdpftriples)
  75. [thread_num][depth-1].inc();
  76. } else {
  77. std::get<WIDTH-1>(mpcpio.rdpftriples)
  78. [thread_num][depth-1].inc();
  79. }
  80. yield();
  81. }
  82. return val;
  83. }
  84. // Only the server calls this; the computational peers should be calling
  85. // rdpftriple() at the same time
  86. template <nbits_t WIDTH>
  87. RDPFPair<WIDTH> MPCTIO::rdpfpair(yield_t &yield, nbits_t depth,
  88. bool incremental)
  89. {
  90. assert(mpcio.player == 2);
  91. RDPFPair<WIDTH> val;
  92. MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
  93. if (mpcio.mode == MODE_ONLINE) {
  94. if (incremental) {
  95. std::get<WIDTH-1>(mpcsrvio.irdpfpairs)
  96. [thread_num][depth-1].get(val);
  97. } else {
  98. std::get<WIDTH-1>(mpcsrvio.rdpfpairs)
  99. [thread_num][depth-1].get(val);
  100. }
  101. } else {
  102. RDPFTriple<WIDTH> trip(*this, yield, depth, incremental, true);
  103. yield();
  104. iostream_p0() >> val.dpf[0];
  105. iostream_p1() >> val.dpf[1];
  106. if (incremental) {
  107. std::get<WIDTH-1>(mpcsrvio.irdpfpairs)
  108. [thread_num][depth-1].inc();
  109. } else {
  110. std::get<WIDTH-1>(mpcsrvio.rdpfpairs)
  111. [thread_num][depth-1].inc();
  112. }
  113. }
  114. return val;
  115. }