diff --git a/src/details/ArborX_DetailsHappyTreeFriends.hpp b/src/details/ArborX_DetailsHappyTreeFriends.hpp index b481be6c5..ab6fdd59c 100644 --- a/src/details/ArborX_DetailsHappyTreeFriends.hpp +++ b/src/details/ArborX_DetailsHappyTreeFriends.hpp @@ -51,7 +51,7 @@ struct HappyTreeFriends } template - static KOKKOS_FUNCTION auto const &getIndexable(BVH const &bvh, int i) + static KOKKOS_FUNCTION decltype(auto) getIndexable(BVH const &bvh, int i) { return bvh._indexable_getter(getValue(bvh, i)); } diff --git a/test/tstIndexableGetter.cpp b/test/tstIndexableGetter.cpp index 4c0b16b55..1914c5be8 100644 --- a/test/tstIndexableGetter.cpp +++ b/test/tstIndexableGetter.cpp @@ -11,6 +11,7 @@ #include "ArborX_EnableDeviceTypes.hpp" // ARBORX_DEVICE_TYPES #include #include +#include #include "BoostTest_CUDA_clang_workarounds.hpp" #include @@ -134,4 +135,53 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(indexables, DeviceType, ARBORX_DEVICE_TYPES) } } +template +struct ReturnByValueIndexableGetter +{ + Kokkos::View _geometries; + + KOKKOS_FUNCTION auto operator()(ArborX::PairValueIndex value) const + { + return _geometries(value.index); + } +}; + +BOOST_AUTO_TEST_CASE_TEMPLATE(indexables_by_value, DeviceType, + ARBORX_DEVICE_TYPES) +{ + // Test that the there is no reference to temporary when indexable getter + // returns by value. Would produce a warning. + + using ExecutionSpace = typename DeviceType::execution_space; + using MemorySpace = typename DeviceType::memory_space; + + using Point = ArborX::Point<3>; + + Kokkos::View points("Testing::points", 2); + auto points_host = Kokkos::create_mirror_view(points); + points_host(0) = {-1, -1, -1}; + points_host(1) = {1, 1, 1}; + Kokkos::deep_copy(points, points_host); + + using Value = ArborX::PairValueIndex; + + using IndexableGetter = ReturnByValueIndexableGetter; + ArborX::BoundingVolumeHierarchy bvh( + ExecutionSpace{}, ArborX::Experimental::attach_indices(points), + IndexableGetter{points}); + + Kokkos::View *, MemorySpace> nearest_queries( + "Testing::nearest_queries", 1); + auto nearest_queries_host = Kokkos::create_mirror_view(nearest_queries); + nearest_queries_host(0) = ArborX::nearest({0, 0, 0}, 1); + deep_copy(nearest_queries, nearest_queries_host); + + // We need to go through HappyTreeFriends to trigger the warning, + // as that's where we use `getIndexable()` + Kokkos::View offsets("Testing::offsets", 0); + Kokkos::View values("Testing::values", 0); + bvh.query(ExecutionSpace{}, nearest_queries, values, offsets); + BOOST_TEST(offsets.size() == 2); // to prevent compiler to optimize out +} + BOOST_AUTO_TEST_SUITE_END()