00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef __peoSyncIslandMig_h
00038 #define __peoSyncIslandMig_h
00039
00040
00041 #include <queue>
00042 #include <cassert>
00043
00044 #include <eoPeriodicContinue.h>
00045
00046 #include <utils/eoUpdater.h>
00047
00048 #include <eoContinue.h>
00049 #include <eoSelect.h>
00050 #include <eoReplacement.h>
00051 #include <eoPop.h>
00052 #include "peoData.h"
00053
00054 #include "core/messaging.h"
00055 #include "core/eoPop_mesg.h"
00056 #include "core/eoVector_mesg.h"
00057
00058 #include "core/topology.h"
00059 #include "core/thread.h"
00060 #include "core/cooperative.h"
00061 #include "core/peo_debug.h"
00062
00063 #include "rmc/mpi/synchron.h"
00064
00065
00071 template< class TYPESELECT, class TYPEREPLACE > class peoSyncIslandMig : public Cooperative, public eoUpdater
00072 {
00073
00074 public:
00075
00081 peoSyncIslandMig(
00082 unsigned __frequency,
00083 selector <TYPESELECT> & __select,
00084 replacement <TYPEREPLACE> & __replace,
00085 Topology& __topology
00086 );
00087
00089 void operator()();
00091 void pack();
00093 void unpack();
00095 void packSynchronizeReq();
00097 void notifySending();
00099 void notifyReceiving();
00101 void notifySendingSyncReq();
00103 void notifySynchronized();
00104
00105 private:
00106
00107 void emigrate();
00108 void immigrate();
00109
00110
00111 private:
00124 eoSyncContinue cont;
00125 selector <TYPESELECT> & select;
00126 replacement <TYPEREPLACE> & replace;
00127 Topology& topology;
00128 std :: queue< TYPEREPLACE > imm;
00129 std :: queue< TYPESELECT > em;
00130 std :: queue< Cooperative* > coop_em;
00131 sem_t sync;
00132 bool explicitPassive;
00133 bool standbyMigration;
00134 std :: vector< Cooperative* > in, out, all;
00135 unsigned nbMigrations;
00136 };
00137
00138
00139 template< class TYPESELECT, class TYPEREPLACE > peoSyncIslandMig< TYPESELECT,TYPEREPLACE > :: peoSyncIslandMig(
00140
00141 unsigned __frequency,
00142 selector <TYPESELECT> & __select,
00143 replacement <TYPEREPLACE> & __replace,
00144 Topology& __topology
00145 ) : cont( __frequency ), select( __select ), replace( __replace ), topology( __topology )
00146 {
00147
00148 __topology.add( *this );
00149 sem_init( &sync, 0, 0 );
00150 }
00151
00152
00153 template< class TYPESELECT, class TYPEREPLACE > void peoSyncIslandMig< TYPESELECT, TYPEREPLACE > :: pack()
00154 {
00155 ::pack( coop_em.front()->getKey() );
00156 ::pack(em.front());
00157 coop_em.pop();
00158 em.pop();
00159 }
00160
00161 template< class TYPESELECT, class TYPEREPLACE > void peoSyncIslandMig< TYPESELECT, TYPEREPLACE > :: unpack()
00162 {
00163 TYPEREPLACE mig;
00164 ::unpack(mig);
00165 imm.push( mig );
00166 explicitPassive = true;
00167 }
00168
00169 template< class TYPESELECT, class TYPEREPLACE > void peoSyncIslandMig< TYPESELECT,TYPEREPLACE > :: packSynchronizeReq()
00170 {
00171
00172 packSynchronRequest( all );
00173 }
00174
00175 template< class TYPESELECT, class TYPEREPLACE > void peoSyncIslandMig< TYPESELECT , TYPEREPLACE > :: emigrate()
00176 {
00177
00178 for ( unsigned i = 0; i < out.size(); i ++ )
00179 {
00180
00181 TYPESELECT mig;
00182 select( mig );
00183 em.push( mig );
00184 coop_em.push( out[ i ] );
00185 send( out[ i ] );
00186 printDebugMessage( "peoSyncIslandMig: sending some emigrants." );
00187 }
00188 }
00189
00190 template< class TYPESELECT, class TYPEREPLACE > void peoSyncIslandMig< TYPESELECT , TYPEREPLACE > :: immigrate()
00191 {
00192 assert( imm.size() );
00193
00194 while ( imm.size() )
00195 {
00196 replace( imm.front() ) ;
00197 imm.pop();
00198 }
00199
00200 printDebugMessage( "peoSyncIslandMig: receiving some immigrants." );
00201 }
00202
00203 template< class TYPESELECT, class TYPEREPLACE > void peoSyncIslandMig< TYPESELECT , TYPEREPLACE > :: operator()()
00204 {
00205
00206 if (! cont.check() )
00207 {
00208 explicitPassive = standbyMigration = false;
00209 topology.setNeighbors( this, in, out );
00210 all = topology;
00211 nbMigrations = 0;
00212 synchronizeCoopEx();
00213 stop();
00214
00215 emigrate();
00216
00217 sem_wait( &sync );
00218
00219 immigrate();
00220 synchronizeCoopEx();
00221 stop();
00222 }
00223 }
00224
00225 template< class TYPESELECT, class TYPEREPLACE > void peoSyncIslandMig< TYPESELECT , TYPEREPLACE > :: notifySending()
00226 {
00227 if ( !explicitPassive ) getOwner()->setPassive();
00228 }
00229
00230 template< class TYPESELECT, class TYPEREPLACE > void peoSyncIslandMig< TYPESELECT , TYPEREPLACE > :: notifyReceiving()
00231 {
00232 nbMigrations++;
00233
00234 if ( nbMigrations == in.size() )
00235 {
00236
00237 if ( standbyMigration ) getOwner()->setActive();
00238 sem_post( &sync );
00239 }
00240 }
00241
00242 template< class TYPESELECT, class TYPE > void peoSyncIslandMig< TYPESELECT, TYPE > :: notifySendingSyncReq ()
00243 {
00244
00245 getOwner()->setPassive();
00246 }
00247
00248 template< class TYPESELECT, class TYPE > void peoSyncIslandMig< TYPESELECT, TYPE > :: notifySynchronized ()
00249 {
00250
00251 standbyMigration = true;
00252 getOwner()->setActive();
00253 resume();
00254 }
00255
00256
00257 #endif