CUDA Dot Product
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
n = 8192
True
- The input
n = 8192determines the size of the 1D arraysaandb, which are used to compute the dot product. - Each element of
ais multiplied by the corresponding element ofb, resulting in an array of products: . - 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: .
- The computed dot product is compared to the reference result obtained using
np.dot(a, b), and the function returnsTrueif 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