0Pricing
CUDA Academy · Aula

threadIdx, blockIdx, blockDim

As variáveis integradas que localizam uma thread.

threadIdx, blockIdx, blockDim é uma aula grátis de CUDA Academy no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de CUDA Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de CUDA Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

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

Perguntas Frequentes

A aula “threadIdx, blockIdx, blockDim” é grátis?

Sim — o texto completo de “threadIdx, blockIdx, blockDim” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de CUDA Academy, atualize para CoddyKit PRO. O curso de CUDA Academy inclui 4 aulas no total.

O que vou aprender em “threadIdx, blockIdx, blockDim”?

As variáveis integradas que localizam uma thread. Você pratica CUDA Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar CUDA Academy?

Nenhuma experiência prévia é necessária. CUDA Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.

Quanto tempo leva a aula “threadIdx, blockIdx, blockDim”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de CUDA Academy?

Sim. Cada aula de CUDA Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. A hierarquia de threads
  2. threadIdx, blockIdx, blockDim
  3. Por que existem blocos
  4. Escolha o número de threads por bloco
← Voltar para CUDA Academy