Task 2 of 5
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.
0, 1, 2, … 31.output: [32] — 32 threadsthis.thread.x from the kernel bodyYou 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.
The entire kernel body: return this.thread.x;
threadIdx/blockIdx in CUDA and ROCm/HIP,
global_invocation_id in WebGPU's WGSL,
thread_position_in_grid in Metal.
This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.