Task 2 of 5

The Full Grid: Matrix × Matrix

On the CPU, C = A × B is the classic triple loop: over rows, over columns, over k. On the GPU the outer two loops vanish into the launchoutput: [16, 16] starts 256 threads, one per cell of C, and only the innermost loop survives inside the kernel.

Cell C[y][x] is the dot product of row y of A with column x of B: walk k across the row a[y][k] and down the column b[k][x]. Same loop as task 1 — now every thread aims it at its own row/column pair.

row y across, column x down — 256 threads, each owning one dot product
Goal: compute the 16×16 product matA × matB — each thread returns the dot product of its row of a with its column of b.

Requirements

Hint 1 — row and column

this.thread.y picks the row of a, this.thread.x picks the column of b, and k is the only index that moves during the loop.

Hint 2 — the inner loop
let sum = 0;
for (let k = 0; k < 16; k++) {
  sum += a[this.thread.y][k] * b[k][this.thread.x];
}
return sum;

Same idea elsewhere

This one-thread-per-output-cell matmul is the "naive kernel" every WebGPU and CUDA tutorial starts from — and the baseline that tiled, shared-memory versions are measured against. The structure you just wrote is their starting point too.

All tasks in Matrix Multiply

  1. One Cell, One Dot Product
  2. The Full Grid: Matrix × Matrix
  3. Rectangular: Three Different Sizes
  4. Transpose: Swap the Axes
  5. One Kernel, Any Size

This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.