OpenLB 1.8.1
Loading...
Searching...
No Matches
loadBalancer.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2024 Adrian Kummerlaender
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#ifndef REFINEMENT_LOAD_BALANCER_H
25#define REFINEMENT_LOAD_BALANCER_H
26
29
30namespace olb {
31
33
40template<typename T, unsigned D>
41class RefinedLoadBalancer final : public LoadBalancer<T> {
42private:
43 std::map<int,int> _coarseLoc;
44
45public:
47 LoadBalancer<T>& coarseBalancer,
48 CuboidDecomposition<T,D>& fineGeometry)
49 : LoadBalancer<T>(0)
50 {
51 OstreamManager clout(std::cout, "RefinedLoadBalancer");
52 clout.setMultiOutput(true);
53 std::vector<int> rankBuffer(fineGeometry.size(), 0);
54 std::vector<int> locBuffer(fineGeometry.size(), 0);
55 std::map<int,int> nLoc;
56 for (unsigned iC=0; iC < fineGeometry.size(); ++iC) {
57 const auto& fineCuboid = fineGeometry.get(iC);
58 if (auto latticeR = coarseGeometry.getLatticeR(fineCuboid.getOrigin())) {
59 const int coarseC = (*latticeR)[0];
60 if (coarseBalancer.isLocal(coarseC)) {
61 rankBuffer[iC] = coarseBalancer.rank(coarseC);
62 locBuffer[iC] = nLoc[rankBuffer[iC]]++;
63 this->_platform[iC] = coarseBalancer.platform(coarseBalancer.loc(coarseC));
64 clout << "Matching fineC=" << iC << " to coarseC=" << coarseC << " on rank=" << rankBuffer[iC] << std::endl;
65 _coarseLoc[locBuffer[iC]] = coarseBalancer.loc(coarseC);
66 }
67 }
68 }
69
70 #ifdef PARALLEL_MODE_MPI
71 std::vector<int> globalRankBuffer(fineGeometry.size(), 0);
72 std::vector<int> globalLocBuffer(fineGeometry.size(), 0);
73 // Broadcast assignments to all processes
74 singleton::mpi().allreduce(rankBuffer.data(), globalRankBuffer.data(), rankBuffer.size(), MPI_SUM);
75 singleton::mpi().allreduce(locBuffer.data(), globalLocBuffer.data(), locBuffer.size(), MPI_SUM);
76 #else
77 auto& globalRankBuffer = rankBuffer;
78 auto& globalLocBuffer = locBuffer;
79 #endif
80
81 // Update internal LoadBalancer structure to match given assignment
82 for (unsigned iC=0; iC < fineGeometry.size(); ++iC) {
83 this->_rank[iC] = globalRankBuffer[iC];
84 this->_loc[iC] = globalLocBuffer[iC];
85 if (globalRankBuffer[iC] == singleton::mpi().getRank()) {
86 this->_glob.resize(std::max(static_cast<int>(this->_glob.size()), this->_loc[iC]+1));
87 this->_glob[this->_loc[iC]] = iC;
88 this->_size = this->_glob.size();
89 }
90 }
91 clout.setMultiOutput(false);
92 }
93
95 int cloc(int fineLoc) const {
96 return _coarseLoc.at(fineLoc);
97 }
98
99};
100
101}
102
103#endif
Decomposition of a physical volume into a set of disjoint cuboids.
int size() const
Returns number of cuboids in decomposition.
const Cuboid< T, D > & get(int iC) const
Read access to a single cuboid.
std::optional< LatticeR< D+1 > > getLatticeR(Vector< T, D > physR) const
Returns lattice position for given physical position if it exists.
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
virtual Platform platform(int loc) const
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
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
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
Load balancer for refined lattice hierarchies.
int cloc(int fineLoc) const
Returns coarse loc for given fine loc.
RefinedLoadBalancer(CuboidDecomposition< T, D > &coarseGeometry, LoadBalancer< T > &coarseBalancer, CuboidDecomposition< T, D > &fineGeometry)
void allreduce(const T *in, T *out, int count, MPI_Op op, MPI_Comm comm=MPI_COMM_WORLD)
MpiManager & mpi()
Top level namespace for all of OpenLB.