Changes

Jump to: navigation, search

Team False Sharing

796 bytes added, 21:58, 17 December 2017
Eliminating False Sharing
[[File:Speedup.png|right]]
<source lang ="cpp">
#define CACHE_LINE_SIZE 64
template<typename T>
struct cache_line_storage {
[[ align(CACHE_LINE_SIZE) ]] T data;
char pad[ CACHE_LINE_SIZE > sizeof(T)
? CACHE_LINE_SIZE - sizeof(T)
: 1 ];
};
 
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <chrono>
#include <algorithm>
#include <omp.h>
#include "timerPadding.h"#define NUMPAD 7NUM_THREADS 9#define NUM_THREADS 8 DIM 1000using namespace std::chrono; int main(int argc, const char ** argv) { int odds = 0; int* matrix = new int[DIM*DIM]; // Initialize matrix to random Values srand(200) {; struct sfor (int i = 0; i < DIM; i++) { float value for(int j = 0; j < DIM;++j){ int pad matrix[NUMPADi*DIM + j]= rand()%50; } }Array cache_line_storage<int> odds_local[4NUM_THREADS]; Timer stopwatchfor(int i = 0;i<NUM_THREADS;i++){//initilize local odds_local[i].data=0; int numThreadsUsed;} const int SomeBigNumber = 100000000threads_used;
omp_set_num_threads(NUM_THREADS);
double start_time = omp_get_wtime();
#pragma omp parallel { int tid = omp_get_thread_num();#pragma omp 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()data;
}
}
#pragma omp critical
odds += odds_local[tid].data;
}
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>
[[File:Numpad0.png]][[File:Numpad7.png]][[File:Numpad15.png]]
96
edits

Navigation menu