OpenLB 1.7
Loading...
Searching...
No Matches
randomLoadBalancer.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2022 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 RANDOM_LOAD_BALANCER_H
25#define RANDOM_LOAD_BALANCER_H
26
28
29#include <random>
30#include <algorithm>
31
32namespace olb {
33
35
39template<typename T>
40struct RandomLoadBalancer final : public LoadBalancer<T> {
41 RandomLoadBalancer(int nCuboid, int nRank, int iRank):
42 LoadBalancer<T>(0)
43 {
44 std::vector<int> rankBuffer(nCuboid, 0);
45 std::vector<int> locBuffer(nCuboid, 0);
46
47 // Randomly distribute cuboids to ranks on rank 0
48 if (iRank == 0) {
49 std::vector<int> cuboids(nCuboid, 0);
50 std::iota(cuboids.begin(),
51 cuboids.end(),
52 0);
53 std::random_device randomDevice;
54 std::default_random_engine randomEngine(randomDevice());
55 std::shuffle(cuboids.begin(), cuboids.end(), randomEngine);
56
57 std::map<int,int> nLoc;
58 int jRank = 0;
59 for (int iCuboid : cuboids) {
60 if (jRank >= nRank) {
61 jRank -= nRank;
62 }
63 rankBuffer[iCuboid] = jRank;
64 locBuffer[iCuboid] = nLoc[jRank]++;
65 jRank += 1;
66 }
67 }
68
69 #ifdef PARALLEL_MODE_MPI
70 // Broadcast assignments to all processes
71 singleton::mpi().bCast(rankBuffer.data(), rankBuffer.size());
72 singleton::mpi().bCast(locBuffer.data(), locBuffer.size());
73 #endif
74
75 // Update internal LoadBalancer structure to match given assignment
76 for (int iCuboid=0; iCuboid < nCuboid; ++iCuboid) {
77 this->_rank[iCuboid] = rankBuffer[iCuboid];
78 this->_loc[iCuboid] = locBuffer[iCuboid];
79 if (rankBuffer[iCuboid] == singleton::mpi().getRank()) {
80 this->_glob.resize(std::max(int{this->_glob.size()}, this->_loc[iCuboid]+1));
81 this->_glob[this->_loc[iCuboid]] = iCuboid;
82 this->_size = this->_glob.size();
83 }
84 }
85 }
86
88 RandomLoadBalancer(cGeometry.getNc(),
89 singleton::mpi().getSize(),
90 singleton::mpi().getRank())
91 { }
92
93};
94
95}
96
97#endif
Base class for all LoadBalancer.
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
void bCast(T *sendBuf, int sendCount, int root=0, MPI_Comm comm=MPI_COMM_WORLD)
Broadcast data from one processor to multiple processors.
MpiManager & mpi()
Top level namespace for all of OpenLB.
std::conditional_t< D==2, CuboidGeometry2D< T >, CuboidGeometry3D< T > > CuboidGeometry
Definition aliases.h:47
Basic Random Load Balancer.
RandomLoadBalancer(CuboidGeometry< T, 3 > &cGeometry)
RandomLoadBalancer(int nCuboid, int nRank, int iRank)