Selaa lähdekoodia

Single-node MPI launcher

Ian Goldberg 14 vuotta sitten
vanhempi
commit
a08006fba7
2 muutettua tiedostoa jossa 33 lisäystä ja 16 poistoa
  1. 1 1
      Makefile
  2. 32 15
      mpi.cc

+ 1 - 1
Makefile

@@ -64,7 +64,7 @@ dpnode: dpnode.o evutils.o dpnode_main.o
 worker: worker.o evutils.o cudadl.o worker_main.o
 	g++ -g -Wall $^ -o $@ -L$(LIBEVENT)/lib -Wl,-rpath=$(LIBEVENT)/lib -levent -levent_pthreads -lntl -L$(GMP) -lgmp -lpthread -L$(CUDA)/lib64 -lcudart
 
-mpi: mpi.o controller.o evutils.o
+mpi: mpi.o controller.o worker.o dpnode.o evutils.o cudadl.o
 	mpiCC -g -Wall $^ -o $@ -L$(LIBEVENT)/lib $(LIBEVENT)/lib/libevent.a $(LIBEVENT)/lib/libevent_pthreads.a -lntl -L$(GMP) -lgmp -lpthread -L$(CUDA)/lib64 -lcudart
 
 mpi.o: mpi.cc

+ 32 - 15
mpi.cc

@@ -4,6 +4,8 @@
 #include <NTL/ZZ.h>
 
 #include "controller.h"
+#include "worker.h"
+#include "dpnode.h"
 
 NTL_CLIENT
 
@@ -16,6 +18,7 @@ static void boundcb(const char *boundaddr, unsigned short boundport)
     // Write the port and addr to the pipe
     write(controllerfds[1], &boundport, 2);
     write(controllerfds[1], boundaddr, strlen(boundaddr));
+    close(controllerfds[1]);
 }
 
 void desired_resources(const ZZ &order, unsigned short &desired_dpnodes,
@@ -56,6 +59,8 @@ int main(int argc, char **argv)
 
     int rank;
 
+    int ret = 0;
+
     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
     MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
 
@@ -63,35 +68,47 @@ int main(int argc, char **argv)
 	// Start the controller
 	pipe(controllerfds);
 	if (fork() == 0) {
-	    // Child; close the read half of the pipe
-	    close(controllerfds[0]);
-	    unsigned short bindport;
-	    Worklist worklist;
-
-	    if (controller_parse_args(argc, argv, bindport, worklist)) {
-		std::cerr << "Usage: " << argv[0] << " [-p listenport] N1 iter1 N2 iter2 ...\n";
-		return 1;
-	    }
-
-	    return controller_main(worklist, bindport, boundcb);
-	} else {
-	    // Parent; close the write half of the pipe
+	    // Child; close the write half of the pipe
 	    close(controllerfds[1]);
 	    unsigned short boundport;
-	    unsigned char boundaddr[257];
+	    char boundaddr[257];
 	    int res;
 
 	    res = read(controllerfds[0], &boundport, 2);
 	    if (res < 2) return 1;
 	    res = read(controllerfds[0], boundaddr, 256);
 	    if (res < 1) return 1;
+	    close(controllerfds[0]);
 	    boundaddr[res] = '\0';
 
 	    std::cerr << "Child bound to " << boundaddr << ":" << boundport << "\n";
+
+	    // The child will spawn two of its own children to be the
+	    // workers
+	    if (fork() == 0) {
+		return worker_main(boundaddr, boundport);
+	    } else if (fork() == 0) {
+		return worker_main(boundaddr, boundport);
+	    }
+
+	    // And now become the dpnode
+	    return dpnode_main(boundaddr, boundport);
+	} else {
+	    // Parent; close the read half of the pipe
+	    close(controllerfds[0]);
+	    unsigned short bindport;
+	    Worklist worklist;
+
+	    if (controller_parse_args(argc, argv, bindport, worklist)) {
+		std::cerr << "Usage: " << argv[0] << " [-p listenport] N1 iter1 N2 iter2 ...\n";
+		return 1;
+	    }
+
+	    ret = controller_main(worklist, bindport, boundcb);
 	}
     }
 
     MPI_Finalize();
 
-    return 0;
+    return ret;
 }