I want to improve the data locality of the following structure:
#include <stdio.h>
struct double16{
double v0;
double v1;
double v2;
double v3;
double v4;
double v5;
double v6;
double v7;
double v8;
double v9;
double v10;
double v11;
double v12;
double v13;
double v14;
double v15;
};/* expected size is 128 bytes*/
// test - padding within a single struct,
int test_struct_padding() {
printf("%s: %ld\n", "double16", sizeof(struct double16));
return 0;
}
// test - address of struct,
int test_struct_address() {
printf("%s: %ld\n", "double16", sizeof(struct double16));
struct double16 dd;
struct double16 ee;
printf("address of %s: %p\n", "dd", &dd);
// dd is only 128 bytes itself, but distance to next struct is 128 bytes(on 64 bit system)
printf("space between %s and %s: %ld\n", "dd", "ff", (long)(&ee) - (long)(&dd));
return 0;
}
int main(int argc, char * argv[]) {
test_struct_padding();
test_struct_address();
return 0;
}
Output
double16: 128
double16: 128
address of dd: 0x7ff7bb3c8540
space between dd and ff: 128
In what way can I use packing and alignment to reduce as much as possible the number of cache hits? I do know that the size of the cache line on my computer is 64 which is half the size of the structure (you can get this information on macos
using the command sysctl hw.cachelinesize
). If someone knows how to measure experimentally the cache hits, a detailed explanation will be highly appreciated.
CodePudding user response:
Unknown if this helps cache hits, but here is how to control alignment and some packing.
You can increase the alignment strictness of an object:
#include <stdalign.h>
// struct double16 dd;
alignas(128) struct double16 dd;
Code can make reasonably certain dd
and ee
are adjacent in memory.
//struct double16 dd;
//struct double16 ee;
alignas(128) struct {
struct double16 dd;
struct double16 ee;
} u;
To make certain, use an array:
alignas(128) struct double16 ddee[2];