[Swift-commit] r5518 - SwiftApps/SciColSim

wilde at ci.uchicago.edu wilde at ci.uchicago.edu
Tue Jan 24 13:25:35 CST 2012


Author: wilde
Date: 2012-01-24 13:25:34 -0600 (Tue, 24 Jan 2012)
New Revision: 5518

Added:
   SwiftApps/SciColSim/testopt1.py
Removed:
   SwiftApps/SciColSim/script-smaller-a.py
Modified:
   SwiftApps/SciColSim/Makefile
   SwiftApps/SciColSim/optimizer.cpp
   SwiftApps/SciColSim/optimizer.orig-mac.cpp
Log:
Integrate cpp mods cleanly into original optimizer code eith #define to select threading model. Rename and clean up test script.

Modified: SwiftApps/SciColSim/Makefile
===================================================================
--- SwiftApps/SciColSim/Makefile	2012-01-24 18:38:35 UTC (rev 5517)
+++ SwiftApps/SciColSim/Makefile	2012-01-24 19:25:34 UTC (rev 5518)
@@ -1,10 +1,8 @@
-all:	mac-optimizer mac-orig-optimizer
+UNAME := $(shell uname)
 
-mac-optimizer: optimizer.cpp
-	g++  -I boost_1_47_0 -o mac-optimizer optimizer.cpp
+ifeq ($(UNAME), Linux)
 
-mac-orig-optimizer: optimizer.orig-mac.cpp
-	g++  -I boost_1_47_0 -o mac-optimizer optimizer.orig-mac.cpp
+all:	toptimizer
 
 protoall:	toptimizer optimizer Optimizer
 
@@ -16,3 +14,24 @@
 
 toptimizer:	optimizer.cpp
 	g++ -static -fopenmp -I boost_1_47_0 -o toptimizer optimizer.cpp
+
+endif
+
+ifeq ($(UNAME), Darwin)
+
+all:	openmp-optimizer dispatch-optimizer orig-optimizer
+
+openmp-optimizer: optimizer.cpp
+	g++ -DP_OPENMP -fopenmp -I boost_1_47_0 -o openmp-optimizer optimizer.cpp
+
+dispatch-optimizer: optimizer.cpp
+	g++ -DP_DISPATCH -I boost_1_47_0 -o dispatch-optimizer optimizer.cpp
+
+orig-optimizer: optimizer.orig-mac.cpp
+	g++ -I boost_1_47_0 -o orig-optimizer optimizer.orig-mac.cpp
+
+clean:
+	rm -rf mac-openmp-optimizer mac-dispatch-optimizer mac-orig-optimizer
+
+endif
+

Modified: SwiftApps/SciColSim/optimizer.cpp
===================================================================
--- SwiftApps/SciColSim/optimizer.cpp	2012-01-24 18:38:35 UTC (rev 5517)
+++ SwiftApps/SciColSim/optimizer.cpp	2012-01-24 19:25:34 UTC (rev 5518)
@@ -6,8 +6,16 @@
 //  Copyright 2011 University of Chicago. All rights reserved.
 //
 
-#define Nworkers 24
+// Select OpenMP or Grand Central Dispatch for multithreading:
 
+#define MAXNworkers 24
+int Nworkers=MAXNworkers;
+
+// Add operation code to enable existing code to be used at lower level from Swift scripts:
+
+char operation = 'n'; // n: normal; m: do one multi_loss (with n_reruns).
+                      // Not used: a: analyze and generate next annealing parameter set. g: tbd
+
 #include <fstream>
 #include <iostream>
 #include <stdio.h>
@@ -21,7 +29,10 @@
 #include <sys/time.h>
 #include <sys/types.h>
 
+#ifdef P_DISPATCH
 #include <dispatch/dispatch.h>
+#endif
+
 #include <fstream>
 
 
@@ -1099,7 +1110,7 @@
         return 0;
     }
     
-    
+#ifdef notdef    
     //=================================================================
     void try_annealing(double starting_jump, int iterations, 
                        double temp_start, double temp_end, double target_rejection){
@@ -1229,17 +1240,26 @@
         
     }
     
+#endif notdef
 	
 };
 
 //============================================================
 
+#ifdef P_DISPATCH
 std::pair<double,double> multi_loss(dispatch_group_t group, 
                                     Universe* un[], 
                                     dispatch_queue_t* CustomQueues,
                                     double* Results,
                                     int*    Counters,
                                     double* params){
+#else
+std::pair<double,double> multi_loss(Universe* un[], 
+                                    double* Results,
+                                    int*    Counters,
+                                    double* params){
+
+#endif
     
     int N = un[0]->get_reruns();
     int step = (int)(double)N/(double)(Nworkers);
@@ -1254,7 +1274,7 @@
         }
     }
         
-       
+#ifdef P_DISPATCH       
     for(int i=0; i<Nworkers; i++){
         
         dispatch_group_async(group, CustomQueues[i], ^{
@@ -1270,6 +1290,15 @@
     }
     dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
     //dispatch_release(group);
+#else
+    int i;
+    #pragma omp parallel for private (i)
+    for(i=0; i<Nworkers; i++){
+        std::cout<<"multi_loss: Calling evolve_to_target_and_save i=" << i << " N=" << N << " step=" << step << " istart=" << i*step\
+	       << " iend=" << (i+1)*step << "\n";
+        un[i]->evolve_to_target_and_save(i*step, min((i+1)*step,N), Results, Counters);
+     }
+#endif
     
     for (int i=0; i<N; i++){
         
@@ -1295,6 +1324,7 @@
 
 
 //============================================================
+#ifdef P_DISPATCH
 void multi_annealing( dispatch_group_t group, 
                      Universe* un[], 
                      dispatch_queue_t* CustomQueues, 
@@ -1306,6 +1336,17 @@
                      int*    Counters,
                      double* params0,
                      double annealing_cycles){
+#else
+void multi_annealing(Universe* un[], 
+                     double T_start, double T_end, 
+                     double Target_rejection, 
+                     int Annealing_repeats, 
+                     double starting_jump,
+                     double* Results,
+                     int*    Counters,
+                     double* params0,
+                     double annealing_cycles){
+#endif
     //.................................
     // re-implement annealing
     
@@ -1332,10 +1373,26 @@
     
     // establish the current value
     std::pair<double,double>Res;
-    
+
+#ifdef P_DISPATCH    
     Res = multi_loss(group, un, CustomQueues, Results, Counters, x);
+#else
+    Res = multi_loss(       un,               Results, Counters, x);
+#endif
     std::cout << Res.first << " +- " << Res.second << std::endl;
     
+    if ( operation == 'm' ) {
+      FILE *f;
+      int N = un[0]->get_reruns();
+
+      f = fopen("multi_loss.data","w");
+      for(int i=0; i<N; i++) {
+        fprintf(f,"%.20e\n",Results[i]);
+      }
+      fclose(f);
+      exit(0);
+    }
+
     curr_x   = Res.first;
     curr_err = Res.second;
     
@@ -1383,8 +1440,11 @@
                     un[w]->set_parameter(x_tmp, j);
                 }
                 
-                
+#ifdef P_DISPATCH                
                 Res = multi_loss(group, un, CustomQueues, Results, Counters, x);
+#else
+                Res = multi_loss(       un,               Results, Counters, x);
+#endif
                 std::cout << Res.first << " +- " << Res.second << std::endl;
                 
                 ratio = min(1.,exp(-(Res.first-curr_x)/temperature));
@@ -1494,6 +1554,8 @@
     string par_names1[4] = {"n_epochs", "n_steps", "n_reruns", "range"};
     string par_names2[5] = {"T_start", "T_end", "Annealing_steps","Target_rejection","Starting_jump"};
     string par_names3[5] = {"FREEZE_alpha_i", "FREEZE_alpha_m", "FREEZE_beta", "FREEZE_gamma", "FREEZE_delta"};    
+    string par_names4[2] = {"Operation", "Nworkers"};
+
     int params1[4] = {300, 50, 1000, 10};
     int params3[5] = { 0, 0, 0, 0, 0};
     
@@ -1502,9 +1564,10 @@
     
     int verbose_level = 2;
     const std::string one="one", two="two";
-    static Universe* un[Nworkers];
-    static dispatch_queue_t CustomQueues[Nworkers];
-    
+    static Universe* un[MAXNworkers];
+#ifdef P_DISPATCH
+    static dispatch_queue_t CustomQueues[MAXNworkers];
+#endif    
     static double* Results;
     static int*    Counters;
     
@@ -1548,6 +1611,14 @@
                 var_fixed[nArg-17]= atof(argv[nArg]);
                 std::cout << par_names3[nArg-17] << ": " << var_fixed[nArg-17] <<  std::endl;
             }
+            if (nArg == 22 ){
+                operation = *argv[nArg];
+                std::cout << par_names4[0] << ": " << operation <<  std::endl;
+            }
+            if (nArg == 23 ){
+                Nworkers = atoi(argv[nArg]);
+                std::cout << par_names4[1] << ": " << Nworkers <<  std::endl;
+            }
             
             
         }
@@ -1571,7 +1642,9 @@
         un[i] = new Universe((char *)filename,n_ep,n_st,
                              (int)n_rep,
                              identify_failed, target, i2string(i));
+#ifdef P_DISPATCH
         CustomQueues[i] = dispatch_queue_create(i2char(i), NULL);
+#endif
     }
     
     //...............................
@@ -1631,7 +1704,7 @@
     double T_start=params2[0], T_end=params2[1], Target_rejection=params2[3], starting_jump=params2[4];
     int Annealing_repeats = (int) params2[2];
     
-    
+#ifdef P_DISPATCH    
     dispatch_group_t group = dispatch_group_create();
     
     //.............................
@@ -1641,6 +1714,10 @@
     //dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
     dispatch_release(group);
     //.............................
+#else
+    multi_annealing(       un,                T_start, T_end, Target_rejection, Annealing_repeats, 
+                    starting_jump, Results, Counters, params0, Annealing_repeats);
+#endif
     
     
     // stop timer

Modified: SwiftApps/SciColSim/optimizer.orig-mac.cpp
===================================================================
--- SwiftApps/SciColSim/optimizer.orig-mac.cpp	2012-01-24 18:38:35 UTC (rev 5517)
+++ SwiftApps/SciColSim/optimizer.orig-mac.cpp	2012-01-24 19:25:34 UTC (rev 5518)
@@ -6,7 +6,7 @@
 //  Copyright 2011 University of Chicago. All rights reserved.
 //
 
-#define Nworkers 24
+#define Nworkers 2
 
 #include <fstream>
 #include <iostream>

Deleted: SwiftApps/SciColSim/script-smaller-a.py
===================================================================
--- SwiftApps/SciColSim/script-smaller-a.py	2012-01-24 18:38:35 UTC (rev 5517)
+++ SwiftApps/SciColSim/script-smaller-a.py	2012-01-24 19:25:34 UTC (rev 5518)
@@ -1,19 +0,0 @@
-#! /usr/bin/env python
-import os
-
-# FULL for target in range(58,1009,50):
-# FAST for target in range(58,209,50):
-for target in range(58,209,50):
-	s = ("%d" % target)
-	print s
-
-	for i in range(15):
-# FAST	for i in range(2):
-		args="./optimizer 0 0 4 50 -1 "+s+" 40000 20 1000 2 1 2. 0.01 100 0.3 2.3 1 1 0 0 0 # > out.T"+str(target)+".i"+str(i)
-		print("\n\n **** CALLING APP: "+args+"\n\n\n")
-	        os.system(args);
-#		print("\n\n **** CALLING APP: ./optimizer 0 0 4 50 -1 "+s+" 40000 20 10 2 1 2. 0.01 100 0.3 2.3 1 1 0 0 0\n\n\n")
-#		FAST: os.system("./optimizer 0 0 4 50 -1 "+s+" 40000 20 10 2 1 2. 0.01 2 0.3 2.3 1 1 0 0 0")
-
-
-print "Done!"

Copied: SwiftApps/SciColSim/testopt1.py (from rev 5515, SwiftApps/SciColSim/script-smaller-a.py)
===================================================================
--- SwiftApps/SciColSim/testopt1.py	                        (rev 0)
+++ SwiftApps/SciColSim/testopt1.py	2012-01-24 19:25:34 UTC (rev 5518)
@@ -0,0 +1,49 @@
+#! /usr/bin/env python -u
+#                      -u => run unbuffered
+
+import os
+
+app              = "./orig-optimizer";     # For Mac only: original code (+1 loop fix) using Grnd Central Dispatch
+app              = "./dispatch-optimizer"; # For Mac only: sing Grand Central Dispatch
+app              = "./openmp-optimizer";   # For Mac or Linux: Using OpenMP (Default)
+
+app              = "./orig-optimizer";
+
+# FULL INITIAL TEST PARAMETERS from Andrey
+
+startTarget      = 58
+endTarget        = 1009
+incrTarget       = 50
+optimizerRepeats = 15
+evolveReruns     = 1000
+annealingSteps   = 100
+
+# FAST TEST PARAMETERS by Mike
+
+startTarget      = 58
+endTarget        = 209
+incrTarget       = 50
+optimizerRepeats = 2
+evolveReruns     = 10
+annealingSteps   = 2
+
+# extra (string) parameters for openmp- and dispatch- versions only:
+
+NWorkers         = "2"
+operation        = "n"  # n=normal, m=manual (runs 1 multi_loss call)
+
+if app == "./orig-optimizer":
+  NWorkers = ""
+  operation = ""
+
+for target in range(startTarget,endTarget,incrTarget):
+  for i in range(optimizerRepeats):
+    args = app + " 0 0 4 50 -1 " + str(target) + " 40000 20 " + str(evolveReruns) + \
+      " 2 1 2. 0.01 " + str(annealingSteps) + " 0.3 2.3 1 1 0 0 0 " + NWorkers + " " + operation
+    print("\n**** Calling optimizer: "+args+"\n")
+    os.system(args);
+print "Done!"
+
+# From older test, using separate file per run:
+#    args = app + " 0 0 4 50 -1 " + str(target) + " 40000 20 " + str(evolveReruns) + \
+#      " 2 1 2. 0.01 " + str(annealingSteps) + " 0.3 2.3 1 1 0 0 0 > out.T"+str(target)+".i"+str(i)




More information about the Swift-commit mailing list