Task 4 of 5

A Second Dimension: this.thread.y

Threads don't have to line up in a row. Give output two numbers — output: [8, 8] — and gpu.js launches an 8×8 grid of 64 threads. Each one now has two coordinates: this.thread.x is its column and this.thread.y is its row, and the result comes back as an array of rows you read as result[y][x].

To prove both coordinates are live, paint a classic: a checkerboard. A cell is “black” or “white” depending on whether x + y is even or odd — which is just (x + y) % 2.

two coordinates per thread — the grid is the picture
Goal: launch an 8×8 grid where each cell holds (x + y) % 2 — an alternating pattern of 0s and 1s.

Requirements

Hint 1 — what changes with 2D?

Two things: output gets a second number ([width, height]), and this.thread.y starts meaning something. Nothing else about the kernel changes.

Hint 2 — the pattern
return (this.thread.x + this.thread.y) % 2;

Neighbours differ by one in x or y, so the parity flips checkerboard-style.

Same idea elsewhere

GPUs are built around 2D grids because images are 2D: ROCm and CUDA launch dim3-shaped blocks, WebGPU dispatches workgroups across x/y/z, and Metal's grids are up to three-dimensional. One thread per pixel — the idea Data In, Data Out runs with — starts exactly here.

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.