Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce AnyTag to allow users not specify tags in AccessTraits #1069

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/access_traits/example_cuda_access_traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct Spheres
};

template <>
struct ArborX::AccessTraits<PointCloud, ArborX::PrimitivesTag>
struct ArborX::AccessTraits<PointCloud>
{
static KOKKOS_FUNCTION std::size_t size(PointCloud const &cloud)
{
Expand All @@ -50,7 +50,7 @@ struct ArborX::AccessTraits<PointCloud, ArborX::PrimitivesTag>
};

template <>
struct ArborX::AccessTraits<Spheres, ArborX::PredicatesTag>
struct ArborX::AccessTraits<Spheres>
{
static KOKKOS_FUNCTION std::size_t size(Spheres const &d) { return d.N; }
static KOKKOS_FUNCTION auto get(Spheres const &d, std::size_t i)
Expand Down
4 changes: 2 additions & 2 deletions examples/access_traits/example_host_access_traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#include <random>
#include <vector>

template <typename T, typename Tag>
struct ArborX::AccessTraits<std::vector<T>, Tag>
template <typename T>
struct ArborX::AccessTraits<std::vector<T>>
{
static std::size_t size(std::vector<T> const &v) { return v.size(); }
static T const &get(std::vector<T> const &v, std::size_t i) { return v[i]; }
Expand Down
4 changes: 2 additions & 2 deletions examples/callback/example_callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct NearestToOrigin
};

template <>
struct ArborX::AccessTraits<FirstOctant, ArborX::PredicatesTag>
struct ArborX::AccessTraits<FirstOctant>
{
static KOKKOS_FUNCTION std::size_t size(FirstOctant) { return 1; }
static KOKKOS_FUNCTION auto get(FirstOctant, std::size_t)
Expand All @@ -40,7 +40,7 @@ struct ArborX::AccessTraits<FirstOctant, ArborX::PredicatesTag>
};

template <>
struct ArborX::AccessTraits<NearestToOrigin, ArborX::PredicatesTag>
struct ArborX::AccessTraits<NearestToOrigin>
{
static KOKKOS_FUNCTION std::size_t size(NearestToOrigin) { return 1; }
static KOKKOS_FUNCTION auto get(NearestToOrigin d, std::size_t)
Expand Down
3 changes: 1 addition & 2 deletions examples/raytracing/example_raytracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ struct DepositEnergy
} // namespace OrderedIntersectsBased

template <typename MemorySpace>
struct ArborX::AccessTraits<OrderedIntersectsBased::Rays<MemorySpace>,
ArborX::PredicatesTag>
struct ArborX::AccessTraits<OrderedIntersectsBased::Rays<MemorySpace>>
{
using memory_space = MemorySpace;
using size_type = std::size_t;
Expand Down
8 changes: 4 additions & 4 deletions src/ArborX_BruteForce.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,7 @@ template <typename ExecutionSpace, typename UserValues>
BruteForce<MemorySpace, Value, IndexableGetter, BoundingVolume>::BruteForce(
ExecutionSpace const &space, UserValues const &user_values,
IndexableGetter const &indexable_getter)
: _size(AccessTraits<UserValues, PrimitivesTag>::size(user_values))
, _values(Kokkos::view_alloc(space, Kokkos::WithoutInitializing,
"ArborX::BruteForce::values"),
_size)
: _values("ArborX::BruteForce::values", 0)
, _indexable_getter(indexable_getter)
{
static_assert(Details::KokkosExt::is_accessible_from<MemorySpace,
Expand All @@ -234,11 +231,14 @@ BruteForce<MemorySpace, Value, IndexableGetter, BoundingVolume>::BruteForce(

Kokkos::Profiling::ScopedRegion guard("ArborX::BruteForce::BruteForce");

_size = values.size();
if (empty())
{
return;
}

Details::KokkosExt::reallocWithoutInitializing(space, _values, _size);

Details::BruteForceImpl::initializeBoundingVolumesAndReduceBoundsOfTheScene(
space, values, _indexable_getter, _values, _bounds);
}
Expand Down
6 changes: 2 additions & 4 deletions src/ArborX_DBSCAN.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ struct MixedBoxPrimitives

template <typename Primitives, typename PermuteFilter>
struct AccessTraits<Details::PrimitivesWithRadiusReorderedAndFiltered<
Primitives, PermuteFilter>,
PredicatesTag>
Primitives, PermuteFilter>>
{
using memory_space = typename Primitives::memory_space;
using Predicates =
Expand Down Expand Up @@ -129,8 +128,7 @@ struct AccessTraits<Details::PrimitivesWithRadiusReorderedAndFiltered<
template <typename Points, typename MixedOffsets, typename CellIndices,
typename Permutation>
struct AccessTraits<
Details::MixedBoxPrimitives<Points, MixedOffsets, CellIndices, Permutation>,
ArborX::PrimitivesTag>
Details::MixedBoxPrimitives<Points, MixedOffsets, CellIndices, Permutation>>
{
using Primitives = Details::MixedBoxPrimitives<Points, MixedOffsets,
CellIndices, Permutation>;
Expand Down
14 changes: 7 additions & 7 deletions src/ArborX_LinearBVH.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,8 @@ BoundingVolumeHierarchy<MemorySpace, Value, IndexableGetter, BoundingVolume>::
UserValues const &user_values,
IndexableGetter const &indexable_getter,
SpaceFillingCurve const &curve)
: _size(AccessTraits<UserValues, PrimitivesTag>::size(user_values))
, _leaf_nodes(Kokkos::view_alloc(space, Kokkos::WithoutInitializing,
"ArborX::BVH::leaf_nodes"),
_size)
, _internal_nodes(Kokkos::view_alloc(space, Kokkos::WithoutInitializing,
"ArborX::BVH::internal_nodes"),
_size > 1 ? _size - 1 : 0)
: _leaf_nodes("ArborX::BVH::leaf_nodes", 0)
, _internal_nodes("ArborX::BVH::internal_nodes", 0)
, _indexable_getter(indexable_getter)
{
static_assert(Details::KokkosExt::is_accessible_from<MemorySpace,
Expand All @@ -314,11 +309,16 @@ BoundingVolumeHierarchy<MemorySpace, Value, IndexableGetter, BoundingVolume>::

Kokkos::Profiling::ScopedRegion guard("ArborX::BVH::BVH");

_size = values.size();
if (empty())
{
return;
}

Details::KokkosExt::reallocWithoutInitializing(space, _leaf_nodes, _size);
Details::KokkosExt::reallocWithoutInitializing(space, _internal_nodes,
_size - 1);

if (size() == 1)
{
Details::TreeConstruction::initializeSingleLeafTree(
Expand Down
39 changes: 28 additions & 11 deletions src/details/ArborX_AccessTraits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,31 @@ struct PrimitivesTag
struct PredicatesTag
{};

template <typename T, typename Tag, typename Enable = void>
namespace Details
{
struct AnyTag
{};
} // namespace Details

template <typename T, typename Tag = Details::AnyTag, typename Enable = void>
struct AccessTraits
{
using not_specialized = void; // tag to detect existence of a specialization
};

namespace Details
{
template <typename Traits>
using AccessTraitsNotSpecializedArchetypeAlias =
typename Traits::not_specialized;

template <typename T, typename Tag>
using TrueTag = std::conditional_t<
!Kokkos::is_detected<AccessTraitsNotSpecializedArchetypeAlias,
AccessTraits<T, AnyTag>>{},
AnyTag, Tag>;
} // namespace Details

template <typename View, typename Tag>
struct AccessTraits<
View, Tag, std::enable_if_t<Kokkos::is_view<View>{} && View::rank == 1>>
Expand Down Expand Up @@ -105,7 +120,8 @@ using PredicateTagArchetypeAlias = typename P::Tag;
template <typename Predicates>
void check_valid_access_traits(PredicatesTag, Predicates const &)
{
using Access = AccessTraits<Predicates, PredicatesTag>;
using Access =
AccessTraits<Predicates, Details::TrueTag<Predicates, PredicatesTag>>;
static_assert(
!Kokkos::is_detected<AccessTraitsNotSpecializedArchetypeAlias, Access>{},
"Must specialize 'AccessTraits<Predicates,PredicatesTag>'");
Expand Down Expand Up @@ -150,7 +166,8 @@ template <typename Primitives, typename CheckGetReturnType = std::true_type>
void check_valid_access_traits(PrimitivesTag, Primitives const &,
CheckGetReturnType = {})
{
using Access = AccessTraits<Primitives, PrimitivesTag>;
using Access =
AccessTraits<Primitives, Details::TrueTag<Primitives, PrimitivesTag>>;
static_assert(
!Kokkos::is_detected<AccessTraitsNotSpecializedArchetypeAlias, Access>{},
"Must specialize 'AccessTraits<Primitives,PrimitivesTag>'");
Expand Down Expand Up @@ -193,7 +210,7 @@ template <typename Values, typename Tag>
class AccessValuesI
{
private:
using Access = AccessTraits<Values, Tag>;
using Access = AccessTraits<Values, TrueTag<Values, Tag>>;
Values _values;

public:
Expand All @@ -210,7 +227,7 @@ class AccessValuesI
KOKKOS_FUNCTION
auto size() const { return Access::size(_values); }

using self_type = AccessValuesI<Values, Tag>;
using self_type = AccessValuesI<Values, TrueTag<Values, Tag>>;
};

template <typename D, typename... P, typename Tag>
Expand All @@ -224,7 +241,8 @@ template <typename Values, typename Tag1, typename Tag2>
class AccessValuesI<AccessValuesI<Values, Tag1>, Tag2>
: public AccessValuesI<Values, Tag1>
{
static_assert(std::is_same_v<Tag1, Tag2>);
static_assert(std::is_same_v<Tag1, Tag2> || std::is_same_v<Tag1, AnyTag> ||
std::is_same_v<Tag2, AnyTag>);
};

template <typename Values, typename Tag>
Expand Down Expand Up @@ -261,11 +279,10 @@ struct Access
};
} // namespace Traits
template <typename T, typename Tag>
struct AccessTraits<
T, Tag,
std::enable_if_t<!Kokkos::is_detected<
AccessTraitsNotSpecializedArchetypeAlias, Traits::Access<T, Tag>>{}>>
: Traits::Access<T, Tag>
struct AccessTraits<T, Tag,
std::enable_if_t<!Kokkos::is_detected<
Details::AccessTraitsNotSpecializedArchetypeAlias,
Traits::Access<T, Tag>>{}>> : Traits::Access<T, Tag>
{
template <class U>
static constexpr bool always_false = std::is_void_v<U>;
Expand Down
6 changes: 4 additions & 2 deletions src/details/ArborX_AttachIndices.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ struct ArborX::AccessTraits<ArborX::Experimental::AttachIndices<Values, Index>,
{
private:
using Self = ArborX::Experimental::AttachIndices<Values, Index>;
using Access = AccessTraits<Values, ArborX::PrimitivesTag>;
using Access =
AccessTraits<Values, Details::TrueTag<Values, ArborX::PrimitivesTag>>;
using value_type = ArborX::PairValueIndex<
std::decay_t<Kokkos::detected_t<
ArborX::Details::AccessTraitsGetArchetypeExpression, Access, Values>>,
Expand All @@ -67,7 +68,8 @@ struct ArborX::AccessTraits<ArborX::Experimental::AttachIndices<Values, Index>,
{
private:
using Self = ArborX::Experimental::AttachIndices<Values, Index>;
using Access = AccessTraits<Values, ArborX::PredicatesTag>;
using Access =
AccessTraits<Values, Details::TrueTag<Values, ArborX::PredicatesTag>>;

public:
using memory_space = typename Access::memory_space;
Expand Down
2 changes: 1 addition & 1 deletion src/details/ArborX_DetailsDistributedTreeNearest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct WithinDistanceFromPredicates

template <class Predicates, class Distances>
struct AccessTraits<
Details::WithinDistanceFromPredicates<Predicates, Distances>, PredicatesTag>
Details::WithinDistanceFromPredicates<Predicates, Distances>>
{
using Predicate = typename Predicates::value_type;
using Geometry =
Expand Down
3 changes: 2 additions & 1 deletion src/details/ArborX_DetailsLegacy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ namespace ArborX::Details
template <typename Primitives, typename BoundingVolume>
class LegacyValues
{
using Access = AccessTraits<Primitives, TrueTag<Primitives, PrimitivesTag>>;

Primitives _primitives;
using Access = AccessTraits<Primitives, PrimitivesTag>;

public:
using memory_space = typename Access::memory_space;
Expand Down
Loading