Questions tagged [false-sharing]

False sharing is the condition, where in parallel programs, memory cache lines are shared by two or more threads and writes on one cache line would force other cores working on the same line to re-validate their cache. This is a concurrency anti-pattern.

Questions with this tag should be about a suspected or actual false sharing problem.

False sharing is the condition in which in parallel programs, in which memory cache lines which are shared by two or more threads. Writes on one cache line would force other cores working in the same line to re-validate their cache. This is a concurrency anti-pattern.

enter image description here

Note that in the diagram above, Thread 1 writes to A and never B, yet Thread 2 must re-validate its cache to continue computation.

Common ways to alleviate false sharing include storing a thread local result to update to a shared spaced once the computation is completed, and/or spacing contiguous memory blocks that are shared, so they are not on the same cache line.

More information:

Wikipedia

C++ Today Blog Article

93 questions
30
votes
3 answers

False sharing and 128-byte alignment/padding

While doing some research about lock-free/wait-free algorithms, I stumbled upon the false sharing problem. Digging a bit more led me to Folly's source code (Facebook's C++ library) and more specifically to this header file and the definition of the…
polyvertex
  • 749
  • 6
  • 15
29
votes
1 answer

Are cache-line-ping-pong and false sharing the same?

For my bachelor thesis I have to evaluate common problems on multicore systems. In some books I have read about false sharing and in other books about cache-line-ping-pong. The specific problems sound very familiar, so are these the same problems…
mbed_dev
  • 1,450
  • 16
  • 33
27
votes
2 answers

Why does using the same cache-line from multiple threads not cause serious slowdown?

Look at this snippet: #include #include typedef volatile unsigned char Type; // typedef std::atomic_uchar Type; void fn(Type *p) { for (int i=0; i<500000000; i++) { (*p)++; } } int main() { const int N = 4; …
geza
  • 28,403
  • 6
  • 61
  • 135
22
votes
2 answers

Which cache-coherence-protocol does Intel and AMD use?

For my bachelor thesis I have to analyse the effecs of False Sharing on multicore systems. So looking for the different cache-coherence-protocol-types I have come across on Wikipedia that Intel has developed the MESIF cache-coherence-protocol, but…
mbed_dev
  • 1,450
  • 16
  • 33
18
votes
1 answer

What is "false sharing"? How to reproduce / avoid it?

Today I got a different understand with my professor on the Parallel Programming class, about what is "false sharing". What my professor said makes little sense so I pointed it out immediately. She thought "false sharing" will cause a mistake in the…
12
votes
1 answer

Should the cache padding size of x86-64 be 128 bytes?

I found a comment from crossbeam. Starting from Intel's Sandy Bridge, spatial prefetcher is now pulling pairs of 64-byte cache lines at a time, so we have to align to 128 bytes rather than…
QuarticCat
  • 1,314
  • 6
  • 20
12
votes
1 answer

Parallel Framework and avoiding false sharing

Recently, I had answered a question about optimizing a likely parallelizable method for generation every permutation of arbitrary base numbers. I posted an answer similar to the Parallelized, poor implementation code block list, and someone nearly…
jdphenix
  • 15,022
  • 3
  • 41
  • 74
11
votes
2 answers

Avoiding False Sharing in OpenMP with arrays

I have started learning how to use OpenMP as part of a University course. As a lab excercise, we have been given a serial program which we need to parallelize. One of the first things we were made aware of the dangers of False Sharing, especially…
Michael Aquilina
  • 5,352
  • 4
  • 33
  • 38
10
votes
3 answers

Tools to detect False Sharing in a C/C++ application

Are there any tools that detect and report False Sharing for applications written in C or C++?
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
10
votes
4 answers

What is true sharing?

While reading this question I encountered the terms “false sharing” and “true sharing”. I read what false sharing is, but I can’t find anything on true sharing. Although in the mentioned question the term is described as “constructive interference”…
user11313931
10
votes
1 answer

Lock-free check for modification of a global shared state in C using Cache-Line alignment

Edit: ST does not allow to post more than two links for newbies. Sorry for the missing references. I'm trying to reduce locking overhead in a C application where detecting changes on a global state is performance relevant. Even though I've been…
instilled
  • 123
  • 5
8
votes
2 answers

False sharing and pthreads

I have the following task to demonstrate false sharing and wrote a simple program: #include #include #include #include long long int tmsBegin1,tmsEnd1,tmsBegin2,tmsEnd2,tmsBegin3,tmsEnd3; int…
Alexey Matveev
  • 519
  • 1
  • 5
  • 13
7
votes
3 answers

False sharing and stack variables

I have small but frequently used function objects. Each thread gets its own copy. Everything is allocated statically. Copies don't share any global or static data. Do I need to protect this objects from false sharing? Thank you. EDIT: Here is a toy…
user401947
  • 850
  • 10
  • 18
6
votes
2 answers

Why is sizeof std::mutex == 40 when cache line size is often 64 bytes

Following static_assert passes in both gcc and clang trunk. #include int main(){ static_assert(sizeof(std::mutex)==40); } Since x86 CPUs have 64 byte cache line I was expecting mutex sizeof to be 64, so false-sharing can be avoided. Is…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
6
votes
1 answer

False sharing prevention with alignas is broken

I'm not used to post any question on the internet so please tell me if I'm doing something wrong. In short How to correctly prevent false sharing on a 64bits architecture with a CPU cacheline size of 64bytes ? How does the usage of C++ 'alignas'…
1
2 3 4 5 6 7