0Pricing
Mojo Academy · Lesson

Anatomy of a Compute Kernel

The hot inner loop that does the work.

Anatomy of a Compute Kernel is a free Mojo Academy lesson on CoddyKit. This is 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, and your progress syncs across the web and the CoddyKit app. The Mojo Academy course includes 4 lessons in total.

What Is a Kernel?

A compute kernel is the small, focused routine that does the real numerical work, like adding two arrays element by element.

The Hot Inner Loop

Most of a kernel's time lives in one tight inner loop. Speed up that loop and you speed up the whole program.

for i in range(n):
    out[i] = a[i] + b[i]

Inputs and Outputs

A clean kernel takes its data buffers as parameters, so the same routine can run on any arrays you pass in.

fn add(a: UnsafePointer[Float32], b: UnsafePointer[Float32], out: UnsafePointer[Float32], n: Int):
    pass

Use fn for Strictness

Kernels are written with fn, not def. The strict, typed style lets Mojo compile tight machine code with no surprises.

fn kernel(n: Int):
    pass

Keep the Body Small

The fastest kernels do one thing. A short, predictable inner body is easy for the compiler to optimize aggressively.

No Surprises Inside

Avoid heavy work like allocation or I/O in the hot loop. Each iteration should be cheap and uniform for peak throughput.

Count the Work

Think in terms of total operations. A kernel over n elements does roughly n units of work, so n drives the cost.

Memory Is the Limit

Many kernels are not compute-bound but memory-bound. They wait on data, so how you move bytes often matters most.

Plan to Vectorize

Design the loop so each step can later process many elements with SIMD. A regular access pattern makes vectorizing easy.

A Tiny Saxpy Kernel

A classic kernel is saxpy: out = scale times a plus b. It is small, regular, and a great baseline to optimize.

for i in range(n):
    out[i] = scale * a[i] + b[i]

Measure Before You Tune

Start with the simple correct version and time it. That number is your baseline for every optimization that follows.

Quick Check

You are about to optimize a kernel. Where does almost all of its time go?

Recap

A kernel is a small fn whose tight inner loop does the work; keep it uniform, mind memory, and measure a baseline first. 🔧

Frequently Asked Questions

Is the “Anatomy of a Compute Kernel” lesson free?

Yes — the full text of “Anatomy of a Compute Kernel” is free to read here on the web. 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. The Mojo Academy course includes 4 lessons in total.

What will I learn in “Anatomy of a Compute Kernel”?

The hot inner loop that does the 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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Anatomy of a Compute Kernel” 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. Anatomy of a Compute Kernel
  2. Combining SIMD with Loops
  3. Reducing Memory Traffic
  4. Tiling for Cache Locality
← Back to Mojo Academy