Threads, Blöcke und Grids
Das Ausführungsmodell der GPU.
Threads, Blöcke und Grids ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 2 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.
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.xEach 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.xComputing 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.xChoosing 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) // 256Guarding 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. 🧵
Häufig gestellte Fragen
Ist die Lektion „Threads, Blöcke und Grids“ kostenlos?
Ja — der vollständige Text von „Threads, Blöcke und Grids“ 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 „Threads, Blöcke und Grids“?
Das Ausführungsmodell der GPU. 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 2 von 4.
Wie lange dauert die Lektion „Threads, Blöcke und Grids“?
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
- Warum GPUs für AI-Workloads
- Threads, Blöcke und Grids
- Eine GPU-Kernelfunktion schreiben
- Daten zum und vom Device übertragen