Neural Mastery
← Practice
Practice: Parallel Sum Reduction (WebGPU)medium

Practice: Parallel Sum Reduction (WebGPU)

medium ML Systems & MLOps 15–20 min

NumPy allowed · plain Python earns 10 bonus points

New to this topic? MLOps Overview
Study this concept first →

Implement parallel binary tree sum reduction, the core reduction pattern executed by GPU workgroups and CUDA kernels.

Task

Implement parallel_sum_reduction(arr). Simulate hierarchical pairwise reduction: at stride s = 1, 2, 4..., add elements pair-by-pair until a single scalar sum remains. Return 0.0 if empty.

Function Signature

parallel_sum_reduction(arr: list[float]) -> float

Examples

Example 1: Power of two length array
Input: arr = [1, 2, 3, 4, 5, 6, 7, 8]
Output: 36
Example 2: Odd length array requiring boundary checks
Input: arr = [2.5, 3.5, 4]
Output: 10

Constraints

  • arr is a list of numbers
  • Simulates tree reduction stages
  • Return scalar float rounded to 6 decimal places
Python3Saved ✓
def parallel_sum_reduction(arr: list[float]) -> float:
"""Simulate parallel tree sum reduction in O(log n) stages.
Return total sum.
"""
# Your code here
pass

Case 1: Power of two length array
Input: arr = [1, 2, 3, 4, 5, 6, 7, 8]
Expected: 36
Case 2: Odd length array requiring boundary checks
Input: arr = [2.5, 3.5, 4]
Expected: 10