What Is the GPU L2 Cache? Reuse Data and Control CUDA Persistence
The GPU L2 cache temporarily keeps data requested through global-memory addresses so multiple processing units can reuse it without every request going to external device memory. Reuse can reduce memory traffic and waiting, but a cache hit or continued residency is never guaranteed. This article explains NVIDIA CUDA's L2 model and persistence controls; behavior and limits must be checked for the GPU generation and execution environment in use.
What Is the GPU L2 Cache? Reuse Data and Control CUDA Persistence
The GPU L2 cache temporarily keeps data requested through global-memory addresses so multiple processing units can reuse it without every request going to external device memory. Reuse can reduce memory traffic and waiting, but a cache hit or continued residency is never guaranteed. This article explains NVIDIA CUDA's L2 model and persistence controls; behavior and limits must be checked for the GPU generation and execution environment in use.
What does L2 do, and how is it different from other memory?
In CUDA, global memory is an address space through which kernel code accesses device data, while L2 is a hardware cache that can serve those requests. They are not alternative places where a programmer chooses to store an allocation. If a request to read a global-memory address hits in L2, the cache can return the data; on a miss, the device memory supplies it. The diagram simplifies this relationship. Whether L1 is involved, and the order and handling of cache requests, can vary with instructions, policies and architecture.
GPU thread requests global-memory data
│
▼
Look up in L2
┌───┴───┐
Hit Miss
│ │
Cache data Read device memory
└────┬─────┘
▼
Return data to threadIn CUDA's programming model, each SM has a register file and data-cache resources shared between L1 and shared memory; L2 is a broader cache level used by the GPU's SMs. This is a programming model, not a circuit diagram or a fixed latency chart for every chip. The L1/shared-memory split and cache capacities can vary by architecture. L2 is also not a large storage space like VRAM, and it does not guarantee a data lifetime like an array explicitly managed by a kernel. CUDA Programming Model
| Term | How the programmer uses it | Main difference |
|---|---|---|
| Global memory | Read and write addresses in device memory | Holds large data sets shared by threads and kernels; requests may be served through caches |
| L2 cache | Usually managed by hardware; supported CUDA versions let programs provide access-policy hints | A cache level used by multiple SMs; hits and residency are not guaranteed |
| L1 cache | Managed by hardware close to an SM | Details such as sharing resources with shared memory vary by GPU generation and configuration |
| Shared memory | The kernel explicitly places values for cooperating threads | Threads can design data exchange and reuse within a block, and are responsible for required synchronization |
When can L2 reuse help an access pattern?
L2's value depends less on how large the data is than on when and how often the same cache line is requested again. Temporal locality means using recently accessed data again; spatial locality means accessing nearby addresses together. Both create opportunities for reuse. By contrast, data read once and discarded is unlikely to hit again. If the working set—the group of data reused at a given time—is larger than the available cache, or several kernels compete for the same cache, lines may keep evicting one another and erase the expected benefit.
| Access pattern | Expected L2 reuse | What to examine first |
|---|---|---|
| Several kernels repeatedly read the same weights or lookup table | Later accesses may hit | Reused-region size, time between kernels and other concurrent work |
| Neighboring output tiles read overlapping input regions | Nearby requests may share cache lines | Thread access layout and actual memory traffic; also compare explicit tiling if useful |
| A large streaming input is read once and not reused | Few repeated hits may occur | Whether the streaming policy is displacing cache space used by reusable data |
| The reused region exceeds the set-aside or several streams persist data concurrently | Cache lines may evict one another | Window size, hitRatio and the combined working set of concurrent kernels |
Example (hypothetical): Suppose several inference kernels read the same small block of weights in sequence, while each kernel scans a large input batch only once. The repeated weight block is a cache candidate; the input batch may be a streaming candidate. Specifying address ranges and access policies does not guarantee that the weights remain in L2 or that runtime decreases. Concurrent work, region size and request patterns can change the result. Fusing kernels or tiling to create reuse changes the data flow more substantially than adjusting a cache policy, so compare implementation complexity and correctness too.
How does CUDA's L2 persistence policy work?
CUDA runtime L2 persistence is not a general cache-pinning feature available on every GPU. NVIDIA's guide describes the control for compute capability 8.0 and later, with runtime API support starting in CUDA 11.0. Repeatedly accessed regions can be marked cudaAccessPropertyPersisting; one-time data can be marked cudaAccessPropertyStreaming; and cudaAccessPropertyNormal can represent ordinary access or clear a prior persistence preference. These properties are replacement-priority hints. Persisting makes data more likely to be retained and streaming makes it more likely to be evicted first; neither guarantees a hit or residency for a particular request. NVIDIA CUDA L2 Cache Control
| Control | Meaning | Commonly missed detail |
|---|---|---|
| L2 set-aside | Reserves part of L2 for preferential use by persisting accesses | The size cannot exceed persistingL2CacheMaxSize; query the target device and mode |
| Access policy window | Assigns an access property to a contiguous global-memory region for a stream or graph node | The window must fit within accessPolicyMaxWindowSize; it encourages persistence but does not ensure it |
hitRatio | A hint for the fraction of window accesses assigned the hit property | It is not the measured L2 hit rate. Which requests get the property depends approximately on probability and on the architecture |
| Reset policy | Removes persistence priority with cudaAccessPropertyNormal or cudaCtxResetPersistingL2Cache() |
Concurrent kernels share the L2 set-aside. Assigning a large window to every stream does not create separate cache stores: if the total persistent data exceeds capacity, lines can evict one another. You can lower hitRatio or shrink the window to give priority to only some candidate data, but the useful setting must be measured on the workload and GPU. Query l2CacheSize, persistingL2CacheMaxSize and accessPolicyMaxWindowSize with cudaGetDeviceProperties(). Set-aside is disabled in MIG mode. With MPS, the limit is set through an environment variable when the server starts, so cudaDeviceSetLimit() in a regular process cannot change it.
How do you verify the effect and find the bottleneck?
Nsight Compute's Memory Workload Analysis examines GPU memory resources and, depending on collection settings, reports traffic or cache metrics for individual memory units. Available report sections and metric names vary by GPU and tool version. Looking at the L2 hit rate alone can hide the cause of overall performance. Even with a better hit rate, memory-instruction issue rate, bandwidth, computation, data dependencies or kernel configuration may remain the bottleneck. Conversely, a low hit rate may matter little if computation already dominates runtime. Profiling adds measurement overhead, so separate timing comparisons from diagnosis. Nsight Compute Profiling Guide
| Observation | Possible interpretation | What to check next |
|---|---|---|
| Many data requests and high external-memory traffic, with repeated access to the same region | L2 reuse or a better data layout may help | Record baseline L2 metrics, DRAM traffic and kernel time together |
| Misses or evictions remain high after applying persistence | The region may exceed set-aside capacity or concurrent accesses may compete | Compare smaller windows and hitRatio; inspect concurrent streams |
| L2 hit rate improves but total time barely changes | Another bottleneck may dominate, or the absolute number of hits may be small | Check bandwidth, instruction issue, compute pipelines and kernel time |
Use this sequence to separate performance changes from correctness issues:
Record output correctness and baseline runtime with the same input, build, GPU and concurrent workload. Also keep a reproducible timing without the profiler.
Separate repeatedly accessed address ranges from one-pass streaming ranges. Estimate each size and the reuse interval between kernels.
Query cache and window limits with `cudaGetDeviceProperties()` and check compute capability, CUDA version, and whether execution uses MIG or MPS.
Apply a persisting hint only to the reused region. Change one condition at a time—streaming regions, concurrent streams or window size—and compare. When finished, reset the window to normal or explicitly reset persistence.
Recheck output under the same accuracy requirements, kernel time, L2-related metrics and external-memory traffic. Remove the policy if benefits do not repeat or other work becomes slower.
NVIDIA's Best Practices Guide shows performance changing with working-set size in a specific A100 microbenchmark. Those results describe that GPU, measurement and access pattern; they should not be treated as expected performance on another GPU or application. CUDA C++ Best Practices Guide, L2 Cache
Sources
[NVIDIA CUDA Programming Guide — L2 Cache Control](https://docs.nvidia.com/cuda/cuda-programming-guide/04-special-topics/l2-cache-control.html)
[NVIDIA CUDA Programming Guide — Programming Model](https://docs.nvidia.com/cuda/cuda-programming-guide/01-introduction/programming-model.html)
[NVIDIA Nsight Compute — Profiling Guide](https://docs.nvidia.com/nsight-compute/ProfilingGuide/index.html)
[NVIDIA CUDA C++ Best Practices Guide](https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/index.html)