6.7. Avoid False Sharing

In the presence of multiple threads that share data, there are a number of sharing effects that may affect performance. One such sharing pattern is false sharing. It arises if at least two threads are both using unrelated data placed close enough to end up in the same cache line. False sharing occurs when they repeatedly update their respective data in such a way that the cache line migrates back and forth between the two threads' caches.

Often this can be avoided by giving explicit alignment pragmas to the compiler.

In OpenMP programs False sharing arises when several threads maintain their respective partial result in a vector indexed by the thread rank. Replacing this with thread local variables often helps.

[Tip]Tip
Avoid writing to global data that is accessed from multiple threads.
[Tip]Tip
Align shared global data to cache line boundaries.
[Tip]Tip
Don't store temporary, thread specific data in an array indexed by the thread id or rank.
[Tip]Tip
When parallelizing an algorithm, partition data sets along cache lines, not across cache lines.