24#ifndef BLOCK_DYNAMICS_MAP_H
25#define BLOCK_DYNAMICS_MAP_H
31#include <unordered_set>
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;
48template <
typename T,
typename DESCRIPTOR,
typename DYNAMICS>
55template <Platform PLATFORM>
72template <
typename T,
typename DESCRIPTOR>
75 const std::type_index
_id;
92 template <
typename DYNAMICS>
94 _id(typeid(DYNAMICS)),
96 return new DYNAMICS();
99 if constexpr (concepts::IntrospectableDynamics<DYNAMICS>) {
100 if constexpr (dynamics::is_cse_optimized<DYNAMICS>::value) {
111 if constexpr (!std::is_same_v<T,Expr>) {
112 if constexpr (std::is_same_v<NoDynamics<T,DESCRIPTOR>, DYNAMICS>) {
118 throw std::domain_error(
"Can not introspect the introspection");
123#ifdef FEATURE_INSPECT_DYNAMICS
124 if constexpr (!std::is_same_v<T,Expr>) {
125 if constexpr (dynamics::is_cse_optimized<DYNAMICS>::value) {
131 throw std::domain_error(
"Can not introspect the introspection - we, sadly, are not in LISP land.");
137#ifdef FEATURE_INSPECT_DYNAMICS
138 if constexpr (!std::is_same_v<T,Expr>) {
139 if constexpr (dynamics::is_cse_optimized<DYNAMICS>::value) {
145 throw std::domain_error(
"Can not introspect the introspection");
151#ifdef FEATURE_INSPECT_DYNAMICS
152 if constexpr (!std::is_same_v<T,Expr>) {
162#ifdef FEATURE_INSPECT_DYNAMICS
163 if constexpr (!std::is_same_v<T,Expr>) {
164 if constexpr (dynamics::is_cse_optimized<DYNAMICS>::value) {
170 throw std::domain_error(
"Can not introspect the introspection - we, sadly, are not in LISP land.");
178 std::type_index
id()
const {
210 return *bandwidth *
sizeof(T);
222 return id() < rhs.
id();
231 template <Platform PLATFORM>
239template <
typename DYNAMICS>
241 typename DYNAMICS::descriptor_t>;
249 template <
typename T,
typename DESCRIPTOR, Platform PLATFORM>
264template<
typename T,
typename DESCRIPTOR, Platform PLATFORM>
269 std::map<std::type_index,
270 std::tuple<DynamicsPromise<T,DESCRIPTOR>,
271 std::unique_ptr<BlockCollisionO<T,DESCRIPTOR,PLATFORM>>>> _map;
273 std::unique_ptr<Dynamics<T,DESCRIPTOR>*[]> _dynamicsOfCells;
274 std::unique_ptr<BlockCollisionO<T,DESCRIPTOR,PLATFORM>*[]> _operatorOfCells;
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;
292 auto accessedFields = promise.accessedFields();
294 field.ensureAvailabilityIn(_lattice);
299 auto& [_, op] = iter->second;
308 _dynamicsOfCells(new
Dynamics<T,DESCRIPTOR>* [_lattice.getNcells()] {
nullptr }),
310 _coreMask(lattice.template getData<CollisionSubdomainMask>()),
311 _dominantCollisionO(
nullptr)
314 _coreMask.set(_lattice.
getCellId(lattice),
true);
324 return resolve(std::forward<
decltype(promise)>(promise)).getDynamics();
329 return _dynamicsOfCells[iCell];
334 return _dynamicsOfCells[iCell];
340 auto& collisionO = resolve(std::forward<
decltype(promise)>(promise));
341 if (_operatorOfCells[iCell] !=
nullptr) {
342 _operatorOfCells[iCell]->set(iCell,
false, !_coreMask[iCell]);
344 collisionO.set(iCell,
true, !_coreMask[iCell]);
345 _operatorOfCells[iCell] = &collisionO;
347 _dynamicsOfCells[iCell] = dynamics;
349 _dominantCollisionO =
nullptr;
365 if (!_dominantCollisionO) {
366 _dominantCollisionO = std::get<1>(std::max_element(
369 [](
const auto& lhs,
const auto& rhs) ->
bool {
370 return std::get<1>(lhs.second)->weight()
371 < std::get<1>(rhs.second)->weight();
374 _dominantCollisionO->
apply(_lattice, _coreMask, strategy);
378 for (
auto& [
id, value] : _map) {
379 auto& [promise, collisionO] = value;
380 if (collisionO->weight() > 0) {
381 collisionO->apply(_lattice, _coreMask, strategy);
387 throw std::runtime_error(
"Invalid collision dispatch strategy");
397 auto iter = _map.find(promise.
id());
398 if (iter != _map.end()) {
399 const auto& [_, collisionO] = std::get<1>(*iter);
400 return collisionO->weight();
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);
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)
bool isIntrospectable() const
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.
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.
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.
std::string name()
Returns name of FIELD for human consumption.
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.
CollisionDispatchStrategy
Collision dispatch strategy.
@ 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)
Base of collision operations performed by BlockDynamicsMap.
Collision operation on concrete blocks of PLATFORM.
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.