worker.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, 2004 00008 * 00009 * Last modified: 00010 * $Date: 2009-08-26 15:58:07 +0200 (Wed, 26 Aug 2009) $ by $Author: schulte $ 00011 * $Revision: 9628 $ 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_WORKER_HH__ 00039 #define __GECODE_SEARCH_WORKER_HH__ 00040 00041 #include <gecode/search.hh> 00042 00043 namespace Gecode { namespace Search { 00044 00048 class Worker : public Statistics { 00049 protected: 00051 bool _stopped; 00053 size_t mem_space; 00055 size_t mem_cur; 00057 size_t mem_total; 00059 unsigned long int root_depth; 00060 public: 00062 Worker(size_t sz); 00064 void start(void); 00066 bool stop(const Options& o, size_t sz); 00068 bool stopped(void) const; 00070 void push(const Space* s, const Choice* c); 00072 void constrained(const Space* s1, const Space* s2); 00074 void adapt(const Space* s); 00076 void pop(const Space* s, const Choice* c); 00078 void lao(const Space* s); 00080 void current(const Space* s); 00082 void reset(const Space* s, unsigned long int d=0); 00084 void reset(void); 00086 void stack_depth(unsigned long int d); 00088 unsigned long int steal_depth(unsigned long int d) const; 00089 }; 00090 00091 00092 00093 forceinline 00094 Worker::Worker(size_t sz) 00095 : _stopped(false), mem_space(sz), mem_cur(0), mem_total(0), 00096 root_depth(0) { 00097 memory = 0; 00098 } 00099 00100 forceinline void 00101 Worker::start(void) { 00102 _stopped = false; 00103 } 00104 00105 forceinline bool 00106 Worker::stop(const Options& o, size_t sz) { 00107 if (o.stop == NULL) 00108 return false; 00109 memory += sz; 00110 _stopped |= o.stop->stop(*this,o); 00111 memory -= sz; 00112 return _stopped; 00113 } 00114 00115 forceinline bool 00116 Worker::stopped(void) const { 00117 return _stopped; 00118 } 00119 00120 forceinline void 00121 Worker::push(const Space* s, const Choice* c) { 00122 if (s != NULL) 00123 mem_total += mem_space + s->allocated(); 00124 mem_total += c->size(); 00125 if (mem_total > memory) 00126 memory = mem_total; 00127 } 00128 00129 forceinline void 00130 Worker::adapt(const Space* s) { 00131 mem_total += mem_space + s->allocated(); 00132 if (mem_total > memory) 00133 memory = mem_total; 00134 } 00135 00136 forceinline void 00137 Worker::constrained(const Space* s1, const Space* s2) { 00138 mem_total -= s1->allocated(); 00139 mem_total += s2->allocated(); 00140 if (mem_total > memory) 00141 memory = mem_total; 00142 } 00143 00144 forceinline void 00145 Worker::lao(const Space* s) { 00146 mem_total -= mem_space + s->allocated(); 00147 } 00148 00149 forceinline void 00150 Worker::pop(const Space* s, const Choice* c) { 00151 if (s != NULL) 00152 mem_total -= mem_space + s->allocated(); 00153 mem_total -= c->size(); 00154 } 00155 00156 forceinline void 00157 Worker::current(const Space* s) { 00158 if (s == NULL) { 00159 mem_total -= mem_cur; 00160 mem_cur = 0; 00161 } else { 00162 mem_cur = mem_space + s->allocated(); 00163 mem_total += mem_cur; 00164 if (mem_total > memory) 00165 memory = mem_total; 00166 } 00167 } 00168 00169 forceinline void 00170 Worker::reset(const Space* s, unsigned long int d) { 00171 mem_cur = mem_space + s->allocated(); 00172 mem_total = mem_cur; 00173 if (mem_total > memory) 00174 memory = mem_total; 00175 root_depth = d; 00176 if (depth < d) 00177 depth = d; 00178 } 00179 00180 forceinline void 00181 Worker::reset(void) { 00182 mem_cur = 0; 00183 mem_total = 0; 00184 root_depth = 0; 00185 } 00186 00187 forceinline void 00188 Worker::stack_depth(unsigned long int d) { 00189 if (depth < root_depth + d) 00190 depth = root_depth + d; 00191 } 00192 00193 forceinline unsigned long int 00194 Worker::steal_depth(unsigned long int d) const { 00195 return root_depth + d; 00196 } 00197 00198 }} 00199 00200 #endif 00201 00202 // STATISTICS: search-other