OpenLB 1.7
Loading...
Searching...
No Matches
registry.hh
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 GPU_CUDA_REGISTRY_HH
25#define GPU_CUDA_REGISTRY_HH
26
27#include "registry.h"
28
29namespace olb {
30
31namespace gpu {
32
33namespace cuda {
34
36
39template <typename CONTEXT, typename TYPE>
40__constant__ std::size_t field_type_index;
41
48
49}
50
51}
52
53template <typename T, typename DESCRIPTOR>
54struct FieldTypeRegistry<T,DESCRIPTOR,Platform::GPU_CUDA>::Data {
56 indexOnHost(2, nullptr)
57 { }
58
59 std::map<std::type_index,gpu::cuda::FieldArrayPointer> fieldArrayPointers;
60
61 std::vector<void**> indexOnHost;
63
64};
65
66template <typename T, typename DESCRIPTOR>
70
71template <typename T, typename DESCRIPTOR>
74
75template <typename T, typename DESCRIPTOR>
76template <typename FIELD_TYPE>
79{
80 _index.template set<FIELD_TYPE>(fieldType);
81
82 // Copy host-side field index to device-side field index
83 std::size_t index = _index.template index<FIELD_TYPE>();
84 cudaMemcpyToSymbol(gpu::cuda::field_type_index<FieldTypeRegistry,FIELD_TYPE>, &index, sizeof(std::size_t));
86 if (index >= _data->indexOnHost.size()) {
87 _data->indexOnHost.resize(2*index);
88 }
89
90 // Update device-side pointers to Array<FIELD> field types
91 using ConcreteFieldType = typename FIELD_TYPE::template type<T,DESCRIPTOR,Platform::GPU_CUDA>;
92 if constexpr (std::is_base_of_v<ColumnVectorBase, ConcreteFieldType>) {
93 using field_t = typename ConcreteFieldType::field_t;
94 auto& fieldArray = *fieldType->template as<FIELD_TYPE>();
95
96 std::array<void*,DESCRIPTOR::template size<field_t>()> componentPointers;
97 for (unsigned iD=0; iD < componentPointers.size(); ++iD) {
98 componentPointers[iD] = fieldArray[iD].deviceData();
99 }
100
101 auto componentPointersOnDevice = gpu::cuda::device::malloc<void*>(DESCRIPTOR::template size<field_t>());
102 gpu::cuda::device::copyToDevice(componentPointers.data(),
103 componentPointersOnDevice,
104 componentPointers.size()*sizeof(void*));
105
106 _data->indexOnHost[index] = componentPointersOnDevice;
107 _data->fieldArrayPointers.emplace(typeid(field_t), gpu::cuda::FieldArrayPointer{
108 gpu::cuda::device::unique_ptr<void*>(componentPointersOnDevice),
109 componentPointers.size(),
110 sizeof(typename field_t::template value_type<T>)
111 });
112 }
113
114 _modified = true;
115}
116
117template<typename T, typename DESCRIPTOR>
119{
120 if (_modified) {
121 _data->indexOnDevice = gpu::cuda::device::malloc<void**>(_data->indexOnHost.size());
122 gpu::cuda::device::copyToDevice(_data->indexOnHost.data(),
123 _data->indexOnDevice.get(),
124 _data->indexOnHost.size()*sizeof(void**));
125 _modified = false;
126 }
127 return _data->indexOnDevice.get();
128}
129
130template <typename T, typename DESCRIPTOR>
131template <typename FIELD>
134{
135 std::array<void*,DESCRIPTOR::template size<FIELD>()> componentPointers;
136 for (unsigned iD=0; iD < componentPointers.size(); ++iD) {
137 componentPointers[iD] = fieldArray[iD].deviceData();
138 }
139 auto& ptr = _data->fieldArrayPointers.at(typeid(FIELD));
140 gpu::cuda::device::copyToDevice(componentPointers.data(),
141 ptr.data.get(),
142 componentPointers.size()*sizeof(void*));
143}
144
145template <typename T, typename DESCRIPTOR>
148 std::type_index field)
149{
150 auto& ptr = _data->fieldArrayPointers.at(field);
152 ptr.data.get(),
153 ptr.column_count,
154 ptr.element_size
155 };
156}
157
158template <typename T, typename DESCRIPTOR>
159std::vector<gpu::cuda::AnyDeviceFieldArrayD>
161 const std::vector<std::type_index>& fields)
162{
163 std::vector<gpu::cuda::AnyDeviceFieldArrayD> deviceFields;
164 deviceFields.reserve(fields.size());
165 for (std::type_index field : fields) {
166 deviceFields.emplace_back(deviceFieldArray(field));
167 }
168 return deviceFields;
169}
170
171}
172
173#endif
Helper for referring to arbitrary data instances.
Definition data.h:72
Storage of any FIELD_TYPE data on PLATFORM.
Definition data.h:155
SoA storage for instances of a single FIELD.
Efficient indexing of dynamically allocated data fields.
Definition data.h:116
void track(AnyFieldType< T, DESCRIPTOR, PLATFORM > *fieldType)
Track newly allocated FIELD_TYPE.
Definition data.h:141
Managed pointer for device-side memory.
Definition device.h:79
void copyToDevice(void *src, void *dst, std::size_t count)
Copy data from host to device.
Definition device.hh:83
void check()
Check errors.
Definition device.hh:48
__constant__ std::size_t field_type_index
Mapping of TYPE in CONTEXT to runtime-fixed index.
Definition registry.hh:40
Top level namespace for all of OpenLB.
Platform
OpenLB execution targets.
Definition platform.h:36
@ GPU_CUDA
Vector CPU (AVX2 / AVX-512 collision)
gpu::cuda::device::unique_ptr< void ** > indexOnDevice
Definition registry.hh:62
std::map< std::type_index, gpu::cuda::FieldArrayPointer > fieldArrayPointers
Definition registry.hh:59
Type-erased pointer to FieldArrayD device data.
Definition registry.h:42
Host-side version of gpu::cuda::AnyDeviceFieldArrayD.
Definition registry.hh:43
gpu::cuda::device::unique_ptr< void * > data
Definition registry.hh:44