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