DiFfRG
Loading...
Searching...
No Matches
integrator_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{
21 template <int d, typename NT, typename KERNEL> class IntegratorGPU
22 {
23 public:
27 using ctype = typename get_type::ctype<NT>;
28
34 template <typename... T> struct functor {
35 public:
40
41 __device__ NT operator()(const uint idx) const
42 {
43 const ctype weight = x_quadrature_w[idx] * x_extent;
44 const ctype q = k * sqrt(x_quadrature_p[idx] * x_extent);
45 constexpr ctype S_d = S_d_prec<ctype>(d);
46 const ctype int_element = S_d // solid nd angle
47 * (powr<d - 2>(q) / 2 * powr<2>(k)) // x = p^2 / k^2 integral
48 / powr<d>(2 * (ctype)M_PI); // fourier factor
49
50 const NT res = std::apply([&](auto &&...args) { return KERNEL::kernel(q, k, args...); }, t);
51 return int_element * res * weight;
52 }
53
54 private:
58 const ctype k;
59 const std::tuple<T...> t;
60 };
61
62 IntegratorGPU(QuadratureProvider &quadrature_provider, const std::array<uint, 1> grid_size, const ctype x_extent,
63 const uint max_block_size = 256)
64 : IntegratorGPU(quadrature_provider, grid_size[0], x_extent, max_block_size)
65 {
66 }
67
68 IntegratorGPU(QuadratureProvider &quadrature_provider, const std::array<uint, 1> grid_size, const ctype x_extent,
69 const JSONValue &json)
70 : IntegratorGPU(quadrature_provider, grid_size[0], x_extent, json.get_uint("/integration/cudathreadsperblock"))
71 {
72 }
73
74 IntegratorGPU(QuadratureProvider &quadrature_provider, const uint grid_size, const ctype x_extent,
75 const uint max_block_size = 256)
77 {
78 (void)max_block_size;
81 }
82
88
99 template <typename... T> NT get(const ctype k, const T &...t) const
100 {
101 return KERNEL::constant(k, t...) +
102 thrust::transform_reduce(
103 thrust::cuda::par.on(rmm::cuda_stream_per_thread.value()), thrust::make_counting_iterator<uint>(0),
104 thrust::make_counting_iterator<uint>(grid_size),
105 functor<T...>(ptr_x_quadrature_p, ptr_x_quadrature_w, x_extent, k, t...), NT(0), thrust::plus<NT>());
106 }
107
118 template <typename... T> std::future<NT> request(const ctype k, const T &...t) const
119 {
120 return std::async(std::launch::deferred, [=, this]() { return get(k, t...); });
121 }
122
123 private:
125
128
130 };
131} // namespace DiFfRG
132
133#else
134
135#ifdef USE_CUDA
136
137namespace DiFfRG
138{
139 template <int d, typename NT, typename KERNEL> class IntegratorGPU;
140}
141
142#else
143
145
146namespace DiFfRG
147{
148 template <int d, typename NT, typename KERNEL> class IntegratorGPU : public IntegratorTBB<d, NT, KERNEL>
149 {
150 public:
151 using ctype = typename get_type::ctype<NT>;
152
153 IntegratorGPU(QuadratureProvider &quadrature_provider, const std::array<uint, 1> grid_size, const ctype x_extent,
154 const uint max_block_size = 256)
155 : IntegratorTBB<d, NT, KERNEL>(quadrature_provider, grid_size, x_extent)
156 {
157 (void)max_block_size;
158 }
159
160 IntegratorGPU(QuadratureProvider &quadrature_provider, const uint grid_size, const ctype x_extent,
161 const uint max_block_size = 256)
162 : IntegratorTBB<d, NT, KERNEL>(quadrature_provider, grid_size, x_extent)
163 {
164 (void)max_block_size;
165 }
166
167 IntegratorGPU(QuadratureProvider &quadrature_provider, const std::array<uint, 1> grid_size, const ctype x_extent,
168 const JSONValue &)
169 : IntegratorTBB<d, NT, KERNEL>(quadrature_provider, grid_size, x_extent)
170 {
171 }
172 };
173} // namespace DiFfRG
174
175#endif
176
177#endif
Definition integrator_gpu.hh:22
IntegratorGPU(const IntegratorGPU &other)
Definition integrator_gpu.hh:83
const ctype x_extent
Definition integrator_gpu.hh:129
typename get_type::ctype< NT > ctype
Numerical type to be used for integration tasks e.g. the argument or possible jacobians.
Definition integrator_gpu.hh:27
NT get(const ctype k, const T &...t) const
Get the integral of the kernel.
Definition integrator_gpu.hh:99
IntegratorGPU(QuadratureProvider &quadrature_provider, const std::array< uint, 1 > grid_size, const ctype x_extent, const JSONValue &json)
Definition integrator_gpu.hh:68
const uint grid_size
Definition integrator_gpu.hh:124
const ctype * ptr_x_quadrature_p
Definition integrator_gpu.hh:126
const ctype * ptr_x_quadrature_w
Definition integrator_gpu.hh:127
IntegratorGPU(QuadratureProvider &quadrature_provider, const std::array< uint, 1 > grid_size, const ctype x_extent, const uint max_block_size=256)
Definition integrator_gpu.hh:62
IntegratorGPU(QuadratureProvider &quadrature_provider, const uint grid_size, const ctype x_extent, const uint max_block_size=256)
Definition integrator_gpu.hh:74
std::future< NT > request(const ctype k, const T &...t) const
Request a future for the integral of the kernel.
Definition integrator_gpu.hh:118
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
constexpr __forceinline__ __host__ __device__ double S_d(NT d)
Surface of a d-dimensional sphere.
Definition math.hh:91
consteval NT S_d_prec(uint d)
Surface of a d-dimensional sphere (precompiled)
Definition math.hh:104
unsigned int uint
Definition utils.hh:22
Custom functor for the thrust::transform_reduce function.
Definition integrator_gpu.hh:34
const ctype x_extent
Definition integrator_gpu.hh:57
__device__ NT operator()(const uint idx) const
Definition integrator_gpu.hh:41
const ctype * x_quadrature_w
Definition integrator_gpu.hh:56
functor(const ctype *x_quadrature_p, const ctype *x_quadrature_w, const ctype x_extent, const ctype k, T... t)
Definition integrator_gpu.hh:36
const ctype * x_quadrature_p
Definition integrator_gpu.hh:55
const ctype k
Definition integrator_gpu.hh:58
const std::tuple< T... > t
Definition integrator_gpu.hh:59