69. 2D Jacobi Stencil 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
- Use only native features (external libraries are not permitted)
- The
solvefunction signature must remain unchanged - The final result must be stored in
output - Read exclusively from
inputand write exclusively tooutput(do not updateinput)
Example:
Input ():
Output ():
Interior cell :
Interior cell :
Interior cell :
Interior cell :
Constraints
- 1 ≤
rows,cols≤ 16,384 - Input values are in the range [-100, 100]
- All values are 32-bit floats
- Performance is measured with
rows= 8,192,cols= 8,192
Starter code (CUDA)
#include <cuda_runtime.h>
// input, output are device pointers
extern "C" void solve(const float* input, float* output, int rows, int cols) {}
From LeetGPU, CC BY-NC-ND 4.0.