# Your First Kernel

*Task 1 of 5 · [Hello, Kernel](https://gpu.rocks/learn/hello-kernel-f1399353.md) · GPU.js Learn*

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.

## Figures

- **one function, sixteen launches — the loop you never wrote**

## Goal

**Goal:** finish the kernel so that **16 threads** each return
the number `42` — your first parallel program.

## Requirements

- Set `output` to `[16]` so 16 threads run
- Return `42` from the kernel body
- Call the kernel and log the result (already wired up)

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

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

## Starter code

```js
// A kernel runs once per output cell — in parallel, not in a loop.
const gpu = new GPU({ mode });

const answer = gpu.createKernel(function () {
  // TODO: every thread should return the same number: 42
  return 0;
}, {
  // TODO: give the kernel 16 output cells, not 1
  output: [1],
});

// Calling a kernel is asynchronous — await it to get the finished result.
const result = await answer();
console.log(result);
```

---

Interactive version: https://gpu.rocks/learn/hello-kernel-f1399353/1

[Next task](https://gpu.rocks/learn/hello-kernel-f1399353/2.md)
