Tarek
Tarek

Reputation: 1090

declaring device constant in terms of another constant

When I try to define a constant in terms of another constant, both stored in device constant memory, as in:

__device__ __constant__ float x=0.1;
__device__ __constant__ float y=2*x;

I get the error:

error: can't generate code for non empty constructors or destructors on device

Any hints ?

Upvotes: 2

Views: 1700

Answers (1)

njuffa
njuffa

Reputation: 26205

__constant__ is not the same as const. In particular, a __constant__ object can be modified from the host. So the compiler cannot apply compile time evaluation. A __constant__ object cannot be written from within the device code at runtime, so runtime initialization is also not possible. In addition, there is no init routine for the device that could perform such an initialization prior to the actual kernel code commencing execution. The error message produced by the compiler seems to allude to that last fact.

You could use defined constants, for example:

#define MAGIC_NUMBER_1  (0.1f)
#define MAGIC_NUMBER_2  (2.0f * MAGIC_NUMBER_1)

__constant__ float x = MAGIC_NUMBER_1;
__constant__ float y = MAGIC_NUMBER_2;

Upvotes: 6

Related Questions