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: 2009-05-12 19:35:42 +0200 (Tue, 12 May 2009) $ by $Author: schulte $ 00011 * $Revision: 9071 $ 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/bab.hh> 00039 00040 namespace Gecode { namespace Search { namespace Parallel { 00041 00042 /* 00043 * Statistics 00044 */ 00045 Statistics 00046 BAB::statistics(void) const { 00047 Statistics s; 00048 for (unsigned int i=0; i<workers(); i++) 00049 s += worker(i)->statistics(); 00050 return s; 00051 } 00052 00053 /* 00054 * Actual work 00055 */ 00056 void 00057 BAB::Worker::run(void) { 00058 // Peform initial delay, if not first worker 00059 if (this != engine().worker(0)) 00060 Support::Thread::sleep(Config::initial_delay); 00061 // Okay, we are in business, start working 00062 while (true) { 00063 switch (engine().cmd()) { 00064 case C_WAIT: 00065 // Wait 00066 engine().wait(); 00067 break; 00068 case C_TERMINATE: 00069 // Acknowledge termination request 00070 engine().ack_terminate(); 00071 // Wait until termination can proceed 00072 engine().wait_terminate(); 00073 // Terminate thread 00074 engine().terminated(); 00075 return; 00076 case C_WORK: 00077 // Perform exploration work 00078 { 00079 m.acquire(); 00080 if (idle) { 00081 m.release(); 00082 // Try to find new work 00083 find(); 00084 } else if (cur != NULL) { 00085 start(); 00086 if (stop(engine().opt(),path.size())) { 00087 // Report stop 00088 m.release(); 00089 engine().stop(); 00090 } else { 00091 /* 00092 * The invariant maintained by the engine is: 00093 * For all nodes stored at a depth less than mark, there 00094 * is no guarantee of betterness. For those above the mark, 00095 * betterness is guaranteed. 00096 */ 00097 node++; 00098 switch (cur->status(*this)) { 00099 case SS_FAILED: 00100 fail++; 00101 delete cur; 00102 cur = NULL; 00103 Worker::current(NULL); 00104 m.release(); 00105 break; 00106 case SS_SOLVED: 00107 { 00108 // Deletes all pending branchings 00109 (void) cur->description(); 00110 Space* s = cur->clone(false); 00111 delete cur; 00112 cur = NULL; 00113 Worker::current(NULL); 00114 m.release(); 00115 engine().solution(s); 00116 } 00117 break; 00118 case SS_BRANCH: 00119 { 00120 Space* c; 00121 if ((d == 0) || (d >= engine().opt().c_d)) { 00122 c = cur->clone(); 00123 d = 1; 00124 } else { 00125 c = NULL; 00126 d++; 00127 } 00128 const BranchingDesc* desc = path.push(*this,cur,c); 00129 Worker::push(c,desc); 00130 cur->commit(*desc,0); 00131 m.release(); 00132 } 00133 break; 00134 default: 00135 GECODE_NEVER; 00136 } 00137 } 00138 } else if (path.next(*this)) { 00139 cur = path.recompute(d,engine().opt().a_d,*this,best,mark); 00140 Worker::current(cur); 00141 m.release(); 00142 } else { 00143 idle = true; 00144 m.release(); 00145 // Report that worker is idle 00146 engine().idle(); 00147 } 00148 } 00149 break; 00150 default: 00151 GECODE_NEVER; 00152 } 00153 } 00154 } 00155 00156 00157 /* 00158 * Termination and deletion 00159 */ 00160 BAB::Worker::~Worker(void) { 00161 delete best; 00162 } 00163 00164 BAB::~BAB(void) { 00165 terminate(); 00166 delete best; 00167 heap.rfree(_worker); 00168 } 00169 00170 }}} 00171 00172 // STATISTICS: search-parallel
