← All problems
#69 Jacobi Stencil 2D Medium

Given a 2D grid of 32-bit floating point values, apply one iteration of the 5-point Jacobi stencil: each interior cell of the output is set to the average of its four cardinal neighbors (top, bottom, left, right) from the input grid. Boundary cells (first/last row and column) are copied unchanged from the input to the output.

5-Point Jacobi Stencil T L (2,2) R B Center cell Neighbors Boundary output[i,j] = ¼ × (top + bottom + left + right)

Implementation Requirements

Example:

Input (\(4 \times 4\)): \[ \begin{bmatrix} 1.0 & 2.0 & 3.0 & 4.0 \\ 5.0 & 6.0 & 7.0 & 8.0 \\ 9.0 & 10.0 & 11.0 & 12.0 \\ 13.0 & 14.0 & 15.0 & 16.0 \end{bmatrix} \] Output (\(4 \times 4\)): \[ \begin{bmatrix} 1.0 & 2.0 & 3.0 & 4.0 \\ 5.0 & 6.0 & 7.0 & 8.0 \\ 9.0 & 10.0 & 11.0 & 12.0 \\ 13.0 & 14.0 & 15.0 & 16.0 \end{bmatrix} \] Interior cell \((1,1)\): \(0.25 \times (\text{input}[0,1] + \text{input}[2,1] + \text{input}[1,0] + \text{input}[1,2])\) \(= 0.25 \times (2.0 + 10.0 + 5.0 + 7.0) = 6.0\)
Interior cell \((1,2)\): \(0.25 \times (\text{input}[0,2] + \text{input}[2,2] + \text{input}[1,1] + \text{input}[1,3])\) \(= 0.25 \times (3.0 + 11.0 + 6.0 + 8.0) = 7.0\)
Interior cell \((2,1)\): \(0.25 \times (\text{input}[1,1] + \text{input}[3,1] + \text{input}[2,0] + \text{input}[2,2])\) \(= 0.25 \times (6.0 + 14.0 + 9.0 + 11.0) = 10.0\)
Interior cell \((2,2)\): \(0.25 \times (\text{input}[1,2] + \text{input}[3,2] + \text{input}[2,1] + \text{input}[2,3])\) \(= 0.25 \times (7.0 + 15.0 + 10.0 + 12.0) = 11.0\)

Constraints

Guided walkthrough coming soon. Below is the official starter signature in each language; the clickable line-by-line explanation hasn't been authored yet.
Problem text & starters from the official AlphaGPU/leetgpu-challenges. Solve it on leetgpu.com.