Task 1 of 5
A kernel is an ordinary-looking JavaScript function with one twist: it doesn't run once. gpu.js compiles it and launches it once per output cell, all in parallel — each launch is called a thread. You never call the function in a loop; you tell the GPU how many cells you want, and it runs that many copies.
That cell count is the output option: output: [16] means
“give me 16 cells”, so 16 threads run and their 16 return values come back to you
collected into one array.
One habit to pick up right now, because it runs through the whole course: calling a
kernel is asynchronous. The call hands you a promise while the GPU gets on with the
work, so you write await in front of it and receive the finished result —
const result = await answer();. Building the kernel with
createKernel stays ordinary and synchronous; only the call is awaited.
42 — your first parallel program.output to [16] so 16 threads run42 from the kernel bodyoutput lives in the options object — the second argument to
createKernel. It's an array because output can have more than one
dimension (that's task 4).
The whole call:
gpu.createKernel(function () {
return 42;
}, {
output: [16],
})
And await answer() gives you an array of sixteen 42s.
kernel<<<blocks, threads>>>(), WebGPU calls it
a compute dispatch, Metal dispatches threadgroups. gpu.js just hides the
ceremony behind output.
This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.