Device-to-Host Transfers
Downloading results back to the CPU.
Device-to-Host Transfers is a free CUDA Academy lesson on CoddyKit — lesson 2 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.
Bringing Results Home
Your kernel just filled a device buffer with answers, but the CPU cannot read GPU memory directly. You must download the results back. 📥
The Download Direction
To pull data back you use cudaMemcpyDeviceToHost. Now the source is device memory and the destination is host RAM.
cudaMemcpy(h_c, d_c, bytes, cudaMemcpyDeviceToHost);Destination Still First
The rule never changes: destination argument first, source second. For a download, the host pointer leads.
cudaMemcpy(h_dst, d_src, bytes, cudaMemcpyDeviceToHost);Host Buffer Must Exist
The destination needs real CPU memory waiting for it. Allocate host storage with malloc or new before you copy back.
float* h_c = (float*)malloc(bytes);Wait for the Kernel
A blocking cudaMemcpy waits for prior work in the default stream, so the kernel finishes before any results are read.
myKernel<<<blocks, threads>>>(d_c, n);
cudaMemcpy(h_c, d_c, bytes, cudaMemcpyDeviceToHost);Same Byte Count
Download exactly as many bytes as you uploaded and computed. Mismatched sizes give you truncated or garbage results.
size_t bytes = n * sizeof(float);Now You Can Read It
Once the copy returns, the values live in normal CPU memory. You can print, check, or save them like any host array.
printf("%f\n", h_c[0]);The Round Trip
A full GPU job is a round trip: upload inputs, launch the kernel, then download outputs. Each leg uses cudaMemcpy.
Verify Before You Trust
After downloading, compare the GPU result against a quick CPU reference. Verification catches bugs before they spread. ✅
Free What You Allocated
When results are home, release both sides: cudaFree the device buffer and free the host buffer to avoid leaks.
cudaFree(d_c);
free(h_c);Downloads Cost Time Too
The return trip crosses the same slow bus, so only copy back the results you actually need on the host.
Quick Check
Your kernel wrote results into device buffer d_c. How do you read them on the CPU?
Recap
You learned the return trip with cudaMemcpyDeviceToHost: allocate host storage, host pointer first, wait for the kernel, then verify and free. 🎉
Frequently asked questions
Is the “Device-to-Host Transfers” lesson free?
Yes — the full text of “Device-to-Host Transfers” 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 “Device-to-Host Transfers”?
Downloading results back to the CPU. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Device-to-Host Transfers” 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
- Host-to-Device Transfers
- Device-to-Host Transfers
- The Copy Direction Enum
- The PCIe Transfer Bottleneck