ALPSCore reference
namedaccumulators.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 
10 
11 namespace alps {
12  namespace accumulators {
13 
14  namespace detail {
15 
16  template<typename A> struct AccumulatorBase {
17  typedef A accumulator_type;
18  typedef typename A::result_type result_type;
19 
21  template<typename ArgumentPack>
22  AccumulatorBase(const ArgumentPack& args,
23  typename std::enable_if<!std::is_base_of<AccumulatorBase,ArgumentPack>::value,int>::type =0)
24  : name(args[accumulator_name])
25  , wrapper(new accumulator_wrapper(A(args)))
26  {}
27 
29  template<typename ArgumentPack>
30  AccumulatorBase(const ArgumentPack& rhs,
31  typename std::enable_if<std::is_base_of<AccumulatorBase,ArgumentPack>::value,int>::type =0)
32  : name(rhs.name),
33  wrapper(std::shared_ptr<accumulator_wrapper>(rhs.wrapper->new_clone()))
34  { }
35 
37  template <typename T>
38  const AccumulatorBase& operator<<(const T& value) const
39  {
40  (*wrapper) << value;
41  return *this;
42  }
43 
45  std::shared_ptr<result_wrapper> result() const
46  {
47  return wrapper->result();
48  }
49 
51  AccumulatorBase& operator=(const AccumulatorBase& rhs)
52  {
53  // Self-assignment is handled correctly (albeit inefficiently)
54  this->name=rhs.name;
55  this->wrapper = std::shared_ptr<accumulator_wrapper>(rhs.wrapper->new_clone());
56  return *this;
57  }
58 
59 #ifdef ALPS_HAVE_MPI
60  // FIXME! TODO: Introduce a proper "interface"-like superclass? with all relevant accumulator methods?
62  void collective_merge(alps::mpi::communicator const & comm, int root)
63  {
64  this->wrapper->collective_merge(comm,root);
65  }
66 #endif
67 
68  std::string name;
69  std::shared_ptr<accumulator_wrapper> wrapper;
70  }; // end struct AccumulatorBase
71  } // end namespace detail
72 
73 
74  template<typename T> struct MeanAccumulator : public detail::AccumulatorBase<
75  impl::Accumulator<T, mean_tag, impl::Accumulator<T, count_tag, impl::AccumulatorBase<T> > >
76  > {
78  typedef detail::AccumulatorBase<accumulator_type> base_type;
79  typedef typename accumulator_type::result_type result_type;
80  BOOST_PARAMETER_CONSTRUCTOR(
82  (detail::AccumulatorBase<accumulator_type>),
83  accumulator_keywords,
84  (required (_accumulator_name, (std::string)))
85  )
86 
87  MeanAccumulator& operator=(const MeanAccumulator& rhs);
88  MeanAccumulator(const MeanAccumulator& rhs);
89  };
90 
91  template<typename T> struct NoBinningAccumulator : public detail::AccumulatorBase<
92  typename impl::Accumulator<T, error_tag, typename MeanAccumulator<T>::accumulator_type>
93  > {
95  typedef detail::AccumulatorBase<accumulator_type> base_type;
96  typedef typename accumulator_type::result_type result_type;
97  BOOST_PARAMETER_CONSTRUCTOR(
99  (detail::AccumulatorBase<accumulator_type>),
100  accumulator_keywords,
101  (required (_accumulator_name, (std::string)))
102  )
103  NoBinningAccumulator& operator=(const NoBinningAccumulator& rhs);
105  };
106 
107  template<typename T> struct LogBinningAccumulator : public detail::AccumulatorBase<
108  typename impl::Accumulator<T, binning_analysis_tag, typename NoBinningAccumulator<T>::accumulator_type>
109  > {
111  typedef detail::AccumulatorBase<accumulator_type> base_type;
112  typedef typename accumulator_type::result_type result_type;
113  BOOST_PARAMETER_CONSTRUCTOR(
115  (detail::AccumulatorBase<accumulator_type>),
116  accumulator_keywords,
117  (required (_accumulator_name, (std::string)))
118  )
119  LogBinningAccumulator& operator=(const LogBinningAccumulator& rhs);
122  typedef typename autocorrelation_type<accumulator_type>::type autocorrelation_type;
124  autocorrelation_type tau() const;
125  };
126 
127  template<typename T> struct FullBinningAccumulator : public detail::AccumulatorBase<
128  typename impl::Accumulator<T, max_num_binning_tag, typename LogBinningAccumulator<T>::accumulator_type>
129  > {
131  typedef detail::AccumulatorBase<accumulator_type> base_type;
132  typedef typename accumulator_type::result_type result_type;
133  BOOST_PARAMETER_CONSTRUCTOR(
135  (detail::AccumulatorBase<accumulator_type>),
136  accumulator_keywords,
137  (required (_accumulator_name, (std::string)))
138  (optional
139  (_max_bin_number, (std::size_t))
140  )
141  )
142  FullBinningAccumulator& operator=(const FullBinningAccumulator& rhs);
144 
146  typedef typename autocorrelation_type<accumulator_type>::type autocorrelation_type;
148  autocorrelation_type tau() const;
149  };
150 
151  #define ALPS_ACCUMULATOR_REGISTER_OPERATOR(A) \
152  template<typename T> accumulator_set & operator<<(accumulator_set & set, const A <T> & arg);
153 
154  ALPS_ACCUMULATOR_REGISTER_OPERATOR(MeanAccumulator)
155  ALPS_ACCUMULATOR_REGISTER_OPERATOR(NoBinningAccumulator)
156  ALPS_ACCUMULATOR_REGISTER_OPERATOR(LogBinningAccumulator)
157  ALPS_ACCUMULATOR_REGISTER_OPERATOR(FullBinningAccumulator)
158  #undef ALPS_ACCUMULATOR_REGISTER_OPERATOR
159 
160  }
161 }
detail::AccumulatorBase< accumulator_type > base_type
impl::Accumulator< T, mean_tag, impl::Accumulator< T, count_tag, impl::AccumulatorBase< T > > > accumulator_type
detail::AccumulatorBase< accumulator_type > base_type
STL namespace.
detail::AccumulatorBase< accumulator_type > base_type
Encapsulation of an MPI communicator and some communicator-related operations.
Definition: mpi.hpp:111
#define ALPS_ACCUMULATOR_REGISTER_OPERATOR(A)
std::ostream & operator<<(std::ostream &os, const result_wrapper &arg)
detail::AccumulatorBase< accumulator_type > base_type
accumulator_type::result_type result_type
accumulator_type::result_type result_type
accumulator_type::result_type result_type
traits< Acc >::result_type result(const Acc &acc)
Definition: util.hpp:53
impl::Accumulator< T, error_tag, typename MeanAccumulator< T >::accumulator_type > accumulator_type
impl::Accumulator< T, binning_analysis_tag, typename NoBinningAccumulator< T >::accumulator_type > accumulator_type
impl::Accumulator< T, max_num_binning_tag, typename LogBinningAccumulator< T >::accumulator_type > accumulator_type
autocorrelation_type< accumulator_type >::type autocorrelation_type
Data type corresponding to autocorrelation.