Initialized shared memory in CUDA


When I tried to initialize a shared memory like:
device shared const float MyArray = {1,2,3,4,5};

I got a syntax error.
Does it mean each time the kernel is launched I have to initialize the shared memory and then run ?
What is the right way doing it ?
I must initialize 336 float numbers.

OpenCL (which generally has less performance) allows it.

Best regards,

shared const float MyArray”

i am not sure whether shared memory can be constant as well; i find that i need to think about it

“I must initialize 336 float numbers”

to what (common initial values, constants), and for how long (visibility of block length or kernel length)?

i think this would determine whether it should be shared memory, or rather constant/ global memory

shared memory cannot be statically initialized. It must be initialized manually using kernel code. Therefore const doesn’t make sense with shared.

device is not necessary when using the shared qualifier.

A shared definition like this:

shared float MyArray[5];

is valid and is considered a “static” definition.

You can also create a shared definition like this:

extern shared float MyArray;

which is considered a dynamically allocated shared variable. This method requires the size (in bytes) of the allocation to be specified at kernel launch time using a kernel launch config parameter.

Niether approach allows for static initialization.

As little_jimmy points out, the constant variable definition may be more suited to what you are trying to accomplish (or perhaps just a global const variable definition - this can be used in device code as well.)

You can read more about these various options in the programming guide and there are many cuda sample codes that demonstrate usage.

Personally I like to put small constant arrays of numbers whose size is known at compile time in a constant array, then load the array into a shared array in the kernel. If I won’t dynamically index the array, I consider hard coding the array into my code.