Skip to content

Commit

Permalink
Simplify the allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
tpadioleau committed Jan 1, 2025
1 parent 9743d94 commit 6e2ea0d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 80 deletions.
129 changes: 58 additions & 71 deletions tests/splines/batched_2d_spline_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <algorithm>
#include <cstddef>

#include "ddc/discrete_domain.hpp"
#if defined(BC_HERMITE)
#include <optional>
#endif
Expand Down Expand Up @@ -209,35 +211,38 @@ struct BatchDimsInitializerFn<ddc::detail::TypeSeq<DDims...>>
template <typename ExecSpace, typename MemorySpace, typename I1, typename I2, typename... X>
void Batched2dSplineTest()
{
using DDim1 = DDim<I1, I1, I2>;
using DDim2 = DDim<I2, I1, I2>;
using DElemAll = DElem<DDim<X, I1, I2>...>;

// Instantiate execution spaces and initialize spaces
Kokkos::DefaultHostExecutionSpace const host_exec_space;
ExecSpace const exec_space;
std::size_t const ncells = 10;
InterestDimInitializer<DDim<I1, I1, I2>>(ncells);
InterestDimInitializer<DDim<I2, I1, I2>>(ncells);
BatchDimsInitializerFn<BatchDims<DDim<I1, I1, I2>, DDim<I2, I1, I2>, DDim<X, I1, I2>...>> const
InterestDimInitializer<DDim1>(ncells);
InterestDimInitializer<DDim2>(ncells);
BatchDimsInitializerFn<BatchDims<DDim1, DDim2, DDim<X, I1, I2>...>> const
batch_dims_initializer;
batch_dims_initializer(ncells);

// Create the values domain (mesh)
ddc::DiscreteDomain<DDim<I1, I1, I2>> const interpolation_domain1
= GrevillePoints<BSplines<I1>>::template get_domain<DDim<I1, I1, I2>>();
ddc::DiscreteDomain<DDim<I2, I1, I2>> const interpolation_domain2
= GrevillePoints<BSplines<I2>>::template get_domain<DDim<I2, I1, I2>>();
ddc::DiscreteDomain<DDim<I1, I1, I2>, DDim<I2, I1, I2>> const
ddc::DiscreteDomain<DDim1> const interpolation_domain1
= GrevillePoints<BSplines<I1>>::template get_domain<DDim1>();
ddc::DiscreteDomain<DDim2> const interpolation_domain2
= GrevillePoints<BSplines<I2>>::template get_domain<DDim2>();
ddc::DiscreteDomain<DDim1, DDim2> const
interpolation_domain(interpolation_domain1, interpolation_domain2);
// If we remove auto using the constructor syntax, nvcc does not compile
auto const dom_vals_tmp = ddc::DiscreteDomain<DDim<X, void, void>...>(
ddc::DiscreteDomain<DDim<
X,
void,
void>>(DElem<DDim<X, void, void>>(0), DVect<DDim<X, void, void>>(ncells))...);
ddc::DiscreteDomain<DDim<X, I1, I2>...> const dom_vals
= ddc::replace_dim_of<DDim<I1, void, void>, DDim<I1, I1, I2>>(
ddc::replace_dim_of<
DDim<I2, void, void>,
DDim<I2, I1, I2>>(dom_vals_tmp, interpolation_domain),
interpolation_domain);
ddc::DiscreteDomain<DDim<X, I1, I2>...> const dom_vals = ddc::replace_dim_of<
DDim<I1, void, void>,
DDim1>(
ddc::replace_dim_of<DDim<I2, void, void>, DDim2>(dom_vals_tmp, interpolation_domain),
interpolation_domain);

#if defined(BC_HERMITE)
// Create the derivs domain
Expand All @@ -248,12 +253,10 @@ void Batched2dSplineTest()
ddc::DiscreteDomain<ddc::Deriv<I1>, ddc::Deriv<I2>> const
derivs_domain(derivs_domain1, derivs_domain2);

auto const dom_derivs_1d
= ddc::replace_dim_of<DDim<I1, I1, I2>, ddc::Deriv<I1>>(dom_vals, derivs_domain1);
auto const dom_derivs2
= ddc::replace_dim_of<DDim<I2, I1, I2>, ddc::Deriv<I2>>(dom_vals, derivs_domain2);
auto const dom_derivs_1d = ddc::replace_dim_of<DDim1, ddc::Deriv<I1>>(dom_vals, derivs_domain1);
auto const dom_derivs2 = ddc::replace_dim_of<DDim2, ddc::Deriv<I2>>(dom_vals, derivs_domain2);
auto const dom_derivs
= ddc::replace_dim_of<DDim<I2, I1, I2>, ddc::Deriv<I2>>(dom_derivs_1d, derivs_domain2);
= ddc::replace_dim_of<DDim2, ddc::Deriv<I2>>(dom_derivs_1d, derivs_domain2);
#endif

// Create a SplineBuilder over BSplines<I> and batched along other dimensions using some boundary conditions
Expand All @@ -262,8 +265,8 @@ void Batched2dSplineTest()
MemorySpace,
BSplines<I1>,
BSplines<I2>,
DDim<I1, I1, I2>,
DDim<I2, I1, I2>,
DDim1,
DDim2,
s_bcl,
s_bcr,
s_bcl,
Expand All @@ -272,16 +275,14 @@ void Batched2dSplineTest()
DDim<X, I1, I2>...> const spline_builder(dom_vals);

// Compute usefull domains (dom_interpolation, dom_batch, dom_bsplines and dom_spline)
ddc::DiscreteDomain<DDim<I1, I1, I2>, DDim<I2, I1, I2>> const dom_interpolation
ddc::DiscreteDomain<DDim1, DDim2> const dom_interpolation
= spline_builder.interpolation_domain();
auto const dom_spline = spline_builder.batched_spline_domain();

// Allocate and fill a chunk containing values to be passed as input to spline_builder. Those are values of cosine along interest dimension duplicated along batch dimensions
ddc::Chunk vals_1d_host_alloc(
dom_interpolation,
ddc::KokkosAllocator<double, Kokkos::DefaultHostExecutionSpace::memory_space>());
ddc::Chunk vals_1d_host_alloc(dom_interpolation, ddc::HostAllocator<double>());
ddc::ChunkSpan const vals_1d_host = vals_1d_host_alloc.span_view();
evaluator_type<DDim<I1, I1, I2>, DDim<I2, I1, I2>> const evaluator(dom_interpolation);
evaluator_type<DDim1, DDim2> const evaluator(dom_interpolation);
evaluator(vals_1d_host);
auto vals_1d_alloc = ddc::create_mirror_view_and_copy(exec_space, vals_1d_host);
ddc::ChunkSpan const vals_1d = vals_1d_alloc.span_view();
Expand All @@ -291,9 +292,7 @@ void Batched2dSplineTest()
ddc::parallel_for_each(
exec_space,
vals.domain(),
KOKKOS_LAMBDA(DElem<DDim<X, I1, I2>...> const e) {
vals(e) = vals_1d(DElem<DDim<I1, I1, I2>, DDim<I2, I1, I2>>(e));
});
KOKKOS_LAMBDA(DElemAll const e) { vals(e) = vals_1d(DElem<DDim1, DDim2>(e)); });

#if defined(BC_HERMITE)
// Allocate and fill a chunk containing derivs to be passed as input to spline_builder.
Expand All @@ -302,16 +301,14 @@ void Batched2dSplineTest()
ddc::ChunkSpan const derivs_1d_lhs = derivs_1d_lhs_alloc.span_view();
if (s_bcl == ddc::BoundCond::HERMITE) {
ddc::Chunk derivs_1d_lhs1_host_alloc(
ddc::DiscreteDomain<
ddc::Deriv<I1>,
DDim<I2, I1, I2>>(derivs_domain1, interpolation_domain2),
ddc::DiscreteDomain<ddc::Deriv<I1>, DDim2>(derivs_domain1, interpolation_domain2),
ddc::HostAllocator<double>());
ddc::ChunkSpan const derivs_1d_lhs1_host = derivs_1d_lhs1_host_alloc.span_view();
ddc::for_each(
derivs_1d_lhs1_host.domain(),
KOKKOS_LAMBDA(ddc::DiscreteElement<ddc::Deriv<I1>, DDim<I2, I1, I2>> const e) {
KOKKOS_LAMBDA(ddc::DiscreteElement<ddc::Deriv<I1>, DDim2> const e) {
auto deriv_idx = ddc::DiscreteElement<ddc::Deriv<I1>>(e).uid();
auto x2 = ddc::coordinate(ddc::DiscreteElement<DDim<I2, I1, I2>>(e));
auto x2 = ddc::coordinate(ddc::DiscreteElement<DDim2>(e));
derivs_1d_lhs1_host(e)
= evaluator.deriv(x0<I1>(), x2, deriv_idx + shift - 1, 0);
});
Expand All @@ -324,24 +321,22 @@ void Batched2dSplineTest()
derivs_1d_lhs.domain(),
KOKKOS_LAMBDA(
typename decltype(derivs_1d_lhs.domain())::discrete_element_type const e) {
derivs_1d_lhs(e) = derivs_1d_lhs1(DElem<ddc::Deriv<I1>, DDim<I2, I1, I2>>(e));
derivs_1d_lhs(e) = derivs_1d_lhs1(DElem<ddc::Deriv<I1>, DDim2>(e));
});
}

ddc::Chunk derivs_1d_rhs_alloc(dom_derivs_1d, ddc::KokkosAllocator<double, MemorySpace>());
ddc::ChunkSpan const derivs_1d_rhs = derivs_1d_rhs_alloc.span_view();
if (s_bcl == ddc::BoundCond::HERMITE) {
ddc::Chunk derivs_1d_rhs1_host_alloc(
ddc::DiscreteDomain<
ddc::Deriv<I1>,
DDim<I2, I1, I2>>(derivs_domain1, interpolation_domain2),
ddc::DiscreteDomain<ddc::Deriv<I1>, DDim2>(derivs_domain1, interpolation_domain2),
ddc::HostAllocator<double>());
ddc::ChunkSpan const derivs_1d_rhs1_host = derivs_1d_rhs1_host_alloc.span_view();
ddc::for_each(
derivs_1d_rhs1_host.domain(),
KOKKOS_LAMBDA(ddc::DiscreteElement<ddc::Deriv<I1>, DDim<I2, I1, I2>> const e) {
KOKKOS_LAMBDA(ddc::DiscreteElement<ddc::Deriv<I1>, DDim2> const e) {
auto deriv_idx = ddc::DiscreteElement<ddc::Deriv<I1>>(e).uid();
auto x2 = ddc::coordinate(ddc::DiscreteElement<DDim<I2, I1, I2>>(e));
auto x2 = ddc::coordinate(ddc::DiscreteElement<DDim2>(e));
derivs_1d_rhs1_host(e)
= evaluator.deriv(xN<I1>(), x2, deriv_idx + shift - 1, 0);
});
Expand All @@ -354,23 +349,21 @@ void Batched2dSplineTest()
derivs_1d_rhs.domain(),
KOKKOS_LAMBDA(
typename decltype(derivs_1d_rhs.domain())::discrete_element_type const e) {
derivs_1d_rhs(e) = derivs_1d_rhs1(DElem<ddc::Deriv<I1>, DDim<I2, I1, I2>>(e));
derivs_1d_rhs(e) = derivs_1d_rhs1(DElem<ddc::Deriv<I1>, DDim2>(e));
});
}

ddc::Chunk derivs2_lhs_alloc(dom_derivs2, ddc::KokkosAllocator<double, MemorySpace>());
ddc::ChunkSpan const derivs2_lhs = derivs2_lhs_alloc.span_view();
if (s_bcl == ddc::BoundCond::HERMITE) {
ddc::Chunk derivs2_lhs1_host_alloc(
ddc::DiscreteDomain<
DDim<I1, I1, I2>,
ddc::Deriv<I2>>(interpolation_domain1, derivs_domain2),
ddc::DiscreteDomain<DDim1, ddc::Deriv<I2>>(interpolation_domain1, derivs_domain2),
ddc::HostAllocator<double>());
ddc::ChunkSpan const derivs2_lhs1_host = derivs2_lhs1_host_alloc.span_view();
ddc::for_each(
derivs2_lhs1_host.domain(),
KOKKOS_LAMBDA(ddc::DiscreteElement<DDim<I1, I1, I2>, ddc::Deriv<I2>> const e) {
auto x1 = ddc::coordinate(ddc::DiscreteElement<DDim<I1, I1, I2>>(e));
KOKKOS_LAMBDA(ddc::DiscreteElement<DDim1, ddc::Deriv<I2>> const e) {
auto x1 = ddc::coordinate(ddc::DiscreteElement<DDim1>(e));
auto deriv_idx = ddc::DiscreteElement<ddc::Deriv<I2>>(e).uid();
derivs2_lhs1_host(e) = evaluator.deriv(x1, x0<I2>(), 0, deriv_idx + shift - 1);
});
Expand All @@ -383,23 +376,21 @@ void Batched2dSplineTest()
derivs2_lhs.domain(),
KOKKOS_LAMBDA(
typename decltype(derivs2_lhs.domain())::discrete_element_type const e) {
derivs2_lhs(e) = derivs2_lhs1(DElem<DDim<I1, I1, I2>, ddc::Deriv<I2>>(e));
derivs2_lhs(e) = derivs2_lhs1(DElem<DDim1, ddc::Deriv<I2>>(e));
});
}

ddc::Chunk derivs2_rhs_alloc(dom_derivs2, ddc::KokkosAllocator<double, MemorySpace>());
ddc::ChunkSpan const derivs2_rhs = derivs2_rhs_alloc.span_view();
if (s_bcl == ddc::BoundCond::HERMITE) {
ddc::Chunk derivs2_rhs1_host_alloc(
ddc::DiscreteDomain<
DDim<I1, I1, I2>,
ddc::Deriv<I2>>(interpolation_domain1, derivs_domain2),
ddc::DiscreteDomain<DDim1, ddc::Deriv<I2>>(interpolation_domain1, derivs_domain2),
ddc::HostAllocator<double>());
ddc::ChunkSpan const derivs2_rhs1_host = derivs2_rhs1_host_alloc.span_view();
ddc::for_each(
derivs2_rhs1_host.domain(),
KOKKOS_LAMBDA(ddc::DiscreteElement<DDim<I1, I1, I2>, ddc::Deriv<I2>> const e) {
auto x1 = ddc::coordinate(ddc::DiscreteElement<DDim<I1, I1, I2>>(e));
KOKKOS_LAMBDA(ddc::DiscreteElement<DDim1, ddc::Deriv<I2>> const e) {
auto x1 = ddc::coordinate(ddc::DiscreteElement<DDim1>(e));
auto deriv_idx = ddc::DiscreteElement<ddc::Deriv<I2>>(e).uid();
derivs2_rhs1_host(e) = evaluator.deriv(x1, xN<I2>(), 0, deriv_idx + shift - 1);
});
Expand All @@ -412,7 +403,7 @@ void Batched2dSplineTest()
derivs2_rhs.domain(),
KOKKOS_LAMBDA(
typename decltype(derivs2_rhs.domain())::discrete_element_type const e) {
derivs2_rhs(e) = derivs2_rhs1(DElem<DDim<I1, I1, I2>, ddc::Deriv<I2>>(e));
derivs2_rhs(e) = derivs2_rhs1(DElem<DDim1, ddc::Deriv<I2>>(e));
});
}

Expand Down Expand Up @@ -525,8 +516,8 @@ void Batched2dSplineTest()
MemorySpace,
BSplines<I1>,
BSplines<I2>,
DDim<I1, I1, I2>,
DDim<I2, I1, I2>,
DDim1,
DDim2,
extrapolation_rule_1_type,
extrapolation_rule_1_type,
extrapolation_rule_2_type,
Expand All @@ -544,9 +535,7 @@ void Batched2dSplineTest()
ddc::parallel_for_each(
exec_space,
coords_eval.domain(),
KOKKOS_LAMBDA(DElem<DDim<X, I1, I2>...> const e) {
coords_eval(e) = ddc::coordinate(e);
});
KOKKOS_LAMBDA(DElemAll const e) { coords_eval(e) = ddc::coordinate(e); });


// Instantiate chunks to receive outputs of spline_evaluator
Expand Down Expand Up @@ -575,37 +564,35 @@ void Batched2dSplineTest()
spline_eval.domain(),
0.,
ddc::reducer::max<double>(),
KOKKOS_LAMBDA(DElem<DDim<X, I1, I2>...> const e) {
return Kokkos::abs(spline_eval(e) - vals(e));
});
KOKKOS_LAMBDA(DElemAll const e) { return Kokkos::abs(spline_eval(e) - vals(e)); });
double const max_norm_error_diff1 = ddc::parallel_transform_reduce(
exec_space,
spline_eval_deriv1.domain(),
0.,
ddc::reducer::max<double>(),
KOKKOS_LAMBDA(DElem<DDim<X, I1, I2>...> const e) {
Coord<I1> const x = ddc::coordinate(DElem<DDim<I1, I1, I2>>(e));
Coord<I2> const y = ddc::coordinate(DElem<DDim<I2, I1, I2>>(e));
KOKKOS_LAMBDA(DElemAll const e) {
Coord<I1> const x = ddc::coordinate(DElem<DDim1>(e));
Coord<I2> const y = ddc::coordinate(DElem<DDim2>(e));
return Kokkos::abs(spline_eval_deriv1(e) - evaluator.deriv(x, y, 1, 0));
});
double const max_norm_error_diff2 = ddc::parallel_transform_reduce(
exec_space,
spline_eval_deriv2.domain(),
0.,
ddc::reducer::max<double>(),
KOKKOS_LAMBDA(DElem<DDim<X, I1, I2>...> const e) {
Coord<I1> const x = ddc::coordinate(DElem<DDim<I1, I1, I2>>(e));
Coord<I2> const y = ddc::coordinate(DElem<DDim<I2, I1, I2>>(e));
KOKKOS_LAMBDA(DElemAll const e) {
Coord<I1> const x = ddc::coordinate(DElem<DDim1>(e));
Coord<I2> const y = ddc::coordinate(DElem<DDim2>(e));
return Kokkos::abs(spline_eval_deriv2(e) - evaluator.deriv(x, y, 0, 1));
});
double const max_norm_error_diff12 = ddc::parallel_transform_reduce(
exec_space,
spline_eval_deriv1.domain(),
0.,
ddc::reducer::max<double>(),
KOKKOS_LAMBDA(DElem<DDim<X, I1, I2>...> const e) {
Coord<I1> const x = ddc::coordinate(DElem<DDim<I1, I1, I2>>(e));
Coord<I2> const y = ddc::coordinate(DElem<DDim<I2, I1, I2>>(e));
KOKKOS_LAMBDA(DElemAll const e) {
Coord<I1> const x = ddc::coordinate(DElem<DDim1>(e));
Coord<I2> const y = ddc::coordinate(DElem<DDim2>(e));
return Kokkos::abs(spline_eval_deriv12(e) - evaluator.deriv(x, y, 1, 1));
});

Expand All @@ -615,7 +602,7 @@ void Batched2dSplineTest()
double const max_norm_diff2 = evaluator.max_norm(0, 1);
double const max_norm_diff12 = evaluator.max_norm(1, 1);

SplineErrorBounds<evaluator_type<DDim<I1, I1, I2>, DDim<I2, I1, I2>>> error_bounds(evaluator);
SplineErrorBounds<evaluator_type<DDim1, DDim2>> error_bounds(evaluator);
EXPECT_LE(
max_norm_error,
std::
Expand Down
4 changes: 1 addition & 3 deletions tests/splines/batched_spline_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,7 @@ void BatchedSplineTest()
auto const dom_spline = spline_builder.batched_spline_domain();

// Allocate and fill a chunk containing values to be passed as input to spline_builder. Those are values of cosine along interest dimension duplicated along batch dimensions
ddc::Chunk vals_1d_host_alloc(
dom_interpolation,
ddc::KokkosAllocator<double, Kokkos::DefaultHostExecutionSpace::memory_space>());
ddc::Chunk vals_1d_host_alloc(dom_interpolation, ddc::HostAllocator<double>());
ddc::ChunkSpan const vals_1d_host = vals_1d_host_alloc.span_view();
evaluator_type<DDim<I, I>> const evaluator(dom_interpolation);
evaluator(vals_1d_host);
Expand Down
4 changes: 1 addition & 3 deletions tests/splines/extrapolation_rule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,7 @@ void ExtrapolationRuleSplineTest()
auto const dom_spline = spline_builder.batched_spline_domain();

// Allocate and fill a chunk containing values to be passed as input to spline_builder. Those are values of cosine along interest dimension duplicated along batch dimensions
ddc::Chunk vals_1d_host_alloc(
dom_interpolation,
ddc::KokkosAllocator<double, Kokkos::DefaultHostExecutionSpace::memory_space>());
ddc::Chunk vals_1d_host_alloc(dom_interpolation, ddc::HostAllocator<double>());
ddc::ChunkSpan const vals_1d_host = vals_1d_host_alloc.span_view();
evaluator_type<DDim<I1, I1, I2>, DDim<I2, I1, I2>> const evaluator(dom_interpolation);
evaluator(vals_1d_host);
Expand Down
4 changes: 1 addition & 3 deletions tests/splines/periodicity_spline_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,7 @@ void PeriodicitySplineBuilderTest()
ddc::DiscreteDomain<BSplines<X>> const dom_bsplines = spline_builder.spline_domain();

// Allocate and fill a chunk containing values to be passed as input to spline_builder. Those are values of cosine along interest dimension duplicated along batch dimensions
ddc::Chunk vals_host_alloc(
dom_vals,
ddc::KokkosAllocator<double, Kokkos::DefaultHostExecutionSpace::memory_space>());
ddc::Chunk vals_host_alloc(dom_vals, ddc::HostAllocator<double>());
ddc::ChunkSpan const vals_host = vals_host_alloc.span_view();
evaluator_type<DDim<X>> const evaluator(dom_vals);
evaluator(vals_host);
Expand Down

0 comments on commit 6e2ea0d

Please sign in to comment.