CUDA Fused AXPBY Kernel
Problem Statement
Implement a fused linear combination of two vectors: out = a * x + b * y with two runtime scalars a and b.
Background
Fusing both scaled adds into one kernel reads each input once and writes the output once, saving memory bandwidth versus separate scale-and-add passes.
Your Task
Implement axpby_kernel and run(n=1024, a=2.0, b=-1.0) comparing to a * x + b * y.
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 = 1024, a = 2.0, b = -1.0
True
- The input values are
n = 1024,a = 2.0, andb = -1.0, which represent the size of the vectors and the scalar coefficients, respectively. - Two random vectors
xandyof sizenare generated, and the reference result is calculated as . - The
axpby_kernelfunction is launched on the GPU, performing the fused linear combination ofxandyusing the scalar coefficientsaandb, and storing the result in theoutvector. - The result from the GPU is compared to the reference result using
np.allclose, which checks if the two arrays are element-wise equal within a tolerance, and returnsTrueif they are equal.
Constraints
- Single kernel computing ax + by
- Two input arrays, two scalar arguments
- Bounds-check the global index