prac.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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] [-c] [-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 << "-c: store DPFs compressed (default is expanded)\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("-c", *args)) {
  99. opts.expand_rdpfs = false;
  100. ++args;
  101. } else if (!strcmp("-x", *args)) {
  102. opts.use_xor_db = true;
  103. ++args;
  104. }
  105. }
  106. if (*args == NULL) {
  107. // No arguments?
  108. usage(argv[0]);
  109. } else {
  110. player = atoi(*args);
  111. ++args;
  112. }
  113. if (player > 2) {
  114. usage(argv[0]);
  115. }
  116. if (player > 0) {
  117. if (*args == NULL) {
  118. usage(argv[0]);
  119. } else {
  120. p0addr = *args;
  121. ++args;
  122. }
  123. }
  124. if (player > 1) {
  125. if (*args == NULL) {
  126. usage(argv[0]);
  127. } else {
  128. p1addr = *args;
  129. ++args;
  130. }
  131. }
  132. /*
  133. std::cout << "Preprocessing = " <<
  134. (preprocessing ? "true" : "false") << "\n";
  135. std::cout << "Thread count = " << num_threads << "\n";
  136. std::cout << "Player = " << player << "\n";
  137. if (p0addr) {
  138. std::cout << "Player 0 addr = " << p0addr << "\n";
  139. }
  140. if (p1addr) {
  141. std::cout << "Player 1 addr = " << p1addr << "\n";
  142. }
  143. std::cout << "Args =";
  144. for (char **a = args; *a; ++a) {
  145. std::cout << " " << *a;
  146. }
  147. std::cout << "\n";
  148. */
  149. // Make the network connections
  150. boost::asio::io_context io_context;
  151. if (player < 2) {
  152. comp_player_main(io_context, player, opts, p0addr, args);
  153. } else {
  154. server_player_main(io_context, opts, p0addr, p1addr, args);
  155. }
  156. return 0;
  157. }