📘
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:
Input:
n = 1024, scale = 3.0
Output:
True
Reasoning:
- 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
Editor
Python 3.13.1
GPU · T4
Test Results
0/0Run code to see test results.