2010-09-08 05:36:32 +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 "engine_cylindermultigrid.h"
|
|
|
|
#include "operator_cylindermultigrid.h"
|
2010-12-06 14:30:47 +00:00
|
|
|
#include "extensions/engine_ext_cylindermultigrid.h"
|
2010-09-08 05:36:32 +00:00
|
|
|
|
|
|
|
Engine_CylinderMultiGrid* Engine_CylinderMultiGrid::New(const Operator_CylinderMultiGrid* op, unsigned int numThreads)
|
|
|
|
{
|
|
|
|
cout << "Create FDTD engine (cylindrical multi grid mesh using sse compression + multithreading)" << endl;
|
|
|
|
Engine_CylinderMultiGrid* e = new Engine_CylinderMultiGrid(op);
|
|
|
|
e->setNumThreads( numThreads );
|
|
|
|
e->Init();
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2012-06-06 08:25:40 +00:00
|
|
|
Engine_CylinderMultiGrid::Engine_CylinderMultiGrid(const Operator_CylinderMultiGrid* op) : Engine_Cylinder(op)
|
2010-09-08 05:36:32 +00:00
|
|
|
{
|
|
|
|
Op_CMG = op;
|
|
|
|
|
|
|
|
m_WaitOnBase = new boost::barrier(2);
|
|
|
|
m_WaitOnChild = new boost::barrier(2);
|
|
|
|
m_WaitOnSync = new boost::barrier(2);
|
|
|
|
|
|
|
|
m_Eng_Ext_MG = new Engine_Ext_CylinderMultiGrid(NULL,true);
|
|
|
|
m_Eng_Ext_MG->SetBarrier(m_WaitOnBase, m_WaitOnChild, m_WaitOnSync);
|
|
|
|
m_Eng_Ext_MG->SetEngine(this);
|
2010-09-08 14:07:28 +00:00
|
|
|
|
|
|
|
Engine* eng = op->GetInnerOperator()->CreateEngine();
|
|
|
|
m_InnerEngine = dynamic_cast<Engine_Multithread*>(eng);
|
|
|
|
|
|
|
|
Engine_Ext_CylinderMultiGrid* m_InnerEng_Ext_MG = new Engine_Ext_CylinderMultiGrid(NULL,false);
|
2010-09-08 05:36:32 +00:00
|
|
|
m_InnerEng_Ext_MG->SetBarrier(m_WaitOnBase, m_WaitOnChild, m_WaitOnSync);
|
|
|
|
|
2010-09-08 14:07:28 +00:00
|
|
|
// if already has a base extension, switch places ... seems to be faster...
|
2010-12-06 12:04:37 +00:00
|
|
|
for (size_t n=0; n<m_InnerEngine->m_Eng_exts.size(); ++n)
|
2010-09-08 14:07:28 +00:00
|
|
|
{
|
|
|
|
Engine_Ext_CylinderMultiGrid* eng_mg = dynamic_cast<Engine_Ext_CylinderMultiGrid*>(m_InnerEngine->m_Eng_exts.at(n));
|
|
|
|
if (eng_mg)
|
|
|
|
{
|
|
|
|
m_InnerEngine->m_Eng_exts.at(n) = m_InnerEng_Ext_MG;
|
|
|
|
m_InnerEng_Ext_MG = eng_mg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-09-08 05:36:32 +00:00
|
|
|
m_InnerEngine->m_Eng_exts.push_back(m_InnerEng_Ext_MG);
|
|
|
|
}
|
|
|
|
|
|
|
|
Engine_CylinderMultiGrid::~Engine_CylinderMultiGrid()
|
|
|
|
{
|
2011-02-23 10:08:24 +00:00
|
|
|
#ifdef MPI_SUPPORT
|
|
|
|
delete m_InnerEngine->m_MPI_Barrier;
|
|
|
|
m_InnerEngine->m_MPI_Barrier = NULL;
|
|
|
|
#endif
|
|
|
|
|
2010-09-08 05:36:32 +00:00
|
|
|
m_Thread_NumTS = 0;
|
|
|
|
m_startBarrier->wait();
|
|
|
|
|
|
|
|
m_IteratorThread_Group.join_all();
|
|
|
|
|
|
|
|
delete m_InnerEngine;
|
|
|
|
m_InnerEngine = NULL;
|
|
|
|
|
|
|
|
delete m_WaitOnBase;
|
|
|
|
m_WaitOnBase = NULL;
|
|
|
|
delete m_WaitOnChild;
|
|
|
|
m_WaitOnChild = NULL;
|
|
|
|
delete m_WaitOnSync;
|
|
|
|
m_WaitOnSync = NULL;
|
|
|
|
|
|
|
|
delete m_startBarrier;
|
|
|
|
m_startBarrier = NULL;
|
|
|
|
delete m_stopBarrier;
|
|
|
|
m_stopBarrier = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Engine_CylinderMultiGrid::Init()
|
|
|
|
{
|
|
|
|
Engine_Multithread::Init();
|
|
|
|
|
|
|
|
m_Eng_exts.push_back(m_Eng_Ext_MG);
|
|
|
|
|
|
|
|
m_startBarrier = new boost::barrier(3); //both engines + organizer
|
|
|
|
m_stopBarrier = new boost::barrier(3); //both engines + organizer
|
|
|
|
|
|
|
|
boost::thread *t = NULL;
|
|
|
|
|
2010-09-08 14:07:28 +00:00
|
|
|
t = new boost::thread( Engine_CylinderMultiGrid_Thread(this,m_startBarrier,m_stopBarrier,&m_Thread_NumTS, true) );
|
2010-09-08 05:36:32 +00:00
|
|
|
m_IteratorThread_Group.add_thread( t );
|
|
|
|
|
2010-09-08 14:07:28 +00:00
|
|
|
t = new boost::thread( Engine_CylinderMultiGrid_Thread(m_InnerEngine,m_startBarrier,m_stopBarrier,&m_Thread_NumTS, false) );
|
2010-09-08 05:36:32 +00:00
|
|
|
m_IteratorThread_Group.add_thread( t );
|
2010-09-25 17:23:53 +00:00
|
|
|
|
|
|
|
m_InnerEngine->SortExtensionByPriority();
|
|
|
|
SortExtensionByPriority();
|
2011-02-23 10:08:24 +00:00
|
|
|
|
|
|
|
#ifdef MPI_SUPPORT
|
|
|
|
//assign an MPI barrier to inner Engine
|
|
|
|
m_InnerEngine->m_MPI_Barrier = new boost::barrier(2);
|
|
|
|
#endif
|
2010-09-08 05:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Engine_CylinderMultiGrid::IterateTS(unsigned int iterTS)
|
|
|
|
{
|
|
|
|
m_Thread_NumTS = iterTS;
|
|
|
|
|
|
|
|
m_startBarrier->wait(); //start base and child iterations
|
|
|
|
|
|
|
|
m_stopBarrier->wait(); //tell base and child to wait for another start event...
|
|
|
|
|
|
|
|
//interpolate child data to base mesh...
|
2010-12-06 12:04:37 +00:00
|
|
|
for (unsigned int n=0; n<Op_CMG->m_Split_Pos-1; ++n)
|
2010-09-08 05:36:32 +00:00
|
|
|
InterpolVoltChild2Base(n);
|
2010-12-06 12:04:37 +00:00
|
|
|
for (unsigned int n=0; n<Op_CMG->m_Split_Pos-2; ++n)
|
2010-09-08 05:36:32 +00:00
|
|
|
InterpolCurrChild2Base(n);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-25 11:22:34 +00:00
|
|
|
void Engine_CylinderMultiGrid::InterpolVoltChild2Base(unsigned int rPos)
|
2010-09-08 05:36:32 +00:00
|
|
|
{
|
|
|
|
//interpolate voltages from child engine to the base engine...
|
|
|
|
unsigned int pos[3];
|
2012-06-25 11:22:34 +00:00
|
|
|
pos[0] = rPos;
|
|
|
|
for (pos[1]=0; pos[1]<numLines[1]; ++pos[1])
|
2010-09-08 05:36:32 +00:00
|
|
|
{
|
|
|
|
for (pos[2]=0; pos[2]<numVectors; ++pos[2])
|
|
|
|
{
|
|
|
|
//r - direction
|
2012-06-25 11:22:34 +00:00
|
|
|
f4_volt[0][pos[0]][pos[1]][pos[2]].v = Op_CMG->f4_interpol_v_2p[0][pos[1]].v * m_InnerEngine->f4_volt[0][pos[0]][Op_CMG->m_interpol_pos_v_2p[0][pos[1]]][pos[2]].v
|
|
|
|
+ Op_CMG->f4_interpol_v_2pp[0][pos[1]].v * m_InnerEngine->f4_volt[0][pos[0]][Op_CMG->m_interpol_pos_v_2pp[0][pos[1]]][pos[2]].v;
|
2010-09-08 05:36:32 +00:00
|
|
|
|
|
|
|
//z - direction
|
2012-06-25 11:22:34 +00:00
|
|
|
f4_volt[2][pos[0]][pos[1]][pos[2]].v = Op_CMG->f4_interpol_v_2p[0][pos[1]].v * m_InnerEngine->f4_volt[2][pos[0]][Op_CMG->m_interpol_pos_v_2p[0][pos[1]]][pos[2]].v
|
|
|
|
+ Op_CMG->f4_interpol_v_2pp[0][pos[1]].v * m_InnerEngine->f4_volt[2][pos[0]][Op_CMG->m_interpol_pos_v_2pp[0][pos[1]]][pos[2]].v;
|
2010-09-08 05:36:32 +00:00
|
|
|
|
|
|
|
//alpha - direction
|
2012-06-25 11:22:34 +00:00
|
|
|
f4_volt[1][pos[0]][pos[1]][pos[2]].v = Op_CMG->f4_interpol_v_2p[1][pos[1]].v * m_InnerEngine->f4_volt[1][pos[0]][Op_CMG->m_interpol_pos_v_2p[1][pos[1]]][pos[2]].v
|
|
|
|
+ Op_CMG->f4_interpol_v_2pp[1][pos[1]].v * m_InnerEngine->f4_volt[1][pos[0]][Op_CMG->m_interpol_pos_v_2pp[1][pos[1]]][pos[2]].v;
|
2010-09-24 10:01:38 +00:00
|
|
|
}
|
2010-09-08 05:36:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-25 11:22:34 +00:00
|
|
|
void Engine_CylinderMultiGrid::InterpolCurrChild2Base(unsigned int rPos)
|
2010-09-08 05:36:32 +00:00
|
|
|
{
|
|
|
|
//interpolate voltages from child engine to the base engine...
|
|
|
|
unsigned int pos[3];
|
2012-06-25 11:22:34 +00:00
|
|
|
pos[0] = rPos;
|
|
|
|
for (pos[1]=0; pos[1]<numLines[1]; ++pos[1])
|
2010-09-08 05:36:32 +00:00
|
|
|
{
|
|
|
|
for (pos[2]=0; pos[2]<numVectors; ++pos[2])
|
|
|
|
{
|
|
|
|
//r - direction
|
2012-06-25 11:22:34 +00:00
|
|
|
f4_curr[0][pos[0]][pos[1]][pos[2]].v = Op_CMG->f4_interpol_i_2p[0][pos[1]].v * m_InnerEngine->f4_curr[0][pos[0]][Op_CMG->m_interpol_pos_i_2p[0][pos[1]]][pos[2]].v
|
|
|
|
+ Op_CMG->f4_interpol_i_2pp[0][pos[1]].v * m_InnerEngine->f4_curr[0][pos[0]][Op_CMG->m_interpol_pos_i_2pp[0][pos[1]]][pos[2]].v;
|
2010-09-08 05:36:32 +00:00
|
|
|
|
|
|
|
//z - direction
|
2012-06-25 11:22:34 +00:00
|
|
|
f4_curr[2][pos[0]][pos[1]][pos[2]].v = Op_CMG->f4_interpol_i_2p[0][pos[1]].v * m_InnerEngine->f4_curr[2][pos[0]][Op_CMG->m_interpol_pos_i_2p[0][pos[1]]][pos[2]].v
|
|
|
|
+ Op_CMG->f4_interpol_i_2pp[0][pos[1]].v * m_InnerEngine->f4_curr[2][pos[0]][Op_CMG->m_interpol_pos_i_2pp[0][pos[1]]][pos[2]].v;
|
2010-09-08 05:36:32 +00:00
|
|
|
|
|
|
|
//alpha - direction
|
2012-06-25 11:22:34 +00:00
|
|
|
f4_curr[1][pos[0]][pos[1]][pos[2]].v = Op_CMG->f4_interpol_i_2p[1][pos[1]].v * m_InnerEngine->f4_curr[1][pos[0]][Op_CMG->m_interpol_pos_i_2p[1][pos[1]]][pos[2]].v
|
|
|
|
+ Op_CMG->f4_interpol_i_2pp[1][pos[1]].v * m_InnerEngine->f4_curr[1][pos[0]][Op_CMG->m_interpol_pos_i_2pp[1][pos[1]]][pos[2]].v;
|
2010-09-24 10:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-08 05:36:32 +00:00
|
|
|
}
|
|
|
|
|
2011-02-23 10:08:24 +00:00
|
|
|
#ifdef MPI_SUPPORT
|
|
|
|
void Engine_CylinderMultiGrid::SendReceiveVoltages()
|
|
|
|
{
|
|
|
|
//do the local voltage sync, child is waiting...
|
|
|
|
Engine_Multithread::SendReceiveVoltages();
|
|
|
|
|
|
|
|
//run inner voltage sync
|
|
|
|
m_InnerEngine->m_MPI_Barrier->wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Engine_CylinderMultiGrid::SendReceiveCurrents()
|
|
|
|
{
|
|
|
|
//do the local current sync, child is waiting...
|
|
|
|
Engine_Multithread::SendReceiveCurrents();
|
|
|
|
|
|
|
|
//run inner voltage sync
|
|
|
|
m_InnerEngine->m_MPI_Barrier->wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2010-09-08 05:36:32 +00:00
|
|
|
/****************************************************************************************/
|
2010-09-08 14:07:28 +00:00
|
|
|
Engine_CylinderMultiGrid_Thread::Engine_CylinderMultiGrid_Thread( Engine_Multithread* engine, boost::barrier *start, boost::barrier *stop, volatile unsigned int* numTS, bool isBase)
|
2010-09-08 05:36:32 +00:00
|
|
|
{
|
|
|
|
m_startBarrier = start;
|
|
|
|
m_stopBarrier = stop;
|
|
|
|
m_Eng=engine;
|
2010-09-08 14:07:28 +00:00
|
|
|
m_isBase=isBase;
|
2010-09-08 05:36:32 +00:00
|
|
|
m_numTS = numTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Engine_CylinderMultiGrid_Thread::operator()()
|
|
|
|
{
|
|
|
|
m_startBarrier->wait(); //wait for Base engine to start the iterations...
|
|
|
|
|
2010-12-06 12:04:37 +00:00
|
|
|
while (*m_numTS>0) //m_numTS==0 request to terminate this thread...
|
2010-09-08 05:36:32 +00:00
|
|
|
{
|
2010-09-08 14:07:28 +00:00
|
|
|
if (m_isBase)
|
|
|
|
m_Eng->Engine_Multithread::IterateTS(*m_numTS);
|
|
|
|
else
|
|
|
|
m_Eng->IterateTS(*m_numTS);
|
2010-09-08 05:36:32 +00:00
|
|
|
m_stopBarrier->wait(); //sync all workers after iterations are performed
|
|
|
|
m_startBarrier->wait(); //wait for Base engine to start the iterations again ...
|
|
|
|
}
|
|
|
|
}
|