Using maximum shared memory in Cuda

from here:

Compute capability 7.x devices allow a single thread block to address the full capacity of shared memory: 96 KB on Volta, 64 KB on Turing. Kernels relying on shared memory allocations over 48 KB per block are architecture-specific, as such they must use dynamic shared memory (rather than statically sized arrays) and require an explicit opt-in using cudaFuncSetAttribute() as follows:

cudaFuncSetAttribute(my_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, 98304);

When I add that line to the code you have shown, the invalid value error goes away. For a Turing device, you would want to change that number from 98304 to 65536. And of course 65536 would be sufficient for your example as well, although not sufficient to use the maximum available on volta, as stated in the question title.

In a similar fashion kernels on Ampere devices should be able to use up to 160KB of shared memory (cc 8.0) or 100KB (cc 8.6), dynamically allocated, using the above opt-in mechanism, with the number 98304 changed to 163840 (for cc 8.0, for example) or 102400 (for cc 8.6).

Note that the above covers the Volta (7.0) Turing (7.5) and Ampere (8.x) cases. GPUs with compute capability prior to 7.x have no ability to address more than 48KB per threadblock. In some cases, these GPUs may have more shared memory per multiprocessor, but this is provided to allow for greater occupancy in certain threadblock configurations. The programmer has no ability to use more than 48KB per threadblock.

Although it doesn’t pertain to the code presented here (which is already using a dynamic shared memory allocation), note from the excerpted documentation quote that using more than 48KB of shared memory on devices that support it requires 2 things:

  1. The opt-in mechanism already described above
  2. A dynamic rather than static shared memory allocation in the kernel code.

example of dynamic:

extern __shared__ int shared_mem[];

example of static:

__shared__ int shared_mem[1024];

Dynamically allocated shared memory also requires a size to be passed in the kernel launch configuration parameters (an example is given in the question).

The assumption here is also that you intend to allocate/use all the shared memory via dynamic allocation. The proper limit, however, is that the sum of your dynamic request and static request cannot exceed the device maximum. So if you allocate 4kbytes statically, then the total dynamic allocation request cannot exceed (device limit)-4kbytes.

Leave a Comment