-
Notifications
You must be signed in to change notification settings - Fork 3
/
detqmc.h
537 lines (450 loc) · 19.3 KB
/
detqmc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. See the enclosed file LICENSE for a copy or if
* that was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright 2017 Max H. Gerlach
*
* */
/*
* detqmc.h
*
* Handling of determinantal quantum Monte Carlo simulations
*
* Created on: Dec 10, 2012
* Author: gerlach
*/
#ifndef DETQMC_H_
#define DETQMC_H_
#include <vector>
#include <functional>
#include <memory>
#include <cstdlib>
#include <limits>
#include <ctime>
#include <functional>
#include <fstream>
#include <armadillo>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#pragma GCC diagnostic ignored "-Wshadow"
#include "boost/preprocessor/comma.hpp"
#include "boost/timer/timer.hpp"
#include "boost/serialization/split_member.hpp"
#include "boost/assign/std/vector.hpp"
#include "boost/filesystem.hpp"
#include "boost/archive/binary_oarchive.hpp"
#include "boost/archive/binary_iarchive.hpp"
#pragma GCC diagnostic pop
#include "metadata.h"
#include "detqmcparams.h"
#include "detmodelloggingparams.h"
#include "detmodelparams.h"
#include "detmodel.h"
#include "observablehandler.h"
#include "rngwrapper.h"
#include "exceptions.h"
#include "tools.h"
#include "git-revision.h"
#include "timing.h"
// Class handling the simulation
template<class Model, class ModelParams = ModelParams<Model> >
class DetQMC {
public:
//constructor to init a new simulation:
DetQMC(const ModelParams& parsmodel, const DetQMCParams& parsmc,
const DetModelLoggingParams& loggingParams = DetModelLoggingParams());
//constructor to resume a simulation from a dumped state file:
//we allow to change some MC parameters at this point:
// sweeps & saveInterval
//if values > than the old values are specified, change them
DetQMC(const std::string& stateFileName, const DetQMCParams& newParsmc);
//carry out simulation determined by parsmc given in construction,
//- handle thermalization & measurement stages as necessary
//- save state and results periodically
//- if granted walltime is almost over, save state & results
// and exit gracefully
void run();
// update results stored on disk
void saveResults();
// dump simulation parameters and the current state to a Boost::S11n archive,
// also write out information about the current simulation state to info.dat
void saveState();
virtual ~DetQMC();
protected:
//helper for constructors -- set all parameters and initialize contained objects
void initFromParameters(const ModelParams& parsmodel, const DetQMCParams& parsmc,
const DetModelLoggingParams& loggingParams = DetModelLoggingParams());
ModelParams parsmodel;
DetQMCParams parsmc;
DetModelLoggingParams parslogging;
typedef DetQMCParams::GreenUpdateType GreenUpdateType;
MetadataMap modelMeta;
MetadataMap mcMeta;
RngWrapper rng;
std::unique_ptr<Model> replica;
typedef std::unique_ptr<ScalarObservableHandler> ObsPtr;
typedef std::unique_ptr<VectorObservableHandler> VecObsPtr;
std::vector<ObsPtr> obsHandlers;
std::vector<VecObsPtr> vecObsHandlers; //need to be pointers: holds both KeyValueObservableHandlers and VectorObservableHandlers
uint32_t sweepsDone; //Measurement sweeps done
uint32_t sweepsDoneThermalization; //thermalization sweeps done
uint32_t swCounter; //helper counter in run() -- e.g. sweeps between measurements -- should also be serialized
boost::timer::cpu_timer elapsedTimer; //during this simulation run
uint32_t curWalltimeSecs() {
return static_cast<uint32_t>(elapsedTimer.elapsed().wall / 1000 / 1000 / 1000); // ns->mus->ms->s
}
uint32_t totalWalltimeSecs; //this is serialized and carries the elapsed walltime in seconds
//accumulated over all runs, updated on call of saveResults()
uint32_t walltimeSecsLastSaveResults; //timer seconds at previous saveResults() call --> used to update totalWalltimeSecs
uint32_t grantedWalltimeSecs; //walltime the simulation is allowed to run
std::string jobid; //id string from the job scheduling system, or "nojobid"
private:
//Serialize only the content data that has changed after construction.
//Only call for deserialization after DetQMC has already been constructed and initialized!
//separate functions loadContents, saveContents; both employ serializeContentsCommon
template<class Archive>
void loadContents(Archive& ar) {
serializeContentsCommon(ar);
replica->loadContents(ar);
}
template<class Archive>
void saveContents(Archive& ar) {
serializeContentsCommon(ar);
replica->saveContents(ar);
}
template<class Archive>
void serializeContentsCommon(Archive& ar) {
ar & rng; //serialize completely
for (auto p = obsHandlers.begin(); p != obsHandlers.end(); ++p) {
//ATM no further derived classes of ScalarObservableHandler have a method serializeContents
(*p)->serializeContents(ar);
}
for (auto p = vecObsHandlers.begin(); p != vecObsHandlers.end(); ++p) {
//ATM no further derived classes of VectorObservableHandler have a method serializeContents
(*p)->serializeContents(ar);
}
ar & sweepsDone & sweepsDoneThermalization;
ar & swCounter;
ar & totalWalltimeSecs;
}
};
template<class Model, class ModelParams>
void DetQMC<Model,ModelParams>::initFromParameters(const ModelParams& parsmodel_, const DetQMCParams& parsmc_,
const DetModelLoggingParams& loggingParams /*default argument*/) {
parsmodel = parsmodel_;
parsmodel = updateTemperatureParameters(parsmodel);
parsmc = parsmc_;
parslogging = loggingParams;
parsmc.check();
parsmodel.check();
parslogging.check();
if (parsmc.specified.count("rngSeed") == 0) {
std::cout << "No rng seed specified, will use std::time(0)" << std::endl;
parsmc.rngSeed = (uint32_t) std::time(0);
}
rng = RngWrapper(parsmc.rngSeed, (parsmc.simindex + 1));
createReplica(replica, rng, parsmodel, parslogging);
//prepare metadata
modelMeta = parsmodel.prepareMetadataMap();
mcMeta = parsmc.prepareMetadataMap();
//prepare observable handlers
auto scalarObs = replica->getScalarObservables();
for (auto obsP = scalarObs.cbegin(); obsP != scalarObs.cend(); ++obsP) {
obsHandlers.push_back(
ObsPtr(new ScalarObservableHandler(*obsP, parsmc, modelMeta, mcMeta))
);
}
auto vectorObs = replica->getVectorObservables();
for (auto obsP = vectorObs.cbegin(); obsP != vectorObs.cend(); ++obsP) {
vecObsHandlers.push_back(
VecObsPtr(new VectorObservableHandler(*obsP, parsmc, modelMeta, mcMeta))
);
}
auto keyValueObs = replica->getKeyValueObservables();
for (auto obsP = keyValueObs.cbegin(); obsP != keyValueObs.cend(); ++obsP) {
vecObsHandlers.push_back(
VecObsPtr(new KeyValueObservableHandler(*obsP, parsmc, modelMeta, mcMeta))
);
}
// setup files for system configuration streams [if files do not exist already]
// [each process for its local replica]
if (parsmc.saveConfigurationStreamText or parsmc.saveConfigurationStreamBinary) {
std::string headerInfoText = metadataToString(modelMeta, "#")
+ metadataToString(mcMeta, "#");
if (parsmc.saveConfigurationStreamText) {
replica->saveConfigurationStreamTextHeader(headerInfoText);
}
if (parsmc.saveConfigurationStreamBinary) {
replica->saveConfigurationStreamBinaryHeaderfile(headerInfoText);
}
}
//query allowed walltime
const char* pbs_walltime = std::getenv("PBS_WALLTIME");
if (pbs_walltime) {
grantedWalltimeSecs = fromString<decltype(grantedWalltimeSecs)>(pbs_walltime);
} else {
grantedWalltimeSecs = std::numeric_limits<decltype(grantedWalltimeSecs)>::max();
}
std::cout << "Granted walltime: " << grantedWalltimeSecs << " seconds.\n";
//query SLURM Jobid
const char* jobid_env = std::getenv("SLURM_JOBID");
if (jobid_env) {
jobid = jobid_env;
} else {
jobid = "nojobid";
}
std::cout << "Job ID: " << jobid << "\n";
std::cout << "\nSimulation initialized, parameters: " << std::endl;
std::cout << metadataToString(mcMeta, " ") << metadataToString(modelMeta, " ") << std::endl;
}
template<class Model, class ModelParams>
DetQMC<Model, ModelParams>::DetQMC(const ModelParams& parsmodel_, const DetQMCParams& parsmc_,
const DetModelLoggingParams& parslogging_ /* default argument */) :
parsmodel(), parsmc(), parslogging(),
//proper initialization of default initialized members done in initFromParameters
modelMeta(), mcMeta(), rng(), replica(),
obsHandlers(), vecObsHandlers(),
sweepsDone(0), sweepsDoneThermalization(),
swCounter(0),
elapsedTimer(), // start timing
totalWalltimeSecs(0), walltimeSecsLastSaveResults(0),
grantedWalltimeSecs(0)
{
initFromParameters(parsmodel_, parsmc_, parslogging_);
}
template<class Model, class ModelParams>
DetQMC<Model, ModelParams>::DetQMC(const std::string& stateFileName, const DetQMCParams& newParsmc) :
parsmodel(), parsmc(), parslogging(),
//proper initialization of default initialized members done by loading from archive
modelMeta(), mcMeta(), rng(), replica(),
obsHandlers(), vecObsHandlers(),
sweepsDone(), sweepsDoneThermalization(),
swCounter(0),
elapsedTimer(), // start timing
totalWalltimeSecs(0), walltimeSecsLastSaveResults(0),
grantedWalltimeSecs(0), jobid("")
{
std::ifstream ifs;
ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit);
ifs.open(stateFileName.c_str(), std::ios::binary);
boost::archive::binary_iarchive ia(ifs);
DetModelLoggingParams parslogging_;
ModelParams parsmodel_;
DetQMCParams parsmc_;
ia >> parslogging_ >> parsmodel_ >> parsmc_;
if (newParsmc.sweeps > parsmc_.sweeps) {
std::cout << "Target sweeps will be changed from " << parsmc_.sweeps
<< " to " << newParsmc.sweeps << std::endl;
parsmc_.sweeps = newParsmc.sweeps;
parsmc_.sweepsHasChanged = true;
}
if (newParsmc.saveInterval > 0 and newParsmc.saveInterval != parsmc_.saveInterval) {
std::cout << "saveInterval will be changed from " << parsmc_.saveInterval
<< " to " << newParsmc.saveInterval << std::endl;
parsmc_.saveInterval = newParsmc.saveInterval;
}
parsmc_.stateFileName = stateFileName;
//make sure mcparams are set correctly as "specified"
#define SPECIFIED_INSERT_VAL(x) if (parsmc_.x) { parsmc_.specified.insert(#x); }
#define SPECIFIED_INSERT_STR(x) if (not parsmc_.x.empty()) { parsmc_.specified.insert(#x); }
SPECIFIED_INSERT_VAL(sweeps);
SPECIFIED_INSERT_VAL(thermalization);
SPECIFIED_INSERT_VAL(jkBlocks);
SPECIFIED_INSERT_VAL(measureInterval);
SPECIFIED_INSERT_VAL(saveInterval);
SPECIFIED_INSERT_STR(stateFileName);
#undef SPECIFIED_INSERT_VAL
#undef SPECIFIED_INSERT_STR
if (not parsmc_.greenUpdateType_string.empty()) {
parsmc_.specified.insert("greenUpdateType");
}
initFromParameters(parsmodel_, parsmc_, parslogging_);
loadContents(ia);
std::cout << "\n"
<< "State of previous simulation has been loaded.\n"
<< " sweepsDoneThermalization: " << sweepsDoneThermalization << "\n"
<< " sweepsDone: " << sweepsDone << std::endl;
}
template<class Model, class ModelParams>
void DetQMC<Model, ModelParams>::saveState() {
timing.start("saveState");
//serialize state to file
std::ofstream ofs;
ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit);
ofs.open(parsmc.stateFileName.c_str(), std::ios::binary);
boost::archive::binary_oarchive oa(ofs);
oa << parslogging << parsmodel << parsmc;
saveContents(oa);
//write out info about state of simulation to "info.dat"
std::string commonInfoFilename = "info.dat";
writeOnlyMetaData(commonInfoFilename, collectVersionInfo(),
"Collected information about this determinantal quantum Monte Carlo simulation",
false);
MetadataMap modelMeta_current = replica->prepareModelMetadataMap();
writeOnlyMetaData(commonInfoFilename, modelMeta_current,
"Model parameters and some data:",
true);
writeOnlyMetaData(commonInfoFilename, mcMeta,
"Monte Carlo parameters:",
true);
MetadataMap currentState;
currentState["sweepsDoneThermalization"] = numToString(sweepsDoneThermalization);
currentState["sweepsDone"] = numToString(sweepsDone);
uint32_t cwts = curWalltimeSecs();
totalWalltimeSecs += (cwts - walltimeSecsLastSaveResults);
walltimeSecsLastSaveResults = cwts;
currentState["totalWallTimeSecs"] = numToString(totalWalltimeSecs);
writeOnlyMetaData(commonInfoFilename, currentState,
"Current state of simulation:",
true);
std::cout << "State has been saved." << std::endl;
timing.stop("saveState");
}
template<class Model, class ModelParams>
DetQMC<Model, ModelParams>::~DetQMC() {
}
template<class Model, class ModelParams>
void DetQMC<Model, ModelParams>::run() {
enum Stage { T, M, F }; //Thermalization, Measurement, Finished
Stage stage = T;
//local helper functions to initialize a "stage" of the big loop
auto thermalizationStage = [&stage, this]() {
stage = T;
std::cout << "Thermalization for " << parsmc.thermalization << " sweeps..." << std::endl;
};
auto measurementsStage = [&stage, this]() {
stage = M;
std::cout << "Measurements for " << parsmc.sweeps << " sweeps..." << std::endl;
};
auto finishedStage = [&stage]() {
stage = F;
std::cout << "Measurements finished\n" << std::endl;
};
if (sweepsDoneThermalization < parsmc.thermalization) {
thermalizationStage();
} else if (sweepsDone < parsmc.sweeps) {
measurementsStage();
} else {
finishedStage();
}
const uint32_t SavetyMinutes = 35;
const std::string abortFilenames[] = { "ABORT." + jobid,
"../ABORT." + jobid,
"ABORT.all",
"../ABORT.all" };
while (stage != F) { //big loop
if (swCounter % 2 == 0) {
bool stop_now = false;
if (curWalltimeSecs() > grantedWalltimeSecs - SavetyMinutes*60) {
std::cout << "Granted walltime will be exceeded in less than " << SavetyMinutes << " minutes.\n";
stop_now = true;
} else {
for (auto abortfn : abortFilenames) {
if (boost::filesystem::exists(abortfn)) {
std::cout << "Found file " << abortfn << ".\n";
stop_now = true;
}
}
}
if (stop_now) {
//close to exceeded walltime or we find that a file has been placed,
//which signals us to abort this run for some other reason.
//but only save state and exit if we have done an even
//number of sweeps for ("economic") serialization guarantee [else do one sweep more]
std::cout << "Current stage:\n"
<< " sweeps done thermalization: " << sweepsDoneThermalization << "\n"
<< " sweeps done measurements: " << sweepsDone << "\n";
std::cout << "Save state / results and exit gracefully... ";
if (stage == Stage::M) {
saveResults();
}
saveState();
std::cout << " OK " << std::endl;
break; //while
}
}
//thermalization & measurement stages
switch (stage) {
case T:
switch(parsmc.greenUpdateType) {
case GreenUpdateType::GreenUpdateTypeSimple:
replica->sweepSimpleThermalization();
break;
case GreenUpdateType::GreenUpdateTypeStabilized:
replica->sweepThermalization();
break;
}
++sweepsDoneThermalization;
++swCounter;
if (swCounter == parsmc.saveInterval) {
std::cout << " " << sweepsDoneThermalization << " ... saving state...";
swCounter = 0;
saveState();
std::cout << " OK" << std::endl;
}
if (sweepsDoneThermalization == parsmc.thermalization) {
std::cout << "Thermalization finished\n" << std::endl;
replica->thermalizationOver();
swCounter = 0;
measurementsStage();
}
break; //case
case M: {
++swCounter;
bool takeMeasurementNow = (swCounter % parsmc.measureInterval == 0);
switch(parsmc.greenUpdateType) {
case GreenUpdateType::GreenUpdateTypeSimple:
replica->sweepSimple(takeMeasurementNow);
break;
case GreenUpdateType::GreenUpdateTypeStabilized:
replica->sweep(takeMeasurementNow);
break;
}
if (takeMeasurementNow) {
for (auto ph = obsHandlers.begin(); ph != obsHandlers.end(); ++ph) {
(*ph)->insertValue(sweepsDone);
}
for (auto ph = vecObsHandlers.begin(); ph != vecObsHandlers.end(); ++ph) {
(*ph)->insertValue(sweepsDone);
}
if (swCounter % parsmc.saveConfigurationStreamInterval == 0) {
// This is a good time to write the current system configuration to disk
if (parsmc.saveConfigurationStreamText) {
replica->saveConfigurationStreamText();
}
if (parsmc.saveConfigurationStreamBinary) {
replica->saveConfigurationStreamBinary();
}
}
}
++sweepsDone;
if (swCounter == parsmc.saveInterval) {
std::cout << " " << sweepsDone << " ... saving results and state ...";
swCounter = 0;
saveResults();
saveState();
std::cout << " OK" << std::endl;
}
if (sweepsDone == parsmc.sweeps) {
swCounter = 0;
finishedStage();
}
break; //case
}
case F:
break; //case
} //switch
}
}
template<class Model, class ModelParams>
void DetQMC<Model, ModelParams>::saveResults() {
timing.start("saveResults");
outputResults(obsHandlers);
for (auto p = obsHandlers.begin(); p != obsHandlers.end(); ++p) {
(*p)->outputTimeseries();
}
outputResults(vecObsHandlers);
timing.stop("saveResults");
}
#endif /* DETQMC_H_ */