Cleaner version of functional parallel job.
This commit is contained in:
parent
d05cbfd60d
commit
ff61676fb7
3 changed files with 151 additions and 104 deletions
|
|
@ -33,42 +33,91 @@ namespace eo
|
|||
const int Finish = 1;
|
||||
}
|
||||
|
||||
class SendTaskFunction : public eoUF<int, void>
|
||||
template< typename JobData, typename Wrapped >
|
||||
struct SharedDataFunction
|
||||
{
|
||||
SharedDataFunction( Wrapped * w )
|
||||
{
|
||||
wrapped = w;
|
||||
}
|
||||
|
||||
void data( JobData* _d )
|
||||
{
|
||||
d = _d;
|
||||
}
|
||||
|
||||
protected:
|
||||
JobData* d;
|
||||
Wrapped* wrapped;
|
||||
};
|
||||
|
||||
template< typename JobData >
|
||||
struct SendTaskFunction : public eoUF<int, void>, public SharedDataFunction< JobData, SendTaskFunction<JobData> >
|
||||
{
|
||||
public:
|
||||
|
||||
SendTaskFunction( SendTaskFunction<JobData>* w = 0 ) : SharedDataFunction<JobData, SendTaskFunction<JobData> >( w )
|
||||
{
|
||||
// empty
|
||||
}
|
||||
|
||||
virtual ~SendTaskFunction() {}
|
||||
};
|
||||
|
||||
class HandleResponseFunction : public eoUF<int, void>
|
||||
template< typename JobData >
|
||||
struct HandleResponseFunction : public eoUF<int, void>, public SharedDataFunction< JobData, HandleResponseFunction<JobData> >
|
||||
{
|
||||
public:
|
||||
|
||||
HandleResponseFunction( HandleResponseFunction<JobData>* w = 0 ) : SharedDataFunction<JobData, HandleResponseFunction<JobData> >( w )
|
||||
{
|
||||
// empty
|
||||
}
|
||||
|
||||
virtual ~HandleResponseFunction() {}
|
||||
};
|
||||
|
||||
class ProcessTaskFunction : public eoF<void>
|
||||
template< typename JobData >
|
||||
struct ProcessTaskFunction : public eoF<void>, public SharedDataFunction< JobData, ProcessTaskFunction<JobData> >
|
||||
{
|
||||
public:
|
||||
|
||||
ProcessTaskFunction( ProcessTaskFunction<JobData>* w = 0 ) : SharedDataFunction<JobData, ProcessTaskFunction<JobData> >( w )
|
||||
{
|
||||
// empty
|
||||
}
|
||||
|
||||
virtual ~ProcessTaskFunction() {}
|
||||
};
|
||||
|
||||
class IsFinishedFunction : public eoF<bool>
|
||||
template< typename JobData >
|
||||
struct IsFinishedFunction : public eoF<bool>, public SharedDataFunction< JobData, IsFinishedFunction<JobData> >
|
||||
{
|
||||
public:
|
||||
|
||||
IsFinishedFunction( IsFinishedFunction<JobData>* w = 0 ) : SharedDataFunction<JobData, IsFinishedFunction<JobData> >( w )
|
||||
{
|
||||
// empty
|
||||
}
|
||||
|
||||
virtual ~IsFinishedFunction() {}
|
||||
};
|
||||
|
||||
template< typename JobData >
|
||||
struct JobStore
|
||||
{
|
||||
virtual SendTaskFunction & sendTask() const = 0;
|
||||
virtual HandleResponseFunction & handleResponse() const = 0;
|
||||
virtual ProcessTaskFunction & processTask() const = 0;
|
||||
virtual IsFinishedFunction & isFinished() const = 0;
|
||||
virtual SendTaskFunction<JobData> & sendTask() const = 0;
|
||||
virtual HandleResponseFunction<JobData> & handleResponse() const = 0;
|
||||
virtual ProcessTaskFunction<JobData> & processTask() const = 0;
|
||||
virtual IsFinishedFunction<JobData> & isFinished() const = 0;
|
||||
virtual JobData* data() = 0;
|
||||
};
|
||||
|
||||
template< class JobData >
|
||||
class Job
|
||||
{
|
||||
public:
|
||||
Job( AssignmentAlgorithm& _algo, int _masterRank, const JobStore & store ) :
|
||||
Job( AssignmentAlgorithm& _algo, int _masterRank, JobStore<JobData> & store ) :
|
||||
// Job( AssignmentAlgorithm& _algo, int _masterRank, long maxTime = 0 ) :
|
||||
assignmentAlgo( _algo ),
|
||||
comm( Node::comm() ),
|
||||
|
|
@ -81,6 +130,11 @@ namespace eo
|
|||
isFinished( store.isFinished() )
|
||||
{
|
||||
_isMaster = Node::comm().rank() == _masterRank;
|
||||
|
||||
sendTask.data( store.data() );
|
||||
handleResponse.data( store.data() );
|
||||
processTask.data( store.data() );
|
||||
isFinished.data( store.data() );
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -94,10 +148,10 @@ namespace eo
|
|||
|
||||
protected:
|
||||
|
||||
SendTaskFunction & sendTask;
|
||||
HandleResponseFunction & handleResponse;
|
||||
ProcessTaskFunction & processTask;
|
||||
IsFinishedFunction & isFinished;
|
||||
SendTaskFunction<JobData> & sendTask;
|
||||
HandleResponseFunction<JobData> & handleResponse;
|
||||
ProcessTaskFunction<JobData> & processTask;
|
||||
IsFinishedFunction<JobData> & isFinished;
|
||||
|
||||
void master( )
|
||||
{
|
||||
|
|
|
|||
|
|
@ -17,79 +17,29 @@ namespace eo
|
|||
};
|
||||
|
||||
template<class EOT>
|
||||
class SendTaskParallelApply;
|
||||
|
||||
template<class EOT>
|
||||
class HandleResponseParallelApply;
|
||||
|
||||
template<class EOT>
|
||||
class ProcessTaskParallelApply;
|
||||
|
||||
template<class EOT>
|
||||
class IsFinishedParallelApply;
|
||||
|
||||
template<class EOT>
|
||||
class ParallelApply;
|
||||
|
||||
template< class EOT >
|
||||
class BaseParallelApply
|
||||
struct JobData
|
||||
{
|
||||
public:
|
||||
void owner(ParallelApply<EOT> * job)
|
||||
{
|
||||
j = job;
|
||||
}
|
||||
|
||||
protected:
|
||||
ParallelApply<EOT> * j;
|
||||
};
|
||||
|
||||
template< typename EOT >
|
||||
class ParallelApply : public Job
|
||||
{
|
||||
friend class SendTaskParallelApply<EOT>;
|
||||
friend class HandleResponseParallelApply<EOT>;
|
||||
friend class ProcessTaskParallelApply<EOT>;
|
||||
friend class IsFinishedParallelApply<EOT>;
|
||||
|
||||
public:
|
||||
|
||||
ParallelApply(
|
||||
JobData(
|
||||
eoUF<EOT&, void> & _proc,
|
||||
std::vector<EOT>& _pop,
|
||||
AssignmentAlgorithm & algo,
|
||||
int _masterRank,
|
||||
const JobStore& store,
|
||||
// long _maxTime = 0,
|
||||
int _packetSize = 1
|
||||
) :
|
||||
Job( algo, _masterRank, store ),
|
||||
// Job( algo, _masterRank, _maxTime ),
|
||||
func( _proc ),
|
||||
data( _pop ),
|
||||
packetSize( _packetSize ),
|
||||
index( 0 ),
|
||||
size( _pop.size() )
|
||||
int _packetSize
|
||||
) :
|
||||
data( _pop ), func( _proc ), index( 0 ), size( _pop.size() ), packetSize( _packetSize ), masterRank( _masterRank ), comm( Node::comm() )
|
||||
{
|
||||
if ( _packetSize <= 0 )
|
||||
{
|
||||
throw std::runtime_error("Packet size should not be negative.");
|
||||
}
|
||||
tempArray = new EOT [ _packetSize ];
|
||||
|
||||
dynamic_cast< BaseParallelApply<EOT>& >( sendTask ).owner( this );
|
||||
dynamic_cast< BaseParallelApply<EOT>& >( handleResponse ).owner( this );
|
||||
dynamic_cast< BaseParallelApply<EOT>& >( processTask ).owner( this );
|
||||
dynamic_cast< BaseParallelApply<EOT>& >( isFinished ).owner( this );
|
||||
tempArray = new EOT[ _packetSize ];
|
||||
}
|
||||
|
||||
~ParallelApply()
|
||||
~JobData()
|
||||
{
|
||||
delete [] tempArray;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
std::vector<EOT> & data;
|
||||
eoUF<EOT&, void> & func;
|
||||
int index;
|
||||
|
|
@ -98,92 +48,131 @@ namespace eo
|
|||
int packetSize;
|
||||
EOT* tempArray;
|
||||
|
||||
// bmpi::communicator& comm;
|
||||
int masterRank;
|
||||
bmpi::communicator& comm;
|
||||
};
|
||||
|
||||
template< class EOT >
|
||||
class SendTaskParallelApply : public SendTaskFunction, public BaseParallelApply<EOT>
|
||||
/*
|
||||
template< typename EOT >
|
||||
class ParallelApply : public Job< JobData<EOT> >
|
||||
{
|
||||
public:
|
||||
using BaseParallelApply<EOT>::j;
|
||||
|
||||
ParallelApply(
|
||||
// eoUF<EOT&, void> & _proc,
|
||||
// std::vector<EOT>& _pop,
|
||||
AssignmentAlgorithm & algo,
|
||||
int _masterRank,
|
||||
const JobStore< JobData<EOT> >& store
|
||||
// long _maxTime = 0,
|
||||
// int _packetSize = 1
|
||||
) :
|
||||
Job( algo, _masterRank, store )
|
||||
// Job( algo, _masterRank, _maxTime ),
|
||||
func( _proc ),
|
||||
data( _pop ),
|
||||
packetSize( _packetSize ),
|
||||
index( 0 ),
|
||||
size( _pop.size() )
|
||||
{
|
||||
// empty
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
// bmpi::communicator& comm;
|
||||
};
|
||||
*/
|
||||
|
||||
template< class EOT >
|
||||
class SendTaskParallelApply : public SendTaskFunction< JobData<EOT> >
|
||||
{
|
||||
public:
|
||||
using SendTaskFunction< JobData<EOT> >::d;
|
||||
|
||||
// futureIndex, index, packetSize, size, comm, assignedTasks, data
|
||||
void operator()(int wrkRank)
|
||||
{
|
||||
int futureIndex;
|
||||
|
||||
if( j->index + j->packetSize < j->size )
|
||||
if( d->index + d->packetSize < d->size )
|
||||
{
|
||||
futureIndex = j->index + j->packetSize;
|
||||
futureIndex = d->index + d->packetSize;
|
||||
} else {
|
||||
futureIndex = j->size;
|
||||
futureIndex = d->size;
|
||||
}
|
||||
|
||||
int sentSize = futureIndex - j->index ;
|
||||
int sentSize = futureIndex - d->index ;
|
||||
|
||||
j->comm.send( wrkRank, 1, sentSize );
|
||||
d->comm.send( wrkRank, 1, sentSize );
|
||||
|
||||
eo::log << eo::progress << "Evaluating individual " << j->index << std::endl;
|
||||
eo::log << eo::progress << "Evaluating individual " << d->index << std::endl;
|
||||
|
||||
j->assignedTasks[ wrkRank ].index = j->index;
|
||||
j->assignedTasks[ wrkRank ].size = sentSize;
|
||||
d->assignedTasks[ wrkRank ].index = d->index;
|
||||
d->assignedTasks[ wrkRank ].size = sentSize;
|
||||
|
||||
j->comm.send( wrkRank, 1, & ( (j->data)[ j->index ] ) , sentSize );
|
||||
j->index = futureIndex;
|
||||
d->comm.send( wrkRank, 1, & ( (d->data)[ d->index ] ) , sentSize );
|
||||
d->index = futureIndex;
|
||||
}
|
||||
};
|
||||
|
||||
template< class EOT >
|
||||
class HandleResponseParallelApply : public HandleResponseFunction, public BaseParallelApply<EOT>
|
||||
class HandleResponseParallelApply : public HandleResponseFunction< JobData<EOT> >
|
||||
{
|
||||
public:
|
||||
using BaseParallelApply<EOT>::j;
|
||||
using HandleResponseFunction< JobData<EOT> >::d;
|
||||
|
||||
void operator()(int wrkRank)
|
||||
{
|
||||
j->comm.recv( wrkRank, 1, & (j->data[ j->assignedTasks[wrkRank].index ] ), j->assignedTasks[wrkRank].size );
|
||||
d->comm.recv( wrkRank, 1, & (d->data[ d->assignedTasks[wrkRank].index ] ), d->assignedTasks[wrkRank].size );
|
||||
}
|
||||
};
|
||||
|
||||
template< class EOT >
|
||||
class ProcessTaskParallelApply : public ProcessTaskFunction, public BaseParallelApply<EOT>
|
||||
class ProcessTaskParallelApply : public ProcessTaskFunction< JobData<EOT> >
|
||||
{
|
||||
public:
|
||||
using BaseParallelApply<EOT>::j;
|
||||
using ProcessTaskFunction< JobData<EOT> >::d;
|
||||
|
||||
void operator()()
|
||||
{
|
||||
int recvSize;
|
||||
|
||||
j->comm.recv( j->masterRank, 1, recvSize );
|
||||
j->comm.recv( j->masterRank, 1, j->tempArray, recvSize );
|
||||
d->comm.recv( d->masterRank, 1, recvSize );
|
||||
d->comm.recv( d->masterRank, 1, d->tempArray, recvSize );
|
||||
timerStat.start("worker_processes");
|
||||
for( int i = 0; i < recvSize ; ++i )
|
||||
{
|
||||
j->func( j->tempArray[ i ] );
|
||||
d->func( d->tempArray[ i ] );
|
||||
}
|
||||
timerStat.stop("worker_processes");
|
||||
j->comm.send( j->masterRank, 1, j->tempArray, recvSize );
|
||||
d->comm.send( d->masterRank, 1, d->tempArray, recvSize );
|
||||
}
|
||||
};
|
||||
|
||||
template< class EOT >
|
||||
class IsFinishedParallelApply : public IsFinishedFunction, public BaseParallelApply<EOT>
|
||||
class IsFinishedParallelApply : public IsFinishedFunction< JobData<EOT> >
|
||||
{
|
||||
public:
|
||||
|
||||
using BaseParallelApply<EOT>::j;
|
||||
using IsFinishedFunction< JobData<EOT> >::d;
|
||||
|
||||
bool operator()()
|
||||
{
|
||||
return j->index == j->size;
|
||||
return d->index == d->size;
|
||||
}
|
||||
};
|
||||
|
||||
template< class EOT >
|
||||
struct ParallelApplyStore : public JobStore
|
||||
struct ParallelApplyStore : public JobStore< JobData<EOT> >
|
||||
{
|
||||
ParallelApplyStore()
|
||||
ParallelApplyStore(
|
||||
eoUF<EOT&, void> & _proc,
|
||||
std::vector<EOT>& _pop,
|
||||
int _masterRank,
|
||||
// long _maxTime = 0,
|
||||
int _packetSize = 1
|
||||
)
|
||||
: j( _proc, _pop, _masterRank, _packetSize )
|
||||
{
|
||||
stpa = new SendTaskParallelApply<EOT>;
|
||||
hrpa = new HandleResponseParallelApply<EOT>;
|
||||
|
|
@ -199,16 +188,20 @@ namespace eo
|
|||
delete ispa;
|
||||
}
|
||||
|
||||
SendTaskFunction& sendTask() const { return *stpa; }
|
||||
HandleResponseFunction& handleResponse() const { return *hrpa; }
|
||||
ProcessTaskFunction& processTask() const { return *ptpa; }
|
||||
IsFinishedFunction& isFinished() const { return *ispa; }
|
||||
SendTaskFunction< JobData<EOT> >& sendTask() const { return *stpa; }
|
||||
HandleResponseFunction< JobData<EOT> >& handleResponse() const { return *hrpa; }
|
||||
ProcessTaskFunction< JobData<EOT> >& processTask() const { return *ptpa; }
|
||||
IsFinishedFunction< JobData<EOT> >& isFinished() const { return *ispa; }
|
||||
|
||||
JobData<EOT>* data() { return &j; }
|
||||
|
||||
protected:
|
||||
SendTaskParallelApply<EOT>* stpa;
|
||||
HandleResponseParallelApply<EOT>* hrpa;
|
||||
ProcessTaskParallelApply<EOT>* ptpa;
|
||||
IsFinishedParallelApply<EOT>* ispa;
|
||||
|
||||
JobData<EOT> j;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue