peoAsyncIslandMig.h

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 // "peoAsyncIslandMig.h"
00004 
00005 // (c) OPAC Team, LIFL, August 2005
00006 
00007 /* This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016 
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with this library; if not, write to the Free Software
00019    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020 
00021    Contact: paradiseo-help@lists.gforge.inria.fr
00022 */
00023 
00024 #ifndef __peoAsyncIslandMig_h
00025 #define __peoAsyncIslandMig_h
00026 
00027 
00028 #include <queue>
00029 
00030 #include <utils/eoUpdater.h>
00031 
00032 #include <eoContinue.h>
00033 #include <eoSelect.h>
00034 #include <eoReplacement.h>
00035 #include <eoPop.h>
00036 
00037 #include "core/topology.h"
00038 #include "core/cooperative.h"
00039 #include "core/eoPop_comm.h"
00040 #include "core/peo_debug.h"
00041 
00042 
00044 
00127 template< class EOT > class peoAsyncIslandMig : public Cooperative, public eoUpdater {
00128 
00129 public:
00130 
00140         peoAsyncIslandMig( 
00141                                 eoContinue< EOT >& __cont,
00142                                 eoSelect< EOT >& __select,
00143                                 eoReplacement< EOT >& __replace,
00144                                 Topology& __topology,
00145                                 eoPop< EOT >& __source,
00146                                 eoPop< EOT >& __destination 
00147         );
00148 
00153         void operator()();
00154         
00156         void pack();
00158         void unpack();
00159 
00160 
00161 private:
00162 
00163         void emigrate();
00164         void immigrate();
00165 
00166 
00167 private:
00168 
00169         eoContinue< EOT >& cont;        // continuator
00170         eoSelect< EOT >& select;        // the selection strategy
00171         eoReplacement< EOT >& replace;  // the replacement strategy
00172         Topology& topology;             // the neighboring topology
00173         
00174         // source and destination populations
00175         eoPop< EOT >& source;
00176         eoPop< EOT >& destination;
00177         
00178         // immigrants & emigrants in the queue
00179         std :: queue< eoPop< EOT > > imm;
00180         std :: queue< eoPop< EOT > > em;
00181         
00182         std :: queue< Cooperative* > coop_em;
00183 };
00184 
00185 
00186 template< class EOT > peoAsyncIslandMig< EOT > :: peoAsyncIslandMig(
00187 
00188                 eoContinue< EOT >& __cont,
00189                 eoSelect< EOT >& __select,
00190                 eoReplacement< EOT >& __replace,
00191                 Topology& __topology,
00192                 eoPop< EOT >& __source,
00193                 eoPop< EOT >& __destination
00194 
00195         ) : cont( __cont ), select( __select ), replace( __replace ), topology( __topology ), source( __source ), destination( __destination )
00196 {
00197 
00198         __topology.add( *this );
00199 }
00200 
00201 
00202 template< class EOT > void peoAsyncIslandMig< EOT > :: pack()
00203 {
00204 
00205         lock(); {
00206 
00207 		 :: pack( coop_em.front()->getKey() );
00208 		 :: pack( em.front() );
00209                 coop_em.pop();
00210                 em.pop();
00211         }
00212         unlock();
00213 }
00214 
00215 
00216 template< class EOT > void peoAsyncIslandMig< EOT > :: unpack()
00217 {
00218 
00219         lock(); {
00220 
00221                 eoPop< EOT > mig;
00222 		 :: unpack( mig );
00223                 imm.push( mig );
00224         }
00225         unlock();
00226 }
00227 
00228 
00229 template< class EOT > void peoAsyncIslandMig< EOT > :: emigrate()
00230 {
00231 
00232         std :: vector< Cooperative* >in, out;
00233         topology.setNeighbors( this, in, out );
00234 
00235         for ( unsigned i = 0; i < out.size(); i++ ) {
00236         
00237                 eoPop< EOT > mig;
00238                 select( source, mig );
00239                 em.push( mig );
00240                 coop_em.push( out[i] );
00241                 send( out[i] );
00242                 printDebugMessage( "sending some emigrants." );
00243         }
00244 }
00245 
00246 
00247 template< class EOT > void peoAsyncIslandMig< EOT > :: immigrate()
00248 {
00249 
00250         lock(); {
00251 
00252                 while ( !imm.empty() ) {
00253 
00254                         replace( destination, imm.front() );
00255                         imm.pop();
00256                         printDebugMessage( "receiving some immigrants." );
00257                 }
00258         }
00259         unlock();
00260 }
00261 
00262 
00263 template< class EOT > void peoAsyncIslandMig< EOT > :: operator()() {
00264 
00265         if ( !cont( source ) ) {
00266 
00267                 emigrate();     // sending emigrants
00268                 immigrate();    // receiving immigrants
00269         }
00270 }
00271 
00272 
00273 #endif

Generated on Sat Dec 30 14:55:29 2006 for ParadisEO-PEO by  doxygen 1.4.7