ParallelApply can now handle many data at a time.

This commit is contained in:
Benjamin Bouvier 2012-06-25 13:41:48 +02:00
commit f4d8b43f7d
3 changed files with 57 additions and 16 deletions

View file

@ -6,39 +6,76 @@
# include <eoFunctor.h> # include <eoFunctor.h>
# include <vector> # include <vector>
template< typename EOT > template< typename EOT >
class ParallelApply : public MpiJob class ParallelApply : public MpiJob
{ {
private:
struct ParallelApplyAssignment
{
int index;
int size;
};
public: public:
ParallelApply( eoUF<EOT&, void> & _proc, std::vector<EOT>& _pop, AssignmentAlgorithm & algo, int _masterRank ) : ParallelApply(
eoUF<EOT&, void> & _proc,
std::vector<EOT>& _pop,
AssignmentAlgorithm & algo,
int _masterRank,
int _packetSize = 1
) :
MpiJob( algo, _masterRank ), MpiJob( algo, _masterRank ),
func( _proc ), func( _proc ),
index( 0 ), index( 0 ),
size( _pop.size() ), size( _pop.size() ),
data( _pop ) data( _pop ),
packetSize( _packetSize )
{ {
// empty tempArray = new EOT[ packetSize ];
}
~ParallelApply()
{
delete [] tempArray;
} }
virtual void sendTask( int wrkRank ) virtual void sendTask( int wrkRank )
{ {
assignedTasks[ wrkRank ] = index; int futureIndex;
comm.send( wrkRank, 1, data[ index ] );
++index; if( index + packetSize < size )
{
futureIndex = index + packetSize;
} else {
futureIndex = size;
}
int sentSize = futureIndex - index ;
comm.send( wrkRank, 1, sentSize );
assignedTasks[ wrkRank ].index = index;
assignedTasks[ wrkRank ].size = sentSize;
comm.send( wrkRank, 1, &data[ index ] , sentSize );
index = futureIndex;
} }
virtual void handleResponse( int wrkRank ) virtual void handleResponse( int wrkRank )
{ {
comm.recv( wrkRank, 1, data[ assignedTasks[ wrkRank ] ] ); comm.recv( wrkRank, 1, &data[ assignedTasks[wrkRank].index ], assignedTasks[wrkRank].size );
} }
virtual void processTask( ) virtual void processTask( )
{ {
EOT ind; int recvSize;
comm.recv( masterRank, 1, ind ); comm.recv( masterRank, 1, recvSize );
func( ind ); comm.recv( masterRank, 1, tempArray, recvSize );
comm.send( masterRank, 1, ind ); for( int i = 0; i < recvSize ; ++i )
{
func( tempArray[ i ] );
}
comm.send( masterRank, 1, tempArray, recvSize );
} }
bool isFinished() bool isFinished()
@ -51,7 +88,10 @@ class ParallelApply : public MpiJob
eoUF<EOT&, void>& func; eoUF<EOT&, void>& func;
int index; int index;
int size; int size;
std::map< int /* worker rank */, int /* index in vector */> assignedTasks; std::map< int /* worker rank */, ParallelApplyAssignment /* min indexes in vector */> assignedTasks;
int packetSize;
EOT* tempArray;
}; };
# endif // __EO_PARALLEL_APPLY_H__ # endif // __EO_PARALLEL_APPLY_H__

View file

@ -10,8 +10,8 @@ using namespace std;
// Role map // Role map
// 0 : general master // 0 : general master
// 1 : worker of general job, master of subjob // 1, 2 : worker of general job, master of subjob
// 2 and more : workers of subjob // 3 to 7 : workers of subjob
struct SubWork: public eoUF< int&, void > struct SubWork: public eoUF< int&, void >
{ {

View file

@ -24,8 +24,9 @@ struct Test
// These tests require at least 3 processes to be launched. // These tests require at least 3 processes to be launched.
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
eo::log << eo::setlevel( eo::debug ); // eo::log << eo::setlevel( eo::debug );
bool launchOnlyOne = true; bool launchOnlyOne = false; // Set this to true if you wanna launch only the first test.
MpiNode::init( argc, argv ); MpiNode::init( argc, argv );
vector<int> v; vector<int> v;