liveBook · Manning

Static shared memory

As explained in Grokking GPU Programming

Static shared memory is shared memory whose capacity is fixed in a CUDA kernel declaration.

Static shared memory reserves a shared array whose size is fixed at compile time, such as `__shared__ int sdata[256]`. Threads in a block can use it to stage data for cooperative work. section 5.11 section 11.14

Staging values for a reduction

Each thread copies its input into its own `sdata` slot, then the block synchronizes before reducing. With six inputs and four threads per block, the second block stages `[data[4], data[5], 0, 0]`; zero safely pads the unused slots for addition. section 5.11

Keep the array size aligned with the launch:

  • If a 256-element array is used with 512 threads, some threads write out of bounds; with fewer threads, slots go unused. Use one shared constant for the declaration and launch, or use dynamic shared memory sized at launch. section 5.25 section 11.2
Figure 5.4 Act 1 (Load): staging data in shared memory for fast access. Each thread reads one value from slow global memory into its private register, then writes it to shared memory, a two-step relay that happens in parallel across all eight threads simultaneously. Once sdata is populated, the reduction can run entirely on-chip without touching global memory again.

Shows threads loading global-memory values into shared memory before the reduction. section 5.11

How Grokking GPU Programming covers it

How deep it goes
Explains compile-time sizing, contrasts launch-sized allocation, and details a block-size mismatch and its fix.
Examples
Stages array values in `sdata[256]`, pads out-of-range reduction inputs with zero, and contrasts 256- and 512-thread launches.
What it assumes
Assumes basic CUDA indexing and launch configuration rather than teaching those topics from zero.
What you can do after
Configure a fixed shared-memory array and align its capacity with the launched block size.
How it connects
Connects CUDA kernel declarations to block dimensions, dynamic 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.