0Pricing
Mojo Academy · Lektion

Eine GPU-Kernelfunktion schreiben

Formulieren Sie Device-Code in Mojo.

Eine GPU-Kernelfunktion schreiben ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Mojo Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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. ⚡

Häufig gestellte Fragen

Ist die Lektion „Eine GPU-Kernelfunktion schreiben“ kostenlos?

Ja — der vollständige Text von „Eine GPU-Kernelfunktion schreiben“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Mojo Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Eine GPU-Kernelfunktion schreiben“?

Formulieren Sie Device-Code in Mojo. Du übst Mojo Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Mojo Academy zu starten?

Keine Vorkenntnisse erforderlich. Mojo Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.

Wie lange dauert die Lektion „Eine GPU-Kernelfunktion schreiben“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Mojo Academy-Lektion Code schreiben und ausführen?

Ja. Jede Mojo Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Warum GPUs für AI-Workloads
  2. Threads, Blöcke und Grids
  3. Eine GPU-Kernelfunktion schreiben
  4. Daten zum und vom Device übertragen
← Zurück zu Mojo Academy