From 9bb4d130c5155238426d94d328e3fb3a1c3d9aa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20L=C3=BCbke?= Date: Wed, 15 Nov 2023 11:41:31 +0100 Subject: [PATCH] put parallel for clauses in parallel construct --- include/tug/Core/BTCS.hpp | 59 +++++++++--------- include/tug/Core/FTCS.hpp | 122 +++++++++++++++++++++----------------- 2 files changed, 99 insertions(+), 82 deletions(-) diff --git a/include/tug/Core/BTCS.hpp b/include/tug/Core/BTCS.hpp index b844ba4..26d20da 100644 --- a/include/tug/Core/BTCS.hpp +++ b/include/tug/Core/BTCS.hpp @@ -393,37 +393,42 @@ static void BTCS_2D(Grid &grid, Boundary &bc, T timestep, Eigen::MatrixX concentrations = grid.getConcentrations(); -#pragma omp parallel for num_threads(numThreads) private(A, b, row_t1) - for (int i = 0; i < rowMax; i++) { +#pragma omp parallel num_threads(numThreads) + { +#pragma omp parallel for private(A, b, row_t1) + for (int i = 0; i < rowMax; i++) { - A = createCoeffMatrix(alphaX, bcLeft, bcRight, colMax, i, sx); - b = createSolutionVector(concentrations, alphaX, alphaY, bcLeft, bcRight, - bcTop, bcBottom, colMax, i, sx, sy); + A = createCoeffMatrix(alphaX, bcLeft, bcRight, colMax, i, sx); + b = createSolutionVector(concentrations, alphaX, alphaY, bcLeft, bcRight, + bcTop, bcBottom, colMax, i, sx, sy); - row_t1 = solverFunc(A, b); + row_t1 = solverFunc(A, b); - concentrations_t1.row(i) = row_t1; + concentrations_t1.row(i) = row_t1; + } + +#pragma omp single + { + concentrations_t1.transposeInPlace(); + concentrations.transposeInPlace(); + alphaX.transposeInPlace(); + alphaY.transposeInPlace(); + } + +#pragma omp parallel for private(A, b, row_t1) + for (int i = 0; i < colMax; i++) { + // swap alphas, boundary conditions and sx/sy for column-wise calculation + A = createCoeffMatrix(alphaY, bcTop, bcBottom, rowMax, i, sy); + b = createSolutionVector(concentrations_t1, alphaY, alphaX, bcTop, + bcBottom, bcLeft, bcRight, rowMax, i, sy, sx); + + row_t1 = solverFunc(A, b); + + concentrations.row(i) = row_t1; + } +#pragma omp single + { concentrations.transposeInPlace(); } } - - concentrations_t1.transposeInPlace(); - concentrations.transposeInPlace(); - alphaX.transposeInPlace(); - alphaY.transposeInPlace(); - -#pragma omp parallel for num_threads(numThreads) private(A, b, row_t1) - for (int i = 0; i < colMax; i++) { - // swap alphas, boundary conditions and sx/sy for column-wise calculation - A = createCoeffMatrix(alphaY, bcTop, bcBottom, rowMax, i, sy); - b = createSolutionVector(concentrations_t1, alphaY, alphaX, bcTop, bcBottom, - bcLeft, bcRight, rowMax, i, sy, sx); - - row_t1 = solverFunc(A, b); - - concentrations.row(i) = row_t1; - } - - concentrations.transposeInPlace(); - grid.setConcentrations(concentrations); } diff --git a/include/tug/Core/FTCS.hpp b/include/tug/Core/FTCS.hpp index 7f12780..468765c 100644 --- a/include/tug/Core/FTCS.hpp +++ b/include/tug/Core/FTCS.hpp @@ -276,66 +276,78 @@ static void FTCS_2D(Grid &grid, Boundary &bc, T timestep, // inner cells // these are independent of the boundary condition type // omp_set_num_threads(10); -#pragma omp parallel for num_threads(numThreads) - for (int row = 1; row < rowMax - 1; row++) { - for (int col = 1; col < colMax - 1; col++) { - concentrations_t1(row, col) = grid.getConcentrations()(row, col) + - timestep / (deltaRow * deltaRow) * - (calcVerticalChange(grid, row, col)) + - timestep / (deltaCol * deltaCol) * - (calcHorizontalChange(grid, row, col)); + int row; + int col; +#pragma omp parallel num_threads(numThreads) + { +#pragma omp parallel for + for (int row = 1; row < rowMax - 1; row++) { + for (int col = 1; col < colMax - 1; col++) { + concentrations_t1(row, col) = + grid.getConcentrations()(row, col) + + timestep / (deltaRow * deltaRow) * + (calcVerticalChange(grid, row, col)) + + timestep / (deltaCol * deltaCol) * + (calcHorizontalChange(grid, row, col)); + } } - } - // boundary conditions - // left without corners / looping over rows - // hold column constant at index 0 - int col = 0; -#pragma omp parallel for num_threads(numThreads) - for (int row = 1; row < rowMax - 1; row++) { - concentrations_t1(row, col) = - grid.getConcentrations()(row, col) + - timestep / (deltaCol * deltaCol) * - (calcHorizontalChangeLeftBoundary(grid, bc, row, col)) + - timestep / (deltaRow * deltaRow) * (calcVerticalChange(grid, row, col)); - } +// boundary conditions +// left without corners / looping over rows +// hold column constant at index 0 +#pragma omp single + col = 0; +#pragma omp parallel for + for (int row = 1; row < rowMax - 1; row++) { + concentrations_t1(row, col) = + grid.getConcentrations()(row, col) + + timestep / (deltaCol * deltaCol) * + (calcHorizontalChangeLeftBoundary(grid, bc, row, col)) + + timestep / (deltaRow * deltaRow) * + (calcVerticalChange(grid, row, col)); + } - // right without corners / looping over rows - // hold column constant at max index - col = colMax - 1; -#pragma omp parallel for num_threads(numThreads) - for (int row = 1; row < rowMax - 1; row++) { - concentrations_t1(row, col) = - grid.getConcentrations()(row, col) + - timestep / (deltaCol * deltaCol) * - (calcHorizontalChangeRightBoundary(grid, bc, row, col)) + - timestep / (deltaRow * deltaRow) * (calcVerticalChange(grid, row, col)); - } +// right without corners / looping over rows +// hold column constant at max index +#pragma omp single + col = colMax - 1; +#pragma omp parallel for + for (int row = 1; row < rowMax - 1; row++) { + concentrations_t1(row, col) = + grid.getConcentrations()(row, col) + + timestep / (deltaCol * deltaCol) * + (calcHorizontalChangeRightBoundary(grid, bc, row, col)) + + timestep / (deltaRow * deltaRow) * + (calcVerticalChange(grid, row, col)); + } - // top without corners / looping over columns - // hold row constant at index 0 - int row = 0; -#pragma omp parallel for num_threads(numThreads) - for (int col = 1; col < colMax - 1; col++) { - concentrations_t1(row, col) = - grid.getConcentrations()(row, col) + - timestep / (deltaRow * deltaRow) * - (calcVerticalChangeTopBoundary(grid, bc, row, col)) + - timestep / (deltaCol * deltaCol) * - (calcHorizontalChange(grid, row, col)); - } +// top without corners / looping over columns +// hold row constant at index 0 +#pragma omp single + row = 0; +#pragma omp parallel for + for (int col = 1; col < colMax - 1; col++) { + concentrations_t1(row, col) = + grid.getConcentrations()(row, col) + + timestep / (deltaRow * deltaRow) * + (calcVerticalChangeTopBoundary(grid, bc, row, col)) + + timestep / (deltaCol * deltaCol) * + (calcHorizontalChange(grid, row, col)); + } - // bottom without corners / looping over columns - // hold row constant at max index - row = rowMax - 1; -#pragma omp parallel for num_threads(numThreads) - for (int col = 1; col < colMax - 1; col++) { - concentrations_t1(row, col) = - grid.getConcentrations()(row, col) + - timestep / (deltaRow * deltaRow) * - (calcVerticalChangeBottomBoundary(grid, bc, row, col)) + - timestep / (deltaCol * deltaCol) * - (calcHorizontalChange(grid, row, col)); +// bottom without corners / looping over columns +// hold row constant at max index +#pragma omp single + row = rowMax - 1; +#pragma omp parallel for + for (int col = 1; col < colMax - 1; col++) { + concentrations_t1(row, col) = + grid.getConcentrations()(row, col) + + timestep / (deltaRow * deltaRow) * + (calcVerticalChangeBottomBoundary(grid, bc, row, col)) + + timestep / (deltaCol * deltaCol) * + (calcHorizontalChange(grid, row, col)); + } } // corner top left