0Pricing
CUDA Academy · Aula

Percorra as fases dos blocos

Acumule somas parciais entre os blocos.

Percorra as fases dos blocos é uma aula grátis de CUDA Academy no CoddyKit. Esta é a aula 3 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.

The Dot Product Is Split

A full row times a full column is too big for one tile. So you split that long sum into chunks of width TILE, one chunk per phase.

Counting the Phases

If the matrices are N wide and tiles are TILE wide, you need N / TILE phases to cover the whole inner dimension.

int numPhases = (N + TILE - 1) / TILE;

The Outer Phase Loop

Wrap your load-sync-compute steps in a loop over phase. Each pass slides the tile window further along the row of A and column of B.

for (int phase = 0; phase < numPhases; ++phase) {
  // load, sync, compute, sync
}

Accumulate Across Phases

The local sum variable lives outside the loop, so it keeps growing. Each phase adds its slice of the dot product to the running total.

float sum = 0.0f;
for (int phase = 0; phase < numPhases; ++phase) { ... }

Tile Offset per Phase

Each phase shifts the column you read from A and the row you read from B by phase * TILE. That is how the window advances.

As[ty][tx] = A[row*N + phase*TILE + tx];
Bs[ty][tx] = B[(phase*TILE + ty)*N + col];

Sync After Loading

Just like before, call __syncthreads() after the loads so the tile is complete before anyone computes on it.

__syncthreads();

Compute This Phase's Slice

The inner loop adds TILE products into sum, using only the freshly loaded tile. It contributes one chunk of the final dot product.

for (int k = 0; k < TILE; ++k)
  sum += As[ty][k] * Bs[k][tx];

The Second Barrier

End each phase with another __syncthreads() so no thread overwrites the tile while a slower thread is still reading it. 🚧

__syncthreads(); // before the next phase loads

Why Two Syncs Matter

One barrier guards reads-after-load, the other guards loads-after-read. Together they keep every thread in lockstep across phases.

Write the Final Sum

After all phases finish, the running sum is the complete dot product. Store it into C once, guarded by a bounds check.

if (row < N && col < N)
  C[row*N + col] = sum;

Handling Ragged Sizes

When N is not a clean multiple of TILE, load zero for out-of-range elements so the extra products add nothing to the sum.

Quick Check

Think about where the running total lives during the phase loop.

Recap

You looped over phases, shifting tiles, syncing twice, and accumulating partial sums into one total. Now let us measure how much faster it is. 📈

Perguntas Frequentes

A aula “Percorra as fases dos blocos” é grátis?

Sim — o texto completo de “Percorra as fases dos blocos” é 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 “Percorra as fases dos blocos”?

Acumule somas parciais entre os blocos. 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 3 de 4.

Quanto tempo leva a aula “Percorra as fases dos blocos”?

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. O núcleo ingênuo de multiplicação de matrizes
  2. Divida o produto interno em blocos
  3. Percorra as fases dos blocos
  4. Meça o ganho de velocidade
← Voltar para CUDA Academy