ALPSCore reference
serialize.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 
10 #include <array>
11 
12 namespace alps { namespace alea { namespace internal {
13 
18 {
19  serializer_sentry(serializer &ser, const std::string &group)
20  : ser_(ser)
21  , group_(group)
22  {
23  if (group != "")
24  ser_.enter(group);
25  }
26 
28  {
29  if (group_ != "")
30  ser_.exit();
31  }
32 
33 private:
34  serializer &ser_;
35  std::string group_;
36 };
37 
42 {
43  deserializer_sentry(deserializer &ser, const std::string &group)
44  : ser_(ser)
45  , group_(group)
46  {
47  if (group != "")
48  ser_.enter(group);
49  }
50 
52  {
53  if (group_ != "")
54  ser_.exit();
55  }
56 
57 private:
58  deserializer &ser_;
59  std::string group_;
60 };
61 
63 template <typename T>
64 void scalar_serialize(serializer &ser, const std::string &key, T value)
65 {
66  ser.write(key, ndview<const T>(&value, nullptr, 0));
67 }
68 
70 template <typename T>
71 T scalar_deserialize(deserializer &ser, const std::string &key)
72 {
73  T value;
74  ser.read(key, ndview<T>(&value, nullptr, 0));
75  return value;
76 }
77 
79 template <typename T>
80 void scalar_deserialize(deserializer &ser, const std::string &key, T &value)
81 {
82  ser.read(key, ndview<T>(&value, nullptr, 0));
83 }
84 
85 }}}
86 
87 namespace alps { namespace alea {
88 
89 // Serialization methods
90 
91 inline void serialize(serializer &ser, const std::string &key, unsigned long value) {
92  internal::scalar_serialize(ser, key, value);
93 }
94 inline void serialize(serializer &ser, const std::string &key, long value) {
95  internal::scalar_serialize(ser, key, value);
96 }
97 inline void serialize(serializer &ser, const std::string &key, double value) {
98  internal::scalar_serialize(ser, key, value);
99 }
100 inline void serialize(serializer &ser, const std::string &key,
101  std::complex<double> value) {
102  internal::scalar_serialize(ser, key, value);
103 }
104 
105 template <typename Derived>
106 void serialize(serializer &ser, const std::string &key,
107  const Eigen::MatrixBase<Derived> &value)
108 {
109  typedef Eigen::internal::traits<Derived> traits;
110  typedef typename traits::Scalar scalar_type;
111  typedef Eigen::Matrix<scalar_type, Derived::RowsAtCompileTime,
112  Derived::ColsAtCompileTime> plain_matrix_type;
113 
114  // Ensure that evaluated expression will be continuous
115  if ((Derived::MaxRowsAtCompileTime != Eigen::Dynamic
116  && Derived::MaxRowsAtCompileTime != value.rows())
117  || (Derived::MaxColsAtCompileTime != Eigen::Dynamic
118  && Derived::MaxColsAtCompileTime != value.cols())
119  || ((Derived::Options & Eigen::RowMajor)
120  && value.rows() != 1 && value.cols() != 1))
121  serialize(ser, key, plain_matrix_type(value));
122 
123  // Evaluate to matrix or proxy object if already matrix
124  auto temp = value.eval();
125 
126  if (Derived::ColsAtCompileTime == 1 || Derived::RowsAtCompileTime == 1) {
127  // Omit second dimension for simple vectors
128  std::array<size_t, 1> dims = {{(size_t)temp.size()}};
129  ser.write(key, ndview<const scalar_type>(temp.data(), dims.data(), 1));
130  } else {
131  // Eigen arrays are column-major
132  std::array<size_t, 2> dims = {{(size_t)temp.cols(), (size_t)temp.rows()}};
133  ser.write(key, ndview<const scalar_type>(temp.data(), dims.data(), 2));
134  }
135 }
136 
137 
138 // Argument-oriented deserialization
139 
140 inline void deserialize(deserializer &ser, const std::string &key, unsigned long &value) {
141  internal::scalar_deserialize(ser, key, value);
142 }
143 inline void deserialize(deserializer &ser, const std::string &key, long &value) {
144  internal::scalar_deserialize(ser, key, value);
145 }
146 inline void deserialize(deserializer &ser, const std::string &key, double &value) {
147  internal::scalar_deserialize(ser, key, value);
148 }
149 inline void deserialize(deserializer &ser, const std::string &key,
150  std::complex<double> &value) {
151  internal::scalar_deserialize(ser, key, value);
152 }
153 
154 template <typename T>
155 void deserialize(deserializer &ser, const std::string &key,
156  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &value)
157 {
158  std::array<size_t, 2> shape = {{(size_t)value.cols(), (size_t)value.rows()}};
159  ser.read(key, ndview<T>(value.data(), shape.data(), shape.size()));
160 }
161 
162 template <typename T>
163 void deserialize(deserializer &ser, const std::string &key,
164  Eigen::Matrix<T, Eigen::Dynamic, 1> &value)
165 {
166  std::array<size_t, 1> shape = {{(size_t)value.rows()}};
167  ser.read(key, ndview<T>(value.data(), shape.data(), shape.size()));
168 }
169 
170 template <typename T>
171 void deserialize(deserializer &ser, const std::string &key,
172  Eigen::Matrix<T, 1, Eigen::Dynamic> &value)
173 {
174  std::array<size_t, 1> shape = {{(size_t)value.cols()}};
175  ser.read(key, ndview<T>(value.data(), shape.data(), shape.size()));
176 }
177 
178 
179 }}
deserializer_sentry(deserializer &ser, const std::string &group)
Definition: serialize.hpp:43
virtual void exit()=0
void deserialize(deserializer &, const std::string &, autocorr_result< T > &)
Definition: autocorr.cpp:262
serializer_sentry(serializer &ser, const std::string &group)
Definition: serialize.hpp:19
void serialize(serializer &, const std::string &, const autocorr_result< T > &)
Definition: autocorr.cpp:243
virtual void read(const std::string &key, ndview< double >)=0
T scalar_deserialize(deserializer &ser, const std::string &key)
Definition: serialize.hpp:71
void scalar_serialize(serializer &ser, const std::string &key, T value)
Definition: serialize.hpp:64
virtual void write(const std::string &key, ndview< const double >)=0
virtual void enter(const std::string &group)=0