Generated on Sat Jul 29 2017 12:41:24 for Gecode by doxygen 1.8.13
dfs.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: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $ by $Author: schulte $
11  * $Revision: 14967 $
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  DFS::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  /*
59  * Engine: search control
60  */
61  void
63  /*
64  * The engine maintains the following invariant:
65  * - If the current space (cur) is not NULL, the path always points
66  * to exactly that space.
67  * - If the current space (cur) is NULL, the path always points
68  * to the next space (if there is any).
69  *
70  * This invariant is needed so that no-goods can be extracted properly
71  * when the engine is stopped or has found a solution.
72  *
73  */
74  // Peform initial delay, if not first worker
75  if (this != engine().worker(0))
77  // Okay, we are in business, start working
78  while (true) {
79  switch (engine().cmd()) {
80  case C_WAIT:
81  // Wait
82  engine().wait();
83  break;
84  case C_TERMINATE:
85  // Acknowledge termination request
86  engine().ack_terminate();
87  // Wait until termination can proceed
88  engine().wait_terminate();
89  // Terminate thread
90  engine().terminated();
91  return;
92  case C_RESET:
93  // Acknowledge reset request
94  engine().ack_reset_start();
95  // Wait until reset has been performed
96  engine().wait_reset();
97  // Acknowledge that reset cycle is over
98  engine().ack_reset_stop();
99  break;
100  case C_WORK:
101  // Perform exploration work
102  {
103  m.acquire();
104  if (idle) {
105  m.release();
106  // Try to find new work
107  find();
108  } else if (cur != NULL) {
109  start();
110  if (stop(engine().opt())) {
111  // Report stop
112  m.release();
113  engine().stop();
114  } else {
115  node++;
116  switch (cur->status(*this)) {
117  case SS_FAILED:
118  fail++;
119  delete cur;
120  cur = NULL;
121  path.next();
122  m.release();
123  break;
124  case SS_SOLVED:
125  {
126  // Deletes all pending branchers
127  (void) cur->choice();
128  Space* s = cur->clone(false);
129  delete cur;
130  cur = NULL;
131  path.next();
132  m.release();
133  engine().solution(s);
134  }
135  break;
136  case SS_BRANCH:
137  {
138  Space* c;
139  if ((d == 0) || (d >= engine().opt().c_d)) {
140  c = cur->clone();
141  d = 1;
142  } else {
143  c = NULL;
144  d++;
145  }
146  const Choice* ch = path.push(*this,cur,c);
147  cur->commit(*ch,0);
148  m.release();
149  }
150  break;
151  default:
152  GECODE_NEVER;
153  }
154  }
155  } else if (!path.empty()) {
156  cur = path.recompute(d,engine().opt().a_d,*this);
157  if (cur == NULL)
158  path.next();
159  m.release();
160  } else {
161  idle = true;
162  path.ngdl(0);
163  m.release();
164  // Report that worker is idle
165  engine().idle();
166  }
167  }
168  break;
169  default:
170  GECODE_NEVER;
171  }
172  }
173  }
174 
175 
176  /*
177  * Perform reset
178  *
179  */
180  void
182  // Grab wait lock for reset
184  // Release workers for reset
185  release(C_RESET);
186  // Wait for reset cycle started
188  // All workers are marked as busy again
189  n_busy = workers();
190  for (unsigned int i=1U; i<workers(); i++)
191  worker(i)->reset(NULL,0);
192  worker(0U)->reset(s,opt().nogoods_limit);
193  // Block workers again to ensure invariant
194  block();
195  // Release reset lock
197  // Wait for reset cycle stopped
199  }
200 
201 
202 
203  /*
204  * Create no-goods
205  *
206  */
207  NoGoods&
208  DFS::nogoods(void) {
209  NoGoods* ng;
210  // Grab wait lock for reset
212  // Release workers for reset
213  release(C_RESET);
214  // Wait for reset cycle started
216  ng = &worker(0)->nogoods();
217  // Block workers again to ensure invariant
218  block();
219  // Release reset lock
221  // Wait for reset cycle stopped
223  return *ng;
224  }
225 
226 
227  /*
228  * Termination and deletion
229  */
230  DFS::~DFS(void) {
231  terminate();
232  heap.rfree(_worker);
233  }
234 
235 }}}
236 
237 #endif
238 
239 // 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 terminate(void)
For engine to peform thread termination.
Definition: engine.hh:354
Search engine statistics
Definition: search.hh:140
void rfree(void *p)
Free memory block starting at p.
Definition: heap.hpp:375
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
void path(Home home, int offset, const IntVarArgs &x, IntVar s, IntVar e, IntPropLevel ipl)
Post propagator such that x forms a Hamiltonian path.
Definition: circuit.cpp:128
virtual Statistics statistics(void) const
Return statistics.
Definition: dfs.cpp:50
void acquire(void)
Acquire the mutex and possibly block.
Definition: none.hpp:46
Perform reset operation.
Definition: engine.hh:94
void stop(void)
Report that worker has been stopped.
Definition: engine.hh:314
virtual void run(void)
Start execution of worker.
Definition: dfs.cpp:62
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
Engine * engine(Engine **slaves, Stop **stops, unsigned int n_slaves, const Statistics &stat, const Search::Options &opt, bool best)
Create sequential portfolio engine.
Definition: pbs.cpp:48
Computation spaces.
Definition: core.hpp:1748
const Options & opt(void) const
Provide access to search options.
Definition: engine.hh:205
volatile unsigned int n_busy
Number of busy workers.
Definition: engine.hh:171
Gecode::IntSet d(v, 7)
void release(void)
Release the mutex.
Definition: none.hpp:52
Gecode::FloatVal c(-8, 8)
virtual NoGoods & nogoods(void)
Return no-goods.
Definition: dfs.cpp:208
Gecode::IntArgs i(4, 1, 2, 3, 4)
Support::Mutex m_wait_reset
Mutex for waiting for reset.
Definition: engine.hh:151
const unsigned int a_d
Create a clone during recomputation if distance is greater than a_d (adaptive distance) ...
Definition: search.hh:111
static void sleep(unsigned int ms)
Put current thread to sleep for ms milliseconds.
Definition: none.hpp:78
virtual void reset(Space *s)
Reset engine to restart at space s.
Definition: dfs.cpp:181
void reset(Space *s, unsigned int ngdl)
Reset worker to restart at space s.
Definition: dfs.hh:131
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
const unsigned int c_d
Create a clone after every c_d commits (commit distance)
Definition: search.hh:109
Worker * worker(unsigned int i) const
Provide access to worker i.
Definition: dfs.hh:98
Gecode toplevel namespace
const unsigned int nogoods_limit
Depth limit for no-good generation during search.
Definition: search.hh:127
Worker ** _worker
Array of worker references.
Definition: dfs.hh:63
Space is failed
Definition: core.hpp:1689
NoGoods & nogoods(void)
Return no-goods.
Definition: engine.hh:423
#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
Support::Event e_reset_ack_stop
Event for reset acknowledgment stopped.
Definition: engine.hh:149
virtual ~DFS(void)
Destructor.
Definition: dfs.cpp:230
Cmd cmd(void) const
Return current command.
Definition: engine.hh:223
Space is solved (no brancher left)
Definition: core.hpp:1690