GPU Programming
When summing many values on a GPU, why use a tree-shaped reduction (repeatedly adding neighbors to halve the count)?
- AIt needs far fewer additions than one thread summing sequentially
- BMany threads add simultaneously at each stage, summing n values in log2 n stages
- CIt always eliminates floating-point error
Show answer and explanation
Answer: B. Many threads add simultaneously at each stage, summing n values in log2 n stages
Summing sequentially in one thread takes n - 1 steps, but with a tree, n/2 threads add neighboring pairs in the first stage, n/4 in the second and so on, finishing in log2 n stages. The total number of additions is the same, but far fewer stages make it fast in parallel. Within a block the stages proceed in shared memory, and per-block results are combined at the end.
- A. Both need n - 1 additions.
- B. Correct. It restructures the sum to be parallel.
- C. Error does not vanish, though it may shrink.