Task 1 of 6
Meet the reduction: many values in, one value out — sum, min,
max, mean. It's the awkward case in GPU land, because a kernel thread writes exactly
one output cell. 4,096 inputs collapsing to 1 output means
output: [1]… a single thread.
You can do it — kernels may loop, as long as the bound is known at compile
time, which is exactly what this.constants is for. But one thread grinding
through 4,096 additions while thousands of its neighbours sit idle is the slowest
possible way to use a GPU. Write it anyway: it's the baseline the rest of this module
tears down.
data
(bound: this.constants.n) and return the total.output: [1] — one thread owns the one output cellfor (let i = 0; i < this.constants.n; i++) — in gpu.js's WebGL backend, loop bounds must be compile-time constantslet sum and return itDeclare let sum = 0; before the loop, add to it inside the loop,
and return sum; after. Plain JavaScript — the transpiler handles it.
One statement: sum += data[i];
This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.