ALPSCore reference
core.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 <cstddef>
9 #include <cassert>
10 #include <stdexcept>
11 #include <complex>
12 #include <string>
13 #include <vector>
14 #include <initializer_list>
15 
16 #include <Eigen/Dense>
17 
18 #include <alps/alea/complex_op.hpp>
19 
20 namespace alps { namespace alea {
21 
22 using std::size_t;
23 using std::ptrdiff_t;
24 
26 struct size_mismatch : public std::exception { };
27 
29 struct unsupported_operation : public std::exception { };
30 
32 struct finalized_accumulator : public std::exception { };
33 
35 struct weight_mismatch : public std::exception { };
36 
37 template <typename T>
38 struct traits;
39 
40 // Metafunction to detect ALEA accumulator types
41 template<typename T> struct is_alea_acc : std::false_type {};
42 
43 // Metafunction to detect ALEA result types
44 template<typename T> struct is_alea_result : std::false_type {};
45 
52 template <typename T>
53 class view
54 {
55 public:
56  typedef T value_type;
57 
58 public:
60  view() : data_(nullptr), size_(0) { }
61 
63  view(T *data, size_t size)
64  : data_(data), size_(size)
65  { }
66 
68  T *data() { return data_; }
69 
71  const T *data() const { return data_; }
72 
74  size_t size() const { return size_; }
75 
76 private:
77  T *data_;
78  size_t size_;
79 };
80 
89 template <typename T>
90 class ndview
91  : public view<T>
92 {
93 public:
94  typedef T value_type;
95 
96 public:
98  ndview() : view<T>(), shape_(nullptr), ndim_(0) { }
99 
101  ndview(T *data, const size_t *shape, size_t ndim)
102  : view<T>(data, compute_size(shape, ndim))
103  , shape_(shape)
104  , ndim_(ndim)
105  { }
106 
108  ndview(T *data, size_t size, const size_t *shape, size_t ndim)
109  : view<T>(data, size)
110  , shape_(shape)
111  , ndim_(ndim)
112  {
113  assert(size == compute_size(shape, ndim));
114  }
115 
117  const size_t *shape() const { return shape_; }
118 
120  size_t ndim() const { return ndim_; }
121 
122 protected:
123  static size_t compute_size(const size_t *shape, size_t ndim)
124  {
125  size_t result = 1;
126  for (size_t d = 0; d != ndim; ++d)
127  result *= shape[d];
128  return result;
129  }
130 
131 private:
132  const size_t *shape_;
133  size_t ndim_;
134 };
135 
156 template <typename T>
157 struct computed
158 {
159  typedef T value_type;
160 
162  virtual size_t size() const = 0;
163 
165  virtual std::vector<size_t> shape() const { return std::vector<size_t>(1, size()); }
166 
174  virtual void add_to(view<T> out) const = 0;
175 
177  virtual computed *clone() { throw unsupported_operation(); }
178 
180  virtual ~computed() { }
181 };
182 
186 template <typename T>
187 class column
188  : public Eigen::Matrix<T, Eigen::Dynamic, 1>
189 {
190 public:
191  column() : Eigen::Matrix<T, Eigen::Dynamic, 1>() {}
192 
193  column(size_t size) : Eigen::Matrix<T, Eigen::Dynamic, 1>(size) {}
194 
195  column(std::initializer_list<T> l) : column(l.size()) {
196  size_t i = 0;
197  for (auto x : l) {
198  this->Eigen::Matrix<T,Eigen::Dynamic,1>::operator()(i) = x;
199  ++i;
200  }
201  }
202 
203  template <typename OtherDerived>
204  column(const Eigen::MatrixBase<OtherDerived>& other)
205  : Eigen::Matrix<T, Eigen::Dynamic, 1>(other) { }
206 
207  template<typename OtherDerived>
208  column& operator=(const Eigen::MatrixBase <OtherDerived>& other)
209  {
210  this->Eigen::Matrix<T, Eigen::Dynamic, 1>::operator=(other);
211  return *this;
212  }
213 
214  // Methods for convenience and backwards compatibility
215 
216  // TODO this prevents us from doing, which we should be at some point ...
217  // template <typename T>
218  // using column = Eigen::Matrix<T, Eigen::Dynamic, 1>;
219 
220  size_t size() const { return this->rows(); }
221 
222  operator std::vector<T>() const
223  {
224  return std::vector<T>(this->data(), this->data() + this->rows());
225  }
226 };
227 
232 {
234  size_t pos;
235 
237  size_t count;
238 
241 };
242 
265 struct reducer
266 {
268  virtual reducer_setup get_setup() const = 0;
269 
271  virtual long get_max(long value) const = 0;
272 
274  virtual void reduce(view<double> data) const = 0;
275 
277  virtual void reduce(view<long> data) const = 0;
278 
280  virtual void commit() const = 0;
281 
283  virtual reducer *clone() { throw unsupported_operation(); }
284 
286  virtual ~reducer() { }
287 
288  // Convenience functions
289 
290  void reduce(view<std::complex<double> > data) const {
291  reduce(view<double>((double *)data.data(), 2 * data.size()));
292  }
293  void reduce(view<complex_op<double> > data) const {
294  reduce(view<double>((double *)data.data(), 4 * data.size()));
295  }
296  void reduce(view<unsigned long> data) const {
297  reduce(view<long>((long *)data.data(), data.size()));
298  }
299 };
300 
311 {
313  virtual void enter(const std::string &group) = 0;
314 
316  virtual void exit() = 0;
317 
319  virtual void write(const std::string &key, ndview<const double>) = 0;
320 
322  virtual void write(const std::string &key, ndview<const std::complex<double>>) = 0;
323 
325  virtual void write(const std::string &key, ndview<const complex_op<double>>) = 0;
326 
328  virtual void write(const std::string &key, ndview<const long>) = 0;
329 
331  virtual void write(const std::string &key, ndview<const unsigned long>) = 0;
332 
334  virtual serializer *clone() { throw unsupported_operation(); }
335 
337  virtual ~serializer() { }
338 };
339 
353 {
355  virtual void enter(const std::string &group) = 0;
356 
358  virtual void exit() = 0;
359 
361  virtual std::vector<size_t> get_shape(const std::string &key) = 0;
362 
364  virtual void read(const std::string &key, ndview<double>) = 0;
365 
367  virtual void read(const std::string &key, ndview<std::complex<double>>) = 0;
368 
370  virtual void read(const std::string &key, ndview<complex_op<double>>) = 0;
371 
373  virtual void read(const std::string &key, ndview<long>) = 0;
374 
376  virtual void read(const std::string &key, ndview<unsigned long>) = 0;
377 
379  virtual deserializer *clone() { throw unsupported_operation(); }
380 
382  virtual ~deserializer() { }
383 };
384 
394 template <typename T>
396 {
398  virtual column<T> operator() (const column<T> &in) const = 0;
399 
401  virtual size_t in_size() const = 0;
402 
404  virtual size_t out_size() const = 0;
405 
407  virtual bool is_linear() const { return false; }
408 
410  virtual ~transformer() { }
411 };
412 
413 }}
T * data()
Definition: core.hpp:68
const size_t * shape() const
Definition: core.hpp:117
size_t size() const
Definition: core.hpp:220
std::enable_if<!is_sequence< T >::value, std::size_t >::type size(T const &)
Definition: size.hpp:20
void reduce(view< unsigned long > data) const
Definition: core.hpp:296
column(size_t size)
Definition: core.hpp:193
virtual bool is_linear() const
Definition: core.hpp:407
virtual ~deserializer()
Definition: core.hpp:382
column(const Eigen::MatrixBase< OtherDerived > &other)
Definition: core.hpp:204
virtual ~transformer()
Definition: core.hpp:410
const T * data() const
Definition: core.hpp:71
column & operator=(const Eigen::MatrixBase< OtherDerived > &other)
Definition: core.hpp:208
static size_t compute_size(const size_t *shape, size_t ndim)
Definition: core.hpp:123
void reduce(view< complex_op< double > > data) const
Definition: core.hpp:293
ndview(T *data, const size_t *shape, size_t ndim)
Definition: core.hpp:101
virtual computed * clone()
Definition: core.hpp:177
size_t size() const
Definition: core.hpp:74
virtual std::vector< size_t > shape() const
Definition: core.hpp:165
virtual ~serializer()
Definition: core.hpp:337
column(std::initializer_list< T > l)
Definition: core.hpp:195
size_t ndim() const
Definition: core.hpp:120
traits< Acc >::result_type result(const Acc &acc)
Definition: util.hpp:53
virtual serializer * clone()
Definition: core.hpp:334
virtual ~computed()
Definition: core.hpp:180
virtual deserializer * clone()
Definition: core.hpp:379
view(T *data, size_t size)
Definition: core.hpp:63
virtual reducer * clone()
Definition: core.hpp:283
virtual ~reducer()
Definition: core.hpp:286
ndview(T *data, size_t size, const size_t *shape, size_t ndim)
Definition: core.hpp:108
void reduce(view< std::complex< double > > data) const
Definition: core.hpp:290