ALPSCore reference
accumulators_.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 
7 #pragma once
8 
9 #include <alps/config.hpp>
10 
11 #include <alps/hdf5/vector.hpp>
12 #include <alps/hdf5/archive.hpp>
13 
14 #ifdef ALPS_HAVE_MPI
16 #endif
17 
19 
20 #include <boost/preprocessor/tuple/to_seq.hpp>
21 #include <boost/preprocessor/seq/for_each.hpp>
22 
23 #include <string>
24 #include <iostream>
25 #include <typeinfo>
26 #include <stdexcept>
27 
28 #ifndef ALPS_ACCUMULATOR_VALUE_TYPES_SEQ
29  #define ALPS_ACCUMULATOR_VALUE_TYPES_SEQ BOOST_PP_TUPLE_TO_SEQ(ALPS_ACCUMULATOR_VALUE_TYPES_SIZE, (ALPS_ACCUMULATOR_VALUE_TYPES))
30 #endif
31 
32 namespace alps {
33  namespace accumulators {
34 
35  class result_wrapper;
36  class accumulator_wrapper;
37 
38  namespace wrapped {
39 
40  template<typename accumulator_type> class virtual_result_wrapper {
41  public:
42 
43  // default constructor
45 
46  // constructor from raw accumulator
48 
49  // copy constructor
51 
52  // constructor from hdf5
54 
55  virtual ~virtual_result_wrapper();
56 
57  // count
58  boost::uint64_t count() const;
59 
60  private:
61 
62  #define ALPS_ACCUMULATOR_MEAN_IMPL(r, data, T) \
63  T mean_impl(T) const;
65  #undef ALPS_ACCUMULATOR_MEAN_IMPL
66 
67  #define ALPS_ACCUMULATOR_ERROR_IMPL(r, data, T) \
68  T error_impl(T) const;
70  #undef ALPS_ACCUMULATOR_ERROR_IMPL
71 
72  public:
73 
74  // mean
75  template<typename T> T mean() const {
76  return mean_impl(T());
77  }
78 
79  // error
80  template<typename T> T error() const {
81  return mean_impl(T());
82  }
83 
84  // save
85  void save(hdf5::archive & ar) const;
86 
87  // load
88  void load(hdf5::archive & ar);
89 
90  // print
91  void print(std::ostream & os) const;
92 
93  private:
94 
95  std::ptrdiff_t * m_cnt;
96  result_wrapper * m_ptr;
97  };
98 
100  public:
101  // default constructor
103 
104  // constructor from raw accumulator
106 
107  // copy constructor
109 
110  // constructor from hdf5
112 
113  virtual ~virtual_accumulator_wrapper();
114 
115  // operator(), operator<<
116  #define ALPS_ACCUMULATOR_OPERATOR_CALL(r, data, T) \
117  virtual_accumulator_wrapper & operator()(T const & value); \
118  virtual_accumulator_wrapper & operator<<(T const & value) { \
119  (*this)(value); \
120  return (*this); \
121  }
123  #undef ALPS_ACCUMULATOR_OPERATOR_CALL
124 
126  void merge(const virtual_accumulator_wrapper & rhs);
127 
128  virtual_accumulator_wrapper & operator=(std::shared_ptr<virtual_accumulator_wrapper> const & rhs);
129 
130  // count
131  boost::uint64_t count() const;
132 
133  private:
134 
135  #define ALPS_ACCUMULATOR_MEAN_IMPL(r, data, T) \
136  T mean_impl(T) const;
138  #undef ALPS_ACCUMULATOR_MEAN_IMPL
139 
140  #define ALPS_ACCUMULATOR_ERROR_IMPL(r, data, T) \
141  T error_impl(T) const;
143  #undef ALPS_ACCUMULATOR_ERROR_IMPL
144 
145  public:
146 
147  // mean
148  template<typename T> T mean() const {
149  return mean_impl(T());
150  }
151 
152  // error
153  template<typename T> T error() const {
154  return mean_impl(T());
155  }
156 
157  // save
158  void save(hdf5::archive & ar) const;
159 
160  // load
161  void load(hdf5::archive & ar);
162 
163  // reset
164  void reset() const;
165 
166  // result
167  std::shared_ptr<virtual_result_wrapper<virtual_accumulator_wrapper> > result() const;
168 
169  // print
170  void print(std::ostream & os) const;
171 
172 #ifdef ALPS_HAVE_MPI
173  // collective_merge
174  void collective_merge(alps::mpi::communicator const & comm, int root);
175  void collective_merge(alps::mpi::communicator const & comm, int root) const;
176 #endif
177 
178  private:
179 
180  std::ptrdiff_t * m_cnt;
181  accumulator_wrapper * m_ptr;
182  };
183 
184  inline std::ostream & operator<<(std::ostream & os, const virtual_accumulator_wrapper & arg) {
185  arg.print(os);
186  return os;
187  }
188 
189  inline void reset(virtual_accumulator_wrapper & arg) {
190  return arg.reset();
191  }
192 
193  }
194  }
195 
198 
199  template<typename T> struct MeanAccumulator {
200  public:
201  MeanAccumulator(std::string const & name): m_name(name) {}
202  std::string const & name() const { return m_name; }
203  private:
204  std::string m_name;
205  };
206 
207  template<typename T> struct NoBinningAccumulator {
208  public:
209  NoBinningAccumulator(std::string const & name): m_name(name) {}
210  std::string const & name() const { return m_name; }
211  private:
212  std::string m_name;
213  };
214 
215  template<typename T> struct LogBinningAccumulator {
216  public:
217  LogBinningAccumulator(std::string const & name): m_name(name) {}
218  std::string const & name() const { return m_name; }
219  private:
220  std::string m_name;
221  };
222 
223  template<typename T> struct FullBinningAccumulator {
224  public:
225  FullBinningAccumulator(std::string const & name): m_name(name) {}
226  std::string const & name() const { return m_name; }
227  private:
228  std::string m_name;
229  };
230 
231  #define ALPS_ACCUMULATOR_ADD_ACCUMULATOR(r, data, T) \
232  accumulator_set & operator<<(accumulator_set & set, const MeanAccumulator< T > & arg); \
233  accumulator_set & operator<<(accumulator_set & set, const NoBinningAccumulator< T > & arg); \
234  accumulator_set & operator<<(accumulator_set & set, const LogBinningAccumulator< T > & arg); \
235  accumulator_set & operator<<(accumulator_set & set, const FullBinningAccumulator< T > & arg);
237  #undef ALPS_ACCUMULATOR_ADD_ACCUMULATOR
238 }
239 
#define ALPS_ACCUMULATOR_OPERATOR_CALL(r, data, T)
impl::wrapper_set< result_wrapper > result_set
NoBinningAccumulator(std::string const &name)
LogBinningAccumulator(std::string const &name)
Encapsulation of an MPI communicator and some communicator-related operations.
Definition: mpi.hpp:111
void reset(virtual_accumulator_wrapper &arg)
std::string const & name() const
#define ALPS_ACCUMULATOR_MEAN_IMPL(r, data, T)
std::string const & name() const
impl::wrapper_set< accumulator_wrapper > accumulator_set
#define ALPS_ACCUMULATOR_ADD_ACCUMULATOR(r, data, T)
MeanAccumulator(std::string const &name)
std::string const & name() const
std::string const & name() const
traits< Acc >::result_type result(const Acc &acc)
Definition: util.hpp:53
#define ALPS_ACCUMULATOR_VALUE_TYPES_SEQ
FullBinningAccumulator(std::string const &name)
#define ALPS_ACCUMULATOR_ERROR_IMPL(r, data, T)
std::vector< T > & merge(std::vector< T > &left, const std::vector< T > &right)
Vector merge.
std::ostream & operator<<(std::ostream &os, const virtual_accumulator_wrapper &arg)