I want to make a multi-thread C program with a proper variable alignemt in cache, to avoid “cache sloshing”. I get cache-line length from /sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size, so I know how to pad my arrays to occupy full cache lines.
But, how can I be sure that an array is allocated exactly at the beginning of a cache line? Or is it automatic? (If it was automatic, I would not care about the padding…)
To allocate memory with a specific alignment, use
posix_memalign.(I don’t know whether the memory allocator is intelligent enough to allocate on cache-line boundaries automatically, though.)