0Pricing
Mojo Academy · Lesson

Writing a GPU Kernel Function

Express device code in Mojo.

Writing a GPU Kernel Function 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.

What a Kernel Is

A GPU kernel is the function each thread runs. You write the work for one element, and the hardware repeats it across many.

Think One Thread

The trick is to write a kernel from a single thread's view. Each thread does its own small slice of the work.

A Kernel Is Just an fn

In Mojo a kernel is a normal fn. Its strict, typed nature is exactly what device code needs to compile fast.

fn add_kernel():
    pass

Passing in Buffers

A kernel takes pointers to device memory as parameters. These buffers hold the inputs and the output it will fill.

fn add_kernel(a: UnsafePointer[Float32], out: UnsafePointer[Float32]):
    pass

Finding This Thread's Index

Inside the kernel, each thread computes its own position first. That index selects which element it must process.

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

Guarding the Bounds

Always check the index before touching memory. A bounds check keeps stray threads from reading past the array.

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

Doing the Element's Work

The body is tiny: read inputs, compute, write one result. The whole kernel often fits in a single line of math.

out[i] = a[i] * b[i]

Launching the Kernel

You launch it by choosing a grid and block size. The launch fans your one-thread code out across the whole grid.

ctx.enqueue_function[add_kernel](grid_dim=blocks, block_dim=256)

No Return Value

Kernels do not return results to the caller. They write into the output buffer, which you read back afterward.

Keep It Branch-Light

Threads run best in lockstep. Heavy branching makes lanes diverge and wait, so keep kernel logic simple and uniform.

Same Idea, Massive Scale

One short kernel plus a big grid equals millions of results. The scale comes from the launch, not from longer code.

Quick Check

You are writing the body of a GPU kernel for one thread.

Recap

A kernel is an fn for one thread: find your index, guard the bounds, do one element's math, then launch over a grid. ⚡

Frequently asked questions

Is the “Writing a GPU Kernel Function” lesson free?

Yes — the full text of “Writing a GPU Kernel 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 “Writing a GPU Kernel Function”?

Express device code in Mojo. 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 “Writing a GPU Kernel 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

  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