CUDA Atomic Max Reduction
Problem Statement
Find the maximum of a 1D array using cuda.atomic.max: every thread atomically maxes its element into out[0].
Background
cuda.atomic.max(out, 0, value) keeps the larger of the current value and value, atomically. Initialize the accumulator to -inf so the first real value always wins.
Your Task
Implement atomic_max_kernel and run(n=4096) returning whether out[0] matches x.max().
How it is tested
Your solution must define a top-level function run(...) that allocates the inputs, copies them to the GPU, launches your @cuda.jit kernel, and returns a Python bool from np.allclose(gpu_result, reference). The grader prints run(...); the expected output is True.
Example
n = 4096
True
- The input
n = 4096represents the size of the 1D array to find the maximum value from. - The
atomic_max_kernelfunction is launched, where every thread atomically updates the maximum value inout[0]usingcuda.atomic.max. - The initial value of
out[0]is set to , ensuring the first real value from the array wins and becomes the new maximum. - After all threads have executed,
out[0]holds the maximum value of the array, which is then compared to the maximum value calculated usingx.max()to produce the outputTrueif they match.
Constraints
- cuda.atomic.max(out, 0, x[i]) for each in-range thread
- Initialize the accumulator to -inf
- A max reduction is exact (no float-order error)