The Copy Direction Enum
cudaMemcpyKind and getting it right.
The Copy Direction Enum 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.
The Fourth Argument
Every cudaMemcpy ends with a direction flag of type cudaMemcpyKind. It tells the runtime which way the bytes travel. 🧭
cudaMemcpy(dst, src, bytes, kind);Host to Device
Use cudaMemcpyHostToDevice to upload inputs from CPU RAM into GPU memory before a kernel runs.
cudaMemcpy(d_a, h_a, bytes, cudaMemcpyHostToDevice);Device to Host
Use cudaMemcpyDeviceToHost to download results from GPU memory back into CPU RAM after computing.
cudaMemcpy(h_c, d_c, bytes, cudaMemcpyDeviceToHost);Device to Device
To copy between two GPU buffers without touching the CPU, reach for cudaMemcpyDeviceToDevice.
cudaMemcpy(d_b, d_a, bytes, cudaMemcpyDeviceToDevice);Host to Host
There is even cudaMemcpyHostToHost, a plain CPU copy. You rarely need it since standard memcpy already does the job.
Let the Driver Decide
With cudaMemcpyDefault, the runtime infers direction from the pointers. It needs unified addressing to work safely.
cudaMemcpy(dst, src, bytes, cudaMemcpyDefault);The Direction Must Match
The flag must agree with your real pointers. A device pointer with a HostToHost kind triggers an error or a crash.
A Common Mix-Up
Swapping HostToDevice and DeviceToHost is a frequent bug. The compiler stays silent, so read each call carefully. 🐛
Read It Like a Sentence
The enum name spells the data flow: HostToDevice means from host to device. Match it to your source and destination.
Pair It With the Pointers
Look at which buffer is the source and which is the destination, then pick the kind that names that exact path.
When in Doubt, Be Explicit
Prefer the named direction over cudaMemcpyDefault. Being explicit makes your intent obvious and catches mistakes sooner.
Quick Check
You are copying one device buffer into another device buffer, never touching the CPU. Which kind fits?
Recap
You met every cudaMemcpyKind: HostToDevice, DeviceToHost, DeviceToDevice, HostToHost, and Default. Match the flag to your pointers every time. 🎉
Frequently asked questions
Is the “The Copy Direction Enum” lesson free?
Yes — the full text of “The Copy Direction Enum” 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 “The Copy Direction Enum”?
cudaMemcpyKind and getting it right. 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 “The Copy Direction Enum” 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