1 | /*
|
---|
2 | * Summator.hpp
|
---|
3 | *
|
---|
4 | * Created on: 28.07.2012
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 | #ifndef SUMMATOR_HPP_
|
---|
9 | #define SUMMATOR_HPP_
|
---|
10 |
|
---|
11 | // include config.h
|
---|
12 | #ifdef HAVE_CONFIG_H
|
---|
13 | #include <config.h>
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | #include <boost/fusion/sequence.hpp>
|
---|
17 |
|
---|
18 | #include "Fragmentation/Summation/Summation.hpp"
|
---|
19 | #include "Fragmentation/Summation/SetValue.hpp"
|
---|
20 | #include "Jobs/MPQCDataMap.hpp"
|
---|
21 |
|
---|
22 | /** Summator is a general class for making us of Summation.
|
---|
23 | *
|
---|
24 | * The idea is that we want to sum up not only one value but a whole bunch of.
|
---|
25 | * However, in general the types of the values will all differ. We would like
|
---|
26 | * to call the summation in a as simple a fashion as possible and at best in
|
---|
27 | * a kind of for_each on each type/value.
|
---|
28 | * For this purpose we require a list of all the types, represented by the
|
---|
29 | * MPQCDataMap_t map that uses the types enumerated in namespace MPQCDataFused,
|
---|
30 | * see the simple example for the associative sequence in boost::fusion.
|
---|
31 | * The MPQCDataMap_t then gives the essential mapping from the type to a specific
|
---|
32 | * key. Via this key, which is a type and hence can be used for template
|
---|
33 | * specification, we may gather all information for launching the
|
---|
34 | * Summation. We only need to convert MPQCData into the above
|
---|
35 | * MPQCDataMap_t instance and may then access the member variables via the above
|
---|
36 | * key and also obtain the type at \b run-time through the key.
|
---|
37 | *
|
---|
38 | * Note that we then need a conversion from the type stored in the MPQCData,
|
---|
39 | * e.g. a vector<double>, to the type in MPQCDataMap_t, e.g. Histogram.
|
---|
40 | *
|
---|
41 | * Boost::fusion is very cool! If we have a namespace with just structs as keys
|
---|
42 | * and the fusion::map from these keys to the true types, then we may do ...
|
---|
43 | * \code
|
---|
44 | * MPQCDataMap_t MPQCDataMap;
|
---|
45 | * using namespace MPQCDataFused;
|
---|
46 | * double MPQCData_energy_total = boost::fusion::at_key<MPQCDataFused::energy_total>(MPQCDataMap);
|
---|
47 | * \endcode
|
---|
48 | * i.e. at_key knows the correct type!
|
---|
49 | *
|
---|
50 | */
|
---|
51 | template <typename MapType, typename MapKey>
|
---|
52 | struct Summator {
|
---|
53 | /** We retrieve the type of the MPQCData member variable from the
|
---|
54 | * boost::fusion::map and stored it in this typedef. Note that for
|
---|
55 | * internal types (e.g. MapValue::const_iterator we need to place
|
---|
56 | * \b typename before).
|
---|
57 | */
|
---|
58 | typedef typename boost::fusion::result_of::value_at_key<MapType, MapKey>::type MapValue;
|
---|
59 |
|
---|
60 | /** Constructor for class Summator.
|
---|
61 | *
|
---|
62 | * \param _subsetmap map with hierarchy of IndexSet's
|
---|
63 | * \param _data MPQCData converted to MPQCDataMap_t type
|
---|
64 | * \param _jobids job ids to sum data in correct order
|
---|
65 | * \param _container container of IndexSet's such that each set has correct order
|
---|
66 | * to job id and hence to _data.
|
---|
67 | * \param _MatrixNrLookup lookup from job id to ordering in above vectors
|
---|
68 | */
|
---|
69 | Summator(
|
---|
70 | SubsetMap::ptr &_subsetmap,
|
---|
71 | const std::vector<MapType> &_data,
|
---|
72 | const std::vector<JobId_t> &_jobids,
|
---|
73 | const IndexSetContainer::Container_t &_container,
|
---|
74 | std::map< JobId_t, size_t > &_MatrixNrLookup) : /* cannot make this const due to operator[] */
|
---|
75 | indices(getSubsets(_container)),
|
---|
76 | values(createValues(_data, _jobids, _container, _MatrixNrLookup)),
|
---|
77 | OS(indices, values, _subsetmap)
|
---|
78 | {
|
---|
79 | ASSERT( _data.size() == _jobids.size(),
|
---|
80 | "Summator() - data and ids don't have same size.");
|
---|
81 | ASSERT( _jobids.size() == _MatrixNrLookup.size(),
|
---|
82 | "Summator() - ids and MatrixNrLookup don't have same size.");
|
---|
83 | }
|
---|
84 |
|
---|
85 | /** Summation operator.
|
---|
86 | *
|
---|
87 | * Initialises instantiated Summation of the respective type via
|
---|
88 | * \a Summator::data, uses Summation::operator() to sum and returns
|
---|
89 | * the result.
|
---|
90 | *
|
---|
91 | * \param level up to which level to sum up
|
---|
92 | * \return result of Summation for given type from MPQCDataMap_t.
|
---|
93 | */
|
---|
94 | MapValue operator()(/* const size_t level */)
|
---|
95 | {
|
---|
96 | // evaluate
|
---|
97 | const MapValue result = OS();
|
---|
98 | return result;
|
---|
99 | }
|
---|
100 |
|
---|
101 | private:
|
---|
102 | /** Tiny helper to create the indices from a given IndexSetContainer.
|
---|
103 | *
|
---|
104 | * Basically, we just have to make sure we miss the last one but only
|
---|
105 | * if the \a container has more than one set.
|
---|
106 | *
|
---|
107 | * @param container container with IndexSet
|
---|
108 | * @return all subsets contained in \a container
|
---|
109 | */
|
---|
110 | typename Summation<MapValue>::InputSets_t getSubsets(
|
---|
111 | const IndexSetContainer::Container_t &container)
|
---|
112 | {
|
---|
113 | // if we just have one indexset, we don't need to get rid of the "union index set"
|
---|
114 | typename Summation<MapValue>::InputSets_t indices(
|
---|
115 | container.begin(),
|
---|
116 | container.end()-1 == container.begin() ? container.end() : container.end()-1);
|
---|
117 | return indices;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /** Tiny helper to create the values for the summation in the correct order.
|
---|
121 | *
|
---|
122 | * @param data
|
---|
123 | * @param jobids
|
---|
124 | * @param container
|
---|
125 | * @param MatrixNrLookup
|
---|
126 | * @return
|
---|
127 | */
|
---|
128 | typename Summation<MapValue>::InputValues_t createValues(
|
---|
129 | const std::vector<MapType> &data,
|
---|
130 | const std::vector<JobId_t> &jobids,
|
---|
131 | const IndexSetContainer::Container_t &container,
|
---|
132 | std::map< JobId_t, size_t > &MatrixNrLookup)
|
---|
133 | {
|
---|
134 | // if we just have one indexset, we don't need to get rid of the "union index set"
|
---|
135 | typename Summation<MapValue>::InputValues_t values(
|
---|
136 | container.size() == 1 ? (size_t)1 : container.size()-1);
|
---|
137 | typename std::vector<MapType>::const_iterator dataiter = data.begin();
|
---|
138 | std::vector<size_t>::const_iterator iditer = jobids.begin();
|
---|
139 | for (; dataiter != data.end(); ++dataiter, ++iditer) {
|
---|
140 | const MapType &Data = *dataiter;
|
---|
141 | const MapValue &value = boost::fusion::at_key<MapKey>(Data);
|
---|
142 | values[ MatrixNrLookup[*iditer] ] = value;
|
---|
143 | }
|
---|
144 | return values;
|
---|
145 | }
|
---|
146 | private:
|
---|
147 | //!> created indices for OS such that we may hand over refs
|
---|
148 | typename Summation<MapValue>::InputSets_t indices;
|
---|
149 | //!> created values for OS such that we may hand over refs
|
---|
150 | typename Summation<MapValue>::InputValues_t values;
|
---|
151 | //!> Summation instance to use in operator()(level)
|
---|
152 | Summation<MapValue> OS;
|
---|
153 | };
|
---|
154 |
|
---|
155 |
|
---|
156 | #endif /* SUMMATOR_HPP_ */
|
---|