GPU Programming
Building a histogram of pixel values on a GPU can be slow if every thread does atomicAdd directly on 256 bins in global memory. What is a good improvement?
- ADrop atomicAdd and use plain additions on global memory
- BMerge all bins into one
- CBuild a partial histogram per block in shared memory, then add it into the global bins at the end
Show answer and explanation
Answer: C. Build a partial histogram per block in shared memory, then add it into the global bins at the end
When many threads atomicAdd to the same bin at once, the updates are serialized, and slow global memory makes the waits long. If each block keeps its own histogram in shared memory, counts there and adds it to the global bins once at the end, contention in slow memory falls to the number of blocks times the number of bins. Atomics in shared memory are much faster.
- A. A data race would lose counts.
- B. The histogram would become meaningless.
- C. Correct. Contention on slow global memory drops sharply.