mc.cpp 3 KB
Newer Older
1
#include "mc.h"
2
#include <sys/stat.h>
3

4
5
namespace loadl {

Lukas Weber's avatar
wrong &    
Lukas Weber committed
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 random_number_generator(param.get<uint64_t>("seed")));
15
	} else {
Lukas Weber's avatar
Lukas Weber committed
16
		rng.reset(new random_number_generator());
17
	}
18
19
}

20
double mc::random01() {
Lukas Weber's avatar
Lukas Weber committed
21
	return rng->random_double();
22
}
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
83
84
85
86
static bool file_exists(const std::string &path) {
	struct stat buf;
	return stat(path.c_str(), &buf) == 0;
}

87
bool mc::_read(const std::string &dir) {
88
	if(!file_exists(dir)) {
89
90
		return false;
	}
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

	struct timespec tstart, tend;
	clock_gettime(CLOCK_MONOTONIC_RAW, &tstart);

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

	g.read("sweeps", sweep_);

	clock_gettime(CLOCK_MONOTONIC_RAW, &tend);
	measure.add("_ll_checkpoint_read_time",
	            (tend.tv_sec - tstart.tv_sec) + 1e-9 * (tend.tv_nsec - tstart.tv_nsec));
106
107
108
	return true;
}

109
void mc::_write_output(const std::string &filename) {
110
	write_output(filename);
111
112
}

113
bool mc::is_thermalized() {
114
	return sweep_ > therm_;
115
}
116
}