CMS 3D CMS Logo

cudaCompat.h
Go to the documentation of this file.
1 #ifndef HeterogeneousCore_CUDAUtilities_interface_cudaCompat_h
2 #define HeterogeneousCore_CUDAUtilities_interface_cudaCompat_h
3 
4 /*
5  * Everything you need to run cuda code in plain sequential c++ code
6  */
7 
8 #ifndef __CUDACC__
9 
10 #include <algorithm>
11 #include <cstdint>
12 #include <cstring>
13 
14 #include <cuda_runtime.h>
15 
16 namespace cms {
17  namespace cudacompat {
18 
19 #ifndef __CUDA_RUNTIME_H__
20  struct dim3 {
21  uint32_t x, y, z;
22  };
23 #endif
24  const dim3 threadIdx = {0, 0, 0};
25  const dim3 blockDim = {1, 1, 1};
26 
27  extern thread_local dim3 blockIdx;
28  extern thread_local dim3 gridDim;
29 
30  template <typename T1, typename T2>
31  T1 atomicInc(T1* a, T2 b) {
32  auto ret = *a;
33  if ((*a) < T1(b))
34  (*a)++;
35  return ret;
36  }
37 
38  template <typename T1, typename T2>
39  T1 atomicAdd(T1* a, T2 b) {
40  auto ret = *a;
41  (*a) += b;
42  return ret;
43  }
44 
45  template <typename T1, typename T2>
46  T1 atomicSub(T1* a, T2 b) {
47  auto ret = *a;
48  (*a) -= b;
49  return ret;
50  }
51 
52  template <typename T1, typename T2>
53  T1 atomicMin(T1* a, T2 b) {
54  auto ret = *a;
55  *a = std::min(*a, T1(b));
56  return ret;
57  }
58  template <typename T1, typename T2>
59  T1 atomicMax(T1* a, T2 b) {
60  auto ret = *a;
61  *a = std::max(*a, T1(b));
62  return ret;
63  }
64 
65  inline void __syncthreads() {}
66  inline void __threadfence() {}
67  inline bool __syncthreads_or(bool x) { return x; }
68  inline bool __syncthreads_and(bool x) { return x; }
69  template <typename T>
70  inline T __ldg(T const* x) {
71  return *x;
72  }
73 
74  inline void resetGrid() {
75  blockIdx = {0, 0, 0};
76  gridDim = {1, 1, 1};
77  }
78 
79  } // namespace cudacompat
80 } // namespace cms
81 
82 // some not needed as done by cuda runtime...
83 #ifndef __CUDA_RUNTIME_H__
84 #define __host__
85 #define __device__
86 #define __global__
87 #define __shared__
88 #define __forceinline__
89 #endif
90 
91 // make sure function are inlined to avoid multiple definition
92 #ifndef __CUDA_ARCH__
93 #undef __global__
94 #define __global__ inline __attribute__((always_inline))
95 #undef __forceinline__
96 #define __forceinline__ inline __attribute__((always_inline))
97 #endif
98 
99 #ifndef __CUDA_ARCH__
100 using namespace cms::cudacompat;
101 #endif
102 
103 #endif
104 
105 #endif // HeterogeneousCore_CUDAUtilities_interface_cudaCompat_h
runTheMatrix.ret
ret
prodAgent to be discontinued
Definition: runTheMatrix.py:355
cms::cudacompat::atomicSub
T1 atomicSub(T1 *a, T2 b)
Definition: cudaCompat.h:46
cms::cudacompat
Definition: cudaCompat.h:17
min
T min(T a, T b)
Definition: MathUtil.h:58
cms::cudacompat::__threadfence
void __threadfence()
Definition: cudaCompat.h:66
cms::cudacompat::dim3::y
uint32_t y
Definition: cudaCompat.h:21
cms::cudacompat::gridDim
thread_local dim3 gridDim
Definition: cudaCompat.cc:6
cms::cudacompat::__syncthreads_or
bool __syncthreads_or(bool x)
Definition: cudaCompat.h:67
cms::cudacompat::dim3::z
uint32_t z
Definition: cudaCompat.h:21
cms::cudacompat::atomicAdd
T1 atomicAdd(T1 *a, T2 b)
Definition: cudaCompat.h:39
b
double b
Definition: hdecay.h:118
cms::cudacompat::dim3
Definition: cudaCompat.h:20
cms::cudacompat::resetGrid
void resetGrid()
Definition: cudaCompat.h:74
a
double a
Definition: hdecay.h:119
cms::cudacompat::blockDim
const dim3 blockDim
Definition: cudaCompat.h:25
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
cms::cudacompat::atomicMax
T1 atomicMax(T1 *a, T2 b)
Definition: cudaCompat.h:59
cms::cudacompat::dim3::x
uint32_t x
Definition: cudaCompat.h:21
cms::cudacompat::threadIdx
const dim3 threadIdx
Definition: cudaCompat.h:24
T
long double T
Definition: Basic3DVectorLD.h:48
cms::cudacompat::atomicMin
T1 atomicMin(T1 *a, T2 b)
Definition: cudaCompat.h:53
cms::cudacompat::__syncthreads_and
bool __syncthreads_and(bool x)
Definition: cudaCompat.h:68
cms::cudacompat::__ldg
T __ldg(T const *x)
Definition: cudaCompat.h:70
cms::cudacompat::__syncthreads
void __syncthreads()
Definition: cudaCompat.h:65
cms
Namespace of DDCMS conversion namespace.
Definition: ProducerAnalyzer.cc:21
cms::cudacompat::atomicInc
T1 atomicInc(T1 *a, T2 b)
Definition: cudaCompat.h:31
cms::cudacompat::blockIdx
thread_local dim3 blockIdx
Definition: cudaCompat.cc:5