DiFfRG
Loading...
Searching...
No Matches
integrator_1D_cartesian_gpu.hh
Go to the documentation of this file.
1#pragma once
2
3#ifdef __CUDACC__
4
5// standard library
6#include <future>
7
8// external libraries
9#include <rmm/cuda_stream_pool.hpp>
10#include <rmm/device_uvector.hpp>
11#include <rmm/mr/device/pool_memory_resource.hpp>
12#include <thrust/reduce.h>
13#include <thrust/transform_reduce.h>
14
15// DiFfRG
18
19namespace DiFfRG
20{
27 template <typename NT, typename KERNEL> class Integrator1DCartesianGPU
28 {
29 public:
33 using ctype = typename get_type::ctype<NT>;
34
40 template <typename... T> struct functor {
41 public:
48
49 __device__ NT operator()(const uint idx) const
50 {
51 constexpr int d = 1;
52 constexpr ctype int_element = powr<-d>(2 * (ctype)M_PI); // fourier factor
53
54 const ctype q = qx_min + qx_extent * x_quadrature_p[idx];
55 const ctype weight = qx_extent * x_quadrature_w[idx];
56
57 const NT res = NT(1) * std::apply([&](auto &&...args) { return KERNEL::kernel(q, k, args...); }, t);
58 return int_element * res * weight;
59 }
60
61 private:
65 const ctype k;
66 const std::tuple<T...> t;
67 };
68
79 Integrator1DCartesianGPU(QuadratureProvider &quadrature_provider, const std::array<uint, 1> grid_size,
80 const ctype x_extent = 0., const uint max_block_size = 0, const ctype qx_min = -M_PI,
81 const ctype qx_max = M_PI)
82 : Integrator1DCartesianGPU(quadrature_provider, grid_size[0], x_extent, qx_min, qx_max)
83 {
84 }
85
93 Integrator1DCartesianGPU(QuadratureProvider &quadrature_provider, const std::array<uint, 1> grid_size,
94 const JSONValue &json)
95 : Integrator1DCartesianGPU(quadrature_provider, grid_size[0], 0.,
96 json.get_double("/discretization/integration/qx_min", -M_PI),
97 json.get_double("/discretization/integration/qx_max", M_PI))
98 {
99 }
100
109 Integrator1DCartesianGPU(QuadratureProvider &quadrature_provider, const std::array<uint, 1> grid_size,
110 const ctype x_extent, const JSONValue &json)
111 : Integrator1DCartesianGPU(quadrature_provider, grid_size[0], x_extent,
112 json.get_double("/discretization/integration/qx_min", -M_PI),
113 json.get_double("/discretization/integration/qx_max", M_PI))
114 {
115 }
116
126 Integrator1DCartesianGPU(QuadratureProvider &quadrature_provider, const uint grid_size, const ctype x_extent = 0.,
127 const ctype qx_min = -M_PI, const ctype qx_max = M_PI)
129 {
130 ptr_x_quadrature_p = quadrature_provider.get_device_points<ctype>(grid_size);
132
133 this->qx_min = qx_min;
134 this->qx_extent = qx_max - qx_min;
135 }
136
149
154 {
155 this->qx_extent = this->qx_extent - qx_min + this->qx_min;
156 this->qx_min = qx_min;
157 }
158
162 void set_qx_max(const ctype qx_max) { this->qx_extent = qx_max - qx_min; }
163
172 template <typename... T> NT get(const ctype k, const T &...t) const
173 {
174 return KERNEL::constant(k, t...) +
175 thrust::transform_reduce(thrust::cuda::par.on(rmm::cuda_stream_per_thread.value()),
176 thrust::make_counting_iterator<uint>(0),
177 thrust::make_counting_iterator<uint>(grid_size),
179 NT(0), thrust::plus<NT>());
180 }
181
190 template <typename... T> std::future<NT> request(const ctype k, const T &...t) const
191 {
192 return std::async(std::launch::deferred, [=, this]() { return get(k, t...); });
193 }
194
195 private:
197
198 ctype qx_min = -M_PI;
199 ctype qx_extent = 2. * M_PI;
200
203 };
204} // namespace DiFfRG
205
206#else
207
208#ifdef USE_CUDA
209
210namespace DiFfRG
211{
212 template <typename NT, typename KERNEL> class Integrator1DCartesianGPU;
213}
214
215#else
216
218
219namespace DiFfRG
220{
221 template <typename NT, typename KERNEL> class Integrator1DCartesianGPU : public Integrator1DCartesianTBB<NT, KERNEL>
222 {
223 public:
224 using ctype = typename get_type::ctype<NT>;
225
226 Integrator1DCartesianGPU(QuadratureProvider &quadrature_provider, const std::array<uint, 1> grid_size,
227 const ctype x_extent, const uint max_block_size = 256, const ctype qx_min = -M_PI,
228 const ctype qx_max = M_PI)
229 : Integrator1DCartesianTBB<NT, KERNEL>(quadrature_provider, grid_size[0], x_extent, qx_min, qx_max)
230 {
231 (void)max_block_size;
232 }
233
234 Integrator1DCartesianGPU(QuadratureProvider &quadrature_provider, const uint grid_size, const ctype x_extent,
235 const uint max_block_size = 256, const ctype qx_min = -M_PI, const ctype qx_max = M_PI)
236 : Integrator1DCartesianTBB<NT, KERNEL>(quadrature_provider, grid_size, x_extent, x_extent, qx_min, qx_max)
237 {
238 (void)max_block_size;
239 }
240
241 Integrator1DCartesianGPU(QuadratureProvider &quadrature_provider, const std::array<uint, 1> grid_size,
242 const ctype x_extent, const JSONValue &json)
243 : Integrator1DCartesianTBB<NT, KERNEL>(quadrature_provider, grid_size, x_extent, json)
244 {
245 }
246 };
247} // namespace DiFfRG
248
249#endif
250
251#endif
Integration of an arbitrary 1D function from qx_min to qx_max using CUDA.
Definition integrator_1D_cartesian_gpu.hh:28
const ctype * ptr_x_quadrature_p
Definition integrator_1D_cartesian_gpu.hh:201
const uint grid_size
Definition integrator_1D_cartesian_gpu.hh:196
typename get_type::ctype< NT > ctype
Numerical type to be used for integration tasks e.g. the argument or possible jacobians.
Definition integrator_1D_cartesian_gpu.hh:33
Integrator1DCartesianGPU(QuadratureProvider &quadrature_provider, const std::array< uint, 1 > grid_size, const JSONValue &json)
Construct a new Integrator1DCartesianGPU object.
Definition integrator_1D_cartesian_gpu.hh:93
const ctype * ptr_x_quadrature_w
Definition integrator_1D_cartesian_gpu.hh:202
std::future< NT > request(const ctype k, const T &...t) const
Get the result of the integration asynchronously.
Definition integrator_1D_cartesian_gpu.hh:190
Integrator1DCartesianGPU(QuadratureProvider &quadrature_provider, const std::array< uint, 1 > grid_size, const ctype x_extent=0., const uint max_block_size=0, const ctype qx_min=-M_PI, const ctype qx_max=M_PI)
Construct a new Integrator1DCartesianGPU object.
Definition integrator_1D_cartesian_gpu.hh:79
ctype qx_min
Definition integrator_1D_cartesian_gpu.hh:198
void set_qx_min(const ctype qx_min)
Set the minimum value of the qx integration range.
Definition integrator_1D_cartesian_gpu.hh:153
Integrator1DCartesianGPU(QuadratureProvider &quadrature_provider, const uint grid_size, const ctype x_extent=0., const ctype qx_min=-M_PI, const ctype qx_max=M_PI)
Construct a new Integrator1DCartesianGPU object.
Definition integrator_1D_cartesian_gpu.hh:126
ctype qx_extent
Definition integrator_1D_cartesian_gpu.hh:199
Integrator1DCartesianGPU(QuadratureProvider &quadrature_provider, const std::array< uint, 1 > grid_size, const ctype x_extent, const JSONValue &json)
Construct a new Integrator1DCartesianGPU object.
Definition integrator_1D_cartesian_gpu.hh:109
Integrator1DCartesianGPU(const Integrator1DCartesianGPU &other)
Construct a copy of an existing Integrator1DCartesianGPU object.
Definition integrator_1D_cartesian_gpu.hh:142
NT get(const ctype k, const T &...t) const
Get the result of the integration.
Definition integrator_1D_cartesian_gpu.hh:172
void set_qx_max(const ctype qx_max)
Set the maximum value of the qx integration range.
Definition integrator_1D_cartesian_gpu.hh:162
A wrapper around the boost json value class.
Definition json.hh:19
A class that provides quadrature points and weights, in host and device memory. The quadrature points...
Definition quadrature_provider.hh:139
const NT * get_device_weights(const size_t order, const int device=0, const QuadratureType type=QuadratureType::legendre)
Get the device-side quadrature weights for a quadrature of size quadrature_size.
Definition quadrature_provider.hh:211
const NT * get_device_points(const size_t order, const int device=0, const QuadratureType type=QuadratureType::legendre)
Get the device-side quadrature points for a quadrature of size quadrature_size.
Definition quadrature_provider.hh:198
typename internal::_ctype< CT >::value ctype
Definition types.hh:106
Definition complex_math.hh:14
constexpr __forceinline__ __host__ __device__ NumberType powr(const NumberType x)
A compile-time evaluatable power function for whole number exponents.
Definition math.hh:45
unsigned int uint
Definition utils.hh:22
Custom functor for the thrust::transform_reduce function.
Definition integrator_1D_cartesian_gpu.hh:40
const std::tuple< T... > t
Definition integrator_1D_cartesian_gpu.hh:66
const ctype k
Definition integrator_1D_cartesian_gpu.hh:65
const ctype qx_extent
Definition integrator_1D_cartesian_gpu.hh:64
const ctype * x_quadrature_w
Definition integrator_1D_cartesian_gpu.hh:63
const ctype qx_min
Definition integrator_1D_cartesian_gpu.hh:64
const ctype * x_quadrature_p
Definition integrator_1D_cartesian_gpu.hh:62
__device__ NT operator()(const uint idx) const
Definition integrator_1D_cartesian_gpu.hh:49
functor(const ctype *x_quadrature_p, const ctype *x_quadrature_w, const ctype qx_min, const ctype qx_extent, const ctype k, T... t)
Definition integrator_1D_cartesian_gpu.hh:42