Cache lines & cache coherence & false sharing

Source: Internet
Author: User
Cache lines

The data in a cache is grouped into blocks
Called cache-lines, which are typically 64 or 128 bytes wide. These are the smallest units of memory that
Can be read from, or written to, main memory. this works well in most programs as data that is close in memory is often needed close in time by a participant thread. however, this is the root of the false sharing problem.

Cache Coherence

When a program writes a value to memory, it goes firstly to the cache
The core that ran the Code. If any other caches hold a copy of that cache line,
Their copy is marked as invalid and cannot be used. The new value is written to main memory, and the other caches
Must re-read it from there if they need it. although this synchronization is implemented in hardware, it still takes time. and, of course, reading from Main Memory takes a few hundred clock cycles by itself.

Modern processors use the MESI Protocol to implement cache coherence.
This basically means eachcache line can be in one of four states:

  • MOdified
  • EXclusive
  • SHared
  • INvalid

When a core modifies any data in a cache line,
It transitions to "modified", and any other caches that hold a copy of the same cache line
Are forced to "invalid ". the other cores must then read the data from main memory next time they need it. that's all I need to say about this here. A detailed explanation is available on Wikipedia [^],
If you're interested.

False sharing

Imagine two different variables are being used by two different threads on two different cores. This appears to be embarrassingly parallel, as the different threads are using isolated data. However,
If the two variables are located in the same cache line and at least one is being written, then there will
Be contention for that cache line. This is false sharing.

It is called false sharing because
Even though the different threads are not sharing data, they are, unintentionally, sharing a cache line.

Source: http://www.codeproject.com/Articles/51553/Concurrency-Hazards-False-Sharing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.