0Pricing
Mojo Academy · Lesson

Threads, Blocks, and Grids

The GPU execution model.

Threads, Blocks, and Grids 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.

The Smallest Worker

A GPU runs your kernel as many tiny workers. Each single worker is a thread, and it usually handles one piece of data.

Grouping Threads

Threads are organized into a block. Threads in the same block can cooperate and share fast on-chip memory.

All Blocks Together

All the blocks for one kernel launch form the grid. The grid is the full set of workers handling your whole problem.

A Simple Hierarchy

The model nests neatly: threads live in blocks, and blocks live in a grid. Three levels describe every launch.

Each Thread Has an ID

Every thread can ask where it sits. Its index tells it which element of the data it is responsible for.

var tid = thread_idx.x

Each Block Has an ID Too

Blocks are numbered as well. A thread combines its block id with its local id to find a unique spot.

var bid = block_idx.x

Computing a Global Index

The classic formula maps each thread to one global element. This global index is how kernels split an array.

var i = block_idx.x * block_dim.x + thread_idx.x

Choosing Block Size

You pick how many threads sit in a block. A good size keeps the hardware busy without wasting resources.

Choosing Grid Size

The grid must cover all your data. You size it so threads times blocks reach every element you need.

var blocks = (n + 255) // 256

Guarding the Edges

The grid often launches a few extra threads. A simple bounds check stops them from touching memory past the end.

if i < n:
    out[i] = a[i] + b[i]

Why This Shape Helps

Blocks let groups share memory and sync, while the grid scales to any size. The structure maps work onto hardware cleanly.

Quick Check

A thread needs the position of its element across the whole array.

Recap

Threads group into blocks, blocks form the grid, and combining their ids gives each worker a unique global index. 🧵

Frequently asked questions

Is the “Threads, Blocks, and Grids” lesson free?

Yes — the full text of “Threads, Blocks, and Grids” 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 “Threads, Blocks, and Grids”?

The GPU execution model. 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 “Threads, Blocks, and Grids” 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. Why GPUs for AI Workloads
  2. Threads, Blocks, and Grids
  3. Writing a GPU Kernel Function
  4. Moving Data to and from Device
← Back to Mojo Academy