Aligning an array of objects with __attribute__aligned () or alignas ()?

Guys with a quick question ... Are these code spinets the same?

struct sse_t { float sse_data[4]; }; // the array "cacheline" will be aligned to 64-byte boundary struct sse_t alignas(64) cacheline[1000000]; 

Or

 // every object of type sse_t will be aligned to 64-byte boundary struct sse_t { float sse_data[4]; } __attribute((aligned(64))); struct sse_t cacheline[1000000]; 
+4
source share
2 answers

Are these code spinets the same?

Not really. Your two examples are actually very different.

In the first example, you get an array of sse_t objects. The sse_t object sse_t guaranteed only 4-byte alignment. But since the entire array is aligned to 64 bytes, each sse_t object will be correctly aligned to access the SSE.

In your second example, you force each sse_t object sse_t align with 64 bytes. But each sse_t object has only 16 bytes. Thus, the array will be 4 times larger. (You will have 48 bytes of padding at the end of each sse_t object).


 struct objA { float sse_data[4]; }; struct objB { float sse_data[4]; } __attribute((aligned(64))); int main(){ cout << sizeof(objA) << endl; cout << sizeof(objB) << endl; } 

Output:

 16 64 

I am sure that the second case is not what you want.

+5
source

But why do you want to align to 64 bytes? http://ideone.com/JNEIBR

 #include <iostream> using namespace std; struct sse_t1 { float sse_data[4]; }; // the array "cacheline" will be aligned to 64-byte boundary struct sse_t1 alignas(64) cacheline1[1000000]; // every object of type sse_t will be aligned to 64-byte boundary struct sse_t2 { float sse_data[4]; } __attribute((aligned(64))); struct sse_t2 cacheline2[1000000]; int main() { cout << "sizeof(sse_t1) = " << sizeof(sse_t1) << endl; cout << "sizeof(sse_t2) = " << sizeof(sse_t2) << endl; cout << "array cacheline1 " << (((size_t)(cacheline1) % 64 == 0)?"aligned to 64":"not aligned to 64") << endl; cout << "array cacheline2 " << (((size_t)(cacheline2) % 64 == 0)?"aligned to 64":"not aligned to 64") << endl; cout << "cacheline1[0] - cacheline1[1] = " << (size_t)&(cacheline1[1]) - (size_t)&(cacheline1[0]) << endl; cout << "cacheline2[0] - cacheline2[1] = " << (size_t)&(cacheline2[1]) - (size_t)&(cacheline2[0]) << endl; return 0; } 

Output:

 sizeof(sse_t1) = 16 sizeof(sse_t2) = 64 array cacheline1 aligned to 64 array cacheline2 aligned to 64 cacheline1[0] - cacheline1[1] = 16 cacheline2[0] - cacheline2[1] = 64 
0
source

All Articles