I have seen in many CUDA programs using shared memory “extern” is used even when the kernel and the main is in the same .cu file. Like:

extern shared float temp;// allocated on invocation

when “extern” is not written I get error. Why is it so ?
Also is it necessary to specify the amount of shared memory in kernel call in main?

Can anyone please clarify my doubts here?


When the size of a kernel’s shared memory is dynamically allocated upon launch, it’s necessary to declare the shared array as extern [1].

Statically allocated shared variables should not be declared with extern. For example:


global void foo(…)


// no need for extern here

shared int shared_array[10];


int main(void)


// no need to specify the size of shared memory here




[1] http://developer.download.nvidia.com/compu…g_Guide_2.3.pdf section B.2.3