peoAsyncIslandMig.h

00001 /* 
00002 * <peoAsyncIslandMig.h>
00003 * Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
00004 * (C) OPAC Team, LIFL, 2002-2007
00005 *
00006 * Sebastien Cahon, Alexandru-Adrian Tantar
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 __peoAsyncIslandMig_h
00038 #define __peoAsyncIslandMig_h
00039 
00040 
00041 #include <queue>
00042 
00043 #include <utils/eoUpdater.h>
00044 
00045 #include <eoContinue.h>
00046 #include <eoSelect.h>
00047 #include <eoReplacement.h>
00048 #include <eoPop.h>
00049 
00050 #include "core/topology.h"
00051 #include "core/cooperative.h"
00052 #include "core/eoPop_comm.h"
00053 #include "core/peo_debug.h"
00054 
00055 
00057 
00140 template< class EOT > class peoAsyncIslandMig : public Cooperative, public eoUpdater {
00141 
00142 public:
00143 
00153         peoAsyncIslandMig( 
00154                                 eoContinue< EOT >& __cont,
00155                                 eoSelect< EOT >& __select,
00156                                 eoReplacement< EOT >& __replace,
00157                                 Topology& __topology,
00158                                 eoPop< EOT >& __source,
00159                                 eoPop< EOT >& __destination 
00160         );
00161 
00166         void operator()();
00167         
00169         void pack();
00171         void unpack();
00172 
00173 
00174 private:
00175 
00176         void emigrate();
00177         void immigrate();
00178 
00179 
00180 private:
00181 
00182         eoContinue< EOT >& cont;        // continuator
00183         eoSelect< EOT >& select;        // the selection strategy
00184         eoReplacement< EOT >& replace;  // the replacement strategy
00185         Topology& topology;             // the neighboring topology
00186         
00187         // source and destination populations
00188         eoPop< EOT >& source;
00189         eoPop< EOT >& destination;
00190         
00191         // immigrants & emigrants in the queue
00192         std :: queue< eoPop< EOT > > imm;
00193         std :: queue< eoPop< EOT > > em;
00194         
00195         std :: queue< Cooperative* > coop_em;
00196 };
00197 
00198 
00199 template< class EOT > peoAsyncIslandMig< EOT > :: peoAsyncIslandMig(
00200 
00201                 eoContinue< EOT >& __cont,
00202                 eoSelect< EOT >& __select,
00203                 eoReplacement< EOT >& __replace,
00204                 Topology& __topology,
00205                 eoPop< EOT >& __source,
00206                 eoPop< EOT >& __destination
00207 
00208         ) : cont( __cont ), select( __select ), replace( __replace ), topology( __topology ), source( __source ), destination( __destination )
00209 {
00210 
00211         __topology.add( *this );
00212 }
00213 
00214 
00215 template< class EOT > void peoAsyncIslandMig< EOT > :: pack()
00216 {
00217 
00218         lock(); {
00219 
00220 		 :: pack( coop_em.front()->getKey() );
00221 		 :: pack( em.front() );
00222                 coop_em.pop();
00223                 em.pop();
00224         }
00225         unlock();
00226 }
00227 
00228 
00229 template< class EOT > void peoAsyncIslandMig< EOT > :: unpack()
00230 {
00231 
00232         lock(); {
00233 
00234                 eoPop< EOT > mig;
00235 		 :: unpack( mig );
00236                 imm.push( mig );
00237         }
00238         unlock();
00239 }
00240 
00241 
00242 template< class EOT > void peoAsyncIslandMig< EOT > :: emigrate()
00243 {
00244 
00245         std :: vector< Cooperative* >in, out;
00246         topology.setNeighbors( this, in, out );
00247 
00248         for ( unsigned i = 0; i < out.size(); i++ ) {
00249         
00250                 eoPop< EOT > mig;
00251                 select( source, mig );
00252                 em.push( mig );
00253                 coop_em.push( out[i] );
00254                 send( out[i] );
00255                 printDebugMessage( "sending some emigrants." );
00256         }
00257 }
00258 
00259 
00260 template< class EOT > void peoAsyncIslandMig< EOT > :: immigrate()
00261 {
00262 
00263         lock(); {
00264 
00265                 while ( !imm.empty() ) {
00266 
00267                         replace( destination, imm.front() );
00268                         imm.pop();
00269                         printDebugMessage( "receiving some immigrants." );
00270                 }
00271         }
00272         unlock();
00273 }
00274 
00275 
00276 template< class EOT > void peoAsyncIslandMig< EOT > :: operator()() {
00277 
00278         if ( !cont( source ) ) {
00279 
00280                 emigrate();     // sending emigrants
00281                 immigrate();    // receiving immigrants
00282         }
00283 }
00284 
00285 
00286 #endif

Generated on Mon Oct 8 11:16:46 2007 for ParadisEO-PEOMovingObjects by  doxygen 1.4.7