Skip to content

Commit dce92a5

Browse files
authored
Build clusters associator with clustered point size (#305)
* Define heterogeneous `count_if` algorithm * Build clusters associator with true value size
1 parent fb85661 commit dce92a5

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
#pragma once
3+
4+
#include <alpaka/alpaka.hpp>
5+
6+
#if defined(ALPAKA_ACC_GPU_CUDA_ENABLED) and not defined(ALPAKA_HOST_ONLY)
7+
#include <thrust/count.h>
8+
#include <thrust/execution_policy.h>
9+
#elif defined(ALPAKA_ACC_GPU_HIP_ENABLED) and not defined(ALPAKA_HOST_ONLY)
10+
#include <thrust/count.h>
11+
#include <thrust/execution_policy.h>
12+
#elif defined(ALPAKA_ACC_SYCL_ENABLED)
13+
#include <oneapi/dpl/algorithm>
14+
#include <oneapi/dpl/execution>
15+
#else
16+
#include <algorithm>
17+
#endif
18+
19+
namespace clue::internal::algorithm {
20+
21+
template <typename InputIterator, typename Predicate>
22+
ALPAKA_FN_HOST inline constexpr auto count_if(InputIterator first,
23+
InputIterator last,
24+
Predicate pred) {
25+
#if defined(ALPAKA_ACC_GPU_CUDA_ENABLED) and not defined(ALPAKA_HOST_ONLY)
26+
return thrust::count_if(thrust::device, first, last, pred);
27+
#elif defined(ALPAKA_ACC_GPU_HIP_ENABLED) and not defined(ALPAKA_HOST_ONLY)
28+
return thrust::count_if(thrust::hip::par, first, last, pred);
29+
#elif defined(ALPAKA_ACC_SYCL_ENABLED)
30+
return oneapi::dpl::count_if(oneapi::dpl::execution::dpcpp_default, first, last, pred);
31+
#else
32+
return std::count_if(first, last, pred);
33+
#endif
34+
}
35+
36+
template <typename ExecutionPolicy, typename InputIterator, typename Predicate>
37+
ALPAKA_FN_HOST inline constexpr auto count_if(ExecutionPolicy&& policy,
38+
InputIterator first,
39+
InputIterator last,
40+
Predicate pred) {
41+
#if defined(ALPAKA_ACC_GPU_CUDA_ENABLED) and not defined(ALPAKA_HOST_ONLY)
42+
return thrust::count_if(std::forward<ExecutionPolicy>(policy), first, last, pred);
43+
#elif defined(ALPAKA_ACC_GPU_HIP_ENABLED) and not defined(ALPAKA_HOST_ONLY)
44+
return thrust::count_if(std::forward<ExecutionPolicy>(policy), first, last, pred);
45+
#elif defined(ALPAKA_ACC_SYCL_ENABLED)
46+
return oneapi::dpl::count_if(std::forward<ExecutionPolicy>(policy), first, last, pred);
47+
#else
48+
return std::count_if(std::forward<ExecutionPolicy>(policy), first, last, pred);
49+
#endif
50+
}
51+
52+
} // namespace clue::internal::algorithm

include/CLUEstering/utils/detail/get_clusters.hpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11

22
#pragma once
33

4-
#include "CLUEstering/utils/get_clusters.hpp"
4+
#include "CLUEstering/data_structures/PointsHost.hpp"
5+
#include "CLUEstering/data_structures/PointsDevice.hpp"
56
#include "CLUEstering/data_structures/internal/MakeAssociator.hpp"
7+
#include "CLUEstering/detail/concepts.hpp"
8+
#include "CLUEstering/internal/algorithm/count_if/count_if.hpp"
9+
#include "CLUEstering/utils/get_clusters.hpp"
10+
#include <algorithm>
11+
#include <cassert>
12+
#include <cstddef>
613
#include <span>
714

815
namespace clue {
916
namespace detail {
1017

18+
template <typename T>
19+
struct non_negative {
20+
ALPAKA_FN_HOST_ACC constexpr auto operator()(T value) const { return value >= -1; }
21+
};
22+
1123
inline auto get_clusters(std::span<const int> cluster_ids) {
12-
return internal::make_associator(cluster_ids, static_cast<int32_t>(cluster_ids.size()));
24+
auto clustered_points = std::ranges::count_if(cluster_ids, non_negative<int>{});
25+
return internal::make_associator(cluster_ids, static_cast<int>(clustered_points));
1326
}
1427

1528
template <concepts::queue TQueue>
1629
inline auto get_clusters(TQueue& queue, std::span<const int> cluster_ids) {
17-
return internal::make_associator(
18-
queue, cluster_ids, static_cast<int32_t>(cluster_ids.size()));
30+
auto clustered_points = internal::algorithm::count_if(
31+
cluster_ids.begin(), cluster_ids.end(), non_negative<int>{});
32+
return internal::make_associator(queue, cluster_ids, clustered_points);
1933
}
2034

2135
} // namespace detail

include/CLUEstering/utils/get_clusters.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
#pragma once
66

7-
#include "CLUEstering/data_structures/AssociationMap.hpp"
87
#include "CLUEstering/data_structures/PointsHost.hpp"
98
#include "CLUEstering/data_structures/PointsDevice.hpp"
9+
#include "CLUEstering/detail/concepts.hpp"
10+
#include <cstddef>
1011

1112
namespace clue {
1213

0 commit comments

Comments
 (0)