| [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 | 
 | 
|---|
 | 18 | #include "Fragmentation/Summation/OrthogonalSummation.hpp"
 | 
|---|
 | 19 | #include "Fragmentation/Summation/SetValue.hpp"
 | 
|---|
 | 20 | #include "Jobs/MPQCDataMap.hpp"
 | 
|---|
 | 21 | 
 | 
|---|
| [c6ca23] | 22 | /** OrthogonalSummator is a general class for making us of OrthogonalSummation.
 | 
|---|
| [56df37] | 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>
 | 
|---|
| [c6ca23] | 52 | struct OrthogonalSummator {
 | 
|---|
| [56df37] | 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 | 
 | 
|---|
| [c6ca23] | 60 |   /** Constructor for class OrthogonalSummator.
 | 
|---|
| [56df37] | 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 |    */
 | 
|---|
| [c6ca23] | 69 |   OrthogonalSummator(
 | 
|---|
| [56df37] | 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[] */
 | 
|---|
| [a96b02] | 75 |     indices(getSubsets(_container)),
 | 
|---|
 | 76 |     values(createValues(_data, _jobids, _container, _MatrixNrLookup)),
 | 
|---|
 | 77 |     OS(indices, values, _subsetmap)
 | 
|---|
| [56df37] | 78 |   {
 | 
|---|
| [a96b02] | 79 |     ASSERT( _data.size() == _jobids.size(),
 | 
|---|
| [c6ca23] | 80 |         "OrthogonalSummator() - data and ids don't have same size.");
 | 
|---|
| [a96b02] | 81 |     ASSERT( _jobids.size() == _MatrixNrLookup.size(),
 | 
|---|
| [c6ca23] | 82 |         "OrthogonalSummator() - ids and MatrixNrLookup don't have same size.");
 | 
|---|
| [56df37] | 83 |   }
 | 
|---|
 | 84 | 
 | 
|---|
 | 85 |   /** Summation operator.
 | 
|---|
 | 86 |    *
 | 
|---|
 | 87 |    * Initialises instantiated OrthogonalSummation of the respective type via
 | 
|---|
| [c6ca23] | 88 |    * \a OrthogonalSummator::data, uses OrthogonalSummation::operator() to sum and returns
 | 
|---|
| [56df37] | 89 |    * the result.
 | 
|---|
 | 90 |    *
 | 
|---|
| [a96b02] | 91 |    * \param level up to which level to sum up
 | 
|---|
| [56df37] | 92 |    * \return result of OrthogonalSummation for given type from MPQCDataMap_t.
 | 
|---|
 | 93 |    */
 | 
|---|
| [ff9963] | 94 |   MapValue operator()(const size_t level)
 | 
|---|
| [a96b02] | 95 |   {
 | 
|---|
 | 96 |     // evaluate
 | 
|---|
| [ff9963] | 97 |     const MapValue result = OS(level);
 | 
|---|
| [a96b02] | 98 |     return result;
 | 
|---|
 | 99 |   }
 | 
|---|
 | 100 | 
 | 
|---|
 | 101 | private:
 | 
|---|
 | 102 |   /** Tiny helper to create the indices from a given IndexSetContainer.
 | 
|---|
 | 103 |    *
 | 
|---|
 | 104 |    * Basically, we just have to make sure we miss the last one but only
 | 
|---|
 | 105 |    * if the \a container has more than one set.
 | 
|---|
 | 106 |    *
 | 
|---|
 | 107 |    * @param container container with IndexSet
 | 
|---|
 | 108 |    * @return all subsets contained in \a container
 | 
|---|
 | 109 |    */
 | 
|---|
 | 110 |   typename OrthogonalSummation<MapValue>::InputSets_t getSubsets(
 | 
|---|
 | 111 |       const IndexSetContainer::Container_t &container)
 | 
|---|
| [56df37] | 112 |   {
 | 
|---|
 | 113 |     // if we just have one indexset, we don't need to get rid of the "union index set"
 | 
|---|
 | 114 |     typename OrthogonalSummation<MapValue>::InputSets_t indices(
 | 
|---|
 | 115 |         container.begin(),
 | 
|---|
 | 116 |         container.end()-1 == container.begin() ? container.end() : container.end()-1);
 | 
|---|
| [a96b02] | 117 |     return indices;
 | 
|---|
 | 118 |   }
 | 
|---|
 | 119 | 
 | 
|---|
 | 120 |   /** Tiny helper to create the values for the summation in the correct order.
 | 
|---|
 | 121 |    *
 | 
|---|
 | 122 |    * @param data
 | 
|---|
 | 123 |    * @param jobids
 | 
|---|
 | 124 |    * @param container
 | 
|---|
 | 125 |    * @param MatrixNrLookup
 | 
|---|
 | 126 |    * @return
 | 
|---|
 | 127 |    */
 | 
|---|
 | 128 |   typename OrthogonalSummation<MapValue>::InputValues_t createValues(
 | 
|---|
 | 129 |       const std::vector<MapType> &data,
 | 
|---|
 | 130 |       const std::vector<JobId_t> &jobids,
 | 
|---|
 | 131 |       const IndexSetContainer::Container_t &container,
 | 
|---|
 | 132 |       std::map< JobId_t, size_t > &MatrixNrLookup)
 | 
|---|
 | 133 |   {
 | 
|---|
 | 134 |     // if we just have one indexset, we don't need to get rid of the "union index set"
 | 
|---|
| [56df37] | 135 |     typename OrthogonalSummation<MapValue>::InputValues_t values(
 | 
|---|
 | 136 |         container.size() == 1 ? (size_t)1 : container.size()-1);
 | 
|---|
 | 137 |     typename std::vector<MapType>::const_iterator dataiter = data.begin();
 | 
|---|
 | 138 |     std::vector<size_t>::const_iterator iditer = jobids.begin();
 | 
|---|
 | 139 |     for (; dataiter != data.end(); ++dataiter, ++iditer) {
 | 
|---|
 | 140 |       const MapType &Data = *dataiter;
 | 
|---|
 | 141 |       const MapValue &value = boost::fusion::at_key<MapKey>(Data);
 | 
|---|
 | 142 |       values[ MatrixNrLookup[*iditer] ] = value;
 | 
|---|
 | 143 |     }
 | 
|---|
| [a96b02] | 144 |     return values;
 | 
|---|
| [56df37] | 145 |   }
 | 
|---|
 | 146 | private:
 | 
|---|
| [a96b02] | 147 |   //!> created indices for OS such that we may hand over refs
 | 
|---|
 | 148 |   typename OrthogonalSummation<MapValue>::InputSets_t indices;
 | 
|---|
 | 149 |   //!> created values for OS such that we may hand over refs
 | 
|---|
 | 150 |   typename OrthogonalSummation<MapValue>::InputValues_t values;
 | 
|---|
 | 151 |   //!> Summation instance to use in operator()(level)
 | 
|---|
 | 152 |   OrthogonalSummation<MapValue> OS;
 | 
|---|
| [56df37] | 153 | };
 | 
|---|
 | 154 | 
 | 
|---|
 | 155 | 
 | 
|---|
| [c6ca23] | 156 | #endif /* ORTHOGONALSUMMATOR_HPP_ */
 | 
|---|