prac.cpp 4.5 KB

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