mpi.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. char hostname[257];
  77. gethostname(hostname, 256);
  78. vector<pid_t> children;
  79. int rank = 0;
  80. int ret = 0;
  81. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  82. MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
  83. char boundportaddr[259];
  84. if (rank == 0) {
  85. // Start the controller
  86. pipe(controllerfds);
  87. char fdenv[30];
  88. sprintf(fdenv, "%d", controllerfds[1]);
  89. setenv("CONTROLLER_BOUNDCB_FD", fdenv, 1);
  90. if (fork_and_remember(children) == 0) {
  91. // Child; close the read half of the pipe and all other fds
  92. close_highfds_except(controllerfds[1]);
  93. execv("./controller", argv);
  94. return 1;
  95. } else {
  96. // Parent; close the write half of the pipe
  97. close(controllerfds[1]);
  98. int res;
  99. memset(boundportaddr, '\0', 259);
  100. res = read(controllerfds[0], boundportaddr, 2);
  101. if (res < 2) return 1;
  102. res = read(controllerfds[0], boundportaddr+2, 256);
  103. if (res < 1) return 1;
  104. close(controllerfds[0]);
  105. // std::cerr << "Parent reports controller bound to " << boundaddr << ":" << boundport << "\n";
  106. }
  107. }
  108. MPI_Bcast(boundportaddr, 259, MPI_CHAR, 0, MPI_COMM_WORLD);
  109. unsigned short boundport;
  110. memmove(&boundport, boundportaddr, 2);
  111. char portstr[10];
  112. sprintf(portstr, "%hu", boundport);
  113. const char *boundaddr = boundportaddr + 2;
  114. // The child will spawn two of its own children to be the
  115. // workers
  116. if (fork_and_remember(children) == 0) {
  117. close_highfds_except(-1);
  118. execl("./worker", "./worker", boundaddr, portstr, "0", NULL);
  119. return 1;
  120. } else if (fork_and_remember(children) == 0) {
  121. close_highfds_except(-1);
  122. execl("./worker", "./worker", boundaddr, portstr, "1", NULL);
  123. return 1;
  124. } else if (fork_and_remember(children) == 0) {
  125. // And a dpnode
  126. close_highfds_except(-1);
  127. execl("./dpnode", "./dpnode", boundaddr, portstr, NULL);
  128. return 1;
  129. }
  130. // Now wait for all the children
  131. vector<pid_t>::iterator pidit;
  132. for (pidit = children.begin(); pidit != children.end(); ++pidit) {
  133. int status;
  134. waitpid(*pidit, &status, 0);
  135. }
  136. MPI_Finalize();
  137. return ret;
  138. }