The Vector Add Kernel
One thread adds one pair of elements.
The Vector Add Kernel 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.
The Big Idea
Vector addition is the perfect first kernel: each output is just C[i] = A[i] + B[i]. Every element is independent, so they can all run at once. 🚀
One Thread, One Element
The whole trick is simple: you assign one thread to one element. Instead of looping over the array, thousands of threads each do a single add in parallel.
Marking It as a Kernel
A function that runs on the GPU is a kernel, marked with the __global__ qualifier. That word tells nvcc this code launches on the device.
__global__ void vecAdd(const float* A, const float* B, float* C, int n) {
// body comes next
}Kernels Return void
A kernel always has a void return type. There is no return value to hand back to the CPU, so results must be written into device memory instead.
Finding This Thread's Index
Each thread computes its own global index so it knows which element to handle. The classic formula combines the block and thread coordinates.
int i = blockIdx.x * blockDim.x + threadIdx.x;The Single Line of Work
Once a thread knows its index i, the real work is one line. No loop, no branching, just one add per thread.
C[i] = A[i] + B[i];Why a Bounds Check Matters
You usually launch more threads than elements, so add an if (i < n) guard. Without it, extra threads read past the array and crash. 🛡️
if (i < n) {
C[i] = A[i] + B[i];
}The Full Kernel
Put it together and the entire vecAdd kernel is just a few lines. Tiny code, but it runs across thousands of threads at once.
__global__ void vecAdd(const float* A, const float* B, float* C, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) C[i] = A[i] + B[i];
}Pointers Live on the Device
The pointers A, B, and C must point to device memory. Hand a kernel a plain host pointer and it will read garbage or fault.
Mark Inputs as const
A and B are only read, so mark them const float*. This documents intent and lets the compiler optimize the read-only inputs more freely.
No Shared State Needed
Because every thread touches a different element, there are no races and no locks. This independence is exactly what makes the GPU shine here.
Quick Check
Why does the vector add kernel need an if (i < n) guard?
Recap
You wrote your first kernel: __global__ void vecAdd, one thread per element, a global index, and a bounds check. Simple code, massive parallelism. 🎉
Frequently asked questions
Is the “The Vector Add Kernel” lesson free?
Yes — the full text of “The Vector Add Kernel” 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 Vector Add Kernel”?
One thread adds one pair of elements. 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 “The Vector Add Kernel” 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
- The Vector Add Kernel
- Wiring Up the Host Side
- Verifying the Result on the CPU
- Timing Your First Speedup