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