prac.cpp 4.8 KB

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