First version of functional job
This commit is contained in:
parent
4675abaa24
commit
d05cbfd60d
3 changed files with 132 additions and 137 deletions
|
|
@ -33,54 +33,42 @@ namespace eo
|
||||||
const int Finish = 1;
|
const int Finish = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class Data>
|
class SendTaskFunction : public eoUF<int, void>
|
||||||
struct SharedDataFunction
|
|
||||||
{
|
|
||||||
void data( Data & _d ) { d = _d; }
|
|
||||||
|
|
||||||
protected:
|
|
||||||
Data d;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<class Data>
|
|
||||||
struct SendTaskFunction : public eoUF<int, void>, public SharedDataFunction<Data>
|
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
virtual ~SendTaskFunction() {}
|
virtual ~SendTaskFunction() {}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class Data>
|
class HandleResponseFunction : public eoUF<int, void>
|
||||||
struct HandleResponseFunction : public eoUF<int, void>, public SharedDataFunction<Data>
|
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
virtual ~HandleResponseFunction() {}
|
virtual ~HandleResponseFunction() {}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class Data>
|
class ProcessTaskFunction : public eoF<void>
|
||||||
struct ProcessTaskFunction : public eoF<void>, public SharedDataFunction<Data>
|
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
virtual ~ProcessTaskFunction() {}
|
virtual ~ProcessTaskFunction() {}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class Data>
|
class IsFinishedFunction : public eoF<bool>
|
||||||
struct IsFinishedFunction : public eoF<bool>, public SharedDataFunction<Data>
|
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
virtual ~IsFinishedFunction() {}
|
virtual ~IsFinishedFunction() {}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class Data>
|
|
||||||
struct JobStore
|
struct JobStore
|
||||||
{
|
{
|
||||||
virtual SendTaskFunction<Data> & sendTask() = 0;
|
virtual SendTaskFunction & sendTask() const = 0;
|
||||||
virtual HandleResponseFunction<Data> & handleResponse() = 0;
|
virtual HandleResponseFunction & handleResponse() const = 0;
|
||||||
virtual ProcessTaskFunction<Data> & processTask() = 0;
|
virtual ProcessTaskFunction & processTask() const = 0;
|
||||||
virtual IsFinishedFunction<Data> & isFinished() = 0;
|
virtual IsFinishedFunction & isFinished() const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class Data>
|
|
||||||
class Job
|
class Job
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
Job( AssignmentAlgorithm& _algo, int _masterRank, const JobStore & store ) :
|
||||||
Job( AssignmentAlgorithm& _algo, int _masterRank, JobStore<Data> store ) :
|
|
||||||
// Job( AssignmentAlgorithm& _algo, int _masterRank, long maxTime = 0 ) :
|
// Job( AssignmentAlgorithm& _algo, int _masterRank, long maxTime = 0 ) :
|
||||||
assignmentAlgo( _algo ),
|
assignmentAlgo( _algo ),
|
||||||
comm( Node::comm() ),
|
comm( Node::comm() ),
|
||||||
|
|
@ -106,10 +94,10 @@ namespace eo
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
SendTaskFunction<Data> & sendTask;
|
SendTaskFunction & sendTask;
|
||||||
HandleResponseFunction<Data> & handleResponse;
|
HandleResponseFunction & handleResponse;
|
||||||
ProcessTaskFunction<Data> & processTask;
|
ProcessTaskFunction & processTask;
|
||||||
IsFinishedFunction<Data> & isFinished;
|
IsFinishedFunction & isFinished;
|
||||||
|
|
||||||
void master( )
|
void master( )
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -17,145 +17,178 @@ namespace eo
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class EOT>
|
template<class EOT>
|
||||||
struct ParallelApplyData
|
class SendTaskParallelApply;
|
||||||
{
|
|
||||||
ParallelApplyData() {}
|
|
||||||
|
|
||||||
ParallelApplyData(
|
template<class EOT>
|
||||||
eoUF<EOT&, void> & _proc,
|
class HandleResponseParallelApply;
|
||||||
std::vector<EOT>& _pop,
|
|
||||||
int _masterRank,
|
template<class EOT>
|
||||||
int _packetSize
|
class ProcessTaskParallelApply;
|
||||||
) :
|
|
||||||
func( _proc ),
|
template<class EOT>
|
||||||
data( _pop ),
|
class IsFinishedParallelApply;
|
||||||
index( 0 ),
|
|
||||||
size( _pop.size() ),
|
template<class EOT>
|
||||||
packetSize( _packetSize ),
|
class ParallelApply;
|
||||||
// job
|
|
||||||
masterRank( _masterRank ),
|
template< class EOT >
|
||||||
comm( Node::comm() )
|
class BaseParallelApply
|
||||||
{
|
{
|
||||||
tempArray = new EOT[ _packetSize ];
|
public:
|
||||||
|
void owner(ParallelApply<EOT> * job)
|
||||||
|
{
|
||||||
|
j = job;
|
||||||
}
|
}
|
||||||
|
|
||||||
~ParallelApplyData()
|
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(
|
||||||
|
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() )
|
||||||
|
{
|
||||||
|
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 );
|
||||||
|
}
|
||||||
|
|
||||||
|
~ParallelApply()
|
||||||
{
|
{
|
||||||
delete [] tempArray;
|
delete [] tempArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
std::vector<EOT> & data;
|
std::vector<EOT> & data;
|
||||||
eoUF<EOT&, void> & func;
|
eoUF<EOT&, void> & func;
|
||||||
int index;
|
int index;
|
||||||
int size;
|
int size;
|
||||||
std::map< int /* worker rank */, ParallelApplyAssignment /* min indexes in vector */> assignedTasks;
|
std::map< int /* worker rank */, ParallelApplyAssignment /* min indexes in vector */> assignedTasks;
|
||||||
|
|
||||||
int packetSize;
|
int packetSize;
|
||||||
EOT* tempArray;
|
EOT* tempArray;
|
||||||
|
|
||||||
int masterRank;
|
// bmpi::communicator& comm;
|
||||||
bmpi::communicator& comm;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class Data>
|
template< class EOT >
|
||||||
struct SendTaskParallelApply : public SendTaskFunction< Data >
|
class SendTaskParallelApply : public SendTaskFunction, public BaseParallelApply<EOT>
|
||||||
{
|
{
|
||||||
SendTaskParallelApply( Data & _d )
|
public:
|
||||||
{
|
using BaseParallelApply<EOT>::j;
|
||||||
data( _d );
|
|
||||||
}
|
|
||||||
|
|
||||||
using SharedDataFunction< Data >::d;
|
|
||||||
|
|
||||||
// futureIndex, index, packetSize, size, comm, assignedTasks, data
|
// futureIndex, index, packetSize, size, comm, assignedTasks, data
|
||||||
void operator()(int wrkRank)
|
void operator()(int wrkRank)
|
||||||
{
|
{
|
||||||
int futureIndex;
|
int futureIndex;
|
||||||
|
|
||||||
if( d.index + d.packetSize < d.size )
|
if( j->index + j->packetSize < j->size )
|
||||||
{
|
{
|
||||||
futureIndex = d.index + d.packetSize;
|
futureIndex = j->index + j->packetSize;
|
||||||
} else {
|
} else {
|
||||||
futureIndex = d.size;
|
futureIndex = j->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sentSize = futureIndex - d.index ;
|
int sentSize = futureIndex - j->index ;
|
||||||
|
|
||||||
d.comm.send( wrkRank, 1, sentSize );
|
j->comm.send( wrkRank, 1, sentSize );
|
||||||
|
|
||||||
eo::log << eo::progress << "Evaluating individual " << d.index << std::endl;
|
eo::log << eo::progress << "Evaluating individual " << j->index << std::endl;
|
||||||
|
|
||||||
d.assignedTasks[ wrkRank ].index = d.index;
|
j->assignedTasks[ wrkRank ].index = j->index;
|
||||||
d.assignedTasks[ wrkRank ].size = sentSize;
|
j->assignedTasks[ wrkRank ].size = sentSize;
|
||||||
|
|
||||||
d.comm.send( wrkRank, 1, & (d.data[ index ]) , sentSize );
|
j->comm.send( wrkRank, 1, & ( (j->data)[ j->index ] ) , sentSize );
|
||||||
d.index = futureIndex;
|
j->index = futureIndex;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class Data>
|
template< class EOT >
|
||||||
struct HandleResponseParallelApply : public HandleResponseFunction< Data >
|
class HandleResponseParallelApply : public HandleResponseFunction, public BaseParallelApply<EOT>
|
||||||
{
|
{
|
||||||
HandleResponseParallelApply( Data & _d )
|
public:
|
||||||
{
|
using BaseParallelApply<EOT>::j;
|
||||||
data( _d );
|
|
||||||
}
|
|
||||||
|
|
||||||
using SharedDataFunction< Data >::d;
|
|
||||||
void operator()(int wrkRank)
|
void operator()(int wrkRank)
|
||||||
{
|
{
|
||||||
d.comm.recv( wrkRank, 1, & (d.data[ d.assignedTasks[wrkRank].index ] ), d.assignedTasks[wrkRank].size );
|
j->comm.recv( wrkRank, 1, & (j->data[ j->assignedTasks[wrkRank].index ] ), j->assignedTasks[wrkRank].size );
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class Data>
|
template< class EOT >
|
||||||
struct ProcessTaskParallelApply : public ProcessTaskFunction< Data >
|
class ProcessTaskParallelApply : public ProcessTaskFunction, public BaseParallelApply<EOT>
|
||||||
{
|
{
|
||||||
ProcessTaskParallelApply( Data & _d )
|
public:
|
||||||
{
|
using BaseParallelApply<EOT>::j;
|
||||||
data( _d );
|
|
||||||
}
|
|
||||||
|
|
||||||
using SharedDataFunction< Data >::d;
|
|
||||||
void operator()()
|
void operator()()
|
||||||
{
|
{
|
||||||
int recvSize;
|
int recvSize;
|
||||||
d.comm.recv( d.masterRank, 1, recvSize );
|
|
||||||
d.comm.recv( d.masterRank, 1, d.tempArray, recvSize );
|
j->comm.recv( j->masterRank, 1, recvSize );
|
||||||
|
j->comm.recv( j->masterRank, 1, j->tempArray, recvSize );
|
||||||
timerStat.start("worker_processes");
|
timerStat.start("worker_processes");
|
||||||
for( int i = 0; i < recvSize ; ++i )
|
for( int i = 0; i < recvSize ; ++i )
|
||||||
{
|
{
|
||||||
d.func( d.tempArray[ i ] );
|
j->func( j->tempArray[ i ] );
|
||||||
}
|
}
|
||||||
timerStat.stop("worker_processes");
|
timerStat.stop("worker_processes");
|
||||||
d.comm.send( d.masterRank, 1, d.tempArray, recvSize );
|
j->comm.send( j->masterRank, 1, j->tempArray, recvSize );
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class Data>
|
template< class EOT >
|
||||||
struct IsFinishedParallelApply : public IsFinishedFunction< Data >
|
class IsFinishedParallelApply : public IsFinishedFunction, public BaseParallelApply<EOT>
|
||||||
{
|
{
|
||||||
IsFinishedParallelApply( Data & _d )
|
public:
|
||||||
{
|
|
||||||
data( _d );
|
using BaseParallelApply<EOT>::j;
|
||||||
}
|
|
||||||
|
|
||||||
using SharedDataFunction< Data >::d;
|
|
||||||
bool operator()()
|
bool operator()()
|
||||||
{
|
{
|
||||||
return d.index == d.size;
|
return j->index == j->size;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template< typename Data >
|
template< class EOT >
|
||||||
struct ParallelApplyStore : public JobStore< Data >
|
struct ParallelApplyStore : public JobStore
|
||||||
{
|
{
|
||||||
ParallelApplyStore( Data & data )
|
ParallelApplyStore()
|
||||||
{
|
{
|
||||||
stpa = new SendTaskParallelApply< Data >( data );
|
stpa = new SendTaskParallelApply<EOT>;
|
||||||
hrpa = new HandleResponseParallelApply< Data >( data );
|
hrpa = new HandleResponseParallelApply<EOT>;
|
||||||
ptpa = new ProcessTaskParallelApply< Data >( data );
|
ptpa = new ProcessTaskParallelApply<EOT>;
|
||||||
ispa = new IsFinishedParallelApply< Data >( data );
|
ispa = new IsFinishedParallelApply<EOT>;
|
||||||
}
|
}
|
||||||
|
|
||||||
~ParallelApplyStore()
|
~ParallelApplyStore()
|
||||||
|
|
@ -166,45 +199,16 @@ namespace eo
|
||||||
delete ispa;
|
delete ispa;
|
||||||
}
|
}
|
||||||
|
|
||||||
SendTaskFunction< Data > & sendTask() { return *stpa; }
|
SendTaskFunction& sendTask() const { return *stpa; }
|
||||||
HandleResponseFunction< Data > & handleResponse() { return *hrpa; }
|
HandleResponseFunction& handleResponse() const { return *hrpa; }
|
||||||
ProcessTaskFunction< Data > & processTask() { return *ptpa; }
|
ProcessTaskFunction& processTask() const { return *ptpa; }
|
||||||
IsFinishedFunction< Data > & isFinished() { return *ispa; }
|
IsFinishedFunction& isFinished() const { return *ispa; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
SendTaskParallelApply< Data >* stpa;
|
SendTaskParallelApply<EOT>* stpa;
|
||||||
HandleResponseParallelApply< Data >* hrpa;
|
HandleResponseParallelApply<EOT>* hrpa;
|
||||||
ProcessTaskParallelApply< Data >* ptpa;
|
ProcessTaskParallelApply<EOT>* ptpa;
|
||||||
IsFinishedParallelApply< Data >* ispa;
|
IsFinishedParallelApply<EOT>* ispa;
|
||||||
};
|
|
||||||
|
|
||||||
template< typename EOT >
|
|
||||||
class ParallelApply : public Job< ParallelApplyData<EOT> >
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
|
|
||||||
ParallelApply(
|
|
||||||
eoUF<EOT&, void> & _proc,
|
|
||||||
std::vector<EOT>& _pop,
|
|
||||||
AssignmentAlgorithm & algo,
|
|
||||||
int _masterRank,
|
|
||||||
// long _maxTime = 0,
|
|
||||||
int _packetSize = 1
|
|
||||||
) :
|
|
||||||
|
|
||||||
Job< ParallelApplyData<EOT> >( algo, _masterRank, ParallelApplyStore< ParallelApplyData<EOT> >( sharedData ) ),
|
|
||||||
// Job( algo, _masterRank, _maxTime ),
|
|
||||||
sharedData( _proc, _pop, _masterRank, _packetSize )
|
|
||||||
|
|
||||||
{
|
|
||||||
if ( _packetSize <= 0 )
|
|
||||||
{
|
|
||||||
throw std::runtime_error("Packet size should not be negative.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
ParallelApplyData<EOT> sharedData;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ struct Test
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
// eo::log << eo::setlevel( eo::debug );
|
// eo::log << eo::setlevel( eo::debug );
|
||||||
|
eo::log << eo::setlevel( eo::quiet );
|
||||||
bool launchOnlyOne = false ; // Set this to true if you wanna launch only the first test.
|
bool launchOnlyOne = false ; // Set this to true if you wanna launch only the first test.
|
||||||
|
|
||||||
Node::init( argc, argv );
|
Node::init( argc, argv );
|
||||||
|
|
@ -45,6 +46,8 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
vector< Test > tests;
|
vector< Test > tests;
|
||||||
|
|
||||||
|
ParallelApplyStore<int> store;
|
||||||
|
|
||||||
const int ALL = Node::comm().size();
|
const int ALL = Node::comm().size();
|
||||||
|
|
||||||
Test tIntervalStatic;
|
Test tIntervalStatic;
|
||||||
|
|
@ -109,7 +112,7 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
for( unsigned int i = 0; i < tests.size(); ++i )
|
for( unsigned int i = 0; i < tests.size(); ++i )
|
||||||
{
|
{
|
||||||
ParallelApply<int> job( plusOneInstance, v, *(tests[i].assign), 0, 3 );
|
ParallelApply<int> job( plusOneInstance, v, *(tests[i].assign), 0, store, 3 );
|
||||||
|
|
||||||
if( job.isMaster() )
|
if( job.isMaster() )
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Reference in a new issue