MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
dgcartesianhelementcollector.h
Go to the documentation of this file.
1// Copyright (C) 2024 The m-AIA AUTHORS
2//
3// This file is part of m-AIA (https://git.rwth-aachen.de/aia/m-AIA/m-AIA)
4//
5// SPDX-License-Identifier: LGPL-3.0-only
6
7#ifndef DGHELEMENTCOLLECTOR_H_
8#define DGHELEMENTCOLLECTOR_H_
9
10
11// The following macro enables the "Structure-of-Arrays" memory layout for multi-dimensional node
12// variables. This might be beneficial for GPU computations. Default is "Array-of-Structures".
13// Examples (for nodes nN with four children cM each)
14// Array-of-Structures (AOS): n0c0, n0c1, n0c2, n0c3, n1c0, n1c1, n1c2, n1c3, n2c0, n2c1, ...
15// Structure-of-Arrays (SOA): n0c0, n1c0, n2c0, n3c0, ..., n0c1, n1c1, n2c1, n3c1, ..., n0c2, ...
16// #define DGCOLLECTOR_SOA_MEMORY_LAYOUT
17
18// The macro 'DGCOLLECTOR_SANITY_CHECKS_ACCESSORS' enables (potentially very expensive) sanity
19// checks
20// for all accessors. It is enabled for build type "extra_debug".
21#ifdef MAIA_EXTRA_DEBUG
22#define DGCOLLECTOR_SANITY_CHECKS_ACCESSORS
23#endif
24
25// Sanity-checking macros for accessors
26#if defined(DGCOLLECTOR_SANITY_CHECKS_ACCESSORS) || defined(MAIA_ASSERT_ACCESSORS)
27#define ENSURE_VALID_ID_ACCESSOR(id) \
28 do { \
29 MAIA_CONTAINER_ENSURE_VALID_ID(id); \
30 } while(false)
31#define ENSURE_VALID_SURFACE_DIR_ACCESSOR(dir) \
32 do { \
33 MAIA_CONTAINER_ENSURE( \
34 dir >= 0 && dir < noDirs(), \
35 "surface direction " + std::to_string(dir) + " out-of-bounds [0, " + std::to_string(noDirs()) + ")", AT_); \
36 } while(false)
37#define ENSURE_VALID_SURFACE_POS_ACCESSOR(pos) \
38 do { \
39 MAIA_CONTAINER_ENSURE(pos >= 0 && pos < noHrefSurfaces(), \
40 "surface position out-of-bounds [0, " + std::to_string(noHrefSurfaces()) + ")", AT_); \
41 } while(false)
42#else
43#define ENSURE_VALID_ID_ACCESSOR(id) \
44 do { \
45 } while(false)
46#define ENSURE_VALID_SURFACE_DIR_ACCESSOR(dir) \
47 do { \
48 } while(false)
49#define ENSURE_VALID_SURFACE_POS_ACCESSOR(pos) \
50 do { \
51 } while(false)
52#endif
53
54
56namespace maia {
57namespace dg {
58namespace collector {
59
61template <MInt nDim, class SysEqn>
62class HElementCollector : public maia::container::Container<HElementCollector<nDim, SysEqn>, Invalid> {
63 // Necessary for CRTP
64 friend class maia::container::Container<HElementCollector<nDim, SysEqn>, Invalid>;
65
66 // Make base class functions known to use without this pointer
69 template <class T>
70 using Storage = typename Base::template Storage<T>;
71
72
73 public:
74 // Types
75 template <class T>
77
78 // Constructor
80 constexpr HElementCollector() = default;
81
82 // Ensure that base class method is found when called from outside
83 using Base::copyData;
85 using Base::reset;
86
87 // Accessors
88 MInt& elementId(const MInt id);
89 MInt elementId(const MInt id) const;
90 MInt& hrefSurfaceIds(const MInt id, const MInt dir, const MInt pos);
91 MInt hrefSurfaceIds(const MInt id, const MInt dir, const MInt pos) const;
92
94 static constexpr MInt noHrefSurfaces() { return 2 * (nDim - 1); }
95
97 static constexpr MInt noDirs() { return 2 * nDim; }
98
99 // Return number of h-refined surface ids
100 static constexpr MInt noHrefSurfaceIds() { return noHrefSurfaces() * noDirs(); }
101
102 private:
103 // Methods required by base class for CRTP
104 void reset();
105 void invalidate(const MInt begin, const MInt end);
106 template <class Functor, class T>
107 void rawCopyGeneric(Functor&& c, const T& source, const MInt begin, const MInt end, const MInt destination);
108
109 // Data containers
112};
113
114
116template <MInt nDim, class SysEqn>
118 resetStorage(1, m_elementId);
119 resetStorage(noHrefSurfaceIds(), m_hrefSurfaceIds);
120}
121
122
124template <MInt nDim, class SysEqn>
126// Prevent accidental compilation without support for SoA layout
127#ifdef DGCOLLECTOR_SOA_MEMORY_LAYOUT
128#error Missing implementation for structure-of-arrays memory layout.
129#endif
130 ENSURE_VALID_ID_ACCESSOR(id);
131 return m_elementId[id];
132}
134template <MInt nDim, class SysEqn>
136// Prevent accidental compilation without support for SoA layout
137#ifdef DGCOLLECTOR_SOA_MEMORY_LAYOUT
138#error Missing implementation for structure-of-arrays memory layout.
139#endif
140 ENSURE_VALID_ID_ACCESSOR(id);
141 return m_elementId[id];
142}
143
144
146template <MInt nDim, class SysEqn>
148// Prevent accidental compilation without support for SoA layout
149#ifdef DGCOLLECTOR_SOA_MEMORY_LAYOUT
150#error Missing implementation for structure-of-arrays memory layout.
151#endif
152 ENSURE_VALID_ID_ACCESSOR(id);
153 ENSURE_VALID_SURFACE_DIR_ACCESSOR(dir);
154 ENSURE_VALID_SURFACE_POS_ACCESSOR(pos);
155 return m_hrefSurfaceIds[id * noHrefSurfaceIds() + dir * noHrefSurfaces() + pos];
156}
158template <MInt nDim, class SysEqn>
160// Prevent accidental compilation without support for SoA layout
161#ifdef DGCOLLECTOR_SOA_MEMORY_LAYOUT
162#error Missing implementation for structure-of-arrays memory layout.
163#endif
164 ENSURE_VALID_ID_ACCESSOR(id * noHrefSurfaceIds());
165 ENSURE_VALID_SURFACE_DIR_ACCESSOR(dir);
166 ENSURE_VALID_SURFACE_POS_ACCESSOR(pos);
167 return m_hrefSurfaceIds[id * noHrefSurfaceIds() + dir * noHrefSurfaces() + pos];
168}
169
170
172template <MInt nDim, class SysEqn>
174// Prevent accidental compilation without support for SoA layout
175#ifdef DGCOLLECTOR_SOA_MEMORY_LAYOUT
176#error Missing implementation for structure-of-arrays memory layout.
177#endif
178
179 // Element id
180 fill_invalid(m_elementId, begin, end);
181
182 // H-refined surface ids
183 fill_invalid(m_hrefSurfaceIds, begin, end, noHrefSurfaceIds());
184}
185
186
188template <MInt nDim, class SysEqn>
189template <class Functor, class T>
190void HElementCollector<nDim, SysEqn>::rawCopyGeneric(Functor&& c, const T& source, const MInt begin, const MInt end,
191 const MInt destination) {
192// Prevent accidental compilation without support for SoA layout
193#ifdef DGCOLLECTOR_SOA_MEMORY_LAYOUT
194#error Missing implementation for structure-of-arrays memory layout.
195#endif
196
197 // Element id
198 copyData(source.m_elementId, m_elementId, c, begin, end, destination);
199
200 // H-refined surface ids
201 copyData(source.m_hrefSurfaceIds, m_hrefSurfaceIds, c, begin, end, destination, noHrefSurfaceIds());
202}
203
204} // namespace collector
205} // namespace dg
206} // namespace maia
207
208
209// Undefine macros that should not be used outside this file
210#undef DGCOLLECTOR_SANITY_CHECKS_ACCESSORS
211#undef ENSURE_VALID_ID_ACCESSOR
212#undef ENSURE_VALID_SURFACE_DIR_ACCESSOR
213#undef ENSURE_VALID_SURFACE_POS_ACCESSOR
214
215#endif // ifndef DGHELEMENTCOLLECTOR_H_
void copyData(const Container_ &source, Container_ &target, Functor &&f, const MInt begin, const MInt end, const MInt dest, const MInt solverSize=1)
Copy [begin, end) range with given solver size from source to dest position of target.
Definition: container.h:138
void fill_invalid(Container_ &c, const MInt begin, const MInt end, const MInt solverSize=1, const T value=Invalid< T >::value())
Definition: container.h:131
void resetStorage(const MInt n, Storage< T > &c)
Create new container with given size and replace original one.
Definition: container.h:420
void reset(const MInt capacity)
Reset tree, re-create data structures with given capacity, and set size to zero.
Definition: container.h:187
Class that represents DG element collector.
void reset()
Reset HElementCollector, re-create data structures.
static constexpr MInt noHrefSurfaces()
Return number of h-refined surfaces.
constexpr HElementCollector()=default
Default c'tor does nothing.
MInt & hrefSurfaceIds(const MInt id, const MInt dir, const MInt pos)
Accessor for h-refined surface ids.
void invalidate(const MInt begin, const MInt end)
Erase range of nodes such that they contain no sensible values anymore.
static constexpr MInt noDirs()
Return number of directions.
MInt & elementId(const MInt id)
Accessor for element id.
void rawCopyGeneric(Functor &&c, const T &source, const MInt begin, const MInt end, const MInt destination)
Helper function for rawCopy(). Destination may refer to beginning or end of target range.
typename maia::dg::collector::Invalid< T > Invalid
typename Base::template Storage< T > Storage
int32_t MInt
Definition: maiatypes.h:62
MInt id
Definition: maiatypes.h:71
Namespace for auxiliary functions/classes.