prac.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include <iostream>
  2. #include <deque>
  3. #include "mpcio.hpp"
  4. #include "preproc.hpp"
  5. #include "online.hpp"
  6. #include "options.hpp"
  7. static void usage(const char *progname)
  8. {
  9. std::cerr << "Usage: " << progname << " [-p | -a | -o] [-t num] [-e] [-x] player_num player_addrs args ...\n";
  10. std::cerr << "-p: preprocessing mode\n";
  11. std::cerr << "-a: append to files in preprocessing mode (implies -p)\n";
  12. std::cerr << "-o: online-only mode\n";
  13. std::cerr << "-t num: use num threads\n";
  14. std::cerr << "-e: store DPFs expanded (default is compressed)\n";
  15. std::cerr << "-x: use XOR-shared database (default is additive)\n";
  16. std::cerr << "player_num = 0 or 1 for the computational players\n";
  17. std::cerr << "player_num = 2 for the server player\n";
  18. std::cerr << "player_addrs is omitted for player 0\n";
  19. std::cerr << "player_addrs is p0's hostname for player 1\n";
  20. std::cerr << "player_addrs is p0's hostname followed by p1's hostname for player 2\n";
  21. exit(1);
  22. }
  23. static void comp_player_main(boost::asio::io_context &io_context,
  24. unsigned player, const PRACOptions &opts, const char *p0addr,
  25. char **args)
  26. {
  27. std::deque<tcp::socket> peersocks, serversocks;
  28. mpcio_setup_computational(player, io_context, p0addr,
  29. opts.num_threads, peersocks, serversocks);
  30. MPCPeerIO mpcio(player, opts.mode, peersocks, serversocks);
  31. // Queue up the work to be done
  32. boost::asio::post(io_context, [&]{
  33. if (opts.mode == MODE_PREPROCESSING) {
  34. preprocessing_comp(mpcio, opts, args);
  35. } else {
  36. online_main(mpcio, opts, args);
  37. }
  38. });
  39. // Start another thread; one will perform the work and the other
  40. // will execute the async_write handlers
  41. boost::thread t([&]{io_context.run();});
  42. io_context.run();
  43. t.join();
  44. mpcio.dump_stats(std::cout);
  45. }
  46. static void server_player_main(boost::asio::io_context &io_context,
  47. const PRACOptions &opts, const char *p0addr,
  48. const char *p1addr, char **args)
  49. {
  50. std::deque<tcp::socket> p0socks, p1socks;
  51. mpcio_setup_server(io_context, p0addr, p1addr,
  52. opts.num_threads, p0socks, p1socks);
  53. MPCServerIO mpcserverio(opts.mode, p0socks, p1socks);
  54. // Queue up the work to be done
  55. boost::asio::post(io_context, [&]{
  56. if (opts.mode == MODE_PREPROCESSING) {
  57. preprocessing_server(mpcserverio, opts, args);
  58. } else {
  59. online_main(mpcserverio, opts, args);
  60. }
  61. });
  62. // Start another thread; one will perform the work and the other
  63. // will execute the async_write handlers
  64. boost::thread t([&]{io_context.run();});
  65. io_context.run();
  66. t.join();
  67. mpcserverio.dump_stats(std::cout);
  68. }
  69. int main(int argc, char **argv)
  70. {
  71. char **args = argv+1; // Skip argv[0] (the program name)
  72. PRACOptions opts;
  73. unsigned player = 0;
  74. const char *p0addr = NULL;
  75. const char *p1addr = NULL;
  76. // Get the options
  77. while (*args && *args[0] == '-') {
  78. if (!strcmp("-p", *args)) {
  79. opts.mode = MODE_PREPROCESSING;
  80. ++args;
  81. } else if (!strcmp("-a", *args)) {
  82. opts.mode = MODE_PREPROCESSING;
  83. opts.append_to_files = true;
  84. ++args;
  85. } else if (!strcmp("-o", *args)) {
  86. opts.mode = MODE_ONLINEONLY;
  87. ++args;
  88. } else if (!strcmp("-t", *args)) {
  89. if (args[1]) {
  90. opts.num_threads = atoi(args[1]);
  91. if (opts.num_threads < 1) {
  92. usage(argv[0]);
  93. }
  94. args += 2;
  95. } else {
  96. usage(argv[0]);
  97. }
  98. } else if (!strcmp("-e", *args)) {
  99. opts.expand_rdpfs = true;
  100. ++args;
  101. } else if (!strcmp("-x", *args)) {
  102. opts.use_xor_db = true;
  103. ++args;
  104. } else {
  105. printf("Unknown option %s\n", *args);
  106. usage(argv[0]);
  107. }
  108. }
  109. if (*args == NULL) {
  110. // No arguments?
  111. usage(argv[0]);
  112. } else {
  113. player = atoi(*args);
  114. ++args;
  115. }
  116. if (player > 2) {
  117. usage(argv[0]);
  118. }
  119. if (player > 0) {
  120. if (*args == NULL) {
  121. usage(argv[0]);
  122. } else {
  123. p0addr = *args;
  124. ++args;
  125. }
  126. }
  127. if (player > 1) {
  128. if (*args == NULL) {
  129. usage(argv[0]);
  130. } else {
  131. p1addr = *args;
  132. ++args;
  133. }
  134. }
  135. /*
  136. std::cout << "Preprocessing = " <<
  137. (preprocessing ? "true" : "false") << "\n";
  138. std::cout << "Thread count = " << num_threads << "\n";
  139. std::cout << "Player = " << player << "\n";
  140. if (p0addr) {
  141. std::cout << "Player 0 addr = " << p0addr << "\n";
  142. }
  143. if (p1addr) {
  144. std::cout << "Player 1 addr = " << p1addr << "\n";
  145. }
  146. std::cout << "Args =";
  147. for (char **a = args; *a; ++a) {
  148. std::cout << " " << *a;
  149. }
  150. std::cout << "\n";
  151. */
  152. // Make the network connections
  153. boost::asio::io_context io_context;
  154. if (player < 2) {
  155. comp_player_main(io_context, player, opts, p0addr, args);
  156. } else {
  157. server_player_main(io_context, opts, p0addr, p1addr, args);
  158. }
  159. return 0;
  160. }