liveBook · Manning

Dynamic shared memory

As explained in Grokking GPU Programming

Dynamic shared memory is shared memory whose capacity is specified when a CUDA kernel is launched.

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

How Grokking GPU Programming covers it

How deep it goes
Explains the declaration, byte-count launch argument, aliasing, and large-allocation opt-in.
Examples
Uses a reduction buffer sized from the block and shows one raw buffer divided into float and integer regions.
What you can do after
Configure launch-time shared-memory capacity with a byte count and prevent overlapping arrays by slicing one buffer.
How it connects
Connects dynamic allocation to CUDA kernel launches, shared memory, and parallel reduction.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking GPU Programming. The section links point to the parts of the book it draws on.