ALPSCore reference
stream_serializer.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 <numeric>
9 #include <type_traits>
10 
11 #include <alps/alea/core.hpp>
12 #include <alps/alea/complex_op.hpp>
13 #include <alps/alea/mean.hpp>
14 #include <alps/alea/variance.hpp>
15 #include <alps/alea/covariance.hpp>
16 #include <alps/alea/autocorr.hpp>
17 #include <alps/alea/batch.hpp>
18 
19 namespace alps { namespace alea {
20 
29 template <typename Archive> class stream_serializer : public serializer
30 {
31 
32 public:
33 
34  stream_serializer(Archive &ar) : ar_(ar) {}
35 
36  // Common methods
37 
38  // Nothing to be done here: streams have no notion of groups
39  void enter(const std::string &) override {}
40  void exit() override {}
41 
42  // Key names are irrelevant
43 
44  void write(const std::string &, ndview<const double> data_view) override
45  {
46  do_write(data_view);
47  }
48  void write(const std::string &, ndview<const std::complex<double>> data_view) override
49  {
50  do_write(data_view);
51  }
52  void write(const std::string &, ndview<const complex_op<double>> data_view) override
53  {
54  do_write(data_view);
55  }
56  void write(const std::string &, ndview<const long> data_view) override
57  {
58  do_write(data_view);
59  }
60  void write(const std::string &, ndview<const unsigned long> data_view) override
61  {
62  do_write(data_view);
63  }
64 
65 protected:
66 
67  template <typename T> void do_write(const ndview<const T> &data_view)
68  {
69  size_t size = data_view.size();
70  const T * data = data_view.data();
71  for(size_t n = 0; n != size; ++n)
72  ar_ << *(data + n);
73  }
74 
75 private:
76 
77  Archive &ar_;
78 };
79 
88 template <typename Archive> class stream_deserializer : public deserializer
89 {
90 
91 public:
92 
93  stream_deserializer(Archive &ar) : ar_(ar) {}
94 
95  // Common methods
96 
97  // Nothing to be done here: streams have no notion of groups
98  void enter(const std::string &) override {}
99  void exit() override {}
100 
101  // Deserialization methods
102 
103  // Key names are irrelevant
104 
105  std::vector<size_t> get_shape(const std::string &key) override
106  {
107  return {{}}; // There is no way to know the shape beforehand
108  }
109 
110  void read(const std::string &, ndview<double> value) override
111  {
112  do_read(value);
113  }
114  void read(const std::string &, ndview<std::complex<double>> value) override
115  {
116  do_read(value);
117  }
118  void read(const std::string &, ndview<complex_op<double>> value) override
119  {
120  do_read(value);
121  }
122  void read(const std::string &, ndview<long> value) override
123  {
124  do_read(value);
125  }
126  void read(const std::string &, ndview<unsigned long> value) override
127  {
128  do_read(value);
129  }
130 
131 protected:
132 
133  template <typename T> void do_read(ndview<T> &data_view)
134  {
135  size_t size = data_view.size();
136  T * data = data_view.data();
137  if(data) {
138  for(size_t n = 0; n != size; ++n)
139  ar_ >> *(data + n);
140  } else {
141  T tmp;
142  for(size_t n = 0; n != size; ++n)
143  ar_ >> tmp;
144  }
145  }
146 
147 private:
148 
149  Archive &ar_;
150 };
151 
156 template <typename Archive, typename T>
157 typename std::enable_if<is_alea_result<T>::value, void>::type
158 save(Archive &ar, const T &r, const unsigned int) {
160  serialize(ser, "", r);
161 }
162 
167 template <typename Archive, typename T>
168 typename std::enable_if<is_alea_result<T>::value, void>::type
169 load(Archive &ar, T &r, const unsigned int) {
171  deserialize(ser, "", r);
172 }
173 
178 template <typename Archive, typename T>
179 void serialize(Archive &ar, complex_op<T> &co, const unsigned int) {
180  ar & co.rere() & co.reim() & co.imre() & co.imim();
181 }
182 
183 }}
T * data()
Definition: core.hpp:68
void write(const std::string &, ndview< const complex_op< double >> data_view) override
std::enable_if<!is_sequence< T >::value, std::size_t >::type size(T const &)
Definition: size.hpp:20
void read(const std::string &, ndview< std::complex< double >> value) override
void do_write(const ndview< const T > &data_view)
void write(const std::string &, ndview< const std::complex< double >> data_view) override
void deserialize(deserializer &, const std::string &, autocorr_result< T > &)
Definition: autocorr.cpp:262
std::enable_if< is_alea_result< T >::value, void >::type load(Archive &ar, T &r, const unsigned int)
void write(const std::string &, ndview< const unsigned long > data_view) override
void read(const std::string &, ndview< long > value) override
void read(const std::string &, ndview< unsigned long > value) override
void serialize(serializer &, const std::string &, const autocorr_result< T > &)
Definition: autocorr.cpp:243
size_t size() const
Definition: core.hpp:74
void write(const std::string &, ndview< const long > data_view) override
void read(const std::string &, ndview< double > value) override
void write(const std::string &, ndview< const double > data_view) override
T r(T x, T y=T(), T z=T())
std::enable_if< is_alea_result< T >::value, void >::type save(Archive &ar, const T &r, const unsigned int)
std::vector< size_t > get_shape(const std::string &key) override
void enter(const std::string &) override
void enter(const std::string &) override
void read(const std::string &, ndview< complex_op< double >> value) override
void do_read(ndview< T > &data_view)