Generated on Fri Mar 20 15:56:18 2015 for Gecode by doxygen 1.6.3

bab.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2009
00008  *
00009  *  Last modified:
00010  *     $Date: 2015-01-08 15:07:24 +0100 (Thu, 08 Jan 2015) $ by $Author: schulte $
00011  *     $Revision: 14341 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 #include <gecode/support.hh>
00039 
00040 #ifdef GECODE_HAS_THREADS
00041 
00042 #include <gecode/search/parallel/bab.hh>
00043 
00044 namespace Gecode { namespace Search { namespace Parallel {
00045 
00046   /*
00047    * Statistics
00048    */
00049   Statistics 
00050   BAB::statistics(void) const {
00051     Statistics s;
00052     for (unsigned int i=0; i<workers(); i++)
00053       s += worker(i)->statistics();
00054     return s;
00055   }
00056 
00057   /*
00058    * Actual work
00059    */
00060   void
00061   BAB::Worker::run(void) {
00062     /*
00063      * The engine maintains the following invariant:
00064      *  - If the current space (cur) is not NULL, the path always points
00065      *    to exactly that space.
00066      *  - If the current space (cur) is NULL, the path always points
00067      *    to the next space (if there is any).
00068      *
00069      * This invariant is needed so that no-goods can be extracted properly
00070      * when the engine is stopped or has found a solution.
00071      *
00072      * An additional invariant maintained by the engine is:
00073      *   For all nodes stored at a depth less than mark, there
00074      *   is no guarantee of betterness. For those above the mark,
00075      *   betterness is guaranteed.
00076      *
00077      */
00078     // Peform initial delay, if not first worker
00079     if (this != engine().worker(0))
00080       Support::Thread::sleep(Config::initial_delay);
00081     // Okay, we are in business, start working
00082     while (true) {
00083       switch (engine().cmd()) {
00084       case C_WAIT:
00085         // Wait
00086         engine().wait();
00087         break;
00088       case C_TERMINATE:
00089         // Acknowledge termination request
00090         engine().ack_terminate();
00091         // Wait until termination can proceed
00092         engine().wait_terminate();
00093         // Terminate thread
00094         engine().terminated();
00095         return;
00096       case C_RESET:
00097         // Acknowledge reset request
00098         engine().ack_reset_start();
00099         // Wait until reset has been performed
00100         engine().wait_reset();
00101         // Acknowledge that reset cycle is over
00102         engine().ack_reset_stop();
00103         break;
00104       case C_WORK:
00105         // Perform exploration work
00106         {
00107           m.acquire();
00108           if (idle) {
00109             m.release();
00110             // Try to find new work
00111             find();
00112           } else if (cur != NULL) {
00113             start();
00114             if (stop(engine().opt())) {
00115               // Report stop
00116               m.release();
00117               engine().stop();
00118             } else {
00119               node++;
00120               switch (cur->status(*this)) {
00121               case SS_FAILED:
00122                 fail++;
00123                 delete cur;
00124                 cur = NULL;
00125                 path.next();
00126                 m.release();
00127                 break;
00128               case SS_SOLVED:
00129                 {
00130                   // Deletes all pending branchers
00131                   (void) cur->choice();
00132                   Space* s = cur->clone(false);
00133                   delete cur;
00134                   cur = NULL;
00135                   path.next();
00136                   m.release();
00137                   engine().solution(s);
00138                 }
00139                 break;
00140               case SS_BRANCH:
00141                 {
00142                   Space* c;
00143                   if ((d == 0) || (d >= engine().opt().c_d)) {
00144                     c = cur->clone();
00145                     d = 1;
00146                   } else {
00147                     c = NULL;
00148                     d++;
00149                   }
00150                   const Choice* ch = path.push(*this,cur,c);
00151                   cur->commit(*ch,0);
00152                   m.release();
00153                 }
00154                 break;
00155               default:
00156                 GECODE_NEVER;
00157               }
00158             }
00159           } else if (!path.empty()) {
00160             cur = path.recompute(d,engine().opt().a_d,*this,*best,mark);
00161             if (cur == NULL)
00162               path.next();
00163             m.release();
00164           } else {
00165             idle = true;
00166             path.ngdl(0);
00167             m.release();
00168             // Report that worker is idle
00169             engine().idle();
00170           }
00171         }
00172         break;
00173       default:
00174         GECODE_NEVER;
00175       }
00176     }
00177   }
00178 
00179 
00180   /*
00181    * Perform reset
00182    *
00183    */
00184   void
00185   BAB::reset(Space* s) {
00186     // Grab wait lock for reset
00187     m_wait_reset.acquire();
00188     // Release workers for reset
00189     release(C_RESET);
00190     // Wait for reset cycle started
00191     e_reset_ack_start.wait();
00192     // All workers are marked as busy again
00193     delete best;
00194     best = NULL;
00195     n_busy = workers();
00196     for (unsigned int i=1; i<workers(); i++)
00197       worker(i)->reset(NULL,0);
00198     worker(0)->reset(s,opt().nogoods_limit);
00199     // Block workers again to ensure invariant
00200     block();
00201     // Release reset lock
00202     m_wait_reset.release();
00203     // Wait for reset cycle stopped
00204     e_reset_ack_stop.wait();
00205   }
00206 
00207 
00208   /*
00209    * Create no-goods
00210    *
00211    */
00212   NoGoods&
00213   BAB::nogoods(void) {
00214     NoGoods* ng;
00215     // Grab wait lock for reset
00216     m_wait_reset.acquire();
00217     // Release workers for reset
00218     release(C_RESET);
00219     // Wait for reset cycle started
00220     e_reset_ack_start.wait();
00221     ng = &worker(0)->nogoods();
00222     // Block workers again to ensure invariant
00223     block();
00224     // Release reset lock
00225     m_wait_reset.release();
00226     // Wait for reset cycle stopped
00227     e_reset_ack_stop.wait();
00228     return *ng;
00229   }
00230 
00231   /*
00232    * Termination and deletion
00233    */
00234   BAB::Worker::~Worker(void) {
00235     delete best;
00236   }
00237 
00238   BAB::~BAB(void) {
00239     terminate();
00240     delete best;
00241     heap.rfree(_worker);
00242   }
00243 
00244 }}}
00245 
00246 #endif
00247 
00248 // STATISTICS: search-parallel