Task 1 of 6

Pass an Array In

Kernels don't reach out and grab data — data is handed to them as arguments, and every thread sees the same arguments. What differs between threads is exactly one thing: this.thread.x, the index of the output cell this thread owns.

Here data is a 64-number array. The kernel below runs 64 times — once per output cell — and each run should pick out its own element.

Goal: make the kernel return double the element of data that belongs to this thread.

Requirements

Hint 1 — which element is mine?

With output: [64] there are 64 threads, numbered this.thread.x = 0…63. Thread 7 should read data[7].

Hint 2 — the one-liner

The whole kernel body is a single statement: return data[this.thread.x] * 2;

Same idea elsewhere

Arguments-in, index-by-thread-id is the universal GPGPU calling convention: CUDA kernels get device pointers plus threadIdx, WebGPU compute shaders get bound buffers plus global_invocation_id. Same shape, different spelling.

All tasks in Data In, Data Out

  1. Pass an Array In
  2. Shape the Output: 2D
  3. Grayscale, the GPU way
  4. Read the Results Back
  5. Images Are Just Arrays
  6. Put It Together: Two Kernels

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