Allowing more than one master.
This commit is contained in:
parent
9275fbedad
commit
2aa312e43d
3 changed files with 40 additions and 23 deletions
|
|
@ -13,7 +13,7 @@ struct DynamicAssignmentAlgorithm : public AssignmentAlgorithm
|
||||||
public:
|
public:
|
||||||
DynamicAssignmentAlgorithm( int offset, int size )
|
DynamicAssignmentAlgorithm( int offset, int size )
|
||||||
{
|
{
|
||||||
for( int i = 0; offset + i < size; ++i)
|
for( int i = 0; offset + i <= size; ++i)
|
||||||
{
|
{
|
||||||
availableWrk.push_back( offset + i );
|
availableWrk.push_back( offset + i );
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,12 @@ template< typename EOT >
|
||||||
class ParallelApply : public MpiJob< EOT >
|
class ParallelApply : public MpiJob< EOT >
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
using MpiJob<EOT>::comm;
|
||||||
|
using MpiJob<EOT>::data;
|
||||||
|
using MpiJob<EOT>::_masterRank;
|
||||||
|
|
||||||
ParallelApply( eoUF<EOT&, void> & _proc, std::vector<EOT>& _pop, AssignmentAlgorithm & algo ) :
|
ParallelApply( eoUF<EOT&, void> & _proc, std::vector<EOT>& _pop, AssignmentAlgorithm & algo, int _masterRank ) :
|
||||||
MpiJob<EOT>( _pop, algo ),
|
MpiJob<EOT>( _pop, algo, _masterRank ),
|
||||||
func( _proc )
|
func( _proc )
|
||||||
{
|
{
|
||||||
// empty
|
// empty
|
||||||
|
|
@ -20,20 +23,20 @@ class ParallelApply : public MpiJob< EOT >
|
||||||
|
|
||||||
virtual void sendTask( int wrkRank, int index )
|
virtual void sendTask( int wrkRank, int index )
|
||||||
{
|
{
|
||||||
MpiJob<EOT>::comm.send( wrkRank, 1, MpiJob<EOT>::data[ index ] );
|
comm.send( wrkRank, 1, data[ index ] );
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void handleResponse( int wrkRank, int index )
|
virtual void handleResponse( int wrkRank, int index )
|
||||||
{
|
{
|
||||||
MpiJob<EOT>::comm.recv( wrkRank, 1, MpiJob<EOT>::data[ index ] );
|
comm.recv( wrkRank, 1, data[ index ] );
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void processTask( )
|
virtual void processTask( )
|
||||||
{
|
{
|
||||||
EOT ind;
|
EOT ind;
|
||||||
MpiJob<EOT>::comm.recv( 0, 1, ind );
|
comm.recv( _masterRank, 1, ind );
|
||||||
func( ind );
|
func( ind );
|
||||||
MpiJob<EOT>::comm.send( 0, 1, ind );
|
comm.send( _masterRank, 1, ind );
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
|
||||||
|
|
@ -50,10 +50,11 @@ class MpiJob
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
MpiJob( std::vector< EOT > & _data, AssignmentAlgorithm& algo ) :
|
MpiJob( std::vector< EOT > & _data, AssignmentAlgorithm& algo, int masterRank ) :
|
||||||
data( _data ),
|
data( _data ),
|
||||||
comm( MpiNode::comm() ),
|
comm( MpiNode::comm() ),
|
||||||
assignmentAlgo( algo )
|
assignmentAlgo( algo ),
|
||||||
|
_masterRank( masterRank )
|
||||||
{
|
{
|
||||||
// empty
|
// empty
|
||||||
}
|
}
|
||||||
|
|
@ -66,29 +67,33 @@ class MpiJob
|
||||||
|
|
||||||
void master( )
|
void master( )
|
||||||
{
|
{
|
||||||
|
int totalWorkers = assignmentAlgo.size();
|
||||||
|
cout << "[M] Have " << totalWorkers << " workers." << endl;
|
||||||
|
|
||||||
for( int i = 0, size = data.size();
|
for( int i = 0, size = data.size();
|
||||||
i < size;
|
i < size;
|
||||||
++i)
|
++i)
|
||||||
{
|
{
|
||||||
cout << "Beginning loop for i = " << i << endl;
|
cout << "[M] Beginning loop for i = " << i << endl;
|
||||||
int assignee = assignmentAlgo.get( );
|
int assignee = assignmentAlgo.get( );
|
||||||
cout << "Assignee : " << assignee << endl;
|
cout << "[M] Assignee : " << assignee << endl;
|
||||||
while( assignee <= 0 )
|
while( assignee <= 0 )
|
||||||
{
|
{
|
||||||
cout << "Waitin' for node..." << endl;
|
cout << "[M] Waitin' for node..." << endl;
|
||||||
mpi::status status = comm.probe( mpi::any_source, mpi::any_tag );
|
mpi::status status = comm.probe( mpi::any_source, mpi::any_tag );
|
||||||
int wrkRank = status.source();
|
int wrkRank = status.source();
|
||||||
cout << "Node " << wrkRank << " just terminated." << endl;
|
cout << "[M] Node " << wrkRank << " just terminated." << endl;
|
||||||
handleResponse( wrkRank, assignedTasks[ wrkRank ] );
|
handleResponse( wrkRank, assignedTasks[ wrkRank ] );
|
||||||
assignmentAlgo.confirm( wrkRank );
|
assignmentAlgo.confirm( wrkRank );
|
||||||
assignee = assignmentAlgo.get( );
|
assignee = assignmentAlgo.get( );
|
||||||
}
|
}
|
||||||
cout << "Assignee found : " << assignee << endl;
|
cout << "[M] Assignee found : " << assignee << endl;
|
||||||
assignedTasks[ assignee ] = i;
|
assignedTasks[ assignee ] = i;
|
||||||
comm.send( assignee, EoMpi::Channel::Commands, EoMpi::Message::Continue );
|
comm.send( assignee, EoMpi::Channel::Commands, EoMpi::Message::Continue );
|
||||||
sendTask( assignee, i );
|
sendTask( assignee, i );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cout << "[M] Frees all the idle." << endl;
|
||||||
// frees all the idle workers
|
// frees all the idle workers
|
||||||
int idle;
|
int idle;
|
||||||
vector<int> idles;
|
vector<int> idles;
|
||||||
|
|
@ -102,9 +107,9 @@ class MpiJob
|
||||||
assignmentAlgo.confirm( idles[i] );
|
assignmentAlgo.confirm( idles[i] );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cout << "[M] Waits for all responses." << endl;
|
||||||
// wait for all responses
|
// wait for all responses
|
||||||
int wrkNb = comm.size() - 1;
|
while( assignmentAlgo.size() != totalWorkers )
|
||||||
while( assignmentAlgo.size() != wrkNb )
|
|
||||||
{
|
{
|
||||||
mpi::status status = comm.probe( mpi::any_source, mpi::any_tag );
|
mpi::status status = comm.probe( mpi::any_source, mpi::any_tag );
|
||||||
int wrkRank = status.source();
|
int wrkRank = status.source();
|
||||||
|
|
@ -112,6 +117,8 @@ class MpiJob
|
||||||
comm.send( wrkRank, EoMpi::Channel::Commands, EoMpi::Message::Finish );
|
comm.send( wrkRank, EoMpi::Channel::Commands, EoMpi::Message::Finish );
|
||||||
assignmentAlgo.confirm( wrkRank );
|
assignmentAlgo.confirm( wrkRank );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cout << "[M] Leaving master task." << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void worker( )
|
void worker( )
|
||||||
|
|
@ -119,34 +126,41 @@ class MpiJob
|
||||||
int order;
|
int order;
|
||||||
while( true )
|
while( true )
|
||||||
{
|
{
|
||||||
comm.recv( 0, EoMpi::Channel::Commands, order );
|
cout << "[W] Waiting for an order..." << std::endl;
|
||||||
|
comm.recv( _masterRank, EoMpi::Channel::Commands, order );
|
||||||
if ( order == EoMpi::Message::Finish )
|
if ( order == EoMpi::Message::Finish )
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
|
cout << "[W] Processing task..." << std::endl;
|
||||||
processTask( );
|
processTask( );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int masterRank()
|
||||||
|
{
|
||||||
|
return _masterRank;
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
std::vector<EOT> & data;
|
std::vector<EOT> & data;
|
||||||
std::map< int /* worker rank */, int /* index in vector */> assignedTasks;
|
std::map< int /* worker rank */, int /* index in vector */> assignedTasks;
|
||||||
AssignmentAlgorithm& assignmentAlgo;
|
AssignmentAlgorithm& assignmentAlgo;
|
||||||
mpi::communicator& comm;
|
mpi::communicator& comm;
|
||||||
|
int _masterRank;
|
||||||
};
|
};
|
||||||
|
|
||||||
template< class EOT >
|
template< class EOT >
|
||||||
class Role
|
class Role
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Role( MpiJob<EOT> & job, bool master ) :
|
Role( MpiJob<EOT> & job ) :
|
||||||
_job( job ),
|
_job( job )
|
||||||
_master( master )
|
|
||||||
{
|
{
|
||||||
// empty
|
_master = job.masterRank() == MpiNode::comm().rank();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool master()
|
bool master()
|
||||||
|
|
@ -156,7 +170,7 @@ class Role
|
||||||
|
|
||||||
virtual void run( )
|
virtual void run( )
|
||||||
{
|
{
|
||||||
if( _master )
|
if( MpiNode::comm().rank() == _job.masterRank() )
|
||||||
{
|
{
|
||||||
_job.master( );
|
_job.master( );
|
||||||
} else
|
} else
|
||||||
|
|
@ -171,7 +185,7 @@ class Role
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool _master;
|
|
||||||
MpiJob<EOT> & _job;
|
MpiJob<EOT> & _job;
|
||||||
|
bool _master;
|
||||||
};
|
};
|
||||||
# endif // __EO_MPI_H__
|
# endif // __EO_MPI_H__
|
||||||
|
|
|
||||||
Reference in a new issue