ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
ReactionKernelBulk_all.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022-2026 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20// kernel generated with pystencils v1.4+1.ge851f4e, lbmpy v1.4+1.ge9efe34,
21// sympy v1.12.1, lbmpy_walberla/pystencils_walberla from waLBerla commit
22// 007e77e077ad9d22b5eed6f3d3118240993e553c
23
24#pragma once
25
26#include <waLBerlaDefinitions.h>
27
30
33
36
39
42
43#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
44
47
50
53
56
59
60#endif
61
62#include <domain_decomposition/BlockDataID.h>
63
64#include <cstddef>
65#include <memory>
66#include <stdexcept>
67#include <utility>
68#include <vector>
69
70namespace walberla {
71namespace detail {
72namespace ReactionKernelBulkSelector {
73
74template <typename FloatType = double, std::size_t N = 1> struct KernelTrait {
75 using ReactionKernelBulk = pystencils::ReactionKernelBulk_1_double_precision;
76};
77
78template <> struct KernelTrait<double, 2> {
79 using ReactionKernelBulk = pystencils::ReactionKernelBulk_2_double_precision;
80};
81
82template <> struct KernelTrait<double, 3> {
83 using ReactionKernelBulk = pystencils::ReactionKernelBulk_3_double_precision;
84};
85
86template <> struct KernelTrait<double, 4> {
87 using ReactionKernelBulk = pystencils::ReactionKernelBulk_4_double_precision;
88};
89
90template <> struct KernelTrait<double, 5> {
91 using ReactionKernelBulk = pystencils::ReactionKernelBulk_5_double_precision;
92};
93
94template <> struct KernelTrait<float, 1> {
95 using ReactionKernelBulk = pystencils::ReactionKernelBulk_1_single_precision;
96};
97
98template <> struct KernelTrait<float, 2> {
99 using ReactionKernelBulk = pystencils::ReactionKernelBulk_2_single_precision;
100};
101
102template <> struct KernelTrait<float, 3> {
103 using ReactionKernelBulk = pystencils::ReactionKernelBulk_3_single_precision;
104};
105
106template <> struct KernelTrait<float, 4> {
107 using ReactionKernelBulk = pystencils::ReactionKernelBulk_4_single_precision;
108};
109
110template <> struct KernelTrait<float, 5> {
111 using ReactionKernelBulk = pystencils::ReactionKernelBulk_5_single_precision;
112};
113
114template <typename FloatType, class Reactant, std::size_t... ints>
115auto get_kernel_impl(const std::vector<std::shared_ptr<Reactant>> &reactants,
116 const double coefficient,
117 std::index_sequence<ints...> int_seq) {
118 auto kernel = std::make_shared<
119 typename KernelTrait<FloatType, int_seq.size()>::ReactionKernelBulk>(
120 walberla::BlockDataID(
121 reactants[ints]->get_species()->get_density_id())...,
122 numeric_cast<FloatType>(reactants[ints]->get_order())...,
123 numeric_cast<FloatType>(coefficient),
124 numeric_cast<FloatType>(reactants[ints]->get_stoech_coeff())...);
125
126 std::function<void(IBlock *)> sweep = [kernel](IBlock *b) { kernel->run(b); };
127 return sweep;
128}
129
130template <typename FloatType, class Reactant, class... Args>
131auto get_kernel_impl(const std::vector<std::shared_ptr<Reactant>> &reactants,
132 Args... args) {
133 switch (reactants.size()) {
134
135 case 1:
136 return get_kernel_impl<FloatType>(reactants, args...,
137 std::make_index_sequence<1>{});
138
139 case 2:
140 return get_kernel_impl<FloatType>(reactants, args...,
141 std::make_index_sequence<2>{});
142
143 case 3:
144 return get_kernel_impl<FloatType>(reactants, args...,
145 std::make_index_sequence<3>{});
146
147 case 4:
148 return get_kernel_impl<FloatType>(reactants, args...,
149 std::make_index_sequence<4>{});
150
151 case 5:
152 return get_kernel_impl<FloatType>(reactants, args...,
153 std::make_index_sequence<5>{});
154
155 default:
156 throw std::runtime_error("reactions of this size are not implemented!");
157 }
158}
159
160template <class Reactant, class... Args>
161auto get_kernel(const std::vector<std::shared_ptr<Reactant>> &reactants,
162 Args... args) {
163
164 const auto is_double_precision =
165 reactants[0]->get_species()->is_double_precision();
166
167 if (is_double_precision) {
168 return get_kernel_impl<double>(reactants, args...);
169 }
170
171 return get_kernel_impl<float>(reactants, args...);
172}
173
174#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
175
176template <typename FloatType = double, std::size_t N = 1>
177struct KernelTraitGPU {
178 using ReactionKernelBulkGPU =
179 pystencils::ReactionKernelBulk_1_double_precision_CUDA;
180};
181
182template <> struct KernelTraitGPU<double, 2> {
183 using ReactionKernelBulkGPU =
184 pystencils::ReactionKernelBulk_2_double_precision_CUDA;
185};
186
187template <> struct KernelTraitGPU<double, 3> {
188 using ReactionKernelBulkGPU =
189 pystencils::ReactionKernelBulk_3_double_precision_CUDA;
190};
191
192template <> struct KernelTraitGPU<double, 4> {
193 using ReactionKernelBulkGPU =
194 pystencils::ReactionKernelBulk_4_double_precision_CUDA;
195};
196
197template <> struct KernelTraitGPU<double, 5> {
198 using ReactionKernelBulkGPU =
199 pystencils::ReactionKernelBulk_5_double_precision_CUDA;
200};
201
202template <> struct KernelTraitGPU<float, 1> {
203 using ReactionKernelBulkGPU =
204 pystencils::ReactionKernelBulk_1_single_precision_CUDA;
205};
206
207template <> struct KernelTraitGPU<float, 2> {
208 using ReactionKernelBulkGPU =
209 pystencils::ReactionKernelBulk_2_single_precision_CUDA;
210};
211
212template <> struct KernelTraitGPU<float, 3> {
213 using ReactionKernelBulkGPU =
214 pystencils::ReactionKernelBulk_3_single_precision_CUDA;
215};
216
217template <> struct KernelTraitGPU<float, 4> {
218 using ReactionKernelBulkGPU =
219 pystencils::ReactionKernelBulk_4_single_precision_CUDA;
220};
221
222template <> struct KernelTraitGPU<float, 5> {
223 using ReactionKernelBulkGPU =
224 pystencils::ReactionKernelBulk_5_single_precision_CUDA;
225};
226
227template <typename FloatType, class Reactant, std::size_t... ints>
228auto get_kernel_impl_gpu(
229 const std::vector<std::shared_ptr<Reactant>> &reactants,
230 const double coefficient, std::index_sequence<ints...> int_seq) {
231 auto kernel = std::make_shared<typename KernelTraitGPU<
232 FloatType, int_seq.size()>::ReactionKernelBulkGPU>(
233 walberla::BlockDataID(
234 reactants[ints]->get_species()->get_density_id())...,
235 numeric_cast<FloatType>(reactants[ints]->get_order())...,
236 numeric_cast<FloatType>(coefficient),
237 numeric_cast<FloatType>(reactants[ints]->get_stoech_coeff())...);
238
239 std::function<void(IBlock *)> sweep = [kernel](IBlock *b) { kernel->run(b); };
240 return sweep;
241}
242
243template <typename FloatType, class Reactant, class... Args>
244auto get_kernel_impl_gpu(
245 const std::vector<std::shared_ptr<Reactant>> &reactants, Args... args) {
246 switch (reactants.size()) {
247
248 case 1:
249 return get_kernel_impl_gpu<FloatType>(reactants, args...,
250 std::make_index_sequence<1>{});
251
252 case 2:
253 return get_kernel_impl_gpu<FloatType>(reactants, args...,
254 std::make_index_sequence<2>{});
255
256 case 3:
257 return get_kernel_impl_gpu<FloatType>(reactants, args...,
258 std::make_index_sequence<3>{});
259
260 case 4:
261 return get_kernel_impl_gpu<FloatType>(reactants, args...,
262 std::make_index_sequence<4>{});
263
264 case 5:
265 return get_kernel_impl_gpu<FloatType>(reactants, args...,
266 std::make_index_sequence<5>{});
267
268 default:
269 throw std::runtime_error("reactions of this size are not implemented!");
270 }
271}
272
273template <class Reactant, class... Args>
274auto get_kernel_gpu(const std::vector<std::shared_ptr<Reactant>> &reactants,
275 Args... args) {
276
277 const auto is_double_precision =
278 reactants[0]->get_species()->is_double_precision();
279
280 if (is_double_precision) {
281 return get_kernel_impl_gpu<double>(reactants, args...);
282 }
283
284 return get_kernel_impl_gpu<float>(reactants, args...);
285}
286
287#endif
288
289} // namespace ReactionKernelBulkSelector
290} // namespace detail
291} // namespace walberla
\file PackInfoPdfDoublePrecision.cpp \author pystencils