DiFfRG
Loading...
Searching...
No Matches
integrator_angle_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{
24 template <int d, typename NT, typename KERNEL> class IntegratorAngleTBB
25 {
26 public:
30 using ctype = typename get_type::ctype<NT>;
31
32 IntegratorAngleTBB(QuadratureProvider &quadrature_provider, const std::array<uint, 2> grid_sizes,
33 const ctype x_extent, const JSONValue &)
34 : IntegratorAngleTBB(quadrature_provider, grid_sizes, x_extent)
35 {
36 }
37
38 IntegratorAngleTBB(QuadratureProvider &quadrature_provider, std::array<uint, 2> grid_sizes, const ctype x_extent,
39 const uint max_block_size = 0)
41 x_quadrature_p(quadrature_provider.get_points<ctype>(grid_sizes[0])),
42 x_quadrature_w(quadrature_provider.get_weights<ctype>(grid_sizes[0])),
43 ang_quadrature_p(quadrature_provider.get_points<ctype>(grid_sizes[1])),
44 ang_quadrature_w(quadrature_provider.get_weights<ctype>(grid_sizes[1]))
45 {
46 (void)max_block_size;
47 }
48
59 template <typename... T> NT get(const ctype k, const T &...t) const
60 {
61 constexpr ctype S_d = S_d_prec<ctype>(d);
62 using std::sqrt;
63
64 const auto constant = KERNEL::constant(k, t...);
65 return constant + tbb::parallel_reduce(
66 tbb::blocked_range2d<uint, uint>(0, grid_sizes[0], 0, grid_sizes[1]), NT(0.),
67 [&](const tbb::blocked_range2d<uint, uint> &r, NT value) -> NT {
68 for (uint idx_x = r.rows().begin(); idx_x != r.rows().end(); ++idx_x) {
69 const ctype q = k * sqrt(x_quadrature_p[idx_x] * x_extent);
70 for (uint idx_y = r.cols().begin(); idx_y != r.cols().end(); ++idx_y) {
71 const ctype cos = 2 * (ang_quadrature_p[idx_y] - (ctype)0.5);
72 const ctype int_element =
73 S_d // solid nd angle
74 * (powr<d - 2>(q) / (ctype)2 * powr<2>(k)) // x = p^2 / k^2 integral
75 * (1 / (ctype)2) // divide the cos integral out
76 / powr<d>(2 * (ctype)M_PI); // fourier factor
77 const ctype weight = 2 * ang_quadrature_w[idx_y] * x_quadrature_w[idx_x] * x_extent;
78 value += int_element * weight * KERNEL::kernel(q, cos, k, t...);
79 }
80 }
81 return value;
82 },
83 [&](NT x, NT y) -> NT { return x + y; });
84 }
85
96 template <typename... T> std::future<NT> request(const ctype k, const T &...t) const
97 {
98 return std::async(std::launch::deferred, [=, this]() { return get(k, t...); });
99 }
100
101 private:
102 const std::array<uint, 2> grid_sizes;
103
105
106 const std::vector<ctype> &x_quadrature_p;
107 const std::vector<ctype> &x_quadrature_w;
108 const std::vector<ctype> &ang_quadrature_p;
109 const std::vector<ctype> &ang_quadrature_w;
110 };
111} // namespace DiFfRG
Integrator for the integration of a function with one angle with TBB. Calculates.
Definition integrator_angle_cpu.hh:25
const std::array< uint, 2 > grid_sizes
Definition integrator_angle_cpu.hh:102
NT get(const ctype k, const T &...t) const
Get the integral of the kernel.
Definition integrator_angle_cpu.hh:59
const std::vector< ctype > & x_quadrature_p
Definition integrator_angle_cpu.hh:106
IntegratorAngleTBB(QuadratureProvider &quadrature_provider, const std::array< uint, 2 > grid_sizes, const ctype x_extent, const JSONValue &)
Definition integrator_angle_cpu.hh:32
std::future< NT > request(const ctype k, const T &...t) const
Request a future for the integral of the kernel.
Definition integrator_angle_cpu.hh:96
const std::vector< ctype > & ang_quadrature_p
Definition integrator_angle_cpu.hh:108
const std::vector< ctype > & ang_quadrature_w
Definition integrator_angle_cpu.hh:109
typename get_type::ctype< NT > ctype
Numerical type to be used for integration tasks e.g. the argument or possible jacobians.
Definition integrator_angle_cpu.hh:30
const ctype x_extent
Definition integrator_angle_cpu.hh:104
const std::vector< ctype > & x_quadrature_w
Definition integrator_angle_cpu.hh:107
IntegratorAngleTBB(QuadratureProvider &quadrature_provider, std::array< uint, 2 > grid_sizes, const ctype x_extent, const uint max_block_size=0)
Definition integrator_angle_cpu.hh:38
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