PIXELBANKv9.1.0
Menu

Problem Statement

Clamp every element into a range: out = min(max(x, lo), hi) with runtime scalars lo and hi.

Background

Two conditionals in sequence bound the value from below and above. Clamping is used to bound activations, gradients, or pixel values.

Your Task

Implement clamp_kernel and run(n=1024, lo=-0.5, hi=0.5) comparing to np.clip(x, lo, hi).

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 = 1024, lo = -0.5, hi = 0.5
Output:
True
Reasoning:
  • The input values n = 1024, lo = -0.5, and hi = 0.5 are used to generate an array x of length n and clamp its elements into the range [โˆ’0.5,0.5][-0.5, 0.5].
  • The clamp_kernel function is applied to x using CUDA, which applies the transformation out=minโก(maxโก(x,โˆ’0.5),0.5)out = \min(\max(x, -0.5), 0.5) to each element.
  • A reference solution is computed using np.clip(x, -0.5, 0.5), which also clamps x into the range [โˆ’0.5,0.5][-0.5, 0.5].
  • The final output is True because the result from the CUDA kernel matches the reference solution, as verified by np.allclose(gpu_result, reference).

Constraints:

  • Bound below by lo then above by hi
  • lo and hi are runtime scalars
  • Bounds-check the global index
๐Ÿ”’

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 Clamp Kernel - Easy | PixelBank