peoSyncIslandMig.h

00001 /*
00002 * <peoSyncIslandMig.h>
00003 * Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2008
00004 * (C) OPAC Team, LIFL, 2002-2008
00005 *
00006 * Sebastien Cahon, Alexandru-Adrian Tantar, Clive Canape
00007 *
00008 * This software is governed by the CeCILL license under French law and
00009 * abiding by the rules of distribution of free software.  You can  use,
00010 * modify and/ or redistribute the software under the terms of the CeCILL
00011 * license as circulated by CEA, CNRS and INRIA at the following URL
00012 * "http://www.cecill.info".
00013 *
00014 * As a counterpart to the access to the source code and  rights to copy,
00015 * modify and redistribute granted by the license, users are provided only
00016 * with a limited warranty  and the software's author,  the holder of the
00017 * economic rights,  and the successive licensors  have only  limited liability.
00018 *
00019 * In this respect, the user's attention is drawn to the risks associated
00020 * with loading,  using,  modifying and/or developing or reproducing the
00021 * software by the user in light of its specific status of free software,
00022 * that may mean  that it is complicated to manipulate,  and  that  also
00023 * therefore means  that it is reserved for developers  and  experienced
00024 * professionals having in-depth computer knowledge. Users are therefore
00025 * encouraged to load and test the software's suitability as regards their
00026 * requirements in conditions enabling the security of their systems and/or
00027 * data to be ensured and,  more generally, to use and operate it in the
00028 * same conditions as regards security.
00029 * The fact that you are presently reading this means that you have had
00030 * knowledge of the CeCILL license and that you accept its terms.
00031 *
00032 * ParadisEO WebSite : http://paradiseo.gforge.inria.fr
00033 * Contact: paradiseo-help@lists.gforge.inria.fr
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       // sending emigrants
00215       emigrate();
00216       // synchronizing
00217       sem_wait( &sync );
00218       // receiving immigrants
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

Generated on Thu Mar 13 15:55:03 2008 for ParadisEO-PEO-ParallelanddistributedEvolvingObjects by  doxygen 1.4.7