initializing shared varialbe initializing shared varialbe at declaration time

Hi all,

if I have a shared float var;

I cannot initialize a shared variable at declaration time like shared float var = 4; // not valid

one way what I think is to initialize it in the first line of my kernel, and then put __syncthreads();

is it a right approach or there is any other efficient way.

please let me know thanks

NUST Pakistan

This is the right way.

Look at it this way: if you ask the compiler to initialize int shared var = 4; - which thread should perform the initialization? One? All of them?

Even more difficult when you demand this from the compiler:

int shared myarray[4] = { 1,2,3,4 };

What should the compiler make of it? Your optimal code would need to look similar to this

// tid would be threadIdx.x

if (tid < 4)


myarray[tid] = tid+1;



That’s nothing a compiler could “guess” or “invent” on its own.


so should I do it like


if(tid == 0){
var = 4;

… more …

} // kernel ends


Yup… that’s the correct and safest way to perform shared mem initialization…