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