Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kernel function parameter as const

Tags:

c++

c

cuda

say I have a kernel

foo(int a, int b)
{
    __shared__ int array[a];
}

it seems a has to be a constant value, I added const in front of int. It sill didn't work out, any idea?

foo(const int a, const int b)
{
    __shared__ int array[a];
}
like image 453
small_potato Avatar asked Jun 08 '26 22:06

small_potato


1 Answers

While you can't have a dynamically-sized array because of the constraints of the C language (as mentioned in other answers), what you can do in CUDA is something like this:

extern __shared__ float fshared[];

__global__ void testShmem( float * result, unsigned int shmemSize ) {
    // use fshared - shmemSize tells you how many bytes
    // Note that the following is not a sensible use of shared memory!
    for( int i = 0; i < shmemSize/sizeof(float); ++i ) {
       fshared[i] = 0;
    }
}

providing you tell CUDA how much shared memory you want during kernel invocation, like so:

testShmem<<<grid, block, 1024>>>( pdata, 1024 );
like image 67
Edric Avatar answered Jun 10 '26 11:06

Edric