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 __peoAsyncIslandMig_h
00038 #define __peoAsyncIslandMig_h
00039
00040
00041 #include <queue>
00042
00043 #include <utils/eoUpdater.h>
00044 #include <eoContinue.h>
00045 #include <eoSelect.h>
00046 #include <eoReplacement.h>
00047 #include <eoPop.h>
00048
00049
00050 #include "core/messaging.h"
00051 #include "core/eoPop_mesg.h"
00052 #include "core/eoVector_mesg.h"
00053 #include "core/topology.h"
00054 #include "core/thread.h"
00055 #include "core/cooperative.h"
00056 #include "core/peo_debug.h"
00057
00058
00064 template< class TYPESELECT, class TYPEREPLACE > class peoAsyncIslandMig : public Cooperative, public eoUpdater
00065 {
00066
00067 public:
00068
00074 peoAsyncIslandMig(
00075 continuator & __cont,
00076 selector <TYPESELECT> & __select,
00077 replacement <TYPEREPLACE> & __replace,
00078 Topology& __topology
00079 );
00080
00082 void operator()();
00084 void pack();
00086 void unpack();
00088 void packSynchronizeReq();
00089
00090 private:
00092 void emigrate();
00094 void immigrate();
00095
00096 private:
00104 continuator & cont;
00105 selector <TYPESELECT> & select;
00106 replacement <TYPEREPLACE> & replace;
00107 Topology& topology;
00108 std :: queue< TYPEREPLACE > imm;
00109 std :: queue< TYPESELECT > em;
00110 std :: queue< Cooperative* > coop_em;
00111 };
00112
00113
00114 template< class TYPESELECT , class TYPEREPLACE> peoAsyncIslandMig< TYPESELECT, TYPEREPLACE > :: peoAsyncIslandMig(
00115
00116 continuator & __cont,
00117 selector <TYPESELECT> & __select,
00118 replacement <TYPEREPLACE> & __replace,
00119 Topology& __topology
00120
00121 ) : select( __select ), replace( __replace ), topology( __topology ), cont(__cont)
00122 {
00123
00124 __topology.add( *this );
00125 }
00126
00127
00128 template< class TYPESELECT , class TYPEREPLACE> void peoAsyncIslandMig< TYPESELECT, TYPEREPLACE > :: pack()
00129 {
00130 lock ();
00131 ::pack( coop_em.front()->getKey() );
00132 ::pack(em.front());
00133 coop_em.pop();
00134 em.pop();
00135 unlock();
00136 }
00137
00138
00139 template< class TYPESELECT, class TYPEREPLACE> void peoAsyncIslandMig< TYPESELECT , TYPEREPLACE> :: unpack()
00140 {
00141 lock ();
00142 TYPEREPLACE mig;
00143 ::unpack(mig);
00144 imm.push( mig );
00145 unlock();
00146 }
00147
00148 template< class TYPESELECT , class TYPEREPLACE> void peoAsyncIslandMig< TYPESELECT, TYPEREPLACE > :: packSynchronizeReq()
00149 {}
00150
00151 template< class TYPESELECT , class TYPEREPLACE> void peoAsyncIslandMig< TYPESELECT , TYPEREPLACE> :: emigrate()
00152 {
00153 std :: vector< Cooperative* >in, out;
00154 topology.setNeighbors( this, in, out );
00155
00156 for ( unsigned i = 0; i < out.size(); i++ )
00157 {
00158
00159 TYPESELECT mig;
00160 select(mig);
00161 em.push( mig );
00162 coop_em.push( out[i] );
00163 send( out[i] );
00164 printDebugMessage( "sending some emigrants." );
00165 }
00166 }
00167
00168
00169 template< class TYPESELECT, class TYPEREPLACE> void peoAsyncIslandMig< TYPESELECT , TYPEREPLACE> :: immigrate()
00170 {
00171
00172 lock ();
00173 {
00174
00175 while ( !imm.empty() )
00176 {
00177 replace(imm.front() );
00178 imm.pop();
00179 printDebugMessage( "receiving some immigrants." );
00180 }
00181 }
00182 unlock();
00183 }
00184
00185
00186 template< class TYPESELECT , class TYPEREPLACE> void peoAsyncIslandMig< TYPESELECT , TYPEREPLACE > :: operator()()
00187 {
00188
00189 if (! cont.check())
00190 {
00191
00192 emigrate();
00193 immigrate();
00194 }
00195 }
00196
00197
00198 #endif