Skip to content

Commit

Permalink
Support stateful accessors
Browse files Browse the repository at this point in the history
Fixes: #604
  • Loading branch information
bernhardmgruber committed Nov 10, 2022
1 parent 1a3c5c2 commit 748150c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
20 changes: 12 additions & 8 deletions include/llama/View.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ namespace llama
LLAMA_FN_HOST_ACC_INLINE auto allocViewUninitialized(
Mapping mapping = {},
const Allocator& alloc = {},
Accessor = {}) -> View<Mapping, internal::AllocatorBlobType<Allocator, typename Mapping::RecordDim>, Accessor>
Accessor accessor = {})
-> View<Mapping, internal::AllocatorBlobType<Allocator, typename Mapping::RecordDim>, Accessor>
{
auto blobs = internal::makeBlobArray(alloc, mapping, std::make_index_sequence<Mapping::blobCount>{});
return {std::move(mapping), std::move(blobs)};
return {std::move(mapping), std::move(blobs), std::move(accessor)};
}

namespace internal
Expand Down Expand Up @@ -407,7 +408,6 @@ namespace llama
#endif
{
static_assert(!std::is_const_v<TMapping>);
static_assert(std::is_empty_v<TAccessor>, "Stateful accessors are not implemented");
using Mapping = TMapping;
using BlobType = TBlobType;
using ArrayExtents = typename Mapping::ArrayExtents;
Expand All @@ -432,8 +432,9 @@ namespace llama
View() = default;

LLAMA_FN_HOST_ACC_INLINE
View(Mapping mapping, Array<BlobType, Mapping::blobCount> storageBlobs)
View(Mapping mapping, Array<BlobType, Mapping::blobCount> storageBlobs, Accessor accessor = {})
: Mapping(std::move(mapping))
, Accessor(std::move(accessor))
, storageBlobs(std::move(storageBlobs))
{
}
Expand Down Expand Up @@ -629,8 +630,8 @@ namespace llama
std::make_index_sequence<blobCount>{});
return llama::View<typename View::Mapping, typename decltype(blobs)::value_type, typename View::Accessor>{
view.mapping(),
std::move(blobs)/*,
view.accessor()*/};
std::move(blobs),
view.accessor()};
}

/// Creates a shallow copy of a view. This copy must not outlive the view, since it references its blob array.
Expand Down Expand Up @@ -659,9 +660,12 @@ namespace llama
// \param view A view which's mapping and blobs are copied into a new view with the different accessor. If you no
// longer need the old view, consider moving it into the argument of this function.
template<typename NewAccessor, typename Mapping, typename BlobType, typename OldAccessor>
LLAMA_FN_HOST_ACC_INLINE auto withAccessor(View<Mapping, BlobType, OldAccessor> view)
LLAMA_FN_HOST_ACC_INLINE auto withAccessor(View<Mapping, BlobType, OldAccessor> view, NewAccessor newAccessor = {})
{
return View<Mapping, BlobType, NewAccessor>{std::move(view.mapping()), std::move(view.storageBlobs)};
return View<Mapping, BlobType, NewAccessor>{
std::move(view.mapping()),
std::move(view.storageBlobs),
std::move(newAccessor)};
}

/// Like a \ref View, but array indices are shifted.
Expand Down
37 changes: 36 additions & 1 deletion tests/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,4 +429,39 @@ TEMPLATE_TEST_CASE("view.withAccessor.shallowCopy.Restrict", "", llama::bloballo
auto view2 = llama::withAccessor<llama::accessor::Restrict>(llama::shallowCopy(view));
iotaFillView(view2);
iotaCheckView(view);
}
}

namespace
{
struct OffsetFloatAccessor
{
float offset;

template<typename T>
auto operator()(T& ref) -> decltype(auto)
{
if constexpr(std::is_same_v<T, float>)
return ref + offset;
else
return ref;
}
};
} // namespace

TEST_CASE("view.withAccessor.OffsetFloatAccessor")
{
auto view
= llama::allocView(llama::mapping::AoS{llama::ArrayExtents{4}, Particle{}}, llama::bloballoc::SharedPtr{});
view(0)(tag::Pos{})(tag::X{}) = 2.0;
view(0)(tag::Mass{}) = 2.0f;

auto view2 = llama::withAccessor(view, OffsetFloatAccessor{3});

CHECK(view2(0)(tag::Pos{})(tag::X{}) == 2.0);
CHECK(view2(0)(tag::Mass{}) == 5.0f);

view2.accessor().offset = 10;

CHECK(view2(0)(tag::Pos{})(tag::X{}) == 2.0);
CHECK(view2(0)(tag::Mass{}) == 12.0f);
}

0 comments on commit 748150c

Please sign in to comment.