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