ALPSCore reference
autocorr.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 #pragma once
7 
8 #include <alps/alea/core.hpp>
9 #include <alps/alea/util.hpp>
10 #include <alps/alea/variance.hpp>
11 
12 #include <vector>
13 
14 // Forward declarations
15 
16 namespace alps { namespace alea {
17  template <typename T> class autocorr_acc;
18  template <typename T> class autocorr_result;
19 
20  template <typename T> class batch_result;
21 
22  template <typename T>
23  void serialize(serializer &, const std::string &, const autocorr_result<T> &);
24 
25  template <typename T>
26  void deserialize(deserializer &, const std::string &, autocorr_result<T> &);
27 
28  template <typename T>
29  std::ostream &operator<<(std::ostream &, const autocorr_result<T> &);
30 }}
31 
32 // Actual declarations
33 
34 namespace alps { namespace alea {
35 
65 template <typename T>
66 class autocorr_acc
67 {
68 public:
69  using value_type = T;
72 
73 public:
74  autocorr_acc(size_t size=1, size_t batch_size=1, size_t granularity=2);
75 
77  void reset();
78 
80  void set_size(size_t size);
81 
83  void set_batch_size(size_t batch_size);
84 
86  void set_granularity(size_t granularity);
87 
89  bool valid() const { return !level_.empty(); }
90 
92  size_t size() const { return size_; }
93 
95  autocorr_acc& operator<<(const computed<T>& src){ add(src, 1); return *this; }
96 
98  autocorr_acc &operator<<(const autocorr_result<T> &result);
99 
101  size_t count() const { return count_; }
102 
104  autocorr_result<T> result() const;
105 
108 
109  size_t nlevel() const { return level_.size(); }
110 
111  const level_acc_type &level(size_t i) const { return level_[i]; }
112 
113 protected:
114  void add(const computed<T> &source, size_t count);
115 
116  void add_level();
117 
118  void finalize_to(autocorr_result<T> &result);
119 
120 private:
121  size_t size_, batch_size_, count_, nextlevel_, granularity_;
122  std::vector<level_acc_type> level_;
123 
124  friend class batch_result<T>;
125 };
126 
127 template <typename T>
128 struct traits< autocorr_acc<T> >
129 {
130  typedef T value_type;
135 };
136 
137 extern template class autocorr_acc<double>;
138 extern template class autocorr_acc<std::complex<double> >;
139 
140 
146 template <typename T>
147 class autocorr_result
148 {
149 public:
150  typedef T value_type;
153 
154 public:
155  autocorr_result(size_t nlevel=0) : level_(nlevel) { }
156 
158  bool valid() const { return !level_.empty(); }
159 
161  size_t size() const { return level_[0].size(); }
162 
164  size_t count() const { return level_[0].count(); }
165 
167  double count2() const;
168 
170  const column<T> &mean() const { return level_[0].mean(); }
171 
173  column<var_type> var() const;
174 
176  column<var_type> stderror() const;
177 
179  column<var_type> tau() const;
180 
182  bool tau_available() const { return find_level(DEFAULT_MIN_SAMPLES) > 0; }
183 
185  void reduce(const reducer &r) { reduce(r, true, true); }
186 
188  friend void serialize<>(serializer &, const std::string &, const autocorr_result &);
189 
191  friend void deserialize<>(deserializer &, const std::string &, autocorr_result &);
192 
194  friend std::ostream &operator<< <>(std::ostream &, const autocorr_result &);
195 
196  size_t find_level(size_t min_samples) const;
197 
198  size_t batch_size(size_t level) const;
199 
200  size_t nlevel() const { return level_.size(); }
201 
202  const level_result_type &level(size_t i) const { return level_[i]; }
203 
204  level_result_type &level(size_t i) { return level_[i]; }
205 
206 protected:
207  void reduce(const reducer &r, bool do_pre_commit, bool do_post_commit);
208 
209 private:
210  const static size_t DEFAULT_MIN_SAMPLES = 1024;
211  std::vector<level_result_type> level_;
212 
213  friend class autocorr_acc<T>;
214 };
215 
217 template <typename T>
218 bool operator==(const autocorr_result<T> &r1, const autocorr_result<T> &r2);
219 template <typename T>
221 {
222  return !operator==(r1, r2);
223 }
224 
225 template<typename T> struct is_alea_acc<autocorr_acc<T>> : std::true_type {};
226 template<typename T> struct is_alea_result<autocorr_result<T>> : std::true_type {};
227 
228 template <typename T>
230 {
231  typedef T value_type;
235 
236  const static bool HAVE_MEAN = true;
237  const static bool HAVE_VAR = true;
238  const static bool HAVE_COV = false;
239  const static bool HAVE_TAU = true;
240  const static bool HAVE_BATCH = false;
241 };
242 
243 extern template class autocorr_result<double>;
244 extern template class autocorr_result<std::complex<double> >;
245 
246 }}
const level_acc_type & level(size_t i) const
Definition: autocorr.hpp:111
size_t nlevel() const
Definition: autocorr.hpp:109
typename bind< circular_var, T >::var_type var_type
Definition: autocorr.hpp:70
const level_result_type & level(size_t i) const
Definition: autocorr.hpp:202
std::enable_if<!is_sequence< T >::value, std::size_t >::type size(T const &)
Definition: size.hpp:20
bool valid() const
Definition: autocorr.hpp:89
bind< circular_var, T >::var_type var_type
Definition: autocorr.hpp:132
void reduce(const reducer &r)
Definition: autocorr.hpp:185
level_result_type & level(size_t i)
Definition: autocorr.hpp:204
size_t nlevel() const
Definition: autocorr.hpp:200
void deserialize(deserializer &, const std::string &, autocorr_result< T > &)
Definition: autocorr.cpp:262
var_result< T, circular_var > level_result_type
Definition: autocorr.hpp:152
bind< circular_var, T >::cov_type cov_type
Definition: autocorr.hpp:234
size_t size() const
Definition: autocorr.hpp:92
void serialize(serializer &, const std::string &, const autocorr_result< T > &)
Definition: autocorr.cpp:243
bind< circular_var, T >::cov_type cov_type
Definition: autocorr.hpp:133
void reset(accumulator_wrapper &arg)
bind< circular_var, T >::var_type var_type
Definition: autocorr.hpp:151
bool operator==(const autocorr_result< T > &r1, const autocorr_result< T > &r2)
Definition: autocorr.cpp:136
bool tau_available() const
Definition: autocorr.hpp:182
traits< Acc >::result_type result(const Acc &acc)
Definition: util.hpp:53
const column< T > & mean() const
Definition: autocorr.hpp:170
T r(T x, T y=T(), T z=T())
traits< Acc >::result_type finalize(Acc &acc)
Definition: util.hpp:45
bind< circular_var, T >::var_type var_type
Definition: autocorr.hpp:233
autocorr_result(size_t nlevel=0)
Definition: autocorr.hpp:155
bool operator!=(const autocorr_result< T > &r1, const autocorr_result< T > &r2)
Definition: autocorr.hpp:220
count_type< T >::type count(T const &arg)
Definition: count.hpp:39
size_t count() const
Definition: autocorr.hpp:101