TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
mc_measure.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2013-2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018-2022 Simons Foundation
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You may obtain a copy of the License at
16// https://www.gnu.org/licenses/gpl-3.0.txt
17//
18// Authors: Michel Ferrero, Olivier Parcollet, Nils Wentzell
19
24
25#pragma once
26
27#include "./concepts.hpp"
28#include "../utility/timer.hpp"
29
30#include <h5/h5.hpp>
31#include <mpi/communicator.hpp>
32
33#include <complex>
34#include <concepts>
35#include <cstdint>
36#include <memory>
37#include <string>
38#include <type_traits>
39#include <utility>
40#include <vector>
41
42namespace triqs::mc_tools {
43
44 // Forward declaration.
45 template <DoubleOrComplex MCSignType> class measure_set;
46
62 template <DoubleOrComplex MCSignType> class measure {
63 private:
64 // MC measure concept defines the interface for MC measures.
65 struct measure_concept {
66 virtual ~measure_concept() = default;
67 virtual void accumulate(MCSignType) = 0;
68 virtual void collect_results(mpi::communicator const &) = 0;
69 [[nodiscard]] virtual std::string report() const = 0;
70 virtual void h5write(h5::group, std::string const &) const = 0;
71 virtual void h5read(h5::group, std::string const &) = 0;
72
73 // // Methods only implemented in measure_set.
74 [[nodiscard]] virtual std::string ms_get_timings(std::string const &) const = 0;
75 [[nodiscard]] virtual std::vector<std::string> ms_names() const = 0;
76 };
77
78 // MC measure model implements the MC measure concept by calling the appropriate methods of the type erased object.
79 template <typename T>
81 struct measure_model : public measure_concept {
82 static constexpr bool is_measure_set = std::is_same_v<T, measure_set<MCSignType>>;
83 T measure_;
84 measure_model(T m) : measure_{std::move(m)} {}
85 void accumulate(MCSignType sign) override { return measure_.accumulate(sign); }
86 void collect_results(mpi::communicator const &c) override { measure_.collect_results(c); }
87 [[nodiscard]] std::string report() const override {
88 if constexpr (requires {
89 { measure_.report() } -> std::convertible_to<std::string>;
90 })
91 return measure_.report();
92 return {};
93 }
94 void h5write(h5::group g, std::string const &name) const override {
95 if constexpr (h5::Storable<T>) h5::write(g, name, measure_);
96 }
97 void h5read(h5::group g, std::string const &name) override {
98 if constexpr (h5::Storable<T>) h5::read(g, name, measure_);
99 }
100
101 // Methods only implemented in measure_set.
102 [[nodiscard]] std::string ms_get_timings(std::string const &prefix) const override {
103 if constexpr (is_measure_set) return measure_.get_timings(prefix);
104 return {};
105 }
106 [[nodiscard]] std::vector<std::string> ms_names() const override {
107 if constexpr (is_measure_set) return measure_.names();
108 return {};
109 }
110 };
111
112 public:
121 template <typename T>
122 requires(MCMeasure<T, MCSignType> && !std::is_same_v<T, measure>)
123 measure(T m, bool enable_timer, bool enable_report)
124 : ptr_{std::make_unique<measure_model<T>>(std::move(m))},
125 enable_timer_(enable_timer),
126 enable_report_(enable_report),
127 is_measure_set_{std::is_same_v<T, measure_set<MCSignType>>} {}
128
130 measure(measure const &) = delete;
131
133 measure &operator=(measure const &) = delete;
134
136 measure(measure &&) = default;
137
139 measure &operator=(measure &&) = default;
140
146 void accumulate(MCSignType sign) {
147 ++count_;
148 if (enable_timer_) timer_.start();
149 ptr_->accumulate(sign);
150 if (enable_timer_) timer_.stop();
151 }
152
158 void collect_results(mpi::communicator const &c);
159
166 [[nodiscard]] std::string report() const;
167
176 [[nodiscard]] std::string get_timings(std::string const &name, std::string const &prefix = "") const;
177
183 [[nodiscard]] std::vector<std::string> names() const;
184
186 [[nodiscard]] double duration() const;
187
189 [[nodiscard]] auto count() const { return count_; }
190
192 [[nodiscard]] auto is_set() const { return is_measure_set_; }
193
203 friend void h5_write(h5::group g, std::string const &name, measure const &m) { m.ptr_->h5write(g, name); }
204
214 friend void h5_read(h5::group g, std::string const &name, measure &m) { m.ptr_->h5read(g, name); }
215
216 private:
217 std::unique_ptr<measure_concept> ptr_;
218 std::uint64_t count_{0};
220 bool enable_timer_{false};
221 bool enable_report_{false};
222 bool is_measure_set_{false};
223 };
224
225 // Explicit template instantiation declarations.
226 extern template class measure<double>;
227 extern template class measure<std::complex<double>>;
228
229} // namespace triqs::mc_tools
Type erasure class for MC measures.
measure & operator=(measure const &)=delete
Deleted copy assignment operator.
double duration() const
Get the duration of the cumulative accumulate() and collect_results() calls.
measure(T m, bool enable_timer, bool enable_report)
Constructor takes an object that models the triqs::mc_tools::MCMeasure concept and erases its type.
measure & operator=(measure &&)=default
Default move assignment operator leaves the moved from object in an empty state.
void collect_results(mpi::communicator const &c)
Collect results from multiple MPI processes.
measure(measure const &)=delete
Deleted copy constructor.
friend void h5_read(h5::group g, std::string const &name, measure &m)
Read the measure object from HDF5.
measure(measure &&)=default
Default move constructor leaves the moved from object in an empty state.
std::string report() const
Report information about the measure.
auto is_set() const
Is the measure object a measure set?
auto count() const
Get the number of measurements performed.
friend void h5_write(h5::group g, std::string const &name, measure const &m)
Write the measure object to HDF5.
void accumulate(MCSignType sign)
Perform the measurement on the current MC configuration.
std::vector< std::string > names() const
Get a vector of all the measure names in a measure set.
std::string get_timings(std::string const &name, std::string const &prefix="") const
Get a formatted string showing the runtime of the accumulate() and collect_results() calls.
Accumulating wall-clock timer based on std::chrono::high_resolution_clock.
Definition timer.hpp:39
Check if a type can be used as a MC measure.
Definition concepts.hpp:77
Provides concepts for the MC tools.
A wall-clock timer that accumulates elapsed seconds across start/stop intervals.