Correct way to use __constant__ memory on CUDA?

邮差的信 提交于 2021-02-11 10:25:08

问题


I have an array I would like to initialize in __constant__ memory on the CUDA device. I don't know it's size or the values until runtime.

I know I can use __constant__ float Points[**N**][2] or something like that, but how do I make this dynamic? Maybe in the form of __constant__ float* Points?

Is this possible? And possibly more important, is this a good idea? If there are better alternatives to doing it like this I would love to hear them.


回答1:


As it has been discussed in Dynamic Allocation of Constant memory in CUDA and Constant memory allocation and initialization, you cannot. As noticed in 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.

The recommendation in Constant memory allocation and initialization was to use texture memory since

Texture sizes can be set dynamically and they are cached.

If you are using a Kepler, my recommendation is to decorate array pointers by const __restrict. In this way, you will be able to dynamically allocate arrays and the compiler will arrange things so that the data will be automatically read through the texture cache at runtime.



来源:https://stackoverflow.com/questions/17614588/correct-way-to-use-constant-memory-on-cuda

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!