| 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/OrthogonalSummation.hpp" | 
|---|
| 19 | #include "Fragmentation/Summation/SetValue.hpp" | 
|---|
| 20 | #include "Jobs/MPQCDataMap.hpp" | 
|---|
| 21 |  | 
|---|
| 22 | /** Summator is a general class for making us of OrthogonalSummation. | 
|---|
| 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 | * OrthogonalSummation. 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 | subsetmap(_subsetmap), | 
|---|
| 76 | data(_data), | 
|---|
| 77 | jobids(_jobids), | 
|---|
| 78 | container(_container), | 
|---|
| 79 | MatrixNrLookup(_MatrixNrLookup) | 
|---|
| 80 | { | 
|---|
| 81 | ASSERT( data.size() == jobids.size(), | 
|---|
| 82 | "Summator() - data and ids don't have same size."); | 
|---|
| 83 | ASSERT( jobids.size() == MatrixNrLookup.size(), | 
|---|
| 84 | "Summator() - ids and MatrixNrLookup don't have same size."); | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | /** Summation operator. | 
|---|
| 88 | * | 
|---|
| 89 | * Initialises instantiated OrthogonalSummation of the respective type via | 
|---|
| 90 | * \a Summator::data, uses OrthogonalSummation::operator() to sum and returns | 
|---|
| 91 | * the result. | 
|---|
| 92 | * | 
|---|
| 93 | * \return result of OrthogonalSummation for given type from MPQCDataMap_t. | 
|---|
| 94 | */ | 
|---|
| 95 | MapValue operator()() | 
|---|
| 96 | { | 
|---|
| 97 | // if we just have one indexset, we don't need to get rid of the "union index set" | 
|---|
| 98 | typename OrthogonalSummation<MapValue>::InputSets_t indices( | 
|---|
| 99 | container.begin(), | 
|---|
| 100 | container.end()-1 == container.begin() ? container.end() : container.end()-1); | 
|---|
| 101 | typename OrthogonalSummation<MapValue>::InputValues_t values( | 
|---|
| 102 | container.size() == 1 ? (size_t)1 : container.size()-1); | 
|---|
| 103 | typename std::vector<MapType>::const_iterator dataiter = data.begin(); | 
|---|
| 104 | std::vector<size_t>::const_iterator iditer = jobids.begin(); | 
|---|
| 105 | for (; dataiter != data.end(); ++dataiter, ++iditer) { | 
|---|
| 106 | const MapType &Data = *dataiter; | 
|---|
| 107 | const MapValue &value = boost::fusion::at_key<MapKey>(Data); | 
|---|
| 108 | values[ MatrixNrLookup[*iditer] ] = value; | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | // create the summation functor and evaluate | 
|---|
| 112 | OrthogonalSummation<MapValue> OS(indices, values, subsetmap); | 
|---|
| 113 | const MapValue result = OS(); | 
|---|
| 114 | return result; | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | private: | 
|---|
| 118 | //!> Hierarchy of IndexSet's | 
|---|
| 119 | SubsetMap::ptr &subsetmap; | 
|---|
| 120 | //!> vector of data converted from MPQCData | 
|---|
| 121 | const std::vector<MapType> &data; | 
|---|
| 122 | //!> vector of jobids | 
|---|
| 123 | const std::vector<JobId_t> &jobids; | 
|---|
| 124 | //!> container with all IndexSet's | 
|---|
| 125 | const IndexSetContainer::Container_t &container; | 
|---|
| 126 | //!> lookup map from job ids to ordering in above vectors | 
|---|
| 127 | std::map< JobId_t, size_t > MatrixNrLookup; | 
|---|
| 128 | }; | 
|---|
| 129 |  | 
|---|
| 130 |  | 
|---|
| 131 | #endif /* SUMMATOR_HPP_ */ | 
|---|