Skip to content

Commit

Permalink
Sync DDC
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilyBourne committed Oct 9, 2023
1 parent 64d4fbf commit 51664bb
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 17 deletions.
10 changes: 6 additions & 4 deletions vendor/ddc/include/ddc/chunk_span.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class ChunkSpan<ElementType, DiscreteDomain<DDims...>, LayoutStridedPolicy, Memo

protected:
template <class QueryDDim, class... ODDims>
auto get_slicer_for(DiscreteElement<ODDims...> const& c) const
constexpr auto get_slicer_for(DiscreteElement<ODDims...> const& c) const
{
DDC_IF_NVCC_THEN_PUSH_AND_SUPPRESS(implicit_return_from_non_void_function)
if constexpr (in_tags_v<QueryDDim, detail::TypeSeq<ODDims...>>) {
Expand All @@ -101,7 +101,7 @@ class ChunkSpan<ElementType, DiscreteDomain<DDims...>, LayoutStridedPolicy, Memo
}

template <class QueryDDim, class... ODDims>
auto get_slicer_for(DiscreteDomain<ODDims...> const& c) const
constexpr auto get_slicer_for(DiscreteDomain<ODDims...> const& c) const
{
DDC_IF_NVCC_THEN_PUSH_AND_SUPPRESS(implicit_return_from_non_void_function)
if constexpr (in_tags_v<QueryDDim, detail::TypeSeq<ODDims...>>) {
Expand Down Expand Up @@ -176,7 +176,9 @@ class ChunkSpan<ElementType, DiscreteDomain<DDims...>, LayoutStridedPolicy, Memo
* @param allocation_mdspan the allocation mdspan to the data
* @param domain the domain that sustains the view
*/
constexpr ChunkSpan(allocation_mdspan_type allocation_mdspan, mdomain_type const& domain)
KOKKOS_FUNCTION constexpr ChunkSpan(
allocation_mdspan_type allocation_mdspan,
mdomain_type const& domain)
{
namespace stdex = std::experimental;
extents_type extents_s((front<DDims>(domain) + extents<DDims>(domain)).uid()...);
Expand Down Expand Up @@ -229,7 +231,7 @@ class ChunkSpan<ElementType, DiscreteDomain<DDims...>, LayoutStridedPolicy, Memo
memory_space>(subview, select_by_type_seq<selected_meshes>(this->m_domain));
}

/** Slice out some dimensions
/** Restrict to a subdomain
*/
template <class... QueryDDims>
constexpr auto operator[](DiscreteDomain<QueryDDims...> const& odomain) const
Expand Down
2 changes: 2 additions & 0 deletions vendor/ddc/include/ddc/detail/tagged_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ class TaggedVector : public ConversionOperators<TaggedVector<ElementType, Tags..
std::array<ElementType, sizeof...(Tags)> m_values;

public:
using value_type = ElementType;

static constexpr std::size_t size() noexcept
{
return sizeof...(Tags);
Expand Down
55 changes: 55 additions & 0 deletions vendor/ddc/include/ddc/detail/type_seq.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,58 @@ struct TypeSeqMerge<TypeSeq<TagsA...>, TypeSeq<HeadTagsB, TailTagsB...>, TypeSeq
{
};

/// A is replaced by element of C at same position than the first element of B equal to A.
/// Remark : It may not be usefull in its own, it is an helper for TypeSeqReplace
template <class TagA, class TagSeqB, class TagSeqC>
struct TypeSeqReplaceSingle;

template <class TagA>
struct TypeSeqReplaceSingle<TagA, TypeSeq<>, TypeSeq<>>
{
using type = TagA;
};

template <class TagA, class HeadTagsB, class... TailTagsB, class HeadTagsC, class... TailTagsC>
struct TypeSeqReplaceSingle<
TagA,
TypeSeq<HeadTagsB, TailTagsB...>,
TypeSeq<HeadTagsC, TailTagsC...>>
: std::conditional_t<
std::is_same_v<TagA, HeadTagsB>,
TypeSeqReplaceSingle<HeadTagsC, TypeSeq<>, TypeSeq<>>,
TypeSeqReplaceSingle<TagA, TypeSeq<TailTagsB...>, TypeSeq<TailTagsC...>>>
{
};

/// R contains all elements of A except those of B which are replaced by those of C.
/// Remark : This operation preserves the orders.
template <class TagSeqA, class TagSeqB, class TagSeqC, class TagSeqR>
struct TypeSeqReplace;

template <class... TagsB, class... TagsC, class... TagsR>
struct TypeSeqReplace<TypeSeq<>, TypeSeq<TagsB...>, TypeSeq<TagsC...>, TypeSeq<TagsR...>>
{
using type = TypeSeq<TagsR...>;
};

template <class HeadTagsA, class... TailTagsA, class... TagsB, class... TagsC, class... TagsR>
struct TypeSeqReplace<
TypeSeq<HeadTagsA, TailTagsA...>,
TypeSeq<TagsB...>,
TypeSeq<TagsC...>,
TypeSeq<TagsR...>>
: TypeSeqReplace<
TypeSeq<TailTagsA...>,
TypeSeq<TagsB...>,
TypeSeq<TagsC...>,
TypeSeq<TagsR...,
typename TypeSeqReplaceSingle<
HeadTagsA,
TypeSeq<TagsB...>,
TypeSeq<TagsC...>>::type>>
{
};

} // namespace detail

template <class QueryTag, class TypeSeq>
Expand Down Expand Up @@ -137,4 +189,7 @@ using type_seq_remove_t = typename detail::TypeSeqRemove<TagSeqA, TagSeqB, detai
template <class TagSeqA, class TagSeqB>
using type_seq_merge_t = typename detail::TypeSeqMerge<TagSeqA, TagSeqB, TagSeqA>::type;

template <class TagSeqA, class TagSeqB, class TagSeqC>
using type_seq_replace_t =
typename detail::TypeSeqReplace<TagSeqA, TagSeqB, TagSeqC, detail::TypeSeq<>>::type;
} // namespace ddc
40 changes: 39 additions & 1 deletion vendor/ddc/include/ddc/discrete_domain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class DiscreteDomain
}
#endif

std::size_t size() const
constexpr std::size_t size() const
{
return (1ul * ... * (uid<DDims>(m_element_end) - uid<DDims>(m_element_begin)));
}
Expand Down Expand Up @@ -405,6 +405,44 @@ constexpr auto remove_dims_of(
return detail::convert_type_seq_to_discrete_domain<type_seq_r>(DDom_a);
}


// Checks if dimension of DDom_a is DDim1. If not, returns restriction to DDim2 of DDom_b. May not be usefull in its own, it helps for replace_dim_of
template <typename DDim1, typename DDim2, typename DDimA, typename... DDimsB>
constexpr std::conditional_t<
std::is_same_v<DDimA, DDim1>,
ddc::DiscreteDomain<DDim2>,
ddc::DiscreteDomain<DDimA>>
replace_dim_of_1d(
DiscreteDomain<DDimA> const& DDom_a,
[[maybe_unused]] DiscreteDomain<DDimsB...> const& DDom_b) noexcept
{
if constexpr (std::is_same_v<DDimA, DDim1>) {
return ddc::select<DDim2>(DDom_b);
} else {
return DDom_a;
}
}

// Replace in DDom_a the dimension Dim1 by the dimension Dim2 of DDom_b
template <typename DDim1, typename DDim2, typename... DDimsA, typename... DDimsB>
constexpr auto replace_dim_of(
DiscreteDomain<DDimsA...> const& DDom_a,
[[maybe_unused]] DiscreteDomain<DDimsB...> const& DDom_b) noexcept
{
// TODO : static_asserts
using TagSeqA = detail::TypeSeq<DDimsA...>;
using TagSeqB = detail::TypeSeq<DDim1>;
using TagSeqC = detail::TypeSeq<DDim2>;

using type_seq_r = ddc::type_seq_replace_t<TagSeqA, TagSeqB, TagSeqC>;
return ddc::detail::convert_type_seq_to_discrete_domain<type_seq_r>(
replace_dim_of_1d<
DDim1,
DDim2,
DDimsA,
DDimsB...>(ddc::select<DDimsA>(DDom_a), DDom_b)...);
}

template <class... QueryDDims, class... DDims>
constexpr DiscreteVector<QueryDDims...> extents(DiscreteDomain<DDims...> const& domain) noexcept
{
Expand Down
32 changes: 32 additions & 0 deletions vendor/ddc/include/ddc/for_each.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,50 @@ class ForEachKokkosLambdaAdapter
public:
ForEachKokkosLambdaAdapter(F const& f) : m_f(f) {}

template <std::size_t N = sizeof...(DDims), std::enable_if_t<(N == 0), bool> = true>
KOKKOS_IMPL_FORCEINLINE void operator()([[maybe_unused]] index_type<void> unused_id) const
{
m_f(DiscreteElement<>());
}

template <std::size_t N = sizeof...(DDims), std::enable_if_t<(N == 0), bool> = true>
KOKKOS_FORCEINLINE_FUNCTION void operator()(
use_annotated_operator,
[[maybe_unused]] index_type<void> unused_id) const
{
m_f(DiscreteElement<>());
}

template <std::size_t N = sizeof...(DDims), std::enable_if_t<(N > 0), bool> = true>
KOKKOS_IMPL_FORCEINLINE void operator()(index_type<DDims>... ids) const
{
m_f(DiscreteElement<DDims...>(ids...));
}

template <std::size_t N = sizeof...(DDims), std::enable_if_t<(N > 0), bool> = true>
KOKKOS_FORCEINLINE_FUNCTION void operator()(use_annotated_operator, index_type<DDims>... ids)
const
{
m_f(DiscreteElement<DDims...>(ids...));
}
};

template <class ExecSpace, class Functor>
inline void for_each_kokkos(
[[maybe_unused]] DiscreteDomain<> const& domain,
Functor const& f) noexcept
{
if constexpr (need_annotated_operator<ExecSpace>()) {
Kokkos::parallel_for(
Kokkos::RangePolicy<ExecSpace, use_annotated_operator>(0, 1),
ForEachKokkosLambdaAdapter<Functor>(f));
} else {
Kokkos::parallel_for(
Kokkos::RangePolicy<ExecSpace>(0, 1),
ForEachKokkosLambdaAdapter<Functor>(f));
}
}

template <class ExecSpace, class Functor, class DDim0>
inline void for_each_kokkos(DiscreteDomain<DDim0> const& domain, Functor const& f) noexcept
{
Expand Down
9 changes: 6 additions & 3 deletions vendor/ddc/include/ddc/kernels/fft.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ using _fftw_plan = std::conditional_t<std::is_same_v<real_type_t<T>, float>, fft

// _fftw_plan_many_dft : templated function working for all types of transformation
template <typename Tin, typename Tout, typename... Args, typename PenultArg, typename LastArg>
_fftw_plan<Tin> _fftw_plan_many_dft(PenultArg penultArg, LastArg lastArg, Args... args)
_fftw_plan<Tin> _fftw_plan_many_dft(
[[maybe_unused]] PenultArg penultArg,
LastArg lastArg,
Args... args)
{ // Ugly, penultArg and lastArg are passed before the rest because of a limitation of C++ (parameter packs must be last arguments)
const TransformType transformType = transform_type_v<Tin, Tout>;
if constexpr (transformType == TransformType::R2C && std::is_same_v<Tin, float>)
Expand Down Expand Up @@ -216,7 +219,7 @@ constexpr auto cufft_transform_type()
// cufftExec : argument passed in the cufftMakePlan function
// _fftw_plan_many_dft : templated function working for all types of transformation
template <typename Tin, typename Tout, typename... Args, typename LastArg>
cufftResult _cufftExec(LastArg lastArg, Args... args)
cufftResult _cufftExec([[maybe_unused]] LastArg lastArg, Args... args)
{ // Ugly for same reason as fftw
const TransformType transformType = transform_type_v<Tin, Tout>;
if constexpr (transformType == TransformType::R2C && std::is_same_v<Tin, float>)
Expand Down Expand Up @@ -280,7 +283,7 @@ constexpr auto hipfft_transform_type()
// hipfftExec : argument passed in the hipfftMakePlan function
// _fftw_plan_many_dft : templated function working for all types of transformation
template <typename Tin, typename Tout, typename... Args, typename LastArg>
hipfftResult _hipfftExec(LastArg lastArg, Args... args)
hipfftResult _hipfftExec([[maybe_unused]] LastArg lastArg, Args... args)
{
const TransformType transformType = transform_type_v<Tin, Tout>;
if constexpr (transformType == TransformType::R2C && std::is_same_v<Tin, float>)
Expand Down
65 changes: 59 additions & 6 deletions vendor/ddc/include/ddc/transform_reduce.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,34 @@ class TransformReducerKokkosLambdaAdapter
{
}

template <std::size_t N = sizeof...(DDims), std::enable_if_t<(N == 0), bool> = true>
KOKKOS_IMPL_FORCEINLINE void operator()(
[[maybe_unused]] index_type<void> unused_id,
typename Reducer::value_type& a) const
{
a = reducer(a, functor(DiscreteElement<>()));
}

template <std::size_t N = sizeof...(DDims), std::enable_if_t<(N == 0), bool> = true>
KOKKOS_FORCEINLINE_FUNCTION void operator()(
use_annotated_operator,
[[maybe_unused]] index_type<void> unused_id,
typename Reducer::value_type& a) const

{
a = reducer(a, functor(DiscreteElement<>()));
}

template <std::size_t N = sizeof...(DDims), std::enable_if_t<(N > 0), bool> = true>
KOKKOS_IMPL_FORCEINLINE void operator()(
index_type<DDims>... ids,
typename Reducer::value_type& a) const
{
a = reducer(a, functor(DiscreteElement<DDims...>(ids...)));
}


template <std::size_t N = sizeof...(DDims), std::enable_if_t<(N > 0), bool> = true>
KOKKOS_FORCEINLINE_FUNCTION void operator()(
use_annotated_operator,
index_type<DDims>... ids,
Expand All @@ -154,6 +175,40 @@ class TransformReducerKokkosLambdaAdapter
}
};

/** A parallel reduction over a nD domain using the default Kokkos execution space
* @param[in] domain the range over which to apply the algorithm
* @param[in] neutral the neutral element of the reduction operation
* @param[in] reduce a binary FunctionObject that will be applied in unspecified order to the
* results of transform, the results of other reduce and neutral.
* @param[in] transform a unary FunctionObject that will be applied to each element of the input
* range. The return type must be acceptable as input to reduce
*/
template <class ExecSpace, class T, class BinaryReductionOp, class UnaryTransformOp>
inline T transform_reduce_kokkos(
[[maybe_unused]] DiscreteDomain<> const& domain,
T neutral,
BinaryReductionOp const& reduce,
UnaryTransformOp const& transform) noexcept
{
T result = neutral;
if constexpr (need_annotated_operator<ExecSpace>()) {
Kokkos::parallel_reduce(
Kokkos::RangePolicy<ExecSpace, use_annotated_operator>(0, 1),
TransformReducerKokkosLambdaAdapter<
BinaryReductionOp,
UnaryTransformOp>(reduce, transform),
ddc_to_kokkos_reducer_t<BinaryReductionOp>(result));
} else {
Kokkos::parallel_reduce(
Kokkos::RangePolicy<ExecSpace>(0, 1),
TransformReducerKokkosLambdaAdapter<
BinaryReductionOp,
UnaryTransformOp>(reduce, transform),
ddc_to_kokkos_reducer_t<BinaryReductionOp>(result));
}
return result;
}

/** A parallel reduction over a nD domain using the default Kokkos execution space
* @param[in] domain the range over which to apply the algorithm
* @param[in] neutral the neutral element of the reduction operation
Expand All @@ -172,19 +227,17 @@ inline T transform_reduce_kokkos(
T result = neutral;
if constexpr (need_annotated_operator<ExecSpace>()) {
Kokkos::parallel_reduce(
Kokkos::RangePolicy<ExecSpace, use_annotated_operator>(
select<DDim0>(domain).front().uid(),
select<DDim0>(domain).back().uid() + 1),
Kokkos::RangePolicy<
ExecSpace,
use_annotated_operator>(domain.front().uid(), domain.back().uid() + 1),
TransformReducerKokkosLambdaAdapter<
BinaryReductionOp,
UnaryTransformOp,
DDim0>(reduce, transform),
ddc_to_kokkos_reducer_t<BinaryReductionOp>(result));
} else {
Kokkos::parallel_reduce(
Kokkos::RangePolicy<ExecSpace>(
select<DDim0>(domain).front().uid(),
select<DDim0>(domain).back().uid() + 1),
Kokkos::RangePolicy<ExecSpace>(domain.front().uid(), domain.back().uid() + 1),
TransformReducerKokkosLambdaAdapter<
BinaryReductionOp,
UnaryTransformOp,
Expand Down
Loading

0 comments on commit 51664bb

Please sign in to comment.