// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- // "peoSyncIslandMig.h" // (c) OPAC Team, LIFL, August 2005 /* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Contact: cahon@lifl.fr */ #ifndef __peoSyncIslandMig_h #define __peoSyncIslandMig_h #include #include #include #include #include #include #include #include #include "topo.h" #include "thread.h" #include "eoPop_comm.h" #include "peo_debug.h" template class peoSyncIslandMig : public Cooperative, public eoUpdater { public : /* Ctor */ peoSyncIslandMig (unsigned __freq, eoSelect & __select, eoReplacement & __replace, Topology & __topo, eoPop & __from, eoPop & __to); void operator () (); void pack (); void unpack (); void notifySending (); private : void emigrate (); void immigrate (); eoPeriodicContinue cont; eoSelect & select; /* The selection strategy */ eoReplacement & replace; /* The replacement strategy */ Topology & topo; /* The neighboring topology */ /* Source and target populations */ eoPop & from, & to; /* Immigrants & emigrants in the queue */ std :: queue > imm, em; std :: queue coop_em; sem_t sync; }; template peoSyncIslandMig :: peoSyncIslandMig (unsigned __freq, eoSelect & __select, eoReplacement & __replace, Topology & __topo, eoPop & __from, eoPop & __to ) : cont (__freq), select (__select), replace (__replace), topo (__topo), from (__from), to (__to) { __topo.add (* this); sem_init (& sync, 0, 0); } template void peoSyncIslandMig :: pack () { lock (); :: pack (coop_em.front () -> getKey ()); :: pack (em.front ()); coop_em.pop (); em.pop (); unlock (); } template void peoSyncIslandMig :: unpack () { lock (); eoPop mig; :: unpack (mig); imm.push (mig); unlock (); sem_post (& sync); } template void peoSyncIslandMig :: emigrate () { std :: vector in, out ; topo.setNeighbors (this, in, out) ; for (unsigned i = 0; i < out.size (); i ++) { eoPop mig; select (from, mig); em.push (mig); coop_em.push (out [i]); send (out [i]); printDebugMessage ("sending some emigrants."); } } template void peoSyncIslandMig :: immigrate () { lock (); assert (imm.size ()); replace (to, imm.front ()) ; imm.pop (); printDebugMessage ("receiving some immigrants."); unlock (); } template void peoSyncIslandMig :: operator () () { if (! cont (from)) { /* Sending emigrants */ emigrate (); stop (); /* Sync. */ sem_wait (& sync); getOwner () -> setActive (); /* Immigrants */ immigrate (); } } template void peoSyncIslandMig :: notifySending () { lock (); if (imm.empty ()) { printDebugMessage ("mode passif\n"); getOwner () -> setPassive (); } unlock (); resume (); } #endif