v9.1.1
Menu

CUDA In-Place Device Update

easyFREEMemory & Data MovementCUDA Basics with Numba

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

Input
n = 1024, scale = 2.0
Output
True
Reasoning
  • The input values n = 1024 and scale = 2.0 are used to create an array of length n and scale it in place using the scale_inplace_kernel function.
  • The scale_inplace_kernel function applies the transformation x[i]=x[i]⋅scalex[i] = x[i] \cdot scale to each element in the array, effectively doubling each value since scale=2.0scale = 2.0.
  • The mutated array is then compared to the original array scaled by scale using np.allclose, checking if the two arrays are element-wise equal within a small tolerance.
  • The comparison yields True because the in-place scaling operation correctly doubles each element in the array, resulting in an array that matches the original array scaled by 2.02.0.

Constraints

  • Mutate the input array directly: x[i] = x[i] * scale
  • No separate output device array
  • Capture the reference before the kernel runs

More from CUDA Basics with Numba

solution.py

Test results

Sign in to run your code against the test cases.