-
Notifications
You must be signed in to change notification settings - Fork 3
/
detqmcpt.h
1175 lines (1022 loc) · 47.8 KB
/
detqmcpt.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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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
*
* */
// Parallel Tempering Determinantal QMC simulation handling
#ifndef MPIDETQMCPT_H_
#define MPIDETQMCPT_H_
#include <vector>
#include <queue>
#include <functional>
#include <numeric> // std::iota
#include <algorithm> // std::copy
#include <memory>
#include <cstdlib>
#include <limits>
#include <ctime>
#include <functional>
#include <fstream>
#include <armadillo>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpragmas"
#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/serialization/string.hpp"
#include "boost/serialization/vector.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 ignored "-Wunused-parameter"
#include "boost/mpi.hpp"
#pragma GCC diagnostic pop
#include "metadata.h"
#include "datamapwriter.h"
#include "detqmcparams.h"
#include "detmodelparams.h"
#include "detmodelloggingparams.h"
#include "detmodel.h"
#include "mpiobservablehandlerpt.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 DetQMCPT {
public:
//constructor to init a new simulation:
DetQMCPT(const ModelParams& parsmodel, const DetQMCParams& parsmc,
const DetQMCPTParams& parspt,
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
DetQMCPT(const std::string& stateFileName, const DetQMCParams& newParsmc);
//carry out simulation determined by parsmc and parspt 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 ~DetQMCPT();
protected:
//helper for constructors -- set all parameters and initialize contained objects
void initFromParameters(const ModelParams& parsmodel, const DetQMCParams& parsmc,
const DetQMCPTParams& parspt,
const DetModelLoggingParams& loggingParams = DetModelLoggingParams());
void replicaExchangeStep();
void replicaExchangeConsistencyCheck(); // verify that processes have the right control parameters
void saveReplicaExchangeStatistics();
// subdirectory currently associated with replica set to the control parameter with index cpi
std::string control_parameter_subdir(int cpi);
ModelParams parsmodel;
DetQMCParams parsmc;
DetQMCPTParams parspt;
DetModelLoggingParams parslogging;
typedef DetQMCParams::GreenUpdateType GreenUpdateType;
MetadataMap modelMeta;
MetadataMap mcMeta;
MetadataMap ptMeta;
RngWrapper rng;
std::unique_ptr<Model> replica;
typedef std::unique_ptr<ScalarObservableHandlerPT> ObsPtr;
typedef std::unique_ptr<VectorObservableHandlerPT> VecObsPtr;
std::vector<ObsPtr> obsHandlers;
std::vector<VecObsPtr> vecObsHandlers; //need to be pointers: holds both KeyValueObservableHandlerPTs and VectorObservableHandlerPTs
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"
//MPI specifics:
int numProcesses; //total number of parallel processes
int processIndex; //MPI-rank of the current process
int local_current_parameter_index; // current control parameter index of this process's replica
// specific to the root process
std::vector<int> current_process_par; // indexed by process rank number, giving control parameter index
// currently associated to the replica at that process
std::vector<int> current_par_process; // the reverse association
std::vector<double> exchange_action; // for each replica: its locally measured exchange action
// for control-parameter specific data, e.g. MC stepsize adjustment
//std::vector<uint8_tString> control_data_buffer_1, control_data_buffer_2;
std::vector<std::string> process_control_data_buffer; // map process index -> control data
// for each process:
std::string local_control_data_buffer;
//Statistics about replica exchange
// -- evaluate at rank 0 --
struct ExchangeStatistics {
// exchange acceptance ratios histogram
std::vector<int> par_swapUpAccepted; // count for each control parameter index how often replica exchanges
// with one index higher are accepted
std::vector<int> par_swapUpProposed; // .. are proposed
// replica diffusion -- histograms if replicas (processes) are
// moving up or down in control parameter space
enum ParameterDirection {NONE_P, UP_P, DOWN_P};
std::vector<ParameterDirection> process_goingWhere; // track for each replica current direction
std::vector<int> par_countGoingUp;//at each attempted parameter swap: if replica at current parameter has visited parMax last (and not parMin) increase
std::vector<int> par_countGoingDown; //vice versa for replicas having visited parMin last (and not parMax)
ExchangeStatistics() :
par_swapUpAccepted(), par_swapUpProposed(), process_goingWhere(),
par_countGoingUp(), par_countGoingDown()
{ }
ExchangeStatistics(const DetQMCPTParams& ptPars) :
par_swapUpAccepted(ptPars.controlParameterValues.size(), 0),
par_swapUpProposed(ptPars.controlParameterValues.size(), 0),
process_goingWhere(ptPars.controlParameterValues.size(), NONE_P),
par_countGoingUp(ptPars.controlParameterValues.size(), 0),
par_countGoingDown(ptPars.controlParameterValues.size(), 0)
{ }
template<class Archive>
void serialize(Archive& ar, const uint32_t /*version*/) {
ar & par_swapUpAccepted & par_swapUpProposed & process_goingWhere
& par_countGoingUp & par_countGoingDown;
}
} es;
// Saving system configurations in parallelized simulations:
// -- root process collects configs from all replicas, saves all of them after buffering
struct SaveConfigurations {
typedef typename Model::SystemConfig SystemConfig;
std::queue<SystemConfig> local_bufferedConfigurations; // each process buffers the configs of its own replica here (FIFO)
std::queue<int> local_bufferedControlParameterIndex; // each process buffers the current controlParameterIndex for its replica
std::string local_mpi_buffer;
// at rank 0:
typedef typename Model::SystemConfig_FileHandle FileHandle;
std::vector<FileHandle> par_fileHandle; // file handle to save configurations for each control parameter index
std::vector<std::string> process_mpi_buffer; // buffer for MPI gathered data for each process index
std::vector<int> process_controlParameterIndex; // cpi mathching the system configuration in process_mpi_buffer
} sc;
void setup_SaveConfigurations();
void buffer_local_system_configuration();
void gather_and_output_buffered_system_configurations();
private:
//Serialize only the content data that has changed after construction.
//Only call for deserialization after DetQMCPT 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);
// distribute and update control parameter for replica
// after deserialization
namespace mpi = boost::mpi;
mpi::communicator world;
int new_param_index = 0;
mpi::scatter(world,
current_process_par, // send
new_param_index, // recv
0 // root
);
local_current_parameter_index = new_param_index;
replica->set_exchange_parameter_value(
parspt.controlParameterValues[new_param_index]
);
}
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 ScalarObservableHandlerPT have a method serializeContents
(*p)->serializeContents(ar);
}
for (auto p = vecObsHandlers.begin(); p != vecObsHandlers.end(); ++p) {
//ATM no further derived classes of VectorObservableHandlerPT have a method serializeContents
(*p)->serializeContents(ar);
}
ar & sweepsDone & sweepsDoneThermalization;
ar & swCounter;
ar & totalWalltimeSecs;
ar & local_current_parameter_index;
ar & current_process_par;
ar & current_par_process;
ar & es; // exchange statistics
}
};
template<class Model, class ModelParams>
void DetQMCPT<Model,ModelParams>::initFromParameters(const ModelParams& parsmodel_, const DetQMCParams& parsmc_,
const DetQMCPTParams& parspt_,
const DetModelLoggingParams& loggingParams /*default argument*/) {
parsmodel = parsmodel_;
parsmodel = updateTemperatureParameters(parsmodel);
parsmc = parsmc_;
parspt = parspt_;
parslogging = loggingParams;
parsmc.check();
parspt.check();
parslogging.check();
// Set up MPI info
namespace mpi = boost::mpi;
mpi::communicator world;
processIndex = world.rank();
numProcesses = world.size();
if (numProcesses != int(parspt.controlParameterValues.size())) {
throw_ConfigurationError("Number of processes " + numToString(numProcesses) +
" does not match number of control parameter values " +
numToString(parspt.controlParameterValues.size()));
}
// set up RNG
if (parsmc.specified.count("rngSeed") == 0) {
if (processIndex == 0) {
std::cout << "No rng seed specified, will use std::time(0) determined at root process" << std::endl;
parsmc.rngSeed = (uint32_t) std::time(0);
}
//unsigned broadcast_rngSeed = parsmc.rngSeed; // work around lacking support for MPI_UINT32_T
//MPI_Bcast(&broadcast_rngSeed, 1, MPI_UNSIGNED, 0, MPI_COMM_WORLD);
mpi::broadcast(world, parsmc.rngSeed, 0);
}
rng = RngWrapper(parsmc.rngSeed, (parsmc.simindex + 1) * (processIndex + 1));
// set up control parameters for current process replica parameters
local_current_parameter_index = processIndex;
parsmodel.set_exchange_parameter_value(
parspt.controlParameterValues[local_current_parameter_index]);
parsmodel.check();
// the replicaLogfiledir is for some consistency checks, and is most likely
// to be unused in later production runs. Replicas to be used with this
// replica-exchange code therefore are required to support this parameter
// in the createReplica function associated to them.
std::string replicaLogfiledir = "log_proc_" + numToString(processIndex);
createReplica(replica, rng, parsmodel, parslogging, replicaLogfiledir);
// at rank 0 keep track of which process has which control parameter currently
// and track exchange action contributions
// also initialize control parameter data buffers,
// at rank 0: exchangeStatistics
if (processIndex == 0) {
// fill with 0, 1, 2, ... numProcesses-1
current_process_par.resize(numProcesses);
std::iota(current_process_par.begin(), current_process_par.end(), 0);
current_par_process.resize(numProcesses);
std::iota(current_par_process.begin(), current_par_process.end(), 0);
exchange_action.resize(numProcesses, 0);
// control_data_buffer_1.resize(numProcesses * replica->get_control_data_buffer_size());
// control_data_buffer_2.resize(numProcesses * replica->get_control_data_buffer_size());
process_control_data_buffer.resize(numProcesses);
// control_data_buffer_2.resize(numProcesses);
es = ExchangeStatistics(parspt);
}
//local_control_data_buffer.resize(replica->get_control_data_buffer_size());
local_control_data_buffer.clear();
//prepare metadata
modelMeta = parsmodel.prepareMetadataMap();
modelMeta.erase(parspt.controlParameterName);
mcMeta = parsmc.prepareMetadataMap();
mcMeta.erase("stateFileName");
ptMeta = parspt.prepareMetadataMap();
//prepare observable handlers
auto scalarObs = replica->getScalarObservables();
for (auto obsP = scalarObs.cbegin(); obsP != scalarObs.cend(); ++obsP) {
obsHandlers.push_back(
ObsPtr(new ScalarObservableHandlerPT(*obsP, current_process_par, parsmc, parspt, modelMeta, mcMeta, ptMeta))
);
}
auto vectorObs = replica->getVectorObservables();
for (auto obsP = vectorObs.cbegin(); obsP != vectorObs.cend(); ++obsP) {
vecObsHandlers.push_back(
VecObsPtr(new VectorObservableHandlerPT(*obsP, current_process_par, parsmc, parspt, modelMeta, mcMeta, ptMeta))
);
}
auto keyValueObs = replica->getKeyValueObservables();
for (auto obsP = keyValueObs.cbegin(); obsP != keyValueObs.cend(); ++obsP) {
vecObsHandlers.push_back(
VecObsPtr(new KeyValueObservableHandlerPT(*obsP, current_process_par, parsmc, parspt, modelMeta, mcMeta, ptMeta))
);
}
// setup files for system configuration streams [if files do not exist already]
// [each process for its local replica]
// Afterwards only the master process will continue to write to these files
if (parsmc.saveConfigurationStreamText or parsmc.saveConfigurationStreamBinary) {
namespace fs = boost::filesystem;
std::string subdir_string = control_parameter_subdir(local_current_parameter_index);
fs::create_directories(subdir_string);
std::string parname = parspt.controlParameterName;
std::string parvalue = numToString(parspt.controlParameterValues[local_current_parameter_index]);
MetadataMap modelMeta_cpi = modelMeta;
modelMeta_cpi[parname] = parvalue;
std::string headerInfoText = metadataToString(modelMeta_cpi, "#")
+ metadataToString(mcMeta, "#")
+ metadataToString(ptMeta, "#");
if (parsmc.saveConfigurationStreamText) {
replica->saveConfigurationStreamTextHeader(headerInfoText, subdir_string);
}
if (parsmc.saveConfigurationStreamBinary) {
replica->saveConfigurationStreamBinaryHeaderfile(headerInfoText, subdir_string);
}
}
//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();
}
if (processIndex == 0) {
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(ptMeta, " ")
<< metadataToString(modelMeta, " ") << std::endl;
}
}
template<class Model, class ModelParams>
DetQMCPT<Model, ModelParams>::DetQMCPT(const ModelParams& parsmodel_, const DetQMCParams& parsmc_,
const DetQMCPTParams& parspt_,
const DetModelLoggingParams& parslogging_ /* default argument */)
:
parsmodel(), parsmc(), parspt(), parslogging(),
//proper initialization of default initialized members done in initFromParameters
modelMeta(), mcMeta(), ptMeta(), rng(), replica(),
obsHandlers(), vecObsHandlers(),
sweepsDone(0), sweepsDoneThermalization(),
swCounter(0),
elapsedTimer(), // start timing
totalWalltimeSecs(0), walltimeSecsLastSaveResults(0),
grantedWalltimeSecs(0), jobid(""),
numProcesses(1), processIndex(0),
local_current_parameter_index(0),
current_process_par(1, 0),
current_par_process(1, 0),
exchange_action(1, 0),
process_control_data_buffer(),
//control_data_buffer_2(),
local_control_data_buffer(),
es(), sc()
{
initFromParameters(parsmodel_, parsmc_, parspt_, parslogging_);
}
template<class Model, class ModelParams>
DetQMCPT<Model, ModelParams>::DetQMCPT(const std::string& stateFileName, const DetQMCParams& newParsmc) :
parsmodel(), parsmc(), parspt(), 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(""),
numProcesses(1), processIndex(0),
local_current_parameter_index(0),
current_process_par(1, 0),
current_par_process(1, 0),
exchange_action(1, 0),
process_control_data_buffer(),
//control_data_buffer_2(),
local_control_data_buffer(),
es()
{
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_;
DetQMCPTParams parspt_;
ia >> parslogging_ >> parsmodel_ >> parsmc_ >> parspt_;
if (newParsmc.sweeps > parsmc_.sweeps) {
if (processIndex == 0) {
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) {
if (processIndex == 0) {
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_, parspt_, parslogging_);
loadContents(ia);
if (processIndex == 0) {
std::cout << "\n"
<< "State of previous simulation has been loaded.\n"
<< " sweepsDoneThermalization: " << sweepsDoneThermalization << "\n"
<< " sweepsDone: " << sweepsDone << std::endl;
}
}
template<class Model, class ModelParams>
void DetQMCPT<Model, ModelParams>::saveState() {
timing.start("saveState");
namespace fs = boost::filesystem;
//serialize state to file
// -- every process needs to do this
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 << parspt;
saveContents(oa);
//write out info about state of simulation to "info.dat"
// -- one for the main directory and one for each subdirectory (control parameter specific)
// -- only the master process does this
if (processIndex == 0) {
MetadataMap currentState;
currentState["sweepsDoneThermalization"] = numToString(sweepsDoneThermalization);
currentState["sweepsDone"] = numToString(sweepsDone);
uint32_t cwts = curWalltimeSecs();
totalWalltimeSecs += (cwts - walltimeSecsLastSaveResults);
walltimeSecsLastSaveResults = cwts;
currentState["totalWallTimeSecs"] = numToString(totalWalltimeSecs);
auto write_info = [this](const MetadataMap& modelMeta_, const MetadataMap& currentState,
const fs::path& subdirectory) {
fs::create_directories(subdirectory);
std::string commonInfoFilename = (subdirectory / fs::path("info.dat")).string();
writeOnlyMetaData(commonInfoFilename, collectVersionInfo(),
"Collected information about this determinantal quantum Monte Carlo simulation",
false);
writeOnlyMetaData(commonInfoFilename, modelMeta_,
"Model parameters:",
true);
writeOnlyMetaData(commonInfoFilename, mcMeta,
"Monte Carlo parameters:",
true);
writeOnlyMetaData(commonInfoFilename, ptMeta,
"Replica exchange parameters:",
true);
writeOnlyMetaData(commonInfoFilename, currentState,
"Current state of simulation:",
true);
};
// top level directory: info not restricted to any value of the control parameter
write_info(modelMeta, currentState, fs::path("."));
// write a separate info.dat for each value of the control parameter
for (int cpi = 0; cpi < numProcesses; ++cpi) {
std::string subdir_string = control_parameter_subdir(cpi);
std::string parname = parspt.controlParameterName;
std::string parvalue = numToString(parspt.controlParameterValues[cpi]);
MetadataMap modelMeta_cpi = modelMeta;
modelMeta_cpi[parname] = parvalue;
write_info(modelMeta_cpi, currentState, fs::path(subdir_string));
}
}
if (processIndex == 0) {
saveReplicaExchangeStatistics();
}
if (processIndex == 0) {
std::cout << "State has been saved." << std::endl;
}
timing.stop("saveState");
}
template<class Model, class ModelParams>
void DetQMCPT<Model, ModelParams>::saveReplicaExchangeStatistics() {
IntDoubleMapWriter mapWriter;
mapWriter.addMetadataMap(modelMeta);
mapWriter.addMetadataMap(mcMeta);
mapWriter.addMetadataMap(ptMeta);
// index -> control parameter
std::shared_ptr<std::map<int, double>> controlParameters(new std::map<int,double>);
for (int cpi = 0; cpi < numProcesses; ++cpi) {
(*controlParameters)[cpi] = parspt.controlParameterValues[cpi];
}
IntDoubleMapWriter controlParametersWriter = mapWriter;
controlParametersWriter.addMeta("key", "control parameter index");
controlParametersWriter.addHeaderText("Control parameter values");
controlParametersWriter.addHeaderText("control parameter index \t control parameter value");
controlParametersWriter.setData(controlParameters);
controlParametersWriter.writeToFile("exchange-parameters.values");
//control parameter swap acceptance
std::shared_ptr<std::map<int, double>> cpiAccRates(new std::map<int,double>);
for (int cpi = 0; cpi < numProcesses; ++cpi) {
int countAccepted = es.par_swapUpAccepted[cpi];
int countProposed = es.par_swapUpProposed[cpi];
double ar = 0.0;
if (countProposed != 0) {
ar = static_cast<double>(countAccepted)
/ static_cast<double>(countProposed);
}
(*cpiAccRates)[cpi] = ar;
}
IntDoubleMapWriter cpiAccRatesWriter = mapWriter;
cpiAccRatesWriter.addMeta("key", "control parameter index");
cpiAccRatesWriter.addHeaderText("Acceptance ratio of exchanging replicas at control parameters (upwards)");
cpiAccRatesWriter.addHeaderText("control parameter index \t acceptance ratio");
cpiAccRatesWriter.setData(cpiAccRates);
cpiAccRatesWriter.writeToFile("exchange-acceptance.values");
//diffusion fraction
std::shared_ptr<std::map<int, double>> dfractions(new std::map<int,double>);
for (int cpi = 0; cpi < numProcesses; ++cpi) {
int countUp = es.par_countGoingUp[cpi];
int countDown = es.par_countGoingDown[cpi];
double df = 0.0;
if (countUp + countDown != 0) {
df = static_cast<double>(countUp)
/ static_cast<double>(countUp + countDown);
}
(*dfractions)[cpi] = df;
}
IntDoubleMapWriter dfractionsWriter = mapWriter;
dfractionsWriter.addMeta("key", "control parameter index");
dfractionsWriter.addHeaderText("Diffusion fraction of replicas at control parameters: df = nUp / (nUp + nDown)");
dfractionsWriter.addHeaderText("control parameter index \t diffusion fraction");
dfractionsWriter.setData(dfractions);
dfractionsWriter.writeToFile("exchange-diffusion.values");
}
template<class Model, class ModelParams>
std::string DetQMCPT<Model, ModelParams>::control_parameter_subdir(int cpi) {
std::string parname = parspt.controlParameterName;
std::string parvalue = numToString(parspt.controlParameterValues[cpi]);
std::string subdir_string = "p" + numToString(cpi) + "_" + parname + parvalue;
return subdir_string;
}
template<class Model, class ModelParams>
DetQMCPT<Model, ModelParams>::~DetQMCPT() {
}
template<class Model, class ModelParams>
void DetQMCPT<Model, ModelParams>::setup_SaveConfigurations() {
namespace fs = boost::filesystem;
if (parsmc.saveConfigurationStreamText or parsmc.saveConfigurationStreamBinary) {
sc = SaveConfigurations();
if (processIndex == 0) {
sc.par_fileHandle.resize(numProcesses);
for (int cpi = 0; cpi < numProcesses; ++cpi) {
std::string subdirectory = control_parameter_subdir(cpi);
fs::create_directories(subdirectory);
sc.par_fileHandle[cpi] = replica->prepareSystemConfigurationStreamFileHandle(
parsmc.saveConfigurationStreamBinary, parsmc.saveConfigurationStreamText,
subdirectory
);
}
sc.process_mpi_buffer.resize(numProcesses);
for (int pi = 0; pi < numProcesses; ++pi) {
sc.process_mpi_buffer[pi].clear();
}
sc.process_controlParameterIndex.resize(numProcesses);
}
}
}
template<class Model, class ModelParams>
void DetQMCPT<Model, ModelParams>::buffer_local_system_configuration() {
// each process buffers the system configuration of its replica in local memory
if (parsmc.saveConfigurationStreamText or parsmc.saveConfigurationStreamBinary) {
sc.local_bufferedConfigurations.push(replica->getCurrentSystemConfiguration());
sc.local_bufferedControlParameterIndex.push(local_current_parameter_index);
}
}
template<class Model, class ModelParams>
void DetQMCPT<Model, ModelParams>::gather_and_output_buffered_system_configurations() {
namespace mpi = boost::mpi;
mpi::communicator world;
if (not (parsmc.saveConfigurationStreamText or parsmc.saveConfigurationStreamBinary)) {
return;
}
while (not sc.local_bufferedConfigurations.empty()) {
// collect at rank0
sc.local_mpi_buffer.clear();
serialize_systemConfig_to_buffer(sc.local_mpi_buffer,
sc.local_bufferedConfigurations.front());
int local_cpi = sc.local_bufferedControlParameterIndex.front();
sc.local_bufferedConfigurations.pop();
sc.local_bufferedControlParameterIndex.pop();
if (processIndex == 0) {
for (auto& datastring : sc.process_mpi_buffer) {
datastring.clear();
}
}
mpi::gather(world,
sc.local_mpi_buffer, // send
sc.process_mpi_buffer, // recv at rank 0
0);
mpi::gather(world,
local_cpi, // send
sc.process_controlParameterIndex, // recv at rank 0
0);
// write to the right files
if (processIndex == 0) {
for (int pi = 0; pi < numProcesses; ++pi) {
typename SaveConfigurations::SystemConfig pi_systemConfig;
deserialize_systemConfig_from_buffer(pi_systemConfig, sc.process_mpi_buffer[pi]);
int cpi = sc.process_controlParameterIndex[pi];
pi_systemConfig.write_to_disk(sc.par_fileHandle[cpi]);
}
}
}
// flush all ofstreams
if (processIndex == 0) {
for (int cpi = 0; cpi < numProcesses; ++cpi) {
sc.par_fileHandle[cpi].flush();
}
}
}
template<class Model, class ModelParams>
void DetQMCPT<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;
if (processIndex == 0) {
std::cout << "Thermalization for " << parsmc.thermalization << " sweeps..." << std::endl;
}
};
auto measurementsStage = [&stage, this]() {
stage = M;
if (processIndex == 0) {
std::cout << "Measurements for " << parsmc.sweeps << " sweeps..." << std::endl;
}
};
auto finishedStage = [&stage, this]() {
stage = F;
if (processIndex == 0) {
std::cout << "Measurements finished\n" << std::endl;
}
};
// helper function for saving state, results, timeseries, configuration streams
auto save = [&]() {
if (stage == Stage::M) {
this->gather_and_output_buffered_system_configurations();
this->saveResults();
}
this->saveState();
};
if (parsmc.saveConfigurationStreamText or parsmc.saveConfigurationStreamBinary) {
setup_SaveConfigurations();
}
if (sweepsDoneThermalization < parsmc.thermalization) {
thermalizationStage();
} else if (sweepsDone < parsmc.sweeps) {
measurementsStage();
} else {
finishedStage();
}
const uint32_t SafetyMinutes = 35;
const std::string abortFilenames[] = { "ABORT." + jobid,
"../ABORT." + jobid,
"ABORT.all",
"../ABORT.all" };
namespace mpi = boost::mpi;
mpi::communicator world;
while (stage != F) { //big loop
// do we need to quit?
if (swCounter % 2 == 0) {
char stop_now = false;
if (processIndex == 0) {
if (curWalltimeSecs() > grantedWalltimeSecs - SafetyMinutes*60) {
std::cout << "Granted walltime will be exceeded in less than " << SafetyMinutes << " minutes.\n";
stop_now = true;
} else {
for (auto abortfn : abortFilenames) {
if (boost::filesystem::exists(abortfn)) {
std::cout << "Found file " << abortfn << ".\n";
stop_now = true;
}
}
}
}
// MPI_Bcast( &stop_now, 1, MPI_CHAR,
// 0, MPI_COMM_WORLD
// );
mpi::broadcast(world, stop_now, 0);
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]
if (processIndex == 0) {
std::cout << "Current stage:\n"
<< " sweeps done thermalization: " << sweepsDoneThermalization << "\n"
<< " sweeps done measurements: " << sweepsDone << "\n";
std::cout << "Save state / results and exit gracefully." << std::endl;
}
save();
std::cout << " OK " << std::endl;
break; //while
}
}
//thermalization & measurement stages | main work
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) {
if (processIndex == 0) {
std::cout << " " << sweepsDoneThermalization << " ... saving state...";
}
swCounter = 0;
save();
//MPI_Barrier(MPI_COMM_WORLD);
world.barrier();
if (processIndex == 0) {
std::cout << " OK" << std::endl;
}
}
if (sweepsDoneThermalization == parsmc.thermalization) {
if (processIndex == 0) {
std::cout << "Thermalization finished\n" << std::endl;
}
replica->thermalizationOver(processIndex);
swCounter = 0;
measurementsStage();
}
break; //case T
}
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) {
buffer_local_system_configuration();
}
// // This is a good time to write the current system configuration to disk
// if (parsmc.saveConfigurationStreamText) {
// replica->saveConfigurationStreamText(
// control_parameter_subdir(local_current_parameter_index));
// }
// if (parsmc.saveConfigurationStreamBinary) {
// replica->saveConfigurationStreamBinary(
// control_parameter_subdir(local_current_parameter_index));
// }
}
++sweepsDone;
if (swCounter == parsmc.saveInterval) {
if (processIndex == 0) {
std::cout << " " << sweepsDone << " ... saving results and state ...";
}
swCounter = 0;
save();
//MPI_Barrier(MPI_COMM_WORLD);
world.barrier();
if (processIndex == 0) {
std::cout << " OK" << std::endl;
}
}
if (sweepsDone == parsmc.sweeps) {
swCounter = 0;
finishedStage();
}
break; //case M
}
case F:
break; //case F
} //switch
//replica exchange
if (stage == T or stage == M) {
if (parspt.exchangeInterval != 0 and ((sweepsDone + sweepsDoneThermalization) % parspt.exchangeInterval == 0)) {
replicaExchangeStep();
}
replicaExchangeConsistencyCheck();
} //replica exchange
} // while (stage != F)
}
template<class Model, class ModelParams>
void DetQMCPT<Model, ModelParams>::replicaExchangeStep() {
timing.start("detqmcpt-replicaExchangeStep");
namespace mpi = boost::mpi;
mpi::communicator world;
// Gather control_data_buffer contents from all processes:
local_control_data_buffer.clear();
replica->get_control_data(local_control_data_buffer);
if (processIndex == 0) {
assert(process_control_data_buffer.size() == (std::size_t)numProcesses);
for (auto& datastring : process_control_data_buffer) {
datastring.clear();
}
}
mpi::gather(world,
local_control_data_buffer, // send
process_control_data_buffer, // recv at rank 0
0);
// double* local_buf = local_control_data_buffer.data();
// uint32_t local_buf_size = local_control_data_buffer.size();
// replica->get_control_data(local_buf);
// MPI_Gather( local_buf, // send buf
// local_buf_size,
// MPI_DOUBLE,
// control_data_buffer_1.data(), // recv buf
// local_buf_size,
// MPI_DOUBLE,
// 0, // root process
// MPI_COMM_WORLD
// );
// Gather exchange action contribution from replicas
double localAction = replica->get_exchange_action_contribution();
// MPI_Gather( &localAction, // send buf
// 1,