Open main menu

CDOT Wiki β

Changes

GPU621/Analyzing False Sharing

846 bytes added, 14:55, 24 November 2022
no edit summary
If you think the above example is too limiting, not all cases can be abstracted to such a local variable. So let's let the two variables exist on different cache lines!
 
#include <thread>
#include <iostream>
#include <chrono>
using namespace std;
struct Number {
int num1;
int num2;
};
void increment1(Number& number) {
for (int i = 0; i < 1000000; ++i) {
number.num1++;
}
cout << "number.num1:" << number.num1 << endl;
}
void increment2(Number& number) {
for (int i = 0; i < 1000000; ++i) {
number.num2++;
}
cout << "number.num2:" << number.num2 << endl;
}
 
int main() {
Number number = { 0, 0 };
auto start = chrono::steady_clock::now();
thread thread1(increment1, ref(number));
thread thread2(increment2, ref(number));
thread1.join();
thread2.join();
auto end = chrono::steady_clock::now();
cout << "Consumption: " << chrono::duration_cast<chrono::microseconds>(end - start).count() << "ms" << endl;
}
In this program code, we can easily see that the Number struct has two member variables (num1 and num2). They are for loop in two different threads to execute the increment operator 1000000 times. This time we use Linux with multiple cores to compile (note that here we are not using any optimization)
118
edits