Task 5 of 5

Pass Something In

So far every kernel has conjured its output from thread coordinates alone. Real kernels also take arguments — declare a parameter on the kernel function, pass a value when you call it, and every thread sees that same value. Combine it with this.thread.x and each thread computes something different from shared input.

Here's the payoff: a compiled kernel is reusable. Build it once, call it with 2.5, call it again with 0.5 — two parallel launches, zero recompiles. That build-once/call-many rhythm is how all real GPU code is structured.

Goal: make ramp return scale * this.thread.x, then call it twice — once with 2.5, once with 0.5.

Requirements

Hint 1 — where arguments come from

Kernel arguments are ordinary function parameters: function (scale) { … }, called as await ramp(3). Every one of the 64 threads receives the same 3.

Hint 2 — the body

return scale * this.thread.x; — the argument is shared, the index is per-thread, the product is different in every cell.

Same idea elsewhere

A value shared by all threads is a uniform: WebGPU binds it as a uniform buffer, CUDA and ROCm pass it as a kernel launch parameter, Metal hands it over with setBytes. And build-once/dispatch-many is universal too — shader and kernel compilation is expensive everywhere, so it's paid once up front.

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.