DiFfRG
Loading...
Searching...
No Matches
integrator_2D_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{
20 template <typename NT, typename KERNEL> class Integrator2DCartesianTBB
21 {
22 public:
26 using ctype = typename get_type::ctype<NT>;
27
37 Integrator2DCartesianTBB(QuadratureProvider &quadrature_provider, const std::array<uint, 2> grid_sizes,
38 const ctype x_extent, const JSONValue &json)
39 : Integrator2DCartesianTBB(quadrature_provider, grid_sizes, x_extent, 0,
40 json.get_double("/discretization/integration/qx_min", -M_PI),
41 json.get_double("/discretization/integration/qy_min", -M_PI),
42 json.get_double("/discretization/integration/qx_max", M_PI),
43 json.get_double("/discretization/integration/qy_max", M_PI))
44 {
45 }
46
61 Integrator2DCartesianTBB(QuadratureProvider &quadrature_provider, std::array<uint, 2> grid_sizes,
62 const ctype x_extent, const uint max_block_size = 0, const ctype qx_min = -M_PI,
63 const ctype qy_min = -M_PI, const ctype qx_max = M_PI, const ctype qy_max = M_PI)
64 : grid_sizes(grid_sizes), x_quadrature_p(quadrature_provider.get_points<ctype>(grid_sizes[0])),
65 x_quadrature_w(quadrature_provider.get_weights<ctype>(grid_sizes[0])),
66 y_quadrature_p(quadrature_provider.get_points<ctype>(grid_sizes[1])),
67 y_quadrature_w(quadrature_provider.get_weights<ctype>(grid_sizes[1]))
68 {
69 (void)max_block_size;
70 this->qx_min = qx_min;
71 this->qy_min = qy_min;
72 this->qx_extent = qx_max - qx_min;
73 this->qy_extent = qy_max - qy_min;
74 }
75
80 {
81 this->qx_extent = this->qx_extent - qx_min + this->qx_min;
82 this->qx_min = qx_min;
83 }
84
89 {
90 this->qy_extent = this->qy_extent - qy_min + this->qy_min;
91 this->qy_min = qy_min;
92 }
93
97 void set_qx_max(const ctype qx_max) { this->qx_extent = qx_max - qx_min; }
98
102 void set_qy_max(const ctype qy_max) { this->qy_extent = qy_max - qy_min; }
103
111 template <typename... T> NT get(const ctype k, const T &...t) const
112 {
113 constexpr int d = 2;
114 constexpr ctype int_element = powr<-d>(2 * (ctype)M_PI); // fourier factor
115
116 const auto constant = KERNEL::constant(k, t...);
117 return constant + tbb::parallel_reduce(
118 tbb::blocked_range2d<uint, uint>(0, grid_sizes[0], 0, grid_sizes[1]), NT(0.),
119 [&](const tbb::blocked_range2d<uint, uint> &r, NT value) -> NT {
120 for (uint idx_x = r.rows().begin(); idx_x != r.rows().end(); ++idx_x) {
121 const ctype qx = qx_min + qx_extent * x_quadrature_p[idx_x];
122 for (uint idx_y = r.cols().begin(); idx_y != r.cols().end(); ++idx_y) {
123 const ctype qy = qy_min + qy_extent * y_quadrature_p[idx_y];
124
125 const ctype weight =
127 value += int_element * weight * KERNEL::kernel(qx, qy, k, t...);
128 }
129 }
130 return value;
131 },
132 [&](NT x, NT y) -> NT { return x + y; });
133 }
134
142 template <typename... T> std::future<NT> request(const ctype k, const T &...t) const
143 {
144 return std::async(std::launch::deferred, [=, this]() { return get(k, t...); });
145 }
146
147 private:
148 const std::array<uint, 2> grid_sizes;
149
150 ctype qx_min = -M_PI;
151 ctype qy_min = -M_PI;
152 ctype qx_extent = 2 * M_PI;
153 ctype qy_extent = 2 * M_PI;
154
155 const std::vector<ctype> &x_quadrature_p;
156 const std::vector<ctype> &x_quadrature_w;
157 const std::vector<ctype> &y_quadrature_p;
158 const std::vector<ctype> &y_quadrature_w;
159 };
160} // namespace DiFfRG
Integration of an arbitrary 2D function from (qx_min, qy_min) to (qx_max, qy_max) using TBB.
Definition integrator_2D_cartesian_cpu.hh:21
const std::vector< ctype > & x_quadrature_w
Definition integrator_2D_cartesian_cpu.hh:156
NT get(const ctype k, const T &...t) const
Get the value of the integral.
Definition integrator_2D_cartesian_cpu.hh:111
void set_qx_min(const ctype qx_min)
Set the minimum value of the qx integration range.
Definition integrator_2D_cartesian_cpu.hh:79
ctype qx_extent
Definition integrator_2D_cartesian_cpu.hh:152
std::future< NT > request(const ctype k, const T &...t) const
Get the value of the integral asynchronously.
Definition integrator_2D_cartesian_cpu.hh:142
typename get_type::ctype< NT > ctype
Numerical type to be used for integration tasks e.g. the argument or possible jacobians.
Definition integrator_2D_cartesian_cpu.hh:26
void set_qx_max(const ctype qx_max)
Set the maximum value of the qx integration range.
Definition integrator_2D_cartesian_cpu.hh:97
Integrator2DCartesianTBB(QuadratureProvider &quadrature_provider, const std::array< uint, 2 > grid_sizes, const ctype x_extent, const JSONValue &json)
Construct a new Integrator2DCartesianTBB object.
Definition integrator_2D_cartesian_cpu.hh:37
const std::vector< ctype > & y_quadrature_p
Definition integrator_2D_cartesian_cpu.hh:157
const std::array< uint, 2 > grid_sizes
Definition integrator_2D_cartesian_cpu.hh:148
const std::vector< ctype > & y_quadrature_w
Definition integrator_2D_cartesian_cpu.hh:158
Integrator2DCartesianTBB(QuadratureProvider &quadrature_provider, std::array< uint, 2 > grid_sizes, const ctype x_extent, const uint max_block_size=0, const ctype qx_min=-M_PI, const ctype qy_min=-M_PI, const ctype qx_max=M_PI, const ctype qy_max=M_PI)
Construct a new Integrator2DCartesianTBB object.
Definition integrator_2D_cartesian_cpu.hh:61
void set_qy_min(const ctype qy_min)
Set the minimum value of the qy integration range.
Definition integrator_2D_cartesian_cpu.hh:88
void set_qy_max(const ctype qy_max)
Set the maximum value of the qy integration range.
Definition integrator_2D_cartesian_cpu.hh:102
ctype qy_extent
Definition integrator_2D_cartesian_cpu.hh:153
ctype qx_min
Definition integrator_2D_cartesian_cpu.hh:150
ctype qy_min
Definition integrator_2D_cartesian_cpu.hh:151
const std::vector< ctype > & x_quadrature_p
Definition integrator_2D_cartesian_cpu.hh:155
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