dfs.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-26 21:10:00 +0200 (Wed, 26 Aug 2009) $ by $Author: schulte $ 00011 * $Revision: 9632 $ 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/dfs.hh> 00043 00044 namespace Gecode { namespace Search { namespace Parallel { 00045 00046 /* 00047 * Statistics 00048 */ 00049 Statistics 00050 DFS::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 00058 /* 00059 * Engine: search control 00060 */ 00061 void 00062 DFS::Worker::run(void) { 00063 // Peform initial delay, if not first worker 00064 if (this != engine().worker(0)) 00065 Support::Thread::sleep(Config::initial_delay); 00066 // Okay, we are in business, start working 00067 while (true) { 00068 switch (engine().cmd()) { 00069 case C_WAIT: 00070 // Wait 00071 engine().wait(); 00072 break; 00073 case C_TERMINATE: 00074 // Acknowledge termination request 00075 engine().ack_terminate(); 00076 // Wait until termination can proceed 00077 engine().wait_terminate(); 00078 // Terminate thread 00079 engine().terminated(); 00080 return; 00081 case C_RESET: 00082 // Acknowledge reset request 00083 engine().ack_reset_start(); 00084 // Wait until reset has been performed 00085 engine().wait_reset(); 00086 // Acknowledge that reset cycle is over 00087 engine().ack_reset_stop(); 00088 break; 00089 case C_WORK: 00090 // Perform exploration work 00091 { 00092 m.acquire(); 00093 if (idle) { 00094 m.release(); 00095 // Try to find new work 00096 find(); 00097 } else if (cur != NULL) { 00098 start(); 00099 if (stop(engine().opt(),path.size())) { 00100 // Report stop 00101 m.release(); 00102 engine().stop(); 00103 } else { 00104 node++; 00105 switch (cur->status(*this)) { 00106 case SS_FAILED: 00107 fail++; 00108 delete cur; 00109 cur = NULL; 00110 Worker::current(NULL); 00111 m.release(); 00112 break; 00113 case SS_SOLVED: 00114 { 00115 // Deletes all pending branchers 00116 (void) cur->choice(); 00117 Space* s = cur->clone(false); 00118 delete cur; 00119 cur = NULL; 00120 Worker::current(NULL); 00121 m.release(); 00122 engine().solution(s); 00123 } 00124 break; 00125 case SS_BRANCH: 00126 { 00127 Space* c; 00128 if ((d == 0) || (d >= engine().opt().c_d)) { 00129 c = cur->clone(); 00130 d = 1; 00131 } else { 00132 c = NULL; 00133 d++; 00134 } 00135 const Choice* ch = path.push(*this,cur,c); 00136 Worker::push(c,ch); 00137 cur->commit(*ch,0); 00138 m.release(); 00139 } 00140 break; 00141 default: 00142 GECODE_NEVER; 00143 } 00144 } 00145 } else if (path.next(*this)) { 00146 cur = path.recompute(d,engine().opt().a_d,*this); 00147 Worker::current(cur); 00148 m.release(); 00149 } else { 00150 idle = true; 00151 m.release(); 00152 // Report that worker is idle 00153 engine().idle(); 00154 } 00155 } 00156 break; 00157 default: 00158 GECODE_NEVER; 00159 } 00160 } 00161 } 00162 00163 00164 /* 00165 * Termination and deletion 00166 */ 00167 DFS::~DFS(void) { 00168 terminate(); 00169 heap.rfree(_worker); 00170 } 00171 00172 }}} 00173 00174 #endif 00175 00176 // STATISTICS: search-parallel