Open main menu

CDOT Wiki β

Changes

Team False Sharing

813 bytes added, 17:18, 17 December 2017
Thread Local Variables
===Thread Local Variables===
<source>
#include <iostream>
#include <omp.h>
#include "timer.h"
#define NUM_THREADS 8
int main(int argc, const char * argv[]) {
struct s{
float value;
}Array[4];
Timer stopwatch;
int numThreadsUsed;
const int SomeBigNumber = 100000000;
omp_set_num_threads(NUM_THREADS);
double start_time = omp_get_wtime();
#pragma omp parallel for
for(int i = 0; i < 4;i++){
if(i ==0){numThreadsUsed = omp_get_num_threads();}
float tmp = Array[i].value;
for(int j = 0;j < SomeBigNumber;j++){
tmp = tmp + (float)rand();
}
#pragma omp critical
Array[i].value = tmp;
}
double time = omp_get_wtime() - start_time;
std::cout<<time<<std::endl;
std::cout<<"Threads Used: "<<numThreadsUsed<<std::endl;
return 0;
}
</source>
96
edits