OpenLB 1.7
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) 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_H
26#define LOAD_BALANCER_H
27
28#include <vector>
29#include <map>
30
31#include "core/singleton.h"
32#include "core/serializer.h"
33#include "io/xmlReader.h"
35
36namespace olb {
37
38
39//template<typename T, typename BaseType> class Vector;
40template<typename T> class CuboidGeometry3D;
41template<typename T> class CuboidGeometry2D;
42template<typename T> class HeuristicLoadBalancer;
43
58template<typename T>
60protected:
62 int _size;
64 std::map<int,int> _loc;
66 std::vector<int> _glob;
68 std::map<int,int> _rank;
70 std::map<int,Platform> _platform;
71
72public:
74 LoadBalancer(int size=1);
76 LoadBalancer(int size, std::map<int,int>& loc, std::vector<int>& glob, std::map<int,int>& rank);
78 LoadBalancer(int size, std::map<int,int>& loc, std::vector<int>& glob, std::map<int,int>& rank, std::map<int,Platform>& platform);
80 virtual ~LoadBalancer();
82 void swap(LoadBalancer<T>& loadBalancer);
84 bool isLocal(const int& glob);
86 int loc(const int& glob);
88 int loc(int glob) const;
90 int glob(int loc) const;
92 int rank(const int& glob);
94 int rank(int glob) const;
96 int size() const;
98 int getRankSize() const;
99
101 virtual Platform platform(int loc) const {
102 auto iter = _platform.find(glob(loc));
103 if (iter != _platform.end()) {
104 return std::get<1>(*iter);
105 } else {
106 #ifdef PLATFORM_GPU_CUDA
107 return Platform::GPU_CUDA;
108 #else
109 #ifdef PLATFORM_CPU_SIMD
110 return Platform::CPU_SIMD;
111 #else
112 return Platform::CPU_SISD;
113 #endif
114 #endif
115 }
116 }
117
118 virtual void setPlatform(int loc, Platform platform) {
120 }
121
123 bool operator==(const LoadBalancer<T>& rhs) const;
124
126 std::size_t getNblock() const override;
128 std::size_t getSerializableSize() const override;
130 bool* getBlock(std::size_t iBlock, std::size_t& sizeBlock, bool loadingMode) override;
131
132 void print(bool multiOutput = false) const;
133
134};
135
136
139// or within the reader (XML tag) itself. Either choice is saved in lbXml.
141template<typename T>
143{
144 OstreamManager clout(std::cout, "createLoadBalancer");
145 std::string defaultMode = "Block";
146
147 LoadBalancer<T>* lb; // The LoadBalancer to be returned
148
149 // Read file attribute to decide if a new XML reader has to be created
150 XMLreader const* lbXml;
151 std::string fileAttr = xmlReader.getAttribute("file");
152 bool newFile = ( fileAttr != "Attribute not found." );
153 if ( newFile ) {
154 lbXml = new XMLreader(fileAttr);
155 }
156 else {
157 lbXml = &xmlReader;
158 }
159
160 bool verbose = false;
161 (*lbXml).setWarningsOn(false);
162
163 std::string mode = lbXml->getAttribute("mode");
164 if ( mode == "Attribute not found.") {
165 clout << "Warning: Cannot read parameter from Xml-file: Mode. Set default: mode = " << defaultMode << std::endl;
166 mode = defaultMode;
167 }
168
169 // Heuristic Mode
170 if ( mode == "Heuristic" ) {
171 // only read ratioFullEmpty - Heuristic LB will constructed from cuboidGeometry deterministicly
172 double ratioFullEmpty;
173 if (!(*lbXml)["RatioFullEmpty"].read<double>(ratioFullEmpty, verbose)) {
174 lb = new HeuristicLoadBalancer<T>(*cGeo);
175 }
176 else {
177 lb = new HeuristicLoadBalancer<T>(*cGeo, ratioFullEmpty);
178 }
179 }
180
181 // Base Mode
182 else if ( mode == "Base" ) {
183 //LoadBalancer<T>* loadBalancer = new LoadBalancer<T>();
184 //loadBalancer->load(fileName);
185 clout << "LOADING BASE LB NOT IMPLEMENTED YET!" << std::endl;
186 (*lbXml).setWarningsOn(true);
187 //return loadBalancer;
188 lb = NULL;
189 }
190
191 // Block Mode
192 else if ( mode == "Block" ) {
193 int size = 1;
194 if (!(*lbXml)["Size"].read<int>(size, verbose)) {
195 clout << "Warning: Cannot read parameter from Xml-file: Size. Set default: size = 1"
196 << std::endl;
197 }
198 lb = new LoadBalancer<T>(size);
199 }
200
201
202 (*lbXml).setWarningsOn(true);
203
204 // Delete XMLreader if it was created for the LB file
205 if ( newFile ) {
206 delete lbXml;
207 }
208
209 return lb;
210}
211
212
214template<typename T>
215LoadBalancer<T>* createLoadBalancer(std::string const& fileName, CuboidGeometry3D<T>* cGeo = NULL)
216{
217 std::string fname = singleton::directories().getLogOutDir() + fileName + ".xml";
218 XMLreader lbReader(fname);
219 return createLoadBalancer(lbReader["LoadBalancer"], cGeo);
220}
221
222} // namespace olb
223
224#endif
Base class for serializable objects of dynamic size
Definition serializer.h:299
A cuboid geometry represents a voxel mesh.
Constructs a load balancer from a given cuboid geometry using a heurist.
Base class for all LoadBalancer.
int rank(const int &glob)
bool operator==(const LoadBalancer< T > &rhs) const
equal operator
virtual void setPlatform(int loc, Platform platform)
bool isLocal(const int &glob)
returns whether glob is on this process
virtual Platform platform(int loc) const
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
std::string getAttribute(const std::string &aName) const
Definition xmlReader.h:541
void setWarningsOn(bool warnings) const
switch warnings on/off
Definition xmlReader.h:412
std::string getLogOutDir() const
Definition singleton.h:89
Directories & directories()
Definition singleton.h:150
Top level namespace for all of OpenLB.
LoadBalancer< T > * createLoadBalancer(XMLreader const &xmlReader, CuboidGeometry3D< T > *cGeo=NULL)
Creator Function for LoadBalancer from XMLreader.
Platform
OpenLB execution targets.
Definition platform.h:36
@ CPU_SIMD
Basic scalar CPU.
@ GPU_CUDA
Vector CPU (AVX2 / AVX-512 collision)
Definition of singletons: global, publicly available information.
Input/Output in XML format – header file.