OpenLB 1.8.1
Loading...
Searching...
No Matches
blockDynamicsMap.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2019 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 BLOCK_DYNAMICS_MAP_H
25#define BLOCK_DYNAMICS_MAP_H
26
27#include <functional>
28#include <typeindex>
29#include <algorithm>
30#include <optional>
31#include <unordered_set>
32
33#include "dynamics/dynamics.h"
34#include "cse/wrapper.h"
35
36#include "platform/cpu/cell.h"
37
38#include "introspection.h"
39
40namespace olb {
41
42template<typename T, typename DESCRIPTOR> struct AbstractCollisionO;
43template<typename T, typename DESCRIPTOR, Platform PLATFORM> struct BlockCollisionO;
44template<typename T, typename DESCRIPTOR, Platform PLATFORM> class ConcreteBlockLattice;
45template<typename T, Platform PLATFORM> class ConcreteBlockMask;
46template<typename T, typename DESCRIPTOR, Platform PLATFORM, typename DYNAMICS> class ConcreteBlockCollisionO;
47
48template <typename T, typename DESCRIPTOR, typename DYNAMICS>
59
61
72template <typename T, typename DESCRIPTOR>
74protected:
75 const std::type_index _id;
76
77 std::function<Dynamics<T,DESCRIPTOR>*()> _makeDynamics;
78 std::function<AbstractCollisionO<T,DESCRIPTOR>*(Platform)> _makeOperator;
79
80 const std::string _name;
81
83 std::function<std::set<FieldTypePromise<T,DESCRIPTOR>>()> _accessedFields;
84
86 std::function<std::optional<std::size_t>()> _inspectArithmeticOperationCount;
87 std::function<std::optional<std::size_t>()> _inspectMemoryBandwidth;
88 std::function<std::optional<bool>()> _inspectOptimizability;
89 std::function<std::optional<std::size_t>()> _inspectComplexity;
90
91public:
92 template <typename DYNAMICS>
94 _id(typeid(DYNAMICS)),
95 _makeDynamics([]() -> Dynamics<T,DESCRIPTOR>* {
96 return new DYNAMICS();
97 }),
98 _makeOperator([](Platform platform) -> AbstractCollisionO<T,DESCRIPTOR>* {
99 if constexpr (concepts::IntrospectableDynamics<DYNAMICS>) {
100 if constexpr (dynamics::is_cse_optimized<DYNAMICS>::value) {
102 } else {
104 }
105 } else {
107 }
108 }),
110 _accessedFields([]() -> std::set<FieldTypePromise<T,DESCRIPTOR>> {
111 if constexpr (!std::is_same_v<T,Expr>) {
112 if constexpr (std::is_same_v<NoDynamics<T,DESCRIPTOR>, DYNAMICS>) {
113 return {};
114 } else {
116 }
117 } else {
118 throw std::domain_error("Can not introspect the introspection");
119 }
120 }),
121 _isOptimizationAvailable{dynamics::is_cse_optimized<DYNAMICS>::value},
122 _inspectArithmeticOperationCount([]() -> std::optional<std::size_t> {
123#ifdef FEATURE_INSPECT_DYNAMICS
124 if constexpr (!std::is_same_v<T,Expr>) {
125 if constexpr (dynamics::is_cse_optimized<DYNAMICS>::value) {
127 } else {
129 }
130 } else {
131 throw std::domain_error("Can not introspect the introspection - we, sadly, are not in LISP land.");
132 }
133#endif // FEATURE_INSPECT_DYNAMICS
134 return std::nullopt;
135 }),
136 _inspectMemoryBandwidth([]() -> std::optional<std::size_t> {
137#ifdef FEATURE_INSPECT_DYNAMICS
138 if constexpr (!std::is_same_v<T,Expr>) {
139 if constexpr (dynamics::is_cse_optimized<DYNAMICS>::value) {
141 } else {
143 }
144 } else {
145 throw std::domain_error("Can not introspect the introspection");
146 }
147#endif // FEATURE_INSPECT_DYNAMICS
148 return std::nullopt;
149 }),
150 _inspectOptimizability([]() -> std::optional<bool> {
151#ifdef FEATURE_INSPECT_DYNAMICS
152 if constexpr (!std::is_same_v<T,Expr>) {
154 } else {
155 return false;
156 }
157#else
158 return std::nullopt;
159#endif // FEATURE_INSPECT_DYNAMICS
160 }),
161 _inspectComplexity([]() -> std::optional<std::size_t> {
162#ifdef FEATURE_INSPECT_DYNAMICS
163 if constexpr (!std::is_same_v<T,Expr>) {
164 if constexpr (dynamics::is_cse_optimized<DYNAMICS>::value) {
166 } else {
168 }
169 } else {
170 throw std::domain_error("Can not introspect the introspection - we, sadly, are not in LISP land.");
171 }
172#endif // FEATURE_INSPECT_DYNAMICS
173 return std::nullopt;
174 })
175 { }
176
178 std::type_index id() const {
179 return _id;
180 }
181
183 std::string name() const {
184 return _name;
185 }
186
188 std::set<FieldTypePromise<T,DESCRIPTOR>> accessedFields() const {
189 return _accessedFields();
190 }
191
193 bool hasOptimizedVersion() const {
195 }
196
198 std::optional<bool> isOptimizable() const {
199 return _inspectOptimizability();
200 }
201
203 std::optional<std::size_t> getArithmeticOperationCount() const {
205 }
206
208 std::optional<std::size_t> getMemoryBandwidth() const {
209 if (auto bandwidth = _inspectMemoryBandwidth()) {
210 return *bandwidth * sizeof(T);
211 } else {
212 return std::nullopt;
213 }
214 }
215
217 std::optional<std::size_t> getComplexity() const {
218 return _inspectComplexity();
219 }
220
222 return id() < rhs.id();
223 }
224
229
231 template <Platform PLATFORM>
236
237};
238
239template <typename DYNAMICS>
241 typename DYNAMICS::descriptor_t>;
242
243
245
249 template <typename T, typename DESCRIPTOR, Platform PLATFORM>
251};
252
254
264template<typename T, typename DESCRIPTOR, Platform PLATFORM>
266private:
268
269 std::map<std::type_index,
270 std::tuple<DynamicsPromise<T,DESCRIPTOR>,
271 std::unique_ptr<BlockCollisionO<T,DESCRIPTOR,PLATFORM>>>> _map;
272
273 std::unique_ptr<Dynamics<T,DESCRIPTOR>*[]> _dynamicsOfCells;
274 std::unique_ptr<BlockCollisionO<T,DESCRIPTOR,PLATFORM>*[]> _operatorOfCells;
275
279 BlockCollisionO<T,DESCRIPTOR,PLATFORM>* _dominantCollisionO;
280
283 {
284 auto iter = _map.find(promise.id());
285 if (iter == _map.end()) {
286 iter = _map.emplace(std::piecewise_construct,
287 std::forward_as_tuple(promise.id()),
288 std::forward_as_tuple(promise, promise.template realize<PLATFORM>())).first;
289 auto& [_, op] = iter->second;
290 op->setup(_lattice);
291 if (_lattice.isIntrospectable()) {
292 auto accessedFields = promise.accessedFields();
293 for (FieldTypePromise<T,DESCRIPTOR> field : accessedFields) {
294 field.ensureAvailabilityIn(_lattice);
295 }
296 }
297 return *op;
298 } else {
299 auto& [_, op] = iter->second;
300 return *op;
301 }
302 }
303
304public:
307 _lattice(lattice),
308 _dynamicsOfCells(new Dynamics<T,DESCRIPTOR>* [_lattice.getNcells()] { nullptr }),
309 _operatorOfCells(new BlockCollisionO<T,DESCRIPTOR,PLATFORM>*[_lattice.getNcells()] { nullptr }),
310 _coreMask(lattice.template getData<CollisionSubdomainMask>()),
311 _dominantCollisionO(nullptr)
312 {
314 _coreMask.set(_lattice.getCellId(lattice), true);
315 });
316 }
317
319
323 {
324 return resolve(std::forward<decltype(promise)>(promise)).getDynamics();
325 }
326
327 const Dynamics<T,DESCRIPTOR>* get(std::size_t iCell) const
328 {
329 return _dynamicsOfCells[iCell];
330 }
331
332 Dynamics<T,DESCRIPTOR>* get(std::size_t iCell)
333 {
334 return _dynamicsOfCells[iCell];
335 }
336
338 void set(std::size_t iCell, DynamicsPromise<T,DESCRIPTOR>&& promise)
339 {
340 auto& collisionO = resolve(std::forward<decltype(promise)>(promise));
341 if (_operatorOfCells[iCell] != nullptr) {
342 _operatorOfCells[iCell]->set(iCell, false, !_coreMask[iCell]);
343 }
344 collisionO.set(iCell, true, !_coreMask[iCell]);
345 _operatorOfCells[iCell] = &collisionO;
346 if (Dynamics<T,DESCRIPTOR>* dynamics = collisionO.getDynamics()) {
347 _dynamicsOfCells[iCell] = dynamics;
348 }
349 _dominantCollisionO = nullptr;
350 }
351
353
362 {
363 switch (strategy) {
365 if (!_dominantCollisionO) {
366 _dominantCollisionO = std::get<1>(std::max_element(
367 _map.begin(),
368 _map.end(),
369 [](const auto& lhs, const auto& rhs) -> bool {
370 return std::get<1>(lhs.second)->weight()
371 < std::get<1>(rhs.second)->weight();
372 })->second).get();
373 }
374 _dominantCollisionO->apply(_lattice, _coreMask, strategy);
375 break;
376
378 for (auto& [id, value] : _map) {
379 auto& [promise, collisionO] = value;
380 if (collisionO->weight() > 0) {
381 collisionO->apply(_lattice, _coreMask, strategy);
382 }
383 }
384 break;
385
386 default:
387 throw std::runtime_error("Invalid collision dispatch strategy");
388 break;
389 }
390 }
391
393
396 std::size_t getWeight(const DynamicsPromise<T,DESCRIPTOR>& promise) const {
397 auto iter = _map.find(promise.id());
398 if (iter != _map.end()) {
399 const auto& [_, collisionO] = std::get<1>(*iter);
400 return collisionO->weight();
401 } else {
402 return 0;
403 }
404 }
405
407 std::set<DynamicsPromise<T,DESCRIPTOR>> getAll() const {
408 std::set<DynamicsPromise<T,DESCRIPTOR>> dynamics;
409 for (auto& [id, value] : _map) {
410 const auto& [promise, collisionO] = value;
411 dynamics.emplace(promise);
412 }
413 return dynamics;
414 }
415
416};
417
418
419}
420
421#endif
Map between cell indices and concrete dynamics.
void set(std::size_t iCell, DynamicsPromise< T, DESCRIPTOR > &&promise)
Assigns promised dynamics to cell index and updates block masks.
std::size_t getWeight(const DynamicsPromise< T, DESCRIPTOR > &promise) const
Returns number of cells assigned to promised dynamics.
void collide(CollisionDispatchStrategy strategy)
Executes local collision step for entire non-overlap area of lattice.
const Dynamics< T, DESCRIPTOR > * get(std::size_t iCell) const
std::set< DynamicsPromise< T, DESCRIPTOR > > getAll() const
Returns set of all dynamics maintained this map.
Dynamics< T, DESCRIPTOR > * get(DynamicsPromise< T, DESCRIPTOR > &&promise)
Returns a pointer to dynamics matching the promise.
BlockDynamicsMap(ConcreteBlockLattice< T, DESCRIPTOR, PLATFORM > &lattice)
Constructor for a BlockDynamicsMap.
Dynamics< T, DESCRIPTOR > * get(std::size_t iCell)
std::size_t getNcells() const
Get number of cells.
void forCoreSpatialLocations(F f) const
CellID getCellId(LatticeR< D > latticeR) const
Get 1D cell ID.
Collision operation of concrete DYNAMICS on concrete block lattices of PLATFORM.
Definition operator.h:104
Implementation of BlockLattice on a concrete PLATFORM.
Factory for instances of a specific Dynamics type.
std::function< std::optional< bool >()> _inspectOptimizability
bool hasOptimizedVersion() const
Returns true if a CSE version is available.
const std::string _name
std::function< Dynamics< T, DESCRIPTOR > *()> _makeDynamics
std::function< std::optional< std::size_t >()> _inspectArithmeticOperationCount
BlockCollisionO< T, DESCRIPTOR, PLATFORM > * realize()
Returns new instance of the collision operator for promised DYNAMICS.
std::string name() const
Returns name of promised DYNAMICS.
std::function< std::set< FieldTypePromise< T, DESCRIPTOR > >()> _accessedFields
Returns the set of all accessed fields.
std::optional< bool > isOptimizable() const
Returns whether a CSE version can be generated (if this can be determined)
std::function< std::optional< std::size_t >()> _inspectComplexity
std::set< FieldTypePromise< T, DESCRIPTOR > > accessedFields() const
Returns set of accessed fields.
std::optional< std::size_t > getComplexity() const
Returns size of expanded expression tree (only for internal CSE generation use)
Dynamics< T, DESCRIPTOR > * realize()
Returns new instance of the promised DYNAMICS.
const bool _isOptimizationAvailable
const std::type_index _id
std::optional< std::size_t > getArithmeticOperationCount() const
Returns FLOPs per collision (if this can be determined)
std::optional< std::size_t > getMemoryBandwidth() const
Returns memory bandwidth per collision (if this can be determined)
std::function< std::optional< std::size_t >()> _inspectMemoryBandwidth
bool operator<(const DynamicsPromise< T, DESCRIPTOR > &rhs) const
std::function< AbstractCollisionO< T, DESCRIPTOR > *(Platform)> _makeOperator
std::type_index id() const
Returns type index of the promised DYNAMICS.
DynamicsPromise(meta::id< DYNAMICS > id=meta::id< DYNAMICS >{})
Helper for conveying the ability for operations on FIELD_TYPE.
Definition data.h:95
Plain old scalar vector.
std::string name()
Returns name of FIELD for human consumption.
Definition fields.h:49
std::optional< std::size_t > getArithmeticOperationCount()
std::optional< std::size_t > getMemoryBandwidthOfCollision()
Returns estimate of number of scalar reads and writes for DYNAMICS::collide.
std::optional< std::size_t > getComplexity()
std::set< FieldTypePromise< T, DESCRIPTOR > > getFieldsAccessedByDynamics()
Top level namespace for all of OpenLB.
Platform
OpenLB execution targets.
Definition platform.h:35
CollisionDispatchStrategy
Collision dispatch strategy.
Definition operator.h:81
@ Individual
Apply all dynamics individually (async for Platform::GPU_CUDA)
@ Dominant
Apply dominant dynamics using mask and fallback to virtual dispatch for others.
DynamicsPromise(meta::id< DYNAMICS >) -> DynamicsPromise< typename DYNAMICS::value_t, typename DYNAMICS::descriptor_t >
CONCRETIZABLE::base_t * constructUsingConcretePlatform(Platform platform, ARGS &&... args)
Definition dispatch.h:107
Base of collision operations performed by BlockDynamicsMap.
Definition operator.h:62
Collision operation on concrete blocks of PLATFORM.
Definition operator.h:90
virtual void apply(ConcreteBlockLattice< T, DESCRIPTOR, PLATFORM > &block, ConcreteBlockMask< T, PLATFORM > &subdomain, CollisionDispatchStrategy strategy)=0
Apply collision on subdomain of block using strategy.
Mask describing the subdomain on which to apply the collision step.
Interface for per-cell dynamics.
Definition interface.h:56
Identity type to pass non-constructible types as value.
Definition meta.h:79