Created
December 14, 2018 21:17
-
-
Save glzjin/66fa3e98aaf6a2233e86c5cf728eb35c to your computer and use it in GitHub Desktop.
Openmp3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "omp.h" | |
#include <sys/time.h> | |
#include <chrono> | |
#include <cmath> | |
using namespace std; | |
int mask[3]; | |
void setMask(int mask1, int mask2, int mask3) { | |
mask[0] = mask1; | |
mask[1] = mask2; | |
mask[2] = mask3; | |
} | |
class EquationsLinearSystem | |
{ | |
private: | |
int Dimension; | |
double* A; | |
double* B; | |
public: | |
EquationsLinearSystem(int N) | |
{ | |
srand(time(0)); | |
Dimension = N; | |
A = new double[N*N]; | |
B = new double[N]; | |
this->RandomaticalInitialization(); | |
this->AutomaticalInitialization(); | |
} | |
public: | |
~EquationsLinearSystem(void) | |
{ | |
delete A; | |
delete B; | |
} | |
private: | |
void PrintAB (void) | |
{ | |
printf ("Matrix A: \n"); | |
for (int I=0;I<Dimension;I++){ | |
printf ("["); | |
for (int J=0;J<Dimension-1;J++){ | |
printf ("%f, ",A[I*Dimension + J]); | |
} | |
printf("%f]\n",A[I*Dimension + Dimension-1]); | |
} | |
printf ("Vector B: \n"); | |
for (int I=0;I<Dimension;I++) printf("[%f]\n ",B[I]); | |
} | |
void AutomaticalInitialization(void) | |
{ | |
for (int I=0;I<Dimension;I++){ | |
for (int J=0;J<Dimension;J++){ | |
if (I==J) | |
A[I*Dimension + J] = 4.; | |
else | |
A[I*Dimension + J] = 0.; | |
} | |
B[I]=2.; | |
} | |
} | |
void RandomaticalInitialization(void) | |
{ | |
for (int I=0; I<Dimension;I++) | |
{ double Sum = 0; | |
for (int J=0; J <Dimension;J++) | |
{ | |
A[I*Dimension + J] = rand()%18 - 9; | |
Sum += this->Absolute(A[I*Dimension + J]); | |
} | |
int sign; | |
if (rand() > RAND_MAX/2) | |
sign = 1; | |
else | |
sign = -1; | |
A[I*Dimension + I] = Sum * (rand()/RAND_MAX + 1) * sign; | |
B[I] = rand()%18 - 9; | |
} | |
} | |
private: | |
void CopyArray(double* Source, double* newArray) | |
{ | |
// code needed | |
//mask 1 | |
if(mask[0] == 1) { | |
#pragma omp parallel for | |
for (int i = 0; i < sizeof(Source) / sizeof(Source[0]); i++) newArray[i] = Source[i]; | |
} else { | |
for (int i = 0; i < sizeof(Source) / sizeof(Source[0]); i++) newArray[i] = Source[i]; | |
} | |
} | |
private: | |
double Absolute(double a) | |
{ | |
if (a > 0) | |
return a; | |
else | |
return -a; | |
} | |
private: | |
int Absolute(int a) | |
{ | |
if (a > 0) | |
return a; | |
else | |
return -a; | |
} | |
private: | |
double MaxDifference(double* X1, double* X2) | |
{ | |
// code needed | |
double result = 0.0; | |
if(mask[2] == 1) { | |
#pragma omp parallel for shared(result) | |
for (int i = 0; i < this->Dimension; i++) { | |
double currentValue = this->Absolute(X1[i] - X2[i]); | |
if (result < currentValue) { | |
result = currentValue; | |
} | |
} | |
} else { | |
for (int i = 0; i < this->Dimension; i++) { | |
double currentValue = this->Absolute(X1[i] - X2[i]); | |
if (result < currentValue) { | |
result = currentValue; | |
} | |
} | |
} | |
return result; | |
} | |
public: | |
void Solve(void) | |
{ | |
// code needed | |
double* x = new double[this->Dimension]; | |
double* x2 = new double[this->Dimension]; | |
if(mask[0] == 1) { | |
#pragma omp parallel for | |
for (int i = 0; i < this->Dimension; i++) { | |
x2[i] = this->B[i] / this->A[i * this->Dimension + i]; | |
} | |
} else { | |
for (int i = 0; i < this->Dimension; i++) { | |
x2[i] = this->B[i] / this->A[i * this->Dimension + i]; | |
} | |
} | |
//this->PrintAB(); | |
double maxDifference; | |
const double targetDifference = 0.0001; | |
int IterationCounter = 1; | |
do { | |
//x0 = x | |
if(IterationCounter > 1) { | |
this->CopyArray(x, x2); | |
} | |
if(mask[1] == 1) { | |
#pragma omp parallel for | |
for (int i = 0; i < this->Dimension; i++) { | |
x[i] = this->B[i]; | |
for (int j = 0; j < this->Dimension; j++) { | |
if (i != j) { | |
x[i] -= this->A[i * this->Dimension + j] * x2[j]; | |
} | |
} | |
} | |
} else { | |
for (int i = 0; i < this->Dimension; i++) { | |
x[i] = this->B[i]; | |
for (int j = 0; j < this->Dimension; j++) { | |
if (i != j) { | |
x[i] -= this->A[i * this->Dimension + j] * x2[j]; | |
} | |
} | |
} | |
} | |
maxDifference = MaxDifference(x, x2); | |
IterationCounter++; | |
//compare to E | |
} while(maxDifference < targetDifference); | |
// for(int i = 0; i < this->Dimension; i++) { | |
// cout << x2[i] << " "; | |
// } | |
// cout << endl; | |
// | |
// cout << IterationCounter << endl; | |
} | |
}; | |
int main(int argc, char* argv[]) | |
{ | |
int dimension = atoi(argv[1]); | |
setMask(atoi(argv[2]), atoi(argv[3]), atoi(argv[4])); | |
EquationsLinearSystem* linearSystem = new EquationsLinearSystem(dimension); | |
auto t=chrono::steady_clock::now(); | |
linearSystem->Solve(); | |
cout << "Time = " << chrono::duration <long long, nano>(chrono::steady_clock::now() - t).count() << endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import subprocess | |
import xlsxwriter | |
times = 21 | |
def test(element_number, mask1, mask2, mask3): | |
sub = subprocess.Popen("./openmp2 " + str(element_number) + " " + str(mask1) + " " + str(mask2) + " " + str(mask3), shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, | |
stderr=subprocess.STDOUT) | |
s = 0 | |
while True: | |
next_line = sub.stdout.readline().strip().decode() | |
if next_line.find("Time") != -1: | |
r = re.findall('Time = (\d*)', next_line) | |
s = int(r[0]) | |
continue | |
if next_line == "": | |
break | |
return s | |
result_set = {} | |
for i in range(0, 4): | |
for j in range(0, 10): | |
for k in range(0, 10): | |
result_list = [] | |
element_number = 10000 * i + 1000 * j + 100 * k | |
if element_number <= 0: | |
continue | |
print("Testing:" + str(element_number)) | |
for m in range(1, times): | |
results = test(element_number, 0, 0, 0) | |
# print("0,0,0: " + str(results)) | |
result_list.append(results) | |
results = test(element_number, 0, 1, 0) | |
# print("0,1,0: " + str(results)) | |
result_list.append(results) | |
results = test(element_number, 1, 0, 1) | |
# print("1,0,1: " + str(results)) | |
result_list.append(results) | |
results = test(element_number, 1, 1, 1) | |
# print("1,1,1: " + str(results)) | |
result_list.append(results) | |
result_set[element_number] = result_list | |
workbook = xlsxwriter.Workbook('openmp2.xlsx') | |
worksheet = workbook.add_worksheet() | |
print(result_set) | |
row = 1 | |
col = 0 | |
for number, result in result_set.items(): | |
worksheet.write(row, col, number) | |
for i in range(1, times): | |
worksheet.write(row, i, result[(i - 1) * 4]) | |
for i in range(1, times): | |
worksheet.write(row, times + i - 1, result[1 + (i - 1) * 4]) | |
for i in range(1, times): | |
worksheet.write(row, times * 2 + i - 2, result[2 + (i - 1) * 4]) | |
for i in range(1, times): | |
worksheet.write(row, times * 3 + i - 3, result[3 + (i - 1) * 4]) | |
row += 1 | |
worksheet.write(0, 0, 'Number') | |
for i in range(1, times): | |
worksheet.write(0, i, 'Mask(0, 0, 0) - %i(ns)' % i) | |
for i in range(1, times): | |
worksheet.write(0, times + i - 1, 'Mask(0, 1, 0) - %i(ns)' % i) | |
for i in range(1, times): | |
worksheet.write(0, times * 2 + i - 2, 'Mask(1, 0, 1) - %i(ns)' % i) | |
for i in range(1, times): | |
worksheet.write(0, times * 3 + i - 3, 'Mask(1, 1, 1) - %i(ns)' % i) | |
workbook.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment