在 CUDA 上使用 __constant__ 内存的正确方法?

标签 c cuda constants

我有一个数组,我想在 CUDA 设备上的 __constant__ 内存中初始化。直到运行时我才知道它的大小或值。

我知道我可以使用 __constant__ float Points[**N**][2] 或类似的东西,但我如何使它动态化?也许以 __constant__ float* Points 的形式?

这可能吗?可能更重要的是,这是个好主意吗?如果有比这样做更好的替代方法,我很乐意听取他们的意见。

最佳答案

正如在 Dynamic Allocation of Constant memory in CUDA 中讨论的那样和 Constant memory allocation and initialization , 你不能。如 Constant memory allocation and initialization 中所述,

Constants are embedded into executable at compile time (that's why you have to copy bytes to addresses specified by symbols to set constant values at run-time ). So, you wouldn't be able to allocate constant arrays of different sizes for different invokations of the same compiled kernel.

Constant memory allocation and initialization中的推荐是因为

Texture sizes can be set dynamically and they are cached.

如果您使用的是 Kepler,我的建议是使用 const __restrict 修饰数组指针。这样,您将能够动态分配数组,编译器会安排一些事情,以便在运行时通过纹理缓存自动读取数据。

关于在 CUDA 上使用 __constant__ 内存的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17614588/

相关文章:

C++ CUDA 推力 vector 多态性

c++ - CUDA 4.0 SDK 丢失文件?

c - 文件范围内可变修改的数组

java - 常量的命名;先输入还是先命名?

C:从 void* 读取 int**

c - C 中的 forks - 练习

c - 如何解压 msgpack 文件?

c++ - 在 Cuda 中使用二维数组数据成员

c - 我应该声明作为函数参数传递的数组的预期大小吗?

c++ - 返回对象时从 'const DList<int>* const' 到 'DList<int>*' [-fpermissive] 的无效转换