Преглед на файлове

Each thread will also need its own storage for precomputed values

Ian Goldberg преди 1 година
родител
ревизия
342faa921c
променени са 4 файла, в които са добавени 34 реда и са изтрити 18 реда
  1. 10 7
      mpcio.hpp
  2. 1 1
      oblivds.cpp
  3. 22 9
      preproc.cpp
  4. 1 1
      preproc.hpp

+ 10 - 7
mpcio.hpp

@@ -24,7 +24,7 @@ template<typename T>
 class PreCompStorage {
 public:
     PreCompStorage(unsigned player, bool preprocessing,
-        const char *filenameprefix);
+        const char *filenameprefix, unsigned thread_num);
     void get(T& nextval);
 private:
     std::ifstream storage;
@@ -32,11 +32,11 @@ private:
 
 template<typename T>
 PreCompStorage<T>::PreCompStorage(unsigned player, bool preprocessing,
-        const char *filenameprefix) {
+        const char *filenameprefix, unsigned thread_num) {
     if (preprocessing) return;
     std::string filename(filenameprefix);
-    char suffix[4];
-    sprintf(suffix, ".p%d", player%10);
+    char suffix[20];
+    sprintf(suffix, ".p%d.t%u", player%10, thread_num);
     filename.append(suffix);
     storage.open(filename);
     if (storage.fail()) {
@@ -188,12 +188,15 @@ struct MPCIO {
     // culprit), but you can have a deque of those for some reason.
     std::deque<MPCSingleIO> peerios;
     MPCSingleIO serverio;
-    PreCompStorage<MultTriple> triples;
+    std::vector<PreCompStorage<MultTriple>> triples;
 
     MPCIO(unsigned player, bool preprocessing,
             std::deque<tcp::socket> &peersocks, tcp::socket &&serversock) :
-        player(player), serverio(std::move(serversock)),
-        triples(player, preprocessing, "triples") {
+        player(player), serverio(std::move(serversock)) {
+        unsigned num_threads = unsigned(peersocks.size());
+        for (unsigned i=0; i<num_threads; ++i) {
+            triples.emplace_back(player, preprocessing, "triples", i);
+        }
         for (auto &&sock : peersocks) {
             peerios.emplace_back(std::move(sock));
         }

+ 1 - 1
oblivds.cpp

@@ -30,7 +30,7 @@ static void comp_player_main(boost::asio::io_context &io_context,
     // Queue up the work to be done
     boost::asio::post(io_context, [&]{
         if (preprocessing) {
-            preprocessing_comp(mpcio, args);
+            preprocessing_comp(mpcio, num_threads, args);
         }
     });
 

+ 22 - 9
preproc.cpp

@@ -1,14 +1,17 @@
+#include <vector>
 #include <bsd/stdlib.h> // arc4random_buf
 
 #include "preproc.hpp"
 
-// Open a file for writing with name the given prefix, and ".pX" suffix,
-// where X is the (one-digit) player number
-static std::ofstream openfile(const char *prefix, unsigned player)
+// Open a file for writing with name the given prefix, and ".pX.tY"
+// suffix, where X is the (one-digit) player number and Y is the thread
+// number
+static std::ofstream openfile(const char *prefix, unsigned player,
+    unsigned thread_num)
 {
     std::string filename(prefix);
-    char suffix[4];
-    sprintf(suffix, ".p%d", player%10);
+    char suffix[20];
+    sprintf(suffix, ".p%d.t%u", player%10, thread_num);
     filename.append(suffix);
     std::ofstream f;
     f.open(filename);
@@ -33,8 +36,11 @@ static std::ofstream openfile(const char *prefix, unsigned player)
 // Then that number of objects
 //
 // Repeat the whole thing until type == 0x00 is received
+//
+// The incoming objects are written into num_threads files in a
+// round-robin manner
 
-void preprocessing_comp(MPCIO &mpcio, char **args)
+void preprocessing_comp(MPCIO &mpcio, int num_threads, char **args)
 {
     while(1) {
         unsigned char type = 0;
@@ -44,15 +50,22 @@ void preprocessing_comp(MPCIO &mpcio, char **args)
         mpcio.serverio.recv(&num, 4);
         if (type == 0x80) {
             // Multiplication triples
-            std::ofstream tripfile = openfile("triples", mpcio.player);
+            std::vector<std::ofstream> tripfiles;
+            for (int i=0; i<num_threads; ++i) {
+                tripfiles.push_back(openfile("triples", mpcio.player, i));
+            }
+            unsigned thread_num = 0;
             
             MultTriple T;
             for (unsigned int i=0; i<num; ++i) {
                 res = mpcio.serverio.recv(&T, sizeof(T));
                 if (res < sizeof(T)) break;
-                tripfile.write((const char *)&T, sizeof(T));
+                tripfiles[thread_num].write((const char *)&T, sizeof(T));
+                thread_num = (thread_num + 1) % num_threads;
+            }
+            for (int i=0; i<num_threads; ++i) {
+                tripfiles[i].close();
             }
-            tripfile.close();
         }
     }
 }

+ 1 - 1
preproc.hpp

@@ -3,7 +3,7 @@
 
 #include "mpcio.hpp"
 
-void preprocessing_comp(MPCIO &mpcio, char **args);
+void preprocessing_comp(MPCIO &mpcio, int num_threads, char **args);
 void preprocessing_server(MPCServerIO &mpcio, char **args);
 
 #endif