Overlap copies with computation
Order copies, kernels and buffer reuse with explicit dependencies.
Before you run
Four chunks pass through two reusable GPU storage slots. CUDA streams queue the copies and kernels, and events order their dependencies. The starter checks the resulting values; the optional model explores overlap using explicitly synthetic durations.
Definitions
- Stream
- An ordered queue of CUDA operations.
- Event
- A marker that another stream can wait for before issuing dependent work.
Read the starter excerpts
Read-only CUDA C++ · main() · host excerpt
check(cudaEventRecord(ready[s], copy));
check(cudaStreamWaitEvent(compute, ready[s], 0));
process<<<1, width, 0, compute>>>(slot[s]);In main, ready[s] is recorded after the input copy in copy’s stream. cudaStreamWaitEvent orders compute after that event, and process doubles the slot values. Later, used[s] is recorded after the output copy; the next reuse waits for it. Streams are ordered queues, and events connect their dependencies.
The starter uses cudaMallocHost to allocate pinned CPU memory: memory the operating system keeps in RAM so CUDA can transfer it asynchronously. Pinned memory supports transfer and computation overlap when the GPU and workload allow it; streams alone do not guarantee overlap.
In process<<<1, width, 0, compute>>>, the third argument requests zero extra bytes of dynamic shared memory, and the fourth selects the compute stream. Dynamic shared memory is block-local storage whose size is supplied at launch; this kernel does not use it.
CUDA C++ · launch inside main()
process<<<1, width, 0, compute>>>(slot[s]);The settings inside <<< >>> give the block count and threads per block. Parentheses pass the arguments to the kernel. These read-only excerpts show the starter. The CUDA source editor contains the storage and launch settings.
Edit and run CUDA
The CUDA source editor opens with the complete starter or your saved draft. To run the starter again, use Editor tools → Restore starter. Choose Undo restore there to recover the previous draft.
Click Compile and run with the starter. Find First output, last output and chunk synchronizations in Console. The starter uses stream and event dependencies for four chunks.
The browser follows the stream and event dependencies. The optional schedule model illustrates overlap between input copies and kernels. It omits result copies; the CUDA program also waits for each result copy before reusing its slot.
Change the source
In the CUDA source editor, above process, change overlap from true to false. Run again and compare First output, last output and chunk synchronizations in Console. Does the added synchronisation change the values? These counts do not measure physical overlap or elapsed time.
Read the starter’s execution diagram
- Copy and record readyFinish supplying the slot’s input.
- Wait and computeOrder the kernel after its input event.
- Record consumedProtect the slot until output copying finishes.
Conceptual dependencies in this example; placement and timing are not specified.
Hardware context
Copy engines can transfer data while SMs execute independent kernels, if the device and workload support overlap. Streams express ordering, not dedicated hardware. A reusable buffer must wait for its previous consumer, even when its next copy is ready.
Read the hardware key
- Resident warp
- A warp has 32 lane positions; a small block can leave some unused. A resident warp has its state and required resources allocated on an SM. Its next instruction may still be waiting for data.
- Instruction
- One operation run by GPU hardware, such as a load, multiply or store.
- Warp scheduler
- Selects ready warp instructions for available execution hardware.
- Registers
- Hold working values associated with individual threads.
- Execution units
- Perform arithmetic and other instructions for threads; they are shared hardware.
- Load/store units
- Execute memory instructions, forming requests from active lanes’ addresses.
- L1 / L2 cache
- Keep recently used data on the GPU chip. L1 is local to an SM; L2 is shared across SMs in this simplified view.
- Shared memory
- SM-local storage allocated to blocks for explicit data exchange. It may share physical storage with L1, depending on the GPU.
- Bank
- A subdivision of shared memory that serves a group of word addresses.
- Tensor cores
- Matrix execution units used by supported warp-wide matrix instructions.
CPU side · CUDA host
Host ↔ device · copies move data; launches request execution
GPU device
GPU compute chip
SM · streaming multiprocessor
Logical groups using this SM’s resources
Issue to the appropriate units
Registers ↔ execution units · operands and results
Have their own execution hardware and registers.
Blocks may be assigned to different SMs.
Global-memory requests ↔ on-chip interconnect
Chip ↔ device memory · data transfers
Simplified NVIDIA-style architecture. Boundaries show containment; arrows name relationships. Unit counts, block placement and timing vary by GPU. A thread or warp is not a dedicated physical processor.
Explore the model: buffer dependencies and overlap
Predict and run
Copy four chunks to the GPU and compute on each. Two storage slots alternate between chunks. An input copy takes two units and computation takes three units in this model. Result copies are omitted; the slot becomes reusable after computation here. The CUDA program also copies results back before releasing a slot. A copy engine and a compute engine each handle one operation at a time.
Try it
Select Serial copy then compute and click Run model. Select Overlap with two reusable slots, predict the time unit when chunk 2 starts copying, then rerun. Follow slot 0 from chunk 0 to chunk 2. Time units belong to this illustrative schedule, not a GPU measurement.
These controls change only this model and its displayed excerpt; they do not edit the CUDA program above. Enter a prediction to compare with the model result.
CUDA C++ excerpt
// CUDA host loop. Each chunk has distinct host input.
// Both operations use one stream.
cudaMemcpyAsync(slot, host[i], bytes,
cudaMemcpyHostToDevice, stream);
process<<<grid, block, 0, stream>>>(slot);The compute stream waits for the chunk’s copy. The copy stream waits for the prior consumer before overwriting that slot. The host creates the streams and events before the loop and waits for final completion before reuse or free.
Apply what you saw
Why can the copy engine be idle while chunk 2 is available on the host?
Compare your explanation
Chunk 0 is still using slot 0. Copying chunk 2 there would overwrite its input before computation finished.
Follow readiness and buffer reuse between engines 1 min 45 sec · Video walkthrough, captions and transcript
Use fullscreen to inspect the code and diagram labels.
Read the transcript
Two buffers can connect copy and compute work
The starter has four input chunks and two reusable GPU buffer slots. Copies supply values; SMs execute the doubling kernel. Separate resources can overlap eligible work, but streams express ordering rather than reserved hardware engines.
A readiness event connects two queues
The copy stream records ready after supplying a slot’s input. The compute stream waits for that event before using the slot. An event marks completion of earlier work in its stream; the wait adds a dependency from the other stream.
The result copy is also a consumer
After the kernel, the compute stream copies the result back to the CPU. Only then does it record used for that slot. Finishing the input copy alone does not make a buffer safe for a later chunk to overwrite.
The copy engine is free; buffer A is not
Chunk two wants to reuse slot A from chunk zero. The copy engine is available, but the previous computation or result copy still uses A. Pause and predict whether the next input can overwrite that slot immediately.
Wait for the slot’s last consumer
It must wait for used from the previous chunk in that slot. The next input copy can proceed after the kernel and result copy have finished using A. Slot B permits a different chunk to make progress while A is held; it does not remove A’s dependency.
Dependencies establish safety, not a speed-up
Correct overlap needs readiness before consumption and release before reuse. The supplied events express those edges. Actual overlap depends on the device and workload. A diagram of eligible work is not evidence of elapsed-time improvement.
References streams · CUDA Runtime API · events
The browser exercises need no installed CUDA documentation version. For local execution, use the documentation matching your toolkit. External references need internet access.
- CUDA · streams (new tab)
Operations in one stream are ordered. Separate streams need an explicit dependency when one consumes the other’s output.
A producer and consumer use separate streams. Does their order in CPU source alone establish the GPU dependency?
Compare your answer
No. Use a supported dependency such as a recorded event and a stream wait. Operations in the same stream are ordered; independent streams need the required cross-stream ordering.
- CUDA Runtime API · events (new tab)
Record an event in the producer stream and wait in the consumer stream before dependent work or buffer reuse.
Which completion must protect a slot before it is reused: its input copy or its last consumer?
Compare your answer
Its last consumer, including any output copy still reading the slot. Finishing the input copy only makes the slot ready for computation; it does not make the slot reusable.
Further GPU investigation Run this CUDA example on an NVIDIA GPU
In the CUDA source editor, open Editor tools and choose Export .cu to save the complete program. Use an NVIDIA GPU and a matching CUDA toolkit to run it outside the browser.
Browser simulation checks program behaviour; physical GPU timing requires a hardware measurement.
Enable JavaScript to save completion.