What Is GPU Shared Memory? Design Block Cooperation and Data Reuse
GPU shared memory is an on-chip workspace that threads in the same thread block can use together while a kernel runs. It can hold values loaded from global memory for reuse within the block, or intermediate data produced by one thread and consumed by another. It adds staging and synchronization work and has limited capacity, so it helps when threads have a concrete reason to share data.
What Is GPU Shared Memory? Design Block Cooperation and Data Reuse
GPU shared memory is an on-chip workspace that threads in the same thread block can use together while a kernel runs. It can hold values loaded from global memory for reuse within the block, or intermediate data produced by one thread and consumed by another. It adds staging and synchronization work and has limited capacity, so it helps when threads have a concrete reason to share data.
How is shared memory different from other GPU memory?
In CUDA, shared memory is programmer-managed workspace on an SM (Streaming Multiprocessor, a GPU unit that schedules thread blocks and warps). Ordinary __shared__ variables are accessible to threads in the block that uses them. Running on the same SM does not make one block’s shared-memory allocation visible to another block. Its data lasts for the kernel execution; the workspace ends with the kernel. That differs from the lifetime of a global-memory allocation, which can survive a kernel. This article covers ordinary block-scoped shared memory, not distributed shared memory for CUDA thread-block clusters. CUDA Programming Guide — Writing SIMT Kernels
| Memory | Access scope | Management and main role |
|---|---|---|
| Registers | One thread | Managed by the compiler; hold thread-local temporary values and accumulators. |
| Shared memory | Threads in one block | The program manages allocation, layout, and synchronization; used for block-local reuse and cooperation. |
| Global memory | Threads across GPU kernels | Holds large inputs, outputs, and results passed between kernels. A CUDA allocation can outlive a kernel. |
| L1/L2 caches | Determined by hardware | Cache memory accesses automatically; they are not arrays that a program directly shares among threads. |
NVIDIA’s CUDA guide describes shared memory as located on each SM and sharing physical resources with L1 cache. In that documented design, allocating more resources to shared memory can reduce the L1 space available to a kernel. Capacity and allocation behavior vary by GPU architecture, so check device properties and the resource use of the kernel. CUDA C++ Best Practices Guide — Shared Memory
How does a block reuse data through shared memory?
A common case is matrix multiplication or transpose: the same input values contribute to several results, or the program needs to rearrange the order of accesses. Threads in a block cooperatively load a tile—a small region of a matrix—from global memory into shared memory. Several threads can then reuse values from that tile. Mapping requests to neighboring global-memory addresses can also help combine transactions. NVIDIA’s matrix multiplication and transpose examples show how shared memory can reduce repeated transfers and reorder accesses. CUDA C++ Best Practices Guide — matrix tiles and shared memory
The diagram shows the conceptual flow when one block stages two matrix tiles for reuse. It does not specify a tile size or performance result.
Global-memory A tile ─┐ ┌─ Global-memory B tile
└──▼────┘
Block threads load cooperatively
│
▼
Shared-memory A tile / B tile
│ __syncthreads(): wait for block tile writes
▼
Threads reuse tile values
│ accumulate partial results
▼
Write C tile to global memoryA code fragment shows the same idea. A real kernel also needs safe handling for input edges and a control flow where every required thread reaches the barrier.
tile[threadIdx.y][threadIdx.x] = input[globalRow][globalCol];
__syncthreads();
// Threads in this block read and reuse values from tile.Shared memory is useful for more than avoiding duplicate reads. In a matrix transpose, threads can read neighboring global-memory addresses, store the tile in shared memory, then read it in a different direction so that writes to global memory are also neighboring. Before choosing shared memory, identify the specific inefficiency it is meant to remove; “it is on-chip” alone is not enough.
How should you handle synchronization and bank conflicts?
When one thread writes shared memory and another reads that value, block-level synchronization may be needed before the read. In CUDA C++, __syncthreads() makes the block wait until all its threads reach the barrier and orders their earlier shared-memory accesses so that threads in the block can observe them. The barrier does not synchronize other blocks. Putting it in a branch taken by only some threads can prevent the block from progressing correctly. For a boundary tile, a common pattern is for threads with no input element to write a neutral value and still reach the same barrier. CUDA Programming Guide — synchronization and shared memory
Shared memory is divided into parallel access units called banks. A warp request spread across different banks can be served in parallel. If multiple distinct addresses in a request map to the same bank, the accesses can be split and serialized. Reads by multiple threads from the same address are a separate broadcast case. Exact bank counts and address mapping depend on architecture and data type. CUDA C++ Best Practices Guide — memory banks
In a square-tile transpose example, reading one column of tile[32][32] can make warp threads access locations 32 elements apart and hit the same bank. NVIDIA’s example pads the second dimension to tile[32][33], changing the stride and removing that conflict. This is a fix for that data type, tile, and access pattern, not a value to add to every shared-memory array. Inspect the actual indices and bank pattern first.
How can you tell whether shared memory helps?
A large shared-memory allocation per block consumes more SM resources and can reduce the number of blocks resident at once. Occupancy is the fraction of the SM’s supported warps that are active. Higher occupancy is not always faster, but shared-memory use can constrain occupancy, so measure its effect on latency hiding and throughput. NVIDIA’s guide explains how to query per-block and per-SM limits and points to the Nsight Compute occupancy calculator. CUDA Programming Guide — device resource limits and block execution Nsight Compute Profiling Guide
Identify values to reuse or accesses to reorder. If one thread reads a value once and uses it once, staging it may add little value.
Calculate tile bytes per block. Check `cudaGetDeviceProperties` fields `sharedMemPerBlock` and `sharedMemPerMultiprocessor`, plus any device-specific dynamic-allocation limit that applies.
Map thread indices to coalesced global loads and stores; write down which threads produce and consume each tile element.
Add barriers where threads exchange values. Confirm every block thread reaches them and that a later tile does not overwrite storage before earlier reads finish.
In Nsight Compute, examine shared-memory accesses, bank conflicts, memory traffic, and occupancy. With the same inputs and correctness conditions, compare kernel time and end-to-end application time against a baseline.
Shared memory is useful when a block reuses data or reorders memory access. Tile copies, barriers, bank conflicts, and occupancy limits can add costs. Measure before and after to see whether the saved traffic and reuse outweigh those costs in the actual application path.
Sources
[NVIDIA CUDA Programming Guide — Writing SIMT Kernels](https://docs.nvidia.com/cuda/cuda-programming-guide/02-basics/writing-cuda-kernels.html)
[NVIDIA CUDA C++ Best Practices Guide](https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/index.html)
[NVIDIA Nsight Compute — Profiling Guide](https://docs.nvidia.com/nsight-compute/ProfilingGuide/index.html)