
Recall first from Constant memory and register spills
- A constant-memory load can broadcast one address to all requesting lanes in a warp.
- CUDA can cache reads from an ordinary global buffer through the read-only load path.
- CUDA local memory is private to a thread and has device-memory backing.
How far apart are the memory tiers?
3 min readYou have visited every place data can live. This lesson answers the latency of each tier and how to measure the effect with Nsight Compute.
The vector-addition kernel reads two input elements, adds them, and writes one output element. Its addition can use register operands as soon as the loads finish. Putting both inputs into shared memory first would still require reading the arrays from global memory and would add shared-memory operations. If each input element is consumed once, what would pay for those extra operations? The number of times a value is reused, and which threads reuse it, determines whether changing its placement is worth testing.
A Load latency is the time between issuing a load and having its result available for a dependent instruction. The estimates below are in GPU cycles. Use them to compare orders of magnitude. Actual load latency depends on the GPU, the access pattern, and what else is using the memory system at the time.
| Where the operand comes from | Approximate cost | What the estimate describes |
|---|---|---|
| Register | ~0 extra cycles | The instruction already has a register operand. Arithmetic and dependencies between instructions still take time. |
| Shared memory | ~20 to 30 cycles | An uncontended on-chip load. This excludes any time spent waiting for other threads to finish producing the data. |
| Global memory reaching device DRAM | ~400 to 800 cycles | An off-chip access after cache misses. A global load served by cache can finish sooner. |
A load can take hundreds of cycles even when a kernel finishes many loads per unit of time. If a warp is waiting for a value, the scheduler can issue work from another eligible warp. Independent instructions also give the GPU work it can execute while a load is outstanding. Now consider an array of indices where each loaded index selects the next element to load. The next address is unavailable until the current load returns. The chain therefore exposes the delay even when it generates little traffic. This is why a small number of bytes per second can coexist with expensive memory waits.
Choosing where a value is reused
2 min readStart with a value that one thread uses repeatedly. In a pixel calculation, a thread might load an input once and use the value in several arithmetic expressions. A scalar local variable gives the compiler an opportunity to hold the value in a register. The generated code determines whether that happens. Keeping more values live at once increases register demand, so a placement change can introduce spill traffic even when the source appears to use fewer array reads.
Shared memory is a candidate when several threads in one block need the same values. For example, nearby outputs may all need some of the same input pixels. A block can load the relevant region from global memory, store it in shared memory, then let its threads read from there when the data is ready. The shared copy uses storage for the lifetime of the block and costs instructions to fill. When threads consume values written by other threads, the implementation must also account for synchronization. Compare those costs with the global requests removed and with the possibility that the original requests already hit in cache.
Use global memory for the input and output arrays that persist across kernel launches. For vector addition, each thread can read its input elements directly and write its result to the output array. The array's placement and the operands' placement describe different moments in the same kernel. An input element starts in global memory and can become a register operand after a load. The sum can stay in a register until the store writes it to the output array.
For a block-local reuse change, make a prediction before measuring. Which repeated requests should disappear? Where should the replacement accesses appear? If the initial global requests mostly miss cache, removing them may lower DRAM traffic. If the cache already serves them, DRAM bytes may change little. In either case, kernel duration tells you whether the new placement paid for its extra work. Check that both versions produce the same result so that lower traffic cannot come from skipping required work.
Measuring the effect with Nsight Compute
3 min readNsight Compute reports how a CUDA kernel used the GPU during profiling. SpeedOfLight gives the kernel duration and high-level memory throughput relative to peak. MemoryWorkloadAnalysis gives the detail needed to interpret that summary, such as DRAM read and write bytes and activity at L1/TEX and L2 where the report provides it. A high rate at a cache and a high rate at DRAM describe different hardware activity. Read the label of the specific unit before attributing a high summary percentage to external memory.
On an Ubuntu 24.04 system with a supported NVIDIA GPU and performance counter access, run the following from the directory containing the course vector-addition program. This command assumes the executable is named vector_add. If your executable uses a different name, substitute its path.
ncu --section SpeedOfLight --section MemoryWorkloadAnalysis ./vector_add
If the program launches multiple kernels, identify the vector-add kernel by its name in the report. Record the duration, the overall memory throughput percentage, and the reported DRAM read and write bytes for that launch. If you compare a modified build, use the same input, launch dimensions, device, and profiler settings. Compare corresponding launches, and repeat the runs if variation is large enough to change the conclusion. Use the timing and counters from the same profiling setup because collecting sections can require replaying a kernel.
A high DRAM throughput percentage indicates that DRAM is operating near the profiler's reference peak. If a new version moves fewer DRAM bytes and finishes sooner, its DRAM throughput may still fall. Throughput depends on bytes transferred and elapsed time. If bytes fall by a greater proportion than time, the transfer rate falls too. In that case, the shorter duration shows the improvement. When a report shows low throughput, inspect the kernel's accesses before drawing a conclusion. A dependent chain, a short launch, or a workload with little demand for DRAM can all produce a low rate.
These two sections measure the effect of placement on work and time. To estimate load latency itself, use a device microbenchmark with a chain of dependent loads. Time a long chain with the per-SM cycle counter read by clock64(), make the last loaded value observable so the compiler retains the chain, and divide the elapsed cycles by the number of loads. The result includes loop and address computation overhead and can include scheduling delays. For global memory, the working set and access pattern affect which loads reach DRAM. Host timings from time(1) or clock_gettime(2) describe elapsed time around the command or host code being timed. Even when host timing waits for GPU completion, it covers more than a single device load.
The next lesson, Tiling matrix multiply to cut global traffic, applies this placement reasoning to a kernel where each loaded value is reused many times, so the shared-memory copy can repay its cost.
Key takeaways
- Register operands require approximately zero extra memory-load cycles, uncontended shared loads are roughly 20 to 30 cycles, and global loads that reach device DRAM are roughly 400 to 800 cycles.
- Independent work can hide a load's latency, while each load in a dependent chain must wait for its address.
- MemoryWorkloadAnalysis describes where traffic goes, while SpeedOfLight reports kernel duration and throughput relative to peak.
- A placement change can reduce DRAM bytes and kernel duration even when measured DRAM throughput falls.
Reflect
- Explain to a colleague, in two sentences: rank register, shared, and uncached global access by approximate latency.
- Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
- What surprised you when you ran the commands yourself?
On a real board
Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.
References and further reading
Official documentation first. Links checked when the lesson was published.