CMS 3D CMS Logo

HostDeviceConstant.h
Go to the documentation of this file.
1 #ifndef FWCore_Utilities_HostDeviceConstant_h
2 #define FWCore_Utilities_HostDeviceConstant_h
3 
4 // The use of host-side consexpr constants in device code is limited to:
5 // - scalars (other than `long double`)
6 // - scalar elements of aggregates used inside `constexpr` functions
7 //
8 // See https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#constexpr-variables .
9 //
10 // In particular, it's not possible to use constexpr scalars by pointer or reference (e.g. std::min() takes arguments
11 // by reference), or pass constexpr arrays as pointers, or access elements of constexpr arrays outside of constexpr
12 // functions.
13 //
14 // The workaround is to define a macro that evaluates to "constexpr" on the host, and "__device__ constexpr" on the
15 // device. Such macro can be used to declare aggregate objects that are available both on the host and on the device.
16 // Note these objects may be at different memory addresses on the host and device, so their pointers will be different
17 // -- but the actual values should be the same.
18 
19 #ifdef __CUDA_ARCH__
20 #define HOST_DEVICE_CONSTANT __device__ constexpr
21 #else
22 #define HOST_DEVICE_CONSTANT constexpr
23 #endif
24 
25 #endif // FWCore_Utilities_HostDeviceConstant_h