DiFfRG
Loading...
Searching...
No Matches
integrator_3D_cartesian_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 <typename NT, typename KERNEL> class Integrator3DCartesianTBB
15 {
16 public:
20 using ctype = typename get_type::ctype<NT>;
21
22 Integrator3DCartesianTBB(QuadratureProvider &quadrature_provider, const std::array<uint, 3> grid_sizes,
23 const ctype x_extent, const JSONValue &json)
24 : Integrator3DCartesianTBB(quadrature_provider, grid_sizes, x_extent, 0,
25 json.get_double("/discretization/integration/qx_min", -M_PI),
26 json.get_double("/discretization/integration/qy_min", -M_PI),
27 json.get_double("/discretization/integration/qz_min", -M_PI),
28 json.get_double("/discretization/integration/qx_max", M_PI),
29 json.get_double("/discretization/integration/qy_max", M_PI),
30 json.get_double("/discretization/integration/qz_max", M_PI))
31 {
32 }
33
34 Integrator3DCartesianTBB(QuadratureProvider &quadrature_provider, std::array<uint, 3> grid_sizes,
35 const ctype x_extent = 0., const uint max_block_size = 0, const ctype qx_min = -M_PI,
36 const ctype qy_min = -M_PI, const ctype qz_min = -M_PI, const ctype qx_max = M_PI,
37 const ctype qy_max = M_PI, const ctype qz_max = M_PI)
38 : grid_sizes(grid_sizes), x_quadrature_p(quadrature_provider.get_points<ctype>(grid_sizes[0])),
39 x_quadrature_w(quadrature_provider.get_weights<ctype>(grid_sizes[0])),
40 y_quadrature_p(quadrature_provider.get_points<ctype>(grid_sizes[1])),
41 y_quadrature_w(quadrature_provider.get_weights<ctype>(grid_sizes[1])),
42 z_quadrature_p(quadrature_provider.get_points<ctype>(grid_sizes[2])),
43 z_quadrature_w(quadrature_provider.get_weights<ctype>(grid_sizes[2]))
44 {
45 (void)max_block_size;
46 (void)x_extent;
47 this->qx_min = qx_min;
48 this->qy_min = qy_min;
49 this->qz_min = qz_min;
50 this->qx_extent = qx_max - qx_min;
51 this->qy_extent = qy_max - qy_min;
52 this->qz_extent = qz_max - qz_min;
53 }
54
59 {
60 this->qx_extent = this->qx_extent - qx_min + this->qx_min;
61 this->qx_min = qx_min;
62 }
63
68 {
69 this->qy_extent = this->qy_extent - qy_min + this->qy_min;
70 this->qy_min = qy_min;
71 }
72
77 {
78 this->qz_extent = this->qz_extent - qz_min + this->qz_min;
79 this->qz_min = qz_min;
80 }
81
85 void set_qx_max(const ctype qx_max) { this->qx_extent = qx_max - qx_min; }
86
90 void set_qy_max(const ctype qy_max) { this->qy_extent = qy_max - qy_min; }
91
95 void set_qz_max(const ctype qz_max) { this->qz_extent = qz_max - qz_min; }
96
97 template <typename... T> NT get(const ctype k, const T &...t) const
98 {
99 constexpr int d = 3;
100 constexpr ctype int_element = powr<-d>(2 * (ctype)M_PI); // fourier factor
101
102 const auto constant = KERNEL::constant(k, t...);
103 return constant +
104 tbb::parallel_reduce(
105 tbb::blocked_range3d<uint, uint, uint>(0, grid_sizes[0], 0, grid_sizes[1], 0, grid_sizes[2]), NT(0.),
106 [&](const tbb::blocked_range3d<uint, uint, uint> &r, NT value) -> NT {
107 for (uint idx_x = r.pages().begin(); idx_x != r.pages().end(); ++idx_x) {
108 const ctype qx = qx_min + qx_extent * x_quadrature_p[idx_x];
109 for (uint idx_y = r.rows().begin(); idx_y != r.rows().end(); ++idx_y) {
110 const ctype qy = qy_min + qy_extent * y_quadrature_p[idx_y];
111 for (uint idx_z = r.cols().begin(); idx_z != r.cols().end(); ++idx_z) {
112 const ctype qz = qz_min + qz_extent * z_quadrature_p[idx_z];
113
114 const ctype weight = qz_extent * z_quadrature_w[idx_z] * qy_extent * y_quadrature_w[idx_y] *
115 qx_extent * x_quadrature_w[idx_x];
116 value += int_element * weight * KERNEL::kernel(qx, qy, qz, k, t...);
117 }
118 }
119 }
120 return value;
121 },
122 [&](NT x, NT y) -> NT { return x + y; });
123 }
124
135 template <typename... T> std::future<NT> request(const ctype k, const T &...t) const
136 {
137 return std::async(std::launch::deferred, [=, this]() { return get(k, t...); });
138 }
139
140 private:
141 const std::array<uint, 3> grid_sizes;
142
143 ctype qx_min = -M_PI;
144 ctype qy_min = -M_PI;
145 ctype qx_extent = 2 * M_PI;
146 ctype qy_extent = 2 * M_PI;
147 ctype qz_min = -M_PI;
148 ctype qz_extent = 2 * M_PI;
149
150 const std::vector<ctype> &x_quadrature_p;
151 const std::vector<ctype> &x_quadrature_w;
152 const std::vector<ctype> &y_quadrature_p;
153 const std::vector<ctype> &y_quadrature_w;
154 const std::vector<ctype> &z_quadrature_p;
155 const std::vector<ctype> &z_quadrature_w;
156 };
157} // namespace DiFfRG
Definition integrator_3D_cartesian_cpu.hh:15
const std::vector< ctype > & x_quadrature_w
Definition integrator_3D_cartesian_cpu.hh:151
void set_qx_max(const ctype qx_max)
Set the maximum value of the qx integration range.
Definition integrator_3D_cartesian_cpu.hh:85
ctype qz_extent
Definition integrator_3D_cartesian_cpu.hh:148
typename get_type::ctype< NT > ctype
Numerical type to be used for integration tasks e.g. the argument or possible jacobians.
Definition integrator_3D_cartesian_cpu.hh:20
ctype qy_min
Definition integrator_3D_cartesian_cpu.hh:144
Integrator3DCartesianTBB(QuadratureProvider &quadrature_provider, const std::array< uint, 3 > grid_sizes, const ctype x_extent, const JSONValue &json)
Definition integrator_3D_cartesian_cpu.hh:22
void set_qx_min(const ctype qx_min)
Set the minimum value of the qx integration range.
Definition integrator_3D_cartesian_cpu.hh:58
ctype qx_extent
Definition integrator_3D_cartesian_cpu.hh:145
void set_qy_min(const ctype qy_min)
Set the minimum value of the qy integration range.
Definition integrator_3D_cartesian_cpu.hh:67
void set_qz_min(const ctype qz_min)
Set the minimum value of the qz integration range.
Definition integrator_3D_cartesian_cpu.hh:76
ctype qx_min
Definition integrator_3D_cartesian_cpu.hh:143
ctype qy_extent
Definition integrator_3D_cartesian_cpu.hh:146
const std::vector< ctype > & y_quadrature_w
Definition integrator_3D_cartesian_cpu.hh:153
NT get(const ctype k, const T &...t) const
Definition integrator_3D_cartesian_cpu.hh:97
void set_qz_max(const ctype qz_max)
Set the maximum value of the qz integration range.
Definition integrator_3D_cartesian_cpu.hh:95
const std::array< uint, 3 > grid_sizes
Definition integrator_3D_cartesian_cpu.hh:141
Integrator3DCartesianTBB(QuadratureProvider &quadrature_provider, std::array< uint, 3 > grid_sizes, const ctype x_extent=0., const uint max_block_size=0, const ctype qx_min=-M_PI, const ctype qy_min=-M_PI, const ctype qz_min=-M_PI, const ctype qx_max=M_PI, const ctype qy_max=M_PI, const ctype qz_max=M_PI)
Definition integrator_3D_cartesian_cpu.hh:34
std::future< NT > request(const ctype k, const T &...t) const
Request a future for the integral of the kernel.
Definition integrator_3D_cartesian_cpu.hh:135
ctype qz_min
Definition integrator_3D_cartesian_cpu.hh:147
const std::vector< ctype > & y_quadrature_p
Definition integrator_3D_cartesian_cpu.hh:152
const std::vector< ctype > & z_quadrature_p
Definition integrator_3D_cartesian_cpu.hh:154
const std::vector< ctype > & z_quadrature_w
Definition integrator_3D_cartesian_cpu.hh:155
const std::vector< ctype > & x_quadrature_p
Definition integrator_3D_cartesian_cpu.hh:150
void set_qy_max(const ctype qy_max)
Set the maximum value of the qy integration range.
Definition integrator_3D_cartesian_cpu.hh:90
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
unsigned int uint
Definition utils.hh:22