preCICE v3.2.0
Loading...
Searching...
No Matches
GinkgoRBFKernels.cpp
Go to the documentation of this file.
3#include "math/math.hpp"
4
5#include <functional>
6#include <ginkgo/extensions/kokkos.hpp>
7#include <ginkgo/ginkgo.hpp>
8
11
12using gko::ext::kokkos::map_data;
13
14namespace precice::mapping {
15
16std::shared_ptr<gko::Executor> create_device_executor(const std::string &execName, bool enableUnifiedMemory)
17{
18#ifdef KOKKOS_ENABLE_SERIAL
19 if (execName == "reference-executor") {
20 return gko::ext::kokkos::create_executor(Kokkos::Serial{});
21 }
22#endif
23#ifdef PRECICE_WITH_OPENMP
24 if (execName == "omp-executor") {
25 return gko::ext::kokkos::create_executor(Kokkos::OpenMP{});
26 }
27#endif
28#ifdef PRECICE_WITH_CUDA
29 if (execName == "cuda-executor") {
30 if (enableUnifiedMemory) {
31 return gko::ext::kokkos::create_executor(Kokkos::Cuda{}, Kokkos::CudaUVMSpace{});
32 } else {
33 return gko::ext::kokkos::create_executor(Kokkos::Cuda{}, Kokkos::CudaSpace{});
34 }
35 }
36#endif
37#ifdef PRECICE_WITH_HIP
38 if (execName == "hip-executor") {
39 if (enableUnifiedMemory) {
40 return gko::ext::kokkos::create_executor(Kokkos::HIP{}, Kokkos::HIPManagedSpace{});
41 } else {
42 return gko::ext::kokkos::create_executor(Kokkos::HIP{}, Kokkos::HIPSpace{});
43 }
44 }
45#endif
46 PRECICE_UNREACHABLE("Executor {} unknown to preCICE", execName);
47}
48
49namespace kernel {
50
51template <typename MemorySpace, typename EvalFunctionType>
53 gko::ptr_param<GinkgoMatrix> mtx,
54 const std::array<bool, 3> activeAxis,
55 gko::ptr_param<GinkgoMatrix> supportPoints,
56 gko::ptr_param<GinkgoMatrix> targetPoints,
57 EvalFunctionType f,
59 bool addPolynomial,
60 unsigned int extraDims)
61{
62 auto k_mtx = map_data<MemorySpace>(mtx.get());
63 auto k_supportPoints = map_data<MemorySpace>(supportPoints.get());
64 auto k_targetPoints = map_data<MemorySpace>(targetPoints.get());
65
66 // the outcome of this if-condition is known at compile time. However, Cuda
67 // has issues with constexpr
69 // Row-major access
70 Kokkos::parallel_for(
71 "create_rbf_system_matrix_row_major",
72 Kokkos::MDRangePolicy<typename MemorySpace::execution_space, Kokkos::Rank<2>>{{0, 0}, {mtx->get_size()[0], mtx->get_size()[1]}},
73 KOKKOS_LAMBDA(const int &i, const int &j) {
74 double dist = 0;
75 if (addPolynomial) {
76 k_mtx(i, j) = 0; // Zero the matrix entry if polynomial terms are added
77 }
78 // We need to use a pointer here, because the bound checking of std::array
79 // contains some host-only code, which yields errors when compiling in
80 // debug mode
81 const bool *deviceActiveAxis = activeAxis.data();
82
83 // Compute Euclidean distance using row-major indexing
84 for (size_t k = 0; k < activeAxis.size(); ++k) {
85 if (deviceActiveAxis[k]) {
86 double diff = k_supportPoints(j, k) - k_targetPoints(i, k);
87 dist += diff * diff;
88 }
89 }
90 dist = Kokkos::sqrt(dist);
91 k_mtx(i, j) = f(dist, rbf_params); // Evaluate the RBF function
92 });
93 } else {
94 // Column-major access
95 Kokkos::parallel_for(
96 "create_rbf_system_matrix_col_major",
97 Kokkos::MDRangePolicy<typename MemorySpace::execution_space, Kokkos::Rank<2>>{{0, 0}, {mtx->get_size()[0], mtx->get_size()[1]}},
98 KOKKOS_LAMBDA(const int &i, const int &j) {
99 double dist = 0;
100 if (addPolynomial) {
101 k_mtx(i, j) = 0; // Zero the matrix entry if polynomial terms are added
102 }
103 const bool *deviceActiveAxis = activeAxis.data();
104
105 // Compute Euclidean distance using column-major indexing
106 for (size_t k = 0; k < activeAxis.size(); ++k) {
107 if (deviceActiveAxis[k]) {
108 double diff = k_supportPoints(k, j) - k_targetPoints(k, i);
109 dist += diff * diff;
110 }
111 }
112 dist = Kokkos::sqrt(dist);
113 k_mtx(i, j) = f(dist, rbf_params); // Evaluate the RBF function
114 });
115 }
116}
117
118template <typename EvalFunctionType>
120 bool unifiedMemory,
121 gko::ptr_param<GinkgoMatrix> mtx,
122 const std::array<bool, 3> activeAxis,
123 gko::ptr_param<GinkgoMatrix> supportPoints,
124 gko::ptr_param<GinkgoMatrix> targetPoints,
125 EvalFunctionType f,
127 bool addPolynomial,
128 unsigned int extraDims)
129{
130#ifdef KOKKOS_ENABLE_SERIAL
132 create_rbf_system_matrix_impl<Kokkos::Serial::memory_space>(exec, mtx, activeAxis, supportPoints, targetPoints, f, rbf_params, addPolynomial, extraDims);
133 return;
134 }
135#endif
136#ifdef PRECICE_WITH_OPENMP
138 create_rbf_system_matrix_impl<Kokkos::OpenMP::memory_space>(exec, mtx, activeAxis, supportPoints, targetPoints, f, rbf_params, addPolynomial, extraDims);
139 return;
140 }
141#endif
142#ifdef PRECICE_WITH_CUDA
144 if (unifiedMemory) {
145 create_rbf_system_matrix_impl<Kokkos::CudaUVMSpace>(exec, mtx, activeAxis, supportPoints, targetPoints, f, rbf_params, addPolynomial, extraDims);
146 } else {
147 create_rbf_system_matrix_impl<Kokkos::CudaSpace>(exec, mtx, activeAxis, supportPoints, targetPoints, f, rbf_params, addPolynomial, extraDims);
148 }
149 return;
150 }
151#endif
152#ifdef PRECICE_WITH_HIP
154 if (unifiedMemory) {
155 create_rbf_system_matrix_impl<Kokkos::HIPManagedSpace>(exec, mtx, activeAxis, supportPoints, targetPoints, f, rbf_params, addPolynomial, extraDims);
156 } else {
157 create_rbf_system_matrix_impl<Kokkos::HIPSpace>(exec, mtx, activeAxis, supportPoints, targetPoints, f, rbf_params, addPolynomial, extraDims);
158 }
159 return;
160 }
161#endif
162 PRECICE_UNREACHABLE("Executor unknown to preCICE");
163}
164
165#define PRECICE_INSTANTIATE_CREATE_RBF_SYSTEM_MATRIX(_function_type) \
166 template void create_rbf_system_matrix<_function_type>(std::shared_ptr<const gko::Executor> exec, \
167 bool unifiedMemory, \
168 gko::ptr_param<GinkgoMatrix> mtx, const std::array<bool, 3> activeAxis, \
169 gko::ptr_param<GinkgoMatrix> supportPoints, gko::ptr_param<GinkgoMatrix> targetPoints, \
170 _function_type f, RadialBasisParameters rbf_params, bool addPolynomial, unsigned int extraDims)
171
183#undef PRECICE_INSTANTIATE_CREATE_RBF_SYSTEM_MATRIX
184
185template <typename MemorySpace>
187 gko::ptr_param<GinkgoMatrix> mtx,
188 gko::ptr_param<const GinkgoMatrix> x,
189 const unsigned int dims)
190{
191
192 auto k_mtx = map_data<MemorySpace>(mtx.get());
193 auto k_x = map_data<MemorySpace>(x.get());
194
196 Kokkos::parallel_for(
197 "fill_polynomial_matrix_row_major",
198 Kokkos::MDRangePolicy<typename MemorySpace::execution_space, Kokkos::Rank<2>>{{0, 0}, {mtx->get_size()[0], mtx->get_size()[1]}},
199 KOKKOS_LAMBDA(const int &i, const int &j) {
200 double value;
201 if (j < dims - 1) {
202 value = k_x(i, j); // Row-major access
203 } else {
204 value = 1;
205 }
206 k_mtx(i, j) = value;
207 });
208 } else {
209 Kokkos::parallel_for(
210 "fill_polynomial_matrix_col_major",
211 Kokkos::MDRangePolicy<typename MemorySpace::execution_space, Kokkos::Rank<2>>{{0, 0}, {mtx->get_size()[0], mtx->get_size()[1]}},
212 KOKKOS_LAMBDA(const int &i, const int &j) {
213 double value;
214 if (j < dims - 1) {
215 value = k_x(j, i); // Column-major access
216 } else {
217 value = 1;
218 }
219 k_mtx(i, j) = value;
220 });
221 }
222}
223
225 bool unifiedMemory,
226 gko::ptr_param<GinkgoMatrix> mtx,
227 gko::ptr_param<const GinkgoMatrix> x,
228 const unsigned int dims)
229{
230#ifdef KOKKOS_ENABLE_SERIAL
233 return;
234 }
235#endif
236#ifdef PRECICE_WITH_OPENMP
239 return;
240 }
241#endif
242#ifdef PRECICE_WITH_CUDA
244 if (unifiedMemory) {
246 } else {
248 }
249 return;
250 }
251#endif
252#ifdef PRECICE_WITH_HIP
254 if (unifiedMemory) {
256 } else {
258 }
259 return;
260 }
261#endif
262 PRECICE_UNREACHABLE("Executor unknown to preCICE");
263}
264} // namespace kernel
265} // namespace precice::mapping
#define PRECICE_INSTANTIATE_CREATE_RBF_SYSTEM_MATRIX(_function_type)
#define PRECICE_UNREACHABLE(...)
Definition assertion.hpp:93
Wendland radial basis function with compact support.
Wendland radial basis function with compact support.
Wendland radial basis function with compact support.
Wendland radial basis function with compact support.
Wendland radial basis function with compact support.
Radial basis function with compact support.
Radial basis function with global and compact support.
Radial basis function with global support.
Radial basis function with global support.
Radial basis function with global support.
Radial basis function with global support.
T data(T... args)
T is_same_v
void fill_polynomial_matrix(std::shared_ptr< const gko::Executor > exec, bool unifiedMemory, gko::ptr_param< GinkgoMatrix > mtx, gko::ptr_param< const GinkgoMatrix > x, const unsigned int dims)
void fill_polynomial_matrix_impl(std::shared_ptr< const gko::Executor > exec, gko::ptr_param< GinkgoMatrix > mtx, gko::ptr_param< const GinkgoMatrix > x, const unsigned int dims)
void create_rbf_system_matrix(std::shared_ptr< const gko::Executor > exec, bool unifiedMemory, gko::ptr_param< GinkgoMatrix > mtx, const std::array< bool, 3 > activeAxis, gko::ptr_param< GinkgoMatrix > supportPoints, gko::ptr_param< GinkgoMatrix > targetPoints, EvalFunctionType f, ::precice::mapping::RadialBasisParameters rbf_params, bool addPolynomial, unsigned int extraDims)
void create_rbf_system_matrix_impl(std::shared_ptr< const gko::Executor > exec, gko::ptr_param< GinkgoMatrix > mtx, const std::array< bool, 3 > activeAxis, gko::ptr_param< GinkgoMatrix > supportPoints, gko::ptr_param< GinkgoMatrix > targetPoints, EvalFunctionType f, ::precice::mapping::RadialBasisParameters rbf_params, bool addPolynomial, unsigned int extraDims)
contains data mapping from points to meshes.
std::shared_ptr< gko::Executor > create_device_executor(const std::string &execName, bool enableUnifiedMemory)
constexpr T pow_int(const T x)
Computes the power of a given number by an integral exponent given at compile time,...
Definition math.hpp:22
T dynamic_pointer_cast(T... args)
T size(T... args)
Wrapper struct that is used to transfer RBF-specific parameters to the GPU.