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

3
4
namespace loadl {

Lukas Weber's avatar
wrong &    
Lukas Weber committed
5
mc::mc(const parser &p) : param{p} {
Lukas Weber's avatar
Lukas Weber committed
6
	therm_ = param.get<int>("thermalization");
7
8
}

9
void mc::write_output(const std::string &) {}
10

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

19
double mc::random01() {
20
21
	return rng->d();
}
Lukas Weber's avatar
Lukas Weber committed
22

23
int mc::sweep() const {
24
	return sweep_;
Lukas Weber's avatar
Lukas Weber committed
25
}
26

27
void mc::_init() {
28
29
30
31
32
	// 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);
33
34
	random_init();
	init();
35
36
}

37
void mc::_do_measurement() {
38
39
40
41
42
43
	struct timespec tstart, tend;
	clock_gettime(CLOCK_MONOTONIC_RAW, &tstart);

	do_measurement();

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

48
void mc::_do_update() {
49
50
	struct timespec tstart, tend;
	clock_gettime(CLOCK_MONOTONIC_RAW, &tstart);
51
	sweep_++;
52

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

59
void mc::_write(const std::string &dir) {
60
61
	struct timespec tstart, tend;
	clock_gettime(CLOCK_MONOTONIC_RAW, &tstart);
62
63

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

66
	iodump dump_file = iodump::create(dir + ".dump.h5");
67
	auto g = dump_file.get_root();
68

69
70
71
72
73
	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_);
74
75
	g.write("thermalization_sweeps", std::min(therm_, sweep_)); // only for convenience

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

81
bool mc::_read(const std::string &dir) {
82
	try {
83
84
		struct timespec tstart, tend;
		clock_gettime(CLOCK_MONOTONIC_RAW, &tstart);
85
86

		iodump dump_file = iodump::open_readonly(dir + ".dump.h5");
87
88
89
90
		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"));
91

92
		g.read("sweeps", sweep_);
93

94
		clock_gettime(CLOCK_MONOTONIC_RAW, &tend);
95
96
97
		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) {
98
99
		return false;
	}
100
101
102
	return true;
}

103
void mc::_write_output(const std::string &filename) {
104
	write_output(filename);
105
106
}

107
bool mc::is_thermalized() {
108
	return sweep_ > therm_;
109
}
110
}