From 9042f4c5c1d58b4c98c1e4873ab8ece7268b240d Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Wed, 11 Jan 2023 11:10:50 -0500 Subject: [PATCH] Allow strongly typed index-rank pairs in parallel tests --- test/ArborX_BoostRTreeHelpers.hpp | 18 +++--- test/Search_UnitTestHelpers.hpp | 47 +++++++++------ test/tstDistributedTree.cpp | 45 +++++++++----- test/tstKokkosToolsAnnotations.cpp | 60 +++++++++---------- test/tstKokkosToolsDistributedAnnotations.cpp | 60 +++++++++---------- test/tstQueryTreeComparisonWithBoost.cpp | 12 ++-- 6 files changed, 134 insertions(+), 108 deletions(-) diff --git a/test/ArborX_BoostRTreeHelpers.hpp b/test/ArborX_BoostRTreeHelpers.hpp index 9a19ccbccb..b144dec2f4 100644 --- a/test/ArborX_BoostRTreeHelpers.hpp +++ b/test/ArborX_BoostRTreeHelpers.hpp @@ -180,8 +180,7 @@ static auto translate(ArborX::Nearest const &query) return boost::geometry::index::nearest(geometry, k); } -template > +template static std::tuple performQueries(RTree const &rtree, InputView const &queries) { @@ -206,10 +205,8 @@ performQueries(RTree const &rtree, InputView const &queries) } #ifdef ARBORX_ENABLE_MPI -template *, Kokkos::HostSpace>, - typename OutputView2 = Kokkos::View> +template static std::tuple performQueries(ParallelRTree const &rtree, InputView const &queries) { @@ -267,7 +264,7 @@ class RTree static_assert(Kokkos::is_execution_space::value); std::tie(offset, indices) = - BoostRTreeHelpers::performQueries(_tree, predicates); + BoostRTreeHelpers::performQueries(_tree, predicates); } template ::value); - std::tie(offset, indices) = - BoostRTreeHelpers::performQueries(_tree, predicates); + auto result = + BoostRTreeHelpers::performQueries(_tree, predicates); + offset = std::get<0>(result); + indices = std::get<1>(result); } private: diff --git a/test/Search_UnitTestHelpers.hpp b/test/Search_UnitTestHelpers.hpp index 9790c76498..68a1e6b30b 100644 --- a/test/Search_UnitTestHelpers.hpp +++ b/test/Search_UnitTestHelpers.hpp @@ -67,14 +67,13 @@ void query(ArborX::DistributedTree const &tree, } #endif -template +template auto query(ExecutionSpace const &exec_space, Tree const &tree, Queries const &queries) { using memory_space = typename Tree::memory_space; - using value_type = - std::conditional_t{}, Kokkos::pair, int>; - Kokkos::View values("Testing::values", 0); + Kokkos::View values("Testing::values", 0); Kokkos::View offsets("Testing::offsets", 0); tree.query(exec_space, queries, values, offsets); return make_compressed_storage( @@ -83,8 +82,11 @@ auto query(ExecutionSpace const &exec_space, Tree const &tree, } #define ARBORX_TEST_QUERY_TREE(exec_space, tree, queries, reference) \ - BOOST_TEST(query(exec_space, tree, queries) == (reference), \ - boost::test_tools::per_element()); + { \ + using value_type = typename decltype(reference)::value_type; \ + BOOST_TEST(query(exec_space, tree, queries) == (reference), \ + boost::test_tools::per_element()); \ + } template @@ -102,24 +104,26 @@ auto query(ExecutionSpace const &exec_space, Tree const &tree, #define ARBORX_TEST_QUERY_TREE_CALLBACK(exec_space, tree, queries, callback, \ reference) \ - using value_type = typename decltype(reference)::value_type; \ - BOOST_TEST(query(exec_space, tree, queries, callback) == \ - (reference), \ - boost::test_tools::per_element()); + { \ + using value_type = typename decltype(reference)::value_type; \ + BOOST_TEST(query(exec_space, tree, queries, callback) == \ + (reference), \ + boost::test_tools::per_element()); \ + } // Workaround for NVCC that complains that the enclosing parent function // (query_with_distance) for an extended __host__ __device__ lambda must not // have deduced return type -template -Kokkos::View, float> *, DeviceType> +template +Kokkos::View zip(ExecutionSpace const &space, Kokkos::View indices, Kokkos::View ranks, Kokkos::View distances) { auto const n = indices.extent(0); - Kokkos::View, float> *, DeviceType> - values(Kokkos::view_alloc(Kokkos::WithoutInitializing, "Testing::values"), - n); + Kokkos::View values( + Kokkos::view_alloc(Kokkos::WithoutInitializing, "Testing::values"), n); Kokkos::parallel_for( "ArborX:UnitTestSupport:zip", Kokkos::RangePolicy(space, 0, n), KOKKOS_LAMBDA(int i) { @@ -129,7 +133,8 @@ zip(ExecutionSpace const &space, Kokkos::View indices, } #ifdef ARBORX_ENABLE_MPI -template +template auto query_with_distance(ExecutionSpace const &exec_space, Tree const &tree, Queries const &queries, std::enable_if_t{}> * = nullptr) @@ -145,7 +150,7 @@ auto query_with_distance(ExecutionSpace const &exec_space, Tree const &tree, ArborX::Details::NearestPredicateTag{}, tree, exec_space, queries, indices, offsets, ranks, &distances); - auto values = zip(exec_space, indices, ranks, distances); + auto values = zip(exec_space, indices, ranks, distances); return make_compressed_storage( Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, offsets), @@ -155,8 +160,12 @@ auto query_with_distance(ExecutionSpace const &exec_space, Tree const &tree, #define ARBORX_TEST_QUERY_TREE_WITH_DISTANCE(exec_space, tree, queries, \ reference) \ - BOOST_TEST(query_with_distance(exec_space, tree, queries) == (reference), \ - boost::test_tools::per_element()); + { \ + using value_type = typename decltype(reference)::value_type; \ + BOOST_TEST(query_with_distance(exec_space, tree, queries) == \ + (reference), \ + boost::test_tools::per_element()); \ + } template auto make(ExecutionSpace const &exec_space, std::vector const &b) diff --git a/test/tstDistributedTree.cpp b/test/tstDistributedTree.cpp index 953d43cffe..a4ce705f0d 100644 --- a/test/tstDistributedTree.cpp +++ b/test/tstDistributedTree.cpp @@ -28,8 +28,26 @@ namespace tt = boost::test_tools; -using PairIndexRank = Kokkos::pair; -using TupleIndexRankDistance = Kokkos::pair, float>; +struct PairIndexRank +{ + int index; + int rank; + friend bool operator==(PairIndexRank lhs, PairIndexRank rhs) + { + return lhs.index == rhs.index && lhs.rank == rhs.rank; + } + friend bool operator<(PairIndexRank lhs, PairIndexRank rhs) + { + return lhs.index < rhs.index || + (lhs.index == rhs.index && lhs.rank < rhs.rank); + } + friend std::ostream &operator<<(std::ostream &stream, + PairIndexRank const &pair) + { + return stream << '[' << pair.index << ',' << pair.rank << ']'; + } +}; +using TupleIndexRankDistance = Kokkos::pair; BOOST_AUTO_TEST_CASE_TEMPLATE(hello_world, DeviceType, ARBORX_DEVICE_TYPES) { @@ -96,11 +114,11 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(hello_world, DeviceType, ARBORX_DEVICE_TYPES) values.reserve(n + 1); for (int i = 0; i < n; ++i) { - values.emplace_back(n - 1 - i, comm_size - 1 - comm_rank); + values.push_back({n - 1 - i, comm_size - 1 - comm_rank}); } if (comm_rank > 0) { - values.emplace_back(0, comm_size - comm_rank); + values.push_back({0, comm_size - comm_rank}); ARBORX_TEST_QUERY_TREE(ExecutionSpace{}, tree, queries, make_reference_solution(values, {0, n + 1})); } @@ -317,19 +335,15 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(one_leaf_per_rank, DeviceType, if (comm_rank > 0) { + std::vector values; + values.reserve(comm_size); + for (int i = 0; i < comm_size; ++i) + values.push_back({0, i}); ARBORX_TEST_QUERY_TREE(ExecutionSpace{}, tree, makeNearestQueries({ {{{0., 0., 0.}}, comm_rank * comm_size}, }), - make_reference_solution( - [comm_size]() { - std::vector values; - values.reserve(comm_size); - for (int i = 0; i < comm_size; ++i) - values.emplace_back(0, i); - return values; - }(), - {0, comm_size})); + make_reference_solution(values, {0, comm_size})); } else { @@ -812,8 +826,9 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(boost_comparison, DeviceType, ARBORX_DEVICE_TYPES) BoostExt::ParallelRTree rtree(comm, ExecutionSpace{}, bounding_boxes_host); - ARBORX_TEST_QUERY_TREE(ExecutionSpace{}, distributed_tree, within_queries, - query(ExecutionSpace{}, rtree, within_queries_host)); + ARBORX_TEST_QUERY_TREE( + ExecutionSpace{}, distributed_tree, within_queries, + query(ExecutionSpace{}, rtree, within_queries_host)); } template diff --git a/test/tstKokkosToolsAnnotations.cpp b/test/tstKokkosToolsAnnotations.cpp index 17bd1493ef..bc0f70607b 100644 --- a/test/tstKokkosToolsAnnotations.cpp +++ b/test/tstKokkosToolsAnnotations.cpp @@ -100,18 +100,18 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(bvh_query_allocations_prefixed, DeviceType, }); // spatial predicates - query(ExecutionSpace{}, tree, - makeIntersectsBoxQueries({ - {{{0, 0, 0}}, {{1, 1, 1}}}, - {{{0, 0, 0}}, {{1, 1, 1}}}, - })); + query(ExecutionSpace{}, tree, + makeIntersectsBoxQueries({ + {{{0, 0, 0}}, {{1, 1, 1}}}, + {{{0, 0, 0}}, {{1, 1, 1}}}, + })); // nearest predicates - query(ExecutionSpace{}, tree, - makeNearestQueries({ - {{{0, 0, 0}}, 1}, - {{{0, 0, 0}}, 2}, - })); + query(ExecutionSpace{}, tree, + makeNearestQueries({ + {{{0, 0, 0}}, 1}, + {{{0, 0, 0}}, 2}, + })); Kokkos::Tools::Experimental::set_allocate_data_callback(nullptr); } @@ -161,18 +161,18 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(kernels_prefixed, DeviceType, ARBORX_DEVICE_TYPES) }); // spatial predicates - query(ExecutionSpace{}, tree, - makeIntersectsBoxQueries({ - {{{0, 0, 0}}, {{1, 1, 1}}}, - {{{0, 0, 0}}, {{1, 1, 1}}}, - })); + query(ExecutionSpace{}, tree, + makeIntersectsBoxQueries({ + {{{0, 0, 0}}, {{1, 1, 1}}}, + {{{0, 0, 0}}, {{1, 1, 1}}}, + })); // nearest predicates - query(ExecutionSpace{}, tree, - makeNearestQueries({ - {{{0, 0, 0}}, 1}, - {{{0, 0, 0}}, 2}, - })); + query(ExecutionSpace{}, tree, + makeNearestQueries({ + {{{0, 0, 0}}, 1}, + {{{0, 0, 0}}, 2}, + })); Kokkos::Tools::Experimental::set_begin_parallel_for_callback(nullptr); Kokkos::Tools::Experimental::set_begin_parallel_scan_callback(nullptr); @@ -221,18 +221,18 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(regions_prefixed, DeviceType, ARBORX_DEVICE_TYPES) }); // spatial predicates - query(ExecutionSpace{}, tree, - makeIntersectsBoxQueries({ - {{{0, 0, 0}}, {{1, 1, 1}}}, - {{{0, 0, 0}}, {{1, 1, 1}}}, - })); + query(ExecutionSpace{}, tree, + makeIntersectsBoxQueries({ + {{{0, 0, 0}}, {{1, 1, 1}}}, + {{{0, 0, 0}}, {{1, 1, 1}}}, + })); // nearest predicates - query(ExecutionSpace{}, tree, - makeNearestQueries({ - {{{0, 0, 0}}, 1}, - {{{0, 0, 0}}, 2}, - })); + query(ExecutionSpace{}, tree, + makeNearestQueries({ + {{{0, 0, 0}}, 1}, + {{{0, 0, 0}}, 2}, + })); Kokkos::Tools::Experimental::set_push_region_callback(nullptr); } diff --git a/test/tstKokkosToolsDistributedAnnotations.cpp b/test/tstKokkosToolsDistributedAnnotations.cpp index 351b163a1d..7ec016cf28 100644 --- a/test/tstKokkosToolsDistributedAnnotations.cpp +++ b/test/tstKokkosToolsDistributedAnnotations.cpp @@ -84,18 +84,18 @@ BOOST_AUTO_TEST_CASE_TEMPLATE( }); // spatial predicates - query(ExecutionSpace{}, tree, - makeIntersectsBoxQueries({ - {{{0, 0, 0}}, {{1, 1, 1}}}, - {{{0, 0, 0}}, {{1, 1, 1}}}, - })); + query>(ExecutionSpace{}, tree, + makeIntersectsBoxQueries({ + {{{0, 0, 0}}, {{1, 1, 1}}}, + {{{0, 0, 0}}, {{1, 1, 1}}}, + })); // nearest predicates - query(ExecutionSpace{}, tree, - makeNearestQueries({ - {{{0, 0, 0}}, 1}, - {{{0, 0, 0}}, 2}, - })); + query>(ExecutionSpace{}, tree, + makeNearestQueries({ + {{{0, 0, 0}}, 1}, + {{{0, 0, 0}}, 2}, + })); Kokkos::Tools::Experimental::set_allocate_data_callback(nullptr); } @@ -122,18 +122,18 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(kernels_prefixed, DeviceType, ARBORX_DEVICE_TYPES) }); // spatial predicates - query(ExecutionSpace{}, tree, - makeIntersectsBoxQueries({ - {{{0, 0, 0}}, {{1, 1, 1}}}, - {{{0, 0, 0}}, {{1, 1, 1}}}, - })); + query>(ExecutionSpace{}, tree, + makeIntersectsBoxQueries({ + {{{0, 0, 0}}, {{1, 1, 1}}}, + {{{0, 0, 0}}, {{1, 1, 1}}}, + })); // nearest predicates - query(ExecutionSpace{}, tree, - makeNearestQueries({ - {{{0, 0, 0}}, 1}, - {{{0, 0, 0}}, 2}, - })); + query>(ExecutionSpace{}, tree, + makeNearestQueries({ + {{{0, 0, 0}}, 1}, + {{{0, 0, 0}}, 2}, + })); Kokkos::Tools::Experimental::set_begin_parallel_for_callback(nullptr); Kokkos::Tools::Experimental::set_begin_parallel_scan_callback(nullptr); @@ -165,18 +165,18 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(regions_prefixed, DeviceType, ARBORX_DEVICE_TYPES) }); // spatial predicates - query(ExecutionSpace{}, tree, - makeIntersectsBoxQueries({ - {{{0, 0, 0}}, {{1, 1, 1}}}, - {{{0, 0, 0}}, {{1, 1, 1}}}, - })); + query>(ExecutionSpace{}, tree, + makeIntersectsBoxQueries({ + {{{0, 0, 0}}, {{1, 1, 1}}}, + {{{0, 0, 0}}, {{1, 1, 1}}}, + })); // nearest predicates - query(ExecutionSpace{}, tree, - makeNearestQueries({ - {{{0, 0, 0}}, 1}, - {{{0, 0, 0}}, 2}, - })); + query>(ExecutionSpace{}, tree, + makeNearestQueries({ + {{{0, 0, 0}}, 1}, + {{{0, 0, 0}}, 2}, + })); Kokkos::Tools::Experimental::set_push_region_callback(nullptr); } diff --git a/test/tstQueryTreeComparisonWithBoost.cpp b/test/tstQueryTreeComparisonWithBoost.cpp index b8afd8e4d5..8f2b1eaa51 100644 --- a/test/tstQueryTreeComparisonWithBoost.cpp +++ b/test/tstQueryTreeComparisonWithBoost.cpp @@ -153,8 +153,9 @@ void boost_rtree_nearest_predicate() BoostExt::RTree rtree(ExecutionSpace{}, cloud); - ARBORX_TEST_QUERY_TREE(ExecutionSpace{}, tree, nearest_queries, - query(ExecutionSpace{}, rtree, nearest_queries_host)); + ARBORX_TEST_QUERY_TREE( + ExecutionSpace{}, tree, nearest_queries, + query(ExecutionSpace{}, rtree, nearest_queries_host)); } BOOST_AUTO_TEST_CASE_TEMPLATE(boost_rtree_spatial_predicate, TreeTypeTraits, @@ -235,10 +236,11 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(boost_rtree_spatial_predicate, TreeTypeTraits, ARBORX_TEST_QUERY_TREE( ExecutionSpace{}, tree, intersects_queries, - query(ExecutionSpace{}, rtree, intersects_queries_host)); + query(ExecutionSpace{}, rtree, intersects_queries_host)); #ifndef ARBORX_TEST_DISABLE_SPATIAL_QUERY_INTERSECTS_SPHERE - ARBORX_TEST_QUERY_TREE(ExecutionSpace{}, tree, within_queries, - query(ExecutionSpace{}, rtree, within_queries_host)); + ARBORX_TEST_QUERY_TREE( + ExecutionSpace{}, tree, within_queries, + query(ExecutionSpace{}, rtree, within_queries_host)); #endif }