A fórmula clássica de índice
blockIdx.x * blockDim.x + threadIdx.x.
A fórmula clássica de índice é uma aula grátis de CUDA Academy no CoddyKit. Esta é a aula 1 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.
One Thread, One Element
The whole point of a CUDA kernel is that every thread handles one piece of data. To do that, each thread needs a unique global index.
Why Local IDs Are Not Enough
Inside a block, threadIdx.x only counts 0 up to blockDim minus one. Many blocks reuse those same small numbers, so it cannot be your final index.
Blocks Sit Side by Side
Picture the grid as blocks laid end to end. Each block owns a contiguous slice of the array, and blockIdx.x tells you which slice you are in.
How Wide Is a Block?
blockDim.x is the number of threads per block. It is the width of each slice, so it scales your block offset to the right spot.
The Classic Formula
Combine the three: skip past earlier blocks, then add your position inside this block. That single line gives every thread a unique index.
int i = blockIdx.x * blockDim.x + threadIdx.x;Walking Through It
With 4 threads per block, block 0 covers 0 to 3, block 1 covers 4 to 7, block 2 covers 8 to 11. The offsets never overlap.
A Concrete Example
Thread 2 in block 3 with blockDim 256 lands at 3 times 256 plus 2, which is 770. That is its global position in the data.
// blockIdx.x=3, blockDim.x=256, threadIdx.x=2
int i = 3 * 256 + 2; // i == 770Using the Index
Once you have i, you treat it as an array subscript. Each thread reads and writes only its own element, with no overlap.
out[i] = a[i] + b[i];It Maps One to One
Launch n threads and the formula produces every value from 0 to n minus 1 exactly once. That is a perfect one-to-one cover of the array.
The Order Matters
Always multiply before you add. blockIdx.x * blockDim.x is the start of your slice, and threadIdx.x is the step inside it.
Beyond One Dimension
The same idea extends to 2D and 3D using the .y and .z members, but for flat arrays the .x formula is all you need. 🚀
Quick Check
Compute one thread's global index.
Recap
You learned the formula every kernel uses: blockIdx.x * blockDim.x + threadIdx.x. It hands each thread one unique slot in your array. 🎉
Perguntas Frequentes
A aula “A fórmula clássica de índice” é grátis?
Sim — o texto completo de “A fórmula clássica de índice” é 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 “A fórmula clássica de índice”?
blockIdx.x * blockDim.x + threadIdx.x. 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 1 de 4.
Quanto tempo leva a aula “A fórmula clássica de índice”?
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
- A fórmula clássica de índice
- Proteção contra índices fora do intervalo
- Arredonde o número de blocos para cima
- Laços com passo da grade