The Naive Matmul Kernel
A 2D-indexed baseline and its limits.
The Naive Matmul 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.
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. 🚀
Frequently asked questions
Is the “The Naive Matmul Kernel” lesson free?
Yes — the full text of “The Naive Matmul 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 Naive Matmul Kernel”?
A 2D-indexed baseline and its limits. 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 Naive Matmul 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 Naive Matmul Kernel
- Tiling the Inner Product
- Looping Over Tile Phases
- Measuring the Speedup