| [c40e15d] | 1 | /*
 | 
|---|
| [ff9963] | 2 |  * AllLevelSummator.hpp
 | 
|---|
| [c40e15d] | 3 |  *
 | 
|---|
 | 4 |  *  Created on: 29.07.2012
 | 
|---|
 | 5 |  *      Author: heber
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [ff9963] | 8 | #ifndef ALLLEVELSUMMATOR_HPP_
 | 
|---|
 | 9 | #define ALLLEVELSUMMATOR_HPP_
 | 
|---|
| [c40e15d] | 10 | 
 | 
|---|
 | 11 | // include config.h
 | 
|---|
 | 12 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 13 | #include <config.h>
 | 
|---|
 | 14 | #endif
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | #include <vector>
 | 
|---|
 | 17 | 
 | 
|---|
| [e08108] | 18 | #include <boost/fusion/sequence.hpp>
 | 
|---|
 | 19 | 
 | 
|---|
| [ff9963] | 20 | #include "CodePatterns/Assert.hpp"
 | 
|---|
 | 21 | 
 | 
|---|
| [c40e15d] | 22 | #include "Fragmentation/Summation/IndexSetContainer.hpp"
 | 
|---|
 | 23 | #include "Fragmentation/Summation/SubsetMap.hpp"
 | 
|---|
 | 24 | #include "Fragmentation/Summation/Summator.hpp"
 | 
|---|
| [da098d2] | 25 | #include "Fragmentation/Summation/ZeroInstance.hpp"
 | 
|---|
| [c40e15d] | 26 | 
 | 
|---|
 | 27 | #include "Fragmentation/Summation/printKeyNames.hpp"
 | 
|---|
 | 28 | 
 | 
|---|
| [ff9963] | 29 | /** Tiny template functor to use Summation, sum up each level, and store the
 | 
|---|
 | 30 |  * result in a given vector.
 | 
|---|
| [c40e15d] | 31 |  *
 | 
|---|
 | 32 |  */
 | 
|---|
 | 33 | template <typename MapType>
 | 
|---|
| [ff9963] | 34 | struct AllLevelSummator {
 | 
|---|
| [c40e15d] | 35 |   /** Constructor takes the arguments that \a Summator also needs and stores
 | 
|---|
 | 36 |    * them internally.
 | 
|---|
 | 37 |    *
 | 
|---|
 | 38 |    * \param _subsetmap map with hierarchy of IndexSet's
 | 
|---|
| [c9f9bb] | 39 |    * \param _data MPQCData converted to MPQCDataMap_t type, associated to job id
 | 
|---|
| [c40e15d] | 40 |    * \param _container container of IndexSet's such that each set has correct order
 | 
|---|
 | 41 |    *        to job id and hence to _data.
 | 
|---|
| [675cd6] | 42 |    * \param _MatrixNrLookup lookup from job id to ordering in above vectors, if
 | 
|---|
 | 43 |    *        out of range this value is set to zero, hence ignored.
 | 
|---|
| [b8f0b25] | 44 |    * \param _levelresults vector place levelresults into
 | 
|---|
| [da098d2] | 45 |    * \param _keysetresults results per index set, set by operator()
 | 
|---|
 | 46 |    * \param _ZeroInstances zero instance to use in summation for each type
 | 
|---|
| [c40e15d] | 47 |    */
 | 
|---|
| [ff9963] | 48 |   AllLevelSummator(
 | 
|---|
| [c40e15d] | 49 |       SubsetMap::ptr &_subsetmap,
 | 
|---|
| [c9f9bb] | 50 |       const std::map<JobId_t, MapType> &_data,
 | 
|---|
| [c40e15d] | 51 |       const IndexSetContainer::Container_t &_container,
 | 
|---|
| [3dd32f] | 52 |       const std::map< JobId_t, size_t > &_MatrixNrLookup,
 | 
|---|
| [b8f0b25] | 53 |       std::vector<MapType> &_levelresults,
 | 
|---|
| [da098d2] | 54 |       std::map<IndexSet::ptr, std::pair<MapType, MapType> > &_keysetresults,
 | 
|---|
 | 55 |       MapType &_ZeroInstances) :
 | 
|---|
| [c40e15d] | 56 |     subsetmap(_subsetmap),
 | 
|---|
 | 57 |     data(_data),
 | 
|---|
 | 58 |     container(_container),
 | 
|---|
| [ff9963] | 59 |     MatrixNrLookup(_MatrixNrLookup),
 | 
|---|
| [b8f0b25] | 60 |     levelresults(_levelresults),
 | 
|---|
| [da098d2] | 61 |     keysetresults(_keysetresults),
 | 
|---|
 | 62 |     ZeroInstances(_ZeroInstances)
 | 
|---|
| [ff9963] | 63 |   {
 | 
|---|
| [b8f0b25] | 64 |     ASSERT( levelresults.size() >= subsetmap->getMaximumSetLevel(),
 | 
|---|
| [ff9963] | 65 |         "AllLevelSummator() - result vector is not large enough.");
 | 
|---|
 | 66 |   }
 | 
|---|
| [c40e15d] | 67 | 
 | 
|---|
 | 68 |   /** Operator that calls on Summator and prints the value.
 | 
|---|
 | 69 |    *
 | 
|---|
 | 70 |    * \note the parameter is needed for boost::mpl::for_each but is not
 | 
|---|
 | 71 |    * used here.
 | 
|---|
 | 72 |    */
 | 
|---|
 | 73 |   template <typename MapKey>
 | 
|---|
 | 74 |   void operator()(MapKey &) {
 | 
|---|
 | 75 |     // We retrieve the type of the MPQCData member variable from the boost::fusion::map.
 | 
|---|
 | 76 |     typedef typename boost::fusion::result_of::value_at_key<MapType, MapKey>::type MapValue;
 | 
|---|
| [b8f0b25] | 77 | 
 | 
|---|
| [c40e15d] | 78 |     // create Summator instance
 | 
|---|
 | 79 |     Summator<MapType, MapKey> sum_value(
 | 
|---|
| [c9f9bb] | 80 |         subsetmap, data, container, MatrixNrLookup
 | 
|---|
| [c40e15d] | 81 |         );
 | 
|---|
| [b8f0b25] | 82 | 
 | 
|---|
| [da098d2] | 83 |     // set zero instance
 | 
|---|
 | 84 |     const MapValue &DesiredZeroInstance = boost::fusion::at_key<MapKey>(ZeroInstances);
 | 
|---|
 | 85 |     if (DesiredZeroInstance != ZeroInstance<MapValue>())
 | 
|---|
 | 86 |       sum_value.setZeroInstance(DesiredZeroInstance);
 | 
|---|
 | 87 | 
 | 
|---|
| [b8f0b25] | 88 |     // fill levelresults
 | 
|---|
| [19c50e] | 89 |     const size_t MaxLevel = subsetmap->getMaximumSetLevel();
 | 
|---|
| [ff9963] | 90 |     for (size_t level=1; level <= MaxLevel; ++level) {
 | 
|---|
| [b8f0b25] | 91 |       MapType &LevelResults = levelresults[level-1];
 | 
|---|
 | 92 |       // sum up and store in levelresults
 | 
|---|
| [ff9963] | 93 |       const MapValue value = sum_value(level);
 | 
|---|
 | 94 |       boost::fusion::at_key<MapKey>(LevelResults) = value;
 | 
|---|
 | 95 |       // print value
 | 
|---|
 | 96 |       //LOG(0, "STATUS: Level " << level << " resulting " << printKeyNames::printName<MapKey>() << " is " << value << ".");
 | 
|---|
 | 97 |     }
 | 
|---|
| [b8f0b25] | 98 | 
 | 
|---|
 | 99 |     // fill keysetresults
 | 
|---|
 | 100 |     for (IndexSetContainer::Container_t::const_iterator indexsetiter = container.begin();
 | 
|---|
 | 101 |         indexsetiter != container.end(); ++indexsetiter) {
 | 
|---|
 | 102 |       const IndexSet::ptr &index = *indexsetiter;
 | 
|---|
 | 103 |       // this either generates a new entry or updates the present one, as we obtain ref to value
 | 
|---|
| [79398d] | 104 |       boost::fusion::at_key<MapKey>(keysetresults[index].first) =
 | 
|---|
 | 105 |           sum_value.getValueForIndexSet(index);
 | 
|---|
 | 106 |       boost::fusion::at_key<MapKey>(keysetresults[index].second) =
 | 
|---|
| [b8f0b25] | 107 |           sum_value.getContributionForIndexSet(index);
 | 
|---|
 | 108 |     }
 | 
|---|
| [c40e15d] | 109 |   }
 | 
|---|
 | 110 | 
 | 
|---|
 | 111 | private:
 | 
|---|
 | 112 |   //!> Hierarchy of IndexSet's
 | 
|---|
 | 113 |   SubsetMap::ptr &subsetmap;
 | 
|---|
 | 114 |   //!> vector of data converted from MPQCData
 | 
|---|
| [c9f9bb] | 115 |   const std::map<JobId_t, MapType> &data;
 | 
|---|
| [c40e15d] | 116 |   //!> container with all IndexSet's
 | 
|---|
 | 117 |   const IndexSetContainer::Container_t &container;
 | 
|---|
 | 118 |   //!> lookup map from job ids to ordering in above vectors
 | 
|---|
| [3dd32f] | 119 |   const std::map< JobId_t, size_t > &MatrixNrLookup;
 | 
|---|
| [b8f0b25] | 120 |   //!> vector of levelresults
 | 
|---|
 | 121 |   std::vector<MapType> &levelresults;
 | 
|---|
| [79398d] | 122 |   typedef std::pair< IndexSet::ptr, std::pair<MapType, MapType> > keysetresults_pair_t;
 | 
|---|
| [b8f0b25] | 123 |   //!> typedef for map for keyset and each resulting value
 | 
|---|
| [79398d] | 124 |   typedef std::map<IndexSet::ptr, std::pair<MapType, MapType> > keysetresults_t;
 | 
|---|
| [b8f0b25] | 125 |   //!> map for IndexSet and its associated contribution
 | 
|---|
 | 126 |   keysetresults_t &keysetresults;
 | 
|---|
| [da098d2] | 127 |   //!> map per type to sum of base/zero instances to enforce specific parameters in summation
 | 
|---|
 | 128 |   MapType &ZeroInstances;
 | 
|---|
| [c40e15d] | 129 | };
 | 
|---|
 | 130 | 
 | 
|---|
| [ff9963] | 131 | #endif /* ALLLEVELSUMMATOR_HPP_ */
 | 
|---|