Generated on Mon Jul 6 18:09:17 2009 for Gecode by doxygen 1.5.9

restart.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: 2009-05-13 21:19:00 +0200 (Wed, 13 May 2009) $ by $Author: schulte $
00011  *     $Revision: 9092 $
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/search/parallel/restart.hh>
00039 
00040 namespace Gecode { namespace Search { namespace Parallel {
00041 
00042   Space*
00043   Restart::next(void) {
00044     // Invariant: the worker holds the wait mutex
00045     m_search.acquire();
00046     // Check whether root space is already failed
00047     if (root == NULL) {
00048       m_search.release();
00049       return NULL;
00050     }
00051     while (!solutions.empty()) {
00052       // No search needs to be done, try to take leftover solution
00053       Space* s = solutions.pop();
00054       if (best == NULL) {
00055         best = s->clone();
00056         reset_needed = true;
00057         m_search.release();
00058         return s;
00059       } else {
00060         s->constrain(*best);
00061         if (s->status() == SS_FAILED) {
00062           delete s;
00063         } else {
00064           delete best;
00065           best = s->clone();
00066           reset_needed = true;
00067           m_search.release();
00068           return s;
00069         }
00070       }
00071     }
00072     // We ignore stopped (it will be reported again if needed)
00073     has_stopped = false;
00074     // No more solutions?
00075     if (n_busy == 0) {
00076       m_search.release();
00077       return NULL;
00078     }
00079     if (reset_needed) {
00080       reset_needed = false;
00081       root->constrain(*best);
00082       // Leave lock
00083       m_search.release();
00084       // Grab wait lock for reset
00085       m_wait_reset.acquire();
00086       // Release workers for reset
00087       release(C_RESET);
00088       // Wait for reset cycle started
00089       e_reset_ack_start.wait();
00090       // Perform reset
00091       root = reset(root);
00092       // Block workers again to ensure invariant
00093       block();
00094       // Release reset lock
00095       m_wait_reset.release();
00096       // Wait for reset cycle stopped
00097       e_reset_ack_stop.wait();
00098       if (root == NULL)
00099         return NULL;
00100     } else {
00101       m_search.release();
00102     }
00103 
00104     // Okay, now search has to continue, make the guys work
00105     release(C_WORK);
00106 
00107     /*
00108      * Wait until a search related event has happened. It might be that
00109      * the event has already been signalled in the last run, but the
00110      * solution has been removed. So we have to try until there has
00111      * something new happened.
00112      */
00113     while (true) {
00114       e_search.wait();
00115       m_search.acquire();
00116       while (!solutions.empty()) {
00117         // Check solution
00118         Space* s = solutions.pop();
00119         if (best == NULL) {
00120           best = s->clone();
00121           reset_needed = true;
00122           m_search.release();
00123           // Make workers wait again
00124           block();
00125           return s;
00126         } else {
00127           s->constrain(*best);
00128           if (s->status() == SS_FAILED) {
00129             delete s;
00130           } else {
00131             delete best;
00132             best = s->clone();
00133             reset_needed = true;
00134             m_search.release();
00135             // Make workers wait again
00136             block();
00137             return s;
00138           }
00139         }
00140       }
00141       // No more solutions or stopped?
00142       if ((n_busy == 0) || has_stopped) {
00143         m_search.release();
00144         // Make workers wait again
00145         block();
00146         return NULL;
00147       }
00148       m_search.release();
00149     }
00150     GECODE_NEVER;
00151     return NULL;
00152   }
00153 
00154   Restart::~Restart(void) {
00155     delete best;
00156     delete root;
00157   }
00158 
00159 }}}
00160 
00161 // STATISTICS: search-parallel