MADNESS  0.10.1
cereal_archive.h
Go to the documentation of this file.
1 /*
2  This file is part of MADNESS.
3 
4  Copyright (C) 2007,2010 Oak Ridge National Laboratory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20  For more information please contact:
21 
22  Robert J. Harrison
23  Oak Ridge National Laboratory
24  One Bethel Valley Road
25  P.O. Box 2008, MS-6367
26 
27  email: harrisonrj@ornl.gov
28  tel: 865-241-3937
29  fax: 865-572-0680
30 */
31 
32 #ifndef MADNESS_WORLD_CEREAL_ARCHIVE_H__INCLUDED
33 #define MADNESS_WORLD_CEREAL_ARCHIVE_H__INCLUDED
34 
35 namespace madness {
36 template <typename Archive> struct is_cereal_archive;
37 }
38 
39 #ifdef MADNESS_HAS_CEREAL
40 # if ! __has_include(<cereal/cereal.hpp>)
41 # error "MADNESS_ENABLE_CEREAL is on, but cereal/cereal.hpp was not found."
42 # endif
43 
44 #include <memory>
45 #include <cereal/cereal.hpp>
46 #include <cereal/details/traits.hpp>
47 #include <madness/world/archive.h>
48 
49 namespace madness {
50 namespace archive {
51 /// Wraps an output archive around a Cereal archive
52 template <typename Muesli>
53 class CerealOutputArchive : public ::madness::archive::BaseOutputArchive {
54  mutable std::shared_ptr<Muesli>
55  muesli; ///< The cereal archive being wrapped, deleter determines whether this is an owning ptr
56 
57 public:
58  CerealOutputArchive(Muesli &muesli) : muesli(&muesli, [](Muesli *) {}) {}
59  template <typename Arg, typename... RestOfArgs,
60  typename = std::enable_if_t<
61  !std::is_same<Muesli, std::decay_t<Arg>>::value>>
62  CerealOutputArchive(Arg &&arg, RestOfArgs &&... rest_of_args)
63  : muesli(new Muesli(std::forward<Arg>(arg),
64  std::forward<RestOfArgs>(rest_of_args)...),
65  std::default_delete<Muesli>{}) {}
66 
67  template <class T, class Cereal = Muesli>
68  inline std::enable_if_t<
70  !cereal::traits::is_text_archive<Cereal>::value,
71  void>
72  store(const T *t, long n) const {
73  const unsigned char *ptr = (unsigned char *)t;
74  (*muesli)(cereal::binary_data(ptr, sizeof(T) * n));
75  }
76 
77  template <class T, class Cereal = Muesli>
78  inline std::enable_if_t<
80  cereal::traits::is_text_archive<Cereal>::value,void>
81  store(const T *t, long n) const {
82  for (long i = 0; i != n; ++i)
83  (*muesli)(t[i]);
84  }
85 
86  void open(std::size_t hint) {}
87  void close(){};
88  void flush(){};
89 };
90 
91 /// Wraps an input archive around a Cereal archive
92 template <typename Muesli> class CerealInputArchive : public BaseInputArchive {
93  std::shared_ptr<Muesli> muesli; ///< The cereal archive being wrapped, deleter determines whether this is an owning ptr
94 
95 public:
96  CerealInputArchive(Muesli &muesli) : muesli(&muesli, [](Muesli *) {}) {}
97  template <typename Arg, typename... RestOfArgs,
98  typename = std::enable_if_t<
99  !std::is_same<Muesli, std::decay_t<Arg>>::value>>
100  CerealInputArchive(Arg &&arg, RestOfArgs &&... rest_of_args)
101  : muesli(new Muesli(std::forward<Arg>(arg),
102  std::forward<RestOfArgs>(rest_of_args)...),
103  std::default_delete<Muesli>{}) {}
104 
105  template <class T, class Cereal = Muesli>
106  inline std::enable_if_t<
108  !cereal::traits::is_text_archive<Cereal>::value,
109  void>
110  load(T *t, long n) const {
111  (*muesli)(cereal::binary_data(t, sizeof(T) * n));
112  }
113 
114  template <class T, class Cereal = Muesli>
115  inline std::enable_if_t<
117  cereal::traits::is_text_archive<Cereal>::value,
118  void>
119  load(T *t, long n) const {
120  for (long i = 0; i != n; ++i)
121  (*muesli)(t[i]);
122  }
123 
124  void open(std::size_t hint) {}
125  void rewind() const {}
126  void close(){};
127 };
128 } // namespace archive
129 
130 template <typename Muesli>
131 struct is_text_archive<
132  archive::CerealInputArchive<Muesli>,
133  std::enable_if_t<cereal::traits::is_text_archive<Muesli>::value>>
134  : std::true_type {};
135 template <typename Muesli>
136 struct is_text_archive<
137  archive::CerealOutputArchive<Muesli>,
138  std::enable_if_t<cereal::traits::is_text_archive<Muesli>::value>>
139  : std::true_type {};
140 
141 template <typename Muesli, typename T>
142 struct is_default_serializable_helper<
143  archive::CerealOutputArchive<Muesli>, T,
144  std::enable_if_t<(is_trivially_serializable<T>::value &&
145  !cereal::traits::is_text_archive<Muesli>::value) ||
146  (cereal::traits::detail::count_output_serializers<T, Muesli>::value != 0 &&
147  cereal::traits::is_text_archive<Muesli>::value)>>
148  : std::true_type {};
149 template <typename Muesli, typename T>
150 struct is_default_serializable_helper<
151  archive::CerealInputArchive<Muesli>, T,
152  std::enable_if_t<
153  (is_trivially_serializable<T>::value &&
154  !cereal::traits::is_text_archive<Muesli>::value) ||
155  (cereal::traits::detail::count_input_serializers<T, Muesli>::value != 0 &&
156  cereal::traits::is_text_archive<Muesli>::value)>>
157  : std::true_type {};
158 
159 template <typename Muesli>
160 struct is_cereal_archive<archive::CerealOutputArchive<Muesli>> : std::true_type {};
161 template <typename Muesli>
162 struct is_cereal_archive<archive::CerealInputArchive<Muesli>> : std::true_type {};
163 
164 // must also be able to introspect bare cereal archives to be able to reuse serialize methods for both
165 template <typename T>
166 struct is_archive<archive::CerealOutputArchive<T>, std::enable_if_t<std::is_base_of_v<cereal::detail::OutputArchiveBase, T>>> : std::true_type {};
167 template <typename T>
168 struct is_archive<archive::CerealInputArchive<T>, std::enable_if_t<std::is_base_of_v<cereal::detail::InputArchiveBase, T>>> : std::true_type {};
169 template <typename T>
170 struct is_output_archive<archive::CerealOutputArchive<T>, std::enable_if_t<std::is_base_of_v<cereal::detail::OutputArchiveBase, T>>> : std::true_type {};
171 template <typename T>
172 struct is_input_archive<archive::CerealInputArchive<T>, std::enable_if_t<std::is_base_of_v<cereal::detail::InputArchiveBase, T>>> : std::true_type {};
173 template <typename T>
174 struct is_archive<T, std::enable_if_t<std::is_base_of_v<cereal::detail::OutputArchiveBase, T>>> : std::true_type {};
175 template <typename T>
176 struct is_archive<T, std::enable_if_t<std::is_base_of_v<cereal::detail::InputArchiveBase, T>>> : std::true_type {};
177 template <typename T>
178 struct is_output_archive<T, std::enable_if_t<std::is_base_of_v<cereal::detail::OutputArchiveBase, T>>> : std::true_type {};
179 template <typename T>
180 struct is_input_archive<T, std::enable_if_t<std::is_base_of_v<cereal::detail::InputArchiveBase, T>>> : std::true_type {};
181 
182 } // namespace madness
183 
184 #endif // MADNESS_HAS_CEREAL
185 
186 namespace madness {
187 template <typename Archive> struct is_cereal_archive : std::false_type {};
188 }
189 
190 #endif // MADNESS_WORLD_CEREAL_ARCHIVE_H__INCLUDED
Interface templates for the archives (serialization).
Base class for output archive classes.
Definition: archive.h:382
auto T(World &world, response_space &f) -> response_space
Definition: global_functions.cc:34
Tensor< typename Tensor< T >::scalar_type > arg(const Tensor< T > &t)
Return a new tensor holding the argument of each element of t (complex types only)
Definition: tensor.h:2502
File holds all helper structures necessary for the CC_Operator and CC2 class.
Definition: DFParameters.h:10
void load(Function< T, NDIM > &f, const std::string name)
Definition: mra.h:2751
Definition: mraimpl.h:50
Definition: cereal_archive.h:187
Definition: type_traits.h:220