0Pricing
CUDA Academy · Lektion

threadIdx, blockIdx, blockDim

Die integrierten Variablen, die einen Thread lokalisieren.

threadIdx, blockIdx, blockDim ist eine kostenlose CUDA 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 CUDA Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der CUDA Academy-Kurs umfasst insgesamt 4 Lektionen.

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

Where Am I?

Every thread runs the same code, so each one needs to ask: which piece of data is mine? CUDA answers with built-in variables. 📍

Meet threadIdx

Inside a kernel, threadIdx tells a thread its position within its own block. The first thread in a block has threadIdx.x equal to 0.

int t = threadIdx.x;

Meet blockIdx

The variable blockIdx tells a thread which block it belongs to within the grid. Block numbering also starts at 0.

int b = blockIdx.x;

Meet blockDim

You also get blockDim, the number of threads per block. If you launched 256 threads, then blockDim.x is 256 for every thread.

int width = blockDim.x;

They Are Automatic

You never set these variables yourself. CUDA fills them in for each thread the moment the kernel launches.

The .x, .y, .z Fields

Each of these has x, y, and z fields for 1D, 2D, or 3D launches. For simple arrays you usually only touch the x field.

Combining Them

On their own, threadIdx repeats in every block. To get a unique spot you must combine blockIdx and blockDim with threadIdx.

A Tiny Walkthrough

With blockDim 4, thread 2 in block 0 is element 2, but thread 2 in block 1 is element 6. The block shifts the start point.

Read-Only Helpers

These built-ins are read-only inside the kernel. Treat them as trusted coordinates describing exactly where your thread sits.

Also gridDim

There is one more friend: gridDim holds the total number of blocks in the grid. It is handy when you loop over more data than threads.

int blocks = gridDim.x;

Why It Matters

These three variables are the raw materials for computing a global index, which is how every thread claims its own element.

Quick Check

Pick the variable that does the stated job.

Recap: Locating a Thread

You met the built-ins: threadIdx for position in a block, blockIdx for the block, and blockDim for block size. Together they pinpoint a thread. ✅

Häufig gestellte Fragen

Ist die Lektion „threadIdx, blockIdx, blockDim“ kostenlos?

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

Was lerne ich in „threadIdx, blockIdx, blockDim“?

Die integrierten Variablen, die einen Thread lokalisieren. Du übst CUDA 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 CUDA Academy zu starten?

Keine Vorkenntnisse erforderlich. CUDA 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 „threadIdx, blockIdx, blockDim“?

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 CUDA Academy-Lektion Code schreiben und ausführen?

Ja. Jede CUDA 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. Die Thread-Hierarchie
  2. threadIdx, blockIdx, blockDim
  3. Warum es Blöcke gibt
  4. Threads pro Block auswählen
← Zurück zu CUDA Academy