Changes

Jump to: navigation, search

GPU621/Analyzing False Sharing

910 bytes added, 16:02, 24 November 2022
no edit summary
We can see from this result that the runtime is actually unstable and takes a long time.
Although the fields within struct have their own default byte alignment, they are generally only aligned to the word length. In this code example, that is, 8-byte alignment, which does not result in num1 and num2 being in separate cache lines. So we need to use the gcc extension syntax to align the program to 64 bytes.
We can define a macro at the beginning of the program code to use the gcc attribute function.
#define ALIGN __attribute__((aligned(64)))
We also need to redefine the Number struct:
struct Number {
int ALIGN num1;
int ALIGN num2;
};
This allows a member variable with a macro name added to the Number struct to be 64 bytes.If you don't believe me you can try to test Number with sizeof, the result is 128 bytes. But you can see that sizeof num1 and num2 are still 4 bytes respectively, that's because the padded bytes are not counted in the field.
     [[File:testBytes.jpg|400px]]<br />
118
edits

Navigation menu