0Pricing
CUDA Academy · Lesson

Host-to-Device Transfers

Uploading input data to the GPU.

Host-to-Device Transfers is a free CUDA Academy lesson on CoddyKit — lesson 1 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.

Two Memories, One Goal

Your data starts in host RAM, but the GPU can only crunch numbers that live in its own device memory. First you must move it across. 🚚

Meet cudaMemcpy

The cudaMemcpy function is your delivery truck: it copies a block of bytes from one address to another, host or device.

cudaMemcpy(dst, src, bytes, kind);

The Upload Direction

To send input data up to the GPU you use cudaMemcpyHostToDevice. Source is host RAM, destination is device memory.

cudaMemcpy(d_a, h_a, n*sizeof(float), cudaMemcpyHostToDevice);

Argument Order Matters

Like memcpy, the destination comes first, then the source. Swap them by accident and your GPU buffer stays empty. 😬

cudaMemcpy(d_dst, h_src, bytes, cudaMemcpyHostToDevice);

Count Bytes, Not Elements

The size argument is measured in bytes, so multiply your element count by the type size with sizeof.

size_t bytes = n * sizeof(float);

Allocate Before You Copy

The device pointer must already point at real GPU memory. Always cudaMalloc the destination before uploading into it.

cudaMalloc(&d_a, bytes);
cudaMemcpy(d_a, h_a, bytes, cudaMemcpyHostToDevice);

A Synchronous Wait

Plain cudaMemcpy is blocking: your CPU thread pauses until every byte has safely landed on the GPU.

Then Launch Your Kernel

Upload first, compute second. Once the input is on the device, your kernel can read it and start the real work.

cudaMemcpy(d_a, h_a, bytes, cudaMemcpyHostToDevice);
myKernel<<<blocks, threads>>>(d_a, n);

Sizes Must Match

Make sure the byte count you upload fits the buffer you allocated. Copying more than you reserved is a classic overflow bug.

Check the Return Code

cudaMemcpy returns a cudaError_t. Inspect it so a bad pointer or size fails loudly instead of corrupting your run.

cudaError_t err = cudaMemcpy(d_a, h_a, bytes, cudaMemcpyHostToDevice);

Copies Are Not Free

Every upload travels across the slow PCIe bus, so copy only the data you truly need on the device.

Quick Check

You want to send an input array from CPU RAM to the GPU. Which call is correct?

Recap

You learned to upload data with cudaMemcpyHostToDevice: allocate the device buffer, put destination first, size it in bytes, and copy before launching. 🎉

Frequently asked questions

Is the “Host-to-Device Transfers” lesson free?

Yes — the full text of “Host-to-Device 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 “Host-to-Device Transfers”?

Uploading input data to the GPU. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Host-to-Device 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

  1. Host-to-Device Transfers
  2. Device-to-Host Transfers
  3. The Copy Direction Enum
  4. The PCIe Transfer Bottleneck
← Back to CUDA Academy