engine.hh
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 * This file is part of Gecode, the generic constraint 00010 * development environment: 00011 * http://www.gecode.org 00012 * 00013 * Permission is hereby granted, free of charge, to any person obtaining 00014 * a copy of this software and associated documentation files (the 00015 * "Software"), to deal in the Software without restriction, including 00016 * without limitation the rights to use, copy, modify, merge, publish, 00017 * distribute, sublicense, and/or sell copies of the Software, and to 00018 * permit persons to whom the Software is furnished to do so, subject to 00019 * the following conditions: 00020 * 00021 * The above copyright notice and this permission notice shall be 00022 * included in all copies or substantial portions of the Software. 00023 * 00024 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00025 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00026 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00027 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00028 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00029 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00030 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00031 * 00032 */ 00033 00034 #ifndef __GECODE_SEARCH_PAR_ENGINE_HH__ 00035 #define __GECODE_SEARCH_PAR_ENGINE_HH__ 00036 00037 #include <gecode/search.hh> 00038 #include <gecode/search/support.hh> 00039 #include <gecode/search/worker.hh> 00040 #include <gecode/search/par/path.hh> 00041 00042 namespace Gecode { namespace Search { namespace Par { 00043 00045 template<class Tracer> 00046 class Engine : public Search::Engine, public Support::Terminator { 00047 protected: 00049 class Worker : public Search::Worker, public Support::Runnable { 00050 public: 00052 Tracer tracer; 00053 protected: 00055 Engine& _engine; 00057 Support::Mutex m; 00059 Path<Tracer> path; 00061 Space* cur; 00063 unsigned int d; 00065 bool idle; 00066 public: 00068 Worker(Space* s, Engine& e); 00070 Space* steal(unsigned long int& d, Tracer& myt, Tracer& ot); 00072 Statistics statistics(void); 00074 Engine& engine(void) const; 00076 NoGoods& nogoods(void); 00078 virtual ~Worker(void); 00080 virtual Support::Terminator* terminator(void) const; 00081 }; 00083 Options _opt; 00084 public: 00086 const Options& opt(void) const; 00088 unsigned int workers(void) const; 00089 00091 00092 00093 enum Cmd { 00094 C_WORK, 00095 C_WAIT, 00096 C_RESET, 00097 C_TERMINATE 00098 }; 00099 protected: 00101 volatile Cmd _cmd; 00103 Support::Mutex _m_wait; 00104 public: 00106 Cmd cmd(void) const; 00108 void block(void); 00110 void release(Cmd c); 00112 void wait(void); 00114 00116 00117 protected: 00119 Support::Mutex _m_term; 00121 volatile unsigned int _n_term_not_ack; 00123 Support::Event _e_term_ack; 00125 Support::Mutex _m_wait_terminate; 00127 volatile unsigned int _n_not_terminated; 00129 Support::Event _e_terminate; 00130 public: 00132 void ack_terminate(void); 00134 virtual void terminated(void); 00136 void wait_terminate(void); 00138 void terminate(void); 00140 00142 00143 protected: 00145 Support::Mutex _m_reset; 00147 volatile unsigned int _n_reset_not_ack; 00149 Support::Event e_reset_ack_start; 00151 Support::Event e_reset_ack_stop; 00153 Support::Mutex m_wait_reset; 00154 public: 00156 void ack_reset_start(void); 00158 void ack_reset_stop(void); 00160 void wait_reset(void); 00162 00164 00165 protected: 00167 Support::Mutex m_search; 00169 Support::Event e_search; 00171 Support::DynamicQueue<Space*,Heap> solutions; 00173 volatile unsigned int n_busy; 00175 volatile bool has_stopped; 00177 bool signal(void) const; 00178 public: 00180 void idle(void); 00182 void busy(void); 00184 void stop(void); 00186 00188 00189 00190 Engine(const Options& o); 00192 virtual Space* next(void); 00194 virtual bool stopped(void) const; 00196 }; 00197 00198 }}} 00199 00200 #include <gecode/search/par/engine.hpp> 00201 00202 #endif 00203 00204 // STATISTICS: search-par