MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
collector.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 COLLECTOR_H
8#define COLLECTOR_H
9
10#include "INCLUDE/maiatypes.h"
11#include "UTIL/functions.h"
12
13template <typename T>
14class Collector {
15 public:
16 // Note: The "dummy" variables here serve ABSOLUTELY NO OTHER PURPOSE than to differentiate the different constructors
17 // (i.e. give them unique signatures so proper overloading can take place)
18 Collector(MLong maxSize, MInt dimension, MFloat dummy, MInt maxNoSets); // G cells
19 Collector(MLong maxSize, MInt dimension, MInt distributions, MInt distributions1);
20 Collector(MLong maxSize, MInt dimension, MInt distributions, MInt distributions1, MInt maxNoSurfaces, MInt dummy1);
21 Collector(MLong maxSize, MInt dimension, MInt distributions, MInt dummy, MInt dummy2);
22 Collector(MLong maxSize, MInt dimension, MInt distributions);
26 delete[] a;
27 delete[] m_rawMemory;
28 };
29 MInt size() { return (MInt)m_size; };
30
31 MInt maxSize() { return (MInt)m_maxSize; };
32
33 void append();
34 MLong memoryUseage() { return (MLong)(m_usedMemory + (m_maxSize + 1) * sizeof(T)); };
35
36 T* operator[](MInt index);
37 T* a;
39
40 MInt setSize(MInt inputSize) {
41 if(inputSize < m_size) m_size = inputSize;
42 return (MInt)inputSize;
43 }
44
45 MInt resetSize(MInt inputSize) {
46 m_size = inputSize;
47 return (MInt)inputSize;
48 }
49
51
52 private:
55 char* m_rawMemory = nullptr;
59 void init(const MLong maxSize_) {
60 m_size = 0;
62 m_maxSize = maxSize_;
63 }
64
66 void allocMemory() {
67 m_staticElementSize = T::staticElementSize();
68 // An extra element is allocated in m_rawMemory to ensure that all element
69 // variables are inside memory even when the all need to be realigned.
71 a = new T[m_maxSize + 1];
72 if(m_usedMemory > 0) {
73 m_rawMemory = new char[m_usedMemory];
74 } else {
75 m_rawMemory = nullptr;
76 }
77 }
81 for(MInt i = 0; i < m_maxSize + 1; i++) {
82 a[i].allocateElements((void*)(m_rawMemory + m_rawMemoryCounter), (void*)m_rawMemory, i);
84 }
85 }
86};
87
88// TODO labels:toenhance make collector constructors a single variadic template:
89// template<class T, class.. Us>
90// Collector<T>::Collector(MLong maxSize, Us... us) {
91// init(maxSize);
92// T::init(maxSize + 1, std::forward<Us>(us)...); // Why maxSize + 1 ? (dummy cell?)
93// allocMemoryAndInitElements();
94// }
95
96template <typename T>
98 init(inputMaxSize);
99 allocMemory();
100 for(MInt i = 0; i < (inputMaxSize + 1); i++) {
101 a[i].allocateElements((void*)(m_rawMemory + m_rawMemoryCounter));
102 m_rawMemoryCounter += m_staticElementSize;
103 }
104}
105
112template <typename T>
113Collector<T>::Collector(MLong inputMaxSize, MInt dimension, MInt dummy) {
114 init(inputMaxSize);
115 T::init(dimension, dummy, inputMaxSize);
116 allocMemoryAndInitElements();
117}
118
119// Used for:
120// - DG: m_surfaces
121template <typename T>
122Collector<T>::Collector(MLong inputMaxSize, MInt dimension, MInt distributions, MInt distributions1) {
123 init(inputMaxSize);
124 T::init(dimension, distributions, distributions1, inputMaxSize + 1);
125 allocMemoryAndInitElements();
126}
127
128// detailed chemistry
129template <typename T>
130Collector<T>::Collector(MLong inputMaxSize, MInt dimension, MInt distributions, MInt distributions1, MInt dummy2) {
131 init(inputMaxSize);
132 T::init(dimension, distributions, distributions1, inputMaxSize + 1, dummy2);
133 allocMemoryAndInitElements();
134}
135
136
137template <typename T>
138Collector<T>::Collector(MLong inputMaxSize, MInt dimension, MInt distributions, MInt distributions1, MInt maxNoSurfaces,
139 MInt /*dummy1*/) {
140 init(inputMaxSize);
141 T::init(dimension, distributions, distributions1, inputMaxSize + 1, maxNoSurfaces);
142 allocMemoryAndInitElements();
143}
144
145template <typename T>
146Collector<T>::Collector(MLong inputMaxSize, MInt dimension, MFloat flameSpeed, MInt maxNoSets) {
147 init(inputMaxSize);
148 T::init(dimension, flameSpeed, inputMaxSize, maxNoSets);
149 allocMemoryAndInitElements();
150}
151
152template <typename T>
154 if(m_size < m_maxSize) {
155 m_size++;
156 } else {
157 std::stringstream errorMessage;
158 errorMessage << " Error in collector, maxSize reached ( " << m_maxSize << " elements ).";
159 mTerm(1, AT_, errorMessage.str());
160 }
161}
162
163template <typename T>
165 return m_rawMemory;
166}
167
168
169template <typename T>
171 return &a[index];
172}
173
174
175namespace maia {
176
178namespace collector_memory {
179
186template <class T, class U>
187inline U* align(U* p) {
188 const MLong N = sizeof(T);
189 const MLong padding = reinterpret_cast<MLong>(static_cast<void*>(p)) % N;
190 if(padding == 0) {
191 return p;
192 } else {
193 return reinterpret_cast<U*>(reinterpret_cast<char*>(p) + padding);
194 }
195}
196
209template <class T>
210inline void rowMajor1D(T*& p, void*& base, const MInt cellId, const MInt Nrows, const MInt maxNoCells) {
211 base = align<T>(base);
212 p = reinterpret_cast<T*>(base) + static_cast<MLong>(1) * Nrows * cellId;
213 base = reinterpret_cast<void*>(reinterpret_cast<T*>(base) + static_cast<MLong>(1) * Nrows * maxNoCells);
214}
215
237template <class T>
238ATTRIBUTES1(ATTRIBUTE_NO_AUTOVEC)
239inline void rowMajor2D(T**& p, void*& base, const MInt cellId, const MInt Nrows, const MInt Ncols,
241 // pointers are stored first in row major order:
243 // the data itself is stored after the pointers:
244 base = align<T>(base);
245 for(MInt i = 0; i < Nrows; ++i) {
246 p[i] =
247 reinterpret_cast<T*>(base) + static_cast<MLong>(1) * Nrows * Ncols * cellId + static_cast<MLong>(1) * i * Ncols;
248 }
249 base = reinterpret_cast<T*>(base) + static_cast<MLong>(1) * Nrows * Ncols * maxNoCells;
250}
251
257template <class T>
258inline void copyElements1D(T* to, T* from, const MInt Nrows) {
259 for(MInt i = 0; i < Nrows; ++i) {
260 to[i] = from[i];
261 }
262}
263
272template <class T>
273inline void copyElements2D(T* to, T* from, const MInt Nrows, const MInt Ncols) {
274 for(MInt i = 0; i < Nrows; ++i) {
275 for(MInt j = 0; j < Ncols; ++j) {
276 to[i][j] = from[i][j];
277 }
278 }
279}
280
284namespace unaligned_cell_wise {
306template <class T>
307inline void rowMajor1D(T*& p, void*& base, const MInt Nrows) {
308 // rowMajor1D(p,base,0,Nrows,1); // DO NOT ALIGN -> requires too much memory
309 p = reinterpret_cast<T*>(base);
310 base = reinterpret_cast<void*>(reinterpret_cast<T*>(base) + Nrows);
311}
312
313template <class T>
314ATTRIBUTES1(ATTRIBUTE_NO_AUTOVEC)
315inline void rowMajor2D(T**& p, void*& base, const MInt Nrows, const MInt Ncols) {
316 // rowMajor2D(p,base,0,Nrows,Ncols,1); // DO NOT ALIGN -> requires too much memory
317 // pointers are stored first in row major order:
318 rowMajor1D(p, base, Nrows);
319 // the data itself is stored after the pointers:
320 for(MInt i = 0; i < Nrows; ++i) {
321 p[i] = reinterpret_cast<T*>(base) + static_cast<MLong>(1) * i * Ncols;
322 }
323 base = reinterpret_cast<T*>(base) + static_cast<MLong>(1) * Nrows * Ncols;
324}
325
326} // namespace unaligned_cell_wise
327
328} // namespace collector_memory
329
330} // namespace maia
331#endif
MLong memoryUseage()
Definition: collector.h:34
T * operator[](MInt index)
Definition: collector.h:170
char * getRawPointer()
Definition: collector.h:164
Collector(MLong maxSize, MInt dimension, MInt distributions)
Definition: collector.h:113
MInt resetSize(MInt inputSize)
Definition: collector.h:45
MInt maxSize()
Definition: collector.h:31
void allocMemoryAndInitElements()
Allocates collector memory and initializes elements.
Definition: collector.h:79
Collector(MLong maxSize=1052000)
Definition: collector.h:97
MInt setSize(MInt inputSize)
Definition: collector.h:40
void init(const MLong maxSize_)
Definition: collector.h:59
Collector(MLong maxSize, MInt dimension, MInt distributions, MInt dummy, MInt dummy2)
Definition: collector.h:130
MInt m_distribution
Definition: collector.h:57
void allocMemory()
Allocates collector memory.
Definition: collector.h:66
MLong m_maxSize
Definition: collector.h:53
Collector(MLong maxSize, MInt dimension, MFloat dummy, MInt maxNoSets)
Definition: collector.h:146
Collector(MLong maxSize, MInt dimension)
char * m_rawMemory
Definition: collector.h:55
Collector(MLong maxSize, MInt dimension, MInt distributions, MInt distributions1, MInt maxNoSurfaces, MInt dummy1)
Definition: collector.h:138
void append()
Definition: collector.h:153
Collector(MLong maxSize, MInt dimension, MInt distributions, MInt distributions1)
Definition: collector.h:122
MInt m_size
Definition: collector.h:54
~Collector()
Definition: collector.h:25
MInt size()
Definition: collector.h:29
MLong m_usedMemory
Definition: collector.h:56
MInt m_staticElementSize
Definition: collector.h:58
MLong m_rawMemoryCounter
Definition: collector.h:38
void mTerm(const MInt errorCode, const MString &location, const MString &message)
Definition: functions.cpp:29
int32_t MInt
Definition: maiatypes.h:62
double MFloat
Definition: maiatypes.h:52
int64_t MLong
Definition: maiatypes.h:64
void rowMajor1D(T *&p, void *&base, const MInt Nrows)
Store 1D variables in row-majow order relative to the start of each cell's memory solver.
Definition: collector.h:307
void const MInt const MInt Ncols
Definition: collector.h:315
ATTRIBUTES1(ATTRIBUTE_NO_AUTOVEC) inline void rowMajor2D(T **&p
void const MInt const MInt const MInt const MInt maxNoCells
Definition: collector.h:240
void copyElements1D(T *to, T *from, const MInt Nrows)
Copies 1D cell elements.
Definition: collector.h:258
ATTRIBUTES1(ATTRIBUTE_NO_AUTOVEC) inline void rowMajor2D(T **&p
Stores 2D variables in row-major order.
void rowMajor1D(T *&p, void *&base, const MInt cellId, const MInt Nrows, const MInt maxNoCells)
Stores 1D variables in row-major order.
Definition: collector.h:210
void copyElements2D(T *to, T *from, const MInt Nrows, const MInt Ncols)
Copies 2D cell elements.
Definition: collector.h:273
void const MInt cellId
Definition: collector.h:239
U * align(U *p)
Aligns pointer p such that a T stored at its adress is aligned.
Definition: collector.h:187
void const MInt const MInt const MInt Ncols
Definition: collector.h:239
void const MInt const MInt Nrows
Definition: collector.h:239
Namespace for auxiliary functions/classes.
Definition: contexttypes.h:19