Generated on Sat Aug 25 2012 03:32:53 for Gecode by doxygen 1.8.1.2
worker.hh
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2004
8  *
9  * Last modified:
10  * $Date: 2009-08-26 23:58:07 +1000 (Wed, 26 Aug 2009) $ by $Author: schulte $
11  * $Revision: 9628 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #ifndef __GECODE_SEARCH_WORKER_HH__
39 #define __GECODE_SEARCH_WORKER_HH__
40 
41 #include <gecode/search.hh>
42 
43 namespace Gecode { namespace Search {
44 
48  class Worker : public Statistics {
49  protected:
51  bool _stopped;
53  size_t mem_space;
55  size_t mem_cur;
57  size_t mem_total;
59  unsigned long int root_depth;
60  public:
62  Worker(size_t sz);
64  void start(void);
66  bool stop(const Options& o, size_t sz);
68  bool stopped(void) const;
70  void push(const Space* s, const Choice* c);
72  void constrained(const Space* s1, const Space* s2);
74  void adapt(const Space* s);
76  void pop(const Space* s, const Choice* c);
78  void lao(const Space* s);
80  void current(const Space* s);
82  void reset(const Space* s, unsigned long int d=0);
84  void reset(void);
86  void stack_depth(unsigned long int d);
88  unsigned long int steal_depth(unsigned long int d) const;
89  };
90 
91 
92 
94  Worker::Worker(size_t sz)
95  : _stopped(false), mem_space(sz), mem_cur(0), mem_total(0),
96  root_depth(0) {
97  memory = 0;
98  }
99 
100  forceinline void
102  _stopped = false;
103  }
104 
105  forceinline bool
106  Worker::stop(const Options& o, size_t sz) {
107  if (o.stop == NULL)
108  return false;
109  memory += sz;
110  _stopped |= o.stop->stop(*this,o);
111  memory -= sz;
112  return _stopped;
113  }
114 
115  forceinline bool
116  Worker::stopped(void) const {
117  return _stopped;
118  }
119 
120  forceinline void
121  Worker::push(const Space* s, const Choice* c) {
122  if (s != NULL)
123  mem_total += mem_space + s->allocated();
124  mem_total += c->size();
125  if (mem_total > memory)
126  memory = mem_total;
127  }
128 
129  forceinline void
130  Worker::adapt(const Space* s) {
131  mem_total += mem_space + s->allocated();
132  if (mem_total > memory)
133  memory = mem_total;
134  }
135 
136  forceinline void
137  Worker::constrained(const Space* s1, const Space* s2) {
138  mem_total -= s1->allocated();
139  mem_total += s2->allocated();
140  if (mem_total > memory)
141  memory = mem_total;
142  }
143 
144  forceinline void
145  Worker::lao(const Space* s) {
146  mem_total -= mem_space + s->allocated();
147  }
148 
149  forceinline void
150  Worker::pop(const Space* s, const Choice* c) {
151  if (s != NULL)
152  mem_total -= mem_space + s->allocated();
153  mem_total -= c->size();
154  }
155 
156  forceinline void
157  Worker::current(const Space* s) {
158  if (s == NULL) {
159  mem_total -= mem_cur;
160  mem_cur = 0;
161  } else {
162  mem_cur = mem_space + s->allocated();
163  mem_total += mem_cur;
164  if (mem_total > memory)
165  memory = mem_total;
166  }
167  }
168 
169  forceinline void
170  Worker::reset(const Space* s, unsigned long int d) {
171  mem_cur = mem_space + s->allocated();
172  mem_total = mem_cur;
173  if (mem_total > memory)
174  memory = mem_total;
175  root_depth = d;
176  if (depth < d)
177  depth = d;
178  }
179 
180  forceinline void
182  mem_cur = 0;
183  mem_total = 0;
184  root_depth = 0;
185  }
186 
187  forceinline void
188  Worker::stack_depth(unsigned long int d) {
189  if (depth < root_depth + d)
190  depth = root_depth + d;
191  }
192 
193  forceinline unsigned long int
194  Worker::steal_depth(unsigned long int d) const {
195  return root_depth + d;
196  }
197 
198 }}
199 
200 #endif
201 
202 // STATISTICS: search-other