Capturing Work into a Graph
Recording a stream into a CUDA graph.
Capturing Work into a Graph is a free CUDA Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CUDA Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What a CUDA Graph Is
A CUDA graph records a whole sequence of kernels and copies as one reusable unit of work you can submit again and again.
Launches Add Up
Each individual kernel launch has a tiny CPU overhead. Across thousands of launches per iteration, that cost becomes real.
Define Once, Run Many
A graph lets you describe the work once, then replay it cheaply, paying the setup cost a single time.
Stream Capture Mode
The easiest way to build one is stream capture: you record the normal launches you already issue into a stream.
cudaStreamBeginCapture(stream, cudaStreamCaptureModeGlobal);Issue Work Normally
Between begin and end, you launch kernels and copies as usual. Nothing runs yet; the operations are recorded, not executed.
kernelA<<<g, b, 0, stream>>>(d);
kernelB<<<g, b, 0, stream>>>(d);End the Capture
You stop recording with cudaStreamEndCapture, which hands back a cudaGraph_t describing all the work.
cudaGraph_t graph;
cudaStreamEndCapture(stream, &graph);Dependencies Inferred
Capture figures out the dependencies between operations automatically from how you used the stream.
Instantiate Before Running
A graph is just a description. You compile it into a runnable graph exec object before you can launch it.
cudaGraphExec_t exec;
cudaGraphInstantiate(&exec, graph, 0);Building Graphs by Hand
You can also add nodes explicitly with the graph API for full control over structure and edges.
cudaGraphAddKernelNode(&n, graph, deps, 1, ¶ms);Multiple Streams, One Graph
Capture can span several streams, so independent branches that run concurrently all land in the same graph.
Reuse Across Many Iterations
Once instantiated, the same exec object is launched repeatedly, which is exactly where graphs earn their keep.
Quick Check
What does cudaStreamEndCapture produce?
Recap: Capturing Graphs
Wrap launches in stream capture to record a cudaGraph_t, instantiate it once, and capture infers the dependencies for you. Great progress! 🎉
Frequently asked questions
Is the “Capturing Work into a Graph” lesson free?
Yes — the full text of “Capturing Work into a Graph” is free to read here on the web, and the CUDA Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CUDA Academy course, upgrade to CoddyKit PRO.
What will I learn in “Capturing Work into a Graph”?
Recording a stream into a CUDA graph. You practise CUDA Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start CUDA Academy?
No prior experience is required. CUDA Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Capturing Work into a Graph” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this CUDA Academy lesson?
Yes. Every CUDA Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Launching Kernels from a Kernel
- When Dynamic Parallelism Pays
- Capturing Work into a Graph
- Replaying Graphs to Cut Overhead