ALPSCore reference
wrapper_set_hdf5.cpp
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 
7 #include <boost/preprocessor/tuple/to_seq.hpp>
8 #include <boost/preprocessor/seq/for_each.hpp>
9 
10 #include <alps/config.hpp>
11 
13 #include <alps/accumulators.hpp>
14 
15 #include <alps/hdf5/vector.hpp>
16 
17 #define ALPS_ACCUMULATOR_VALUE_TYPES_SEQ BOOST_PP_TUPLE_TO_SEQ(ALPS_ACCUMULATOR_VALUE_TYPES_SIZE, (ALPS_ACCUMULATOR_VALUE_TYPES))
18 
19 namespace alps {
20  namespace accumulators {
21  namespace detail {
22 
23  template<typename T> struct serializable_type {
24  virtual ~serializable_type() {}
25  virtual std::size_t rank() const = 0;
26  virtual bool can_load(hdf5::archive & ar) const = 0;
27  virtual T * create(hdf5::archive & ar) const = 0;
28  };
29 
30  template<typename T, typename A> struct serializable_type_impl : public serializable_type<T> {
31  std::size_t rank() const {
32  return A::rank();
33  }
34  bool can_load(hdf5::archive & ar) const {
35  return A::can_load(ar);
36  }
37  T * create(hdf5::archive & /*ar*/) const {
38  return new T(A());
39  }
40  };
41 
42  void register_predefined_serializable_types();
43  }
44 
45  namespace impl {
47  template<typename T> template<typename A> void wrapper_set<T>::register_serializable_type_nolock() {
48  m_types.push_back(std::shared_ptr<detail::serializable_type<T> >(new detail::serializable_type_impl<T, A>));
49  for (std::size_t i = m_types.size(); i > 1 && m_types[i - 1]->rank() > m_types[i - 2]->rank(); --i)
50  m_types[i - 1].swap(m_types[i - 2]);
51  }
52 
54  template<typename T> template<typename A> void wrapper_set<T>::register_serializable_type() {
55  std::lock_guard<std::mutex> guard(m_types_mutex);
56  if (m_types.empty()) detail::register_predefined_serializable_types();
57  register_serializable_type_nolock<A>();
58  }
59  }
60 
61  namespace detail {
62  void register_predefined_serializable_types() {
63  #define ALPS_ACCUMULATOR_REGISTER_ACCUMULATOR(A) \
64  accumulator_set::register_serializable_type_nolock<A::accumulator_type>(); \
65  result_set::register_serializable_type_nolock<A::result_type>();
66 
67  #define ALPS_ACCUMULATOR_REGISTER_TYPE(r, data, T) \
68  ALPS_ACCUMULATOR_REGISTER_ACCUMULATOR(MeanAccumulator<T>) \
69  ALPS_ACCUMULATOR_REGISTER_ACCUMULATOR(NoBinningAccumulator<T>) \
70  ALPS_ACCUMULATOR_REGISTER_ACCUMULATOR(LogBinningAccumulator<T>) \
71  ALPS_ACCUMULATOR_REGISTER_ACCUMULATOR(FullBinningAccumulator<T>)
72 
74 
75  #undef ALPS_ACCUMULATOR_REGISTER_TYPE
76  #undef ALPS_ACCUMULATOR_REGISTER_ACCUMULATOR
77  }
78  }
79 
80  namespace impl {
81 
82  template<typename T>
84  ar.create_group("");
85  for(const_iterator it = begin(); it != end(); ++it) {
86  if (it->second->count()!=0) {
87  ar[it->first] = *(it->second);
88  }
89  }
90  }
91 
92  template<typename T>
94  std::lock_guard<std::mutex> guard(m_types_mutex);
95  std::vector<std::string> list = ar.list_children("");
96  for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it) {
97  ar.set_context(*it);
98  for (typename std::vector<std::shared_ptr<detail::serializable_type<T> > >::const_iterator jt = m_types.begin()
99  ; jt != m_types.end()
100  ; ++jt
101  )
102  if ((*jt)->can_load(ar)) {
103  operator[](*it) = std::shared_ptr<T>((*jt)->create(ar));
104  break;
105  }
106  if (!has(*it))
107  throw std::logic_error("The Accumulator/Result " + *it + " cannot be unserilized" + ALPS_STACKTRACE);
108  operator[](*it).load(ar);
109  ar.set_context("..");
110  }
111  }
112 
114  template void wrapper_set<result_wrapper>::save(hdf5::archive &) const;
117  }
118  }
119 }
#define ALPS_ACCUMULATOR_VALUE_TYPES_SEQ
void load(archive &ar, std::string const &path, T &value, std::vector< std::size_t > chunk=std::vector< std::size_t >(), std::vector< std::size_t >=std::vector< std::size_t >())
Definition: archive.hpp:309
void swap(params &p1, params &p2)
void set_context(std::string const &context)
Definition: archive.cpp:148
void create_group(std::string path) const
Definition: archive.cpp:327
#define ALPS_ACCUMULATOR_REGISTER_TYPE(r, data, T)
std::vector< std::string > list_children(std::string path) const
Definition: archive.cpp:259
#define ALPS_STACKTRACE
Definition: stacktrace.hpp:37
std::map< std::string, std::shared_ptr< T > >::const_iterator const_iterator
Definition: wrapper_set.hpp:33
void save(archive &ar, std::string const &path, T const &value, std::vector< std::size_t >=std::vector< std::size_t >(), std::vector< std::size_t > chunk=std::vector< std::size_t >(), std::vector< std::size_t >=std::vector< std::size_t >())
Definition: archive.hpp:292