Przeglądaj źródła

Split off the three main()s in preparation for MPI wrapper

Ian Goldberg 14 lat temu
rodzic
commit
5b62cbb2c4
10 zmienionych plików z 117 dodań i 48 usunięć
  1. 7 7
      Makefile
  2. 33 18
      controller.cc
  3. 6 0
      controller.h
  4. 21 0
      controller_main.cc
  5. 3 9
      dpnode.cc
  6. 6 0
      dpnode.h
  7. 16 0
      dpnode_main.cc
  8. 3 14
      worker.cc
  9. 6 0
      worker.h
  10. 16 0
      worker_main.cc

+ 7 - 7
Makefile

@@ -31,7 +31,7 @@ NVCC=nvcc $(NVCCOPTS)
 
 CXXFILES = dlrho.cc gen_N.cc
 CUFILES = parrhoasm.cu dpstream.cu
-OFILES = dlrho.o gen_N.o cudadl.o controller.o evutils.o dpnode.o worker.o
+OFILES = dlrho.o gen_N.o cudadl.o controller.o controller_main.o evutils.o dpnode.o dpnode_main.o worker.o worker_main.o
 TARGETS = gen_N dlrho controller dpnode worker
 
 all: $(TARGETS)
@@ -51,14 +51,14 @@ cudadl.o: parrhoasm.cu cios.asm dpstream.cu
 cios.asm: gencios_reg_20
 	./gencios_reg_20 $(WORDS) > $@
 
-controller: controller.o evutils.o
-	g++ -g -Wall $^ -o $@ -L$(LIBEVENT)/lib -levent -lntl -L$(GMP) -lgmp
+controller: controller.o evutils.o controller_main.o
+	g++ -g -Wall $^ -o $@ -L$(LIBEVENT)/lib -Wl,-rpath=$(LIBEVENT)/lib -levent -lntl -L$(GMP) -lgmp
 
-dpnode: dpnode.o evutils.o
-	g++ -g -Wall $^ -o $@ -L$(LIBEVENT)/lib -levent -lntl -L$(GMP) -lgmp
+dpnode: dpnode.o evutils.o dpnode_main.o
+	g++ -g -Wall $^ -o $@ -L$(LIBEVENT)/lib -Wl,-rpath=$(LIBEVENT)/lib -levent -lntl -L$(GMP) -lgmp
 
-worker: worker.o evutils.o cudadl.o
-	g++ -g -Wall $^ -o $@ -L$(LIBEVENT)/lib -levent -levent_pthreads -lntl -L$(GMP) -lgmp -lpthread -L$(CUDA)/lib64 -lcudart
+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
 
 clean:
 	-rm -f $(OFILES)

+ 33 - 18
controller.cc

@@ -18,11 +18,11 @@ extern "C" {
 #include <vector>
 #include <set>
 #include <map>
-#include <stdlib.h>
 #include <string.h>
 
 #include "evutils.h"
 #include "subproblem.h"
+#include "controller.h"
 
 NTL_CLIENT
 
@@ -33,7 +33,7 @@ struct SubproblemProgress;
 typedef std::set<struct bufferevent *> BESet;
 typedef std::map<struct bufferevent *, SubproblemProgress *> BEMap;
 
-void besetdump(const BESet &bes, ostream &os)
+static void besetdump(const BESet &bes, ostream &os)
 {
     BESet::const_iterator besit;
 
@@ -44,7 +44,7 @@ void besetdump(const BESet &bes, ostream &os)
     os << dec << "\n";
 }
 
-void besetfree(BESet &bes)
+static void besetfree(BESet &bes)
 {
     BESet::iterator besit;
 
@@ -55,7 +55,7 @@ void besetfree(BESet &bes)
     bes.clear();
 }
 
-void bemapdump(const BEMap &bem, ostream &os)
+static void bemapdump(const BEMap &bem, ostream &os)
 {
     BEMap::const_iterator bemit;
 
@@ -66,7 +66,7 @@ void bemapdump(const BEMap &bem, ostream &os)
     os << dec << "\n";
 }
 
-void bemapfree(BEMap &bem)
+static void bemapfree(BEMap &bem)
 {
     BEMap::iterator bemit;
 
@@ -100,7 +100,7 @@ struct FactorDecomp {
     vec_ZZ fvec;
 };
 
-void vsppdump(const vector<SubproblemProgress> &spv, ostream &os);
+static void vsppdump(const vector<SubproblemProgress> &spv, ostream &os);
 
 static struct ControllerState {
     ZZ rho;
@@ -162,7 +162,7 @@ struct IPPort {
 
 typedef vector<IPPort> IPPortSet;
 
-void ipportsetdump(const IPPortSet &ipps, ostream &os)
+static void ipportsetdump(const IPPortSet &ipps, ostream &os)
 {
     IPPortSet::const_iterator ippsit;
 
@@ -256,7 +256,7 @@ struct SubproblemProgress : Subproblem {
 };
 
 // Dump the state for debug purposes
-void vsppdump(const vector<SubproblemProgress> &spv, ostream &os)
+static void vsppdump(const vector<SubproblemProgress> &spv, ostream &os)
 {
     vector<SubproblemProgress>::const_iterator spiter;
     int count = 0;
@@ -738,14 +738,32 @@ static void controller_accept_cb(struct evconnlistener *listener,
 // Create a new controller socket.  bindport is the port to bind to (in
 // host byte order), or 0 if any port will do.  ip and boundport are set
 // to the IP and port of the socket, in network byte order.
-struct evconnlistener *controller_create(struct event_base *evbase,
+static struct evconnlistener *controller_create(struct event_base *evbase,
     unsigned short bindport, unsigned int *ip, unsigned short *boundport)
 {
     return listener_create(evbase, bindport, controller_accept_cb, NULL,
 	ip, boundport, false);
 }
 
-int main(int argc, char **argv)
+// Read the modulus (and the factorization of the modulus and its
+// totient) from the given file.  "-" means cin.  Returns true if
+// successful.
+static bool read_modulus(const char *filename)
+{
+    if (strcmp(filename, "-")) {
+	ifstream ins(filename);
+	if (!ins.good()) return false;
+	ins >> ctrlstate.rho >> ctrlstate.p.factor >> ctrlstate.p.fvec >>
+	    ctrlstate.q.factor >> ctrlstate.q.fvec;
+	ins.close();
+    } else {
+	cin >> ctrlstate.rho >> ctrlstate.p.factor >> ctrlstate.p.fvec >>
+	    ctrlstate.q.factor >> ctrlstate.q.fvec;
+    }
+    return true;
+}
+
+int controller_main(const char *modulus_file, unsigned short bindport)
 {
     // Initialize the prng with some randomness from the kernel
     unsigned char randbuf[1024];
@@ -755,14 +773,11 @@ int main(int argc, char **argv)
     ZZ randzz = ZZFromBytes(randbuf, sizeof(randbuf));
     SetSeed(randzz);
 
-    // Read the modulus and the factorization of its totient from cin
-    cin >> ctrlstate.rho >> ctrlstate.p.factor >> ctrlstate.p.fvec >>
-	ctrlstate.q.factor >> ctrlstate.q.fvec;
-
-    unsigned short bindport = 0;
-
-    if (argc > 1) {
-	bindport = strtoul(argv[1], NULL, 10);
+    // Read the modulus and the factorization of its totient from the
+    // specified file
+    if (!read_modulus(modulus_file)) {
+	cerr << "Unable to read file " << modulus_file << "\n";
+	return 1;
     }
 
     struct event_base *evbase = event_base_new();

+ 6 - 0
controller.h

@@ -0,0 +1,6 @@
+#ifndef __CONTROLLER_H__
+#define __CONTROLLER_H__
+
+int controller_main(const char *modulus_file, unsigned short bindport);
+
+#endif

+ 21 - 0
controller_main.cc

@@ -0,0 +1,21 @@
+#include <iostream>
+
+#include <stdlib.h>
+
+#include "controller.h"
+
+int main(int argc, char **argv)
+{
+    if (argc < 2) {
+	std::cerr << "Usage: " << argv[0] << " N [listenport]\n";
+	return 1;
+    }
+
+    unsigned short bindport = 0;
+
+    if (argc > 2) {
+	bindport = strtoul(argv[2], NULL, 10);
+    }
+
+    return controller_main(argv[1], bindport);
+}

+ 3 - 9
dpnode.cc

@@ -17,6 +17,7 @@ extern "C" {
 
 #include "evutils.h"
 #include "subproblem.h"
+#include "dpnode.h"
 
 typedef map<std::string, pair<ZZ,ZZ> > DTable;
 
@@ -275,15 +276,8 @@ static void controllerconn_event_cb(struct bufferevent *bev, short events,
     }
 }
 
-int main(int argc, char **argv)
+int dpnode_main(const char *controller_host, unsigned short controller_port)
 {
-    if (argc != 3) {
-	fprintf(stderr, "Usage: %s controller_host controller_port\n", argv[0]);
-	return 1;
-    }
-
-    unsigned short controller_port = strtoul(argv[2], NULL, 10);
-
-    return controller_client(argv[1], controller_port,
+    return controller_client(controller_host, controller_port,
 				controllerconn_event_cb, false);
 }

+ 6 - 0
dpnode.h

@@ -0,0 +1,6 @@
+#ifndef __DPNODE_H__
+#define __DPNODE_H__
+
+int dpnode_main(const char *controller_host, unsigned short controller_port);
+
+#endif

+ 16 - 0
dpnode_main.cc

@@ -0,0 +1,16 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "dpnode.h"
+
+int main(int argc, char **argv)
+{
+    if (argc != 3) {
+	fprintf(stderr, "Usage: %s controller_host controller_port\n", argv[0]);
+	return 1;
+    }
+
+    unsigned short controller_port = strtoul(argv[2], NULL, 10);
+
+    return dpnode_main(argv[1], controller_port);
+}

+ 3 - 14
worker.cc

@@ -19,6 +19,7 @@ extern "C" {
 
 #include "evutils.h"
 #include "subproblem.h"
+#include "worker.h"
 
 NTL_CLIENT
 
@@ -281,24 +282,12 @@ static void controllerconn_event_cb(struct bufferevent *bev, short events,
     }
 }
 
-int main(int argc, char **argv)
+int worker_main(const char *controller_host, unsigned short controller_port)
 {
-    if (argc != 4) {
-	fprintf(stderr, "Usage: %s controller_host controller_port cuda_dev_id\n", argv[0]);
-	return 1;
-    }
-
-    unsigned short controller_port = strtoul(argv[2], NULL, 10);
-
-    if (cuda_init(strtoul(argv[3], NULL, 10)) < 0) {
-	cerr << "Error setting CUDA device\n";
-	exit(1);
-    }
-
     evthread_use_pthreads();
 
     signal(SIGPIPE, SIG_IGN);
 
-    return controller_client(argv[1], controller_port,
+    return controller_client(controller_host, controller_port,
 				controllerconn_event_cb, true);
 }

+ 6 - 0
worker.h

@@ -0,0 +1,6 @@
+#ifndef __WORKER_H__
+#define __WORKER_H__
+
+int worker_main(const char *controller_host, unsigned short controller_port);
+
+#endif

+ 16 - 0
worker_main.cc

@@ -0,0 +1,16 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "worker.h"
+
+int main(int argc, char **argv)
+{
+    if (argc != 4) {
+	fprintf(stderr, "Usage: %s controller_host controller_port cuda_dev_id\n", argv[0]);
+	return 1;
+    }
+
+    unsigned short controller_port = strtoul(argv[2], NULL, 10);
+
+    return worker_main(argv[1], controller_port);
+}