Der naive Matmul-Kernel
Eine 2D-indizierte Ausgangsbasis und ihre Grenzen.
Der naive Matmul-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.
Matrix Multiply, GPU Style
Matrix multiplication is the heart of graphics and AI. Today you build a naive GPU version first, then learn why it leaves speed on the table.
The Math in One Line
Each output cell C[row][col] is a dot product: multiply a full row of A by a full column of B and sum the results. 🧮
C[row][col] = sum over k of A[row][k] * B[k][col]One Thread per Output
The simplest plan gives each thread one output element of C. Thousands of cells get computed at the same time across the GPU.
A 2D Grid of Threads
Since C is a 2D grid, you launch threads in two dimensions. The x index maps to a column and the y index maps to a row.
dim3 threads(16, 16);
dim3 blocks((N+15)/16, (N+15)/16);Finding This Thread's Cell
Inside the kernel, each thread computes its own row and col from its block and thread indices, just like 1D indexing but on both axes.
int row = blockIdx.y*blockDim.y + threadIdx.y;
int col = blockIdx.x*blockDim.x + threadIdx.x;The Bounds Check
Grids round up, so some threads fall outside the matrix. Guard with if (row < N && col < N) before you touch memory.
if (row < N && col < N) {
// safe to compute
}The Inner Loop
Each thread runs a loop over k, accumulating products into a local sum. That local variable lives in a fast register.
float sum = 0.0f;
for (int k = 0; k < N; ++k)
sum += A[row*N+k] * B[k*N+col];Writing the Result
After the loop finishes, the thread stores its accumulated sum into C exactly once. One thread, one clean write.
C[row*N + col] = sum;Row-Major Flattening
The matrix is a flat 1D array, so you index it as row*N + col. Getting this layout right is half the battle in matmul.
Why It Works, But Slowly
This kernel is correct and easy to read, but every thread reads its row and column straight from global memory, the slowest space.
The Hidden Cost
Neighboring threads re-read the same A rows and B columns over and over. That wasted memory traffic is exactly what tiling will fix next.
Quick Check
Think about how the naive kernel maps work to threads.
Recap
You mapped one thread to one output cell, looped over k from global memory, and saw the redundant reads. Next you cut that traffic with tiling. 🚀
Häufig gestellte Fragen
Ist die Lektion „Der naive Matmul-Kernel“ kostenlos?
Ja — der vollständige Text von „Der naive Matmul-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 naive Matmul-Kernel“?
Eine 2D-indizierte Ausgangsbasis und ihre Grenzen. 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 naive Matmul-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 naive Matmul-Kernel
- Das Skalarprodukt kacheln
- Über die Kachelphasen iterieren
- Die Beschleunigung messen