Task 2 of 5

Who Am I? this.thread.x

Sixteen identical 42s prove the launch works, but parallel code is only useful if each thread can do something different. The trick: every thread knows which output cell it owns. That number is this.thread.x — 0 for the first cell, 1 for the next, up to output − 1.

Same function, same arguments, different this.thread.x — that one number is the only thing telling the threads apart, and it's how each one finds its own work.

Goal: make each of the 32 threads return its own index, so the result counts 0, 1, 2, … 31.

Requirements

Hint 1 — it’s already there

You don't compute the index and you don't pass it in. Inside the kernel body, this.thread.x is simply available — gpu.js fills it in per thread.

Hint 2 — the one-liner

The entire kernel body: return this.thread.x;

Same idea elsewhere

Every platform hands threads this same self-identity, just under a different name: threadIdx/blockIdx in CUDA and ROCm/HIP, global_invocation_id in WebGPU's WGSL, thread_position_in_grid in Metal.

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.