GPU Memory Hierarchy and Memory Optimization
Contents
How data moves inside a GPU and how to optimize that movement. Optimize a CUDA matrix transpose kernel progressively, measure performance, and explain each improvement at the hardware level.
GPU Memory Hierarchy
Memory have different capacity, latency, bandwidth and accessibility.


| Memory | Relative speed | Scope | Managed by |
|---|---|---|---|
| Registers | Fastest | Thread | Compiler |
| Shared memory | Very fast | Block | Programmer |
| L1 cache | Very fast | SM | Hardware |
| L2 cache | Fast | GPU | Hardware |
| Global memory | Slower | GPU | Programmer / hardware |
| Local memory | Often slow | Thread | Compiler |

CUDA local memory sounds like fast memory close to the thread, but it is actually backed by device memory (VRAM). Local describes accessibility, not physical location.
Variables declartion typlically use registers, each SM has a limited number of registers. If a thread needs more registers than the compiler can allocate to it, some values may be moved into local memory. This is called register spilling.

For large or dynamically indexed local arrays:
__global__ void kernel(float* output, int index){
float buffer[256];
for (int i = 0; i < 256; i++)
buffer[i] = i * 2.0f;
output[threadIdx.x] = buffer[index];
}
The compiler may place buffer in local memory because it is large and dynamically indexed.
Memory Coalescing
Memory coalescing is the most important GPU memory optimization concept
Memory transactions
When a warp executes a memory instruction, its 32 threads may request 32 different memory address. The GPU combines these requests into memory transactions where possible.
// Coalesced access
float value = input[tid];
// Strided access
float value = input[tid * 8];
Both execute one load per thread, but their hardware behaviour is very different.
float(FP32) occupies 4 bytes (32 bits), on mordern NVIDIA GPUs, global memory coalescing can be analyzed using 32-byte sectors. If the starting address is suitably aligned, the warp's 128 bytes can be covered by four 32 bytes sectors.

Strided memory access
For strided version, each neighbouring thread accesses an address 32 bytes away from the previous thread, each thread now touch a separate 32-byte memory sector. The warp still request only 128 useful bytes, but those requests cover 1024 bytes of sectors. The Sector Utilization = 128/1024 = 12.5%
Poor memory coalescing on a GPU does not always translate directly into proportionally higher DRAM traffic, because cache reuse can reduce redundant memory accesses.
Memory alignment
Even consecutive accesses can become less ifficient when the starting address is misaligned.
// Aligned access
float value = A[tid];
// Offset by one FP32 element
float value = A[tid + 1];

Memory layout: AoS vs. SoA
Array of Structures:
struct Point {
float x;
float y;
float z;
};
Point points[N];
Structure of Arrays
float x[N];
float y[N];
float z[N];
SoA is often preferable for GPU workloads that process individual fields across many elements.
AoS can still be efficient when threads need all fields together, especially with appropriate alignment and vectorized loads.
Why might changing a point cloud representation from AoS to SoA improve GPU performance without changing the algirhtm? Becuase it changes the memory access pattern, potentially reducing the number of sectors required to retrieve useful data.
Cache and Memory Locality
Temporal and spatial locality
There are two fundamental forms of locality: Temporal locality and Spatial locality. GEMM benefits heavily from temporal locality because each input element participates in multiple multiply and operations.
Vector addition primarily benefits from spatial locality because each element is normally used only once per kernel launch.
Shared Memoy and Bank Conflicts
Shared memory is a fast, programmer-managed, on-chip memory region accessible to all threads within a block.
__shared__ float tile[32][32]
Shared memory is primarily used for data reuse, communication between threads, parallel reductions, matrix tiling and reorganizing memory access patterns. However, shared memory is not automatically efficient.
Shared memory banks
On modern NVIDIA GPUs, shared memory is organized into 32 banks. Each successive 32-bit word (4 bytes) maps to the next bank.

General formula is: BankID = \frac{ByteAddress}{4} mod 32
For an aligned FP32 array:
__shared__ float data[1024];
float x = data[threadIdx.x];
Thread 0 accesses bank 0, thread 1 accesses bank 1, and so on. All 32 thread can access different banks without bank conflicts.
Bank conflicts
Consider a different access pattern:
float x = data[threadIdx.x * 32]
Every thread accesses a different address mapped to bank 0. These accesses must be serviced in multiple conflict-free phases, reducing effective shared memory throughput.