Commit c43f9386 authored by Lukas Weber's avatar Lukas Weber

optimize observable::add/add support for many containers

parent 07ab802f
......@@ -16,10 +16,10 @@ public:
const std::string &name() const;
template<class T>
void add(T);
auto add(T val) -> decltype(val + val, void());
template<class T>
void add(const std::vector<T> &);
auto add(const T& val) -> decltype(val[0], void());
template<class T>
void add(const std::valarray<T> &);
......@@ -34,7 +34,6 @@ public:
// switch copy with target rank.
// useful for parallel tempering mode
void mpi_sendrecv(int target_rank);
private:
static const size_t initial_bin_length = 1000;
......@@ -48,38 +47,13 @@ private:
};
template<class T>
void observable::add(T val) {
std::vector<T> v = {val};
add(v);
auto observable::add(T val) -> decltype(val + val, void()) {
add(std::array<T,1>{val});
}
template<class T>
void observable::add(const std::vector<T> &val) {
if(vector_length_ != val.size()) {
throw std::runtime_error{
fmt::format("observable::add: added vector has unexpected size ({}). Observable was "
"initialized with vector length ({})",
val.size(), vector_length_)};
}
for(size_t j = 0; j < vector_length_; ++j)
samples_[j + current_bin_ * vector_length_] += static_cast<double>(val[j]);
current_bin_filling_++;
if(current_bin_filling_ == bin_length_) { // need to start a new bin next time
if(bin_length_ > 1) {
for(size_t j = 0; j < vector_length_; ++j) {
samples_[current_bin_ * vector_length_ + j] /= bin_length_;
}
}
current_bin_++;
samples_.resize((current_bin_ + 1) * vector_length_);
current_bin_filling_ = 0;
}
}
template<class T>
void observable::add(const std::valarray<T> &val) {
auto observable::add(const T &val) -> decltype(val[0], void()) {
if(vector_length_ != val.size()) {
throw std::runtime_error{
fmt::format("observable::add: added vector has unexpected size ({}). Observable was "
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment