observable.h 1.79 KB
Newer Older
1
#pragma once
Stefan Weßel's avatar
ic  
Stefan Weßel committed
2

3
#include "iodump.h"
Stefan Weßel's avatar
ic  
Stefan Weßel committed
4
#include <cmath>
5
#include <map>
Stefan Weßel's avatar
ic  
Stefan Weßel committed
6 7 8
#include <string>
#include <vector>

9 10
namespace loadl {

11 12
class observable {
public:
13
	observable(std::string name, size_t bin_length, size_t vector_length);
Stefan Weßel's avatar
ic  
Stefan Weßel committed
14

15
	const std::string &name() const;
Stefan Weßel's avatar
ic  
Stefan Weßel committed
16

17
	template<class T>
18
	void add(T);
Stefan Weßel's avatar
ic  
Stefan Weßel committed
19

20 21 22 23
	template<class T>
	void add(const std::vector<T> &);

	void checkpoint_write(const iodump::group &dump_file) const;
24 25

	// This will empty the cache of already completed bins
26 27
	void measurement_write(const iodump::group &meas_file);

Lukas Weber's avatar
Lukas Weber committed
28
	void checkpoint_read(const std::string &name, const iodump::group &dump_file);
29

30 31 32
	// switch copy with target rank.
	// useful for parallel tempering mode
	void mpi_sendrecv(int target_rank);
Lukas Weber's avatar
Lukas Weber committed
33

34
private:
35 36
	static const size_t initial_bin_length = 1000;

37
	std::string name_;
38 39 40 41
	size_t bin_length_{};
	size_t vector_length_{};
	size_t current_bin_{};
	size_t current_bin_filling_{};
Stefan Weßel's avatar
ic  
Stefan Weßel committed
42

43
	std::vector<double> samples_;
Stefan Weßel's avatar
ic  
Stefan Weßel committed
44 45
};

46
template<class T>
47 48 49 50
void observable::add(T val) {
	std::vector<T> v = {val};
	add(v);
}
Stefan Weßel's avatar
ic  
Stefan Weßel committed
51

52 53
template<class T>
void observable::add(const std::vector<T> &val) {
54
	if(vector_length_ != val.size()) {
Lukas Weber's avatar
Lukas Weber committed
55 56 57 58
		throw std::runtime_error{
		    fmt::format("observable::add: added vector has unexpected size ({}). Observable was "
		                "initialized with vector length ({})",
		                val.size(), vector_length_)};
59
	}
60 61 62

	for(size_t j = 0; j < vector_length_; ++j)
		samples_[j + current_bin_ * vector_length_] += static_cast<double>(val[j]);
63
	current_bin_filling_++;
64 65

	if(current_bin_filling_ == bin_length_) { // need to start a new bin next time
66 67
		if(bin_length_ > 1) {
			for(size_t j = 0; j < vector_length_; ++j) {
68
				samples_[current_bin_ * vector_length_ + j] /= bin_length_;
69 70
			}
		}
71
		current_bin_++;
72 73
		samples_.resize((current_bin_ + 1) * vector_length_);
		current_bin_filling_ = 0;
74 75
	}
}
76
}