mc.cpp 3 KB
Newer Older
1
2
#include "mc.h"

3
4
abstract_mc::abstract_mc(const std::string &jobfile, const std::string &taskname)
    : param{parser{jobfile}["tasks"][taskname]} {
Lukas Weber's avatar
Lukas Weber committed
5
	therm_ = param.get<int>("thermalization");
6
7
}

8
void abstract_mc::random_init() {
9
	if(param.defined("seed")) {
Lukas Weber's avatar
Lukas Weber committed
10
		rng.reset(new randomnumbergenerator(param.get<uint64_t>("seed")));
11
	} else {
12
		rng.reset(new randomnumbergenerator());
13
	}
14
15
}

16
double abstract_mc::random01() {
17
18
	return rng->d();
}
Lukas Weber's avatar
Lukas Weber committed
19
20

int abstract_mc::sweep() const {
21
	return sweep_;
Lukas Weber's avatar
Lukas Weber committed
22
}
23

24
void abstract_mc::_init() {
25
26
27
28
29
	// simple profiling support: measure the time spent for sweeps/measurements etc
	measure.add_observable("_ll_checkpoint_read_time", 1);
	measure.add_observable("_ll_checkpoint_write_time", 1);
	measure.add_observable("_ll_measurement_time", 1000);
	measure.add_observable("_ll_sweep_time", 1000);
30
31
	random_init();
	init();
32
33
34
35
36
37
38
39
40
}

void abstract_mc::_do_measurement() {
	struct timespec tstart, tend;
	clock_gettime(CLOCK_MONOTONIC_RAW, &tstart);

	do_measurement();

	clock_gettime(CLOCK_MONOTONIC_RAW, &tend);
41
42
	measure.add("_ll_measurement_time",
	            (tend.tv_sec - tstart.tv_sec) + 1e-9 * (tend.tv_nsec - tstart.tv_nsec));
43
44
45
}

void abstract_mc::_do_update() {
46
47
	struct timespec tstart, tend;
	clock_gettime(CLOCK_MONOTONIC_RAW, &tstart);
48
	sweep_++;
49

50
	do_update();
51
	clock_gettime(CLOCK_MONOTONIC_RAW, &tend);
52
53
	measure.add("_ll_sweep_time",
	            (tend.tv_sec - tstart.tv_sec) + 1e-9 * (tend.tv_nsec - tstart.tv_nsec));
54
55
}

56
void abstract_mc::_write(const std::string &dir) {
57
58
	struct timespec tstart, tend;
	clock_gettime(CLOCK_MONOTONIC_RAW, &tstart);
59
60

	iodump meas_file = iodump::open_readwrite(dir + ".meas.h5");
61
	measure.samples_write(meas_file.get_root());
62

63
	iodump dump_file = iodump::create(dir + ".dump.h5");
64
	auto g = dump_file.get_root();
65

66
67
68
69
70
	measure.checkpoint_write(g.open_group("measurements"));
	rng->checkpoint_write(g.open_group("random_number_generator"));
	checkpoint_write(g.open_group("simulation"));

	g.write("sweeps", sweep_);
71
72
	g.write("thermalization_sweeps", std::min(therm_, sweep_)); // only for convenience

73
	clock_gettime(CLOCK_MONOTONIC_RAW, &tend);
74
75
	measure.add("_ll_checkpoint_write_time",
	            (tend.tv_sec - tstart.tv_sec) + 1e-9 * (tend.tv_nsec - tstart.tv_nsec));
76
77
}

78
bool abstract_mc::_read(const std::string &dir) {
79
	try {
80
81
		struct timespec tstart, tend;
		clock_gettime(CLOCK_MONOTONIC_RAW, &tstart);
82
83

		iodump dump_file = iodump::open_readonly(dir + ".dump.h5");
84
85
86
87
		auto g = dump_file.get_root();
		measure.checkpoint_read(g.open_group("measurements"));
		rng->checkpoint_read(g.open_group("random_number_generator"));
		checkpoint_read(g.open_group("simulation"));
88

89
		g.read("sweeps", sweep_);
90

91
		clock_gettime(CLOCK_MONOTONIC_RAW, &tend);
92
93
94
		measure.add("_ll_checkpoint_read_time",
		            (tend.tv_sec - tstart.tv_sec) + 1e-9 * (tend.tv_nsec - tstart.tv_nsec));
	} catch(const iodump_exception &e) {
95
96
		return false;
	}
97
98
99
	return true;
}

100
void abstract_mc::_write_output(const std::string &filename) {
101
	write_output(filename);
102
103
104
}

bool abstract_mc::is_thermalized() {
105
	return sweep_ > therm_;
106
}