The base algorithm will be a for-loop with a sum / subtraction depending on i mod 2 . The variables are aligned to be at the start of a cache line, because my machine has 64 bytes, we use int __attribute__((aligned(64))) val1.
int val1;
int val2;
long int n = 1000000000;
void thread1(){
val1 = 0;
for (int i=0;i<n;i++){
if(i%2 == 0){
val1 += i;
}else{
val1 -= i;
}
}
}
void thread2(){
val2 = 0;
for (int i=0;i<n;i++){
if(i%2 == 0){
val2 += i;
}else{
val2 -= i;
}
}
}
The alternate algorithm simply guarantees false sharing by guaranteeing that both val1 and val2 are right next to each other in memory. We can guarantee that there will be false sharing because the calloc(n, size) library function allocates n number of bytes that are of size sequentially. But, to verify that something like example 1 wouldn't happen the variable will be bounded to the start of a cache line.
int* val = NULL;
long int n = 1000000000;
void thread1(){
for (int i=0;i<n;i++){
if(i%2 == 0){
val[0] += i;
}else{
val[0] -= i;
}
}
}
void thread2(){
for (int i=0;i<n;i++){
if(i%2 == 0){
val[1] += i;
}else{
val[1] -= i;
}
}
}
...
int main(){
val = calloc(2, sizeof(int));
...
}
We need to then begin describing the experimentation procedures.
Each program is run 50 times, and averaged over the 50 runs.
We also want to verify that false sharing is guaranteed, and therefore,
gcc compiler optimizations are disabled.
The false sharing program reports 4.858 seconds average runtime and the non-false sharing program reports 2.613 seconds. This is an overall an 85.9% increase in runtime for the false sharing program.
The source code can be found at: https://github.com/kakaname/false-shr-exp1
Please send any support about what I could have improved to youngwo2@ualberta.ca