8 #include <Kokkos_Core.hpp>
10 #include "ddc_alias_inline_functions.hpp"
11 #include "ddc_aliases.hpp"
12 #include "ddc_helper.hpp"
25 class IdxRangeQuadrature,
26 class IdxRangeTotal = IdxRangeQuadrature,
27 class MemorySpace = Kokkos::DefaultExecutionSpace::memory_space>
32 using IdxQuadrature =
typename IdxRangeQuadrature::discrete_element_type;
34 using QuadConstField = DConstField<IdxRangeQuadrature, MemorySpace>;
36 QuadConstField m_coefficients;
44 explicit Quadrature(QuadConstField coeffs) : m_coefficients(coeffs) {}
59 template <
class ExecutionSpace,
class IntegratorFunction>
60 double operator()(ExecutionSpace exec_space, IntegratorFunction integrated_function)
const
63 Kokkos::SpaceAccessibility<ExecutionSpace, MemorySpace>::accessible,
64 "Execution space is not compatible with memory space where coefficients are found");
66 std::is_invocable_v<IntegratorFunction, IdxQuadrature>,
67 "The object passed to Quadrature::operator() is not defined on the quadrature "
70 QuadConstField
const coeff_proxy = m_coefficients;
76 if constexpr (std::is_same_v<ExecutionSpace, Kokkos::DefaultHostExecutionSpace>) {
77 return ddc::transform_reduce(
78 get_idx_range(coeff_proxy),
80 ddc::reducer::sum<double>(),
81 KOKKOS_LAMBDA(IdxQuadrature
const ix) {
82 return coeff_proxy(ix) * integrated_function(ix);
85 return ddc::parallel_transform_reduce(
87 get_idx_range(coeff_proxy),
89 ddc::reducer::sum<double>(),
90 KOKKOS_LAMBDA(IdxQuadrature
const ix) {
91 return coeff_proxy(ix) * integrated_function(ix);
111 template <
class ExecutionSpace,
class BatchIdxRange,
class IntegratorFunction>
113 ExecutionSpace exec_space,
114 Field<double, BatchIdxRange, MemorySpace>
const result,
115 IntegratorFunction integrated_function)
const
118 Kokkos::SpaceAccessibility<ExecutionSpace, MemorySpace>::accessible,
119 "Execution space is not compatible with memory space where coefficients are found");
121 std::is_same_v<ExecutionSpace, Kokkos::DefaultExecutionSpace>,
122 "Kokkos::TeamPolicy only works with the default execution space. Please use "
123 "DefaultExecutionSpace to call this batched operator.");
124 using ExpectedBatchDims = ddc::type_seq_remove_t<
125 ddc::to_type_seq_t<IdxRangeTotal>,
126 ddc::to_type_seq_t<IdxRangeQuadrature>>;
128 ddc::type_seq_same_v<ddc::to_type_seq_t<BatchIdxRange>, ExpectedBatchDims>,
129 "The batch idx_range deduced from the type of result does not match the class "
130 "template parameters.");
133 using IdxTotal =
typename IdxRangeTotal::discrete_element_type;
134 using IdxBatch =
typename BatchIdxRange::discrete_element_type;
137 std::is_invocable_v<IntegratorFunction, IdxTotal>,
138 "The object passed to Quadrature::operator() is not defined on the total "
142 IdxRangeQuadrature quad_idx_range(get_idx_range(m_coefficients));
143 BatchIdxRange batch_idx_range(get_idx_range(result));
145 QuadConstField
const coeff_proxy = m_coefficients;
147 Kokkos::parallel_for(
148 Kokkos::TeamPolicy<>(exec_space, batch_idx_range.size(), Kokkos::AUTO),
149 KOKKOS_LAMBDA(
const Kokkos::TeamPolicy<>::member_type& team) {
150 const int idx = team.league_rank();
151 IdxBatch ib = to_discrete_element(idx, batch_idx_range);
155 Kokkos::parallel_reduce(
156 Kokkos::TeamThreadRange(team, quad_idx_range.size()),
157 [&](
int const& thread_index,
double& sum) {
159 = to_discrete_element(thread_index, quad_idx_range);
161 sum += coeff_proxy(iq) * integrated_function(it);
164 result(ib) = teamSum;
179 template <
class HeadDim,
class... Grid1D>
180 KOKKOS_FUNCTION
static Idx<HeadDim, Grid1D...> to_discrete_element(
182 IdxRange<HeadDim, Grid1D...> idx_range)
184 IdxRange<Grid1D...> subidx_range(idx_range);
185 Idx<HeadDim> head_idx(ddc::select<HeadDim>(idx_range).front() + idx / subidx_range.size());
186 if constexpr (
sizeof...(Grid1D) == 0) {
189 Idx<Grid1D...> tail_idx = to_discrete_element(idx % subidx_range.size(), subidx_range);
190 return Idx<HeadDim, Grid1D...>(head_idx, tail_idx);
196 template <
class NewMemorySpace,
class IdxRangeQuadrature,
class IdxRangeTotal,
class MemorySpace>
197 struct OnMemorySpace<NewMemorySpace,
Quadrature<IdxRangeQuadrature, IdxRangeTotal, MemorySpace>>
A class providing an operator for integrating functions defined on a discrete index range.
Definition: quadrature.hpp:29
double operator()(ExecutionSpace exec_space, IntegratorFunction integrated_function) const
An operator for calculating the integral of a function defined on a discrete index range.
Definition: quadrature.hpp:60
void operator()(ExecutionSpace exec_space, Field< double, BatchIdxRange, MemorySpace > const result, IntegratorFunction integrated_function) const
An operator for calculating the integral of a function defined on a discrete index range by cycling o...
Definition: quadrature.hpp:112
Quadrature(QuadConstField coeffs)
Create a Quadrature object.
Definition: quadrature.hpp:44