ALPSCore reference
mean.cpp
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 #include <boost/preprocessor/tuple/to_seq.hpp>
8 #include <boost/preprocessor/seq/for_each.hpp>
9 
10 #include <alps/config.hpp>
11 
13 #include <alps/hdf5/vector.hpp>
14 
15 #define ALPS_ACCUMULATOR_VALUE_TYPES_SEQ BOOST_PP_TUPLE_TO_SEQ(ALPS_ACCUMULATOR_VALUE_TYPES_SIZE, (ALPS_ACCUMULATOR_VALUE_TYPES))
16 
17 namespace alps {
18  namespace accumulators {
19  namespace impl {
20 
21  //
22  // Accumulator<T, mean_tag, B>
23  //
24 
25  template<typename T, typename B>
27  using alps::numeric::operator/;
28 
29  // TODO: make library for scalar type
31 
32  return mean_type(m_sum) / cnt;
33  }
34 
35  template<typename T, typename B>
37  using alps::numeric::operator+=;
39 
40  B::operator()(val);
41  check_size(m_sum, val);
42  m_sum += val;
43  }
44 
45  template<typename T, typename B>
47  B::save(ar);
48  ar["mean/value"] = mean();
49  }
50 
51  template<typename T, typename B>
52  void Accumulator<T, mean_tag, B>::load(hdf5::archive & ar) { // TODO: make archive const
53  using alps::numeric::operator*;
54 
55  B::load(ar);
57  ar["mean/value"] >> mean;
58  // TODO: make library for scalar type
60  m_sum = mean * cnt;
61  }
62 
63  template<typename T, typename B>
64  bool Accumulator<T, mean_tag, B>::can_load(hdf5::archive & ar) { // TODO: make archive const
66  const char name[]="mean/value";
67  const std::size_t ndim=std::is_scalar<T>::value? 0 : get_extent(T()).size();
68  return B::can_load(ar) &&
69  detail::archive_trait<mean_type>::can_load(ar, name, ndim);
70  }
71 
72 
73 #ifdef ALPS_HAVE_MPI
74  template<typename T, typename B>
76  alps::mpi::communicator const & comm
77  , int root
78  ) {
79  if (comm.rank() == root) {
80  B::collective_merge(comm, root);
81  B::reduce_if(comm, T(m_sum), m_sum, std::plus<typename alps::hdf5::scalar_type<T>::type>(), root);
82  } else
83  const_cast<Accumulator<T, mean_tag, B> const *>(this)->collective_merge(comm, root);
84  }
85 
86  template<typename T, typename B>
88  alps::mpi::communicator const & comm
89  , int root
90  ) const {
91  B::collective_merge(comm, root);
92  if (comm.rank() == root)
93  throw std::runtime_error("A const object cannot be root" + ALPS_STACKTRACE);
94  else
95  B::reduce_if(comm, m_sum, std::plus<typename alps::hdf5::scalar_type<T>::type>(), root);
96  }
97 #endif
98 
99  template<typename T, typename B>
101  return m_sum;
102  }
103 
104  #define ALPS_ACCUMULATOR_INST_MEAN_ACC(r, data, T) \
105  template class Accumulator<T, mean_tag, \
106  Accumulator<T, count_tag, AccumulatorBase<T>>>;
108 
109  //
110  // Result<T, mean_tag, B>
111  //
112 
113  template<typename T, typename B>
114  void Result<T, mean_tag, B>::save(hdf5::archive & ar) const {
115  B::save(ar);
116  ar["mean/value"] = mean();
117  }
118 
119  template<typename T, typename B>
121  B::load(ar);
122  ar["mean/value"] >> m_mean;
123  }
124 
125  template<typename T, typename B>
126  bool Result<T, mean_tag, B>::can_load(hdf5::archive & ar) { // TODO: make archive const
128  const char name[]="mean/value";
129  const std::size_t ndim=std::is_scalar<T>::value? 0 : get_extent(T()).size();
130  return B::can_load(ar) &&
131  detail::archive_trait<mean_type>::can_load(ar, name, ndim);
132  }
133 
134  template<typename T, typename B>
136  using alps::numeric::operator-;
137  m_mean = -m_mean;
138  B::negate();
139  }
140 
141  template<typename T, typename B>
143  using alps::numeric::operator/;
144  // TODO: make library for scalar type
146  m_mean = one / m_mean;
147  B::inverse();
148  }
149 
150  #define NUMERIC_FUNCTION_IMPLEMENTATION(FUNCTION_NAME) \
151  template<typename T, typename B> \
152  void Result<T, mean_tag, B>:: FUNCTION_NAME () { \
153  B:: FUNCTION_NAME (); \
154  using std:: FUNCTION_NAME ; \
155  using alps::numeric:: FUNCTION_NAME ; \
156  m_mean = FUNCTION_NAME (m_mean); \
157  }
158 
171 
172  #undef NUMERIC_FUNCTION_IMPLEMENTATION
173 
174  #define NUMERIC_FUNCTION_IMPLEMENTATION(FUNCTION_NAME) \
175  template<typename T, typename B> \
176  void Result<T, mean_tag, B>:: FUNCTION_NAME () { \
177  B:: FUNCTION_NAME (); \
178  using alps::numeric:: FUNCTION_NAME ; \
179  using alps::numeric:: FUNCTION_NAME ; \
180  m_mean = FUNCTION_NAME (m_mean); \
181  }
182 
186 
187  #undef NUMERIC_FUNCTION_IMPLEMENTATION
188 
189  #define ALPS_ACCUMULATOR_INST_MEAN_RESULT(r, data, T) \
190  template class Result<T, mean_tag, \
191  Result<T, count_tag, ResultBase<T>>>;
193  }
194  }
195 }
result_wrapper cbrt(result_wrapper const &arg)
void check_size(T &, U const &)
Definition: check_size.hpp:40
#define ALPS_ACCUMULATOR_INST_MEAN_RESULT(r, data, T)
Definition: mean.cpp:189
void load(archive &ar, std::string const &path, T &value, std::vector< std::size_t > chunk=std::vector< std::size_t >(), std::vector< std::size_t >=std::vector< std::size_t >())
Definition: archive.hpp:309
result_wrapper cos(result_wrapper const &arg)
result_wrapper sqrt(result_wrapper const &arg)
result_wrapper cb(result_wrapper const &arg)
result_wrapper sq(result_wrapper const &arg)
std::vector< std::size_t > get_extent(T const &value)
Definition: archive.hpp:280
Encapsulation of an MPI communicator and some communicator-related operations.
Definition: mpi.hpp:111
Metafunction returning "mathematical scalar" type for type T.
Definition: scalar.hpp:28
#define ALPS_ACCUMULATOR_VALUE_TYPES_SEQ
Definition: mean.cpp:15
mean_type< T >::type mean(T const &arg)
Definition: mean.hpp:47
result_wrapper log(result_wrapper const &arg)
result_wrapper tanh(result_wrapper const &arg)
result_wrapper acos(result_wrapper const &arg)
alps::accumulators::mean_type< B >::type mean_type
Definition: mean.hpp:74
#define NUMERIC_FUNCTION_IMPLEMENTATION(FUNCTION_NAME)
Definition: mean.cpp:174
#define ALPS_STACKTRACE
Definition: stacktrace.hpp:37
result_wrapper sin(result_wrapper const &arg)
result_wrapper asin(result_wrapper const &arg)
int rank() const
Returns process rank in this communicator.
Definition: mpi.hpp:156
result_wrapper tan(result_wrapper const &arg)
result_wrapper abs(result_wrapper const &arg)
result_wrapper sinh(result_wrapper const &arg)
#define ALPS_ACCUMULATOR_INST_MEAN_ACC(r, data, T)
Definition: mean.cpp:104
result_wrapper cosh(result_wrapper const &arg)
count_type< T >::type count(T const &arg)
Definition: count.hpp:39
result_wrapper atan(result_wrapper const &arg)
void save(archive &ar, std::string const &path, T const &value, std::vector< std::size_t >=std::vector< std::size_t >(), std::vector< std::size_t > chunk=std::vector< std::size_t >(), std::vector< std::size_t >=std::vector< std::size_t >())
Definition: archive.hpp:292