]> git.treefish.org Git - phys/latlib.git/blobdiff - o815/o815.cpp
Added resetconfig routine.
[phys/latlib.git] / o815 / o815.cpp
index 8700f675e394ef4d7bd8b20ca7dc4ebd9c703bf6..36d9fb00877098956293a61945fc7bf627eddf35 100644 (file)
@@ -2,6 +2,11 @@
 
 #include <sstream>
 #include <iomanip>
+#include <cstring>
+
+#ifndef MPI_DISABLED
+#include <mpi.h>
+#endif
 
 #include "latlib/progress.h"
 
@@ -10,8 +15,6 @@ using namespace std;
 extern int opterr;
 
 o815::o815(int argc, char **argv, const string& _programid, comoption specOps[], void (*helpHeader)()) {
-  long timestamp;
-
   programid = _programid;
 
   comargs.nmeas = 100;
@@ -24,24 +27,29 @@ o815::o815(int argc, char **argv, const string& _programid, comoption specOps[],
   comargs.outdir="";
   comargs.idonly = false;
   comargs.showjobnum = false;
-
+  comargs.startconfig = "";
+  
+#ifndef MPI_DISABLED
   MPI_Init(&argc, &argv);
   MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
-
-  paraQ = new paraq(numprocs, rank);
-
-  addComOption("lsize",  required_argument, NULL,                'L', "define lattice size",                   "xsize:tsize");
-  addComOption("nmeas",  required_argument, NULL,                'N', "set number of measurements",            "nmeas");
-  addComOption("skip",   required_argument, NULL,                'S', "set number of skips between configs",   "nskip");
-  addComOption("nequi",  required_argument, NULL,                'E', "set number of equilibrations",          "nequi");
-  addComOption("oro",    required_argument, NULL,                'o', "read-only observable-store directory",  "dir");
-  addComOption("orw",    required_argument, NULL,                'O', "read-write observable-store directory", "dir");
-  addComOption("cro",    required_argument, NULL,                'c', "read-only config-store directory",      "dir");
-  addComOption("crw",    required_argument, NULL,                'C', "read-write config-store directory",     "dir");
-  addComOption("write",  required_argument, NULL,                'w', "data writeout directory",               "dir");
-  addComOption("idonly", no_argument,       &comargs.idonly,     'i', "show output-id only",                   "");
-  addComOption("jobnum", no_argument,       &comargs.showjobnum, 'j', "show jobnumber only",                   "");
+#else
+  numprocs = 1;
+  rank = 0;
+#endif
+
+  addComOption("lsize",       required_argument, NULL,                'L', "define lattice size",                     "xsize:tsize");
+  addComOption("nmeas",       required_argument, NULL,                'N', "set number of measurements",              "nmeas");
+  addComOption("skip",        required_argument, NULL,                'S', "set number of skips between configs",     "nskip");
+  addComOption("nequi",       required_argument, NULL,                'E', "set number of equilibrations",            "nequi");
+  addComOption("ocache",      required_argument, NULL,                'O', "set observable cache (mode=oo|ro|wo|rw)", "dir:mode");
+  addComOption("ccache",      required_argument, NULL,                'C', "set config cache (mode=oo|ro|wo|rw)",     "dir:mode");
+  addComOption("write",       required_argument, NULL,                'W', "data writeout directory",                 "dir");
+  addComOption("idonly",      no_argument,       &comargs.idonly,     'I', "show output-id only",                     "");
+  addComOption("jobnum",      no_argument,       &comargs.showjobnum, 'J', "show jobnumber only",                     "");
+  addComOption("startconfig", required_argument, NULL,                'X', "read start config from file",             "startconfigfile");
+  
+  vector<comoption>::iterator lastO815Opt = comOptions.end()-1;
 
   if (specOps != NULL)
     for (int ispecop = 0; specOps[ispecop].name != ""; ispecop++)
@@ -55,7 +63,7 @@ o815::o815(int argc, char **argv, const string& _programid, comoption specOps[],
     if (helpHeader != NULL)
       helpHeader();
 
-    cout << "Options:" << endl;
+    cout << "General options:" << endl;
 
     for (vector<comoption>::iterator opit = comOptions.begin(); opit != comOptions.end(); ++opit) {
       stringstream combinedLong;
@@ -86,27 +94,29 @@ o815::o815(int argc, char **argv, const string& _programid, comoption specOps[],
       cout << "\t" << opit->optdesc;
       
       cout << endl;
+
+      if ( opit == lastO815Opt && (opit+1) != comOptions.end() ) {
+       cout << endl;
+       cout << "Simulation specific options:" << endl;
+      }
     }
     exit(0);
   }
 
+  paraQ = new paraq(numprocs, rank);
+
+  hypercache::addPara("lx", comargs.lsize[0]);
+  hypercache::addPara("lt", comargs.lsize[1]);
+}
+
+void o815::postParaInit() {
+  long timestamp;
+
   if(comargs.idonly) {
-    cout << programid << headMaster() << endl << flush;
+    cout << programid << headMaster(true) << endl << flush;
     exit(0);
   }
 
-  if(rank==0) {
-    timestamp = time(NULL);
-    for(int idest=1; idest<numprocs; idest++)
-      MPI_Send(&timestamp, 1, MPI_LONG, idest, 123, MPI_COMM_WORLD);
-  }
-  else if(rank>0)
-    MPI_Recv(&timestamp, 1, MPI_LONG, 0, 123, MPI_COMM_WORLD, &mpiStatus);
-  
-  out = new writeout(comargs.outdir, programid+headMaster(), rank, numprocs, timestamp);
-}
-
-void o815::mainLoop() {
   if( comargs.showjobnum ) {
     for( int i=1; i<=paraQ->getTotalJobs(); i++ ) {
       if( paraQ->getTotalJobs()%i == 0 ) cout << paraQ->getTotalJobs()/i << "@" << i << " ";
@@ -114,9 +124,16 @@ void o815::mainLoop() {
     cout << endl;
     exit(0);
   }
+  
+  out = new writeout(comargs.outdir, programid+headMaster(true), rank, numprocs);
+}
 
+void o815::mainLoop() {
   *out->log << "OBS: Starting main loop." << endl;
 
+  if ( observables.size() == 0 )
+    *out->log << "O815: Running in 0-observable-mode." << endl;
+
   for (vector<obs*>::iterator obsit = observables.begin(); obsit != observables.end(); ++obsit)
     (*obsit)->start();
 
@@ -125,6 +142,10 @@ void o815::mainLoop() {
     for (vector<string>::iterator parait = paraQ->allParaIds.begin(); parait != paraQ->allParaIds.end(); ++parait)
       *out->log << *parait << "=" << (*paraQ)[*parait] << " ";
     *out->log << ">" << endl << flush;
+
+    Sim->_newParas();
+
+    Sim->_resetConfig();
     
     progress measProg(comargs.nmeas);
 
@@ -133,22 +154,31 @@ void o815::mainLoop() {
       *out->log << " " << (*obsit)->obsid;
     *out->log << endl << flush;
 
-    Sim->toEquilibrate = true;
-
     for( int imeas=0; imeas<comargs.nmeas; imeas++ ) {
       bool nextAlready = false;
+
       for (vector<obs*>::iterator obsit = observables.begin(); obsit != observables.end(); ++obsit) {
-       if( hypercache::readO( (*obsit)->ocid ) != -1 ) {
+       bool readnewObs;
+       int nequileftNewObs;
+       
+       hypercache::readO( (*obsit)->ocid, &readnewObs, &nequileftNewObs );
+
+       if ( readnewObs && nequileftNewObs < 0 ) {
+         (*obsit)->meas(true, imeas);
+       }
+       else {
          if (!nextAlready) {
            Sim->nextConfig();
            nextAlready = true;
          }
-         (*obsit)->meas(false);
+         (*obsit)->meas(false, imeas);
          hypercache::writeO( (*obsit)->ocid );
-       }
-       else
-         (*obsit)->meas(true);
+       }         
       }
+
+      if ( observables.size() == 0 )
+       Sim->nextConfig();
+
       while( measProg.madeStep(imeas) ) 
        *out->log << "O815: " << measProg.done()*100 << "% of measurements done." << endl << flush;
     }
@@ -201,25 +231,18 @@ void o815::parseArgs(int argc, char **argv, comoption specOps[]) {
     case 'E':
       comargs.nequi = atoi(optarg);
       break;
-    case 'o':
-      comargs.obscache.first = optarg;
-      comargs.obscache.second = 1;
-      break;
     case 'O':
-      comargs.obscache.first = optarg;
-      comargs.obscache.second = 2;
-      break;
-    case 'c':
-      comargs.confcache.first = optarg;
-      comargs.confcache.second = 1;
+      readCacheArgs(optarg, comargs.obscache.first, comargs.obscache.second);
       break;
     case 'C':
-      comargs.confcache.first = optarg;
-      comargs.confcache.second = 2;
+      readCacheArgs(optarg, comargs.confcache.first, comargs.confcache.second);
       break;
-    case 'w':
+    case 'W':
       comargs.outdir = optarg;
       break;
+    case 'X':
+      comargs.startconfig = optarg;
+      break;
     default:
       if ( opt != 0) {
        comoption* thisop = getOptionByVal(opt);
@@ -258,25 +281,33 @@ void o815::listArg(int *target, int tlen, char *listarg) {
     }
 }
 
-string o815::headMaster()
+string o815::headMaster(bool hashedrange)
 {
   stringstream hm;
   
-  hm << "-L" << comargs.lsize[0] << "_" << comargs.lsize[1] << "-E" << comargs.nequi << "-S" << comargs.nskip << "-N" << comargs.nmeas 
-     << paraQ->rangeString();
+  hm << "_L" << comargs.lsize[0] << "x" << comargs.lsize[1] << "_E" << comargs.nequi << "_S" << comargs.nskip << "_N" << comargs.nmeas;
+
+  if( ! hashedrange )
+    hm << paraQ->rangeString();
+  else
+    hm << "_" << hash( paraQ->rangeString() );
   
   return hm.str();
 }
 
 o815::~o815() {
   if(comargs.outdir=="") {
+#ifndef MPI_DISABLED
     MPI_Barrier(MPI_COMM_WORLD);
+#endif
     if(rank==0)
       cout << "#end" << endl << flush;
   }
   hypercache::finalize();
   delete out;
+#ifndef MPI_DISABLED
   MPI_Finalize();
+#endif
 }
 
 int o815::nextParas()
@@ -294,3 +325,37 @@ void o815::addPara(const string& paraid, const double& paraDefault) {
   hypercache::addPara(paraid);
   paraQ->setDefault(paraid, paraDefault);
 }
+
+void o815::readCacheArgs(const string& arg, string& cachedir, int& cachemode)
+{
+  if ( arg.rfind(":") == string::npos ) {
+    cerr << "O815: Invalid cache argument!" << endl;
+    exit(1);
+  }
+
+  string arg_mode = arg.substr( arg.rfind(":")+1 );
+  if ( arg_mode == "oo" )
+    cachemode = CACHE_MODE_OO;
+  else if ( arg_mode == "ro" )
+    cachemode = CACHE_MODE_RO;
+  else if ( arg_mode == "wo" )
+    cachemode = CACHE_MODE_WO;
+  else if ( arg_mode == "rw" )
+    cachemode = CACHE_MODE_RW;
+  else {
+    cerr << "O815: Unknown cache mode " << arg_mode << "!" << endl;
+    exit(1);
+  }
+
+  cachedir = arg.substr( 0, arg.rfind(":") );
+}
+
+unsigned long o815::hash(const string& str)
+{
+  unsigned long hash = 5381;
+
+  for(string::const_iterator it=str.begin();it!=str.end();it++) 
+    hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */
+
+  return hash;
+}