]> git.treefish.org Git - phys/latlib.git/blob - o815/o815.cpp
Fixed writeout option.
[phys/latlib.git] / o815 / o815.cpp
1 #include "o815.h"
2
3 #include <sstream>
4 #include <iomanip>
5 #include <cstring>
6
7 #ifndef MPI_DISABLED
8 #include <mpi.h>
9 #endif
10
11 #include "latlib/progress.h"
12
13 using namespace std;
14
15 extern int opterr;
16
17 o815::o815(int argc, char **argv, const string& _programid, comoption specOps[], void (*helpHeader)()) {
18   programid = _programid;
19
20   comargs.nmeas = 100;
21   comargs.nskip = 10;
22   comargs.nequi = 100;
23   comargs.lsize[0] = 4;
24   comargs.lsize[1] = 4;
25   comargs.obscache = make_pair("",0);
26   comargs.confcache = make_pair("",0);
27   comargs.outdir="";
28   comargs.idonly = false;
29   comargs.showjobnum = false;
30
31 #ifndef MPI_DISABLED
32   MPI_Init(&argc, &argv);
33   MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
34   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
35 #else
36   numprocs = 1;
37   rank = 0;
38 #endif
39
40   addComOption("lsize",  required_argument, NULL,                'L', "define lattice size",                     "xsize:tsize");
41   addComOption("nmeas",  required_argument, NULL,                'N', "set number of measurements",              "nmeas");
42   addComOption("skip",   required_argument, NULL,                'S', "set number of skips between configs",     "nskip");
43   addComOption("nequi",  required_argument, NULL,                'E', "set number of equilibrations",            "nequi");
44   addComOption("ocache", required_argument, NULL,                'O', "set observable cache (mode=oo|ro|wo|rw)", "dir:mode");
45   addComOption("ccache", required_argument, NULL,                'C', "set config cache (mode=oo|ro|wo|rw)",     "dir:mode");
46   addComOption("write",  required_argument, NULL,                'W', "data writeout directory",                 "dir");
47   addComOption("idonly", no_argument,       &comargs.idonly,     'I', "show output-id only",                     "");
48   addComOption("jobnum", no_argument,       &comargs.showjobnum, 'J', "show jobnumber only",                     "");
49
50   vector<comoption>::iterator lastO815Opt = comOptions.end()-1;
51
52   if (specOps != NULL)
53     for (int ispecop = 0; specOps[ispecop].name != ""; ispecop++)
54       comOptions.push_back(specOps[ispecop]);
55
56   if (argc > 1)
57     parseArgs(argc, argv, specOps);
58   else {
59     int longestCombinedLong=0;
60     
61     if (helpHeader != NULL)
62       helpHeader();
63
64     cout << "General options:" << endl;
65
66     for (vector<comoption>::iterator opit = comOptions.begin(); opit != comOptions.end(); ++opit) {
67       stringstream combinedLong;
68
69       combinedLong << opit->name;
70       if (opit->has_arg == required_argument)
71         combinedLong << "=" << opit->argdesc;
72       else if (opit->has_arg == optional_argument)
73         combinedLong << "[=" << opit->argdesc << "]";
74
75       if (longestCombinedLong < combinedLong.str().size())
76         longestCombinedLong = combinedLong.str().size();
77     }
78
79     for (vector<comoption>::iterator opit = comOptions.begin(); opit != comOptions.end(); ++opit) {
80       stringstream combinedLong;
81
82       cout << "  -" << char(opit->val) << ", --";
83
84       combinedLong << opit->name;
85       if (opit->has_arg == required_argument)
86         combinedLong << "=" << opit->argdesc;
87       else if (opit->has_arg == optional_argument)
88         combinedLong << "[=" << opit->argdesc << "]";
89
90       cout << setw(longestCombinedLong) << setiosflags(ios::left) << combinedLong.str();
91
92       cout << "\t" << opit->optdesc;
93       
94       cout << endl;
95
96       if ( opit == lastO815Opt && (opit+1) != comOptions.end() ) {
97         cout << endl;
98         cout << "Simulation specific options:" << endl;
99       }
100     }
101     exit(0);
102   }
103
104   paraQ = new paraq(numprocs, rank);
105
106   hypercache::addPara("lx", comargs.lsize[0]);
107   hypercache::addPara("lt", comargs.lsize[1]);
108 }
109
110 void o815::postParaInit() {
111   long timestamp;
112
113   if(comargs.idonly) {
114     cout << programid << headMaster() << endl << flush;
115     exit(0);
116   }
117
118   if( comargs.showjobnum ) {
119     for( int i=1; i<=paraQ->getTotalJobs(); i++ ) {
120       if( paraQ->getTotalJobs()%i == 0 ) cout << paraQ->getTotalJobs()/i << "@" << i << " ";
121     }
122     cout << endl;
123     exit(0);
124   }
125   
126   out = new writeout(comargs.outdir, programid+headMaster(), rank, numprocs);
127 }
128
129 void o815::mainLoop() {
130   *out->log << "OBS: Starting main loop." << endl;
131
132   if ( observables.size() == 0 )
133     *out->log << "O815: Running in 0-observable-mode." << endl;
134
135   for (vector<obs*>::iterator obsit = observables.begin(); obsit != observables.end(); ++obsit)
136     (*obsit)->start();
137
138   while( nextParas() ) {
139     *out->log << endl << "O815: < ";
140     for (vector<string>::iterator parait = paraQ->allParaIds.begin(); parait != paraQ->allParaIds.end(); ++parait)
141       *out->log << *parait << "=" << (*paraQ)[*parait] << " ";
142     *out->log << ">" << endl << flush;
143
144     Sim->_newParas();
145     
146     progress measProg(comargs.nmeas);
147
148     *out->log << "OBS: Starting measurement of observables:";
149     for (vector<obs*>::iterator obsit = observables.begin(); obsit != observables.end(); ++obsit)
150       *out->log << " " << (*obsit)->obsid;
151     *out->log << endl << flush;
152
153     for( int imeas=0; imeas<comargs.nmeas; imeas++ ) {
154       bool nextAlready = false;
155
156       for (vector<obs*>::iterator obsit = observables.begin(); obsit != observables.end(); ++obsit) {
157         bool readnewObs;
158         int nequileftNewObs;
159         
160         hypercache::readO( (*obsit)->ocid, &readnewObs, &nequileftNewObs );
161
162         if ( readnewObs && nequileftNewObs < 0 ) {
163           (*obsit)->meas(true, imeas);
164         }
165         else {
166           if (!nextAlready) {
167             Sim->nextConfig();
168             nextAlready = true;
169           }
170           (*obsit)->meas(false, imeas);
171           hypercache::writeO( (*obsit)->ocid );
172         }         
173       }
174
175       if ( observables.size() == 0 )
176         Sim->nextConfig();
177
178       while( measProg.madeStep(imeas) ) 
179         *out->log << "O815: " << measProg.done()*100 << "% of measurements done." << endl << flush;
180     }
181     for (vector<obs*>::iterator obsit = observables.begin(); obsit != observables.end(); ++obsit)
182       (*obsit)->finish();
183   }
184 }
185
186 void o815::addComOption(const char* name, int has_arg, int *flag, int val, const char* optdesc, const char* argdesc) {
187   comoption newop = { name, has_arg, flag, val, optdesc, argdesc };
188   comOptions.push_back(newop);
189 }
190
191 void o815::parseArgs(int argc, char **argv, comoption specOps[]) {
192   int opt;
193   int indexptr=0;
194   stringstream optargstr;
195   option allOptions[comOptions.size()+1];
196
197   for (int iop=0; iop < comOptions.size(); iop++) {
198     allOptions[iop].name = comOptions[iop].name.c_str();
199     allOptions[iop].has_arg = comOptions[iop].has_arg;
200     //allOptions[iop].flag = NULL;
201     allOptions[iop].flag = comOptions[iop].flag;
202     allOptions[iop].val = comOptions[iop].val;
203
204     optargstr << char(allOptions[iop].val);
205     if (allOptions[iop].has_arg == required_argument)
206       optargstr << ":";
207     else if (allOptions[iop].has_arg == optional_argument)
208       optargstr << "::";
209   }
210
211   allOptions[comOptions.size()].name = 0;
212   allOptions[comOptions.size()].has_arg = 0;
213   allOptions[comOptions.size()].flag = 0;
214   allOptions[comOptions.size()].val = 0;
215
216   while((opt = getopt_long( argc, argv, optargstr.str().c_str(), allOptions, &indexptr )) != -1)
217     switch(opt) {
218     case 'L':
219       listArg(comargs.lsize, 2, optarg);
220       break;
221     case 'N':
222       comargs.nmeas = atoi(optarg);
223       break;
224     case 'S':
225       comargs.nskip = atoi(optarg);
226       break;
227     case 'E':
228       comargs.nequi = atoi(optarg);
229       break;
230     case 'O':
231       readCacheArgs(optarg, comargs.obscache.first, comargs.obscache.second);
232       break;
233     case 'C':
234       readCacheArgs(optarg, comargs.confcache.first, comargs.confcache.second);
235       break;
236     case 'W':
237       comargs.outdir = optarg;
238       break;
239     default:
240       if ( opt != 0) {
241         comoption* thisop = getOptionByVal(opt);
242         if (thisop->flag != 0)
243           *thisop->flag = thisop->val;
244         else
245           parsedSpecOps.push_back( pair<int,char*>(thisop->val, optarg) );
246       }
247       break;
248     }
249
250   for (int ilon=0; optind+ilon < argc; ilon++)
251     lonelyArgs.push_back(argv[optind+ilon]);
252 }
253
254 o815::comoption* o815::getOptionByVal(int val) {
255   for (vector<comoption>::iterator opit = comOptions.begin(); opit != comOptions.end(); ++opit)
256     if ( opit->val == val )
257       return &(*opit);
258   exit(1);
259 }
260
261 void o815::listArg(int *target, int tlen, char *listarg) {
262   int nargs=0;
263   
264   for( int pos=0; pos<strlen(listarg); pos++ ) 
265     if( listarg[pos] == ':' ) nargs++;
266   
267   if(nargs==0) 
268     for(int i=0; i<tlen; i++) target[i] = atoi(listarg);
269   else
270     {
271       target[0] = atoi(strtok(listarg, ":"));
272       for(int i=0; i<nargs; i++)
273         target[i+1] = atoi(strtok(NULL, ":"));
274     }
275 }
276
277 string o815::headMaster()
278 {
279   stringstream hm;
280   
281   hm << "-L" << comargs.lsize[0] << "_" << comargs.lsize[1] << "-E" << comargs.nequi << "-S" << comargs.nskip << "-N" << comargs.nmeas 
282      << paraQ->rangeString();
283   
284   return hm.str();
285 }
286
287 o815::~o815() {
288   if(comargs.outdir=="") {
289 #ifndef MPI_DISABLED
290     MPI_Barrier(MPI_COMM_WORLD);
291 #endif
292     if(rank==0)
293       cout << "#end" << endl << flush;
294   }
295   hypercache::finalize();
296   delete out;
297 #ifndef MPI_DISABLED
298   MPI_Finalize();
299 #endif
300 }
301
302 int o815::nextParas()
303 {
304   if( paraQ->nextParas() ) {
305     for (vector<string>::iterator parait = paraQ->allParaIds.begin(); parait != paraQ->allParaIds.end(); ++parait)
306       hypercache::setPara(*parait, (*paraQ)[*parait]);
307     return 1;
308   }
309   else
310     return 0;
311 }
312
313 void o815::addPara(const string& paraid, const double& paraDefault) {
314   hypercache::addPara(paraid);
315   paraQ->setDefault(paraid, paraDefault);
316 }
317
318 void o815::readCacheArgs(const string& arg, string& cachedir, int& cachemode)
319 {
320   if ( arg.rfind(":") == string::npos ) {
321     cerr << "O815: Invalid cache argument!" << endl;
322     exit(1);
323   }
324
325   string arg_mode = arg.substr( arg.rfind(":")+1 );
326   if ( arg_mode == "oo" )
327     cachemode = CACHE_MODE_OO;
328   else if ( arg_mode == "ro" )
329     cachemode = CACHE_MODE_RO;
330   else if ( arg_mode == "wo" )
331     cachemode = CACHE_MODE_WO;
332   else if ( arg_mode == "rw" )
333     cachemode = CACHE_MODE_RW;
334   else {
335     cerr << "O815: Unknown cache mode " << arg_mode << "!" << endl;
336     exit(1);
337   }
338
339   cachedir = arg.substr( 0, arg.rfind(":") );
340 }