]> git.treefish.org Git - phys/latlib.git/blob - o815/o815.cpp
improved argument handling.
[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     progress measProg(comargs.nmeas);
130
131     *out->log << "OBS: Starting measurement of observables:";
132     for (vector<obs*>::iterator obsit = observables.begin(); obsit != observables.end(); ++obsit)
133       *out->log << " " << (*obsit)->obsid;
134     *out->log << endl << flush;
135
136     Sim->toEquilibrate = true;
137
138     for( int imeas=0; imeas<comargs.nmeas; imeas++ ) {
139       bool nextAlready = false;
140       for (vector<obs*>::iterator obsit = observables.begin(); obsit != observables.end(); ++obsit) {
141         if( hypercache::readO( (*obsit)->ocid ) != -1 ) {
142           if (!nextAlready) {
143             Sim->nextConfig();
144             nextAlready = true;
145           }
146           (*obsit)->meas(false);
147           hypercache::writeO( (*obsit)->ocid );
148         }
149         else
150           (*obsit)->meas(true);
151       }
152       while( measProg.madeStep(imeas) ) 
153         *out->log << "O815: " << measProg.done()*100 << "% of measurements done." << endl << flush;
154     }
155     for (vector<obs*>::iterator obsit = observables.begin(); obsit != observables.end(); ++obsit)
156       (*obsit)->finish();
157   }
158 }
159
160 void o815::addComOption(const char* name, int has_arg, int *flag, int val, const char* optdesc, const char* argdesc) {
161   comoption newop = { name, has_arg, flag, val, optdesc, argdesc };
162   comOptions.push_back(newop);
163 }
164
165 void o815::parseArgs(int argc, char **argv, comoption specOps[]) {
166   int opt;
167   int indexptr=0;
168   stringstream optargstr;
169   option allOptions[comOptions.size()+1];
170
171   for (int iop=0; iop < comOptions.size(); iop++) {
172     allOptions[iop].name = comOptions[iop].name.c_str();
173     allOptions[iop].has_arg = comOptions[iop].has_arg;
174     //allOptions[iop].flag = NULL;
175     allOptions[iop].flag = comOptions[iop].flag;
176     allOptions[iop].val = comOptions[iop].val;
177
178     optargstr << char(allOptions[iop].val);
179     if (allOptions[iop].has_arg == required_argument)
180       optargstr << ":";
181     else if (allOptions[iop].has_arg == optional_argument)
182       optargstr << "::";
183   }
184
185   allOptions[comOptions.size()].name = 0;
186   allOptions[comOptions.size()].has_arg = 0;
187   allOptions[comOptions.size()].flag = 0;
188   allOptions[comOptions.size()].val = 0;
189
190   while((opt = getopt_long( argc, argv, optargstr.str().c_str(), allOptions, &indexptr )) != -1)
191     switch(opt) {
192     case 'L':
193       listArg(comargs.lsize, 2, optarg);
194       break;
195     case 'N':
196       comargs.nmeas = atoi(optarg);
197       break;
198     case 'S':
199       comargs.nskip = atoi(optarg);
200       break;
201     case 'E':
202       comargs.nequi = atoi(optarg);
203       break;
204     case 'o':
205       comargs.obscache.first = optarg;
206       comargs.obscache.second = 1;
207       break;
208     case 'O':
209       comargs.obscache.first = optarg;
210       comargs.obscache.second = 2;
211       break;
212     case 'c':
213       comargs.confcache.first = optarg;
214       comargs.confcache.second = 1;
215       break;
216     case 'C':
217       comargs.confcache.first = optarg;
218       comargs.confcache.second = 2;
219       break;
220     case 'w':
221       comargs.outdir = optarg;
222       break;
223     default:
224       if ( opt != 0) {
225         comoption* thisop = getOptionByVal(opt);
226         if (thisop->flag != 0)
227           *thisop->flag = thisop->val;
228         else
229           parsedSpecOps.push_back( pair<int,char*>(thisop->val, optarg) );
230       }
231       break;
232     }
233
234   for (int ilon=0; optind+ilon < argc; ilon++)
235     lonelyArgs.push_back(argv[optind+ilon]);
236 }
237
238 o815::comoption* o815::getOptionByVal(int val) {
239   for (vector<comoption>::iterator opit = comOptions.begin(); opit != comOptions.end(); ++opit)
240     if ( opit->val == val )
241       return &(*opit);
242   return NULL;
243 }
244
245 void o815::listArg(int *target, int tlen, char *listarg) {
246   int nargs=0;
247   
248   for( int pos=0; pos<strlen(listarg); pos++ ) 
249     if( listarg[pos] == ':' ) nargs++;
250   
251   if(nargs==0) 
252     for(int i=0; i<tlen; i++) target[i] = atoi(listarg);
253   else
254     {
255       target[0] = atoi(strtok(listarg, ":"));
256       for(int i=0; i<nargs; i++)
257         target[i+1] = atoi(strtok(NULL, ":"));
258     }
259 }
260
261 string o815::headMaster()
262 {
263   stringstream hm;
264   
265   hm << "-L" << comargs.lsize[0] << "_" << comargs.lsize[1] << "-E" << comargs.nequi << "-S" << comargs.nskip << "-N" << comargs.nmeas 
266      << paraQ->rangeString();
267   
268   return hm.str();
269 }
270
271 o815::~o815() {
272   if(comargs.outdir=="") {
273     MPI_Barrier(MPI_COMM_WORLD);
274     if(rank==0)
275       cout << "#end" << endl << flush;
276   }
277   hypercache::finalize();
278   delete out;
279   MPI_Finalize();
280 }
281
282 int o815::nextParas()
283 {
284   if( paraQ->nextParas() ) {
285     for (vector<string>::iterator parait = paraQ->allParaIds.begin(); parait != paraQ->allParaIds.end(); ++parait)
286       hypercache::setPara(*parait, (*paraQ)[*parait]);
287     return 1;
288   }
289   else
290     return 0;
291 }
292
293 void o815::addPara(const string& paraid, const double& paraDefault) {
294   hypercache::addPara(paraid);
295   paraQ->setDefault(paraid, paraDefault);
296 }