Aligning the cache line and knowing the size of the cache line

To prevent false sharing, I want each element of the array to be bound to a cache line. So first I need to know the size of the cache line, so I assign so many bytes to each element. Secondly, I want the beginning of the array to be aligned with the cache line.

I use Linux and the x86 octa-core platform. First, how to find the size of the cache line. Secondly, how do I align the cache line in C. I use the gcc compiler.

Thus, the structure will follow, for example, provided that the size of the cache line is 64.

element[0] occupies bytes 0-63 element[1] occupies bytes 64-127 element[2] occupies bytes 128-191 

etc., assuming 0-63 is aligned with the cache line.

+42
c linux caching memory-alignment computer-architecture
02 Sep 2018-11-11T00:
source share
6 answers

To find out the dimensions, you need to look at it using the documentation for the processor, afaik there is no software way to do this. However, on the plus side, most cache lines have a standard size based on international standards. However, x86 cache caches use 64 bytes to prevent false sharing, you need to follow the recommendations of the processor you are targeting (Intel has some special notes on its netburst based processors), usually you need align up to 64 bytes for this (Intel says you should also avoid crossing 16 byte boundaries).

For this, in C or C ++, you need to use the standard aligned_alloc function or one of the compiler-specific qualifiers, such as __attribute__((align(64))) or __declspec(align(64)) . To lay between members in a structure to split them into different cache lines, you need to insert an element large enough to align it to the next 64-byte boundery

+24
Sep 02 '11 at 9:50 a.m.
source share

I use Linux and the x86 octa-core platform. First, how to find the size of the cache line.

 $ getconf LEVEL1_DCACHE_LINESIZE 64 

Pass the value as a macro definition to the compiler.

 $ gcc -DLEVEL1_DCACHE_LINESIZE=`getconf LEVEL1_DCACHE_LINESIZE` ... 

At run time, sysconf(_SC_LEVEL1_DCACHE_LINESIZE) can be used to get the size of the L1 cache.

+54
Sep 02 2018-11-11T00:
source share

Another simple way is just cat / proc / cpuinfo:

cat / proc / cpuinfo | grep cache_alignment

+8
Jun 02 '12 at 7:17
source share

posix_memalign or valloc can be used to align the allocated memory in the cache line.

+7
02 Sep 2018-11-11T00:
source share

There is no completely portable way to get cashline size. But if you are on x86 / 64, you can call the cpuid command to get everything you need to know about the cache, including size, size of the cell, number of levels, etc.

http://softpixel.com/~cwright/programming/simd/cpuid.php

(Scroll a little page, a page about SIMD, but there is a section in it that gets a skittle.)

Regarding the alignment of your data structures, there is also no completely portable way to do this. GCC and VS10 have different ways to indicate structure alignment. One way to β€œcrack” it is to stick your structure with unused variables until it aligns with the desired alignment.

To align your mallocs (), all mainstream compilers have also aligned the malloc functions for this purpose.

+6
Sep 02 2018-11-11T00:
source share

If someone is interested in learning how to do this easily in C ++, I created a library with a CacheAligned<T> class that handles determining the size of the cache line, as well as the alignment for your T object, which is referenced by the .Ref() call of the object CacheAligned<T> . You can also use Aligned<typename T, size_t Alignment> if you know the size of the cache line in advance, or just want to stick to a very general value of 64 (bytes).

https://github.com/NickStrupat/Aligned

+1
Jan 23 '15 at 6:22
source share



All Articles