0Pricing
CUDA Academy · Lesson

Wiring Up the Host Side

Alloc, copy, launch, copy back, free.

Wiring Up the Host Side 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.

The Host Has a Job Too

The kernel does the math, but the host CPU sets everything up. Its job is allocate, copy in, launch, copy back, and free. 🧩

Two Sets of Pointers

You keep host pointers for CPU arrays and separate device pointers for GPU buffers. A common habit is naming them h_A and d_A.

float *h_A, *h_B, *h_C;
float *d_A, *d_B, *d_C;

Fill the Input on the CPU

First prepare your data in normal host memory. Here you just initialize h_A and h_B with values the GPU will add later.

for (int i = 0; i < n; i++) {
    h_A[i] = i; h_B[i] = 2 * i;
}

Allocate on the Device

The GPU needs its own buffers, so call cudaMalloc for each array. Note the size is in bytes, computed as count times sizeof(float).

size_t bytes = n * sizeof(float);
cudaMalloc(&d_A, bytes);

Copy Inputs Up

Move your input arrays to the GPU with cudaMemcpy and the HostToDevice direction. The kernel can only see data that lives on the device.

cudaMemcpy(d_A, h_A, bytes, cudaMemcpyHostToDevice);
cudaMemcpy(d_B, h_B, bytes, cudaMemcpyHostToDevice);

Decide the Launch Shape

Pick a thread count per block, then compute how many blocks cover all elements. Rounding up guarantees every element gets a thread.

int threads = 256;
int blocks = (n + threads - 1) / threads;

Launch the Kernel

Now fire the kernel with the triple-angle-bracket syntax, passing your device pointers. This call returns immediately while the GPU works.

vecAdd<<<blocks, threads>>>(d_A, d_B, d_C, n);

Copy the Result Down

When the kernel finishes, bring C back with cudaMemcpyDeviceToHost. This copy also waits for the launch to complete first.

cudaMemcpy(h_C, d_C, bytes, cudaMemcpyDeviceToHost);

Free Device Memory

GPU memory is not garbage collected, so release every buffer with cudaFree. Skipping this leaks memory that lasts until the process exits.

cudaFree(d_A); cudaFree(d_B); cudaFree(d_C);

The Order Is Sacred

Always follow the same sequence: allocate, copy in, launch, copy back, free. Swap steps and the kernel reads stale or invalid data.

Host Code Is Plain C++

Notice the host side is ordinary C++ plus a handful of cuda calls. There is no special compiler magic beyond the kernel launch.

Quick Check

What must happen before you can launch the vecAdd kernel?

Recap

You wired the host side end to end: two pointer sets, cudaMalloc, copy up, launch, copy back, and cudaFree. The boilerplate that powers every kernel. ✅

Frequently asked questions

Is the “Wiring Up the Host Side” lesson free?

Yes — the full text of “Wiring Up the Host Side” 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 “Wiring Up the Host Side”?

Alloc, copy, launch, copy back, free. 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 “Wiring Up the Host Side” 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. The Vector Add Kernel
  2. Wiring Up the Host Side
  3. Verifying the Result on the CPU
  4. Timing Your First Speedup
← Back to CUDA Academy