Multicore & Synchronization

When several threads sum all elements of an array, which approach performs well?

  1. AEvery thread adds each element to one shared variable, taking a lock each time
  2. BEvery thread adds each element to one shared variable without synchronization
  3. CEach thread sums its share in a local variable, and the partial sums are added once each at the end
Show answer and explanation

Answer: C. Each thread sums its share in a local variable, and the partial sums are added once each at the end

Updating a shared variable per element is very slow because of lock or atomic waits and cache lines bouncing between cores. If each thread computes a partial sum in a register or local variable and the total is formed with just one addition per thread at the end, shared writes become negligible. This approach is called a reduction and is built into parallel frameworks such as OpenMP.

  • A. Lock waits and communication nearly eliminate the benefit.
  • B. A data race makes the total wrong.
  • C. Correct. This is called a reduction.