Skip to content

Commit

Permalink
Merge pull request #37159 from fwyzard/implement_HostDeviceConstant
Browse files Browse the repository at this point in the history
Define HOST_DEVICE_CONSTANT to implement constexpr aggregate host/device constants
  • Loading branch information
cmsbuild authored Mar 8, 2022
2 parents 692e417 + cae2de9 commit 7394df0
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions FWCore/Utilities/interface/HostDeviceConstant.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef FWCore_Utilities_HostDeviceConstant_h
#define FWCore_Utilities_HostDeviceConstant_h

// The use of host-side consexpr constants in device code is limited to:
// - scalars (other than `long double`)
// - scalar elements of aggregates used inside `constexpr` functions
//
// See https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#constexpr-variables .
//
// In particular, it's not possible to use constexpr scalars by pointer or reference (e.g. std::min() takes arguments
// by reference), or pass constexpr arrays as pointers, or access elements of constexpr arrays outside of constexpr
// functions.
//
// The workaround is to define a macro that evaluates to "constexpr" on the host, and "__device__ constexpr" on the
// device. Such macro can be used to declare aggregate objects that are available both on the host and on the device.
// Note these objects may be at different memory addresses on the host and device, so their pointers will be different
// -- but the actual values should be the same.

#ifdef __CUDA_ARCH__
#define HOST_DEVICE_CONSTANT __device__ constexpr
#else
#define HOST_DEVICE_CONSTANT constexpr
#endif

#endif // FWCore_Utilities_HostDeviceConstant_h

0 comments on commit 7394df0

Please sign in to comment.