engine.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-12 11:28:28 +0200 (Tue, 12 May 2009) $ by $Author: schulte $ 00011 * $Revision: 9059 $ 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/engine.hh> 00039 00040 namespace Gecode { namespace Search { namespace Parallel { 00041 00042 /* 00043 * Engine: search control 00044 */ 00045 Space* 00046 Engine::next(void) { 00047 // Invariant: the worker holds the wait mutex 00048 m_search.acquire(); 00049 if (!solutions.empty()) { 00050 // No search needs to be done, take leftover solution 00051 Space* s = solutions.pop(); 00052 m_search.release(); 00053 return s; 00054 } 00055 // We ignore stopped (it will be reported again if needed) 00056 has_stopped = false; 00057 // No more solutions? 00058 if (n_busy == 0) { 00059 m_search.release(); 00060 return NULL; 00061 } 00062 m_search.release(); 00063 // Okay, now search has to continue, make the guys work 00064 release(C_WORK); 00065 00066 /* 00067 * Wait until a search related event has happened. It might be that 00068 * the event has already been signalled in the last run, but the 00069 * solution has been removed. So we have to try until there has 00070 * something new happened. 00071 */ 00072 while (true) { 00073 e_search.wait(); 00074 m_search.acquire(); 00075 if (!solutions.empty()) { 00076 // Report solution 00077 Space* s = solutions.pop(); 00078 m_search.release(); 00079 // Make workers wait again 00080 block(); 00081 return s; 00082 } 00083 // No more solutions or stopped? 00084 if ((n_busy == 0) || has_stopped) { 00085 m_search.release(); 00086 // Make workers wait again 00087 block(); 00088 return NULL; 00089 } 00090 m_search.release(); 00091 } 00092 GECODE_NEVER; 00093 return NULL; 00094 } 00095 00096 bool 00097 Engine::stopped(void) const { 00098 return has_stopped; 00099 } 00100 00101 00102 /* 00103 * Termination and deletion 00104 */ 00105 Engine::Worker::~Worker(void) { 00106 delete cur; 00107 path.reset(); 00108 } 00109 00110 }}} 00111 00112 // STATISTICS: search-parallel
