forked from cms-patatrack/pixeltrack-standalone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcudastdAlgorithm.h
70 lines (57 loc) · 1.98 KB
/
cudastdAlgorithm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef HeterogeneousCore_CUDAUtilities_cudastdAlgorithm_h
#define HeterogeneousCore_CUDAUtilities_cudastdAlgorithm_h
#include <utility>
#include <cuda_runtime.h>
// reimplementation of std algorithms able to compile with CUDA and run on GPUs,
// mostly by declaringthem constexpr
namespace cuda_std {
template <typename T = void>
struct less {
__host__ __device__ constexpr bool operator()(const T &lhs, const T &rhs) const { return lhs < rhs; }
};
template <>
struct less<void> {
template <typename T, typename U>
__host__ __device__ constexpr bool operator()(const T &lhs, const U &rhs) const {
return lhs < rhs;
}
};
template <typename RandomIt, typename T, typename Compare = less<T>>
__host__ __device__ constexpr RandomIt lower_bound(RandomIt first, RandomIt last, const T &value, Compare comp = {}) {
auto count = last - first;
while (count > 0) {
auto it = first;
auto step = count / 2;
it += step;
if (comp(*it, value)) {
first = ++it;
count -= step + 1;
} else {
count = step;
}
}
return first;
}
template <typename RandomIt, typename T, typename Compare = less<T>>
__host__ __device__ constexpr RandomIt upper_bound(RandomIt first, RandomIt last, const T &value, Compare comp = {}) {
auto count = last - first;
while (count > 0) {
auto it = first;
auto step = count / 2;
it += step;
if (!comp(value, *it)) {
first = ++it;
count -= step + 1;
} else {
count = step;
}
}
return first;
}
template <typename RandomIt, typename T, typename Compare = cuda_std::less<T>>
__host__ __device__ constexpr RandomIt binary_find(RandomIt first, RandomIt last, const T &value, Compare comp = {}) {
first = cuda_std::lower_bound(first, last, value, comp);
return first != last && !comp(value, *first) ? first : last;
}
} // namespace cuda_std
#endif // HeterogeneousCore_CUDAUtilities_cudastdAlgorithm_h