2010-05-20 20:02:06 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Thorsten Liebig (Thorsten.Liebig@gmx.de)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "operator_multithread.h"
|
|
|
|
#include "engine_multithread.h"
|
2010-10-05 15:24:36 +00:00
|
|
|
#include "tools/useful.h"
|
2010-05-20 20:02:06 +00:00
|
|
|
|
2010-06-05 09:50:13 +00:00
|
|
|
Operator_Multithread* Operator_Multithread::New(unsigned int numThreads)
|
2010-05-20 20:02:06 +00:00
|
|
|
{
|
|
|
|
cout << "Create FDTD operator (compressed SSE + multi-threading)" << endl;
|
|
|
|
Operator_Multithread* op = new Operator_Multithread();
|
2010-06-05 09:50:13 +00:00
|
|
|
op->setNumThreads(numThreads);
|
2010-05-20 20:02:06 +00:00
|
|
|
op->Init();
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
|
|
|
Operator_Multithread::~Operator_Multithread()
|
|
|
|
{
|
2011-01-24 10:11:45 +00:00
|
|
|
Delete();
|
2010-05-20 20:02:06 +00:00
|
|
|
}
|
|
|
|
|
2010-06-05 09:50:13 +00:00
|
|
|
void Operator_Multithread::setNumThreads( unsigned int numThreads )
|
|
|
|
{
|
|
|
|
m_numThreads = numThreads;
|
|
|
|
}
|
|
|
|
|
2010-05-20 20:02:06 +00:00
|
|
|
Engine* Operator_Multithread::CreateEngine() const
|
|
|
|
{
|
2010-06-05 09:50:13 +00:00
|
|
|
Engine_Multithread* e = Engine_Multithread::New(this,m_numThreads);
|
2010-05-20 20:02:06 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2011-02-16 14:27:56 +00:00
|
|
|
Operator_Multithread::Operator_Multithread() : OPERATOR_MULTITHREAD_BASE()
|
2010-05-20 20:02:06 +00:00
|
|
|
{
|
2010-06-05 09:50:13 +00:00
|
|
|
m_CalcEC_Start=NULL;
|
|
|
|
m_CalcEC_Stop=NULL;
|
2010-06-05 23:47:32 +00:00
|
|
|
|
|
|
|
m_CalcPEC_Start=NULL;
|
|
|
|
m_CalcPEC_Stop=NULL;
|
2010-05-20 20:02:06 +00:00
|
|
|
}
|
2010-06-05 09:50:13 +00:00
|
|
|
|
|
|
|
void Operator_Multithread::Init()
|
|
|
|
{
|
2011-02-16 14:27:56 +00:00
|
|
|
OPERATOR_MULTITHREAD_BASE::Init();
|
2011-01-24 10:11:45 +00:00
|
|
|
|
2010-06-05 09:50:13 +00:00
|
|
|
m_CalcEC_Start=NULL;
|
|
|
|
m_CalcEC_Stop=NULL;
|
2010-06-05 23:47:32 +00:00
|
|
|
|
|
|
|
m_CalcPEC_Start=NULL;
|
|
|
|
m_CalcPEC_Stop=NULL;
|
2010-06-05 09:50:13 +00:00
|
|
|
}
|
|
|
|
|
2011-01-24 10:11:45 +00:00
|
|
|
void Operator_Multithread::Delete()
|
2010-06-05 09:50:13 +00:00
|
|
|
{
|
|
|
|
m_thread_group.join_all();
|
|
|
|
|
2010-12-06 12:04:37 +00:00
|
|
|
delete m_CalcEC_Start;
|
|
|
|
m_CalcEC_Start=NULL;
|
|
|
|
delete m_CalcEC_Stop;
|
|
|
|
m_CalcEC_Stop=NULL;
|
2010-06-05 23:47:32 +00:00
|
|
|
|
2010-12-06 12:04:37 +00:00
|
|
|
delete m_CalcPEC_Start;
|
|
|
|
m_CalcPEC_Start=NULL;
|
|
|
|
delete m_CalcPEC_Stop;
|
|
|
|
m_CalcPEC_Stop=NULL;
|
2010-06-05 09:50:13 +00:00
|
|
|
}
|
|
|
|
|
2011-01-24 10:11:45 +00:00
|
|
|
void Operator_Multithread::Reset()
|
|
|
|
{
|
|
|
|
Delete();
|
2011-02-16 14:27:56 +00:00
|
|
|
OPERATOR_MULTITHREAD_BASE::Reset();
|
2011-01-24 10:11:45 +00:00
|
|
|
}
|
|
|
|
|
2010-09-03 10:14:25 +00:00
|
|
|
void Operator_Multithread::CalcStartStopLines(unsigned int &numThreads, vector<unsigned int> &start, vector<unsigned int> &stop) const
|
|
|
|
{
|
2010-10-05 15:24:36 +00:00
|
|
|
vector<unsigned int> jpt = AssignJobs2Threads(numLines[0], numThreads, true);
|
2010-09-08 05:36:32 +00:00
|
|
|
|
2010-10-05 15:24:36 +00:00
|
|
|
numThreads = jpt.size();
|
2010-09-03 10:14:25 +00:00
|
|
|
|
|
|
|
start.resize(numThreads);
|
|
|
|
stop.resize(numThreads);
|
|
|
|
|
2010-10-05 15:24:36 +00:00
|
|
|
start.at(0)=0;
|
|
|
|
stop.at(0)=jpt.at(0)-1;
|
|
|
|
|
|
|
|
for (unsigned int n=1; n<numThreads; n++)
|
2010-09-03 10:14:25 +00:00
|
|
|
{
|
2010-10-05 15:24:36 +00:00
|
|
|
start.at(n) = stop.at(n-1)+1;
|
|
|
|
stop.at(n) = start.at(n) + jpt.at(n) - 1;
|
2010-09-03 10:14:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-27 09:17:58 +00:00
|
|
|
int Operator_Multithread::CalcECOperator( DebugFlags debugFlags )
|
2010-06-05 09:50:13 +00:00
|
|
|
{
|
|
|
|
if (m_numThreads == 0)
|
|
|
|
m_numThreads = boost::thread::hardware_concurrency();
|
|
|
|
|
2010-09-03 10:14:25 +00:00
|
|
|
vector<unsigned int> m_Start_Lines;
|
|
|
|
vector<unsigned int> m_Stop_Lines;
|
|
|
|
CalcStartStopLines( m_numThreads, m_Start_Lines, m_Stop_Lines );
|
2010-08-25 13:27:59 +00:00
|
|
|
|
2010-06-07 21:08:38 +00:00
|
|
|
cout << "Multithreaded operator using " << m_numThreads << " threads." << std::endl;
|
2010-06-05 09:50:13 +00:00
|
|
|
|
|
|
|
m_thread_group.join_all();
|
2010-12-06 12:04:37 +00:00
|
|
|
delete m_CalcEC_Start;
|
|
|
|
m_CalcEC_Start = new boost::barrier(m_numThreads+1); // numThread workers + 1 controller
|
|
|
|
delete m_CalcEC_Stop;
|
|
|
|
m_CalcEC_Stop = new boost::barrier(m_numThreads+1); // numThread workers + 1 controller
|
2010-06-05 09:50:13 +00:00
|
|
|
|
2010-12-06 12:04:37 +00:00
|
|
|
delete m_CalcPEC_Start;
|
|
|
|
m_CalcPEC_Start = new boost::barrier(m_numThreads+1); // numThread workers + 1 controller
|
|
|
|
delete m_CalcPEC_Stop;
|
|
|
|
m_CalcPEC_Stop = new boost::barrier(m_numThreads+1); // numThread workers + 1 controller
|
2010-06-05 23:47:32 +00:00
|
|
|
|
2010-06-05 09:50:13 +00:00
|
|
|
for (unsigned int n=0; n<m_numThreads; n++)
|
|
|
|
{
|
2010-09-03 10:14:25 +00:00
|
|
|
boost::thread *t = new boost::thread( Operator_Thread(this,m_Start_Lines.at(n),m_Stop_Lines.at(n),n) );
|
2010-06-05 09:50:13 +00:00
|
|
|
m_thread_group.add_thread( t );
|
|
|
|
}
|
|
|
|
|
2011-02-16 14:27:56 +00:00
|
|
|
return OPERATOR_MULTITHREAD_BASE::CalcECOperator( debugFlags );
|
2010-06-05 09:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Operator_Multithread::Calc_EC()
|
|
|
|
{
|
2010-12-06 12:04:37 +00:00
|
|
|
if (CSX==NULL)
|
|
|
|
{
|
|
|
|
cerr << "CartOperator::Calc_EC: CSX not given or invalid!!!" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
2010-06-05 09:50:13 +00:00
|
|
|
|
|
|
|
MainOp->SetPos(0,0,0);
|
|
|
|
|
|
|
|
m_CalcEC_Start->wait();
|
|
|
|
|
|
|
|
m_CalcEC_Stop->wait();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-05 23:47:32 +00:00
|
|
|
bool Operator_Multithread::CalcPEC()
|
|
|
|
{
|
2010-12-06 12:04:37 +00:00
|
|
|
m_Nr_PEC[0]=0;
|
|
|
|
m_Nr_PEC[1]=0;
|
|
|
|
m_Nr_PEC[2]=0;
|
2010-06-05 23:47:32 +00:00
|
|
|
|
|
|
|
m_Nr_PEC_thread = new unsigned int[m_numThreads][3];
|
|
|
|
|
|
|
|
m_CalcPEC_Start->wait();
|
|
|
|
|
|
|
|
m_CalcPEC_Stop->wait();
|
|
|
|
|
2010-12-06 12:04:37 +00:00
|
|
|
for (unsigned int t=0; t<m_numThreads; ++t)
|
|
|
|
for (int n=0; n<3; ++n)
|
2010-06-05 23:47:32 +00:00
|
|
|
m_Nr_PEC[n]+=m_Nr_PEC_thread[t][n];
|
|
|
|
|
|
|
|
CalcPEC_Curves();
|
|
|
|
|
|
|
|
delete[] m_Nr_PEC_thread;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-05 09:50:13 +00:00
|
|
|
Operator_Thread::Operator_Thread( Operator_Multithread* ptr, unsigned int start, unsigned int stop, unsigned int threadID )
|
|
|
|
{
|
|
|
|
m_start=start;
|
|
|
|
m_stop=stop;
|
|
|
|
m_threadID=threadID;
|
|
|
|
m_OpPtr = ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Operator_Thread::operator()()
|
|
|
|
{
|
|
|
|
//************** calculate EC (Calc_EC) ***********************//
|
|
|
|
m_OpPtr->m_CalcEC_Start->wait();
|
|
|
|
unsigned int ipos;
|
|
|
|
unsigned int pos[3];
|
|
|
|
double inEC[4];
|
2010-12-06 12:04:37 +00:00
|
|
|
for (int n=0; n<3; ++n)
|
2010-06-05 09:50:13 +00:00
|
|
|
{
|
2010-12-06 12:04:37 +00:00
|
|
|
for (pos[0]=m_start; pos[0]<=m_stop; ++pos[0])
|
2010-06-05 09:50:13 +00:00
|
|
|
{
|
2010-12-06 12:04:37 +00:00
|
|
|
for (pos[1]=0; pos[1]<m_OpPtr->numLines[1]; ++pos[1])
|
2010-06-05 09:50:13 +00:00
|
|
|
{
|
2010-12-06 12:04:37 +00:00
|
|
|
for (pos[2]=0; pos[2]<m_OpPtr->numLines[2]; ++pos[2])
|
2010-06-05 09:50:13 +00:00
|
|
|
{
|
|
|
|
m_OpPtr->Calc_ECPos(n,pos,inEC);
|
|
|
|
ipos = m_OpPtr->MainOp->GetPos(pos[0],pos[1],pos[2]);
|
|
|
|
m_OpPtr->EC_C[n][ipos]=inEC[0];
|
|
|
|
m_OpPtr->EC_G[n][ipos]=inEC[1];
|
|
|
|
m_OpPtr->EC_L[n][ipos]=inEC[2];
|
|
|
|
m_OpPtr->EC_R[n][ipos]=inEC[3];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_OpPtr->m_CalcEC_Stop->wait();
|
2010-06-05 23:47:32 +00:00
|
|
|
|
|
|
|
//************** calculate EC (Calc_EC) ***********************//
|
|
|
|
m_OpPtr->m_CalcPEC_Start->wait();
|
2010-12-06 12:04:37 +00:00
|
|
|
for (int n=0; n<3; ++n)
|
2010-06-05 23:47:32 +00:00
|
|
|
m_OpPtr->m_Nr_PEC_thread[m_threadID][n] = 0;
|
|
|
|
|
|
|
|
m_OpPtr->CalcPEC_Range(m_start,m_stop,m_OpPtr->m_Nr_PEC_thread[m_threadID]);
|
|
|
|
m_OpPtr->m_CalcPEC_Stop->wait();
|
2010-06-05 09:50:13 +00:00
|
|
|
}
|
|
|
|
|