Generated on Mon Nov 25 2013 15:56:11 for Gecode by doxygen 1.8.5
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: 2013-07-12 18:20:11 +0200 (Fri, 12 Jul 2013) $ by $Author: schulte $
11  * $Revision: 13877 $
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  // Peform initial delay, if not first worker
63  if (this != engine().worker(0))
65  // Okay, we are in business, start working
66  while (true) {
67  switch (engine().cmd()) {
68  case C_WAIT:
69  // Wait
70  engine().wait();
71  break;
72  case C_TERMINATE:
73  // Acknowledge termination request
75  // Wait until termination can proceed
77  // Terminate thread
78  engine().terminated();
79  return;
80  case C_RESET:
81  // Acknowledge reset request
83  // Wait until reset has been performed
84  engine().wait_reset();
85  // Acknowledge that reset cycle is over
87  break;
88  case C_WORK:
89  // Perform exploration work
90  {
91  m.acquire();
92  if (idle) {
93  m.release();
94  // Try to find new work
95  find();
96  } else if (cur != NULL) {
97  start();
98  if (stop(engine().opt())) {
99  // Report stop
100  m.release();
101  engine().stop();
102  } else {
103  /*
104  * The invariant maintained by the engine is:
105  * For all nodes stored at a depth less than mark, there
106  * is no guarantee of betterness. For those above the mark,
107  * betterness is guaranteed.
108  */
109  node++;
110  switch (cur->status(*this)) {
111  case SS_FAILED:
112  fail++;
113  delete cur;
114  cur = NULL;
115  m.release();
116  break;
117  case SS_SOLVED:
118  {
119  // Deletes all pending branchers
120  (void) cur->choice();
121  Space* s = cur->clone(false);
122  delete cur;
123  cur = NULL;
124  m.release();
125  engine().solution(s);
126  }
127  break;
128  case SS_BRANCH:
129  {
130  Space* c;
131  if ((d == 0) || (d >= engine().opt().c_d)) {
132  c = cur->clone();
133  d = 1;
134  } else {
135  c = NULL;
136  d++;
137  }
138  const Choice* ch = path.push(*this,cur,c);
139  cur->commit(*ch,0);
140  m.release();
141  }
142  break;
143  default:
144  GECODE_NEVER;
145  }
146  }
147  } else if (path.next()) {
148  cur = path.recompute(d,engine().opt().a_d,*this,best,mark);
149  m.release();
150  } else {
151  idle = true;
152  path.ngdl(0);
153  m.release();
154  // Report that worker is idle
155  engine().idle();
156  }
157  }
158  break;
159  default:
160  GECODE_NEVER;
161  }
162  }
163  }
164 
165 
166  /*
167  * Perform reset
168  *
169  */
170  void
172  // Grab wait lock for reset
174  // Release workers for reset
175  release(C_RESET);
176  // Wait for reset cycle started
178  // All workers are marked as busy again
179  delete best;
180  best = NULL;
181  n_busy = workers();
182  for (unsigned int i=1; i<workers(); i++)
183  worker(i)->reset(NULL,0);
184  worker(0)->reset(s,opt().nogoods_limit);
185  // Block workers again to ensure invariant
186  block();
187  // Release reset lock
189  // Wait for reset cycle stopped
191  }
192 
193 
194  /*
195  * Create no-goods
196  *
197  */
198  NoGoods&
199  BAB::nogoods(void) {
200  NoGoods* ng;
201  // Grab wait lock for reset
203  // Release workers for reset
204  release(C_RESET);
205  // Wait for reset cycle started
207  ng = &worker(0)->nogoods();
208  // Block workers again to ensure invariant
209  block();
210  // Release reset lock
212  // Wait for reset cycle stopped
214  return *ng;
215  }
216 
217  /*
218  * Termination and deletion
219  */
221  delete best;
222  }
223 
224  BAB::~BAB(void) {
225  terminate();
226  delete best;
227  heap.rfree(_worker);
228  }
229 
230 }}}
231 
232 #endif
233 
234 // 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:1266
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:224
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:2777
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:171
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:1325
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)
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
bool next(void)
Generate path for next node and return whether a next node exists.
Definition: path.hh:253
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:2785
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:220
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:354
virtual NoGoods & nogoods(void)
Return no-goods.
Definition: bab.cpp:199
Space * best
Best solution found so far.
Definition: bab.hh:54
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:251
const unsigned int c_d
Create a clone after every c_d commits (commit distance)
Definition: search.hh:97
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:1264
const Choice * choice(void)
Create new choice for current brancher.
Definition: core.cpp:365
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:1265