Peano
Loading...
Searching...
No Matches
AbstractLoadBalancing.cpp
Go to the documentation of this file.
2
4
5tarch::logging::Log toolbox::loadbalancing::AbstractLoadBalancing::_log("toolbox::loadbalancing::AbstractLoadBalancing"
6);
7
8std::ostream& operator<<(std::ostream& out, const toolbox::loadbalancing::AbstractLoadBalancing& balancing) {
9 out << balancing.toString();
10 return out;
11}
12
14 Configuration* configuration, CostMetrics* costMetrics
15):
16 _blacklist(),
17 _statistics(),
18 _configuration(configuration),
19 _costMetrics(costMetrics),
21
23 if (_configuration != nullptr) {
24 delete _configuration;
25 }
26 if (_costMetrics != nullptr) {
27 delete _costMetrics;
28 }
29}
30
31
33 _configuration = nullptr;
34 _costMetrics = nullptr;
35}
36
37
39 std::ostringstream msg;
40
41 msg << "(state=" << ::toString(_state) << ",statistics=" << _statistics.toString()
42 << ",cost-metrics=" << _costMetrics->toString() << ",blacklist=" << _blacklist.toString() << ")";
43
44 return msg.str();
45}
46
47
49 return _statistics.getNumberOfStateUpdatesWithoutAnySplit() < 3;
50}
51
52
54 if (value and _state == State::SwitchedOff) {
56 logInfo("enable()", "switched load balancing on (again) and therefore change to state " << ::toString(_state));
57 } else if (not value and _state != State::SwitchedOff) {
58 logInfo("enable()", "switched load balancing off");
60 }
61
62 _statistics.notifyOfStateChange(_state);
63}
64
65
67 logDebug("isEnabled(bool)", toString());
68 return globally ? (_statistics.getGlobalNumberOfRanksWithEnabledLoadBalancing() > 0) : (_state != State::SwitchedOff);
69}
70
71
75
76
78 return _statistics.getGlobalNumberOfTrees();
79}
80
81
83 // This is a magic parameter with the 2.
84 // You might still run out of memory if you refine while you rebalance.
85 if (_configuration->makeSplitDependOnMemory(_state)) {
86 int worstCaseEstimateForSizeOfSpacetree = 2 * tarch::getMemoryUsage(tarch::MemoryUsageFormat::MByte);
87 return tarch::getFreeMemory(tarch::MemoryUsageFormat::MByte) >= worstCaseEstimateForSizeOfSpacetree;
88 } else {
89 return true;
90 }
91}
92
93
95 _statistics.waitForGlobalDataExchange();
96 _costMetrics->waitForGlobalDataExchange();
97}
98
99
101 if (not _statistics.hasConsistentViewOfWorld()) {
102 return false;
103 } else if (_statistics.getGlobalNumberOfTrees() < tarch::mpi::Rank::getInstance().getNumberOfRanks()) {
104 return true;
105 } else if ( static_cast<int>(peano4::parallel::SpacetreeSet::getInstance().getLocalSpacetrees().size()) >= _configuration->getMaxLocalTreesPerRank(_state) ) {
106 return false;
107 } else {
108 const double optimalCost = static_cast<double>(_costMetrics->getGlobalCost())
110
111 const double illbalancing = (_costMetrics->getCostOfLocalRank() - optimalCost) / optimalCost;
112 const double balancingRatio = _configuration->getWorstCaseBalancingRatio(_state);
113 bool result = illbalancing > 1.0 - balancingRatio;
114
115 if (result) {
116 logInfo(
117 "doesRankViolateBalancingCondition()",
118 "rank does violate balancing as we have ill-balancing of "
119 << illbalancing << " (global cost=" << _costMetrics->getGlobalCost() << ")"
120 );
121 }
122
123 return result;
124 }
125}
126
127
129 if (not _statistics.hasConsistentViewOfWorld()) {
130 return false;
131 } else if (peano4::parallel::SpacetreeSet::getInstance().getLocalSpacetrees().size() == 0) {
132 return false;
133 } else if (peano4::parallel::SpacetreeSet::getInstance().getLocalSpacetrees().size() == 1) {
134 return true;
135 } else if ( static_cast<int>(peano4::parallel::SpacetreeSet::getInstance().getLocalSpacetrees().size()) >= _configuration->getMaxLocalTreesPerRank(_state) ) {
136 return false;
137 } else {
138 double maxCost = 0;
139 double minCost = std::numeric_limits<double>::max();
140
142 maxCost = std::max(maxCost, _costMetrics->getCostOfLocalTree(p));
143 minCost = std::min(minCost, _costMetrics->getCostOfLocalTree(p));
144 }
145
146 assertion(minCost >= 0.0);
147 assertion(maxCost >= 0.0);
148
149 bool result = false;
150
151 if (maxCost > 0.0 and minCost > 0.0) {
152 double relativeWeightSmalltestTree = minCost / maxCost;
153 double balanceThreshold = _configuration->getWorstCaseBalancingRatio(_state);
154
155 result = relativeWeightSmalltestTree < balanceThreshold;
156
157 logDebug(
158 "isLocalBalancingBad()",
159 "local trees are ill-balanced="
160 << result << ", rel-weight=" << relativeWeightSmalltestTree << ", threshold=" << balanceThreshold
161 << ", #min-cost=" << minCost << ", #max-cost=" << maxCost
162 );
163 }
164
165 return result;
166 }
167}
168
169
171 return tarch::la::equals(_costMetrics->getMinimumOfMaximumRankWeights(), 0.0)
172 and _statistics.getGlobalNumberOfTrees() > 0;
173}
174
175
177 const int heaviestSpacetree = getIdOfHeaviestLocalSpacetree();
178 logDebug(
179 "getWeightOfHeaviestLocalSpacetree()",
180 "id="
181 << heaviestSpacetree << ", #octants="
183 .getGridStatistics(heaviestSpacetree)
184 .getNumberOfLocalUnrefinedCells()
185 );
186 return heaviestSpacetree == NoHeaviestTreeAvailable ? -1 : _costMetrics->getCostOfLocalTree(heaviestSpacetree);
187}
188
189
191 std::set<int> idsOfLocalSpacetrees = peano4::parallel::SpacetreeSet::getInstance().getLocalSpacetrees();
192 int result = NoHeaviestTreeAvailable;
193 double maxLocalCost = -1;
194
195 for (auto p : idsOfLocalSpacetrees) {
196 if (
197 _costMetrics->getCostOfLocalTree(p)>maxLocalCost
198 and
200 ) {
201 maxLocalCost = _costMetrics->getCostOfLocalTree(p) > maxLocalCost;
202 result = p;
203 }
204 }
205 return result;
206}
207
208
210 std::set<int> idsOfLocalSpacetrees = peano4::parallel::SpacetreeSet::getInstance().getLocalSpacetrees();
211 int result = NoHeaviestTreeAvailable;
212 int maxLocalCost = -1;
213
214 for (auto p : idsOfLocalSpacetrees) {
215 if (
216 _costMetrics->getCostOfLocalTree(p)>maxLocalCost
217 and
219 ) {
220 maxLocalCost = _costMetrics->getCostOfLocalTree(p);
221 }
222 }
223
224 if (maxLocalCost > 0.0) {
225 logInfo(
226 "getIdOfHeaviestLocalSpacetree(double)",
227 "try to find one maximum tree which has weight close to "
228 << maxLocalCost << ", i.e. has more than " << (tolerance * maxLocalCost) << " cost"
229 );
230 for (auto p : idsOfLocalSpacetrees) {
231 if (
232 tarch::la::greaterEquals( _costMetrics->getCostOfLocalTree(p), tolerance * maxLocalCost )
233 and
235 and
236 (rand() % 2 == 1 or result == NoHeaviestTreeAvailable)
237 ) {
238 logInfo(
239 "getIdOfHeaviestLocalSpacetree(double)",
240 "tree " << p << " meets criterion with weight of " << _costMetrics->getCostOfLocalTree(p)
241 );
242 result = p;
243 }
244 }
245 }
246 return result;
247}
std::ostream & operator<<(std::ostream &out, const toolbox::loadbalancing::AbstractLoadBalancing &balancing)
#define assertion(expr)
#define ThreePowerD
Definition Globals.h:24
AutomatonState state
#define logDebug(methodName, logMacroMessageStream)
Definition Log.h:50
#define logInfo(methodName, logMacroMessageStream)
Wrapper macro around tarch::tarch::logging::Log to improve logging.
Definition Log.h:411
peano4::grid::GridStatistics getGridStatistics() const
Return statistics object for primary spacetree.
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
AbstractLoadBalancing(Configuration *configuration, CostMetrics *costMetrics)
Constructor.
virtual std::string toString() const
Generic string serialisation.
bool isEnabled(bool globally) const
Clarifies whether load balancing is, in principle, enabled.
virtual bool hasStagnated() const
A load balancing can either be stagnating or be switched off for this predicate to hold.
bool isIntraRankBalancingBad() const
Is the balancing on the rank ok.
bool fitsIntoMemory(State state) const
Ensure enough memory is left-over.
virtual int getGlobalNumberOfTrees() const
Delegate to stats.
int getIdOfHeaviestLocalSpacetree() const
Determines the maximum spacetree size a tree should have in the optimal case.
bool isInterRankBalancingBad() const
Is the balancing between the ranks ok.
void setConfigurationAndMetricsNullWithoutDelete()
This is only used when you concatenate balancing rules and you want to disable any deletion.
Abstract interface to tweak the behaviour of the recursive subdivision.
std::string toString(Filter filter)
Definition convert.cpp:170
bool greaterEquals(double lhs, double rhs, double tolerance=NUMERICAL_ZERO_DIFFERENCE)
bool equals(const Matrix< Rows, Cols, Scalar > &lhs, const Matrix< Rows, Cols, Scalar > &rhs, const Scalar &tolerance=NUMERICAL_ZERO_DIFFERENCE)
Compares to matrices on equality by means of a numerical accuracy.
int getMemoryUsage(MemoryUsageFormat format)
Method for getting the application's memory footprint.
Definition tarch.cpp:95
int getFreeMemory(MemoryUsageFormat format)
Definition tarch.cpp:83
State
State descriptor of load balancing.
Definition State.h:22
@ InterRankBalancing
We have completed the first two phases and try to balance between the ranks now to meet the load bala...
@ SwitchedOff
You usually don't get this state when we query the configuration, i.e.
@ Stagnation
You usually don't get this state when we query the configuration, i.e.