dfs.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 * Last modified: 00010 * $Date: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $ by $Author: schulte $ 00011 * $Revision: 14967 $ 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 #ifndef __GECODE_SEARCH_PARALLEL_DFS_HH__ 00039 #define __GECODE_SEARCH_PARALLEL_DFS_HH__ 00040 00041 #include <gecode/search/parallel/engine.hh> 00042 00043 namespace Gecode { namespace Search { namespace Parallel { 00044 00046 class DFS : public Engine { 00047 protected: 00049 class Worker : public Engine::Worker { 00050 public: 00052 Worker(Space* s, DFS& e); 00054 DFS& engine(void) const; 00056 virtual void run(void); 00058 void find(void); 00060 void reset(Space* s, unsigned int ngdl); 00061 }; 00063 Worker** _worker; 00064 public: 00066 Worker* worker(unsigned int i) const; 00067 00069 00070 00071 void solution(Space* s); 00073 00075 00076 00077 DFS(Space* s, const Options& o); 00079 virtual Statistics statistics(void) const; 00081 virtual void reset(Space* s); 00083 virtual NoGoods& nogoods(void); 00085 virtual ~DFS(void); 00087 }; 00088 00089 00090 /* 00091 * Basic access routines 00092 */ 00093 forceinline DFS& 00094 DFS::Worker::engine(void) const { 00095 return static_cast<DFS&>(_engine); 00096 } 00097 forceinline DFS::Worker* 00098 DFS::worker(unsigned int i) const { 00099 return _worker[i]; 00100 } 00101 00102 00103 /* 00104 * Engine: initialization 00105 */ 00106 forceinline 00107 DFS::Worker::Worker(Space* s, DFS& e) 00108 : Engine::Worker(s,e) {} 00109 forceinline 00110 DFS::DFS(Space* s, const Options& o) 00111 : Engine(o) { 00112 // Create workers 00113 _worker = static_cast<Worker**> 00114 (heap.ralloc(workers() * sizeof(Worker*))); 00115 // The first worker gets the entire search tree 00116 _worker[0] = new Worker(s,*this); 00117 // All other workers start with no work 00118 for (unsigned int i=1; i<workers(); i++) 00119 _worker[i] = new Worker(NULL,*this); 00120 // Block all workers 00121 block(); 00122 // Create and start threads 00123 for (unsigned int i=0; i<workers(); i++) 00124 Support::Thread::run(_worker[i]); 00125 } 00126 00127 /* 00128 * Reset 00129 */ 00130 forceinline void 00131 DFS::Worker::reset(Space* s, unsigned int ngdl) { 00132 delete cur; 00133 path.reset((s != NULL) ? ngdl : 0); 00134 d = 0; 00135 idle = false; 00136 if ((s == NULL) || (s->status(*this) == SS_FAILED)) { 00137 delete s; 00138 cur = NULL; 00139 } else { 00140 cur = s; 00141 } 00142 Search::Worker::reset(); 00143 } 00144 00145 00146 /* 00147 * Engine: search control 00148 */ 00149 forceinline void 00150 DFS::solution(Space* s) { 00151 m_search.acquire(); 00152 bool bs = signal(); 00153 solutions.push(s); 00154 if (bs) 00155 e_search.signal(); 00156 m_search.release(); 00157 } 00158 00159 00160 00161 /* 00162 * Worker: finding and stealing working 00163 */ 00164 forceinline void 00165 DFS::Worker::find(void) { 00166 // Try to find new work (even if there is none) 00167 for (unsigned int i=0; i<engine().workers(); i++) { 00168 unsigned long int r_d = 0ul; 00169 if (Space* s = engine().worker(i)->steal(r_d)) { 00170 // Reset this guy 00171 m.acquire(); 00172 idle = false; 00173 // Not idle but also does not have the root of the tree 00174 path.ngdl(0); 00175 d = 0; 00176 cur = s; 00177 Search::Worker::reset(r_d); 00178 m.release(); 00179 return; 00180 } 00181 } 00182 } 00183 00184 }}} 00185 00186 #endif 00187 00188 // STATISTICS: search-parallel