Tiling for Cache Locality
Block work to fit in cache.
Tiling for Cache Locality is a free Mojo Academy lesson on CoddyKit — lesson 4 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.
The Cache Idea
The CPU keeps recently used data in a small, fast cache. Hits are quick; misses force a slow trip to main memory.
What Is Locality?
Cache locality means reusing data that is already nearby in the cache before it gets evicted to make room.
Big Data Spills the Cache
If your kernel sweeps a huge array, early elements are evicted before you reuse them, causing repeated cache misses.
Tile the Work
Tiling splits a big loop into small blocks that fit in cache, so each block's data stays hot while you use it.
alias tile = 64An Outer and Inner Loop
Tiling turns one loop into two: an outer loop over blocks and an inner loop over the elements inside each block.
for t in range(0, n, tile):
for i in range(t, min(t + tile, n)):
out[i] = a[i] * 2One Block at a Time
Each block is small enough to live in cache. You finish all its work before moving on, so reuse stays cheap.
Great for Matrices
Matrix multiply reuses rows and columns heavily. Blocking a matmul into tiles keeps reused data in cache and cuts misses.
Pick the Tile Size
The best tile fills the cache without overflowing it. Too small wastes reuse; too large spills, so you tune the size.
Tile Then Vectorize
Tiling and SIMD stack well. Vectorize the inner loop of each block to get cache locality and vector speed together.
for t in range(0, n, tile):
vectorize[body, width](min(tile, n - t))Mind the Edges
The last block may be smaller than a full tile. Clamp its range with min so you never read past the buffer.
var end = min(t + tile, n)Measure the Gain
Tiling can help a lot or a little depending on sizes. Always benchmark a few tile values on your real data to choose.
Quick Check
Your kernel keeps missing cache because it sweeps a huge array end to end. What technique helps?
Recap
Tiling blocks a big loop so each chunk fits in cache; tune the tile size, vectorize the inner loop, and clamp the edges. 🧱
Frequently asked questions
Is the “Tiling for Cache Locality” lesson free?
Yes — the full text of “Tiling for Cache Locality” 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 “Tiling for Cache Locality”?
Block work to fit in cache. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Tiling for Cache Locality” 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
- Anatomy of a Compute Kernel
- Combining SIMD with Loops
- Reducing Memory Traffic
- Tiling for Cache Locality