mpi.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/time.h>
  4. #include <sys/types.h>
  5. #include <sys/resource.h>
  6. #include <sys/wait.h>
  7. #include <errno.h>
  8. #include <mpi.h>
  9. #include <NTL/ZZ.h>
  10. #include "controller.h"
  11. #include "worker.h"
  12. #include "dpnode.h"
  13. NTL_CLIENT
  14. static int mpi_size;
  15. static int controllerfds[2];
  16. // Close fds 3 and up, except for the one given (pass -1 to close them
  17. // all)
  18. static void close_highfds_except(int exceptfd)
  19. {
  20. // Find the max fd number
  21. struct rlimit limit;
  22. getrlimit(RLIMIT_NOFILE, &limit);
  23. for (int fd = 3; fd < limit.rlim_cur; ++fd) {
  24. if (fd != exceptfd) {
  25. // There's no ill effect from closing a non-open fd, so just
  26. // do it
  27. close(fd);
  28. }
  29. }
  30. }
  31. static void boundcb(const char *boundaddr, unsigned short boundport)
  32. {
  33. // Write the port and addr to the pipe
  34. write(controllerfds[1], &boundport, 2);
  35. write(controllerfds[1], boundaddr, strlen(boundaddr));
  36. close(controllerfds[1]);
  37. }
  38. void desired_resources(const ZZ &order, unsigned short &desired_dpnodes,
  39. unsigned int &max_workers, unsigned int &dpfreq)
  40. {
  41. // How many DPnodes should we use for a problem of this size?
  42. desired_dpnodes = 1;
  43. // How many workers would we like to use?
  44. ZZ sorder = SqrRoot(order >> 46);
  45. if (NumBits(sorder) > 30) {
  46. // Just use all the workers we can find
  47. max_workers = 4294967295U; // 2^32 - 1
  48. } else {
  49. max_workers = trunc_long(sorder,31) + 1;
  50. }
  51. // By default, 1 in 1000 points are distinguihed points. The
  52. // number in the next line is 2^32/1000
  53. dpfreq = 4294967;
  54. if (order < 1000) {
  55. // Just make every point a DP
  56. dpfreq = 4294967295U;
  57. } else if (NumBits(order) < 27) {
  58. // The frequency of DPs should be 10/sqrt(order) to avoid
  59. // a DP-free cycle, so dpfreq = (10*2^32)/sqrt(order)
  60. ZZ f = (to_ZZ(10) << 32) / SqrRoot(order);
  61. dpfreq = trunc_long(f, 31);
  62. }
  63. }
  64. static pid_t fork_and_remember(vector<pid_t> &children)
  65. {
  66. pid_t pid = fork();
  67. if (pid > 0) {
  68. children.push_back(pid);
  69. }
  70. return pid;
  71. }
  72. int main(int argc, char **argv)
  73. {
  74. // Init MPI
  75. MPI_Init(&argc, &argv);
  76. if (argc < 3) {
  77. printf("You must pass at least two arguments (the number of workers and number of dpnodes to start per node/rank).\n");
  78. printf("The remaining arguments will be passed to the controller.\n");
  79. MPI_Finalize();
  80. exit(1);
  81. }
  82. char hostname[257];
  83. gethostname(hostname, 256);
  84. vector<pid_t> children;
  85. int rank = 0;
  86. int ret = 0;
  87. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  88. MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
  89. char boundportaddr[259];
  90. unsigned int num_workers = strtol(argv[1], NULL, 10);
  91. unsigned int num_dpnodes = strtol(argv[2], NULL, 10);
  92. if (rank == 0) {
  93. // Start the controller
  94. pipe(controllerfds);
  95. char fdenv[30];
  96. sprintf(fdenv, "%d", controllerfds[1]);
  97. setenv("CONTROLLER_BOUNDCB_FD", fdenv, 1);
  98. if (fork_and_remember(children) == 0) {
  99. // Child; close the read half of the pipe and all other fds
  100. close_highfds_except(controllerfds[1]);
  101. char name[] = "controller";
  102. argv[2] = name;
  103. execv("./controller", argv+2);
  104. return 1;
  105. } else {
  106. // Parent; close the write half of the pipe
  107. close(controllerfds[1]);
  108. int res;
  109. memset(boundportaddr, '\0', 259);
  110. res = read(controllerfds[0], boundportaddr, 2);
  111. if (res < 2) return 1;
  112. res = read(controllerfds[0], boundportaddr+2, 256);
  113. if (res < 1) return 1;
  114. close(controllerfds[0]);
  115. // std::cerr << "Parent reports controller bound to " << boundaddr << ":" << boundport << "\n";
  116. }
  117. }
  118. MPI_Bcast(boundportaddr, 259, MPI_CHAR, 0, MPI_COMM_WORLD);
  119. unsigned short boundport;
  120. memmove(&boundport, boundportaddr, 2);
  121. char portstr[10];
  122. sprintf(portstr, "%hu", boundport);
  123. const char *boundaddr = boundportaddr + 2;
  124. // The child will spawn its own children to be the workers
  125. for (int i=0; i<num_workers; i++) {
  126. if (fork_and_remember(children) == 0) {
  127. close_highfds_except(-1);
  128. char gpu_index[10];
  129. snprintf(gpu_index, 10, "%d", i);
  130. execl("./worker", "./worker", boundaddr, portstr, gpu_index, NULL);
  131. return 1;
  132. }
  133. }
  134. // And dpnodes
  135. for (int i=0; i<num_dpnodes; i++) {
  136. if (fork_and_remember(children) == 0) {
  137. close_highfds_except(-1);
  138. execl("./dpnode", "./dpnode", boundaddr, portstr, NULL);
  139. return 1;
  140. }
  141. }
  142. // Now wait for all the children
  143. vector<pid_t>::iterator pidit;
  144. for (pidit = children.begin(); pidit != children.end(); ++pidit) {
  145. int status;
  146. waitpid(*pidit, &status, 0);
  147. }
  148. MPI_Finalize();
  149. return ret;
  150. }