# One Cell, One Dot Product

*Task 1 of 5 · [Matrix Multiply](https://gpu.rocks/learn/matrix-multiply-972e080b.md) · GPU.js Learn*

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

**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

- Change `output` to a single cell: `[1]`
- Loop `k` from 0 to 15 *inside* the kernel — statically bounded loops are allowed
- Accumulate `a[k] * b[k]` into a running sum and return it

## 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

```js
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.

## Starter code

```js
// A dot product folds two 16-vectors into ONE number.
const gpu = new GPU({ mode });

const dot = gpu.createKernel(function (a, b) {
  // TODO: one thread owns the whole sum. Loop k = 0..15,
  // multiply matching elements, add them up, return the total.
  return a[this.thread.x] * b[this.thread.x];
}, {
  // TODO: how many output cells does a dot product have?
  output: [16],
});

console.log(await dot(a, b));
```

---

Interactive version: https://gpu.rocks/learn/matrix-multiply-972e080b/1

[Next task](https://gpu.rocks/learn/matrix-multiply-972e080b/2.md)
