Peano
Loading...
Searching...
No Matches
CostMetrics.cpp
Go to the documentation of this file.
1#include "CostMetrics.h"
2
4
5
6tarch::logging::Log toolbox::loadbalancing::CostMetrics::_log( "toolbox::loadbalancing::CostMetrics" );
7
8
10 _localRankWeight(0.0),
11 _globalWeight(0.0),
12 _minimumOfMaximumOfRankWeights(0.0),
13 _globalWeightIn(0.0),
14 _globalWeightOut(0.0),
15 _minimumOfMaximumOfRankWeightsIn(0.0),
16 _minimumOfMaximumOfRankWeightsOut(0.0) {
17 #ifdef Parallel
18 _globalWeightRequest = nullptr;
19 _lightestRankRequest = nullptr;
21 #endif
22}
23
24
25std::string toolbox::loadbalancing::CostMetrics::toString(const std::string& metricName) const {
26 std::ostringstream msg;
27 msg << "("
28 << metricName
29 << ",local-rank-weight=" << _localRankWeight
30 << ",global-weight=" << _globalWeight
31 << ",lightest-rank=" << _lightestRank._rank
32 << ",lightest-ranks-weight=" << _lightestRank._weight
33 << ",min-of-max-of-rank-weights=" << _minimumOfMaximumOfRankWeights
34 << ")";
35 return msg.str();
36}
37
38
40 double localCost = 0;
41 std::set<int> idsOfLocalSpacetrees = peano4::parallel::SpacetreeSet::getInstance().getLocalSpacetrees();
42 for (auto p: idsOfLocalSpacetrees) {
43 localCost += getCostOfLocalTree(p);
44 }
45 return localCost;
46}
47
48
50 #ifdef Parallel
51 if (_globalWeightRequest != nullptr ) {
52 MPI_Wait( _globalWeightRequest, MPI_STATUS_IGNORE );
53 MPI_Wait( _lightestRankRequest, MPI_STATUS_IGNORE );
54 MPI_Wait( _minimumOfMaximumOfRankWeightsRequest, MPI_STATUS_IGNORE );
55
56 delete _globalWeightRequest;
57 delete _lightestRankRequest;
58 delete _minimumOfMaximumOfRankWeightsRequest;
59
60 _globalWeightRequest = nullptr;
61 _lightestRankRequest = nullptr;
62 _minimumOfMaximumOfRankWeightsRequest = nullptr;
63 }
64 #endif
65}
66
67
69 if (tarch::mpi::Rank::getInstance().getNumberOfRanks()<=1) {
70 _globalWeight = _localRankWeight;
71 _lightestRank._rank = 0;
72 _lightestRank._weight = getCostOfLocalRank();
73 _minimumOfMaximumOfRankWeights = getCostOfLocalRank();
74 }
75 else {
76 #ifdef Parallel
77 waitForGlobalDataExchange();
78
79 _globalWeight = _globalWeightIn;
80 _lightestRank._rank = _lightestRankIn._weight < _localRankWeight ? _lightestRankIn._rank : tarch::mpi::Rank::getInstance().getRank();
81 _lightestRank._weight = _lightestRankIn._weight;
82 _minimumOfMaximumOfRankWeights = _minimumOfMaximumOfRankWeightsIn;
83
84 if ( _globalWeight < _localRankWeight ) {
85 logInfo(
86 "updateGlobalView()",
87 "local number of cells (" << _localRankWeight << ") is bigger than global cell count (" << _globalWeight <<
88 "). This usually happens if a forking tree has some pending refinement events and cannot refine anymore, as it has already spawned cells. Statistics might have inconsistent view of world"
89 );
90 _globalWeight = _localRankWeight;
91 _lightestRank._rank = tarch::mpi::Rank::getInstance().getRank();
92 }
93
94 _globalWeightRequest = new MPI_Request();
95 _lightestRankRequest = new MPI_Request();
96 _minimumOfMaximumOfRankWeightsRequest = new MPI_Request();
97
98 _globalWeightOut = _localRankWeight;
99 _lightestRankOut._weight = _localRankWeight;
100 _lightestRankOut._rank = tarch::mpi::Rank::getInstance().getRank();
101
102 MPI_Iallreduce(
103 &_globalWeightOut, // send
104 &_globalWeightIn, // receive
105 1, // count
106 MPI_DOUBLE,
107 MPI_SUM,
108 tarch::mpi::Rank::getInstance().getCommunicator(),
109 _globalWeightRequest
110 );
111 MPI_Iallreduce(
112 &_lightestRankOut, // send
113 &_lightestRankIn, // receive
114 1, // count
115 MPI_DOUBLE_INT,
116 MPI_MINLOC,
117 tarch::mpi::Rank::getInstance().getCommunicator(),
118 _lightestRankRequest
119 );
120 MPI_Iallreduce(
121 &_minimumOfMaximumOfRankWeightsOut, // send
122 &_minimumOfMaximumOfRankWeightsIn, // receive
123 1, // count
124 MPI_DOUBLE,
125 MPI_MIN,
126 tarch::mpi::Rank::getInstance().getCommunicator(),
127 _minimumOfMaximumOfRankWeightsRequest
128 );
129 #endif
130 }
131}
132
134 return _globalWeight;
135}
136
137
139 assertion1( _lightestRank._rank>=0, toString() );
141 return _lightestRank._rank;
142}
143
144
146 return _minimumOfMaximumOfRankWeights;
147}
#define assertion1(expr, param)
#define logInfo(methodName, logMacroMessageStream)
Wrapper macro around tarch::tarch::logging::Log to improve logging.
Definition Log.h:411
static SpacetreeSet & getInstance()
std::set< int > getLocalSpacetrees() const
Log Device.
Definition Log.h:516
int getNumberOfRanks() const
Definition Rank.cpp:552
static Rank & getInstance()
This operation returns the singleton instance.
Definition Rank.cpp:539
int getRank() const
Return rank of this node.
Definition Rank.cpp:529
virtual double getMinimumOfMaximumRankWeights() const
static tarch::logging::Log _log
virtual double getCostOfLocalRank() const
Wrapper around getCostOfLocalTree().
MPI_Request * _globalWeightRequest
Replicate compared to stats.
virtual void updateGlobalView()
Please overwrite in subclass and set the value of _localRankWeight.
virtual void waitForGlobalDataExchange()
Typically called by.
virtual std::string toString() const =0
Feel free to invoke the variant below if you want.
MPI_Request * _minimumOfMaximumOfRankWeightsRequest
virtual double getGlobalCost() const
std::string toString(Filter filter)
Definition convert.cpp:170