Aligning to cache line and knowing the cache line size

前端 未结 7 1695
盖世英雄少女心
盖世英雄少女心 2020-12-12 10:07

To prevent false sharing, I want to align each element of an array to a cache line. So first I need to know the size of a cache line, so I assign each element that amount of

相关标签:
7条回答
  • 2020-12-12 10:57

    There's no completely portable way to get the cacheline size. But if you're on x86/64, you can call the cpuid instruction to get everything you need to know about the cache - including size, cacheline size, how many levels, etc...

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

    (scroll down a little bit, the page is about SIMD, but it has a section getting the cacheline.)

    As for aligning your data structures, there's also no completely portable way to do it. GCC and VS10 have different ways to specify alignment of a struct. One way to "hack" it is to pad your struct with unused variables until it matches the alignment you want.

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

    0 讨论(0)
提交回复
热议问题