PIXELBANKv9.1.0
Menu

Problem Statement

Compute the dot product of two 1D arrays, sum(a * b), by multiplying elementwise and reducing. Use a shared-memory tree reduction per block plus one atomic add per block into the global result.

Background

This fuses an elementwise multiply with a reduction: each thread loads a[i] * b[i] into shared memory, the block tree-reduces, and thread 0 atomically adds the block's partial into out[0].

Your Task

Implement dot_kernel and run(n=8192) returning whether out[0] matches np.dot(a, b).

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 = 8192
Output:
True
Reasoning:
  • The input n = 8192 determines the size of the 1D arrays a and b, which are used to compute the dot product.
  • Each element of a is multiplied by the corresponding element of b, resulting in an array of products: aiโ‹…bia_i \cdot b_i.
  • The products are then reduced using a tree reduction per block, followed by an atomic add per block into the global result, to compute the dot product: โˆ‘i=0nโˆ’1aiโ‹…bi\sum_{i=0}^{n-1} a_i \cdot b_i.
  • The computed dot product is compared to the reference result obtained using np.dot(a, b), and the function returns True if the two results are close, indicating a successful computation.

Constraints:

  • Load s[t] = a[i] * b[i] (0.0 for out-of-range threads)
  • Tree-reduce within the block, then thread 0 atomic-adds the partial
  • Initialize the device accumulator to 0
๐Ÿ”’

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 Dot Product - Hard | PixelBank