MT operator: new separate calc start-stop lines method

pull/1/head
Thorsten Liebig 2010-09-03 12:14:25 +02:00
parent fc2b60ba3e
commit a52cd4711a
3 changed files with 38 additions and 14 deletions

View File

@ -96,6 +96,10 @@ void Engine_Multithread::Init()
if (m_numThreads == 0) if (m_numThreads == 0)
m_numThreads = boost::thread::hardware_concurrency(); m_numThreads = boost::thread::hardware_concurrency();
vector<unsigned int> m_Start_Lines;
vector<unsigned int> m_Stop_Lines;
m_Op_MT->CalcStartStopLines( m_numThreads, m_Start_Lines, m_Stop_Lines );
cout << "Multithreaded engine using " << m_numThreads << " threads. Utilization: ("; cout << "Multithreaded engine using " << m_numThreads << " threads. Utilization: (";
m_barrier_VoltUpdate = new boost::barrier(m_numThreads); // numThread workers m_barrier_VoltUpdate = new boost::barrier(m_numThreads); // numThread workers
m_barrier_VoltExcite = new boost::barrier(m_numThreads+1); // numThread workers + 1 excitation thread m_barrier_VoltExcite = new boost::barrier(m_numThreads+1); // numThread workers + 1 excitation thread
@ -110,15 +114,14 @@ void Engine_Multithread::Init()
m_startBarrier = new boost::barrier(m_numThreads+1); // numThread workers + 1 controller m_startBarrier = new boost::barrier(m_numThreads+1); // numThread workers + 1 controller
m_stopBarrier = new boost::barrier(m_numThreads+1); // numThread workers + 1 controller m_stopBarrier = new boost::barrier(m_numThreads+1); // numThread workers + 1 controller
unsigned int linesPerThread = round((float)numLines[0] / (float)m_numThreads);
for (unsigned int n=0; n<m_numThreads; n++) for (unsigned int n=0; n<m_numThreads; n++)
{ {
unsigned int start = n * linesPerThread; unsigned int start = m_Start_Lines.at(n);
unsigned int stop = (n+1) * linesPerThread - 1; unsigned int stop = m_Stop_Lines.at(n);
unsigned int stop_h = stop; unsigned int stop_h = stop;
if (n == m_numThreads-1) { if (n == m_numThreads-1)
{
// last thread // last thread
stop = numLines[0]-1;
stop_h = stop-1; stop_h = stop-1;
cout << stop-start+1 << ")" << endl; cout << stop-start+1 << ")" << endl;
} }

View File

@ -74,14 +74,32 @@ void Operator_Multithread::Reset()
delete m_CalcPEC_Stop;m_CalcPEC_Stop=NULL; delete m_CalcPEC_Stop;m_CalcPEC_Stop=NULL;
} }
void Operator_Multithread::CalcStartStopLines(unsigned int &numThreads, vector<unsigned int> &start, vector<unsigned int> &stop) const
{
unsigned int linesPerThread = round((float)numLines[0] / (float)numThreads);
if ((numThreads-1) * linesPerThread >= numLines[0])
--numThreads;
start.resize(numThreads);
stop.resize(numThreads);
for (unsigned int n=0; n<numThreads; n++)
{
start.at(n) = n * linesPerThread;
stop.at(n) = (n+1) * linesPerThread - 1;
if (n == numThreads-1) // last thread
stop.at(n) = numLines[0]-1;
}
}
int Operator_Multithread::CalcECOperator() int Operator_Multithread::CalcECOperator()
{ {
if (m_numThreads == 0) if (m_numThreads == 0)
m_numThreads = boost::thread::hardware_concurrency(); m_numThreads = boost::thread::hardware_concurrency();
unsigned int linesPerThread = round((float)numLines[0] / (float)m_numThreads); vector<unsigned int> m_Start_Lines;
if ((m_numThreads-1) * linesPerThread >= numLines[0]) vector<unsigned int> m_Stop_Lines;
--m_numThreads; CalcStartStopLines( m_numThreads, m_Start_Lines, m_Stop_Lines );
cout << "Multithreaded operator using " << m_numThreads << " threads." << std::endl; cout << "Multithreaded operator using " << m_numThreads << " threads." << std::endl;
@ -94,12 +112,7 @@ int Operator_Multithread::CalcECOperator()
for (unsigned int n=0; n<m_numThreads; n++) for (unsigned int n=0; n<m_numThreads; n++)
{ {
unsigned int start = n * linesPerThread; boost::thread *t = new boost::thread( Operator_Thread(this,m_Start_Lines.at(n),m_Stop_Lines.at(n),n) );
unsigned int stop = (n+1) * linesPerThread - 1;
if (n == m_numThreads-1) // last thread
stop = numLines[0]-1;
boost::thread *t = new boost::thread( Operator_Thread(this,start,stop,n) );
m_thread_group.add_thread( t ); m_thread_group.add_thread( t );
} }

View File

@ -56,6 +56,14 @@ protected:
boost::thread_group m_thread_group; boost::thread_group m_thread_group;
unsigned int m_numThreads; // number of worker threads unsigned int m_numThreads; // number of worker threads
//! Calculate the start/stop lines for the multithreading operator and engine.
/*!
It depends on the number of threads and number of lines to simulate.
This method is also used by the multithreading engine!
This method may also reduce the usable number of thread in case of too few lines or otherwise bad utilization.
*/
virtual void CalcStartStopLines(unsigned int &numThreads, vector<unsigned int> &start, vector<unsigned int> &stop) const;
}; };
class Operator_Thread class Operator_Thread