Task 1 of 5
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].
a and b — one output cell holding
a[0]·b[0] + a[1]·b[1] + … + a[15]·b[15].output to a single cell: [1]k from 0 to 15 inside the kernel — statically bounded loops are alloweda[k] * b[k] into a running sum and return itYes — 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.
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.
This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.