Просмотр исходного кода

Construct and use the worklist from cmdline args in controller

Ian Goldberg 14 лет назад
Родитель
Сommit
44686d4966
2 измененных файлов с 48 добавлено и 33 удалено
  1. 34 26
      controller.cc
  2. 14 7
      controller_main.cc

+ 34 - 26
controller.cc

@@ -111,11 +111,11 @@ static struct ControllerState {
     vector<SubproblemProgress> subproblems_p, subproblems_q;
     Statuses dpnodes, workers;
     unsigned int num_unsolved_subproblems;
-    unsigned int problems_remaining;
+    Worklist worklist;
     struct evconnlistener *listener;
 
     ControllerState() : working(false), num_unsolved_subproblems(0),
-	problems_remaining(0), listener(NULL) {}
+	listener(NULL) {}
 
     // Reset the state for a new problem with the same modulus
     void reset(void) {
@@ -400,6 +400,7 @@ static void computation_complete(void)
 					    computation_length_ms % 1000);
 
     cout << "expon = " << expon << "\n";
+    cout << ctrlstate.worklist[0].first << " ";
     ZZ base_exp = PowerMod(ctrlstate.base, expon, ctrlstate.rho);
     if (base_exp == ctrlstate.target) {
 	cout << "CORRECT in " << length_buf << "\n";
@@ -456,10 +457,37 @@ static vector<SubproblemProgress> decomp(const ZZ_p &base, const ZZ_p &target,
     return ret;
 }
 
+// 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;
+}
+
 static int generate_problem(struct event_base *evbase)
 {
+    while (ctrlstate.worklist[0].second == 0) {
+	ctrlstate.worklist.erase(ctrlstate.worklist.begin());
+	if (ctrlstate.worklist.size() > 0) {
+	    read_modulus(ctrlstate.worklist[0].first);
+	} else {
+	    break;
+	}
+    }
+
     // If there are no more problems to generate, close the listener
-    if (ctrlstate.problems_remaining == 0) {
+    if (ctrlstate.worklist.size() == 0) {
 	evconnlistener_free(ctrlstate.listener);
 	ctrlstate.dpnodes.free();
 	ctrlstate.workers.free();
@@ -496,7 +524,7 @@ static int generate_problem(struct event_base *evbase)
 
     schedule();
 
-    --ctrlstate.problems_remaining;
+    --(ctrlstate.worklist[0].second);
 
     return 0;
 }
@@ -729,24 +757,6 @@ static struct evconnlistener *controller_create(struct event_base *evbase,
 	ip, boundport, false);
 }
 
-// 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 Worklist &worklist, unsigned short bindport)
 {
     // Initialize the prng with some randomness from the kernel
@@ -761,6 +771,8 @@ int controller_main(const Worklist &worklist, unsigned short bindport)
 	return 0;
     }
 
+    ctrlstate.worklist = worklist;
+
     // Read the modulus and the factorization of its totient from the
     // specified file
     if (!read_modulus(worklist[0].first)) {
@@ -774,12 +786,8 @@ int controller_main(const Worklist &worklist, unsigned short bindport)
     unsigned short myport;
 
     ctrlstate.listener = controller_create(evbase, bindport, &myip, &myport);
-#ifdef VERBOSE
     struct in_addr myaddr = { myip };
     printf("Bound to %s:%d\n", inet_ntoa(myaddr), ntohs(myport));
-#endif
-
-    ctrlstate.problems_remaining = worklist[0].second;
 
     // Kick off the first problem to solve
     generate_problem(evbase);

+ 14 - 7
controller_main.cc

@@ -2,6 +2,7 @@
 
 #include <iostream>
 
+#include <string.h>
 #include <stdlib.h>
 
 #include "controller.h"
@@ -38,20 +39,26 @@ void desired_resources(const ZZ &order, unsigned short &desired_dpnodes,
 
 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) {
+    if (argc > 2 && !strncmp(argv[1], "-p", 2)) {
+	// A port number was specified
 	bindport = strtoul(argv[2], NULL, 10);
+	argc -= 2;
+	argv += 2;
+    }
+
+    if (argc < 3 || (argc % 2 == 0)) {
+	std::cerr << "Usage: " << argv[0] << " [-p listenport] N1 iter1 N2 iter2 ...\n";
+	return 1;
     }
 
+
     Worklist worklist;
 
-    worklist.push_back(Workentry(argv[1], 2));
+    for (int i=1;i<argc;i+=2) {
+	worklist.push_back(Workentry(argv[i], strtoul(argv[i+1], NULL, 10)));
+    }
 
     return controller_main(worklist, bindport);
 }