Vectorizing a Loop
Turn a scalar loop into vector work.
Vectorizing a Loop 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 Scalar Loop
A plain loop touches one element per step. It is clear but leaves your CPU's vector lanes mostly unused.
for i in range(n):
out[i] = a[i] + b[i]The Vector Idea
Vectorizing means each loop step handles a whole pack of elements instead of one. You do more work per iteration.
Load a Chunk
Instead of one value, you load several at once into a SIMD vector. That single load grabs a slice of your data.
var chunk = a.load[width=4](i)Compute the Whole Chunk
With both chunks in SIMD form, add them in one element-wise step. The math covers every lane at once.
var sum = a.load[width=4](i) + b.load[width=4](i)Store the Result
Write the whole SIMD result back in one move with a vector store. One write covers all the lanes you computed.
out.store[width=4](i, sum)Step by the Width
A vectorized loop advances by the width, not by one. Each pass covers a full pack of elements.
for i in range(0, n, 4):
passLet Mojo Drive It
Mojo's vectorize helper runs a closure across the range in vector steps and handles the bookkeeping for you.
from algorithm import vectorizeA vectorize Closure
You write a small parameterized function that processes one chunk. Mojo calls it with the right width as it sweeps the range.
fn work[w: Int](i: Int):
out.store[width=w](i, a.load[width=w](i) + b.load[width=w](i))Run the Vectorize
Call vectorize with your chunk function, the width, and the total size. It does the looping and the tail for you.
vectorize[work, 4](n)Same Result, Far Faster
The vectorized loop gives identical output to the scalar one but moves through data in big steps, so it finishes sooner.
Measure to Confirm
Always time both versions. Vectorization usually wins big, but a benchmark proves the gain on your real data.
Quick Check
You replace a one-element-per-step loop with SIMD loads and stores. How should the loop advance?
Recap
Vectorizing loads and stores whole chunks, steps by the width, and lets vectorize handle the sweep and tail, giving the same result far faster. 🚀
Frequently asked questions
Is the “Vectorizing a Loop” lesson free?
Yes — the full text of “Vectorizing a Loop” 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 “Vectorizing a Loop”?
Turn a scalar loop into vector work. 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 “Vectorizing a Loop” 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
- Meet the SIMD Type
- Element-Wise SIMD Math
- Choosing a SIMD Width
- Vectorizing a Loop