소스 검색

PreCompStorage for wide RDPFs

This also unlocks the unit test.  The unit test is not yet expected to
pass, since wide RDPFs are not yet actually being created.
Ian Goldberg 3 년 전
부모
커밋
59772f3c07
4개의 변경된 파일116개의 추가작업 그리고 44개의 파일을 삭제
  1. 79 34
      mpcio.cpp
  2. 27 3
      mpcio.hpp
  3. 10 5
      mpcio.tcc
  4. 0 2
      online.cpp

+ 79 - 34
mpcio.cpp

@@ -231,6 +231,53 @@ void MPCIO::dump_stats(std::ostream &os)
     dump_memusage(os);
 }
 
+// TVA is a tuple of vectors of arrays of PreCompStorage
+template <nbits_t WIDTH, typename TVA>
+static void rdpfstorage_init(TVA &storage, unsigned player,
+    ProcessingMode mode, unsigned num_threads)
+{
+    auto &VA = std::get<WIDTH-1>(storage);
+    VA.resize(num_threads);
+    char prefix[11];
+    strcpy(prefix, "rdpf");
+    if (WIDTH > 1) {
+        sprintf(prefix+strlen(prefix), "_w%d", WIDTH);
+    }
+    for (unsigned i=0; i<num_threads; ++i) {
+        for (unsigned depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
+            VA[i][depth-1].init(player, mode, prefix, i, depth, WIDTH);
+        }
+    }
+}
+
+// TVA is a tuple of vectors of arrays of PreCompStorage
+template <nbits_t WIDTH, typename TVA>
+static void rdpfstorage_dumpstats(std::ostream &os, TVA &storage,
+    size_t thread_num)
+{
+    auto &VA = std::get<WIDTH-1>(storage);
+    for (nbits_t depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
+        size_t cnt = VA[thread_num][depth-1].get_stats();
+        if (cnt > 0) {
+            os << " r" << int(depth);
+            if (WIDTH > 1) {
+                os << "." << int(WIDTH);
+            }
+            os << ":" << cnt;
+        }
+    }
+}
+
+// TVA is a tuple of vectors of arrays of PreCompStorage
+template <nbits_t WIDTH, typename TVA>
+static void rdpfstorage_resetstats(TVA &storage, size_t thread_num)
+{
+    auto &VA = std::get<WIDTH-1>(storage);
+    for (nbits_t depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
+        VA[thread_num][depth-1].reset_stats();
+    }
+}
+
 MPCPeerIO::MPCPeerIO(unsigned player, ProcessingMode mode,
         std::deque<tcp::socket> &peersocks,
         std::deque<tcp::socket> &serversocks) :
@@ -249,13 +296,11 @@ MPCPeerIO::MPCPeerIO(unsigned player, ProcessingMode mode,
     for (unsigned i=0; i<num_threads; ++i) {
         valselecttriples.emplace_back(player, mode, "selects", i);
     }
-    rdpftriples.resize(num_threads);
-    for (unsigned i=0; i<num_threads; ++i) {
-        for (unsigned depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
-            rdpftriples[i][depth-1].init(player, mode,
-                "rdpf", i, depth);
-        }
-    }
+    rdpfstorage_init<1>(rdpftriples, player, mode, num_threads);
+    rdpfstorage_init<2>(rdpftriples, player, mode, num_threads);
+    rdpfstorage_init<3>(rdpftriples, player, mode, num_threads);
+    rdpfstorage_init<4>(rdpftriples, player, mode, num_threads);
+    rdpfstorage_init<5>(rdpftriples, player, mode, num_threads);
     for (unsigned i=0; i<num_threads; ++i) {
         cdpfs.emplace_back(player, mode, "cdpf", i);
     }
@@ -291,12 +336,11 @@ void MPCPeerIO::dump_precomp_stats(std::ostream &os)
         if (cnt > 0) {
             os << " s:" << cnt;
         }
-        for (nbits_t depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
-            cnt = rdpftriples[i][depth-1].get_stats();
-            if (cnt > 0) {
-                os << " r" << int(depth) << ":" << cnt;
-            }
-        }
+        rdpfstorage_dumpstats<1>(os, rdpftriples, i);
+        rdpfstorage_dumpstats<2>(os, rdpftriples, i);
+        rdpfstorage_dumpstats<3>(os, rdpftriples, i);
+        rdpfstorage_dumpstats<4>(os, rdpftriples, i);
+        rdpfstorage_dumpstats<5>(os, rdpftriples, i);
         cnt = cdpfs[i].get_stats();
         if (cnt > 0) {
             os << " c:" << cnt;
@@ -312,9 +356,11 @@ void MPCPeerIO::reset_precomp_stats()
         halftriples[i].reset_stats();
         andtriples[i].reset_stats();
         valselecttriples[i].reset_stats();
-        for (nbits_t depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
-            rdpftriples[i][depth-1].reset_stats();
-        }
+        rdpfstorage_resetstats<1>(rdpftriples, i);
+        rdpfstorage_resetstats<2>(rdpftriples, i);
+        rdpfstorage_resetstats<3>(rdpftriples, i);
+        rdpfstorage_resetstats<4>(rdpftriples, i);
+        rdpfstorage_resetstats<5>(rdpftriples, i);
     }
 }
 
@@ -330,13 +376,11 @@ MPCServerIO::MPCServerIO(ProcessingMode mode,
         std::deque<tcp::socket> &p1socks) :
     MPCIO(2, mode, p0socks.size())
 {
-    rdpfpairs.resize(num_threads);
-    for (unsigned i=0; i<num_threads; ++i) {
-        for (unsigned depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
-            rdpfpairs[i][depth-1].init(player, mode,
-                "rdpf", i, depth);
-        }
-    }
+    rdpfstorage_init<1>(rdpfpairs, player, mode, num_threads);
+    rdpfstorage_init<2>(rdpfpairs, player, mode, num_threads);
+    rdpfstorage_init<3>(rdpfpairs, player, mode, num_threads);
+    rdpfstorage_init<4>(rdpfpairs, player, mode, num_threads);
+    rdpfstorage_init<5>(rdpfpairs, player, mode, num_threads);
     for (unsigned i=0; i<num_threads; ++i) {
         p0ios.emplace_back(std::move(p0socks[i]), "p0", i);
     }
@@ -347,27 +391,28 @@ MPCServerIO::MPCServerIO(ProcessingMode mode,
 
 void MPCServerIO::dump_precomp_stats(std::ostream &os)
 {
-    for (size_t i=0; i<rdpfpairs.size(); ++i) {
+    for (size_t i=0; i<std::get<0>(rdpfpairs).size(); ++i) {
         if (i > 0) {
             os << " ";
         }
         os << "T" << i;
-        for (nbits_t depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
-            size_t cnt = rdpfpairs[i][depth-1].get_stats();
-            if (cnt > 0) {
-                os << " r" << int(depth) << ":" << cnt;
-            }
-        }
+        rdpfstorage_dumpstats<1>(os, rdpfpairs, i);
+        rdpfstorage_dumpstats<2>(os, rdpfpairs, i);
+        rdpfstorage_dumpstats<3>(os, rdpfpairs, i);
+        rdpfstorage_dumpstats<4>(os, rdpfpairs, i);
+        rdpfstorage_dumpstats<5>(os, rdpfpairs, i);
     }
     os << "\n";
 }
 
 void MPCServerIO::reset_precomp_stats()
 {
-    for (size_t i=0; i<rdpfpairs.size(); ++i) {
-        for (nbits_t depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
-            rdpfpairs[i][depth-1].reset_stats();
-        }
+    for (size_t i=0; i<std::get<0>(rdpfpairs).size(); ++i) {
+        rdpfstorage_resetstats<1>(rdpfpairs, i);
+        rdpfstorage_resetstats<2>(rdpfpairs, i);
+        rdpfstorage_resetstats<3>(rdpfpairs, i);
+        rdpfstorage_resetstats<4>(rdpfpairs, i);
+        rdpfstorage_resetstats<5>(rdpfpairs, i);
     }
 }
 

+ 27 - 3
mpcio.hpp

@@ -30,7 +30,8 @@ public:
     PreCompStorage(unsigned player, ProcessingMode mode,
         const char *filenameprefix, unsigned thread_num);
     void init(unsigned player, ProcessingMode mode,
-        const char *filenameprefix, unsigned thread_num, nbits_t depth = 0);
+        const char *filenameprefix, unsigned thread_num,
+        nbits_t depth = 0, nbits_t width = 1);
     void get(T& nextval);
 
     inline void inc() { ++count; }
@@ -40,6 +41,7 @@ private:
     std::ifstream storage;
     std::string name;
     nbits_t depth;
+    nbits_t width;
     size_t count;
 };
 
@@ -215,7 +217,18 @@ struct MPCPeerIO : public MPCIO {
     std::vector<PreCompStorage<CDPF, CDPFName>> cdpfs;
     // The outer vector is (like above) one item per thread
     // The inner array is indexed by DPF depth (depth d is at entry d-1)
-    std::vector<std::array<PreCompStorage<RDPFTriple<1>, RDPFTripleName>,ADDRESS_MAX_BITS>> rdpftriples;
+    // We have one of these whole vectors-of-arrays for each RDPF width,
+    // wrapped into a tuple
+    template <nbits_t WIDTH>
+    using RDPFPrecomps =
+        std::vector<std::array<
+            PreCompStorage<RDPFTriple<WIDTH>, RDPFTripleName>,ADDRESS_MAX_BITS>>;
+    std::tuple<
+        RDPFPrecomps<1>,
+        RDPFPrecomps<2>,
+        RDPFPrecomps<3>,
+        RDPFPrecomps<4>,
+        RDPFPrecomps<5>> rdpftriples;
 
     MPCPeerIO(unsigned player, ProcessingMode mode,
             std::deque<tcp::socket> &peersocks,
@@ -236,7 +249,18 @@ struct MPCServerIO : public MPCIO {
     std::deque<MPCSingleIO> p1ios;
     // The outer vector is (like above) one item per thread
     // The inner array is indexed by DPF depth (depth d is at entry d-1)
-    std::vector<std::array<PreCompStorage<RDPFPair<1>, RDPFPairName>,ADDRESS_MAX_BITS>> rdpfpairs;
+    // We have one of these whole vectors-of-arrays for each RDPF width,
+    // wrapped into a tuple
+    template <nbits_t WIDTH>
+    using RDPFPrecomps =
+        std::vector<std::array<
+            PreCompStorage<RDPFPair<WIDTH>, RDPFPairName>,ADDRESS_MAX_BITS>>;
+    std::tuple<
+        RDPFPrecomps<1>,
+        RDPFPrecomps<2>,
+        RDPFPrecomps<3>,
+        RDPFPrecomps<4>,
+        RDPFPrecomps<5>> rdpfpairs;
 
     MPCServerIO(ProcessingMode mode,
             std::deque<tcp::socket> &p0socks,

+ 10 - 5
mpcio.tcc

@@ -12,13 +12,15 @@ PreCompStorage<T,N>::PreCompStorage(unsigned player, ProcessingMode mode,
 
 template<typename T, typename N>
 void PreCompStorage<T,N>::init(unsigned player, ProcessingMode mode,
-        const char *filenameprefix, unsigned thread_num, nbits_t depth)
+        const char *filenameprefix, unsigned thread_num, nbits_t depth,
+        nbits_t width)
 {
     if (mode != MODE_ONLINE) return;
     std::string filename(filenameprefix);
     char suffix[20];
     if (depth) {
         this->depth = depth;
+        this->width = width;
         sprintf(suffix, "%02d.p%d.t%u", depth, player%10, thread_num);
     } else {
         sprintf(suffix, ".p%d.t%u", player%10, thread_num);
@@ -40,6 +42,9 @@ void PreCompStorage<T,N>::get(T& nextval)
         if (depth) {
             std::cerr << (int)depth;
         }
+        if (width > 1) {
+            std::cerr << "." << (int)width;
+        }
         std::cerr << " storage\n";
         exit(1);
     }
@@ -57,13 +62,13 @@ RDPFTriple<WIDTH> MPCTIO::rdpftriple(yield_t &yield, nbits_t depth,
 
     MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
     if (mpcio.mode == MODE_ONLINE) {
-        mpcpio.rdpftriples[thread_num][depth-1].get(val);
+        std::get<WIDTH-1>(mpcpio.rdpftriples)[thread_num][depth-1].get(val);
     } else {
         val = RDPFTriple<WIDTH>(*this, yield, depth,
             keep_expansion);
         iostream_server() <<
             val.dpf[(mpcio.player == 0) ? 1 : 2];
-        mpcpio.rdpftriples[thread_num][depth-1].inc();
+        std::get<WIDTH-1>(mpcpio.rdpftriples)[thread_num][depth-1].inc();
         yield();
     }
     return val;
@@ -79,13 +84,13 @@ RDPFPair<WIDTH> MPCTIO::rdpfpair(yield_t &yield, nbits_t depth)
 
     MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
     if (mpcio.mode == MODE_ONLINE) {
-        mpcsrvio.rdpfpairs[thread_num][depth-1].get(val);
+        std::get<WIDTH-1>(mpcsrvio.rdpfpairs)[thread_num][depth-1].get(val);
     } else {
         RDPFTriple<WIDTH> trip(*this, yield, depth, true);
         yield();
         iostream_p0() >> val.dpf[0];
         iostream_p1() >> val.dpf[1];
-        mpcsrvio.rdpfpairs[thread_num][depth-1].inc();
+        std::get<WIDTH-1>(mpcsrvio.rdpfpairs)[thread_num][depth-1].inc();
     }
     return val;
 }

+ 0 - 2
online.cpp

@@ -1177,7 +1177,6 @@ void online_main(MPCIO &mpcio, const PRACOptions &opts, char **args)
     } else if (!strcmp(*args, "rdpftest")) {
         ++args;
         rdpf_test<1>(mpcio, opts, args);
-/*
     } else if (!strcmp(*args, "rdpftest2")) {
         ++args;
         rdpf_test<2>(mpcio, opts, args);
@@ -1190,7 +1189,6 @@ void online_main(MPCIO &mpcio, const PRACOptions &opts, char **args)
     } else if (!strcmp(*args, "rdpftest5")) {
         ++args;
         rdpf_test<5>(mpcio, opts, args);
-*/
     } else if (!strcmp(*args, "rdpftime")) {
         ++args;
         rdpf_timing(mpcio, opts, args);