Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define HOST_DEVICE_CONSTANT to implement constexpr aggregate host/device constants #37159

Merged
merged 1 commit into from
Mar 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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