What we're building
Vector addition: for . On a CPU it's a one-line loop; on a GPU it's the canonical first kernel because every step is independent — perfect for one-thread-per-element parallelism.
The full program does six things, and that pattern repeats in every CUDA app:
- Allocate input/output buffers on the host (
malloc). - Allocate matching buffers on the device (
cudaMalloc). - Copy inputs from host to device (
cudaMemcpy). - Launch the kernel.
- Copy the result back to the host.
- Free both sides.
Master this six-step ritual and you've got the skeleton for everything else.
