| [c40e15d] | 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 | 
 | 
|---|
| [302345] | 18 | #include <iterator>
 | 
|---|
 | 19 | 
 | 
|---|
| [3dd32f] | 20 | #include "CodePatterns/Assert.hpp"
 | 
|---|
 | 21 | 
 | 
|---|
| [c40e15d] | 22 | #include "Fragmentation/Summation/Summation.hpp"
 | 
|---|
 | 23 | #include "Fragmentation/Summation/SetValue.hpp"
 | 
|---|
 | 24 | 
 | 
|---|
| [a96b02] | 25 | /** Summator is a general class for making us of Summation.
 | 
|---|
| [c40e15d] | 26 |  *
 | 
|---|
 | 27 |  * The idea is that we want to sum up not only one value but a whole bunch of.
 | 
|---|
 | 28 |  * However, in general the types of the values will all differ. We would like
 | 
|---|
 | 29 |  * to call the summation in a as simple a fashion as possible and at best in
 | 
|---|
 | 30 |  * a kind of for_each on each type/value.
 | 
|---|
 | 31 |  * For this purpose we require a list of all the types, represented  by the
 | 
|---|
 | 32 |  * MPQCDataMap_t map that uses the types enumerated in namespace MPQCDataFused,
 | 
|---|
 | 33 |  * see the simple example for the associative sequence in boost::fusion.
 | 
|---|
 | 34 |  * The MPQCDataMap_t then gives the essential mapping from the type to a specific
 | 
|---|
 | 35 |  * key. Via this key, which is a type and hence can be used for template
 | 
|---|
 | 36 |  * specification, we may gather all information for launching the
 | 
|---|
| [a96b02] | 37 |  * Summation. We only need to convert MPQCData into the above
 | 
|---|
| [c40e15d] | 38 |  * MPQCDataMap_t instance and may then access the member variables via the above
 | 
|---|
 | 39 |  * key and also obtain the type at \b run-time through the key.
 | 
|---|
 | 40 |  *
 | 
|---|
 | 41 |  * Note that we then need a conversion from the type stored in the MPQCData,
 | 
|---|
 | 42 |  * e.g. a vector<double>, to the type in MPQCDataMap_t, e.g. Histogram.
 | 
|---|
 | 43 |  *
 | 
|---|
 | 44 |  * Boost::fusion is very cool! If we have a namespace with just structs as keys
 | 
|---|
 | 45 |  * and the fusion::map from these keys to the true types, then we may do ...
 | 
|---|
 | 46 |  * \code
 | 
|---|
 | 47 |  *   MPQCDataMap_t MPQCDataMap;
 | 
|---|
 | 48 |  *   using namespace MPQCDataFused;
 | 
|---|
 | 49 |  *   double MPQCData_energy_total = boost::fusion::at_key<MPQCDataFused::energy_total>(MPQCDataMap);
 | 
|---|
 | 50 |  * \endcode
 | 
|---|
 | 51 |  * i.e. at_key knows the correct type!
 | 
|---|
 | 52 |  *
 | 
|---|
 | 53 |  */
 | 
|---|
 | 54 | template <typename MapType, typename MapKey>
 | 
|---|
 | 55 | struct Summator {
 | 
|---|
 | 56 |   /** We retrieve the type of the MPQCData member variable from the
 | 
|---|
 | 57 |    * boost::fusion::map and stored it in this typedef. Note that for
 | 
|---|
 | 58 |    * internal types (e.g. MapValue::const_iterator we need to place
 | 
|---|
 | 59 |    * \b typename before).
 | 
|---|
 | 60 |    */
 | 
|---|
 | 61 |   typedef typename boost::fusion::result_of::value_at_key<MapType, MapKey>::type MapValue;
 | 
|---|
 | 62 | 
 | 
|---|
 | 63 |   /** Constructor for class Summator.
 | 
|---|
 | 64 |    *
 | 
|---|
 | 65 |    * \param _subsetmap map with hierarchy of IndexSet's
 | 
|---|
| [c9f9bb] | 66 |    * \param _data MPQCData converted to MPQCDataMap_t type, associated to job id
 | 
|---|
| [c40e15d] | 67 |    * \param _container container of IndexSet's such that each set has correct order
 | 
|---|
 | 68 |    *        to job id and hence to _data.
 | 
|---|
 | 69 |    * \param _MatrixNrLookup lookup from job id to ordering in above vectors
 | 
|---|
 | 70 |    */
 | 
|---|
 | 71 |   Summator(
 | 
|---|
 | 72 |       SubsetMap::ptr &_subsetmap,
 | 
|---|
| [c9f9bb] | 73 |       const std::map<JobId_t, MapType> &_data,
 | 
|---|
| [c40e15d] | 74 |       const IndexSetContainer::Container_t &_container,
 | 
|---|
| [3dd32f] | 75 |       const std::map< JobId_t, size_t > &_MatrixNrLookup) :
 | 
|---|
| [302345] | 76 |     indices(getSubsets(_data.size(),_container)),
 | 
|---|
| [c9f9bb] | 77 |     values(createValues(_data, _container, _MatrixNrLookup)),
 | 
|---|
| [a96b02] | 78 |     OS(indices, values, _subsetmap)
 | 
|---|
| [c40e15d] | 79 |   {
 | 
|---|
| [c9f9bb] | 80 |     ASSERT( _data.size() == _MatrixNrLookup.size(),
 | 
|---|
| [c40e15d] | 81 |         "Summator() - ids and MatrixNrLookup don't have same size.");
 | 
|---|
 | 82 |   }
 | 
|---|
 | 83 | 
 | 
|---|
 | 84 |   /** Summation operator.
 | 
|---|
 | 85 |    *
 | 
|---|
| [a96b02] | 86 |    * Initialises instantiated Summation of the respective type via
 | 
|---|
 | 87 |    * \a Summator::data, uses Summation::operator() to sum and returns
 | 
|---|
| [c40e15d] | 88 |    * the result.
 | 
|---|
 | 89 |    *
 | 
|---|
| [a96b02] | 90 |    * \param level up to which level to sum up
 | 
|---|
 | 91 |    * \return result of Summation for given type from MPQCDataMap_t.
 | 
|---|
| [c40e15d] | 92 |    */
 | 
|---|
| [ff9963] | 93 |   MapValue operator()(const size_t level)
 | 
|---|
| [a96b02] | 94 |   {
 | 
|---|
 | 95 |     // evaluate
 | 
|---|
| [ff9963] | 96 |     const MapValue result = OS(level);
 | 
|---|
| [a96b02] | 97 |     return result;
 | 
|---|
 | 98 |   }
 | 
|---|
 | 99 | 
 | 
|---|
| [69e065] | 100 | 
 | 
|---|
 | 101 |   /** Getter for the contribution of the SetValue<> to a specific \a index.
 | 
|---|
 | 102 |    *
 | 
|---|
 | 103 |    * @param index SetValue to this index
 | 
|---|
 | 104 |    * @return contribution
 | 
|---|
 | 105 |    */
 | 
|---|
 | 106 |   MapValue getContributionForIndexSet(const IndexSet::ptr &ptr) const
 | 
|---|
 | 107 |   {
 | 
|---|
 | 108 |     typedef SetValueMap<MapValue> setvalues_t;
 | 
|---|
 | 109 |     const setvalues_t &values = OS.getSetValues();
 | 
|---|
 | 110 |     return values.getConstValue(ptr)->getContribution();
 | 
|---|
 | 111 |   }
 | 
|---|
 | 112 | 
 | 
|---|
 | 113 |   /** Getter for the value of the SetValue<> to a specific \a index.
 | 
|---|
 | 114 |    *
 | 
|---|
 | 115 |    * @param index SetValue to this index
 | 
|---|
 | 116 |    * @return value
 | 
|---|
 | 117 |    */
 | 
|---|
 | 118 |   MapValue getValueForIndexSet(const IndexSet::ptr &ptr) const
 | 
|---|
 | 119 |   {
 | 
|---|
 | 120 |     typedef SetValueMap<MapValue> setvalues_t;
 | 
|---|
 | 121 |     const setvalues_t &values = OS.getSetValues();
 | 
|---|
 | 122 |     return values.getConstValue(ptr)->getValue();
 | 
|---|
 | 123 |   }
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 | 
 | 
|---|
| [a96b02] | 126 | private:
 | 
|---|
 | 127 |   /** Tiny helper to create the indices from a given IndexSetContainer.
 | 
|---|
 | 128 |    *
 | 
|---|
 | 129 |    * Basically, we just have to make sure we miss the last one but only
 | 
|---|
 | 130 |    * if the \a container has more than one set.
 | 
|---|
 | 131 |    *
 | 
|---|
| [302345] | 132 |    * @param count how many indices of container to use
 | 
|---|
| [a96b02] | 133 |    * @param container container with IndexSet
 | 
|---|
 | 134 |    * @return all subsets contained in \a container
 | 
|---|
 | 135 |    */
 | 
|---|
 | 136 |   typename Summation<MapValue>::InputSets_t getSubsets(
 | 
|---|
| [302345] | 137 |       const size_t count,
 | 
|---|
| [a96b02] | 138 |       const IndexSetContainer::Container_t &container)
 | 
|---|
| [c40e15d] | 139 |   {
 | 
|---|
| [302345] | 140 |     IndexSetContainer::Container_t::const_iterator iter = container.begin();
 | 
|---|
 | 141 |     std::advance(iter, count); // step till after desired element
 | 
|---|
 | 142 |     typename Summation<MapValue>::InputSets_t indices(container.begin(), iter);
 | 
|---|
| [a96b02] | 143 |     return indices;
 | 
|---|
 | 144 |   }
 | 
|---|
 | 145 | 
 | 
|---|
 | 146 |   /** Tiny helper to create the values for the summation in the correct order.
 | 
|---|
 | 147 |    *
 | 
|---|
 | 148 |    * @param data
 | 
|---|
 | 149 |    * @param container
 | 
|---|
 | 150 |    * @param MatrixNrLookup
 | 
|---|
 | 151 |    * @return
 | 
|---|
 | 152 |    */
 | 
|---|
 | 153 |   typename Summation<MapValue>::InputValues_t createValues(
 | 
|---|
| [c9f9bb] | 154 |       const std::map<JobId_t, MapType> &data,
 | 
|---|
| [a96b02] | 155 |       const IndexSetContainer::Container_t &container,
 | 
|---|
| [3dd32f] | 156 |       const std::map< JobId_t, size_t > &MatrixNrLookup)
 | 
|---|
| [a96b02] | 157 |   {
 | 
|---|
 | 158 |     // if we just have one indexset, we don't need to get rid of the "union index set"
 | 
|---|
| [302345] | 159 |     typename Summation<MapValue>::InputValues_t values(data.size());
 | 
|---|
| [c9f9bb] | 160 |     for (typename std::map<JobId_t, MapType>::const_iterator dataiter = data.begin();
 | 
|---|
 | 161 |         dataiter != data.end(); ++dataiter) {
 | 
|---|
 | 162 |       const MapType &Data = dataiter->second;
 | 
|---|
 | 163 |       const JobId_t &jobid = dataiter->first;
 | 
|---|
| [c40e15d] | 164 |       const MapValue &value = boost::fusion::at_key<MapKey>(Data);
 | 
|---|
| [3dd32f] | 165 |       const std::map< JobId_t, size_t >::const_iterator nriter = MatrixNrLookup.find(jobid);
 | 
|---|
 | 166 |       ASSERT( nriter != MatrixNrLookup.end(),
 | 
|---|
 | 167 |           "Summation<>::createValues() - MatrixNrLookup does not contain id "
 | 
|---|
 | 168 |           +toString(jobid)+".");
 | 
|---|
 | 169 |       values[ nriter->second ] = value;
 | 
|---|
| [c40e15d] | 170 |     }
 | 
|---|
| [a96b02] | 171 |     return values;
 | 
|---|
| [c40e15d] | 172 |   }
 | 
|---|
 | 173 | private:
 | 
|---|
| [a96b02] | 174 |   //!> created indices for OS such that we may hand over refs
 | 
|---|
 | 175 |   typename Summation<MapValue>::InputSets_t indices;
 | 
|---|
 | 176 |   //!> created values for OS such that we may hand over refs
 | 
|---|
 | 177 |   typename Summation<MapValue>::InputValues_t values;
 | 
|---|
 | 178 |   //!> Summation instance to use in operator()(level)
 | 
|---|
 | 179 |   Summation<MapValue> OS;
 | 
|---|
| [c40e15d] | 180 | };
 | 
|---|
 | 181 | 
 | 
|---|
 | 182 | 
 | 
|---|
 | 183 | #endif /* SUMMATOR_HPP_ */
 | 
|---|