96
edits
Changes
→Identifying False Sharing
<source lang="cpp">
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <chrono>
#include <algorithm>
#include <omp.h>
#define NUM_THREADS 8
#define DIM 1000using namespace std::chrono; //report time int main(int argc, const char ** argv) { int* matrix = new int[DIM*DIM]; int odds = 0; // Initialize matrix to random Values srand(200); for (int i = 0; i < DIM; i++) { struct s for(int j = 0; j < DIM; ++j){ float value matrix[i*DIM + j] = rand()%50; } }Array int* odds_local = new int[4NUM_THREADS];//odd numbers in matrix local to thread for(int i = 0; i < NUM_THREADS;i++){ odds_local[i]=0; } int numThreadsUsedthreads_used; const int SomeBigNumber = 100000000tid;
omp_set_num_threads(NUM_THREADS);
}
}
#pragma omp critical
odds += odds_local[tid];
}
double time = omp_get_wtime() - start_time;
std::cout<<"Execution Time: "<<time<<std::endl; std::cout<<"Threads Used: "<<numThreadsUsedthreads_used<<std::endl; std::cout<<"Odds: "<<odds<<std::endl;
return 0;
}
</source>
As you can see the execution time increase with the number of threads. These results are not what you would expect but there are 2 reasons that may have caused this. The first is that the overhead for creating and maintaining the threads is overwhelming larger than the contents of the for loop. The second is False sharing.