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