Dynamic shared memory lets a CUDA kernel use a shared-memory capacity chosen at launch time. Declare it with `extern __shared__`; the launch’s third argument supplies the allocation size in bytes. section 11.2
extern __shared__ int sdata[]; #A
sumKernel<<<numBlocks, threadsPerBlock, #B
threadsPerBlock * sizeof(int)>>>(d_data, d_result, n);See the declaration and launch configuration together: the byte count is passed as the third launch argument. section 5.11
Size a reduction buffer
For a block with `threadsPerBlock` integer values, request `threadsPerBlock * sizeof(int)` bytes when launching the kernel. section 5.11
Keep these details in mind:
- The launch size is in bytes, not elements. section 11.2
- Multiple `extern __shared__` declarations alias the same allocation. Use one raw buffer and divide it into regions if you need multiple arrays. section 11.2
- If a static array’s size no longer matches the block size, threads can write out of bounds; dynamic sizing avoids that coupling. section 5.25
