PIXELBANKv9.1.0
Menu

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:

Input:
n = 4096
Output:
True
Reasoning:
  • The input n = 4096 represents the size of the 1D array to find the maximum value from.
  • The atomic_max_kernel function is launched, where every thread atomically updates the maximum value in out[0] using cuda.atomic.max.
  • The initial value of out[0] is set to โˆ’โˆž-\infty, 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 using x.max() to produce the output True if 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)
๐Ÿ”’

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
CUDA Atomic Max Reduction - Medium | PixelBank