OpenLB 1.7
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)
70{
71 return rank(glob) == singleton::mpi().getRank();
72}
73
74template<typename T>
75int LoadBalancer<T>::loc(const int& glob)
77 return _loc[glob];
79
80template<typename T>
81int LoadBalancer<T>::loc(int glob) const
83 std::map<int,int>::const_iterator iter = _loc.find(glob);
84 return iter->second;
85}
87template<typename T>
88int LoadBalancer<T>::glob(int loc) const
89{
90 return _glob[loc];
91}
93template<typename T>
94int LoadBalancer<T>::rank(const int& glob)
95{
96 return _rank[glob];
97}
99template<typename T>
100int LoadBalancer<T>::rank(int glob) const
101{
102 std::map<int,int>::const_iterator iter = _rank.find(glob);
103 return iter->second;
104}
105
106template<typename T>
108{
109 return _size;
110}
111
112template<typename T>
114{
115 return _rank.size();
116}
117
118template<typename T>
120{
121 return _size == rhs._size &&
122 _loc == rhs._loc &&
123 _glob == rhs._glob &&
124 _rank == rhs._rank;
125}
127
128template<typename T>
131 return 4 // _size, plus vector length of _loc, _glob and _rank
132 + _loc.size()
133 + _rank.size()
134 + _glob.size();
135}
136
137
138template<typename T>
140{
141 return sizeof(int) // _size
142 + 3 * sizeof(size_t) // vector length of _loc, _glob and _rank
143 + _loc.size() * sizeof(std::pair<int, int>)
144 + _rank.size() * sizeof(std::pair<int, int>)
145 + _glob.size() * sizeof(int);
146}
147
148
149template<typename T>
150bool* LoadBalancer<T>::getBlock(std::size_t iBlock, std::size_t& sizeBlock, bool loadingMode)
151{
152 std::size_t currentBlock = 0;
153 size_t sizeBufferIndex = 0;
154 bool* dataPtr = nullptr;
155
156 this->registerVar<int> (iBlock, sizeBlock, currentBlock, dataPtr, _size);
157 this->registerMap<int, int> (iBlock, sizeBlock, currentBlock, sizeBufferIndex, dataPtr, _loc, loadingMode);
158 this->registerStdVectorOfVars<int>(iBlock, sizeBlock, currentBlock, sizeBufferIndex, dataPtr, _glob, loadingMode);
159 this->registerMap<int, int> (iBlock, sizeBlock, currentBlock, sizeBufferIndex, dataPtr, _rank, loadingMode);
160
161 return dataPtr;
162}
163
164
165template<typename T>
166void LoadBalancer<T>::print(bool multiOutput) const
167{
168 OstreamManager clout(std::cout,"LoadBalancer");
169 clout.setMultiOutput(multiOutput);
170 for (unsigned i = 0; i < this->_glob.size(); i++) {
171 clout << "glob[" << i << "]=" << this->_glob[i] << std::endl;
172 }
173 for (const auto& [globC, locC] : this->_loc) {
174 clout << "loc[" << globC << "]=" << locC << std::endl;
175 }
176 for (const auto& [globC, rank] : this->_rank) {
177 clout << "rank[" << globC << "]=" << rank << std::endl;
178 }
179 clout.setMultiOutput(false);
180}
181
182
183} // namespace olb
184#endif
Base class for all LoadBalancer.
int rank(const int &glob)
bool operator==(const LoadBalancer< T > &rhs) const
equal operator
bool isLocal(const int &glob)
returns whether glob is on this process
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.