CUDA In-Place Device Update
Problem Statement
Scale a device array in place: x[i] = x[i] * scale, writing back into the same buffer rather than a second output array.
Background
A kernel can read and write the same device array. Allocate it once with cuda.to_device, mutate it, then copy it back — no separate output buffer.
Your Task
Implement scale_inplace_kernel and run(n=1024, scale=2.0) returning whether the mutated array equals the original times 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 = 2.0
True
- The input values
n = 1024andscale = 2.0are used to create an array of lengthnand scale it in place using thescale_inplace_kernelfunction. - The
scale_inplace_kernelfunction applies the transformation to each element in the array, effectively doubling each value since . - The mutated array is then compared to the original array scaled by
scaleusingnp.allclose, checking if the two arrays are element-wise equal within a small tolerance. - The comparison yields
Truebecause the in-place scaling operation correctly doubles each element in the array, resulting in an array that matches the original array scaled by .
Constraints
- Mutate the input array directly: x[i] = x[i] * scale
- No separate output device array
- Capture the reference before the kernel runs