Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use a single thread to initialize shared memory?

This seems like it should be simple, but I can't find any references, so I'm asking here.

I have the following CUDA kernel, which I am launching in a grid of 2D thread blocks:

__global__ void kernel(){

    if (threadIdx.x == 0 && threadIdx.y == 0) {
        __shared__ int test = 100;
    }
    __syncthreads();

    // Do more stuff
}

When I try to compile, I get the error

initializer not allowed for shared variable

What am I doing wrong? It seems to me like I have just one thread doing the initialization...

Thanks!

like image 300
user3257898 Avatar asked Jan 18 '26 14:01

user3257898


1 Answers

Do this instead:

__global__ void kernel(){
    __shared__ int test;
    if (threadIdx.x == 0 && threadIdx.y == 0) {
        test = 100;
    }
    __syncthreads();

    // Do more stuff
}

The declaration of the __shared___ variable must be separate from code that manipulates it.

like image 66
Robert Crovella Avatar answered Jan 21 '26 02:01

Robert Crovella



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!