Generated on Thu Apr 5 2018 19:44:19 for Gecode by doxygen 1.8.13
pbs.hpp
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, 2015
8  *
9  * Last modified:
10  * $Date$ by $Author$
11  * $Revision$
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 #include <algorithm>
39 
40 namespace Gecode { namespace Search { namespace Par {
41 
42 
45  : solutions(heap) {}
46  forceinline bool
48  solutions.push(s);
49  return true;
50  }
51  forceinline bool
53  (void) b;
54  return false;
55  }
56  forceinline bool
57  CollectAll::empty(void) const {
58  return solutions.empty();
59  }
62  return solutions.pop();
63  }
66  while (!solutions.empty())
67  delete solutions.pop();
68  }
69 
70 
73  : b(NULL), reporter(NULL) {}
74  forceinline bool
76  if (b != NULL) {
77  b->constrain(*s);
78  if (b->status() == SS_FAILED) {
79  delete b;
80  } else {
81  delete s;
82  return false;
83  }
84  }
85  b = s;
86  reporter = r;
87  return true;
88  }
89  forceinline bool
91  if (b != NULL) {
92  b->constrain(s);
93  if (b->status() == SS_FAILED) {
94  delete b;
95  } else {
96  return false;
97  }
98  }
99  b = s.clone();
100  reporter = NULL;
101  return true;
102  }
103  forceinline bool
104  CollectBest::empty(void) const {
105  return reporter == NULL;
106  }
109  assert(!empty());
110  r = reporter;
111  reporter = NULL;
112  return b->clone();
113  }
116  delete b;
117  }
118 
119 
122  : so(so0), tostop(NULL) {}
123 
124  forceinline void
125  PortfolioStop::share(volatile bool* ts) {
126  tostop = ts;
127  }
128 
129 
130  template<class Collect>
133  : Support::Runnable(false), master(m), slave(s), stop(so) {}
134  template<class Collect>
137  return slave->statistics();
138  }
139  template<class Collect>
140  forceinline bool
142  return slave->stopped();
143  }
144  template<class Collect>
145  forceinline void
147  slave->constrain(b);
148  }
149  template<class Collect>
151  delete slave;
152  delete stop;
153  }
154 
155 
156 
157  template<class Collect>
159  PBS<Collect>::PBS(Engine** engines, Stop** stops, unsigned int n,
160  const Statistics& stat0)
161  : stat(stat0), slaves(heap.alloc<Slave<Collect>*>(n)),
162  n_slaves(n), n_active(n),
163  slave_stop(false), tostop(false), n_busy(0) {
164  // Initialize slaves
165  for (unsigned int i=n_slaves; i--; ) {
166  slaves[i] = new Slave<Collect>(this,engines[i],stops[i]);
167  static_cast<PortfolioStop*>(stops[i])->share(&tostop);
168  }
169  }
170 
171 
172  template<class Collect>
173  forceinline bool
175  // If b is false the report should be repeated (solution was worse)
176  bool b = true;
177  m.acquire();
178  if (s != NULL) {
179  b = solutions.add(s,slave);
180  if (b)
181  tostop = true;
182  } else if (slave->stopped()) {
183  if (!tostop)
184  slave_stop = true;
185  } else {
186  // Move slave to inactive, as it has exhausted its engine
187  unsigned int i=0;
188  while (slaves[i] != slave)
189  i++;
190  assert(i < n_active);
191  assert(n_active > 0);
192  std::swap(slaves[i],slaves[--n_active]);
193  tostop = true;
194  }
195  if (b) {
196  if (--n_busy == 0)
197  idle.signal();
198  }
199  m.release();
200  return b;
201  }
202 
203  template<class Collect>
204  void
206  Space* s;
207  do {
208  s = slave->next();
209  } while (!master->report(this,s));
210  }
211 
212  template<class Collect>
213  Space*
215  m.acquire();
216  if (solutions.empty()) {
217  // Clear all
218  tostop = false;
219  slave_stop = false;
220 
221  // Invariant: all slaves are idle!
222  assert(n_busy == 0);
223  assert(!tostop);
224 
225  if (n_active > 0) {
226  // Run all active slaves
227  n_busy = n_active;
228  for (unsigned int i=n_active; i--; )
229  Support::Thread::run(slaves[i]);
230  m.release();
231  // Wait for all slaves to become idle
232  idle.wait();
233  m.acquire();
234  }
235  }
236 
237  // Invariant all slaves are idle!
238  assert(n_busy == 0);
239 
240  Space* s;
241 
242  // Process solutions
243  if (solutions.empty()) {
244  s = NULL;
245  } else {
246  Slave<Collect>* r;
247  s = solutions.get(r);
248  if (Collect::best)
249  for (unsigned int i=n_active; i--; )
250  if (slaves[i] != r)
251  slaves[i]->constrain(*s);
252  }
253 
254  m.release();
255  return s;
256  }
257 
258  template<class Collect>
259  bool
260  PBS<Collect>::stopped(void) const {
261  return slave_stop;
262  }
263 
264  template<class Collect>
265  Statistics
267  assert(n_busy == 0);
268  Statistics s(stat);
269  for (unsigned int i=n_slaves; i--; )
270  s += slaves[i]->statistics();
271  return s;
272  }
273 
274  template<class Collect>
275  void
277  assert(n_busy == 0);
278  if (!Collect::best)
279  throw NoBest("PBS::constrain");
280  if (solutions.constrain(b)) {
281  // The solution is better
282  for (unsigned int i=n_active; i--; )
283  slaves[i]->constrain(b);
284  }
285  }
286 
287  template<class Collect>
289  assert(n_busy == 0);
290  heap.free<Slave<Collect>*>(slaves,n_slaves);
291  }
292 
293 }}}
294 
295 // STATISTICS: search-par
bool stopped(void) const
Check whether slave has been stopped.
Definition: pbs.hpp:141
bool empty(void) const
Check whether there is any solution left.
Definition: pbs.hpp:57
Space * b
Currently best solution.
Definition: pbs.hh:120
Search engine statistics
Definition: search.hh:149
CollectBest(void)
Initialize.
Definition: pbs.hpp:72
void constrain(const Space &b)
Constrain with better solution b.
Definition: pbs.hpp:146
void stop(Support::Timer &timer, std::ostream &os)
Get time since start of timer and print user friendly time information.
Definition: script.cpp:46
bool add(Space *s, Slave< CollectBest > *r)
Add a solution s by r and return whether is was better.
Definition: pbs.hpp:75
bool empty(void) const
Check whether there is any solution left.
Definition: pbs.hpp:104
~CollectBest(void)
Destructor.
Definition: pbs.hpp:115
#define forceinline
Definition: config.hpp:182
Parallel depth-first search engine
Definition: engine.hh:50
Computation spaces.
Definition: core.hpp:1668
Slave(PBS< Collect > *m, Engine *s, Stop *so)
Initialize with master m, slave s, and its stop object so.
Definition: pbs.hpp:132
PortfolioStop(Stop *so)
Initialize.
Definition: pbs.hpp:121
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Slave< CollectBest > * reporter
Who has reported the best solution (NULL if solution has already been reported)
Definition: pbs.hh:122
~CollectAll(void)
Destructor.
Definition: pbs.hpp:65
Space * get(Slave< CollectBest > *&r)
Return solution reported by r (only if a better one was found)
Definition: pbs.hpp:108
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
Space * clone(CloneStatistics &stat=unused_clone) const
Clone space.
Definition: core.hpp:3144
bool stop(void) const
Whether search must be stopped.
void share(volatile bool *ts)
Set pointer to shared tostop variable.
Definition: pbs.hpp:125
bool constrain(const Space &b)
Dummy function.
Definition: pbs.hpp:52
virtual void constrain(const Space &best)
Constrain function for best solution search.
Definition: core.cpp:807
Space * get(Slave< CollectAll > *&r)
Return solution reported by r.
Definition: pbs.hpp:61
Support::DynamicQueue< Space *, Heap > solutions
Queue of solutions.
Definition: pbs.hh:98
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:769
struct Gecode::@585::NNF::@62::@63 b
For binary nodes (and, or, eqv)
void free(T *b, long unsigned int n)
Delete n objects starting at b.
Definition: heap.hpp:461
Stop object used for controling slaves in a portfolio.
Definition: pbs.hh:46
Statistics statistics(void) const
Return statistics of slave.
Definition: pbs.hpp:136
Heap heap
The single global heap.
Definition: heap.cpp:48
Parallel portfolio engine implementation.
Definition: pbs.hh:67
SpaceStatus status(StatusStatistics &stat=unused_status)
Query space status.
Definition: core.cpp:229
Runnable slave of a portfolio master.
Definition: pbs.hh:71
Exception: Best solution search is not supported
Definition: exception.hpp:64
bool constrain(const Space &b)
Check whether b better and update accordingly.
Definition: pbs.hpp:90
Gecode toplevel namespace
Space is failed
Definition: core.hpp:1608
Base-class for Stop-object.
Definition: search.hh:801
bool add(Space *s, Slave< CollectAll > *r)
Add a solution a reported by r and always return true.
Definition: pbs.hpp:47
int solutions(TestSpace *c, Gecode::Search::Options &o, int maxNbSol=-1)
Find number of solutions.
Definition: branch.cpp:416
IntRelType swap(IntRelType irt)
Return swapped relation type of irt.
Definition: irt.hpp:41
CollectAll(void)
Initialize.
Definition: pbs.hpp:44