Skip to content

Abstract the use of constexpr aggregate host/device constants #36311

@fwyzard

Description

@fwyzard

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

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 that we found, is to write portable code that declare two copies of the constants, one on the host and one on the device:

#ifdef __CUDA_ARCH__
__device__
#endif
constexpr uint32_t values[N] = { ... };

We should probably abstract it with something like

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

to be used as

HOST_DEVICE_CONSTANT uint32_t values[N] = { ... };

Suggestions for a better name are welcome :-)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions