问题描述
如何在 cuda
中实现模板常量变量。我有一个 struct
How do I implement templated constant variable in cuda
. I have a struct
template<typename T> mystruct{ T d1; T d2[10];}
我想有一个常量变量与上述 struct
并使用下面的代码(此时代码可能不正确)
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;
此后在main内我要复制一些
after this within main I want to copy some
mystruct<float> data;
到 const_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.
推荐答案
在CUDA中, __ constant __
变量意味着静态存储。从你的问题不清楚什么时候你想实例化常量内存变量,但是由于常量内存变量是静态的,需要在标准编译模型的同一个翻译单元中声明和使用,你的选项是非常有限。
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.
这篇关于在cuda中定义模板常量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!