user1612986
user1612986

Reputation: 1415

Defining templated constant variables in cuda

How do I implement templated constant variable in cuda. I have a struct

template<typename T> mystruct{ T d1; T d2[10];}

I want to have a constant variable with the above struct and use a code something like below (code may not be correct at this point)

template<typename T> __constant__ mystruct<T> const_data;

after this within main I want to copy some

  mystruct<float> data; 

into const_data and eventually access it within device code. It would be kind if someone points out how to achieve this. Thanks in advance.

Upvotes: 5

Views: 885

Answers (1)

talonmies
talonmies

Reputation: 72349

In CUDA, __constant__ variables have implied static storage. It isn't clear from your question at what point you would want to instantiate the constant memory variable, but given that constant memory variables are static and need to be declared and used within the same translation unit in the standard compilation model, your options are pretty limited.

There is nothing stopping you from defining a templated type and then statically defining a particular instance of that type in constant memory, for example:

template<typename T> struct mystruct{ T d1; T d2[10]; };

__constant__ mystruct<float> const_data;

But, to the best of my knowledge, that is all you can do.

Upvotes: 5

Related Questions