Task 3 of 5
Here's the payoff of the thread index. On the CPU you'd sample a sine wave like this:
for (let i = 0; i < 64; i++) {
wave[i] = Math.sin(i / 64 * 2 * Math.PI);
}
On the GPU, the loop disappears — the 64 iterations become 64 threads,
and the loop variable i becomes this.thread.x. The body of the loop
is your kernel body, unchanged. (Math.sin and Math.PI work inside
kernels, along with most of Math.)
x returns Math.sin(x / 64 * 2 * Math.PI).output: [64] — one thread per samplethis.thread.x where the CPU loop used iTake the CPU loop body, delete the loop, and substitute
this.thread.x for i. That mechanical rewrite is how most
for-loops become kernels.
return Math.sin(this.thread.x / 64 * 2 * Math.PI);This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.