Parallel version of loop not faster than serial version
- by Il-Bhima
I'm writing a program in C++ to perform a simulation of particular system. For each timestep, the biggest part of the execution is taking up by a single loop. Fortunately this is embarassingly parallel, so I decided to use Boost Threads to parallelize it (I'm running on a 2 core machine). I would expect at speedup close to 2 times the serial version, since there is no locking. However I am finding that there is no speedup at all.
I implemented the parallel version of the loop as follows:
Wake up the two threads (they are blocked on a barrier).
Each thread then performs the following:
Atomically fetch and increment a global counter.
Retrieve the particle with that index.
Perform the computation on that particle, storing the result in a separate array
Wait on a job finished barrier
The main thread waits on the job finished barrier.
I used this approach since it should provide good load balancing (since each computation may take differing amounts of time). I am really curious as to what could possibly cause this slowdown. I always read that atomic variables are fast, but now I'm starting to wonder whether they have their performance costs.
If anybody has some ideas what to look for or any hints I would really appreciate it. I've been bashing my head on it for a week, and profiling has not revealed much.