From 6b8368d9f7988d8a51ac62602f0dae55a4946416 Mon Sep 17 00:00:00 2001 From: Hannes Signer Date: Wed, 23 Aug 2023 18:34:48 +0200 Subject: [PATCH] add setNumberThreads method --- include/tug/Simulation.hpp | 3 +++ src/BTCSv2.cpp | 20 ++++++++------------ src/FTCS.cpp | 16 ++++++++-------- src/Simulation.cpp | 31 ++++++++++++++++++++++--------- 4 files changed, 41 insertions(+), 29 deletions(-) diff --git a/include/tug/Simulation.hpp b/include/tug/Simulation.hpp index 7bca259..ae67f5f 100644 --- a/include/tug/Simulation.hpp +++ b/include/tug/Simulation.hpp @@ -142,6 +142,8 @@ class Simulation { */ void setSolver(SOLVER solver); + void setNumberThreads(int num_threads); + /** * @brief Return the currently set iterations to be calculated. * @@ -185,6 +187,7 @@ class Simulation { double timestep; int iterations; int innerIterations; + int numThreads; CSV_OUTPUT csv_output; CONSOLE_OUTPUT console_output; TIME_MEASURE time_measure; diff --git a/src/BTCSv2.cpp b/src/BTCSv2.cpp index 8751a43..06d9da2 100644 --- a/src/BTCSv2.cpp +++ b/src/BTCSv2.cpp @@ -9,7 +9,7 @@ #include #include -#define NUM_THREADS_BTCS 1 +#define NUM_THREADS_BTCS 10 using namespace Eigen; @@ -87,7 +87,6 @@ static SparseMatrix createCoeffMatrix(MatrixXd &alpha, vector 0 && rowIndex < numRows-1) { - #pragma omp parallel for num_threads(NUM_THREADS_BTCS) for (int i = 0; i < length; i++) { sv(i) = sy * calcAlphaIntercell(alphaY(rowIndex,i), alphaY(rowIndex+1,i)) * concentrations(rowIndex+1,i) @@ -220,7 +218,6 @@ static VectorXd createSolutionVector(MatrixXd &concentrations, MatrixXd &alphaX, // first row if (rowIndex == 0) { - #pragma omp parallel for num_threads(NUM_THREADS_BTCS) for (int i = 0; i < length; i++) { type = bcTop[i].getType(); if (type == BC_TYPE_CONSTANT) { @@ -235,7 +232,6 @@ static VectorXd createSolutionVector(MatrixXd &concentrations, MatrixXd &alphaX, // last row if (rowIndex == numRows-1) { - #pragma omp parallel for num_threads(NUM_THREADS_BTCS) for (int i = 0; i < length; i++) { type = bcBottom[i].getType(); if (type == BC_TYPE_CONSTANT) { @@ -357,7 +353,7 @@ static void BTCS_1D(Grid &grid, Boundary &bc, double ×tep, VectorXd (*solve // BTCS solution for 2D grid -static void BTCS_2D(Grid &grid, Boundary &bc, double ×tep, VectorXd (*solverFunc) (SparseMatrix &A, VectorXd &b)) { +static void BTCS_2D(Grid &grid, Boundary &bc, double ×tep, VectorXd (*solverFunc) (SparseMatrix &A, VectorXd &b), int &numThreads) { int rowMax = grid.getRow(); int colMax = grid.getCol(); double sx = timestep / (2 * grid.getDeltaCol() * grid.getDeltaCol()); @@ -377,7 +373,7 @@ static void BTCS_2D(Grid &grid, Boundary &bc, double ×tep, VectorXd (*solve vector bcBottom = bc.getBoundarySide(BC_SIDE_BOTTOM); MatrixXd concentrations = grid.getConcentrations(); - #pragma omp parallel for num_threads(NUM_THREADS_BTCS) private(A, b, row_t1) + #pragma omp parallel for num_threads(numThreads) private(A, b, row_t1) for (int i = 0; i < rowMax; i++) { @@ -397,7 +393,7 @@ static void BTCS_2D(Grid &grid, Boundary &bc, double ×tep, VectorXd (*solve alphaX.transposeInPlace(); alphaY.transposeInPlace(); - #pragma omp parallel for num_threads(NUM_THREADS_BTCS) private(A, b, row_t1) + #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 @@ -417,22 +413,22 @@ static void BTCS_2D(Grid &grid, Boundary &bc, double ×tep, VectorXd (*solve // entry point for EigenLU solver; differentiate between 1D and 2D grid -static void BTCS_LU(Grid &grid, Boundary &bc, double ×tep) { +static void BTCS_LU(Grid &grid, Boundary &bc, double ×tep, int &numThreads) { if (grid.getDim() == 1) { BTCS_1D(grid, bc, timestep, EigenLUAlgorithm); } else if (grid.getDim() == 2) { - BTCS_2D(grid, bc, timestep, EigenLUAlgorithm); + BTCS_2D(grid, bc, timestep, EigenLUAlgorithm, numThreads); } else { throw_invalid_argument("Error: Only 1- and 2-dimensional grids are defined!"); } } // entry point for Thomas algorithm solver; differentiate 1D and 2D grid -static void BTCS_Thomas(Grid &grid, Boundary &bc, double ×tep) { +static void BTCS_Thomas(Grid &grid, Boundary &bc, double ×tep, int &numThreads) { if (grid.getDim() == 1) { BTCS_1D(grid, bc, timestep, ThomasAlgorithm); } else if (grid.getDim() == 2) { - BTCS_2D(grid, bc, timestep, ThomasAlgorithm); + BTCS_2D(grid, bc, timestep, ThomasAlgorithm, numThreads); } else { throw_invalid_argument("Error: Only 1- and 2-dimensional grids are defined!"); } diff --git a/src/FTCS.cpp b/src/FTCS.cpp index e341d45..449a1c1 100644 --- a/src/FTCS.cpp +++ b/src/FTCS.cpp @@ -259,7 +259,7 @@ static void FTCS_1D(Grid &grid, Boundary &bc, double ×tep) { // FTCS solution for 2D grid -static void FTCS_2D(Grid &grid, Boundary &bc, double ×tep) { +static void FTCS_2D(Grid &grid, Boundary &bc, double ×tep, int numThreads) { int rowMax = grid.getRow(); int colMax = grid.getCol(); double deltaRow = grid.getDeltaRow(); @@ -271,7 +271,7 @@ static void FTCS_2D(Grid &grid, Boundary &bc, double ×tep) { // inner cells // these are independent of the boundary condition type // omp_set_num_threads(10); -#pragma omp parallel for num_threads(NUM_THREADS) +#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) @@ -291,7 +291,7 @@ static void FTCS_2D(Grid &grid, Boundary &bc, double ×tep) { // left without corners / looping over rows // hold column constant at index 0 int col = 0; -#pragma omp parallel for num_threads(NUM_THREADS) +#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) @@ -308,7 +308,7 @@ static void FTCS_2D(Grid &grid, Boundary &bc, double ×tep) { // right without corners / looping over rows // hold column constant at max index col = colMax-1; -#pragma omp parallel for num_threads(NUM_THREADS) +#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) @@ -326,7 +326,7 @@ static void FTCS_2D(Grid &grid, Boundary &bc, double ×tep) { // top without corners / looping over columns // hold row constant at index 0 int row = 0; -#pragma omp parallel for num_threads(NUM_THREADS) +#pragma omp parallel for num_threads(numThreads) for (int col=1; col #include #include +#include #include @@ -22,6 +23,7 @@ Simulation::Simulation(Grid &grid, Boundary &bc, APPROACH approach) : grid(grid) this->timestep = -1; // error per default this->iterations = -1; this->innerIterations = 1; + this->numThreads = omp_get_num_procs(); this->csv_output = CSV_OUTPUT_OFF; this->console_output = CONSOLE_OUTPUT_OFF; @@ -138,6 +140,17 @@ void Simulation::setSolver(SOLVER solver) { this->solver = solver; } +void Simulation::setNumberThreads(int numThreads){ + if(numThreads>0 && numThreads<=omp_get_num_procs()){ + this->numThreads=numThreads; + } + else{ + int maxThreadNumber = omp_get_num_procs(); + + throw_invalid_argument("Number of threads exceeds the number of processor cores or is less than 1."); + } +} + int Simulation::getIterations() { return this->iterations; } @@ -227,14 +240,14 @@ void Simulation::run() { printConcentrationsCSV(filename); } - FTCS(this->grid, this->bc, this->timestep); + FTCS(this->grid, this->bc, this->timestep, this->numThreads); - if (i % (iterations * innerIterations / 100) == 0) { - double percentage = (double)i / ((double)iterations * (double)innerIterations) * 100; - if ((int)percentage % 10 == 0) { - cout << "Progress: " << percentage << "%" << endl; - } - } + // if (i % (iterations * innerIterations / 100) == 0) { + // double percentage = (double)i / ((double)iterations * (double)innerIterations) * 100; + // if ((int)percentage % 10 == 0) { + // cout << "Progress: " << percentage << "%" << endl; + // } + // } } } else if (approach == BTCS_APPROACH) { // BTCS case @@ -248,7 +261,7 @@ void Simulation::run() { printConcentrationsCSV(filename); } - BTCS_LU(this->grid, this->bc, this->timestep); + BTCS_LU(this->grid, this->bc, this->timestep, this->numThreads); } } else if (solver == THOMAS_ALGORITHM_SOLVER) { @@ -260,7 +273,7 @@ void Simulation::run() { printConcentrationsCSV(filename); } - BTCS_Thomas(this->grid, this->bc, this->timestep); + BTCS_Thomas(this->grid, this->bc, this->timestep, this->numThreads); } }