Skip to content

Commit

Permalink
Add Stacked accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardmgruber committed Aug 1, 2023
1 parent 6564102 commit d65fee7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ Acessors
.. doxygenstruct:: llama::accessor::Const
.. doxygenstruct:: llama::accessor::Restrict
.. doxygenstruct:: llama::accessor::Atomic
.. doxygenstruct:: llama::accessor::Stacked

RecordDim flattener
^^^^^^^^^^^^^^^^^^^
Expand Down
38 changes: 38 additions & 0 deletions include/llama/Accessors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,42 @@ namespace llama::accessor
}
};
#endif

namespace internal
{
template<std::size_t I, typename Accessor>
struct StackedLeave : Accessor
{
};
} // namespace internal

/// Accessor combining multiple other accessors. The contained accessors are applied in left to right order to the
/// memory location when forming the reference returned from a view.
template<typename... Accessors>
struct Stacked : internal::StackedLeave<0, Default>
{
};

template<typename FirstAccessor, typename... MoreAccessors>
struct Stacked<FirstAccessor, MoreAccessors...>
: internal::StackedLeave<1 + sizeof...(MoreAccessors), FirstAccessor>
, Stacked<MoreAccessors...>
{
using First = internal::StackedLeave<1 + sizeof...(MoreAccessors), FirstAccessor>;
using Rest = Stacked<MoreAccessors...>;

LLAMA_FN_HOST_ACC_INLINE Stacked() = default;

LLAMA_FN_HOST_ACC_INLINE explicit Stacked(FirstAccessor first, MoreAccessors... rest)
: First{std::move(first)}
, Rest{std::move(rest)...}
{
}

template<typename Reference>
LLAMA_FN_HOST_ACC_INLINE auto operator()(Reference&& r) const -> decltype(auto)
{
return static_cast<const Rest&>(*this)(static_cast<const First&>(*this)(std::forward<Reference>(r)));
}
};
} // namespace llama::accessor
12 changes: 12 additions & 0 deletions tests/accessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,15 @@ TEST_CASE("view.withAccessor.OffsetFloatAccessor")
CHECK(view2(0)(tag::Pos{})(tag::X{}) == 2.0);
CHECK(view2(0)(tag::Mass{}) == 12.0f);
}

TEST_CASE("view.allocView.Stacked")
{
auto mapping = llama::mapping::AoS{llama::ArrayExtents{3, 4}, Particle{}};

auto view = llama::allocView(
mapping,
llama::bloballoc::Vector{},
llama::accessor::Stacked<llama::accessor::Default, llama::accessor::Restrict, llama::accessor::Default>{});
iotaFillView(view);
iotaCheckView(view);
}

0 comments on commit d65fee7

Please sign in to comment.