Generated on Sat Feb 7 2015 02:01:29 for Gecode by doxygen 1.8.9.1
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: 2015-01-08 15:07:24 +0100 (Thu, 08 Jan 2015) $ by $Author: schulte $
11  * $Revision: 14341 $
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  */
49  Statistics
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  /*
58  * Actual work
59  */
60  void
62  /*
63  * The engine maintains the following invariant:
64  * - If the current space (cur) is not NULL, the path always points
65  * to exactly that space.
66  * - If the current space (cur) is NULL, the path always points
67  * to the next space (if there is any).
68  *
69  * This invariant is needed so that no-goods can be extracted properly
70  * when the engine is stopped or has found a solution.
71  *
72  * An additional invariant maintained by the engine is:
73  * For all nodes stored at a depth less than mark, there
74  * is no guarantee of betterness. For those above the mark,
75  * betterness is guaranteed.
76  *
77  */
78  // Peform initial delay, if not first worker
79  if (this != engine().worker(0))
81  // Okay, we are in business, start working
82  while (true) {
83  switch (engine().cmd()) {
84  case C_WAIT:
85  // Wait
86  engine().wait();
87  break;
88  case C_TERMINATE:
89  // Acknowledge termination request
91  // Wait until termination can proceed
93  // Terminate thread
94  engine().terminated();
95  return;
96  case C_RESET:
97  // Acknowledge reset request
99  // Wait until reset has been performed
100  engine().wait_reset();
101  // Acknowledge that reset cycle is over
103  break;
104  case C_WORK:
105  // Perform exploration work
106  {
107  m.acquire();
108  if (idle) {
109  m.release();
110  // Try to find new work
111  find();
112  } else if (cur != NULL) {
113  start();
114  if (stop(engine().opt())) {
115  // Report stop
116  m.release();
117  engine().stop();
118  } else {
119  node++;
120  switch (cur->status(*this)) {
121  case SS_FAILED:
122  fail++;
123  delete cur;
124  cur = NULL;
125  path.next();
126  m.release();
127  break;
128  case SS_SOLVED:
129  {
130  // Deletes all pending branchers
131  (void) cur->choice();
132  Space* s = cur->clone(false);
133  delete cur;
134  cur = NULL;
135  path.next();
136  m.release();
137  engine().solution(s);
138  }
139  break;
140  case SS_BRANCH:
141  {
142  Space* c;
143  if ((d == 0) || (d >= engine().opt().c_d)) {
144  c = cur->clone();
145  d = 1;
146  } else {
147  c = NULL;
148  d++;
149  }
150  const Choice* ch = path.push(*this,cur,c);
151  cur->commit(*ch,0);
152  m.release();
153  }
154  break;
155  default:
156  GECODE_NEVER;
157  }
158  }
159  } else if (!path.empty()) {
160  cur = path.recompute(d,engine().opt().a_d,*this,*best,mark);
161  if (cur == NULL)
162  path.next();
163  m.release();
164  } else {
165  idle = true;
166  path.ngdl(0);
167  m.release();
168  // Report that worker is idle
169  engine().idle();
170  }
171  }
172  break;
173  default:
174  GECODE_NEVER;
175  }
176  }
177  }
178 
179 
180  /*
181  * Perform reset
182  *
183  */
184  void
186  // Grab wait lock for reset
188  // Release workers for reset
189  release(C_RESET);
190  // Wait for reset cycle started
192  // All workers are marked as busy again
193  delete best;
194  best = NULL;
195  n_busy = workers();
196  for (unsigned int i=1; i<workers(); i++)
197  worker(i)->reset(NULL,0);
198  worker(0)->reset(s,opt().nogoods_limit);
199  // Block workers again to ensure invariant
200  block();
201  // Release reset lock
203  // Wait for reset cycle stopped
205  }
206 
207 
208  /*
209  * Create no-goods
210  *
211  */
212  NoGoods&
213  BAB::nogoods(void) {
214  NoGoods* ng;
215  // Grab wait lock for reset
217  // Release workers for reset
218  release(C_RESET);
219  // Wait for reset cycle started
221  ng = &worker(0)->nogoods();
222  // Block workers again to ensure invariant
223  block();
224  // Release reset lock
226  // Wait for reset cycle stopped
228  return *ng;
229  }
230 
231  /*
232  * Termination and deletion
233  */
235  delete best;
236  }
237 
238  BAB::~BAB(void) {
239  terminate();
240  delete best;
241  heap.rfree(_worker);
242  }
243 
244 }}}
245 
246 #endif
247 
248 // STATISTICS: search-parallel
Support::Event e_reset_ack_start
Event for reset acknowledgment started.
Definition: engine.hh:147
Worker * worker(unsigned int i) const
Provide access to worker i.
Definition: bab.hh:110
Space must be branched (at least one brancher left)
Definition: core.hpp:1303
void ack_terminate(void)
For worker to acknowledge termination command.
Definition: engine.hh:335
void wait_terminate(void)
For worker to wait until termination is legal.
Definition: engine.hh:343
void wait_reset(void)
For worker to wait for all workers to reset.
Definition: engine.hh:385
void terminate(void)
For engine to peform thread termination.
Definition: engine.hh:349
Path path
Current path ins search tree.
Definition: engine.hh:59
Search engine statistics
Definition: search.hh:136
void rfree(void *p)
Free memory block starting at p.
Definition: heap.hpp:355
virtual ~BAB(void)
Destructor.
Definition: bab.cpp:238
virtual Statistics statistics(void) const
Return statistics.
Definition: bab.cpp:50
Space * clone(bool share=true, CloneStatistics &stat=unused_clone) const
Clone space.
Definition: core.hpp:2854
void idle(void)
Report that worker is idle.
Definition: engine.hh:293
Statistics statistics(void)
Return statistics.
Definition: engine.hh:277
const unsigned int initial_delay
Initial delay in milliseconds for all but first worker thread.
Definition: search.hh:104
virtual void reset(Space *s)
Reset engine to restart at space s.
Definition: bab.cpp:185
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:139
Perform reset operation.
Definition: engine.hh:94
void stop(void)
Report that worker has been stopped.
Definition: engine.hh:309
void block(void)
Block all workers.
Definition: engine.hh:222
void reset(Space *s, int ngdl)
Reset engine to restart at space s.
Definition: bab.hh:115
Computation spaces.
Definition: core.hpp:1362
volatile unsigned int n_busy
Number of busy workers.
Definition: engine.hh:171
Heap heap
The single global heap.
Definition: heap.cpp:49
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)
bool empty(void) const
Test whether path is empty.
Definition: path.hh:273
void wait(void)
Ensure that worker waits.
Definition: engine.hh:232
Gecode::IntArgs i(4, 1, 2, 3, 4)
Support::Mutex m_wait_reset
Mutex for waiting for reset.
Definition: engine.hh:151
unsigned int d
Distance until next clone.
Definition: engine.hh:63
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:99
Cmd cmd(void) const
Return current command.
Definition: engine.hh:218
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:2862
static void sleep(unsigned int ms)
Put current thread to sleep for ms milliseconds.
Definition: none.hpp:78
void terminated(void)
For worker to register termination.
Definition: engine.hh:323
Space * cur
Current space being explored.
Definition: engine.hh:61
unsigned int workers(void) const
Return number of workers.
Definition: engine.hh:209
virtual ~Worker(void)
Destructor.
Definition: bab.cpp:234
void find(void)
Try to find some work.
Definition: bab.hh:202
const Options & opt(void) const
Provide access to search options.
Definition: engine.hh:205
Space * recompute(unsigned int &d, unsigned int a_d, Worker &s)
Recompute space according to path.
Definition: path.hh:353
virtual NoGoods & nogoods(void)
Return no-goods.
Definition: bab.cpp:213
Space * best
Best solution found so far.
Definition: bab.hh:54
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
BAB & engine(void) const
Provide access to engine.
Definition: bab.hh:106
int ngdl(void) const
Return no-good depth limit.
Definition: path.hh:229
void ack_reset_stop(void)
For worker to acknowledge stop of reset cycle.
Definition: engine.hh:377
Choice for performing commit
Definition: core.hpp:1036
No-goods recorded from restarts.
Definition: core.hpp:1240
void release(Cmd c)
Release all workers.
Definition: engine.hh:227
void solution(Space *s)
Report solution s.
Definition: bab.hh:172
SpaceStatus status(StatusStatistics &stat=unused_status)
Query space status.
Definition: core.cpp:252
const unsigned int c_d
Create a clone after every c_d commits (commit distance)
Definition: search.hh:97
Gecode toplevel namespace
unsigned long int node
Number of nodes expanded.
Definition: search.hh:141
const unsigned int nogoods_limit
Depth limit for no-good generation during search.
Definition: search.hh:107
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:61
Space is failed
Definition: core.hpp:1301
const Choice * choice(void)
Create new choice for current brancher.
Definition: core.cpp:366
NoGoods & nogoods(void)
Return no-goods.
Definition: engine.hh:418
void ack_reset_start(void)
For worker to acknowledge start of reset cycle.
Definition: engine.hh:369
#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
Space is solved (no brancher left)
Definition: core.hpp:1302