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 aโ x+bโ y=2.0โ x+(โ1.0)โ y. - 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
Background Knowledge
The problem involves implementing a fused linear combination of two vectors using CUDA, a parallel computing platform developed by NVIDIA. The goal is to compute the expression out = a * x + b * y, where a and b are runtime scalars, and x and y are input vectors. This operation is a fundamental component of many linear algebra algorithms and is commonly used in various fields, including machine learning, scientific computing, and data analysis.
To understand the problem, it's essential to have a basic knowledge of linear algebra, including vector operations such as scalar multiplication and addition. Additionally, familiarity with CUDA and its programming model is necessary to implement the kernel function. The problem also mentions memory bandwidth, which is a critical aspect of parallel computing. By fusing the two scaled adds into one kernel, we can reduce the number of memory accesses, resulting in improved performance.
The problem is tested using a Python function run(), which allocates inputs, copies them to the GPU, launches the kernel, and compares the result with a reference solution using NumPy. The expected output is True, indicating that the GPU result matches the reference solution. This setup allows for easy testing and verification of the implementation.
Algorithm/Approach
The general approach to solving this problem involves implementing a CUDA kernel that performs the fused linear combination of two vectors. The kernel will take the input vectors x and y, scalars a and b, and output vector out as inputs. The kernel will then compute the expression out = a * x + b * y using parallel threads to achieve high performance.
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
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.