ALPSCore reference
batch.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/computed.hpp>
10 #include <alps/alea/util.hpp>
13 
14 #include <memory>
15 
16 // Forward declarations
17 
18 namespace alps { namespace alea {
19  template <typename T> class batch_acc;
20  template <typename T> class batch_data;
21  template <typename T> class batch_result;
22 
23  template <typename T>
24  void serialize(serializer &, const std::string &, const batch_result<T> &);
25 
26  template <typename T>
27  void deserialize(deserializer &, const std::string &, batch_result<T> &);
28 
29  template <typename T>
30  std::ostream &operator<<(std::ostream &, const batch_result<T> &);
31 }}
32 
33 // Actual declarations
34 
35 namespace alps { namespace alea {
36 
40 template <typename T>
41 class batch_data
42 {
43 public:
44  batch_data(size_t size, size_t num_batches=256);
45 
47  void reset();
48 
49  size_t num_batches() const { return batch_.cols(); }
50 
52  size_t size() const { return batch_.rows(); }
53 
54  typename eigen<T>::matrix &batch() { return batch_; }
55 
56  const typename eigen<T>::matrix &batch() const { return batch_; }
57 
59  typename eigen<size_t>::row &count() { return count_; }
60 
62  const typename eigen<size_t>::row &count() const { return count_; }
63 
64 private:
65  typename eigen<T>::matrix batch_;
66  typename eigen<size_t>::row count_;
67 };
68 
69 template <typename T>
70 struct traits< batch_data<T> >
71 {
72  typedef T value_type;
73 };
74 
75 extern template class batch_data<double>;
76 extern template class batch_data<std::complex<double> >;
77 
81 template <typename T>
82 class batch_acc
83 {
84 public:
85  using value_type = T;
86 
87 public:
88  batch_acc(size_t size=1, size_t num_batches=256, size_t base_size=1);
89 
90  batch_acc(const batch_acc &other);
91 
92  batch_acc &operator=(const batch_acc &other);
93 
95  void reset();
96 
98  void set_size(size_t size);
99 
101  void set_num_batches(size_t batch_size);
102 
104  void set_batch_size(size_t batch_size);
105 
107  bool valid() const { return (bool)store_; }
108 
110  size_t size() const { return size_; }
111 
113  size_t num_batches() const { return num_batches_; }
114 
116  batch_acc& operator<<(const computed<T>& src){ add(src, 1); return *this; }
117 
119  batch_acc &operator<<(const batch_result<T> &result);
120 
122  size_t count() const { return store_->count().sum(); }
123 
125  batch_result<T> result() const;
126 
129 
131  const batch_data<T> &store() const { return *store_; }
132 
133  const internal::galois_hopper &cursor() const { return cursor_; }
134 
135  const typename eigen<size_t>::row &offset() const { return offset_; }
136 
137  size_t current_batch_size() const { return base_size_ * cursor_.factor(); }
138 
139 protected:
140  void add(const computed<T> &source, size_t count);
141 
142  void next_batch();
143 
144  void finalize_to(batch_result<T> &result);
145 
146 private:
147  size_t size_, num_batches_, base_size_;
148  std::unique_ptr< batch_data<value_type> > store_;
149  internal::galois_hopper cursor_;
150  typename eigen<size_t>::row offset_;
151 };
152 
153 template <typename T>
154 struct traits< batch_acc<T> >
155 {
156  typedef T value_type;
160 };
161 
162 extern template class batch_acc<double>;
163 extern template class batch_acc<std::complex<double> >;
164 
165 
169 template <typename T>
170 class batch_result
171 {
172 public:
173  typedef T value_type;
174 
175 public:
177 
178  batch_result(const batch_data<T> &acc_data)
179  : store_(new batch_data<T>(acc_data))
180  { }
181 
182  batch_result(const batch_result &other);
183 
184  batch_result &operator=(const batch_result &other);
185 
187  bool valid() const { return (bool)store_; }
188 
190  size_t size() const { return store_->size(); }
191 
193  size_t num_batches() const { return store_->num_batches(); }
194 
196  size_t count() const { return store_->count().sum(); }
197 
199  double count2() const { return store_->count().squaredNorm(); }
200 
202  column<T> mean() const;
203 
205  template <typename Strategy=circular_var>
207 
209  template <typename Strategy=circular_var>
210  typename eigen<typename bind<Strategy,T>::cov_type>::matrix cov() const;
211 
214 
216  const batch_data<T> &store() const { return *store_; }
217 
219  batch_data<T> &store() { return *store_; }
220 
222  void reduce(const reducer &r) { reduce(r, true, true); }
223 
225  friend void serialize<>(serializer &, const std::string &, const batch_result &);
226 
228  friend void deserialize<>(deserializer &, const std::string &, batch_result &);
229 
231  friend std::ostream &operator<< <>(std::ostream &, const batch_result &);
232 
233 protected:
234  void reduce(const reducer &r, bool do_pre_commit, bool do_post_commit);
235 
236 private:
237  std::unique_ptr< batch_data<value_type> > store_;
238 
239  friend class batch_acc<T>;
240 };
241 
243 template <typename T>
244 bool operator==(const batch_result<T> &r1, const batch_result<T> &r2);
245 template <typename T>
246 bool operator!=(const batch_result<T> &r1, const batch_result<T> &r2)
247 {
248  return !operator==(r1, r2);
249 }
250 
251 template<typename T> struct is_alea_acc<batch_acc<T>> : std::true_type {};
252 template<typename T> struct is_alea_result<batch_result<T>> : std::true_type {};
253 
254 template <typename T>
255 struct traits< batch_result<T> >
256 {
257  typedef T value_type;
258 
259  // TODO: batch result supports multiple strategies
260  typedef typename make_real<T>::type var_type;
261  typedef T cov_type;
263 
264  const static bool HAVE_MEAN = true;
265  const static bool HAVE_VAR = true;
266  const static bool HAVE_COV = true;
267  const static bool HAVE_TAU = false;
268  const static bool HAVE_BATCH = true;
269 };
270 
271 extern template class batch_result<double>;
272 extern template class batch_result<std::complex<double> >;
273 
274 }} /* namespace alps::alea */
void reduce(const reducer &r)
Definition: batch.hpp:222
eigen< T >::matrix & batch()
Definition: batch.hpp:54
size_t count() const
Definition: batch.hpp:122
std::enable_if<!is_sequence< T >::value, std::size_t >::type size(T const &)
Definition: size.hpp:20
size_t size() const
Definition: batch.hpp:190
void deserialize(deserializer &, const std::string &, autocorr_result< T > &)
Definition: autocorr.cpp:262
eigen< size_t >::row & count()
Definition: batch.hpp:59
batch_data< T > & store()
Definition: batch.hpp:219
mean_type< T >::type mean(T const &arg)
Definition: mean.hpp:47
const eigen< size_t >::row & offset() const
Definition: batch.hpp:135
void serialize(serializer &, const std::string &, const autocorr_result< T > &)
Definition: autocorr.cpp:243
void reset(accumulator_wrapper &arg)
const eigen< size_t >::row & count() const
Definition: batch.hpp:62
bool valid() const
Definition: batch.hpp:107
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > matrix
Definition: util.hpp:49
size_t num_batches() const
Definition: batch.hpp:113
bool operator==(const autocorr_result< T > &r1, const autocorr_result< T > &r2)
Definition: autocorr.cpp:136
bool valid() const
Definition: batch.hpp:187
size_t size() const
Definition: batch.hpp:52
const internal::galois_hopper & cursor() const
Definition: batch.hpp:133
traits< Acc >::result_type result(const Acc &acc)
Definition: util.hpp:53
const eigen< T >::matrix & batch() const
Definition: batch.hpp:56
size_t num_batches() const
Definition: batch.hpp:193
size_t num_batches() const
Definition: batch.hpp:49
batch_result(const batch_data< T > &acc_data)
Definition: batch.hpp:178
T r(T x, T y=T(), T z=T())
size_t size() const
Definition: batch.hpp:110
size_t current_batch_size() const
Definition: batch.hpp:137
traits< Acc >::result_type finalize(Acc &acc)
Definition: util.hpp:45
const batch_data< T > & store() const
Definition: batch.hpp:131
const batch_data< T > & store() const
Definition: batch.hpp:216
bool operator!=(const autocorr_result< T > &r1, const autocorr_result< T > &r2)
Definition: autocorr.hpp:220
double count2() const
Definition: batch.hpp:199
count_type< T >::type count(T const &arg)
Definition: count.hpp:39
size_t count() const
Definition: batch.hpp:196