OpenLB 1.7
Loading...
Searching...
No Matches
blockPostProcessorMap.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 BLOCK_POST_PROCESSOR_MAP_H
25#define BLOCK_POST_PROCESSOR_MAP_H
26
27#include <functional>
28#include <typeindex>
29
30#include "operator.h"
31#include "postProcessing.h"
32
33namespace olb {
34
35
37
47template <typename T, typename DESCRIPTOR>
49protected:
50 std::type_index _id;
54
55public:
56 template <typename POST_PROCESSOR>
58 _id(typeid(POST_PROCESSOR)),
59 _priority(POST_PROCESSOR().getPriority()),
60 _scope(POST_PROCESSOR::scope),
61 _constructor([](Platform platform) -> AbstractBlockO* {
62 switch (platform) {
63 #ifdef PLATFORM_CPU_SISD
66 #endif
67 #ifdef PLATFORM_CPU_SIMD
70 #endif
71 #ifdef PLATFORM_GPU_CUDA
74 #endif
75 default:
76 throw std::invalid_argument("Invalid PLATFORM");
77 }
78 })
79 { }
80
82 std::type_index id() const {
83 return _id;
84 }
85
86 int priority() const {
87 return _priority;
88 }
89
91 return _scope;
92 }
93
94 template <Platform PLATFORM>
96 return static_cast<BlockO<T,DESCRIPTOR,PLATFORM>*>(
97 _constructor(PLATFORM));
98 };
99
100};
101
102template <typename PP>
104 typename PP::descriptor_t>;
105
107template <typename T, typename DESCRIPTOR, Platform PLATFORM>
108class LegacyBlockPostProcessorO final : public BlockO<T,DESCRIPTOR,PLATFORM> {
109private:
111 std::vector<std::unique_ptr<PostProcessor<T,DESCRIPTOR>>> _postProcessors;
112
113public:
114 std::type_index id() const override
115 {
116 return typeid(LegacyBlockPostProcessorO);
117 }
118
119 void set(CellID iCell, bool state) override
120 {
121 throw std::logic_error("Invalid legacy post processor setter");
122 }
123
125
127 {
128 #ifdef PLATFORM_GPU_CUDA
129 if constexpr (PLATFORM == Platform::GPU_CUDA) {
130 if (!_postProcessors.empty()) {
131 throw std::runtime_error("Legacy post processors not supported on GPU_CUDA");
132 }
133 }
134 #else // CPU_* platform
135 #ifdef PARALLEL_MODE_OMP
136 #pragma omp parallel for schedule(dynamic)
137 #endif
138 for (std::size_t i=0; i < _postProcessors.size(); ++i) {
139 if constexpr (DESCRIPTOR::d == 3) {
140 _postProcessors[i]->processSubDomain(block, 0, block.getNx()-1, 0, block.getNy()-1, 0, block.getNz()-1);
141 } else {
142 _postProcessors[i]->processSubDomain(block, 0, block.getNx()-1, 0, block.getNy()-1);
143 }
144 }
145 #endif
146 }
147
148 void add(PostProcessor<T,DESCRIPTOR>* postProcessor)
149 {
150 _postProcessors.emplace_back(postProcessor);
151 }
152
153};
154
156
159template<typename T, typename DESCRIPTOR, Platform PLATFORM>
161private:
164
166
169 std::map<std::type_index,
170 std::unique_ptr<BlockO<T,DESCRIPTOR,PLATFORM>>> _map;
171
173
177
180 {
181 auto iter = _map.find(promise.id());
182 if (iter == _map.end()) {
183 iter = _map.emplace(std::piecewise_construct,
184 std::forward_as_tuple(promise.id()),
185 std::forward_as_tuple(promise.template realize<PLATFORM>())).first;
186 iter->second->setup(_lattice);
187 }
188 return *(iter->second);
189 }
190
191public:
195
197 {
198 _legacyPostProcessors.add(postProcessor);
199 }
200
202 void add(std::size_t iCell, PostProcessorPromise<T,DESCRIPTOR>&& promise)
203 {
204 resolve(std::forward<decltype(promise)>(promise)).set(iCell, true);
205 }
206
209 {
210 resolve(std::forward<decltype(promise)>(promise));
211 }
212
215 {
216 return _map.find(promise.id()) != _map.end();
217 }
218
220
231 void apply()
232 {
233 _legacyPostProcessors.apply(_lattice);
234
235 for (auto& [_, postProcessor] : _map) {
236 postProcessor->apply(_lattice);
237 }
238
239 #ifdef PLATFORM_GPU_CUDA
241 #endif
242 }
243
244};
245
246
247}
248
249#endif
Map of post processors of a single priority and stage.
void add(PostProcessorPromise< T, DESCRIPTOR > &&promise)
Add post processor to map, do nothing if it already exists.
BlockPostProcessorMap(ConcreteBlockLattice< T, DESCRIPTOR, PLATFORM > *lattice)
bool contains(PostProcessorPromise< T, DESCRIPTOR > &&promise) const
Returns true if map contains post processor.
void addLegacy(PostProcessor< T, DESCRIPTOR > *postProcessor)
void apply()
Apply all managed post processors to lattice.
void add(std::size_t iCell, PostProcessorPromise< T, DESCRIPTOR > &&promise)
Schedule post processor for application at iCell.
int getNy() const
Read only access to block height.
int getNx() const
Read only access to block width.
int getNz() const
Read only access to block height.
Implementation of BlockLattice on a concrete PLATFORM.
Block application of concrete OPERATOR called using SCOPE on PLATFORM.
Definition operator.h:65
Block operator for supporting legacy post processor in the new operator-centric framework.
void add(PostProcessor< T, DESCRIPTOR > *postProcessor)
void setup(ConcreteBlockLattice< T, DESCRIPTOR, PLATFORM > &block) override
Setup operator context.
void set(CellID iCell, bool state) override
Set whether iCell is covered by the operator (optional)
void apply(ConcreteBlockLattice< T, DESCRIPTOR, PLATFORM > &block) override
Apply operator on block.
std::type_index id() const override
Factory for instances of a specific POST_PROCESSOR type.
std::function< AbstractBlockO *(Platform)> _constructor
std::type_index id() const
Returns type index of the promised POST_PROCESSOR.
BlockO< T, DESCRIPTOR, PLATFORM > * realize()
PostProcessorPromise(meta::id< POST_PROCESSOR > id=meta::id< POST_PROCESSOR >{})
void synchronize()
Synchronize device.
Definition device.hh:64
Top level namespace for all of OpenLB.
std::uint32_t CellID
Type for sequential block-local cell indices.
Platform
OpenLB execution targets.
Definition platform.h:36
@ CPU_SIMD
Basic scalar CPU.
@ GPU_CUDA
Vector CPU (AVX2 / AVX-512 collision)
PostProcessorPromise(meta::id< PP >) -> PostProcessorPromise< typename PP::value_t, typename PP::descriptor_t >
std::conditional_t< DESCRIPTOR::d==2, PostProcessor2D< T, DESCRIPTOR >, PostProcessor3D< T, DESCRIPTOR > > PostProcessor
Definition aliases.h:67
OperatorScope
Block-wide operator application scopes.
Definition operator.h:54
Interface for post-processing steps – header file.
Base of any block operator.
Definition operator.h:33
Base of block-wide operators such as post processors.
Definition operator.h:41
virtual void setup(ConcreteBlockLattice< T, DESCRIPTOR, PLATFORM > &block)=0
Setup operator context.
Identity type to pass non-constructible types as value.
Definition meta.h:79