Building a Matmul Step by Step
From naive loops to a real kernel.
Building a Matmul Step by Step is a free Mojo Academy lesson on CoddyKit — lesson 2 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 Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Matmul Computes
Matrix multiply, or matmul, combines an M by K matrix with a K by N matrix to produce an M by N result. It is the heart of AI math.
The Dot Product Rule
Each output cell is a dot product: walk across one row of A and down one column of B, multiplying and summing as you go.
Three Nested Loops
The naive version uses three loops over i, j, and k. The outer two pick the output cell, the inner one sums the products.
for i in range(M):
for j in range(N):
for k in range(K):
C[i, j] += A[i, k] * B[k, j]Start Each Cell at Zero
Before accumulating, set each result cell to zero. Otherwise old or garbage values pollute your sum.
C[i, j] = 0.0Accumulate the Sum
The inner k loop keeps a running accumulator. Summing into a local variable is often faster than touching C every step.
var acc: Float32 = 0.0
for k in range(K):
acc += A[i, k] * B[k, j]
C[i, j] = accUse fn for Speed
Write the kernel with fn and typed arguments. Strict types let Mojo compile tight machine code with no dynamic overhead.
fn matmul(A: Matrix, B: Matrix, C: Matrix):
passWhy Naive Is Slow
The basic triple loop does the right math but reads B by column, jumping through memory. Poor locality wastes cache and time.
Loop Order Matters
Reordering to i, k, j keeps the inner loop walking memory in straight lines. Better access patterns can speed matmul a lot.
for i in range(M):
for k in range(K):
for j in range(N):
C[i, j] += A[i, k] * B[k, j]The Inner Loop Is the Target
Nearly all the time lives in the innermost loop. That hot inner loop is exactly where vectorizing and tuning pay off.
Correctness First
Get the simple version right and save its output. It becomes the reference you compare every faster kernel against.
A Path to a Real Kernel
From here you add SIMD, tiling, and parallelism. Each step keeps the same result but raises throughput toward peak hardware speed.
Quick Check
Why is the textbook triple-loop matmul often slow in practice?
Recap
Matmul sums a dot product per output cell with three loops; start cells at zero, accumulate locally, and mind loop order for cache. 🔢
Frequently asked questions
Is the “Building a Matmul Step by Step” lesson free?
Yes — the full text of “Building a Matmul Step by Step” is free to read here on the web, and the Mojo 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 Mojo Academy course, upgrade to CoddyKit PRO.
What will I learn in “Building a Matmul Step by Step”?
From naive loops to a real kernel. You practise Mojo 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 Mojo Academy?
No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Building a Matmul Step by Step” 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 Mojo Academy lesson?
Yes. Every Mojo 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
- Modeling a Tensor in Mojo
- Building a Matmul Step by Step
- Optimizing the Inner Product
- Verifying Numeric Correctness