PIXELBANKv9.1.0
Menu

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 = 1024 and scale = 3.0, representing the size of the 1D array and the scalar multiplier, respectively.
  • A 1D array x of size n is created, and its elements are multiplied by the scale factor using the mul_kernel function, resulting in an output array out where each element is calculated as outi=xiโ‹…scaleout_i = x_i \cdot scale.
  • The resulting array out is compared to the reference array, which is also calculated as xโ‹…scalex \cdot scale, using np.allclose to check for equality within a tolerance.
  • Since the mul_kernel function correctly multiplies each element of the array by the scale factor, the comparison returns True, 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 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.

solution.py

Test Results

0/0
Run code to see test results.
CUDA Scalar Multiply Kernel - Easy | PixelBank