OpenLB 1.7
Loading...
Searching...
No Matches
data.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 CORE_DATA_H
25#define CORE_DATA_H
26
27#include "platform/platform.h"
28
30
31#include <optional>
32
33namespace olb {
34
35template <typename T, Platform PLATFORM> class ConcreteBlockMask;
36
38
41template <typename FIELD>
42struct Array {
43 template <typename T, typename DESCRIPTOR, Platform PLATFORM>
45};
46
48
51template <typename OPERATOR>
53 template <typename T, typename DESCRIPTOR, Platform PLATFORM>
55};
56
58
61template <typename DYNAMICS>
63 template <typename T, typename DESCRIPTOR, Platform PLATFORM>
65};
66
68
71template<typename T, typename DESCRIPTOR, Platform PLATFORM>
73private:
74 std::unique_ptr<Serializable> _data;
75 std::function<void(ProcessingContext)> _setProcessingContext;
76
77public:
78 template<typename FIELD_TYPE, typename... ARGS>
80 _data(new typename FIELD_TYPE::template type<T,DESCRIPTOR,PLATFORM>(
81 std::forward<decltype(args)>(args)...)),
82 _setProcessingContext([&](ProcessingContext context) {
83 static_cast<typename FIELD_TYPE::template type<T,DESCRIPTOR,PLATFORM>*>(_data.get())->setProcessingContext(context);
84 })
85 { }
86
87 template<typename FIELD_TYPE>
88 auto* as() {
89 return static_cast<typename FIELD_TYPE::template type<T,DESCRIPTOR,PLATFORM>*>(_data.get());
90 }
91
93 return _data.get();
94 }
95
96 template<typename TYPE>
97 std::optional<TYPE*> tryAs() {
98 if (TYPE* ptr = dynamic_cast<TYPE*>(_data.get())) {
99 return std::optional<TYPE*>(ptr);
100 } else {
101 return std::nullopt;
102 }
103 }
104
106 _setProcessingContext(context);
107 }
108
109};
110
112
115template<typename T, typename DESCRIPTOR, Platform PLATFORM>
117private:
119
120public:
122
125 template <typename FIELD_TYPE>
126 bool provides() const {
127 return _index.template provides<FIELD_TYPE>();
128 }
129
131
134 template <typename FIELD_TYPE>
136 return _index.template get<FIELD_TYPE>();
137 }
138
140 template <typename FIELD_TYPE>
142 _index.template set<FIELD_TYPE>(fieldType);
143 }
144
145};
146
147
149
154template<typename T, typename DESCRIPTOR, Platform PLATFORM>
155class Data final : public Serializable {
156private:
158 std::map<std::type_index, std::unique_ptr<AnyFieldType<T,DESCRIPTOR,PLATFORM>>> _map;
162 std::map<std::string, Serializable*> _serializationNames;
163
164public:
166 template <typename FIELD_TYPE>
167 bool provides() const {
168 return _registry.template provides<FIELD_TYPE>();
169 }
170
172
175 template <typename FIELD_TYPE, typename... ARGS>
176 auto& allocate(ARGS&&... args) {
177 _map[typeid(FIELD_TYPE)] = std::make_unique<AnyFieldType<T,DESCRIPTOR,PLATFORM>>(
178 meta::id<FIELD_TYPE>(), std::forward<decltype(args)>(args)...);
179
180 AnyFieldType<T,DESCRIPTOR,PLATFORM>* newField = _map[typeid(FIELD_TYPE)].get();
181 _registry.template track<FIELD_TYPE>(newField);
182 return *newField->template as<FIELD_TYPE>();
183 }
184
186
191 template <typename FIELD_TYPE>
192 void setSerialization(bool active) {
193 if (active) {
194 _serializationNames[meta::name<FIELD_TYPE>()] = _registry.template get<FIELD_TYPE>()->asSerializable();
195 } else {
196 _serializationNames.erase(meta::name<FIELD_TYPE>());
197 }
198 }
199
201 template <typename FIELD_TYPE>
202 auto& get() {
203 return *_registry.template get<FIELD_TYPE>()->template as<FIELD_TYPE>();
204 }
205
207
210 template <typename FIELD>
212 return get<Array<FIELD>>();
213 }
214
216 for (auto& [_, field] : _map) {
217 field->setProcessingContext(context);
218 }
219 }
220
222 template <typename F>
223 void forEach(F f) {
224 for (auto& [_, field] : _map) {
225 f(*field);
226 }
227 }
228
230
233 template <typename TYPE, typename F>
234 void forEachCastable(F f) {
236 if (auto casted = any.template tryAs<TYPE>()) {
237 f(*casted);
238 }
239 });
240 }
241
243 auto& getRegistry() {
244 return _registry;
245 }
246
248 std::size_t getNblock() const override;
250 std::size_t getSerializableSize() const override;
252 bool* getBlock(std::size_t iBlock, std::size_t& sizeBlock, bool loadingMode) override;
253
254};
255
256template<typename T, typename DESCRIPTOR, Platform PLATFORM>
258{
259 std::size_t nBlock = 0;
260 for (const auto& [_, serializable] : _serializationNames) {
261 nBlock += serializable->getNblock();
262 }
263 return nBlock;
264}
265
266template<typename T, typename DESCRIPTOR, Platform PLATFORM>
268{
269 std::size_t size = 0;
270 for (const auto& [_, serializable] : _serializationNames) {
271 size += serializable->getSerializableSize();
272 }
273 return size;
274}
275
276template<typename T, typename DESCRIPTOR, Platform PLATFORM>
277bool* Data<T,DESCRIPTOR,PLATFORM>::getBlock(std::size_t iBlock, std::size_t& sizeBlock, bool loadingMode)
278{
279 std::size_t currentBlock = 0;
280 bool* dataPtr = nullptr;
281
282 for (const auto& [_, serializable] : _serializationNames) {
283 registerSerializableOfConstSize(iBlock, sizeBlock, currentBlock, dataPtr, *serializable, loadingMode);
284 }
285
286 return dataPtr;
287}
288
289}
290
291#endif
Helper for referring to arbitrary data instances.
Definition data.h:72
Serializable * asSerializable()
Definition data.h:92
AnyFieldType(meta::id< FIELD_TYPE >, ARGS &&... args)
Definition data.h:79
auto * as()
Definition data.h:88
void setProcessingContext(ProcessingContext context)
Definition data.h:105
std::optional< TYPE * > tryAs()
Definition data.h:97
Storage of any FIELD_TYPE data on PLATFORM.
Definition data.h:155
std::size_t getNblock() const override
Number of data blocks for the serializable interface.
Definition data.h:257
void setProcessingContext(ProcessingContext context)
Definition data.h:215
auto & get()
Return reference to data of FIELD_TYPE.
Definition data.h:202
void forEach(F f)
Call f for each managed AnyFieldType.
Definition data.h:223
auto & allocate(ARGS &&... args)
Allocate and return FIELD_TYPE data.
Definition data.h:176
FieldArrayD< T, DESCRIPTOR, PLATFORM, FIELD > & getFieldArray()
Return reference to data of Array<FIELD>, i.e. FieldArrayD<FIELD>
Definition data.h:211
void forEachCastable(F f)
Call f for each managed AnyFieldType of TYPE.
Definition data.h:234
bool * getBlock(std::size_t iBlock, std::size_t &sizeBlock, bool loadingMode) override
Return a pointer to the memory of the current block and its size for the serializable interface.
Definition data.h:277
std::size_t getSerializableSize() const override
Binary size for the serializer.
Definition data.h:267
void setSerialization(bool active)
Sets FIELD_TYPE serialization state to active.
Definition data.h:192
bool provides() const
Returns true iff FIELD_TYPE is allocated and can be accessed.
Definition data.h:167
auto & getRegistry()
Expose FieldTypeRegistry for device-support.
Definition data.h:243
SoA storage for instances of a single FIELD.
Efficient indexing of dynamically allocated data fields.
Definition data.h:116
AnyFieldType< T, DESCRIPTOR, PLATFORM > * get()
Return pointer to FIELD_TYPE data.
Definition data.h:135
void track(AnyFieldType< T, DESCRIPTOR, PLATFORM > *fieldType)
Track newly allocated FIELD_TYPE.
Definition data.h:141
bool provides() const
Returns true iff FIELD_TYPE is registered.
Definition data.h:126
Base class for serializable objects of constant size. For dynamic size use BufferSerializable.
Definition serializer.h:145
(Time) efficient mapping between TYPEs and VALUEs
Top level namespace for all of OpenLB.
ProcessingContext
OpenLB processing contexts.
Definition platform.h:55
Describe FieldArray of a FIELD in Data.
Definition data.h:42
Concrete storage of ParametersD in olb::Data.
Describe mask of DYNAMICS in Data.
Definition data.h:62
Describe paramaters of OPERATOR in Data.
Definition data.h:52
Identity type to pass non-constructible types as value.
Definition meta.h:79