What this video shows
About this walkthrough
A fast kernel can still sit in a slow pipeline. This lab splits a serial copy-compute-copy pipeline into four independent chunks using CUDA streams, pinned host memory and asynchronous copies, with the compute kernel unchanged. One 64 MiB transfer becomes four 16 MiB transfers in each direction, and the pipeline runs 1.52x faster than the runner's serial reference in the same run. The complete unprofiled test run goes from 761 ms to 634 ms.
Read along
Transcript
Select a time to play the video from that chapter.
0:00Learn CUDA Streams in Rust
A fast GPU kernel can still be part of a slow pipeline. In this Rust lab, we will change how data transfers and computation are scheduled, then use GPUFlight to check what really overlaps.
0:15What the problem asks
The task has three steps: copy an array from the CPU to the GPU, process every element, and copy the results back. The compute kernel is provided. We are editing the Rust code that launches the work. The largest test moves 64 MiB in each direction. That is about 16.8 million floating-point values. Smaller tests check that the pipeline also handles different array sizes correctly.
0:47Streams, pinned memory, and ordering
Think of a CUDA stream as an ordered queue. Within one stream, a copy finishes before the kernel that uses its data. Independent streams can overlap when the hardware has resources available. They do not guarantee overlap. The harness provides pinned host memory. These pages stay fixed in physical memory so the device can transfer data asynchronously. We must also use asynchronous copies and give each chunk its own data range.
1:19Read the serial starter
The starter uses the default stream for the whole array. First it copies all the input. Then it launches one kernel. Finally it copies all the output back. That is correct, but there is no independent chunk ready to use another engine while one phase is running. Before changing it, run the starter and open its profiling session.
1:44Inspect the baseline timeline
Open the baseline session, select Timeline, and zoom into a full-array serial sequence. The blue bar copies 64 MiB from CPU memory to GPU memory. The orange bar is the return trip. The input and output transfers happen one after the other. This input copy takes about 5.4 ms; the return copy takes about 5.1 ms. The full capture also contains warm-ups, a reference, and correctness checks. We are inspecting one sequence, not calling the entire session a single pipeline.
2:24Edit 1: create streams and independent chunks
Create four streams before submitting the chunks. Round the chunk size up so we cover the whole array. Each stream receives an offset and a count. The last chunk may be shorter. The minimum keeps its range inside the buffer. Different chunks must not write the same elements.
2:45Edit 2: enqueue copies and computation
For each chunk, enqueue the copy to the GPU, launch the same compute kernel, and enqueue the copy back. All three operations use that chunk's stream. That preserves their dependencies without waiting on the CPU after every step. The launch uses the chunk's count, and the kernel receives its offset. We are changing the scheduling, not the calculation. These ranged copy helpers come from the lab harness. They enqueue part of a buffer rather than copying the whole allocation.
3:20Edit 3: wait after submitting all chunks
Wait for the streams after all the chunks have been submitted. Waiting inside the submission loop would stop us from getting the other chunks ready in time. The unsafe block needs a concrete argument: the ranges are valid, chunks do not overlap, and the buffers remain alive until the work completes. The harness also waits before stopping its timer and checking the results. Timing only the enqueue calls would give a misleading result.
3:51Verify correctness before interpreting speed
The four-stream version passes the output checks and reaches 1.52× the serial reference, above the 1.3× target. That score compares the pipeline with the serial reference inside the same run. Now open the profiling session to see what changed.
4:11After: the transfer bands overlap
In the new timeline, zoom into the four-chunk sequence. Each input and output transfer is now 16 MiB. Together, the four chunks still move 64 MiB in each direction. Look at the shared horizontal time axis. An orange output transfer begins while later blue input transfers are still running. This is visible overlap, not just an asynchronous function name. The chunk kernels also appear on separate stream lanes. The calculation is unchanged; the schedule gives independent work a chance to progress together.
4:50More operations, the same amount of data
Chunking increases the number of operations. One pipeline now has four kernel launches and eight transfers, instead of one launch and two transfers. Across the complete trace, including the test runner, launches increase from 13 to 31, and transfers from 26 to 62. But both captures report 322 MiB in each direction. The improvement comes from scheduling, not skipping data. More chunks are not always better. Smaller transfers and extra launches add overhead, so measure the tradeoff on your workload.
5:30Elapsed time: compare the same scope
Back in the lab, compare the baseline and candidate. The unprofiled runtime drops from 761.4 ms to 634.1 ms. That is 16.7% less elapsed time for the complete test runner. This broader measurement includes setup and checking. It is different from the 1.52× pipeline score. The kernel-time row is not the time for all copies and computation to finish. Use a completion-based timer for that question. These are two fresh runs on the same GPU model, not a repeated benchmark study.
6:13Find the idle time. Change the schedule. Verify.
CUDA streams are useful when you have independent work to overlap. Keep each chunk ordered, use the right memory and copy operations, and wait before reusing the results. Try the Rust Streams lab in GPUFlight Performance Lab. Change the code, inspect the timeline, and measure the full outcome. Thanks for watching!
Measure it yourself
Run the same experiment on a real GPU
Edit the kernel in your browser, run it on managed hardware, and read the profile the video walks through.
Run the CUDA streams lab