GPU Programming
How many stages does a tree-shaped reduction need to sum 1024 values?
- A10
- B512
- C1023
Show answer and explanation
Answer: A. 10
Each stage halves the number of values, 1024 → 512 → 256 → ... → 1, which takes 10 halvings, so the number of stages is log2 1024 = 10. The total number of additions is 512 + 256 + ... + 1 = 1023, the same as one thread summing in sequence, but with only 10 stages it finishes very quickly given enough threads.
- A. Correct. 2^10 = 1024.
- B. 512 is the number of threads needed in the first stage.
- C. 1023 is the total number of additions.