peo style

git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1120 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
canape 2008-03-12 11:21:25 +00:00
commit ceeaa4b533
5 changed files with 677 additions and 575 deletions

View file

@ -10,31 +10,36 @@
template < typename Type > struct Entity; template < typename Type > struct Entity;
struct AbstractEntity { struct AbstractEntity
{
virtual ~AbstractEntity() {} virtual ~AbstractEntity() {}
template < typename EntityType > operator EntityType& () { template < typename EntityType > operator EntityType& ()
{
return ( dynamic_cast< Entity< EntityType >& >( *this ) ).entity; return ( dynamic_cast< Entity< EntityType >& >( *this ) ).entity;
} }
}; };
struct AbstractFunctor : virtual public AbstractEntity { struct AbstractFunctor : virtual public AbstractEntity
{
virtual ~AbstractFunctor() {} virtual ~AbstractFunctor() {}
virtual void operator()() {} virtual void operator()() {}
}; };
struct AbstractUnaryFunctor : virtual public AbstractEntity { struct AbstractUnaryFunctor : virtual public AbstractEntity
{
virtual ~AbstractUnaryFunctor() {} virtual ~AbstractUnaryFunctor() {}
virtual void operator()( AbstractEntity& dataEntity ) {} virtual void operator()( AbstractEntity& dataEntity ) {}
}; };
struct AbstractBinaryFunctor : virtual public AbstractEntity { struct AbstractBinaryFunctor : virtual public AbstractEntity
{
virtual ~AbstractBinaryFunctor() {} virtual ~AbstractBinaryFunctor() {}
@ -43,19 +48,22 @@ struct AbstractBinaryFunctor : virtual public AbstractEntity {
template < typename EntityType > struct Entity : virtual public AbstractEntity { template < typename EntityType > struct Entity : virtual public AbstractEntity
{
Entity( EntityType& externalEntityRef ) : entity( externalEntityRef ) {} Entity( EntityType& externalEntityRef ) : entity( externalEntityRef ) {}
EntityType& entity; EntityType& entity;
}; };
template < typename FunctorType, typename DataType > struct FunctorEx : public Entity< DataType >, public AbstractFunctor { template < typename FunctorType, typename DataType > struct FunctorEx : public Entity< DataType >, public AbstractFunctor
{
FunctorEx( FunctorType& externalFunctorRef, DataType& externalDataRef ) FunctorEx( FunctorType& externalFunctorRef, DataType& externalDataRef )
: externalFunctor( externalFunctorRef ), Entity< DataType >( externalDataRef ) {} : externalFunctor( externalFunctorRef ), Entity< DataType >( externalDataRef ) {}
void operator()() { void operator()()
{
externalFunctor( Entity< DataType > :: entity ); externalFunctor( Entity< DataType > :: entity );
} }
@ -63,12 +71,14 @@ template < typename FunctorType, typename DataType > struct FunctorEx : public E
FunctorType& externalFunctor; FunctorType& externalFunctor;
}; };
template < typename FunctorType > struct FunctorEx< FunctorType, void > : public Entity< AbstractEntity >, public AbstractFunctor { template < typename FunctorType > struct FunctorEx< FunctorType, void > : public Entity< AbstractEntity >, public AbstractFunctor
{
FunctorEx( FunctorType& externalFunctorRef ) FunctorEx( FunctorType& externalFunctorRef )
: externalFunctor( externalFunctorRef ), Entity< AbstractEntity >( *this ) {} : externalFunctor( externalFunctorRef ), Entity< AbstractEntity >( *this ) {}
void operator()() { void operator()()
{
externalFunctor(); externalFunctor();
} }
@ -77,12 +87,14 @@ template < typename FunctorType > struct FunctorEx< FunctorType, void > : public
}; };
template < typename ReturnType, typename DataType > struct FnFunctorEx template < typename ReturnType, typename DataType > struct FnFunctorEx
: public Entity< DataType >, public AbstractFunctor { : public Entity< DataType >, public AbstractFunctor
{
FnFunctorEx( ReturnType (*externalFunctorRef)( DataType& ), DataType& externalDataRef ) FnFunctorEx( ReturnType (*externalFunctorRef)( DataType& ), DataType& externalDataRef )
: externalFunctor( externalFunctorRef ), Entity< DataType >( externalDataRef ) {} : externalFunctor( externalFunctorRef ), Entity< DataType >( externalDataRef ) {}
void operator()() { void operator()()
{
externalFunctor( Entity< DataType > :: entity ); externalFunctor( Entity< DataType > :: entity );
} }
@ -91,12 +103,14 @@ template < typename ReturnType, typename DataType > struct FnFunctorEx
}; };
template < typename ReturnType > struct FnFunctorEx< ReturnType, void > template < typename ReturnType > struct FnFunctorEx< ReturnType, void >
: public Entity< AbstractEntity >, public AbstractFunctor { : public Entity< AbstractEntity >, public AbstractFunctor
{
FnFunctorEx( ReturnType (*externalFunctorRef)() ) FnFunctorEx( ReturnType (*externalFunctorRef)() )
: externalFunctor( externalFunctorRef ), Entity< AbstractEntity >( *this ) {} : externalFunctor( externalFunctorRef ), Entity< AbstractEntity >( *this ) {}
void operator()() { void operator()()
{
externalFunctor(); externalFunctor();
} }
@ -106,23 +120,28 @@ template < typename ReturnType > struct FnFunctorEx< ReturnType, void >
template < typename FunctorType > struct UnaryFunctor : public Entity< FunctorType >, public AbstractUnaryFunctor { template < typename FunctorType > struct UnaryFunctor : public Entity< FunctorType >, public AbstractUnaryFunctor
{
UnaryFunctor( FunctorType& externalFunctorRef ) : Entity< FunctorType >( externalFunctorRef ) {} UnaryFunctor( FunctorType& externalFunctorRef ) : Entity< FunctorType >( externalFunctorRef ) {}
void operator()( AbstractEntity& dataEntity ) { void operator()( AbstractEntity& dataEntity )
{
Entity< FunctorType > :: entity( dataEntity ); Entity< FunctorType > :: entity( dataEntity );
} }
}; };
template < typename ReturnType, typename DataType > struct UnaryFnFunctor template < typename ReturnType, typename DataType > struct UnaryFnFunctor
: public Entity< AbstractEntity >, public AbstractUnaryFunctor { : public Entity< AbstractEntity >, public AbstractUnaryFunctor
{
UnaryFnFunctor( ReturnType (*externalFnRef)( DataType& ) ) : Entity< AbstractEntity >( *this ), externalFn( externalFnRef ) { UnaryFnFunctor( ReturnType (*externalFnRef)( DataType& ) ) : Entity< AbstractEntity >( *this ), externalFn( externalFnRef )
{
} }
void operator()( AbstractEntity& dataEntity ) { void operator()( AbstractEntity& dataEntity )
{
externalFn( dataEntity ); externalFn( dataEntity );
} }
@ -130,52 +149,66 @@ template < typename ReturnType, typename DataType > struct UnaryFnFunctor
ReturnType (*externalFn)( DataType& ); ReturnType (*externalFn)( DataType& );
}; };
template < typename FunctorType > struct BinaryFunctor : public Entity< FunctorType >, public AbstractBinaryFunctor { template < typename FunctorType > struct BinaryFunctor : public Entity< FunctorType >, public AbstractBinaryFunctor
{
BinaryFunctor( FunctorType& externalFunctorRef ) : Entity< FunctorType >( externalFunctorRef ) {} BinaryFunctor( FunctorType& externalFunctorRef ) : Entity< FunctorType >( externalFunctorRef ) {}
void operator()( AbstractEntity& dataEntityA, AbstractEntity& dataEntityB ) { void operator()( AbstractEntity& dataEntityA, AbstractEntity& dataEntityB )
{
Entity< FunctorType > :: entity( dataEntityA, dataEntityB ); Entity< FunctorType > :: entity( dataEntityA, dataEntityB );
} }
}; };
struct AbstractMsgTransferQueue : virtual public AbstractEntity { struct AbstractMsgTransferQueue : virtual public AbstractEntity
{
virtual ~AbstractMsgTransferQueue() {} virtual ~AbstractMsgTransferQueue() {}
virtual void pushMessage() {} virtual void pushMessage() {}
virtual void popMessage() {} virtual void popMessage() {}
virtual bool empty() { return true; } virtual bool empty()
{
return true;
}
virtual void packMessage() {} virtual void packMessage() {}
virtual void unpackMessage() {} virtual void unpackMessage() {}
}; };
template < typename EntityType > struct MsgTransferQueue : public Entity< EntityType >, public AbstractMsgTransferQueue { template < typename EntityType > struct MsgTransferQueue : public Entity< EntityType >, public AbstractMsgTransferQueue
{
MsgTransferQueue( EntityType& externalDataRef ) MsgTransferQueue( EntityType& externalDataRef )
: Entity< EntityType >( externalDataRef ) { : Entity< EntityType >( externalDataRef )
{
aggregationFunctor = new BinaryFunctor< AssignmentFunctor >( assignmentFunctor ); aggregationFunctor = new BinaryFunctor< AssignmentFunctor >( assignmentFunctor );
} }
template < typename FunctorType > template < typename FunctorType >
MsgTransferQueue( EntityType& externalDataRef, FunctorType& externalFunctorRef ) MsgTransferQueue( EntityType& externalDataRef, FunctorType& externalFunctorRef )
: Entity< EntityType >( externalDataRef ) { : Entity< EntityType >( externalDataRef )
{
aggregationFunctor = new BinaryFunctor< FunctorType >( externalFunctorRef ); aggregationFunctor = new BinaryFunctor< FunctorType >( externalFunctorRef );
} }
~MsgTransferQueue() { delete aggregationFunctor; } ~MsgTransferQueue()
{
delete aggregationFunctor;
}
void pushMessage() { void pushMessage()
{
transferQueue.push( Entity< EntityType > :: entity ); transferQueue.push( Entity< EntityType > :: entity );
} }
void popMessage() { void popMessage()
{
Entity< EntityType > message( transferQueue.front() ); Entity< EntityType > message( transferQueue.front() );
aggregationFunctor->operator()( *this, message ); aggregationFunctor->operator()( *this, message );
@ -183,23 +216,32 @@ template < typename EntityType > struct MsgTransferQueue : public Entity< Entity
transferQueue.pop(); transferQueue.pop();
} }
bool empty() { return transferQueue.empty(); } bool empty()
{
return transferQueue.empty();
}
void packMessage() { void packMessage()
{
pack( transferQueue.front() ); pack( transferQueue.front() );
transferQueue.pop(); transferQueue.pop();
} }
void unpackMessage() { void unpackMessage()
{
EntityType transferredData; EntityType transferredData;
unpack( transferredData ); unpack( transferredData );
transferQueue.push( transferredData ); transferQueue.push( transferredData );
} }
struct AssignmentFunctor { struct AssignmentFunctor
void operator()( EntityType& A, EntityType& B ) { A = B; } {
void operator()( EntityType& A, EntityType& B )
{
A = B;
}
} assignmentFunctor; } assignmentFunctor;
std::queue< EntityType > transferQueue; std::queue< EntityType > transferQueue;

View file

@ -16,7 +16,8 @@
#include "core/peo_debug.h" #include "core/peo_debug.h"
class peoAsyncDataTransfer : public Cooperative, public eoUpdater { class peoAsyncDataTransfer : public Cooperative, public eoUpdater
{
public: public:
@ -80,7 +81,8 @@ class peoAsyncDataTransfer : public Cooperative, public eoUpdater {
__topology.add( *this ); __topology.add( *this );
} }
~peoAsyncDataTransfer() { ~peoAsyncDataTransfer()
{
delete source; delete source;
delete destination; delete destination;
} }
@ -113,7 +115,8 @@ class peoAsyncDataTransfer : public Cooperative, public eoUpdater {
}; };
void peoAsyncDataTransfer :: pack() { void peoAsyncDataTransfer :: pack()
{
lock (); lock ();
@ -124,22 +127,26 @@ void peoAsyncDataTransfer :: pack() {
unlock(); unlock();
} }
void peoAsyncDataTransfer :: unpack() { void peoAsyncDataTransfer :: unpack()
{
lock (); lock ();
destination->unpackMessage(); destination->unpackMessage();
unlock(); unlock();
} }
void peoAsyncDataTransfer :: packSynchronizeReq() { void peoAsyncDataTransfer :: packSynchronizeReq()
{
} }
void peoAsyncDataTransfer :: sendData() { void peoAsyncDataTransfer :: sendData()
{
std :: vector< Cooperative* > in, out; std :: vector< Cooperative* > in, out;
topology.setNeighbors( this, in, out ); topology.setNeighbors( this, in, out );
for ( unsigned i = 0; i < out.size(); i++ ) { for ( unsigned i = 0; i < out.size(); i++ )
{
source->pushMessage(); source->pushMessage();
@ -150,12 +157,14 @@ void peoAsyncDataTransfer :: sendData() {
} }
} }
void peoAsyncDataTransfer :: receiveData() { void peoAsyncDataTransfer :: receiveData()
{
lock (); lock ();
{ {
while ( !( destination->empty() ) ) { while ( !( destination->empty() ) )
{
printDebugMessage( "peoAsyncDataTransfer: received data." ); printDebugMessage( "peoAsyncDataTransfer: received data." );
destination->popMessage(); destination->popMessage();
@ -165,7 +174,8 @@ void peoAsyncDataTransfer :: receiveData() {
unlock(); unlock();
} }
void peoAsyncDataTransfer :: operator()() { void peoAsyncDataTransfer :: operator()()
{
sendData(); // sending data sendData(); // sending data
receiveData(); // receiving data receiveData(); // receiving data

View file

@ -134,19 +134,22 @@ class peoSyncDataTransfer : public Cooperative, public eoUpdater
}; };
void peoSyncDataTransfer :: pack() { void peoSyncDataTransfer :: pack()
{
::pack( coop_em.front()->getKey() ); ::pack( coop_em.front()->getKey() );
source->packMessage(); source->packMessage();
coop_em.pop(); coop_em.pop();
} }
void peoSyncDataTransfer :: unpack() { void peoSyncDataTransfer :: unpack()
{
destination->unpackMessage(); destination->unpackMessage();
} }
void peoSyncDataTransfer :: packSynchronizeReq() { void peoSyncDataTransfer :: packSynchronizeReq()
{
packSynchronRequest( all ); packSynchronRequest( all );
} }
@ -154,9 +157,11 @@ void peoSyncDataTransfer :: packSynchronizeReq() {
extern void wakeUpCommunicator(); extern void wakeUpCommunicator();
extern int getNodeRank(); extern int getNodeRank();
void peoSyncDataTransfer :: sendData() { void peoSyncDataTransfer :: sendData()
{
for ( unsigned i = 0; i < out.size(); i ++ ) { for ( unsigned i = 0; i < out.size(); i ++ )
{
source->pushMessage(); source->pushMessage();
@ -169,11 +174,13 @@ void peoSyncDataTransfer :: sendData() {
wakeUpCommunicator(); wakeUpCommunicator();
} }
void peoSyncDataTransfer :: receiveData() { void peoSyncDataTransfer :: receiveData()
{
assert( !( destination->empty() ) ); assert( !( destination->empty() ) );
while ( !( destination->empty() ) ) { while ( !( destination->empty() ) )
{
printDebugMessage( "peoSyncDataTransfer: received data." ); printDebugMessage( "peoSyncDataTransfer: received data." );
destination->popMessage(); destination->popMessage();
@ -181,14 +188,17 @@ void peoSyncDataTransfer :: receiveData() {
} }
} }
void peoSyncDataTransfer :: operator()() { void peoSyncDataTransfer :: operator()()
{
standbyTransfer = false; standbyTransfer = false;
nbTransfersIn = nbTransfersOut = 0; nbTransfersIn = nbTransfersOut = 0;
topology.setNeighbors( this, in, out ); all = topology; topology.setNeighbors( this, in, out );
all = topology;
synchronizeCoopEx(); stop(); synchronizeCoopEx();
stop();
// sending data out // sending data out
sendData(); sendData();
@ -197,46 +207,54 @@ void peoSyncDataTransfer :: operator()() {
// receiving data in // receiving data in
receiveData(); receiveData();
synchronizeCoopEx(); stop(); synchronizeCoopEx();
stop();
} }
void peoSyncDataTransfer :: notifySending() { void peoSyncDataTransfer :: notifySending()
{
nbTransfersOut++; nbTransfersOut++;
printDebugMessage( "peoSyncDataTransfer: notified of the completion of a transfer round." ); printDebugMessage( "peoSyncDataTransfer: notified of the completion of a transfer round." );
getOwner()->setActive(); getOwner()->setActive();
if ( nbTransfersOut == out.size() && nbTransfersIn < in.size() ) { if ( nbTransfersOut == out.size() && nbTransfersIn < in.size() )
{
getOwner()->setPassive(); getOwner()->setPassive();
} }
} }
void peoSyncDataTransfer :: notifyReceiving() { void peoSyncDataTransfer :: notifyReceiving()
{
nbTransfersIn++; nbTransfersIn++;
printDebugMessage( "peoSyncIslandMig: notified of incoming data." ); printDebugMessage( "peoSyncIslandMig: notified of incoming data." );
if ( standbyTransfer ) { if ( standbyTransfer )
{
getOwner()->setActive(); getOwner()->setActive();
if ( nbTransfersOut == out.size() && nbTransfersIn < in.size() ) if ( nbTransfersOut == out.size() && nbTransfersIn < in.size() )
getOwner()->setPassive(); getOwner()->setPassive();
} }
if ( nbTransfersIn == in.size() ) { if ( nbTransfersIn == in.size() )
{
printDebugMessage( "peoSyncIslandMig: finished collecting incoming data." ); printDebugMessage( "peoSyncIslandMig: finished collecting incoming data." );
sem_post( &sync ); sem_post( &sync );
} }
} }
void peoSyncDataTransfer :: notifySendingSyncReq () { void peoSyncDataTransfer :: notifySendingSyncReq ()
{
getOwner()->setPassive(); getOwner()->setPassive();
printDebugMessage( "peoSyncIslandMig: synchronization request sent." ); printDebugMessage( "peoSyncIslandMig: synchronization request sent." );
} }
void peoSyncDataTransfer :: notifySynchronized () { void peoSyncDataTransfer :: notifySynchronized ()
{
printDebugMessage( "peoSyncIslandMig: cooperators synchronized." ); printDebugMessage( "peoSyncIslandMig: cooperators synchronized." );

View file

@ -11,27 +11,37 @@
#define MIGRATIONS_AT_N_GENERATIONS 5 #define MIGRATIONS_AT_N_GENERATIONS 5
#define NUMBER_OF_MIGRANTS 10 #define NUMBER_OF_MIGRANTS 10
struct Representation : public eoVector< eoMinimizingFitness, double > { struct Representation : public eoVector< eoMinimizingFitness, double >
{
Representation() { resize( SIZE ); } Representation()
{
resize( SIZE );
}
}; };
struct Init : public eoInit< Representation > { struct Init : public eoInit< Representation >
{
void operator()( Representation& rep ) { void operator()( Representation& rep )
{
for ( int i = 0; i < SIZE; i++ ) { for ( int i = 0; i < SIZE; i++ )
{
rep[ i ] = (rng.uniform() - 0.5) * DEF_DOMAIN; rep[ i ] = (rng.uniform() - 0.5) * DEF_DOMAIN;
} }
} }
}; };
struct Eval : public eoEvalFunc< Representation > { struct Eval : public eoEvalFunc< Representation >
{
void operator()( Representation& rep ) { void operator()( Representation& rep )
{
double fitnessValue = 0.0; double fitnessValue = 0.0;
for ( int i = 0; i < SIZE; i++ ) { for ( int i = 0; i < SIZE; i++ )
{
fitnessValue += pow( rep[ i ], 2.0 ); fitnessValue += pow( rep[ i ], 2.0 );
} }
@ -40,9 +50,11 @@ struct Eval : public eoEvalFunc< Representation > {
} }
}; };
struct MutationOp : public eoMonOp< Representation > { struct MutationOp : public eoMonOp< Representation >
{
bool operator()( Representation& rep ) { bool operator()( Representation& rep )
{
unsigned int pos = (unsigned int)( rng.uniform() * SIZE ); unsigned int pos = (unsigned int)( rng.uniform() * SIZE );
rep[ pos ] = (rng.uniform() - 0.5) * DEF_DOMAIN; rep[ pos ] = (rng.uniform() - 0.5) * DEF_DOMAIN;
@ -53,20 +65,24 @@ struct MutationOp : public eoMonOp< Representation > {
} }
}; };
struct XoverOp : public eoQuadOp< Representation > { struct XoverOp : public eoQuadOp< Representation >
{
bool operator()( Representation& repA, Representation& repB ) { bool operator()( Representation& repA, Representation& repB )
{
static Representation offA, offB; static Representation offA, offB;
double lambda = rng.uniform(); double lambda = rng.uniform();
for ( int i = 0; i < SIZE; i++ ) { for ( int i = 0; i < SIZE; i++ )
{
offA[ i ] = lambda * repA[ i ] + ( 1.0 - lambda ) * repB[ i ]; offA[ i ] = lambda * repA[ i ] + ( 1.0 - lambda ) * repB[ i ];
offB[ i ] = lambda * repB[ i ] + ( 1.0 - lambda ) * repA[ i ]; offB[ i ] = lambda * repB[ i ] + ( 1.0 - lambda ) * repA[ i ];
} }
repA = offA; repB = offB; repA = offA;
repB = offB;
repA.invalidate(); repA.invalidate();
repB.invalidate(); repB.invalidate();
@ -77,42 +93,51 @@ struct XoverOp : public eoQuadOp< Representation > {
void pack( const Representation& rep ) { void pack( const Representation& rep )
{
if ( rep.invalid() ) ::pack( (unsigned int)0 ); if ( rep.invalid() ) ::pack( (unsigned int)0 );
else { else
{
::pack( (unsigned int)1 ); ::pack( (unsigned int)1 );
::pack( (double)(rep.fitness()) ); ::pack( (double)(rep.fitness()) );
} }
for ( unsigned int index = 0; index < SIZE; index++ ) { for ( unsigned int index = 0; index < SIZE; index++ )
{
::pack( (double)rep[ index ] ); ::pack( (double)rep[ index ] );
} }
} }
void unpack( Representation& rep ) { void unpack( Representation& rep )
{
eoScalarFitness<double, std::greater<double> > fitness; eoScalarFitness<double, std::greater<double> > fitness;
unsigned int validFitness; unsigned int validFitness;
unpack( validFitness ); unpack( validFitness );
if ( validFitness ) { if ( validFitness )
{
double fitnessValue; ::unpack( fitnessValue ); double fitnessValue;
::unpack( fitnessValue );
rep.fitness( fitnessValue ); rep.fitness( fitnessValue );
} }
else { else
{
rep.invalidate(); rep.invalidate();
} }
double value; double value;
for ( unsigned int index = 0; index < SIZE; index++ ) { for ( unsigned int index = 0; index < SIZE; index++ )
{
::unpack( value ); ::unpack( value );
rep[ index ] = value; rep[ index ] = value;
} }
} }
int main( int __argc, char** __argv ) { int main( int __argc, char** __argv )
{
rng.reseed( time( NULL ) ); rng.reseed( time( NULL ) );

View file

@ -26,12 +26,15 @@ struct CoSearch
peoAsyncDataTransfer& asyncTransferA, peoAsyncDataTransfer& asyncTransferB peoAsyncDataTransfer& asyncTransferA, peoAsyncDataTransfer& asyncTransferB
) )
: transferA( A ), transferB( B ), : transferA( A ), transferB( B ),
asyncDataTransferA( asyncTransferA ), asyncDataTransferB( asyncTransferB ) { asyncDataTransferA( asyncTransferA ), asyncDataTransferB( asyncTransferB )
{
} }
void operator()() { void operator()()
{
for ( unsigned int index = 0; index < 100; index++ ) { for ( unsigned int index = 0; index < 100; index++ )
{
asyncDataTransferA(); asyncDataTransferA();
asyncDataTransferB(); asyncDataTransferB();
@ -51,18 +54,22 @@ struct CoSearch
}; };
struct PushBackAggregation { struct PushBackAggregation
{
void operator()( eoPop< Route >& A, eoPop< Route >& B ) { void operator()( eoPop< Route >& A, eoPop< Route >& B )
{
for ( unsigned int index = 0; index < B.size(); index++ ) { for ( unsigned int index = 0; index < B.size(); index++ )
{
A.push_back( B[ index ] ); A.push_back( B[ index ] );
} }
} }
}; };
int main( int __argc, char** __argv ) { int main( int __argc, char** __argv )
{
peo :: init( __argc, __argv ); peo :: init( __argc, __argv );