diff --git a/FDTD/operator_cylindermultigrid.cpp b/FDTD/operator_cylindermultigrid.cpp index 8edd06b..b8ff83d 100644 --- a/FDTD/operator_cylindermultigrid.cpp +++ b/FDTD/operator_cylindermultigrid.cpp @@ -18,6 +18,7 @@ #include "operator_cylindermultigrid.h" #include "engine_cylindermultigrid.h" #include "operator_ext_cylinder.h" +#include "tools/useful.h" Operator_CylinderMultiGrid::Operator_CylinderMultiGrid(vector Split_Radii) : Operator_Cylinder() { @@ -127,22 +128,20 @@ void Operator_CylinderMultiGrid::Init() void Operator_CylinderMultiGrid::CalcStartStopLines(unsigned int &numThreads, vector &start, vector &stop) const { - if ((numLines[0] - m_Split_Pos + 1) jpt = AssignJobs2Threads(numLines[0]- m_Split_Pos + 1, numThreads, true); - unsigned int linesPerThread = round((float)(numLines[0] - m_Split_Pos + 1) / (float)numThreads); - if ((numThreads-1) * linesPerThread >= (numLines[0] - m_Split_Pos + 1)) - --numThreads; + numThreads = jpt.size(); start.resize(numThreads); stop.resize(numThreads); - for (unsigned int n=0; n &start, vector &stop) const { - if (numLines[0] jpt = AssignJobs2Threads(numLines[0], numThreads, true); - unsigned int linesPerThread = round((float)numLines[0] / (float)numThreads); - if ((numThreads-1) * linesPerThread >= numLines[0]) - --numThreads; + numThreads = jpt.size(); start.resize(numThreads); stop.resize(numThreads); - for (unsigned int n=0; n #include #include +#include +#include +#include unsigned int CalcNyquistNum(double fmax, double dT) { @@ -29,3 +32,34 @@ unsigned int CalcNyquistNum(double fmax, double dT) return floor(T0/2/dT); } +std::vector AssignJobs2Threads(unsigned int jobs, unsigned int nrThreads, bool RemoveEmpty) +{ + std::vector jpt; //jobs per thread + + unsigned int ui_jpt = jobs/nrThreads; + for (unsigned int n=0;n0) + { + ++jpt.at(n); + --jobs; + } + } + + if (jobs>0) + std::cerr << "AssignJobs2Threads: Error, " << jobs << " remain to be assigned, this should not have happend..." << std::endl; + + if (RemoveEmpty) + { + while (jpt.back()==0) + jpt.pop_back(); + } + + return jpt; +} diff --git a/tools/useful.h b/tools/useful.h index a5a36a3..c6160cf 100644 --- a/tools/useful.h +++ b/tools/useful.h @@ -18,7 +18,12 @@ #ifndef USEFUL_H #define USEFUL_H +#include + //! Calc the nyquist number of timesteps for a given frequency and timestep unsigned int CalcNyquistNum(double fmax, double dT); +//! Calculate an optimal job distribution to a given number of threads. Will return a vector with the jobs for each thread. +std::vector AssignJobs2Threads(unsigned int jobs, unsigned int nrThreads, bool RemoveEmpty=false); + #endif // USEFUL_H