Task 1 of 5

Your First Kernel

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.

one function, sixteen launches — the loop you never wrote
Goal: finish the kernel so that 16 threads each return the number 42 — your first parallel program.

Requirements

Hint 1 — where does the 16 go?

output 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).

Hint 2 — the whole thing

The whole call:

gpu.createKernel(function () {
  return 42;
}, {
  output: [16],
})

And await answer() gives you an array of sixteen 42s.

Same idea elsewhere

Launching N copies of one function is the primitive of every GPU API: CUDA spells it kernel<<<blocks, threads>>>(), WebGPU calls it a compute dispatch, Metal dispatches threadgroups. gpu.js just hides the ceremony behind output.

All tasks in Hello, Kernel

  1. Your First Kernel
  2. Who Am I? this.thread.x
  3. From For-Loop to Formula
  4. A Second Dimension: this.thread.y
  5. Pass Something In

This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.