CMS 3D CMS Logo

cudastdAlgorithm.h
Go to the documentation of this file.
1 #ifndef HeterogeneousCore_CUDAUtilities_cudastdAlgorithm_h
2 #define HeterogeneousCore_CUDAUtilities_cudastdAlgorithm_h
3 
4 #include <utility>
5 
6 #include <cuda_runtime.h>
7 
8 // reimplementation of std algorithms able to compile with CUDA and run on GPUs,
9 // mostly by declaringthem constexpr
10 
11 namespace cuda_std {
12 
13  template <typename T = void>
14  struct less {
15  __host__ __device__ constexpr bool operator()(const T &lhs, const T &rhs) const { return lhs < rhs; }
16  };
17 
18  template <>
19  struct less<void> {
20  template <typename T, typename U>
21  __host__ __device__ constexpr bool operator()(const T &lhs, const U &rhs) const {
22  return lhs < rhs;
23  }
24  };
25 
26  template <typename RandomIt, typename T, typename Compare = less<T>>
27  __host__ __device__ constexpr RandomIt lower_bound(RandomIt first, RandomIt last, const T &value, Compare comp = {}) {
28  auto count = last - first;
29 
30  while (count > 0) {
31  auto it = first;
32  auto step = count / 2;
33  it += step;
34  if (comp(*it, value)) {
35  first = ++it;
36  count -= step + 1;
37  } else {
38  count = step;
39  }
40  }
41  return first;
42  }
43 
44  template <typename RandomIt, typename T, typename Compare = less<T>>
45  __host__ __device__ constexpr RandomIt upper_bound(RandomIt first, RandomIt last, const T &value, Compare comp = {}) {
46  auto count = last - first;
47 
48  while (count > 0) {
49  auto it = first;
50  auto step = count / 2;
51  it += step;
52  if (!comp(value, *it)) {
53  first = ++it;
54  count -= step + 1;
55  } else {
56  count = step;
57  }
58  }
59  return first;
60  }
61 
62  template <typename RandomIt, typename T, typename Compare = cuda_std::less<T>>
63  __host__ __device__ constexpr RandomIt binary_find(RandomIt first, RandomIt last, const T &value, Compare comp = {}) {
65  return first != last && !comp(value, *first) ? first : last;
66  }
67 
68 } // namespace cuda_std
69 
70 #endif // HeterogeneousCore_CUDAUtilities_cudastdAlgorithm_h
#define __host__
__host__ __device__ constexpr RandomIt upper_bound(RandomIt first, RandomIt last, const T &value, Compare comp={})
__host__ __device__ constexpr bool operator()(const T &lhs, const U &rhs) const
TEMPL(T2) struct Divides void
Definition: Factorize.h:24
__host__ __device__ constexpr RandomIt binary_find(RandomIt first, RandomIt last, const T &value, Compare comp={})
Definition: value.py:1
__host__ __device__ constexpr bool operator()(const T &lhs, const T &rhs) const
__host__ __device__ constexpr RandomIt lower_bound(RandomIt first, RandomIt last, const T &value, Compare comp={})
step
Definition: StallMonitor.cc:98
long double T
#define __device__