Task 1 of 5

One Cell, One Dot Product

Matrix multiply is the workload GPUs were born for, and every cell of the result is the same small machine: a dot product. Multiply matching elements of two vectors, add the products up, one number comes out. Get one cell right before launching a grid of them.

Notice what is parallel and what is not. The loop over k runs sequentially inside one thread — GPUs don't parallelize the sum, they parallelize the thousands of independent sums a full matrix needs. This task needs exactly one, so the launch is a single thread: output: [1].

Goal: make the kernel return the dot product of the 16-vectors a and b — one output cell holding a[0]·b[0] + a[1]·b[1] + … + a[15]·b[15].

Requirements

Hint 1 — a loop? inside a kernel?

Yes — as long as the bound is a compile-time constant: for (let k = 0; k < 16; k++) { … }. The loop belongs to one thread; the parallelism (next task) comes from launching many threads that each own a loop.

Hint 2 — the whole body
let sum = 0;
for (let k = 0; k < 16; k++) {
  sum += a[k] * b[k];
}
return sum;

— and output: [1] so only one thread runs it.

Same idea elsewhere

Every GPU linear-algebra library — cuBLAS on CUDA, rocBLAS on ROCm, Metal Performance Shaders — bottoms out in this exact shape: one output element, one multiply-accumulate loop. All their sophistication goes into feeding that loop faster.

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.