问题
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