Skip to content

Commit

Permalink
Fix forwarding of custom tuple and add indexed apply
Browse files Browse the repository at this point in the history
  • Loading branch information
ikbuibui committed Feb 4, 2025
1 parent 8b64dbb commit 250e9fc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
4 changes: 2 additions & 2 deletions include/picongpu/plugins/binning/Binner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ namespace picongpu
{
pmacc::DataSpace<nAxes> const nDIdx = pmacc::math::mapToND(extentsDataspace, linearTid);
float_X factor = 1.;
apply(
binning::apply(
[&](auto const&... binWidthsKernel)
{
// uses bin width for axes without dimensions as well should those be ignored?
Expand Down Expand Up @@ -246,7 +246,7 @@ namespace picongpu
this->histBuffer->getDeviceBuffer().getDataBox());

// change output dimensions
apply(
std::apply(
[&](auto const&... tupleArgs)
{ ((dimensionSubtraction(outputUnits, tupleArgs.units)), ...); },
binningData.axisTuple);
Expand Down
16 changes: 9 additions & 7 deletions include/picongpu/plugins/binning/BinningFunctors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,21 @@ namespace picongpu

auto binsDataspace = pmacc::DataSpace<T_nAxes>{};
bool validIdx = true;
apply(
[&](auto const&... tupleArgs)
binning::applyIndexed(
[&](auto const& tuple, auto... idxs)
{
uint32_t i = 0;
// This assumes n_bins and getBinIdx exist
validIdx
= ((
= (
[&]
{
auto [isValid, binIdx] = tupleArgs.getBinIdx(domainInfo, worker, particle);
binsDataspace[i++] = binIdx;
auto [isValid, binIdx] = pmacc::memory::tuple::get<idxs>(tuple).getBinIdx(
domainInfo,
worker,
particle);
binsDataspace[idxs] = binIdx;
return isValid;
}())
}()
&& ...);
},
axes);
Expand Down
41 changes: 29 additions & 12 deletions include/picongpu/plugins/binning/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,43 @@ namespace picongpu
{
namespace detail
{
template<typename TFunc, typename... TArgs, std::size_t... Is>
HDINLINE constexpr auto apply_impl(
TFunc&& f,
pmacc::memory::tuple::Tuple<TArgs...>&& t,
std::index_sequence<Is...>)
template<typename TFunc, typename TPmaccTuple, std::size_t... Is>
HDINLINE constexpr decltype(auto) applyImpl(TFunc&& f, TPmaccTuple&& t, std::index_sequence<Is...>)
{
// @todo give Is as a param to f, so compiler has knowledge
return f(pmacc::memory::tuple::get<Is>(std::forward<pmacc::memory::tuple::Tuple<TArgs...>&&>(t))...);
return std::forward<TFunc>(f)(pmacc::memory::tuple::get<Is>(std::forward<TPmaccTuple>(t))...);
}

template<typename TFunc, typename TPmaccTuple, std::size_t... Is>
HDINLINE constexpr decltype(auto) applyIdxImpl(TFunc&& f, TPmaccTuple&& t, std::index_sequence<Is...>)
{
return std::forward<TFunc>(
f)(std::forward<TPmaccTuple>(t), std::integral_constant<std::size_t, Is>{}...);
}


} // namespace detail

template<typename TFunc, typename... TArgs>
HDINLINE constexpr auto apply(TFunc&& f, pmacc::memory::tuple::Tuple<TArgs...> t)
// takes pmacc::memory::tuple::Tuple
template<typename TFunc, typename TPmaccTuple>
HDINLINE constexpr decltype(auto) apply(TFunc&& f, TPmaccTuple&& t)
{
return detail::apply_impl(
return detail::applyImpl(
std::forward<TFunc>(f),
std::forward<pmacc::memory::tuple::Tuple<TArgs...>&&>(t),
std::make_index_sequence<sizeof...(TArgs)>{});
std::forward<TPmaccTuple>(t),
std::make_index_sequence<pmacc::memory::tuple::tuple_size_v<TPmaccTuple>>{});
}

// takes pmacc::memory::tuple::Tuple
template<typename TFunc, typename TPmaccTuple>
HDINLINE constexpr decltype(auto) applyIndexed(TFunc&& f, TPmaccTuple&& t)
{
return detail::applyIdxImpl(
std::forward<TFunc>(f),
std::forward<TPmaccTuple>(t),
std::make_index_sequence<pmacc::memory::tuple::tuple_size_v<TPmaccTuple>>{});
}


namespace detail
{
template<size_t... Is, typename... Args, typename Functor>
Expand Down
11 changes: 11 additions & 0 deletions include/pmacc/memory/STLTuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@ static constexpr size_t size(Tuple<Args...>&) {
return sizeof...(Args);
}

// Handles cv-qualifiers and refs
template <typename T>
struct tuple_size : tuple_size<std::remove_cvref_t<T>> {};

template <typename... Args>
struct tuple_size<Tuple<Args...>> : std::integral_constant<std::size_t, sizeof...(Args)>{
};

template<typename T>
constexpr std::size_t tuple_size_v = tuple_size<T>::value;

/// @struct IndexList
/// @brief Creates a list of index from the elements in the tuple
/// @tparam Is... a list of index from [0 to sizeof...(tuple elements))
Expand Down

0 comments on commit 250e9fc

Please sign in to comment.