TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
mc_move.cpp
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#include "./concepts.hpp"
26#include "./mc_move.hpp"
27
28#include <fmt/format.h>
29#include <mpi/mpi.hpp>
30
31#include <complex>
32#include <cstdint>
33#include <string>
34
35namespace triqs::mc_tools {
36
37 template <DoubleOrComplex MCSignType> void move<MCSignType>::collect_statistics(mpi::communicator const &c) {
38 std::uint64_t nacc_tot = mpi::all_reduce(nacc_, c);
39 std::uint64_t nprop_tot = mpi::all_reduce(nprop_, c);
40 acc_rate_ = (nprop_tot > 0 ? static_cast<double>(nacc_tot) / static_cast<double>(nprop_tot) : 0.0);
41 ptr_->collect_statistics(c);
42 }
43
44 template <DoubleOrComplex MCSignType> void move<MCSignType>::clear_statistics() {
45 nacc_ = 0;
46 nprop_ = 0;
47 acc_rate_ = -1;
48 timer_.reset();
49 ptr_->ms_clear_statistics();
50 }
51
52 template <DoubleOrComplex MCSignType> std::string move<MCSignType>::get_statistics(std::string const &name, std::string const &prefix) const {
53 if (is_move_set_) {
54 auto str = fmt::format("{}Move set {}: Proposed = {}, Accepted = {}, Rate = {:.4f}\n", prefix, name, nprop_, nacc_, acc_rate_);
55 return str + ptr_->ms_get_statistics(prefix + " ");
56 } else {
57 return fmt::format("{}Move {}: Proposed = {}, Accepted = {}, Rate = {:.4f}\n", prefix, name, nprop_, nacc_, acc_rate_);
58 }
59 }
60
61 template <DoubleOrComplex MCSignType> std::string move<MCSignType>::get_timings(std::string const &name, std::string const &prefix) const {
62 if (is_move_set_) {
63 auto str = fmt::format("{}Move set {}: Duration = {:.4f}\n", prefix, name, duration());
64 return str + ptr_->ms_get_timings(prefix + " ");
65 } else {
66 return fmt::format("{}Move {}: Duration = {:.4f}\n", prefix, name, duration());
67 }
68 }
69
70 // Explicit template instantiations.
71 template class move<double>;
72 template class move<std::complex<double>>;
73
74} // namespace triqs::mc_tools
Type erasure class for MC moves.
Definition mc_move.hpp:68
std::string get_timings(std::string const &name, std::string const &prefix="") const
Get a formatted string showing the runtime of the move.
Definition mc_move.cpp:61
void clear_statistics()
Reset the gathered statistics to their initial states.
Definition mc_move.cpp:44
std::string get_statistics(std::string const &name, std::string const &prefix="") const
Get a formatted string showing the statistics of the move (and other moves if it is a move set).
Definition mc_move.cpp:52
double duration() const
Get the duration of the cumulative attempt(), accept(), and reject() calls.
Definition mc_move.hpp:239
void collect_statistics(mpi::communicator const &c)
Collect statistics from multiple MPI processes.
Definition mc_move.cpp:37
Provides type erasure for MC moves.
Provides concepts for the MC tools.