mpi.cc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <stdio.h>
  2. #include <mpi.h>
  3. #include <NTL/ZZ.h>
  4. #include "controller.h"
  5. NTL_CLIENT
  6. static int mpi_size;
  7. static int controllerfds[2];
  8. static void boundcb(const char *boundaddr, unsigned short boundport)
  9. {
  10. // Write the port and addr to the pipe
  11. write(controllerfds[1], &boundport, 2);
  12. write(controllerfds[1], boundaddr, strlen(boundaddr));
  13. }
  14. void desired_resources(const ZZ &order, unsigned short &desired_dpnodes,
  15. unsigned int &max_workers, unsigned int &dpfreq)
  16. {
  17. // How many DPnodes should we use for a problem of this size?
  18. desired_dpnodes = 2;
  19. // How many workers would we like to use?
  20. ZZ sorder = SqrRoot(order >> 46);
  21. if (NumBits(sorder) > 30) {
  22. // Just use all the workers we can find
  23. max_workers = 4294967295U; // 2^32 - 1
  24. } else {
  25. max_workers = trunc_long(sorder,31) + 1;
  26. }
  27. // By default, 1 in 1000 points are distinguihed points. The
  28. // number in the next line is 2^32/1000
  29. dpfreq = 4294967;
  30. if (order < 1000) {
  31. // Just make every point a DP
  32. dpfreq = 4294967295U;
  33. } else if (NumBits(order) < 27) {
  34. // The frequency of DPs should be 10/sqrt(order) to avoid
  35. // a DP-free cycle, so dpfreq = (10*2^32)/sqrt(order)
  36. ZZ f = (to_ZZ(10) << 32) / SqrRoot(order);
  37. dpfreq = trunc_long(f, 31);
  38. }
  39. }
  40. int main(int argc, char **argv)
  41. {
  42. // Init MPI
  43. MPI_Init(&argc, &argv);
  44. char hostname[257];
  45. gethostname(hostname, 256);
  46. int rank;
  47. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  48. MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
  49. if (rank == 0) {
  50. // Start the controller
  51. pipe(controllerfds);
  52. if (fork() == 0) {
  53. // Child; close the read half of the pipe
  54. close(controllerfds[0]);
  55. unsigned short bindport;
  56. Worklist worklist;
  57. if (controller_parse_args(argc, argv, bindport, worklist)) {
  58. std::cerr << "Usage: " << argv[0] << " [-p listenport] N1 iter1 N2 iter2 ...\n";
  59. return 1;
  60. }
  61. return controller_main(worklist, bindport, boundcb);
  62. } else {
  63. // Parent; close the write half of the pipe
  64. close(controllerfds[1]);
  65. unsigned short boundport;
  66. unsigned char boundaddr[257];
  67. int res;
  68. res = read(controllerfds[0], &boundport, 2);
  69. if (res < 2) return 1;
  70. res = read(controllerfds[0], boundaddr, 256);
  71. if (res < 1) return 1;
  72. boundaddr[res] = '\0';
  73. std::cerr << "Child bound to " << boundaddr << ":" << boundport << "\n";
  74. }
  75. }
  76. MPI_Finalize();
  77. return 0;
  78. }