Home Knowledge Base GPU Shared Memory Optimization

GPU Shared Memory Optimization is the critical CUDA/GPU programming technique of using on-chip shared memory (32-228 KB per Streaming Multiprocessor) as a programmer-managed cache to reduce global memory accesses — where properly tiled algorithms using shared memory achieve 5-50x speedup over naive global memory implementations because shared memory provides ~20 cycle latency and ~100 TB/s aggregate bandwidth compared to global memory's ~400 cycle latency and ~2-8 TB/s bandwidth.

Shared Memory Architecture

Bank Conflicts

Shared memory is organized into 32 banks (NVIDIA). Consecutive 4-byte words map to consecutive banks. If multiple threads in a warp access different addresses in the same bank in the same cycle, the accesses serialize (bank conflict):

Common conflict patterns:

Tiling Pattern

The canonical optimization pattern for matrix operations: 1. Load tile: Threads cooperatively load a tile of input data from global memory into shared memory (coalesced global reads). 2. __syncthreads(): Barrier ensures all threads have completed loading. 3. Compute: Threads read from shared memory (fast, reusable) to compute their outputs. Each element loaded once from global memory but read multiple times from shared memory. 4. __syncthreads(): Barrier before the next tile load (prevent overwriting data still in use). 5. Repeat: Iterate over tiles until the full input is processed.

GEMM Example

Naive GEMM: each element of C reads an entire row of A and column of B from global memory — N³ global reads for an N×N matrix multiply. Tiled GEMM with shared memory: load a TILE_SIZE × TILE_SIZE block of A and B into shared memory, compute partial products, iterate over tiles. Global memory reads drop from N³ to N³/TILE_SIZE — a 16-32x reduction for typical tile sizes.

GPU Shared Memory is the key lever that transforms memory-bound GPU kernels into compute-bound ones — enabling the data reuse patterns that are essential to achieve a significant fraction of the GPU's peak computational throughput.

gpu shared memory optimizationshared memory bank conflicttiling gpu kernelshared memory usage cudalocal data share

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.