Generated on Sat Jul 28 2018 17:18:25 for Gecode by doxygen 1.8.14
bab.cpp
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, 2009
8  *
9  * Last modified:
10  * $Date: 2017-03-28 16:18:06 +0200 (Tue, 28 Mar 2017) $ by $Author: schulte $
11  * $Revision: 15620 $
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 <gecode/support.hh>
39 
40 #ifdef GECODE_HAS_THREADS
41 
43 
44 namespace Gecode { namespace Search { namespace Parallel {
45 
46  /*
47  * Statistics
48  */
50  BAB::statistics(void) const {
51  Statistics s;
52  for (unsigned int i=0; i<workers(); i++)
53  s += worker(i)->statistics();
54  return s;
55  }
56 
57  void
59  m_search.acquire();
60  if (best != NULL) {
61  best->constrain(b);
62  if (best->status() != SS_FAILED) {
63  m_search.release();
64  return;
65  }
66  delete best;
67  }
68  best = b.clone(false);
69  // Announce better solutions
70  for (unsigned int i=0; i<workers(); i++)
71  worker(i)->better(best);
72  m_search.release();
73  }
74 
75  /*
76  * Actual work
77  */
78  void
80  /*
81  * The engine maintains the following invariant:
82  * - If the current space (cur) is not NULL, the path always points
83  * to exactly that space.
84  * - If the current space (cur) is NULL, the path always points
85  * to the next space (if there is any).
86  *
87  * This invariant is needed so that no-goods can be extracted properly
88  * when the engine is stopped or has found a solution.
89  *
90  * An additional invariant maintained by the engine is:
91  * For all nodes stored at a depth less than mark, there
92  * is no guarantee of betterness. For those above the mark,
93  * betterness is guaranteed.
94  *
95  */
96  // Peform initial delay, if not first worker
97  if (this != engine().worker(0))
99  // Okay, we are in business, start working
100  while (true) {
101  switch (engine().cmd()) {
102  case C_WAIT:
103  // Wait
104  engine().wait();
105  break;
106  case C_TERMINATE:
107  // Acknowledge termination request
108  engine().ack_terminate();
109  // Wait until termination can proceed
111  // Terminate thread
112  engine().terminated();
113  return;
114  case C_RESET:
115  // Acknowledge reset request
117  // Wait until reset has been performed
118  engine().wait_reset();
119  // Acknowledge that reset cycle is over
121  break;
122  case C_WORK:
123  // Perform exploration work
124  {
125  m.acquire();
126  if (idle) {
127  m.release();
128  // Try to find new work
129  find();
130  } else if (cur != NULL) {
131  start();
132  if (stop(engine().opt())) {
133  // Report stop
134  m.release();
135  engine().stop();
136  } else {
137  node++;
138  switch (cur->status(*this)) {
139  case SS_FAILED:
140  fail++;
141  delete cur;
142  cur = NULL;
143  path.next();
144  m.release();
145  break;
146  case SS_SOLVED:
147  {
148  // Deletes all pending branchers
149  (void) cur->choice();
150  Space* s = cur->clone(false);
151  delete cur;
152  cur = NULL;
153  path.next();
154  m.release();
155  engine().solution(s);
156  }
157  break;
158  case SS_BRANCH:
159  {
160  Space* c;
161  if ((d == 0) || (d >= engine().opt().c_d)) {
162  c = cur->clone();
163  d = 1;
164  } else {
165  c = NULL;
166  d++;
167  }
168  const Choice* ch = path.push(*this,cur,c);
169  cur->commit(*ch,0);
170  m.release();
171  }
172  break;
173  default:
174  GECODE_NEVER;
175  }
176  }
177  } else if (!path.empty()) {
178  cur = path.recompute(d,engine().opt().a_d,*this,*best,mark);
179  if (cur == NULL)
180  path.next();
181  m.release();
182  } else {
183  idle = true;
184  path.ngdl(0);
185  m.release();
186  // Report that worker is idle
187  engine().idle();
188  }
189  }
190  break;
191  default:
192  GECODE_NEVER;
193  }
194  }
195  }
196 
197 
198  /*
199  * Perform reset
200  *
201  */
202  void
204  // Grab wait lock for reset
206  // Release workers for reset
207  release(C_RESET);
208  // Wait for reset cycle started
210  // All workers are marked as busy again
211  delete best;
212  best = NULL;
213  n_busy = workers();
214  for (unsigned int i=1; i<workers(); i++)
215  worker(i)->reset(NULL,0);
216  worker(0)->reset(s,opt().nogoods_limit);
217  // Block workers again to ensure invariant
218  block();
219  // Release reset lock
221  // Wait for reset cycle stopped
223  }
224 
225 
226  /*
227  * Create no-goods
228  *
229  */
230  NoGoods&
231  BAB::nogoods(void) {
232  NoGoods* ng;
233  // Grab wait lock for reset
235  // Release workers for reset
236  release(C_RESET);
237  // Wait for reset cycle started
239  ng = &worker(0)->nogoods();
240  // Block workers again to ensure invariant
241  block();
242  // Release reset lock
244  // Wait for reset cycle stopped
246  return *ng;
247  }
248 
249  /*
250  * Termination and deletion
251  */
253  delete best;
254  }
255 
256  BAB::~BAB(void) {
257  terminate();
258  delete best;
259  heap.rfree(_worker);
260  }
261 
262 }}}
263 
264 #endif
265 
266 // STATISTICS: search-parallel
unsigned int workers(void) const
Return number of workers.
Definition: engine.hh:209
Support::Event e_reset_ack_start
Event for reset acknowledgment started.
Definition: engine.hh:147
Space must be branched (at least one brancher left)
Definition: core.hpp:1691
void ack_terminate(void)
For worker to acknowledge termination command.
Definition: engine.hh:340
void wait_terminate(void)
For worker to wait until termination is legal.
Definition: engine.hh:348
void wait_reset(void)
For worker to wait for all workers to reset.
Definition: engine.hh:390
void terminate(void)
For engine to peform thread termination.
Definition: engine.hh:354
bool empty(void) const
Test whether path is empty.
Definition: path.hh:273
Path path
Current path ins search tree.
Definition: engine.hh:59
Search engine statistics
Definition: search.hh:140
void rfree(void *p)
Free memory block starting at p.
Definition: heap.hpp:375
virtual ~BAB(void)
Destructor.
Definition: bab.cpp:256
Worker * worker(unsigned int i) const
Provide access to worker i.
Definition: bab.hh:113
void idle(void)
Report that worker is idle.
Definition: engine.hh:298
Statistics statistics(void)
Return statistics.
Definition: engine.hh:282
const unsigned int initial_delay
Initial delay in milliseconds for all but first worker thread.
Definition: search.hh:116
virtual void reset(Space *s)
Reset engine to restart at space s.
Definition: bab.cpp:203
void acquire(void)
Acquire the mutex and possibly block.
Definition: none.hpp:46
unsigned long int fail
Number of failed nodes in search tree.
Definition: search.hh:143
Perform reset operation.
Definition: engine.hh:94
void stop(void)
Report that worker has been stopped.
Definition: engine.hh:314
Space * clone(bool share_data=true, bool share_info=true, CloneStatistics &stat=unused_clone) const
Clone space.
Definition: core.hpp:3326
void block(void)
Block all workers.
Definition: engine.hh:227
Computation spaces.
Definition: core.hpp:1748
const Options & opt(void) const
Provide access to search options.
Definition: engine.hh:205
void better(Space *b)
Accept better solution b.
Definition: bab.hh:166
volatile unsigned int n_busy
Number of busy workers.
Definition: engine.hh:171
void start(void)
Reset stop information.
Definition: worker.hh:78
void release(void)
Release the mutex.
Definition: none.hpp:52
Gecode::FloatVal c(-8, 8)
void wait(void)
Ensure that worker waits.
Definition: engine.hh:237
Gecode::IntArgs i(4, 1, 2, 3, 4)
Support::Mutex m_wait_reset
Mutex for waiting for reset.
Definition: engine.hh:151
virtual void constrain(const Space &b)
Constrain future solutions to be better than b.
Definition: bab.cpp:58
unsigned int d
Distance until next clone.
Definition: engine.hh:63
virtual Statistics statistics(void) const
Return statistics.
Definition: bab.cpp:50
bool idle
Whether the worker is idle.
Definition: engine.hh:65
const unsigned int a_d
Create a clone during recomputation if distance is greater than a_d (adaptive distance) ...
Definition: search.hh:111
const Choice * push(Worker &stat, Space *s, Space *c)
Push space c (a clone of s or NULL)
Definition: path.hh:239
void commit(const Choice &c, unsigned int a, CommitStatistics &stat=unused_commit)
Commit choice c for alternative a.
Definition: core.hpp:3334
static void sleep(unsigned int ms)
Put current thread to sleep for ms milliseconds.
Definition: none.hpp:78
virtual void constrain(const Space &best)
Constrain function for best solution search.
Definition: core.cpp:819
void terminated(void)
For worker to register termination.
Definition: engine.hh:328
struct Gecode::@579::NNF::@61::@62 b
For binary nodes (and, or, eqv)
Space * cur
Current space being explored.
Definition: engine.hh:61
virtual ~Worker(void)
Destructor.
Definition: bab.cpp:252
void find(void)
Try to find some work.
Definition: bab.hh:206
Space * recompute(unsigned int &d, unsigned int a_d, Worker &s)
Recompute space according to path.
Definition: path.hh:353
virtual NoGoods & nogoods(void)
Constrain Return no-goods.
Definition: bab.cpp:231
Space * best
Best solution found so far.
Definition: bab.hh:54
unsigned int ngdl(void) const
Return no-good depth limit.
Definition: path.hh:229
void next(void)
Generate path for next node.
Definition: path.hh:253
int mark
Number of entries not yet constrained to be better.
Definition: bab.hh:52
void ack_reset_stop(void)
For worker to acknowledge stop of reset cycle.
Definition: engine.hh:382
Choice for performing commit
Definition: core.hpp:1414
No-goods recorded from restarts.
Definition: core.hpp:1595
void release(Cmd c)
Release all workers.
Definition: engine.hh:232
Heap heap
The single global heap.
Definition: heap.cpp:48
BAB & engine(void) const
Provide access to engine.
Definition: bab.hh:109
void solution(Space *s)
Report solution s.
Definition: bab.hh:176
SpaceStatus status(StatusStatistics &stat=unused_status)
Query space status.
Definition: core.cpp:224
const unsigned int c_d
Create a clone after every c_d commits (commit distance)
Definition: search.hh:109
void reset(Space *s, unsigned int ngdl)
Reset engine to restart at space s.
Definition: bab.hh:118
Gecode toplevel namespace
unsigned long int node
Number of nodes expanded.
Definition: search.hh:145
const unsigned int nogoods_limit
Depth limit for no-good generation during search.
Definition: search.hh:127
Worker ** _worker
Array of worker references.
Definition: bab.hh:72
Space * best
Best solution so far.
Definition: bab.hh:74
virtual void run(void)
Start execution of worker.
Definition: bab.cpp:79
Space is failed
Definition: core.hpp:1689
const Choice * choice(void)
Create new choice for current brancher.
Definition: core.cpp:503
NoGoods & nogoods(void)
Return no-goods.
Definition: engine.hh:423
void ack_reset_start(void)
For worker to acknowledge start of reset cycle.
Definition: engine.hh:374
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
void wait(void)
Wait until the event becomes signalled.
Definition: none.hpp:65
bool stop(const Options &o)
Check whether engine must be stopped.
Definition: worker.hh:83
Support::Event e_reset_ack_stop
Event for reset acknowledgment stopped.
Definition: engine.hh:149
Support::Mutex m
Mutex for access to worker.
Definition: engine.hh:57
Cmd cmd(void) const
Return current command.
Definition: engine.hh:223
Space is solved (no brancher left)
Definition: core.hpp:1690
Support::Mutex m_search
Mutex for search.
Definition: engine.hh:165