merger.cpp 9.25 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <functional>
#include <string>
#include "merger.h"
#include "mc.h"
#include "evalable.h"
#include "measurements.h"
#include "dump.h"

#include <vector>
#include <fmt/format.h>

void merger::add_evalable(const std::string& name, const std::vector<std::string>& used_observables, evalable::func func) {
	// evalable names also should be valid HDF5 paths
	if(not measurements::observable_name_is_legal(name)) {
		throw std::runtime_error{fmt::format("illegal evalable name '{}': must not contain . or /", name)};
	}

	evalables_.emplace_back(name, used_observables, func);
}

Lukas Weber's avatar
Lukas Weber committed
21
22
results merger::merge(const std::vector<std::string>& filenames, size_t rebinning_bin_count) {
	results res;
23
24
25
26
27
28
29
30
31
32

	// This thing reads the complete time series of an observable which will
	// probably make it the biggest memory user of load leveller. But since
	// it’s only one observable at a time, it is maybe still okay.

	// If not, research a custom solution using fancy HDF5 virtual datasets or something.

	// In the first pass we gather the metadata to decide on the rebinning_bin_length.
	for(auto& filename : filenames) {
		iodump meas_file = iodump::open_readonly(filename);
33
34
		auto g = meas_file.get_root();
		for(const auto& obs_name : g) {
35
36
37
38
39
40
			res.observables.try_emplace(obs_name);
			auto& obs = res.observables[obs_name];
			obs.name = obs_name;

			size_t vector_length;
			std::vector<double> samples;
41
			std::cerr << obs_name << "\n";
42

43
44
45
46
47
			auto obs_group = g.open_group(obs_name);
			obs_group.read("bin_length", obs.internal_bin_length);
			obs_group.read("vector_length", vector_length);
			int sample_size = obs_group.get_extent("samples");
			obs_group.read("samples", samples);
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

			if(sample_size % vector_length != 0) {
				throw std::runtime_error{"merge: sample count is not an integer multiple of the vector length. Corrupt file?"};
			}

			obs.total_sample_count += sample_size/vector_length;
			obs.mean.resize(vector_length);
			obs.error.resize(vector_length);
			obs.autocorrelation_time.resize(vector_length);

		}
	}
	
	for(auto& entry : res.observables) {
		auto& obs = entry.second;
		obs.rebinning_means.reserve(rebinning_bin_count*obs.mean.size());
		obs.rebinning_bin_length = obs.total_sample_count/rebinning_bin_count;
		obs.rebinning_bin_count = rebinning_bin_count;
	}

	size_t samples_processed = 0;
	for(auto& filename : filenames) {
		iodump meas_file = iodump::open_readonly(filename);
71
72
		auto g = meas_file.get_root();
		for(const auto& obs_name : g) {
73
74
75
76
			std::vector<double> samples;
			auto& obs = res.observables[obs_name];
			obs.name = obs_name;
			
77
			g.read(fmt::format("{}/samples", obs_name), samples);
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

			// rebinning_bin_count*rebinning_bin_length may be smaller than
			// total_sample_count. In that case, we throw away the leftover samples.
			// 
			for(size_t i = 0; samples_processed < obs.rebinning_bin_count*obs.rebinning_bin_length and i < samples.size(); i++) {
				size_t vector_length = obs.mean.size();
				size_t vector_idx = i%vector_length;

				obs.mean[vector_idx] += samples[i];
				samples_processed++;
			}
		}
	}

	struct obs_rebinning_metadata {
		size_t current_rebin = 0;
		size_t current_rebin_filling = 0;
	};

	std::map<std::string, obs_rebinning_metadata> metadata;
	for(auto& entry : res.observables) {
		auto& obs = entry.second;
		for(size_t i = 0; i < obs.mean.size(); i++) {
			obs.mean[i] /= obs.total_sample_count;
		}

		metadata.emplace(obs.name, obs_rebinning_metadata{});
	}
	
	// now handle the error and autocorrelation time which are calculated by rebinning.
	for(auto& filename : filenames) {
		iodump meas_file = iodump::open_readonly(filename);
110
111
		auto g = meas_file.get_root();
		for(const auto& obs_name : g) {
112
113
114
115
116
117
			std::vector<double> samples;
			auto& obs = res.observables.at(obs_name);
			auto& obs_meta = metadata.at(obs_name);

			size_t vector_length = obs.mean.size();
			
118
			g.read(fmt::format("{}/samples", obs_name), samples);
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

			for(size_t i = 0; obs_meta.current_rebin < rebinning_bin_count and i < samples.size(); i++) {
				size_t vector_idx = i%vector_length;
				size_t rebin_idx = obs_meta.current_rebin*vector_length + vector_idx;

				obs.rebinning_means[rebin_idx] += samples[vector_idx];
				
				obs_meta.current_rebin_filling++;

				// I used autocorrelation_time as a buffer here to hold the naive no-rebinning error (sorry)
				obs.autocorrelation_time[vector_idx] += (samples[i]-obs.mean[vector_idx])*(samples[i]-obs.mean[vector_idx]);

				if(obs_meta.current_rebin_filling >= obs.rebinning_bin_length) {
					obs.rebinning_means[rebin_idx] /= obs.rebinning_bin_length;
					
					double diff = obs.rebinning_means[rebin_idx] - obs.mean[vector_idx];
					obs.error[vector_idx] += diff*diff;

					obs_meta.current_rebin++;
					obs.rebinning_means.resize(obs_meta.current_rebin*obs.mean.size(), 0);
					obs_meta.current_rebin_filling = 0;

				}
			}
		}
	}
	
	for(auto& entry : res.observables) {
		auto& obs = entry.second;
		for(size_t i = 0; i < obs.error.size(); i++) {
			double no_rebinning_error = sqrt(obs.autocorrelation_time[i]/(obs.total_sample_count-1)/obs.total_sample_count);

			obs.error[i] = sqrt(obs.error[i]/(obs.rebinning_bin_count-1)/(obs.total_sample_count/static_cast<double>(obs.rebinning_bin_length)));

			obs.autocorrelation_time[i] = 0.5*pow(no_rebinning_error/obs.error[i],2);
		}
	}

	evaluate_evalables(res);

	return res;
}

Lukas Weber's avatar
Lukas Weber committed
162
void merger::evaluate_evalables(results& res) {
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
	std::vector<observable_result> evalable_results;
	for(auto &eval : evalables_) {
		evalable_results.emplace_back();
		
		eval.jackknife(res,evalable_results.back());
	}

	for(auto &eval : evalable_results) {
		res.observables.emplace(eval.name, eval);
	}
}
/*
int merge(function<abstract_mc* (string&)> mccreator, int argc, char* argv[]) {
	MPI_Init(&argc,&argv);
        if (argc==1) {
                cout << " usage: "<< argv[0] <<" jobfilename [-s number_of_bins_to_be_skipped] [-t min_task max_task] [-r min_run max_run]" << endl;
                exit(1);
        }
        std::string jobfile(argv[1]);
        int task_min=1;
        int task_max=-1;
        int run_min=1;
        int run_max=-1;
        int skipbins=0;
        int rarg=2;
        while (argc>rarg) {
		if (argv[rarg][0]=='s' || argv[rarg][1]=='s') {
			rarg++;
			skipbins=atoi(argv[rarg]);
			rarg++;
		}
		else if (argv[rarg][0]=='t' || argv[rarg][1]=='t') {
			rarg++;
			task_min=atoi(argv[rarg]);
			rarg++;
			task_max=atoi(argv[rarg]);
			rarg++;
		}
		else if (argv[rarg][0]=='r' || argv[rarg][1]=='r') {
			rarg++;
			run_min=atoi(argv[rarg]);
			rarg++;
			run_max=atoi(argv[rarg]);
			rarg++;
		}
		else {
			cout<<"unknown option "<<argv[rarg]<<endl;
			exit(1);
		}
	};
	cout <<"Merging "<<jobfile;
	if (task_max==-1) {
		cout << " all task";
	}
        else {
                cout <<" task " <<task_min << " to " << task_max;
        }
        if (run_max==-1) {
                cout << " all runs";
        }
        else {
                cout <<" run " <<run_min << " to " << run_max;
        }
        if (skipbins) cout << " skipping the first " << skipbins <<" bins";
        cout << endl;

        parser parsedfile(jobfile+".alltasks");
        std::vector<string> taskfiles;
        taskfiles= parsedfile.return_vector< string >("@taskfiles");
        std::string masterfile = parsedfile.value_or_default<string>("masterfile",jobfile+".master");
        if (task_max==-1) task_max=taskfiles.size();
        for (int i=task_min-1;i<task_max;++i) {
                std::string taskfile = taskfiles[i];
                parser cfg(taskfile);
                std::string taskdir = cfg.value_of("taskdir");
                std::stringstream rb; rb << taskdir << "/run" << run_min << ".";
                std::string rundir = rb.str();
                abstract_mc* sys = mccreator(taskfile);
                if ((*sys)._read(rundir)) {
                        if ((*sys).measure.merge(rundir,skipbins)) cout << rundir <<endl;
                        if (1) {
                                int run_counter=run_min+1;
                                bool success=true;
                                while (success && ((run_max==-1) || (run_counter<=run_max))) {
                                        stringstream b;b<<taskdir<<"/run"<<run_counter<<".";
                                        success=(*sys).measure.merge(b.str(),skipbins);
                                        if (success) cout << b.str() <<endl;
                                        ++run_counter;
                                }
                                std::stringstream mfb;
                                if (run_max==-1) {
                                         mfb << taskdir << ".";
                                }
                                else {
                                        if (run_min==run_max) {
                                                mfb << taskdir <<"."<<run_min<<".";
                                        }
                                        else {
                                                mfb << taskdir <<"."<<run_min<<"."<<run_max<<".";
                                        }
                                }
                                if (skipbins) mfb <<"s"<<skipbins<<".";
                                mfb <<"out";
                                (*sys)._write_output(mfb.str());
                        }
                }
                delete sys;
        }
        return 0;
}
*/