Task 3 of 5

Rectangular: Three Different Sizes

Square matrices hide a trap: every dimension is 16, so any loop bound "works". Real matmuls are rectangular — here rectA is 8×32 (8 rows, 32 columns) and rectB is 32×12, so the product is 8×12. Suddenly there are three different sizes and each belongs somewhere specific.

Two of them shape the launch: output: [width, height] = [columns of B, rows of A] = [12, 8] — already set up below. The third, 32, is the shared dimension: A's columns must equal B's rows, and that's the only dimension the loop is allowed to run over.

8 and 12 shape the launch; 32 is the loop’s whole world
Goal: compute the 8×12 product rectA × rectB — fix the inner loop so it covers the full shared dimension of 32.

Requirements

Hint 1 — which size does the loop get?

The loop walks across a row of A (32 long) and down a column of B (also 32 long — that's why the shapes are compatible). Neither 8 nor 12 appears in the loop at all.

Hint 2 — the fix

The starter loop stops at 12 — it sums only the first 12 of 32 terms. Change the bound: for (let k = 0; k < 32; k++).

Same idea elsewhere

BLAS calls this M, N, K — sgemm(M, N, K, …) in cuBLAS and rocBLAS keeps the three sizes as separate parameters for exactly this reason. Mixing them up is the classic GEMM bug on every platform, not just here.

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.