0Pricing
CUDA Academy · Lesson

Looping Over Tile Phases

Accumulating partial sums across tiles.

Looping Over Tile Phases is a free CUDA Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CUDA Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Frequently asked questions

Is the “Looping Over Tile Phases” lesson free?

Yes — the full text of “Looping Over Tile Phases” is free to read here on the web, and the CUDA Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CUDA Academy course, upgrade to CoddyKit PRO.

What will I learn in “Looping Over Tile Phases”?

Accumulating partial sums across tiles. You practise CUDA Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start CUDA Academy?

No prior experience is required. CUDA Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Looping Over Tile Phases” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this CUDA Academy lesson?

Yes. Every CUDA Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The Naive Matmul Kernel
  2. Tiling the Inner Product
  3. Looping Over Tile Phases
  4. Measuring the Speedup
← Back to CUDA Academy