0Pricing
Mojo Academy · Lesson

Combining Parallel and Vector

Stack threading on top of SIMD.

Combining Parallel and Vector is a free Mojo Academy lesson on CoddyKit — lesson 3 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.

Two Kinds of Speed

SIMD packs many values into one core's instruction. Threads spread work across cores. Stacking both gives you the most. ⚡

Outer Layer: Threads

Use parallelize on the outer level so each core owns a chunk of the data. That is the coarse split across cores.

parallelize[do_chunk](workers)

Inner Layer: Vectors

Inside each chunk, use vectorize so one core chews through its slice in SIMD packs. That is the fine split per core.

from algorithm import vectorize

Import Both Helpers

Both tools live in the algorithm module, so you can bring them in together and combine them in one function.

from algorithm import parallelize, vectorize

The Chunk Worker

Each chunk worker computes its start and end, then hands its own slice to vectorize for the SIMD sweep.

fn do_chunk(c: Int):
    var start = c * chunk

Vectorize the Inner Range

Within the worker, call vectorize on just this chunk's length. The SIMD width sets how many lanes run at once.

vectorize[step, width](end - start)

Offset Into the Data

The inner step gets a local index, so add the chunk start to reach the right spot in the full array.

fn step[w: Int](i: Int):
    out.store[width=w](start + i, ...)

Pick the SIMD Width

Match the vector width to your hardware lanes with simdwidthof so each core uses its registers fully.

alias width = simdwidthof[DType.float32]()

Cores Times Lanes

The win multiplies: many cores, each doing many lanes per step. That product is why combined code is so fast.

Keep Chunks Independent

This stacking only works because chunks never touch each other's data. Independence keeps the two layers safe.

Measure the Combined Gain

Benchmark scalar, vector-only, and combined versions. The numbers reveal how much each layer contributes.

Quick Check

You combine threads and SIMD on the same workload.

Recap

You wrap parallelize over chunks and call vectorize inside each, offsetting by the chunk start, so cores times lanes multiply your throughput. 🚀

Frequently asked questions

Is the “Combining Parallel and Vector” lesson free?

Yes — the full text of “Combining Parallel and Vector” 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 Parallel and Vector”?

Stack threading on top of SIMD. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Combining Parallel and Vector” 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

  1. The parallelize Function
  2. Splitting Work into Chunks
  3. Combining Parallel and Vector
  4. Avoiding Data Races
← Back to Mojo Academy