Prechádzať zdrojové kódy

Add -a mode for preprocessing with appending

This will enable the creation of large preprocess files without
running out of memory, by running prac multiple times with the -a
options.
Ian Goldberg 1 rok pred
rodič
commit
45357b1778
3 zmenil súbory, kde vykonal 14 pridanie a 4 odobranie
  1. 2 1
      options.hpp
  2. 6 1
      prac.cpp
  3. 6 2
      preproc.cpp

+ 2 - 1
options.hpp

@@ -8,9 +8,10 @@ struct PRACOptions {
     int num_threads;
     bool expand_rdpfs;
     bool use_xor_db;
+    bool append_to_files;
 
     PRACOptions() : mode(MODE_ONLINE), num_threads(1),
-        expand_rdpfs(true), use_xor_db(false) {}
+        expand_rdpfs(true), use_xor_db(false), append_to_files(false) {}
 };
 
 #endif

+ 6 - 1
prac.cpp

@@ -8,8 +8,9 @@
 
 static void usage(const char *progname)
 {
-    std::cerr << "Usage: " << progname << " [-p] [-t num] player_num player_addrs args ...\n";
+    std::cerr << "Usage: " << progname << " [-p | -a | -o] [-t num] [-c] [-x] player_num player_addrs args ...\n";
     std::cerr << "-p: preprocessing mode\n";
+    std::cerr << "-a: append to files in preprocessing mode (implies -p)\n";
     std::cerr << "-o: online-only mode\n";
     std::cerr << "-t num: use num threads\n";
     std::cerr << "-c: store DPFs compressed (default is expanded)\n";
@@ -86,6 +87,10 @@ int main(int argc, char **argv)
         if (!strcmp("-p", *args)) {
             opts.mode = MODE_PREPROCESSING;
             ++args;
+        } else if (!strcmp("-a", *args)) {
+            opts.mode = MODE_PREPROCESSING;
+            opts.append_to_files = true;
+            ++args;
         } else if (!strcmp("-o", *args)) {
             opts.mode = MODE_ONLINEONLY;
             ++args;

+ 6 - 2
preproc.cpp

@@ -8,9 +8,12 @@
 
 // Keep track of open files that coroutines might be writing into
 class Openfiles {
+    bool append_mode;
     std::vector<std::ofstream> files;
 
 public:
+    Openfiles(bool append_mode = false) : append_mode(append_mode) {}
+
     class Handle {
         Openfiles &parent;
         size_t idx;
@@ -42,7 +45,8 @@ Openfiles::Handle Openfiles::open(const char *prefix, unsigned player,
         sprintf(suffix, ".p%d.t%u", player%10, thread_num);
     }
     filename.append(suffix);
-    std::ofstream &f = files.emplace_back(filename);
+    std::ofstream &f = files.emplace_back(filename,
+        append_mode ? std::ios_base::app : std::ios_base::out);
     if (f.fail()) {
         std::cerr << "Failed to open " << filename << "\n";
         exit(1);
@@ -83,7 +87,7 @@ void preprocessing_comp(MPCIO &mpcio, const PRACOptions &opts, char **args)
     for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
         boost::asio::post(pool, [&mpcio, &opts, thread_num] {
             MPCTIO tio(mpcio, thread_num);
-            Openfiles ofiles;
+            Openfiles ofiles(opts.append_to_files);
             std::vector<coro_t> coroutines;
             while(1) {
                 unsigned char type = 0;