From ea9c372d5c6980e5e891a957fdd0b9960adc30d0 Mon Sep 17 00:00:00 2001 From: Andreu Glasmann Date: Wed, 27 Apr 2016 15:12:51 -0400 Subject: [PATCH] Parallelized red black algorithm (somewhat final) --- redblack.cpp | 201 +++++++++++++++++++++++++------------------ redblack.h | 1 + redblack_example.cpp | 36 ++++++-- 3 files changed, 147 insertions(+), 91 deletions(-) diff --git a/redblack.cpp b/redblack.cpp index 9eecf5b..0b56102 100644 --- a/redblack.cpp +++ b/redblack.cpp @@ -13,7 +13,7 @@ using namespace std; void redBlackSerial(double * T, double * b, int n, int m, double alpha, int itermax) { int i, j; - int index, index_left, index_right, index_up, index_down; + int index, pos_l, pos_r, pos_u, pos_d; int iter = 0; while (iter < itermax) { @@ -21,29 +21,29 @@ void redBlackSerial(double * T, double * b, for (i=1; i #include #include +#include +#include using namespace std; void initArrays(double * T, double * b, int n, int m) { @@ -17,19 +19,30 @@ void initArrays(double * T, double * b, int n, int m) { } int main() { - const int n = 100; - const int m = 100; + const int n = 500; + const int m = 500; double alpha = 0.9; double * T = new double[n*m]; double * b = new double[n*m]; initArrays(T, b, n, m); + /* see if threads are utilized properly */ + int tid, nthreads; + #pragma omp parallel shared(nthreads) private(tid) + { + tid = omp_get_thread_num(); + if (tid == 0) { nthreads = omp_get_num_threads(); } + #pragma omp barrier + printf("hello from thread %d of %d!\n", tid, nthreads); + } + double cc = 99.0; int iter = 0; cout << "Starting without parallel!\n"; - clock_t start; + double t1, t2; double duration; - start = clock(); + + t1 = omp_get_wtime(); while (cc > 1e-5) { redBlackSerial(T, b, n, m, alpha, 1); cc = computeResidual(T, b, n, m, alpha); @@ -37,8 +50,12 @@ int main() { cout << "Iteration: " << iter << ", Res: " << cc << "\n"; } iter ++; + + //remove this + cc=1e-6; } - duration = (clock() - start) / (double) CLOCKS_PER_SEC; + t2 = omp_get_wtime(); + duration = (t2 - t1); /// (double) CLOCKS_PER_SEC; cout << "Non-parallel: " << duration << " seconds.\n"; FILE * output = fopen("T_serial.txt", "w"); @@ -48,22 +65,23 @@ int main() { } } fclose(output); - initArrays(T, b, n, m); cc = 99.0; iter = 0; cout << "Starting with parallel!\n"; - start = clock(); + t1 = omp_get_wtime(); while (cc > 1e-5) { - redBlackParallel(T, b, n, m, alpha, 1); + //redBlackParallel(T, b, n, m, alpha, 1); + gaussSeidelParallel(T, b, n, m, alpha, 1); cc = computeResidual(T, b, n, m, alpha); if (iter%1000==0) { cout << "Iteration: " << iter << ", Res: " << cc << "\n"; } iter ++; } - duration = (clock() - start) / (double) CLOCKS_PER_SEC; + t2 = omp_get_wtime(); + duration = (t2 - t1);// / (double) CLOCKS_PER_SEC; cout << "Parallel: " << duration << " seconds.\n"; output = fopen("T_parallel.txt", "w");