prac.cpp 5.6 KB

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