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