在cuda中定义模板化常量变量

时间:2021-04-12 17:03:25

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

如何在cuda中实现模板化常量变量。我有一个结构

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.

到const_data并最终在设备代码中访问它。如果有人指出如何实现这一点,那就太好了。提前致谢。

1 个解决方案

#1


3  

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.

在CUDA中,__ constant__变量隐含了静态存储。从你的问题中你不清楚你想要实例化常量内存变量,但鉴于常量内存变量是静态的并且需要在标准编译模型中的同一个转换单元中声明和使用,你的选择是相当有限。

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.

但是,据我所知,这就是你能做的一切。

#1


3  

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.

在CUDA中,__ constant__变量隐含了静态存储。从你的问题中你不清楚你想要实例化常量内存变量,但鉴于常量内存变量是静态的并且需要在标准编译模型中的同一个转换单元中声明和使用,你的选择是相当有限。

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.

但是,据我所知,这就是你能做的一切。