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