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

3
4
namespace loadl {

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

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

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

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

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

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

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

	do_measurement();

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

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

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

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

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

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

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

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

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

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

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

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

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

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