Open main menu

CDOT Wiki β

Changes

Team False Sharing

572 bytes added, 19:36, 17 December 2017
Identifying False Sharing
<source lang="cpp">
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <chrono>
#include <algorithm>
#include <omp.h>
#include "timer.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);
double start_time = omp_get_wtime();#pragma omp parallel{ tid = omp_get_thread_num();#pragma omp parallel for for(int i = 0; i < 4DIM;++i){ for(int j = 0; j < DIM; ++j){ if(i ==0 && j==0){numThreadsUsed threads_used = omp_get_num_threads();} for if(int matrix[i*DIM + j ] % 2 != 0;j < SomeBigNumber;j) ++){ Array[i].value = Arrayodds_local[itid].value + (float)rand();
}
}
#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>
</source>
[[File:ExecVsThreadsFalse.png|center]]
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.
96
edits