ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
ReactionKernelIndexed_5_double_precision_CUDA.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022-2023 The ESPResSo project
3 * Copyright (C) 2020-2023 The waLBerla project
4 *
5 * This file is part of ESPResSo.
6 *
7 * ESPResSo is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * ESPResSo is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21// kernel generated with pystencils v1.3.7+13.gdfd203a, lbmpy
22// v1.3.7+10.gd3f6236, sympy v1.12.1, lbmpy_walberla/pystencils_walberla from
23// waLBerla commit e12db9965373887d86aab4aaaf4dd7b38fa588e8
24
25/*
26 * Boundary class.
27 * Adapted from the waLBerla source file
28 * https://i10git.cs.fau.de/walberla/walberla/-/blob/e12db9965373887d86aab4aaaf4dd7b38fa588e8/python/pystencils_walberla/templates/Boundary.tmpl.h
29 */
30
31#pragma once
32
33#include <core/DataTypes.h>
34
35#include <blockforest/StructuredBlockForest.h>
36#include <core/debug/Debug.h>
37#include <domain_decomposition/BlockDataID.h>
38#include <domain_decomposition/IBlock.h>
39#include <field/FlagField.h>
40#include <gpu/FieldCopy.h>
41#include <gpu/GPUField.h>
42#include <gpu/GPUWrapper.h>
43
44#include <cassert>
45#include <functional>
46#include <memory>
47#include <vector>
48
49#if defined(__clang__)
50#pragma clang diagnostic push
51#pragma clang diagnostic ignored "-Wunused-variable"
52#pragma clang diagnostic ignored "-Wunused-parameter"
53#elif defined(__GNUC__) or defined(__GNUG__)
54#pragma GCC diagnostic push
55#pragma GCC diagnostic ignored "-Wunused-variable"
56#pragma GCC diagnostic ignored "-Wunused-parameter"
57#endif
58
59#ifdef __GNUC__
60#define RESTRICT __restrict__
61#elif _MSC_VER
62#define RESTRICT __restrict
63#else
64#define RESTRICT
65#endif
66
67#ifdef WALBERLA_BUILD_WITH_HALF_PRECISION_SUPPORT
68using walberla::half;
69#endif
70
71namespace walberla {
72namespace pystencils {
73
75public:
76 struct IndexInfo {
77 int32_t x;
78 int32_t y;
79 int32_t z;
80 IndexInfo(int32_t x_, int32_t y_, int32_t z_) : x(x_), y(y_), z(z_) {}
81 bool operator==(const IndexInfo &o) const {
82 return x == o.x && y == o.y && z == o.z;
83 }
84 };
85
87 public:
88 using CpuIndexVector = std::vector<IndexInfo>;
89
90 enum Type { ALL = 0, INNER = 1, OUTER = 2, NUM_TYPES = 3 };
91
92 IndexVectors() = default;
93 bool operator==(IndexVectors const &other) const {
94 return other.cpuVectors_ == cpuVectors_;
95 }
96
98 for (auto &gpuVec : gpuVectors_) {
99 if (gpuVec) {
100 WALBERLA_GPU_CHECK(gpuFree(gpuVec));
101 }
102 }
103 }
104 CpuIndexVector &indexVector(Type t) { return cpuVectors_[t]; }
106 return cpuVectors_[t].empty() ? nullptr : cpuVectors_[t].data();
107 }
108
109 IndexInfo *pointerGpu(Type t) { return gpuVectors_[t]; }
110 void syncGPU() {
111 for (auto &gpuVec : gpuVectors_)
112 WALBERLA_GPU_CHECK(gpuFree(gpuVec));
113 gpuVectors_.resize(cpuVectors_.size());
114
115 WALBERLA_ASSERT_EQUAL(cpuVectors_.size(), NUM_TYPES);
116 for (size_t i = 0; i < cpuVectors_.size(); ++i) {
117 auto &gpuVec = gpuVectors_[i];
118 auto &cpuVec = cpuVectors_[i];
119 if (cpuVec.empty()) {
120 continue;
121 }
122 WALBERLA_GPU_CHECK(
123 gpuMalloc(&gpuVec, sizeof(IndexInfo) * cpuVec.size()));
124 WALBERLA_GPU_CHECK(gpuMemcpy(gpuVec, cpuVec.data(),
125 sizeof(IndexInfo) * cpuVec.size(),
126 gpuMemcpyHostToDevice));
127 }
128 }
129
130 private:
131 std::vector<CpuIndexVector> cpuVectors_{NUM_TYPES};
132
133 using GpuIndexVector = IndexInfo *;
134 std::vector<GpuIndexVector> gpuVectors_;
135 };
136
138 const std::shared_ptr<StructuredBlockForest> &blocks,
139 BlockDataID rho_0ID_, BlockDataID rho_1ID_, BlockDataID rho_2ID_,
140 BlockDataID rho_3ID_, BlockDataID rho_4ID_, double order_0,
141 double order_1, double order_2, double order_3, double order_4,
142 double rate_coefficient, double stoech_0, double stoech_1,
143 double stoech_2, double stoech_3, double stoech_4)
144 : rho_0ID(rho_0ID_), rho_1ID(rho_1ID_), rho_2ID(rho_2ID_),
145 rho_3ID(rho_3ID_), rho_4ID(rho_4ID_), order_0_(order_0),
146 order_1_(order_1), order_2_(order_2), order_3_(order_3),
147 order_4_(order_4), rate_coefficient_(rate_coefficient),
148 stoech_0_(stoech_0), stoech_1_(stoech_1), stoech_2_(stoech_2),
149 stoech_3_(stoech_3), stoech_4_(stoech_4) {
150 auto createIdxVector = [](IBlock *const, StructuredBlockStorage *const) {
151 return new IndexVectors();
152 };
153 indexVectorID = blocks->addStructuredBlockData<IndexVectors>(
154 createIdxVector,
155 "IndexField_ReactionKernelIndexed_5_double_precision_CUDA");
156 }
157
159 BlockDataID indexVectorID_, BlockDataID rho_0ID_, BlockDataID rho_1ID_,
160 BlockDataID rho_2ID_, BlockDataID rho_3ID_, BlockDataID rho_4ID_,
161 double order_0, double order_1, double order_2, double order_3,
162 double order_4, double rate_coefficient, double stoech_0, double stoech_1,
163 double stoech_2, double stoech_3, double stoech_4)
164 : indexVectorID(indexVectorID_), rho_0ID(rho_0ID_), rho_1ID(rho_1ID_),
165 rho_2ID(rho_2ID_), rho_3ID(rho_3ID_), rho_4ID(rho_4ID_),
166 order_0_(order_0), order_1_(order_1), order_2_(order_2),
167 order_3_(order_3), order_4_(order_4),
168 rate_coefficient_(rate_coefficient), stoech_0_(stoech_0),
169 stoech_1_(stoech_1), stoech_2_(stoech_2), stoech_3_(stoech_3),
170 stoech_4_(stoech_4) {}
171
172 void run(IBlock *block, gpuStream_t stream = nullptr);
173
174 void operator()(IBlock *block, gpuStream_t stream = nullptr) {
175 run(block, stream);
176 }
177
178 void inner(IBlock *block, gpuStream_t stream = nullptr);
179
180 void outer(IBlock *block, gpuStream_t stream = nullptr);
181
182 Vector3<real_t> getForce(IBlock * /*block*/) {
183
184 WALBERLA_ABORT(
185 "Boundary condition was not generated including force calculation.")
186 return Vector3<real_t>(real_c(0.0));
187 }
188
189 std::function<void(IBlock *)> getSweep(gpuStream_t stream = nullptr) {
190 return [this, stream](IBlock *b) { this->run(b, stream); };
191 }
192
193 std::function<void(IBlock *)> getInnerSweep(gpuStream_t stream = nullptr) {
194 return [this, stream](IBlock *b) { this->inner(b, stream); };
195 }
196
197 std::function<void(IBlock *)> getOuterSweep(gpuStream_t stream = nullptr) {
198 return [this, stream](IBlock *b) { this->outer(b, stream); };
199 }
200
201 template <typename FlagField_T>
202 void fillFromFlagField(const std::shared_ptr<StructuredBlockForest> &blocks,
203 ConstBlockDataID flagFieldID, FlagUID boundaryFlagUID,
204 FlagUID domainFlagUID) {
205 for (auto blockIt = blocks->begin(); blockIt != blocks->end(); ++blockIt)
206 fillFromFlagField<FlagField_T>(&*blockIt, flagFieldID, boundaryFlagUID,
207 domainFlagUID);
208 }
209
210 template <typename FlagField_T>
211 void fillFromFlagField(IBlock *block, ConstBlockDataID flagFieldID,
212 FlagUID boundaryFlagUID, FlagUID domainFlagUID) {
213 auto *indexVectors = block->getData<IndexVectors>(indexVectorID);
214 auto &indexVectorAll = indexVectors->indexVector(IndexVectors::ALL);
215 auto &indexVectorInner = indexVectors->indexVector(IndexVectors::INNER);
216 auto &indexVectorOuter = indexVectors->indexVector(IndexVectors::OUTER);
217
218 auto *flagField = block->getData<FlagField_T>(flagFieldID);
219
220 if (!(flagField->flagExists(boundaryFlagUID) and
221 flagField->flagExists(domainFlagUID)))
222 return;
223
224 auto boundaryFlag = flagField->getFlag(boundaryFlagUID);
225 auto domainFlag = flagField->getFlag(domainFlagUID);
226
227 auto inner = flagField->xyzSize();
228 inner.expand(cell_idx_t(-1));
229
230 indexVectorAll.clear();
231 indexVectorInner.clear();
232 indexVectorOuter.clear();
233
234 auto flagWithGLayers = flagField->xyzSizeWithGhostLayer();
235 for (auto it = flagField->beginWithGhostLayerXYZ(); it != flagField->end();
236 ++it) {
237
238 if (!isFlagSet(it, boundaryFlag))
239 continue;
240 if (flagWithGLayers.contains(it.x() + cell_idx_c(0),
241 it.y() + cell_idx_c(0),
242 it.z() + cell_idx_c(0)) &&
243 isFlagSet(it.neighbor(0, 0, 0, 0), domainFlag)) {
244
245 auto element = IndexInfo(it.x(), it.y(), it.z(), 0);
246
247 indexVectorAll.emplace_back(element);
248 if (inner.contains(it.x(), it.y(), it.z()))
249 indexVectorInner.emplace_back(element);
250 else
251 indexVectorOuter.emplace_back(element);
252 }
253 }
254
255 indexVectors->syncGPU();
256 }
257
258private:
259 void run_impl(IBlock *block, IndexVectors::Type type,
260 gpuStream_t stream = nullptr);
261
262 BlockDataID indexVectorID;
263
264public:
265 BlockDataID rho_0ID;
266 BlockDataID rho_1ID;
267 BlockDataID rho_2ID;
268 BlockDataID rho_3ID;
269 BlockDataID rho_4ID;
270 double order_0_;
271 double order_1_;
272 double order_2_;
273 double order_3_;
274 double order_4_;
276 double stoech_0_;
277 double stoech_1_;
278 double stoech_2_;
279 double stoech_3_;
280 double stoech_4_;
281};
282
283} // namespace pystencils
284} // namespace walberla
ReactionKernelIndexed_5_double_precision_CUDA(const std::shared_ptr< StructuredBlockForest > &blocks, BlockDataID rho_0ID_, BlockDataID rho_1ID_, BlockDataID rho_2ID_, BlockDataID rho_3ID_, BlockDataID rho_4ID_, double order_0, double order_1, double order_2, double order_3, double order_4, double rate_coefficient, double stoech_0, double stoech_1, double stoech_2, double stoech_3, double stoech_4)
void fillFromFlagField(IBlock *block, ConstBlockDataID flagFieldID, FlagUID boundaryFlagUID, FlagUID domainFlagUID)
ReactionKernelIndexed_5_double_precision_CUDA(BlockDataID indexVectorID_, BlockDataID rho_0ID_, BlockDataID rho_1ID_, BlockDataID rho_2ID_, BlockDataID rho_3ID_, BlockDataID rho_4ID_, double order_0, double order_1, double order_2, double order_3, double order_4, double rate_coefficient, double stoech_0, double stoech_1, double stoech_2, double stoech_3, double stoech_4)
void fillFromFlagField(const std::shared_ptr< StructuredBlockForest > &blocks, ConstBlockDataID flagFieldID, FlagUID boundaryFlagUID, FlagUID domainFlagUID)
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
static double * block(double *p, std::size_t index, std::size_t size)
Definition elc.cpp:176
\file PackInfoPdfDoublePrecision.cpp \author pystencils