0Pricing
Mojo Academy · Lesson

Splitting Work into Chunks

Divide a range for balanced load.

Splitting Work into Chunks 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.

Why Chunk at All

Launching a thread per element is wasteful. Instead you split the range into a few larger chunks, one per worker.

A Chunk Is a Slice

Each chunk is just a contiguous slice of indices, like 0 to 99 or 100 to 199. One worker owns one slice.

Pick a Chunk Count

A common choice is one chunk per core. You ask the machine for that number with num_physical_cores.

var workers = num_physical_cores()

Compute the Chunk Size

Divide the total length by the worker count to get how many items each worker should handle.

var chunk = (n + workers - 1) // workers

Find a Chunk's Start

Inside the work function, the chunk index tells you where to begin. Multiply it by the chunk size.

var start = c * chunk

Clamp the End

The last chunk may run past the data, so clamp its end to the total length with min to stay in bounds.

var end = min(start + chunk, n)

Loop Inside the Chunk

Each worker runs a normal loop over just its own range. Inside the chunk it is plain, ordinary code.

for i in range(start, end):
    out[i] = heavy(i)

parallelize Over Chunks

Now you call parallelize with the number of chunks, not the number of elements. Each call processes one whole chunk.

parallelize[do_chunk](workers)

Balance the Load

Equal-sized chunks keep every core equally busy. If one chunk is far bigger, that worker finishes late.

More Chunks Than Cores

When some items cost more than others, using more, smaller chunks lets fast workers grab extra work and stay busy.

Tune by Measuring

The best chunk count depends on your data and machine. Try a few sizes and keep the one your benchmark likes.

Quick Check

You split a length-n array into one chunk per worker.

Recap

You divide the range into balanced chunks, clamp each end with min, loop inside a chunk, and call parallelize over the chunk count for even load. 🚀

Frequently asked questions

Is the “Splitting Work into Chunks” lesson free?

Yes — the full text of “Splitting Work into Chunks” 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 “Splitting Work into Chunks”?

Divide a range for balanced load. 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 “Splitting Work into Chunks” 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