ALPSCore reference
count.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 
14 #define ALPS_ACCUMULATOR_VALUE_TYPES_SEQ BOOST_PP_TUPLE_TO_SEQ(ALPS_ACCUMULATOR_VALUE_TYPES_SIZE, (ALPS_ACCUMULATOR_VALUE_TYPES))
15 
16 namespace alps {
17  namespace accumulators {
18  namespace impl {
19 
20  //
21  // Result<T, count_tag, B>
22  //
23 
24  template<typename T, typename B>
26  throw std::runtime_error("No values can be added to a result" + ALPS_STACKTRACE);
27  }
28 
29  template<typename T, typename B>
31  if (m_count==0) {
32  throw std::logic_error("Attempt to save an empty result" + ALPS_STACKTRACE);
33  }
34  ar["count"] = m_count;
35  }
36 
37  template<typename T, typename B>
39  count_type cnt;
40  ar["count"] >> cnt;
41  if (cnt==0) {
42  throw std::runtime_error("Malformed archive containing an empty result"
43  + ALPS_STACKTRACE);
44  }
45  m_count=cnt;
46  }
47 
48  template<typename T, typename B>
49  bool Result<T, count_tag, B>::can_load(hdf5::archive & ar) { // TODO: make archive const
50  return ar.is_data("count");
51  }
52 
53  template<typename T, typename B>
55  throw std::runtime_error("A result cannot be reset" + ALPS_STACKTRACE);
56  }
57 
58  #define ALPS_ACCUMULATOR_INST_COUNT_RESULT(r, data, T) \
59  template class Result<T, count_tag, ResultBase<T>>;
61 
62  //
63  // Accumulator<T, count_tag, B>
64  //
65 
66  template<typename T, typename B>
67  void Accumulator<T, count_tag, B>::save(hdf5::archive & ar) const {
68  if (m_count==0) {
69  throw std::logic_error("Attempt to save an empty accumulator" + ALPS_STACKTRACE);
70  }
71  ar["count"] = m_count;
72  }
73 
74  template<typename T, typename B>
75  void Accumulator<T, count_tag, B>::load(hdf5::archive & ar) { // TODO: make archive const
76  count_type cnt;
77  ar["count"] >> cnt;
78  if (cnt==0) {
79  throw std::runtime_error("Malformed archive containing an empty accumulator"
80  + ALPS_STACKTRACE);
81  }
82  m_count=cnt;
83  }
84 
85  template<typename T, typename B>
87  return ar.is_data("count");
88  }
89 
90 #ifdef ALPS_HAVE_MPI
91  template<typename T, typename B>
93  alps::mpi::communicator const & comm
94  , int root
95  ) {
96  if (comm.rank() == root)
97  alps::alps_mpi::reduce(comm, m_count, m_count, std::plus<count_type>(), root);
98  else
99  const_cast<Accumulator<T, count_tag, B> const *>(this)->collective_merge(comm, root);
100  }
101 
102  template<typename T, typename B>
104  alps::mpi::communicator const & comm
105  , int root
106  ) const {
107  if (comm.rank() == root)
108  throw std::runtime_error("A const object cannot be root" + ALPS_STACKTRACE);
109  else
110  alps::alps_mpi::reduce(comm, m_count, std::plus<count_type>(), root);
111  }
112 #endif
113 
114  #define ALPS_ACCUMULATOR_INST_COUNT_ACC(r, data, T) \
115  template class Accumulator<T, count_tag, AccumulatorBase<T>>;
117  }
118  }
119 }
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
bool is_data(std::string path) const
Definition: archive.cpp:170
#define ALPS_ACCUMULATOR_VALUE_TYPES_SEQ
Definition: count.cpp:14
Encapsulation of an MPI communicator and some communicator-related operations.
Definition: mpi.hpp:111
void reset(accumulator_wrapper &arg)
#define ALPS_ACCUMULATOR_INST_COUNT_ACC(r, data, T)
Definition: count.cpp:114
#define ALPS_STACKTRACE
Definition: stacktrace.hpp:37
int rank() const
Returns process rank in this communicator.
Definition: mpi.hpp:156
#define ALPS_ACCUMULATOR_INST_COUNT_RESULT(r, data, T)
Definition: count.cpp:58
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