Sum a random sequence of numbers with multiple threads

Problem What is the best way to sum a large sequence of numbers using multiple threads? The question is asked language-agnostically: reason about correctness, contention, and the speed-up you can realistically expect from p threads.

Input / Output

  • Input: a large array/sequence of numbers and a thread (or core) count p.
  • Output: the total sum of all numbers.

Constraints

  • The sequence is large enough that parallelism could plausibly help.
  • Threads share memory; any shared mutable state must be reasoned about for correctness and contention.

Example

  • Summing 10^8 doubles across p = 8 worker threads should approach an 8× speed-up only if the work per thread dominates coordination overhead.
asked …
LeaderboardSalaryAccount