bab.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: 2017-03-28 16:18:06 +0200 (Tue, 28 Mar 2017) $ by $Author: schulte $ 00011 * $Revision: 15620 $ 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/bab.hh> 00043 00044 namespace Gecode { namespace Search { namespace Parallel { 00045 00046 /* 00047 * Statistics 00048 */ 00049 Statistics 00050 BAB::statistics(void) const { 00051 Statistics s; 00052 for (unsigned int i=0; i<workers(); i++) 00053 s += worker(i)->statistics(); 00054 return s; 00055 } 00056 00057 void 00058 BAB::constrain(const Space& b) { 00059 m_search.acquire(); 00060 if (best != NULL) { 00061 best->constrain(b); 00062 if (best->status() != SS_FAILED) { 00063 m_search.release(); 00064 return; 00065 } 00066 delete best; 00067 } 00068 best = b.clone(false); 00069 // Announce better solutions 00070 for (unsigned int i=0; i<workers(); i++) 00071 worker(i)->better(best); 00072 m_search.release(); 00073 } 00074 00075 /* 00076 * Actual work 00077 */ 00078 void 00079 BAB::Worker::run(void) { 00080 /* 00081 * The engine maintains the following invariant: 00082 * - If the current space (cur) is not NULL, the path always points 00083 * to exactly that space. 00084 * - If the current space (cur) is NULL, the path always points 00085 * to the next space (if there is any). 00086 * 00087 * This invariant is needed so that no-goods can be extracted properly 00088 * when the engine is stopped or has found a solution. 00089 * 00090 * An additional invariant maintained by the engine is: 00091 * For all nodes stored at a depth less than mark, there 00092 * is no guarantee of betterness. For those above the mark, 00093 * betterness is guaranteed. 00094 * 00095 */ 00096 // Peform initial delay, if not first worker 00097 if (this != engine().worker(0)) 00098 Support::Thread::sleep(Config::initial_delay); 00099 // Okay, we are in business, start working 00100 while (true) { 00101 switch (engine().cmd()) { 00102 case C_WAIT: 00103 // Wait 00104 engine().wait(); 00105 break; 00106 case C_TERMINATE: 00107 // Acknowledge termination request 00108 engine().ack_terminate(); 00109 // Wait until termination can proceed 00110 engine().wait_terminate(); 00111 // Terminate thread 00112 engine().terminated(); 00113 return; 00114 case C_RESET: 00115 // Acknowledge reset request 00116 engine().ack_reset_start(); 00117 // Wait until reset has been performed 00118 engine().wait_reset(); 00119 // Acknowledge that reset cycle is over 00120 engine().ack_reset_stop(); 00121 break; 00122 case C_WORK: 00123 // Perform exploration work 00124 { 00125 m.acquire(); 00126 if (idle) { 00127 m.release(); 00128 // Try to find new work 00129 find(); 00130 } else if (cur != NULL) { 00131 start(); 00132 if (stop(engine().opt())) { 00133 // Report stop 00134 m.release(); 00135 engine().stop(); 00136 } else { 00137 node++; 00138 switch (cur->status(*this)) { 00139 case SS_FAILED: 00140 fail++; 00141 delete cur; 00142 cur = NULL; 00143 path.next(); 00144 m.release(); 00145 break; 00146 case SS_SOLVED: 00147 { 00148 // Deletes all pending branchers 00149 (void) cur->choice(); 00150 Space* s = cur->clone(false); 00151 delete cur; 00152 cur = NULL; 00153 path.next(); 00154 m.release(); 00155 engine().solution(s); 00156 } 00157 break; 00158 case SS_BRANCH: 00159 { 00160 Space* c; 00161 if ((d == 0) || (d >= engine().opt().c_d)) { 00162 c = cur->clone(); 00163 d = 1; 00164 } else { 00165 c = NULL; 00166 d++; 00167 } 00168 const Choice* ch = path.push(*this,cur,c); 00169 cur->commit(*ch,0); 00170 m.release(); 00171 } 00172 break; 00173 default: 00174 GECODE_NEVER; 00175 } 00176 } 00177 } else if (!path.empty()) { 00178 cur = path.recompute(d,engine().opt().a_d,*this,*best,mark); 00179 if (cur == NULL) 00180 path.next(); 00181 m.release(); 00182 } else { 00183 idle = true; 00184 path.ngdl(0); 00185 m.release(); 00186 // Report that worker is idle 00187 engine().idle(); 00188 } 00189 } 00190 break; 00191 default: 00192 GECODE_NEVER; 00193 } 00194 } 00195 } 00196 00197 00198 /* 00199 * Perform reset 00200 * 00201 */ 00202 void 00203 BAB::reset(Space* s) { 00204 // Grab wait lock for reset 00205 m_wait_reset.acquire(); 00206 // Release workers for reset 00207 release(C_RESET); 00208 // Wait for reset cycle started 00209 e_reset_ack_start.wait(); 00210 // All workers are marked as busy again 00211 delete best; 00212 best = NULL; 00213 n_busy = workers(); 00214 for (unsigned int i=1; i<workers(); i++) 00215 worker(i)->reset(NULL,0); 00216 worker(0)->reset(s,opt().nogoods_limit); 00217 // Block workers again to ensure invariant 00218 block(); 00219 // Release reset lock 00220 m_wait_reset.release(); 00221 // Wait for reset cycle stopped 00222 e_reset_ack_stop.wait(); 00223 } 00224 00225 00226 /* 00227 * Create no-goods 00228 * 00229 */ 00230 NoGoods& 00231 BAB::nogoods(void) { 00232 NoGoods* ng; 00233 // Grab wait lock for reset 00234 m_wait_reset.acquire(); 00235 // Release workers for reset 00236 release(C_RESET); 00237 // Wait for reset cycle started 00238 e_reset_ack_start.wait(); 00239 ng = &worker(0)->nogoods(); 00240 // Block workers again to ensure invariant 00241 block(); 00242 // Release reset lock 00243 m_wait_reset.release(); 00244 // Wait for reset cycle stopped 00245 e_reset_ack_stop.wait(); 00246 return *ng; 00247 } 00248 00249 /* 00250 * Termination and deletion 00251 */ 00252 BAB::Worker::~Worker(void) { 00253 delete best; 00254 } 00255 00256 BAB::~BAB(void) { 00257 terminate(); 00258 delete best; 00259 heap.rfree(_worker); 00260 } 00261 00262 }}} 00263 00264 #endif 00265 00266 // STATISTICS: search-parallel