PIXELBANKv9.1.0
Menu

Problem Statement

Sum a 1D array into a single value using cuda.atomic.add: every thread atomically adds its element into out[0].

Background

When many threads update the same location, plain += races. cuda.atomic.add(out, 0, value) performs a read-modify-write that can't be interrupted, so no updates are lost. Initialize the accumulator to 0 before launching.

Your Task

Implement atomic_sum_kernel and run(n=4096) returning whether out[0] matches x.sum().

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 is used to create a 1D array of size 4096 with random values.
  • The atomic_sum_kernel function is launched with multiple threads, each of which atomically adds its corresponding element from the array to out[0] using cuda.atomic.add.
  • The atomic addition operation ensures that all updates to out[0] are properly synchronized, avoiding any data races or lost updates, resulting in the correct sum of the array elements: out[0]=โˆ‘i=0nโˆ’1xiout[0] = \sum_{i=0}^{n-1} x_i.
  • The final output is True because the sum computed using cuda.atomic.add matches the reference sum computed using x.sum(), i.e., np.allclose(gpu_result, reference) evaluates to True.

Constraints:

  • cuda.atomic.add(out, 0, x[i]) for each in-range thread
  • Initialize the device accumulator to 0
  • Compare with a tolerant atol (float32 atomic order varies)
solution.py

Test Results

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