【问题标题】:Correct way to use __constant__ memory on CUDA?在 CUDA 上使用 __constant__ 内存的正确方法?
【发布时间】:2013-07-10 23:26:47
【问题描述】:

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

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

这可能吗?可能更重要的是,这是个好主意吗?如果有更好的选择来做这样的事情,我很想听听。

【问题讨论】:

标签: c cuda constants


【解决方案1】:

正如Dynamic Allocation of Constant memory in CUDAConstant memory allocation and initialization 中讨论的那样,您不能。正如Constant memory allocation and initialization 中所述,

常量在编译时嵌入到可执行文件中(这就是为什么您必须将字节复制到符号指定的地址以在运行时设置常量值)。因此,您将无法为同一个编译内核的不同调用分配不同大小的常量数组。

Constant memory allocation and initialization 中的建议是使用纹理内存,因为

纹理大小可以动态设置并被缓存。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 2021-08-31
    相关资源
    最近更新 更多