Der Vector-Add-Kernel
Jeder Thread addiert ein Elementpaar.
Der Vector-Add-Kernel ist eine kostenlose CUDA Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des CUDA Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der CUDA Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🎉
Häufig gestellte Fragen
Ist die Lektion „Der Vector-Add-Kernel“ kostenlos?
Ja — der vollständige Text von „Der Vector-Add-Kernel“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des CUDA Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der CUDA Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Der Vector-Add-Kernel“?
Jeder Thread addiert ein Elementpaar. Du übst CUDA Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um CUDA Academy zu starten?
Keine Vorkenntnisse erforderlich. CUDA Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „Der Vector-Add-Kernel“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser CUDA Academy-Lektion Code schreiben und ausführen?
Ja. Jede CUDA Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Der Vector-Add-Kernel
- Die Host-Seite verdrahten
- Das Ergebnis auf der CPU prüfen
- Die erste Beschleunigung messen