Open main menu

CDOT Wiki β

Changes

DPS921/Group 8

1,091 bytes added, 04:05, 28 November 2018
Solutions
= Solutions =
We will now explore two typical ways to deal with false sharing in an OMP environment.
____________________________________________________________________________________________________________________________________________________________
 
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <chrono>
#include <algorithm>
#include <omp.h>
#include "pch.h"
using namespace std::chrono;
 
struct MyStruct
{
int value;
};
 
int main(int argc, char** argv)
{
MyStruct testArr[4];
 
int value[4] = {};
 
omp_set_num_threads(4);
double start_time = omp_get_wtime();
#pragma omp parallel for
for (int i = 0; i < 4; i++) {
 
testArr[i].value = 0;
 
for (int j = 0; j < 10000; j++) {
testArr[i].value = testArr[i].value + 1;
}
 
}
 
std::cout << "testArr value 1: " << testArr[0].value << std::endl;
std::cout << "testArr value 2: " << testArr[1].value << std::endl;
std::cout << "testArr value 3: " << testArr[2].value << std::endl;
std::cout << "testArr value 4: " << testArr[3].value << std::endl;
 
 
double time = omp_get_wtime() - start_time;
std::cout << "Execution Time: " << time << std::endl;
 
return 0;
}
 
 
[[File:serial.png|1000px]]
== '''Padding''' ==
42
edits