Dynamic allocation of shared GPU 2D memory

I am aware of dynamic allocation when using 1D arrays, but how can this be done using 2D arrays?

myKernel<<<blocks, threads,sizeofSharedMemoryinBytes>>>(); .... __global__ void myKernerl(){ __shared__ float sData[][]; ..... } 

Suppose I want to allocate a two-dimensional array of shared memory:

 __shared__ float sData[32][32]; 

How can this be done dynamically? will be:

 myKernel<<< blocks, threads, sizeof(float)*32*32 >>>(); 
+6
source share
1 answer

As you wrote correctly, you need to specify the size of the dynamically allocated shared memory before each kernel call in the execution configuration (in <<<blocks, threads, sizeofSharedMemoryinBytes>>> ). This indicates the number of bytes in shared memory that is dynamically allocated to a block for this call in addition to statically allocated memory. IMHO there is no access to memory such as a 2D array, you need to use a 1D array and use it as a 2D. Think, don't forget the extern qualifier. Therefore, your code should look like this:

  sizeofSharedMemoryinBytes = dimX * dimY * sizeof(float); myKernel<<<blocks, threads,sizeofSharedMemoryinBytes>>>(); .... __global__ void myKernerl() { extern __shared__ float sData[]; ..... sData[dimX * y + x] = ... } 
+4
source

All Articles