Combining SIMD with Loops
Vectorize the kernel's core.
Combining SIMD with Loops 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.
From Scalar to SIMD
To accelerate a kernel, swap the one-at-a-time loop for one that handles a whole pack of values per step with SIMD.
for i in range(n):
out[i] = a[i] + b[i]Pick a Width
Choose how many elements fit in one vector. The width is the number of lanes each step processes together.
alias width = 4Load a Chunk
Grab several elements at once into a SIMD value with a vector load instead of reading them one by one.
var va = a.load[width=width](i)Compute the Pack
Run the kernel's math on both chunks at once. The whole pack is added element-wise in a single operation.
var vsum = a.load[width=width](i) + b.load[width=width](i)Store the Pack
Write the full result back with a vector store, covering every lane you just computed in one move.
out.store[width=width](i, vsum)Step by the Width
The vectorized loop advances by the width, not by one. Each pass covers a full pack of elements.
for i in range(0, n, width):
passLet vectorize Help
Mojo's vectorize helper sweeps a closure across the range in vector steps so you skip the manual bookkeeping.
from algorithm import vectorizeWrite the Chunk Closure
You define a small parameterized function that handles one chunk. Mojo calls it with the right width as it sweeps.
fn body[w: Int](i: Int):
out.store[width=w](i, a.load[width=w](i) + b.load[width=w](i))Run vectorize
Call vectorize with your closure, the width, and the size. It loops and even handles the leftover tail for you.
vectorize[body, width](n)Mind the Tail
When n is not a multiple of the width, a few elements remain. vectorize cleans up that tail so nothing is missed.
Same Output, More Speed
The vectorized kernel produces identical results but moves through data in big steps, so it finishes much sooner.
Quick Check
You vectorize a kernel with width 4 but n is 10. What handles the last two elements?
Recap
Vectorize a kernel by loading and storing packs, stepping by the width, and letting vectorize handle the sweep and the tail. 🚀
Frequently asked questions
Is the “Combining SIMD with Loops” lesson free?
Yes — the full text of “Combining SIMD with Loops” 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 “Combining SIMD with Loops”?
Vectorize the kernel's core. 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 “Combining SIMD with Loops” 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