| [56df37] | 1 | /* | 
|---|
| [c6ca23] | 2 | * OrthogonalSummator.hpp | 
|---|
| [56df37] | 3 | * | 
|---|
|  | 4 | *  Created on: 28.07.2012 | 
|---|
|  | 5 | *      Author: heber | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [c6ca23] | 8 | #ifndef ORTHOGONALSUMMATOR_HPP_ | 
|---|
|  | 9 | #define ORTHOGONALSUMMATOR_HPP_ | 
|---|
| [56df37] | 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 |  | 
|---|
| [56df37] | 22 | #include "Fragmentation/Summation/OrthogonalSummation.hpp" | 
|---|
|  | 23 | #include "Fragmentation/Summation/SetValue.hpp" | 
|---|
|  | 24 |  | 
|---|
| [c6ca23] | 25 | /** OrthogonalSummator is a general class for making us of OrthogonalSummation. | 
|---|
| [56df37] | 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 | 
|---|
|  | 37 | * OrthogonalSummation. We only need to convert MPQCData into the above | 
|---|
|  | 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> | 
|---|
| [c6ca23] | 55 | struct OrthogonalSummator { | 
|---|
| [56df37] | 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 |  | 
|---|
| [c6ca23] | 63 | /** Constructor for class OrthogonalSummator. | 
|---|
| [56df37] | 64 | * | 
|---|
|  | 65 | * \param _subsetmap map with hierarchy of IndexSet's | 
|---|
| [c9f9bb] | 66 | * \param _data MPQCData converted to MPQCDataMap_t type, associated to job id | 
|---|
| [56df37] | 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 | */ | 
|---|
| [c6ca23] | 71 | OrthogonalSummator( | 
|---|
| [56df37] | 72 | SubsetMap::ptr &_subsetmap, | 
|---|
| [c9f9bb] | 73 | const std::map<JobId_t, MapType> &_data, | 
|---|
| [56df37] | 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) | 
|---|
| [56df37] | 79 | { | 
|---|
| [c9f9bb] | 80 | ASSERT( _data.size() == _MatrixNrLookup.size(), | 
|---|
| [c6ca23] | 81 | "OrthogonalSummator() - ids and MatrixNrLookup don't have same size."); | 
|---|
| [56df37] | 82 | } | 
|---|
|  | 83 |  | 
|---|
|  | 84 | /** Summation operator. | 
|---|
|  | 85 | * | 
|---|
|  | 86 | * Initialises instantiated OrthogonalSummation of the respective type via | 
|---|
| [c6ca23] | 87 | * \a OrthogonalSummator::data, uses OrthogonalSummation::operator() to sum and returns | 
|---|
| [56df37] | 88 | * the result. | 
|---|
|  | 89 | * | 
|---|
| [a96b02] | 90 | * \param level up to which level to sum up | 
|---|
| [56df37] | 91 | * \return result of OrthogonalSummation for given type from MPQCDataMap_t. | 
|---|
|  | 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 | /** Getter for the contribution of the SetValue<> to a specific \a index. | 
|---|
|  | 101 | * | 
|---|
|  | 102 | * @param index SetValue to this index | 
|---|
|  | 103 | * @return contribution | 
|---|
|  | 104 | */ | 
|---|
|  | 105 | MapValue getContributionForIndexSet(const IndexSet::ptr &ptr) const | 
|---|
|  | 106 | { | 
|---|
|  | 107 | typedef SetValueMap<MapValue> setvalues_t; | 
|---|
|  | 108 | const setvalues_t &values = OS.getSetValues(); | 
|---|
|  | 109 | return values.getConstValue(ptr)->getContribution(); | 
|---|
|  | 110 | } | 
|---|
|  | 111 |  | 
|---|
|  | 112 | /** Getter for the value of the SetValue<> to a specific \a index. | 
|---|
|  | 113 | * | 
|---|
|  | 114 | * @param index SetValue to this index | 
|---|
|  | 115 | * @return value | 
|---|
|  | 116 | */ | 
|---|
|  | 117 | MapValue getValueForIndexSet(const IndexSet::ptr &ptr) const | 
|---|
|  | 118 | { | 
|---|
|  | 119 | typedef SetValueMap<MapValue> setvalues_t; | 
|---|
|  | 120 | const setvalues_t &values = OS.getSetValues(); | 
|---|
|  | 121 | return values.getConstValue(ptr)->getValue(); | 
|---|
|  | 122 | } | 
|---|
|  | 123 |  | 
|---|
| [a96b02] | 124 | private: | 
|---|
|  | 125 | /** Tiny helper to create the indices from a given IndexSetContainer. | 
|---|
|  | 126 | * | 
|---|
|  | 127 | * Basically, we just have to make sure we miss the last one but only | 
|---|
|  | 128 | * if the \a container has more than one set. | 
|---|
|  | 129 | * | 
|---|
| [302345] | 130 | * @param count how many indices of container to use | 
|---|
| [a96b02] | 131 | * @param container container with IndexSet | 
|---|
|  | 132 | * @return all subsets contained in \a container | 
|---|
|  | 133 | */ | 
|---|
|  | 134 | typename OrthogonalSummation<MapValue>::InputSets_t getSubsets( | 
|---|
| [302345] | 135 | const size_t count, | 
|---|
| [a96b02] | 136 | const IndexSetContainer::Container_t &container) | 
|---|
| [56df37] | 137 | { | 
|---|
| [302345] | 138 | IndexSetContainer::Container_t::const_iterator iter = container.begin(); | 
|---|
|  | 139 | std::advance(iter, count); // step till after desired element | 
|---|
|  | 140 | typename OrthogonalSummation<MapValue>::InputSets_t indices(container.begin(), iter); | 
|---|
| [a96b02] | 141 | return indices; | 
|---|
|  | 142 | } | 
|---|
|  | 143 |  | 
|---|
|  | 144 | /** Tiny helper to create the values for the summation in the correct order. | 
|---|
|  | 145 | * | 
|---|
|  | 146 | * @param data | 
|---|
|  | 147 | * @param container | 
|---|
|  | 148 | * @param MatrixNrLookup | 
|---|
|  | 149 | * @return | 
|---|
|  | 150 | */ | 
|---|
|  | 151 | typename OrthogonalSummation<MapValue>::InputValues_t createValues( | 
|---|
| [c9f9bb] | 152 | const std::map<JobId_t, MapType> &data, | 
|---|
| [a96b02] | 153 | const IndexSetContainer::Container_t &container, | 
|---|
| [3dd32f] | 154 | const std::map< JobId_t, size_t > &MatrixNrLookup) | 
|---|
| [a96b02] | 155 | { | 
|---|
| [302345] | 156 | // if the power set of , we don't need to get rid of the "union index set" | 
|---|
|  | 157 | typename OrthogonalSummation<MapValue>::InputValues_t values(data.size()); | 
|---|
| [c9f9bb] | 158 | for (typename std::map<JobId_t, MapType>::const_iterator dataiter = data.begin(); | 
|---|
|  | 159 | dataiter != data.end(); ++dataiter) { | 
|---|
|  | 160 | const MapType &Data = dataiter->second; | 
|---|
|  | 161 | const JobId_t &jobid = dataiter->first; | 
|---|
| [56df37] | 162 | const MapValue &value = boost::fusion::at_key<MapKey>(Data); | 
|---|
| [3dd32f] | 163 | const std::map< JobId_t, size_t >::const_iterator nriter = MatrixNrLookup.find(jobid); | 
|---|
|  | 164 | ASSERT( nriter != MatrixNrLookup.end(), | 
|---|
|  | 165 | "OrthogonalSummation<>::createValues() - MatrixNrLookup does not contain id " | 
|---|
|  | 166 | +toString(jobid)+"."); | 
|---|
|  | 167 | values[ nriter->second ] = value; | 
|---|
| [56df37] | 168 | } | 
|---|
| [a96b02] | 169 | return values; | 
|---|
| [56df37] | 170 | } | 
|---|
|  | 171 | private: | 
|---|
| [a96b02] | 172 | //!> created indices for OS such that we may hand over refs | 
|---|
|  | 173 | typename OrthogonalSummation<MapValue>::InputSets_t indices; | 
|---|
|  | 174 | //!> created values for OS such that we may hand over refs | 
|---|
|  | 175 | typename OrthogonalSummation<MapValue>::InputValues_t values; | 
|---|
|  | 176 | //!> Summation instance to use in operator()(level) | 
|---|
|  | 177 | OrthogonalSummation<MapValue> OS; | 
|---|
| [56df37] | 178 | }; | 
|---|
|  | 179 |  | 
|---|
|  | 180 |  | 
|---|
| [c6ca23] | 181 | #endif /* ORTHOGONALSUMMATOR_HPP_ */ | 
|---|