OpenLB 1.8.1
Loading...
Searching...
No Matches
loadBalancer.hh
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2007, 2014 Mathias Krause, Peter Weisbrod
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
24
25#ifndef LOAD_BALANCER_HH
26#define LOAD_BALANCER_HH
27
29
30namespace olb {
31
32
33template<typename T>
34LoadBalancer<T>::LoadBalancer(int size) : _size(size)
35{}
36
37template<typename T>
39 std::map<int,int>& loc,
40 std::vector<int>& glob,
41 std::map<int,int>& rank)
42 : _size(size), _loc(loc), _glob(glob), _rank(rank)
43{}
44
45template<typename T>
47 std::map<int,int>& loc,
48 std::vector<int>& glob,
49 std::map<int,int>& rank,
50 std::map<int,Platform>& platform)
51 : _size(size), _loc(loc), _glob(glob), _rank(rank), _platform(platform)
52{}
53
54template<typename T>
57
58template<typename T>
60{
61 std::swap(_size, loadBalancer._size);
62 _loc.swap(loadBalancer._loc);
63 _glob.swap(loadBalancer._glob);
64 _rank.swap(loadBalancer._rank);
65 _platform.swap(loadBalancer._platform);
66}
67
68template<typename T>
69bool LoadBalancer<T>::isLocal(const int& glob) const
70{
71 return rank(glob) == singleton::mpi().getRank();
72}
73
74template<typename T>
77 for (auto [loc, _] : _loc) {
78 if (this->platform(loc) == platform) {
79 return true;
80 }
81 }
82 return false;
83}
85template<typename T>
86int LoadBalancer<T>::loc(const int& glob)
87{
88 return _loc[glob];
89}
91template<typename T>
92int LoadBalancer<T>::loc(int glob) const
93{
94 std::map<int,int>::const_iterator iter = _loc.find(glob);
95 return iter->second;
97
98template<typename T>
99int LoadBalancer<T>::glob(int loc) const
101 return _glob[loc];
102}
103
104template<typename T>
105int LoadBalancer<T>::rank(int glob) const
106{
107 std::map<int,int>::const_iterator iter = _rank.find(glob);
108 return iter->second;
109}
110
111template<typename T>
113{
114 return _size;
115}
116
117template<typename T>
119{
120 return _rank.size();
121}
122
123template<typename T>
125{
126 return _size == rhs._size &&
127 _loc == rhs._loc &&
128 _glob == rhs._glob &&
129 _rank == rhs._rank;
130}
131
132
133template<typename T>
135{
136 return 4 // _size, plus vector length of _loc, _glob and _rank
137 + _loc.size()
138 + _rank.size()
139 + _glob.size();
140}
142
143template<typename T>
146 return sizeof(int) // _size
147 + 3 * sizeof(size_t) // vector length of _loc, _glob and _rank
148 + _loc.size() * sizeof(std::pair<int, int>)
149 + _rank.size() * sizeof(std::pair<int, int>)
150 + _glob.size() * sizeof(int);
151}
152
153
154template<typename T>
155bool* LoadBalancer<T>::getBlock(std::size_t iBlock, std::size_t& sizeBlock, bool loadingMode)
156{
157 std::size_t currentBlock = 0;
158 size_t sizeBufferIndex = 0;
159 bool* dataPtr = nullptr;
160
161 this->registerVar<int> (iBlock, sizeBlock, currentBlock, dataPtr, _size);
162 this->registerMap<int, int> (iBlock, sizeBlock, currentBlock, sizeBufferIndex, dataPtr, _loc, loadingMode);
163 this->registerStdVectorOfVars<int>(iBlock, sizeBlock, currentBlock, sizeBufferIndex, dataPtr, _glob, loadingMode);
164 this->registerMap<int, int> (iBlock, sizeBlock, currentBlock, sizeBufferIndex, dataPtr, _rank, loadingMode);
165
166 return dataPtr;
167}
168
169
170template<typename T>
171void LoadBalancer<T>::print(bool multiOutput) const
172{
173 OstreamManager clout(std::cout,"LoadBalancer");
174 clout.setMultiOutput(multiOutput);
175 for (unsigned i = 0; i < this->_glob.size(); i++) {
176 clout << "glob[" << i << "]=" << this->_glob[i] << std::endl;
177 }
178 for (const auto& [globC, locC] : this->_loc) {
179 clout << "loc[" << globC << "]=" << locC << std::endl;
180 }
181 for (const auto& [globC, rank] : this->_rank) {
182 clout << "rank[" << globC << "]=" << rank << std::endl;
183 }
184 clout.setMultiOutput(false);
185}
186
187
188} // namespace olb
189#endif
Base class for all LoadBalancer.
Definition vtiWriter.h:42
bool isLocal(const int &glob) const
returns whether glob is on this process
int rank(int glob) const
bool operator==(const LoadBalancer< T > &rhs) const
equal operator
int getRankSize() const
int glob(int loc) const
std::size_t getSerializableSize() const override
Binary size for the serializer.
void print(bool multiOutput=false) const
virtual ~LoadBalancer()
Default empty destructor.
std::map< int, Platform > _platform
maps global cuboid number to local platform
std::vector< int > _glob
content is 0,1,2,...,_size
int _size
number of cuboids after shrink -1 in appropriate thread
void swap(LoadBalancer< T > &loadBalancer)
Swap method.
std::size_t getNblock() const override
Number of data blocks for the serializable interface.
bool * getBlock(std::size_t iBlock, std::size_t &sizeBlock, bool loadingMode) override
Return a pointer to the memory of the current block and its size for the serializable interface.
std::map< int, int > _rank
maps global cuboid number to the processing thread
std::map< int, int > _loc
maps global cuboid to (local) thread cuboid
LoadBalancer(int size=1)
Default empty constructor.
int loc(const int &glob)
class for marking output with some text
void setMultiOutput(bool b)
enable message output for all MPI processes, disabled by default
int getRank() const
Returns the process ID.
MpiManager & mpi()
Top level namespace for all of OpenLB.
Platform
OpenLB execution targets.
Definition platform.h:35