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

Add upper_bound kernel #1170

Merged
merged 5 commits into from
Oct 8, 2024
Merged
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
21 changes: 21 additions & 0 deletions src/kokkos_ext/ArborX_KokkosExtKernelStdAlgorithms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ KOKKOS_FUNCTION void nth_element(Iterator first, Iterator nth, Iterator last)
}
}

template <typename Iterator, typename T>
KOKKOS_FUNCTION Iterator upper_bound(Iterator first, Iterator last,
T const &value)
{
int count = last - first;
while (count > 0)
{
int step = count / 2;
if (!(value < *(first + step)))
{
first += step + 1;
count -= step + 1;
}
else
{
count = step;
}
}
return first;
}

} // namespace ArborX::Details::KokkosExt

#endif
13 changes: 10 additions & 3 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,23 @@ add_executable(ArborX_Test_DetailsUtils.exe
tstDetailsVector.cpp
tstDetailsUtils.cpp
tstDetailsGeometryReducer.cpp
utf_main.cpp
)
target_link_libraries(ArborX_Test_DetailsUtils.exe PRIVATE ArborX Boost::unit_test_framework Boost::dynamic_linking)
target_include_directories(ArborX_Test_DetailsUtils.exe PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME ArborX_Test_DetailsUtils COMMAND ArborX_Test_DetailsUtils.exe)

add_executable(ArborX_Test_KokkosExt.exe
tstDetailsKokkosExtStdAlgorithms.cpp
tstDetailsKokkosExtKernelStdAlgorithms.cpp
tstDetailsKokkosExtUninitializedMemoryAlgorithms.cpp
tstDetailsKokkosExtMinMaxReduce.cpp
tstDetailsKokkosExtViewHelpers.cpp
utf_main.cpp
)
target_link_libraries(ArborX_Test_DetailsUtils.exe PRIVATE ArborX Boost::unit_test_framework Boost::dynamic_linking)
target_include_directories(ArborX_Test_DetailsUtils.exe PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME ArborX_Test_DetailsUtils COMMAND ArborX_Test_DetailsUtils.exe)
target_link_libraries(ArborX_Test_KokkosExt.exe PRIVATE ArborX Boost::unit_test_framework Boost::dynamic_linking)
target_include_directories(ArborX_Test_KokkosExt.exe PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME ArborX_Test_KokkosExt COMMAND ArborX_Test_KokkosExt.exe)

add_executable(ArborX_Test_Geometry.exe
tstDetailsAlgorithms.cpp
Expand Down
54 changes: 47 additions & 7 deletions test/tstDetailsKokkosExtKernelStdAlgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@

namespace tt = boost::test_tools;

template <typename T>
using UnmanagedHostView = Kokkos::View<T *, Kokkos::HostSpace,
Kokkos::MemoryTraits<Kokkos::Unmanaged>>;

BOOST_AUTO_TEST_CASE_TEMPLATE(nth_element, DeviceType, ARBORX_DEVICE_TYPES)
{
using ExecutionSpace = typename DeviceType::execution_space;
Expand All @@ -41,18 +45,17 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(nth_element, DeviceType, ARBORX_DEVICE_TYPES)
{
int const n = v_ref.size();

Kokkos::View<float *, DeviceType> v("v", n);
Kokkos::deep_copy(
space, v,
Kokkos::View<float *, Kokkos::HostSpace,
Kokkos::MemoryTraits<Kokkos::Unmanaged>>(v_ref.data(), n));
Kokkos::View<float *, DeviceType> v("Testing::v", 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we were going for "[ArborX]{Test,Example}::foo"

Copy link
Contributor Author

@aprokop aprokop Oct 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use "Testing::foo" and "Example::foo". We use ArborXTest namespaces.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please Testing (does not need to be resolved here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please Testing (does not need to be resolved here

Are you asking to change all Testing:: to Test:: in a separate PR? Or to ArborXTest::?

v = Kokkos::create_mirror_view_and_copy(
space, UnmanagedHostView<float>(v_ref.data(), n));

Kokkos::View<float *, DeviceType> nth("nth", n);
Kokkos::View<float *, DeviceType> nth("Testing::nth", n);
for (int i = 0; i < n; ++i)
{
auto v_copy = ArborX::Details::KokkosExt::clone(space, v);
Kokkos::parallel_for(
Kokkos::RangePolicy(space, 0, 1), KOKKOS_LAMBDA(int) {
"Testing::run_nth_element", Kokkos::RangePolicy(space, 0, 1),
KOKKOS_LAMBDA(int) {
nth_element(v_copy.data(), v_copy.data() + i, v_copy.data() + n);
nth(i) = v_copy(i);
});
Expand All @@ -65,3 +68,40 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(nth_element, DeviceType, ARBORX_DEVICE_TYPES)
BOOST_TEST(nth_host == v_ref, tt::per_element());
}
}

template <typename DeviceType>
int findUpperBound(std::vector<float> const &v_host, float x)
{
auto const n = v_host.size();
Kokkos::View<float *, DeviceType> v("Testing::v", n);
Kokkos::deep_copy(v, UnmanagedHostView<float const>(v_host.data(), n));

int pos;
Kokkos::parallel_reduce(
aprokop marked this conversation as resolved.
Show resolved Hide resolved
"Testing::run_upper_bound",
Kokkos::RangePolicy<typename DeviceType::execution_space>(0, 1),
KOKKOS_LAMBDA(int, int &update) {
update =
ArborX::Details::KokkosExt::upper_bound(v.data(), v.data() + n, x) -
v.data();
},
pos);

return pos;
}

BOOST_AUTO_TEST_CASE_TEMPLATE(upper_bound, DeviceType, ARBORX_DEVICE_TYPES)
{
BOOST_TEST(findUpperBound<DeviceType>({}, 0) == 0);
BOOST_TEST(findUpperBound<DeviceType>({0}, -1) == 0);
BOOST_TEST(findUpperBound<DeviceType>({0}, 0) == 1);
BOOST_TEST(findUpperBound<DeviceType>({0}, 1) == 1);
BOOST_TEST(findUpperBound<DeviceType>({1, 1}, 0) == 0);
BOOST_TEST(findUpperBound<DeviceType>({1, 1}, 1) == 2);
BOOST_TEST(findUpperBound<DeviceType>({1, 3, 5}, 1) == 1);
BOOST_TEST(findUpperBound<DeviceType>({1, 3, 5}, 2) == 1);
BOOST_TEST(findUpperBound<DeviceType>({1, 3, 5}, 3) == 2);
BOOST_TEST(findUpperBound<DeviceType>({1, 3, 5}, 4) == 2);
BOOST_TEST(findUpperBound<DeviceType>({1, 3, 5}, 5) == 3);
BOOST_TEST(findUpperBound<DeviceType>({1, 3, 5, 7, 9, 11, 13}, 8) == 4);
}
Loading