The parallelize Function
Run loop iterations across threads.
The parallelize Function is a free Mojo 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 Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
One Core Is Not Enough
A plain loop runs on a single core while the rest sit idle. For heavy work, that wastes most of your CPU.
for i in range(n):
out[i] = heavy(i)Spread the Work
Parallelism means handing different loop iterations to different cores so they all work at the same time. 🧵
Meet parallelize
Mojo's parallelize helper runs your loop body across many threads for you, so you skip the manual thread plumbing.
from algorithm import parallelizeThe Work Closure
You write a small function that does the work for one index. Mojo calls it many times, once per iteration.
fn work(i: Int):
out[i] = heavy(i)Pass the Closure as a Parameter
The closure goes in square brackets as a compile-time parameter, not as a normal call argument. Mojo specializes on it.
parallelize[work](n)The Count Is Runtime
The number in the parentheses is how many work items to run. That count is an ordinary runtime value.
parallelize[work](num_items)Ask the Machine for Cores
Use num_physical_cores from the sys module to learn how many real cores you have to spread work across.
from sys import num_physical_coresCap the Worker Count
An optional second argument sets how many workers run at once. Matching it to your cores often works best.
parallelize[work](n, num_physical_cores())Each Call Is Independent
parallelize assumes work items do not depend on each other's order. Each call should stand on its own.
Best for Heavy Loops
Parallelism shines when each item does real work. For tiny loops, the thread overhead can cost more than it saves.
Measure the Speedup
Time the loop before and after going parallel. A real benchmark tells you whether the extra threads actually helped.
Quick Check
You want to run a loop body across several CPU cores in Mojo.
Recap
You import parallelize, pass a per-item closure as a bracket parameter and the count in parentheses, then size workers to your cores for a real speedup. 🚀
Frequently asked questions
Is the “The parallelize Function” lesson free?
Yes — the full text of “The parallelize Function” 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 “The parallelize Function”?
Run loop iterations across threads. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The parallelize Function” 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
- The parallelize Function
- Splitting Work into Chunks
- Combining Parallel and Vector
- Avoiding Data Races