Entry in Parallel Programming with .NET blog "Most Common Performance Issues in Parallel Programs" and recent article in MSDN ".NET Matters: False Sharing" have attracted my attention. Basically they both suggest to eliminate false sharing. Wrong! Wrong! Wrong! It's not the whole truth, so to say. So if authors were under oath in the virtual IT court, they would have to be arrested. Fortunately they weren't under oath :)
The first thing one has to say in that context is:
Second thing one has to say in that context is:
Points 1 and 2 can be aggregated as:
1+2. Do pay attention to data layout. This was important in the 60's. This is even more important in the multicore era.
Only after that one can also add:
3. Sometimes sharing can show up when you are not expecting it, i.e false sharing. This is important to eliminate false sharing too... etceteras... [insert here contents of False Sharing article]
If one says only point 3, well, it's basically senseless. And sometimes it can even hurt.
Let's consider simple example:
What does naive programmer think about it? Hmmm... Let's see... I use "fast" non-blocking interlocked operations. Good!... Hmmm... False sharing. Let's see... Hmmm... Here is no false sharing. Good! So my program fully conforms to recommendations of experts.
Rubbish! It's a dead-slow, completely non-scalable program.
Now let's apply consistent rules to the example. First of all we have to do something like this:
It's good distributed design. When we need aggregate number of operations we just sum up all thread local counters.
Only at this point we can remember about false-sharing and put the final touches to the code:
Ok, this distributed version is also fast and scalable. It has linear scalability and can be faster up to 100x on modern multi-core hardware as compared with original version.
So, point 1+2 is a kind of general rule, while point 3 is just a refinement to them.
Why people don't say the whole truth? I don't know. I don't beleive that authors don't aware of the problem. Maybe they think that it's obvious. The practice shows that it's not...
Now you may move to the following applied sections: