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