Task 1 of 5
A convolution slides a small window of weights along a signal:
each output sample is a weighted average of the input around it. With weights
[0.25, 0.5, 0.25] the window smooths — every sample leans toward
its neighbors and jitter cancels out.
On the GPU nothing actually slides. Every output sample gets its own thread, and each thread reads its own three inputs, all at the same time. The only wrinkle is the ends: sample 0 has no left neighbor, so we clamp — reuse the nearest in-bounds sample instead of reading past the edge.
signal — each output is
0.25·left + 0.5·center + 0.25·right, with indexes clamped at both ends.signal[x - 1] and signal[x + 1]0 becomes 0, above 127 becomes 1270.25·left + 0.5·center + 0.25·rightThread x only ever touches signal[x - 1],
signal[x] and signal[x + 1]. Three reads, one weighted sum,
done — the "sliding" is 128 threads doing this at once.
let left = x - 1;
if (left < 0) left = 0;
and the mirror image
for right against 127. Plain if statements work
fine inside kernels.
let left = x - 1;
if (left < 0) left = 0;
let right = x + 1;
if (right > 127) right = 127;
return 0.25 * signal[left] + 0.5 * signal[x] + 0.25 * signal[right];This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.