CUDA Scalar Multiply Kernel
Problem Statement
Multiply every element of a 1D array by a runtime scalar: out = x * scale.
Background
Scalars are passed as ordinary kernel arguments โ no device pointer needed โ and used directly inside the kernel.
Your Task
Implement mul_kernel and run(n=1024, scale=3.0) returning whether the output equals x * scale.
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, scale = 3.0
True
- The input values are
n = 1024andscale = 3.0, representing the size of the 1D array and the scalar multiplier, respectively. - A 1D array
xof sizenis created, and its elements are multiplied by thescalefactor using themul_kernelfunction, resulting in an output arrayoutwhere each element is calculated as outiโ=xiโโ scale. - The resulting array
outis compared to the reference array, which is also calculated as xโ scale, usingnp.allcloseto check for equality within a tolerance. - Since the
mul_kernelfunction correctly multiplies each element of the array by thescalefactor, the comparison returnsTrue, indicating that the output array matches the reference array.
Constraints:
- Pass scale as a plain kernel argument
- out[i] = x[i] * scale
- Bounds-check the global index
Background Knowledge
The problem involves using CUDA, a parallel computing platform and programming model developed by NVIDIA. CUDA allows developers to harness the power of Graphics Processing Units (GPUs) to perform general-purpose computing tasks, such as scientific simulations, data analysis, and machine learning. In the context of this problem, we will be using CUDA to perform a simple scalar multiplication operation on a 1D array.
The CUDA Execution Model is based on a hierarchical structure, where a kernel is launched on a grid of blocks, each containing a set of threads. This allows for massive parallelization of tasks, making CUDA well-suited for large-scale computations. In this problem, we will be implementing a kernel function, mul_kernel, which will be executed on the GPU to perform the scalar multiplication operation.
To solve this problem, we need to understand how to launch a CUDA kernel, how to pass arguments to the kernel, and how to manage memory on the GPU. We will also need to use Numba, a Python library that provides a high-level interface to CUDA, to define and launch our kernel function. Additionally, we will need to use NumPy to create and manipulate arrays, and to compare the results of our GPU computation with a reference solution.
Algorithm/Approach
The general approach to solving this type of problem involves the following steps:
- Define a kernel function that performs the desired operation (in this case, scalar multiplication)
- Launch the kernel function on the GPU, passing in the necessary arguments (e.g. the input array and scalar value)
- Allocate memory on the GPU to store the output of the kernel function
- Copy the output from the GPU to the host (CPU) for verification
- Compare the output of the GPU computation with a reference solution to verify correctness
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.