DiFfRG
Loading...
Searching...
No Matches
integrator_cpu.hh
Go to the documentation of this file.
1#pragma once
2
3// standard library
4#include <future>
5
6// external libraries
7#include <tbb/tbb.h>
8
9// DiFfRG
11
12namespace DiFfRG
13{
14 template <int d, typename NT, typename KERNEL> class IntegratorTBB
15 {
16 public:
20 using ctype = typename get_type::ctype<NT>;
21 IntegratorTBB(QuadratureProvider &quadrature_provider, const std::array<uint, 1> grid_size, const ctype x_extent,
22 const uint max_block_size = 0)
23 : IntegratorTBB(quadrature_provider, grid_size[0], x_extent, max_block_size)
24 {
25 }
26
27 IntegratorTBB(QuadratureProvider &quadrature_provider, const std::array<uint, 1> grid_size, const ctype x_extent,
28 const JSONValue &)
29 : IntegratorTBB(quadrature_provider, grid_size[0], x_extent)
30 {
31 }
32
33 IntegratorTBB(QuadratureProvider &quadrature_provider, const uint grid_size, const ctype x_extent,
34 const uint max_block_size = 0)
35 : grid_size(grid_size), x_extent(x_extent), x_quadrature_p(quadrature_provider.get_points<ctype>(grid_size)),
36 x_quadrature_w(quadrature_provider.get_weights<ctype>(grid_size))
37 {
38 (void)max_block_size;
39 }
40
51 template <typename... T> NT get(const ctype k, const T &...t) const
52 {
53 constexpr ctype S_d = S_d_prec<ctype>(d);
54 using std::sqrt;
55
56 return KERNEL::constant(k, t...) +
57 tbb::parallel_reduce(
58 tbb::blocked_range<uint>(0, grid_size), NT(0),
59 [&](const tbb::blocked_range<uint> &r, NT value) -> NT {
60 for (uint idx_x = r.begin(); idx_x != r.end(); ++idx_x) {
61 const ctype q = k * sqrt(x_quadrature_p[idx_x] * x_extent);
62 const ctype int_element = S_d // solid nd angle
63 * (powr<d - 2>(q) / (ctype)2 * powr<2>(k)) // x = p^2 / k^2 integral
64 / powr<d>(2 * (ctype)M_PI); // fourier factor
65 const ctype weight = x_quadrature_w[idx_x] * x_extent;
66 value += int_element * weight * KERNEL::kernel(q, k, t...);
67 }
68 return value;
69 },
70 [&](NT x, NT y) -> NT { return x + y; });
71 }
72
83 template <typename... T> std::future<NT> request(const ctype k, const T &...t) const
84 {
85 return std::async(std::launch::deferred, [=, this]() { return get(k, t...); });
86 }
87
88 private:
90
92
93 const std::vector<ctype> &x_quadrature_p;
94 const std::vector<ctype> &x_quadrature_w;
95 };
96} // namespace DiFfRG
Definition integrator_cpu.hh:15
const std::vector< ctype > & x_quadrature_p
Definition integrator_cpu.hh:93
typename get_type::ctype< NT > ctype
Numerical type to be used for integration tasks e.g. the argument or possible jacobians.
Definition integrator_cpu.hh:20
NT get(const ctype k, const T &...t) const
Get the integral of the kernel.
Definition integrator_cpu.hh:51
IntegratorTBB(QuadratureProvider &quadrature_provider, const std::array< uint, 1 > grid_size, const ctype x_extent, const uint max_block_size=0)
Definition integrator_cpu.hh:21
const std::vector< ctype > & x_quadrature_w
Definition integrator_cpu.hh:94
IntegratorTBB(QuadratureProvider &quadrature_provider, const uint grid_size, const ctype x_extent, const uint max_block_size=0)
Definition integrator_cpu.hh:33
const uint grid_size
Definition integrator_cpu.hh:89
std::future< NT > request(const ctype k, const T &...t) const
Request a future for the integral of the kernel.
Definition integrator_cpu.hh:83
const ctype x_extent
Definition integrator_cpu.hh:91
IntegratorTBB(QuadratureProvider &quadrature_provider, const std::array< uint, 1 > grid_size, const ctype x_extent, const JSONValue &)
Definition integrator_cpu.hh:27
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
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