11 #include "latlib/progress.h"
17 o815::o815(int argc, char **argv, const string& _programid, comoption specOps[], void (*helpHeader)()) {
18 programid = _programid;
25 comargs.obscache = make_pair("",0);
26 comargs.confcache = make_pair("",0);
28 comargs.idonly = false;
29 comargs.showjobnum = false;
30 comargs.startconfig = "";
33 MPI_Init(&argc, &argv);
34 MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
35 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
41 addComOption("lsize", required_argument, NULL, 'L', "define lattice size", "xsize:tsize");
42 addComOption("nmeas", required_argument, NULL, 'N', "set number of measurements", "nmeas");
43 addComOption("skip", required_argument, NULL, 'S', "set number of skips between configs", "nskip");
44 addComOption("nequi", required_argument, NULL, 'E', "set number of equilibrations", "nequi");
45 addComOption("ocache", required_argument, NULL, 'O', "set observable cache (mode=oo|ro|wo|rw)", "dir:mode");
46 addComOption("ccache", required_argument, NULL, 'C', "set config cache (mode=oo|ro|wo|rw)", "dir:mode");
47 addComOption("write", required_argument, NULL, 'W', "data writeout directory", "dir");
48 addComOption("idonly", no_argument, &comargs.idonly, 'I', "show output-id only", "");
49 addComOption("jobnum", no_argument, &comargs.showjobnum, 'J', "show jobnumber only", "");
50 addComOption("startconfig", required_argument, NULL, 'X', "read start config from file", "startconfigfile");
52 vector<comoption>::iterator lastO815Opt = comOptions.end()-1;
55 for (int ispecop = 0; specOps[ispecop].name != ""; ispecop++)
56 comOptions.push_back(specOps[ispecop]);
59 parseArgs(argc, argv, specOps);
61 int longestCombinedLong=0;
63 if (helpHeader != NULL)
66 cout << "General options:" << endl;
68 for (vector<comoption>::iterator opit = comOptions.begin(); opit != comOptions.end(); ++opit) {
69 stringstream combinedLong;
71 combinedLong << opit->name;
72 if (opit->has_arg == required_argument)
73 combinedLong << "=" << opit->argdesc;
74 else if (opit->has_arg == optional_argument)
75 combinedLong << "[=" << opit->argdesc << "]";
77 if (longestCombinedLong < combinedLong.str().size())
78 longestCombinedLong = combinedLong.str().size();
81 for (vector<comoption>::iterator opit = comOptions.begin(); opit != comOptions.end(); ++opit) {
82 stringstream combinedLong;
84 cout << " -" << char(opit->val) << ", --";
86 combinedLong << opit->name;
87 if (opit->has_arg == required_argument)
88 combinedLong << "=" << opit->argdesc;
89 else if (opit->has_arg == optional_argument)
90 combinedLong << "[=" << opit->argdesc << "]";
92 cout << setw(longestCombinedLong) << setiosflags(ios::left) << combinedLong.str();
94 cout << "\t" << opit->optdesc;
98 if ( opit == lastO815Opt && (opit+1) != comOptions.end() ) {
100 cout << "Simulation specific options:" << endl;
106 paraQ = new paraq(numprocs, rank);
108 hypercache::addPara("lx", comargs.lsize[0]);
109 hypercache::addPara("lt", comargs.lsize[1]);
112 void o815::postParaInit() {
116 cout << programid << headMaster(true) << endl << flush;
120 if( comargs.showjobnum ) {
121 for( int i=1; i<=paraQ->getTotalJobs(); i++ ) {
122 if( paraQ->getTotalJobs()%i == 0 ) cout << paraQ->getTotalJobs()/i << "@" << i << " ";
128 out = new writeout(comargs.outdir, programid+headMaster(true), rank, numprocs);
131 void o815::mainLoop() {
132 *out->log << "OBS: Starting main loop." << endl;
134 if ( observables.size() == 0 )
135 *out->log << "O815: Running in 0-observable-mode." << endl;
137 for (vector<obs*>::iterator obsit = observables.begin(); obsit != observables.end(); ++obsit)
140 while( nextParas() ) {
141 *out->log << endl << "O815: < ";
142 for (vector<string>::iterator parait = paraQ->allParaIds.begin(); parait != paraQ->allParaIds.end(); ++parait)
143 *out->log << *parait << "=" << (*paraQ)[*parait] << " ";
144 *out->log << ">" << endl << flush;
150 progress measProg(comargs.nmeas);
152 *out->log << "OBS: Starting measurement of observables:";
153 for (vector<obs*>::iterator obsit = observables.begin(); obsit != observables.end(); ++obsit)
154 *out->log << " " << (*obsit)->obsid;
155 *out->log << endl << flush;
157 for( int imeas=0; imeas<comargs.nmeas; imeas++ ) {
158 bool nextAlready = false;
160 for (vector<obs*>::iterator obsit = observables.begin(); obsit != observables.end(); ++obsit) {
164 hypercache::readO( (*obsit)->ocid, &readnewObs, &nequileftNewObs );
166 if ( readnewObs && nequileftNewObs < 0 ) {
167 (*obsit)->meas(true, imeas);
174 (*obsit)->meas(false, imeas);
175 hypercache::writeO( (*obsit)->ocid );
179 if ( observables.size() == 0 )
182 while( measProg.madeStep(imeas) )
183 *out->log << "O815: " << measProg.done()*100 << "% of measurements done." << endl << flush;
185 for (vector<obs*>::iterator obsit = observables.begin(); obsit != observables.end(); ++obsit)
190 void o815::addComOption(const char* name, int has_arg, int *flag, int val, const char* optdesc, const char* argdesc) {
191 comoption newop = { name, has_arg, flag, val, optdesc, argdesc };
192 comOptions.push_back(newop);
195 void o815::parseArgs(int argc, char **argv, comoption specOps[]) {
198 stringstream optargstr;
199 option allOptions[comOptions.size()+1];
201 for (int iop=0; iop < comOptions.size(); iop++) {
202 allOptions[iop].name = comOptions[iop].name.c_str();
203 allOptions[iop].has_arg = comOptions[iop].has_arg;
204 //allOptions[iop].flag = NULL;
205 allOptions[iop].flag = comOptions[iop].flag;
206 allOptions[iop].val = comOptions[iop].val;
208 optargstr << char(allOptions[iop].val);
209 if (allOptions[iop].has_arg == required_argument)
211 else if (allOptions[iop].has_arg == optional_argument)
215 allOptions[comOptions.size()].name = 0;
216 allOptions[comOptions.size()].has_arg = 0;
217 allOptions[comOptions.size()].flag = 0;
218 allOptions[comOptions.size()].val = 0;
220 while((opt = getopt_long( argc, argv, optargstr.str().c_str(), allOptions, &indexptr )) != -1)
223 listArg(comargs.lsize, 2, optarg);
226 comargs.nmeas = atoi(optarg);
229 comargs.nskip = atoi(optarg);
232 comargs.nequi = atoi(optarg);
235 readCacheArgs(optarg, comargs.obscache.first, comargs.obscache.second);
238 readCacheArgs(optarg, comargs.confcache.first, comargs.confcache.second);
241 comargs.outdir = optarg;
244 comargs.startconfig = optarg;
248 comoption* thisop = getOptionByVal(opt);
249 if (thisop->flag != 0)
250 *thisop->flag = thisop->val;
252 parsedSpecOps.push_back( pair<int,char*>(thisop->val, optarg) );
257 for (int ilon=0; optind+ilon < argc; ilon++)
258 lonelyArgs.push_back(argv[optind+ilon]);
261 o815::comoption* o815::getOptionByVal(int val) {
262 for (vector<comoption>::iterator opit = comOptions.begin(); opit != comOptions.end(); ++opit)
263 if ( opit->val == val )
268 void o815::listArg(int *target, int tlen, char *listarg) {
271 for( int pos=0; pos<strlen(listarg); pos++ )
272 if( listarg[pos] == ':' ) nargs++;
275 for(int i=0; i<tlen; i++) target[i] = atoi(listarg);
278 target[0] = atoi(strtok(listarg, ":"));
279 for(int i=0; i<nargs; i++)
280 target[i+1] = atoi(strtok(NULL, ":"));
284 string o815::headMaster(bool hashedrange)
288 hm << "_L" << comargs.lsize[0] << "x" << comargs.lsize[1] << "_E" << comargs.nequi << "_S" << comargs.nskip << "_N" << comargs.nmeas;
291 hm << paraQ->rangeString();
293 hm << "_" << hash( paraQ->rangeString() );
299 if(comargs.outdir=="") {
301 MPI_Barrier(MPI_COMM_WORLD);
304 cout << "#end" << endl << flush;
306 hypercache::finalize();
313 int o815::nextParas()
315 if( paraQ->nextParas() ) {
316 for (vector<string>::iterator parait = paraQ->allParaIds.begin(); parait != paraQ->allParaIds.end(); ++parait)
317 hypercache::setPara(*parait, (*paraQ)[*parait]);
324 void o815::addPara(const string& paraid, const double& paraDefault) {
325 hypercache::addPara(paraid);
326 paraQ->setDefault(paraid, paraDefault);
329 void o815::readCacheArgs(const string& arg, string& cachedir, int& cachemode)
331 if ( arg.rfind(":") == string::npos ) {
332 cerr << "O815: Invalid cache argument!" << endl;
336 string arg_mode = arg.substr( arg.rfind(":")+1 );
337 if ( arg_mode == "oo" )
338 cachemode = CACHE_MODE_OO;
339 else if ( arg_mode == "ro" )
340 cachemode = CACHE_MODE_RO;
341 else if ( arg_mode == "wo" )
342 cachemode = CACHE_MODE_WO;
343 else if ( arg_mode == "rw" )
344 cachemode = CACHE_MODE_RW;
346 cerr << "O815: Unknown cache mode " << arg_mode << "!" << endl;
350 cachedir = arg.substr( 0, arg.rfind(":") );
353 unsigned long o815::hash(const string& str)
355 unsigned long hash = 5381;
357 for(string::const_iterator it=str.begin();it!=str.end();it++)
358 hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */