Generated on Sat Feb 7 2015 02:01:29 for Gecode by doxygen 1.8.9.1
engine.hh
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-11 12:30:18 +0200 (Thu, 11 Jul 2013) $ by $Author: schulte $
11  * $Revision: 13840 $
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 #ifndef __GECODE_SEARCH_PARALLEL_ENGINE_HH__
39 #define __GECODE_SEARCH_PARALLEL_ENGINE_HH__
40 
41 #include <gecode/search.hh>
42 #include <gecode/search/support.hh>
43 #include <gecode/search/worker.hh>
45 
46 namespace Gecode { namespace Search { namespace Parallel {
47 
49  class Engine : public Search::Engine {
50  protected:
52  class Worker : public Search::Worker, public Support::Runnable {
53  protected:
63  unsigned int d;
65  bool idle;
66  public:
68  Worker(Space* s, Engine& e);
70  Space* steal(unsigned long int& d);
72  Statistics statistics(void);
74  Engine& engine(void) const;
76  NoGoods& nogoods(void);
78  virtual ~Worker(void);
79  };
81  const Options _opt;
82  public:
84  const Options& opt(void) const;
86  unsigned int workers(void) const;
87 
89 
90  enum Cmd {
96  };
97  protected:
99  volatile Cmd _cmd;
102  public:
104  Cmd cmd(void) const;
106  void block(void);
108  void release(Cmd c);
110  void wait(void);
112 
114 
115  protected:
119  volatile unsigned int _n_term_not_ack;
125  volatile unsigned int _n_not_terminated;
128  public:
130  void ack_terminate(void);
132  void terminated(void);
134  void wait_terminate(void);
136  void terminate(void);
138 
140 
141  protected:
145  volatile unsigned int _n_reset_not_ack;
152  public:
154  void ack_reset_start(void);
156  void ack_reset_stop(void);
158  void wait_reset(void);
160 
162 
163  protected:
171  volatile unsigned int n_busy;
173  volatile bool has_stopped;
175  bool signal(void) const;
176  public:
178  void idle(void);
180  void busy(void);
182  void stop(void);
184 
186 
187  Engine(const Options& o);
190  virtual Space* next(void);
192  virtual bool stopped(void) const;
194  };
195 
196 
197  /*
198  * Basic access routines
199  */
202  return _engine;
203  }
204  forceinline const Options&
205  Engine::opt(void) const {
206  return _opt;
207  }
208  forceinline unsigned int
209  Engine::workers(void) const {
210  return static_cast<unsigned int>(opt().threads);
211  }
212 
213 
214  /*
215  * Engine: command and wait handling
216  */
218  Engine::cmd(void) const {
219  return _cmd;
220  }
221  forceinline void
223  _cmd = C_WAIT;
224  _m_wait.acquire();
225  }
226  forceinline void
228  _cmd = c;
229  _m_wait.release();
230  }
231  forceinline void
232  Engine::wait(void) {
234  }
235 
236 
237  /*
238  * Engine: initialization
239  */
242  : _engine(e),
243  path(s == NULL ? 0 : static_cast<int>(e.opt().nogoods_limit)), d(0),
244  idle(false) {
245  if (s != NULL) {
246  if (s->status(*this) == SS_FAILED) {
247  fail++;
248  cur = NULL;
249  if (!engine().opt().clone)
250  delete s;
251  } else {
252  cur = snapshot(s,engine().opt(),false);
253  }
254  } else {
255  cur = NULL;
256  }
257  }
258 
261  : _opt(o), solutions(heap) {
262  // Initialize termination information
265  // Initialize search information
266  n_busy = workers();
267  has_stopped = false;
268  // Initialize reset information
270  }
271 
272 
273  /*
274  * Statistics
275  */
278  m.acquire();
279  Statistics s = *this;
280  m.release();
281  return s;
282  }
283 
284 
285  /*
286  * Engine: search control
287  */
288  forceinline bool
289  Engine::signal(void) const {
290  return solutions.empty() && (n_busy > 0) && !has_stopped;
291  }
292  forceinline void
293  Engine::idle(void) {
294  m_search.acquire();
295  bool bs = signal();
296  n_busy--;
297  if (bs && (n_busy == 0))
298  e_search.signal();
299  m_search.release();
300  }
301  forceinline void
302  Engine::busy(void) {
303  m_search.acquire();
304  assert(n_busy > 0);
305  n_busy++;
306  m_search.release();
307  }
308  forceinline void
309  Engine::stop(void) {
310  m_search.acquire();
311  bool bs = signal();
312  has_stopped = true;
313  if (bs)
314  e_search.signal();
315  m_search.release();
316  }
317 
318 
319  /*
320  * Engine: termination control
321  */
322  forceinline void
324  unsigned int n;
325  _m_term.acquire();
326  n = --_n_not_terminated;
327  _m_term.release();
328  // The signal must be outside of the look, otherwise a thread might be
329  // terminated that still holds a mutex.
330  if (n == 0)
332  }
333 
334  forceinline void
336  _m_term.acquire();
337  if (--_n_term_not_ack == 0)
339  _m_term.release();
340  }
341 
342  forceinline void
346  }
347 
348  forceinline void
350  // Grab the wait mutex for termination
352  // Release all threads
354  // Wait until all threads have acknowledged termination request
355  _e_term_ack.wait();
356  // Release waiting threads
358  // Wait until all threads have in fact terminated
359  _e_terminate.wait();
360  // Now all threads are terminated!
361  }
362 
363 
364 
365  /*
366  * Engine: reset control
367  */
368  forceinline void
370  _m_reset.acquire();
371  if (--_n_reset_not_ack == 0)
373  _m_reset.release();
374  }
375 
376  forceinline void
378  _m_reset.acquire();
379  if (++_n_reset_not_ack == workers())
381  _m_reset.release();
382  }
383 
384  forceinline void
388  }
389 
390 
391 
392  /*
393  * Worker: finding and stealing working
394  */
396  Engine::Worker::steal(unsigned long int& d) {
397  /*
398  * Make a quick check whether the worker might have work
399  *
400  * If that is not true any longer, the worker will be asked
401  * again eventually.
402  */
403  if (!path.steal())
404  return NULL;
405  m.acquire();
406  Space* s = path.steal(*this,d);
407  m.release();
408  // Tell that there will be one more busy worker
409  if (s != NULL)
410  engine().busy();
411  return s;
412  }
413 
414  /*
415  * Return No-Goods
416  */
419  return path;
420  }
421 
422 }}}
423 
424 #endif
425 
426 // STATISTICS: search-parallel
Cmd
Commands from engine to workers.
Definition: engine.hh:91
Support::Event e_reset_ack_start
Event for reset acknowledgment started.
Definition: engine.hh:147
Worker(void)
Initialize.
Definition: worker.hh:74
volatile Cmd _cmd
The current command.
Definition: engine.hh:99
virtual ~Worker(void)
Destructor.
Definition: engine.cpp:108
Search engine implementation interface
Definition: search.hh:445
Support::Event _e_term_ack
Event for termination acknowledgment.
Definition: engine.hh:121
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
Depth-first path (stack of edges) supporting recomputation.
Definition: path.hh:61
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 idle(void)
Report that worker is idle.
Definition: engine.hh:293
virtual bool stopped(void) const
Check whether engine has been stopped.
Definition: engine.cpp:101
Search engine options
Definition: search.hh:204
An interface for objects that can be run by a thread.
Definition: thread.hpp:258
Statistics statistics(void)
Return statistics.
Definition: engine.hh:277
Support::Event _e_terminate
Event for termination (all threads have terminated)
Definition: engine.hh:127
virtual Space * next(void)
Return next solution (NULL, if none exists or search has been stopped)
Definition: engine.cpp:50
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
volatile unsigned int _n_not_terminated
Number of not yet terminated workers.
Definition: engine.hh:125
void path(Home home, int offset, const IntVarArgs &x, IntVar s, IntVar e, IntConLevel icl)
Post propagator such that x forms a Hamiltonian path.
Definition: circuit.cpp:128
void block(void)
Block all workers.
Definition: engine.hh:222
void signal(void)
Signal the event.
Definition: none.hpp:63
Computation spaces.
Definition: core.hpp:1362
Support::Mutex _m_term
Mutex for access to termination information.
Definition: engine.hh:117
void busy(void)
Report that worker is busy.
Definition: engine.hh:302
volatile unsigned int n_busy
Number of busy workers.
Definition: engine.hh:171
Heap heap
The single global heap.
Definition: heap.cpp:49
A mutex for mutual exclausion among several threads.
Definition: thread.hpp:99
Gecode::IntSet d(v, 7)
Support::Mutex _m_wait_terminate
Mutex for waiting for termination.
Definition: engine.hh:123
void release(void)
Release the mutex.
Definition: none.hpp:52
Gecode::FloatVal c(-8, 8)
double threads
Number of threads to use.
Definition: search.hh:209
void wait(void)
Ensure that worker waits.
Definition: engine.hh:232
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
An event for synchronization.
Definition: thread.hpp:223
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
Cmd cmd(void) const
Return current command.
Definition: engine.hh:218
void terminated(void)
For worker to register termination.
Definition: engine.hh:323
volatile unsigned int _n_term_not_ack
Number of workers that have not yet acknowledged termination.
Definition: engine.hh:119
Space * cur
Current space being explored.
Definition: engine.hh:61
bool clone
Whether engines create a clone when being initialized.
Definition: search.hh:207
unsigned int workers(void) const
Return number of workers.
Definition: engine.hh:209
const Options & opt(void) const
Provide access to search options.
Definition: engine.hh:205
Support::Mutex _m_wait
Mutex for forcing workers to wait.
Definition: engine.hh:101
volatile bool has_stopped
Whether a worker had been stopped.
Definition: engine.hh:173
Space * steal(unsigned long int &d)
Hand over some work (NULL if no work available)
Definition: engine.hh:396
Support::Event e_search
Event for search (solution found, no more solutions, search stopped)
Definition: engine.hh:167
Search worker statistics
Definition: worker.hh:48
Parallel depth-first search engine
Definition: engine.hh:49
Queue with arbitrary number of elements.
const Options _opt
Search options.
Definition: engine.hh:81
Support::Mutex _m_reset
Mutex for access to reset information.
Definition: engine.hh:143
void ack_reset_stop(void)
For worker to acknowledge stop of reset cycle.
Definition: engine.hh:377
Engine(const Options &o)
Initialize with options o.
Definition: engine.hh:260
No-goods recorded from restarts.
Definition: core.hpp:1240
void release(Cmd c)
Release all workers.
Definition: engine.hh:227
#define forceinline
Definition: config.hpp:132
Parallel depth-first search worker
Definition: engine.hh:52
SpaceStatus status(StatusStatistics &stat=unused_status)
Query space status.
Definition: core.cpp:252
Support::DynamicQueue< Space *, Heap > solutions
Queue of solutions.
Definition: engine.hh:169
Engine & _engine
Reference to engine.
Definition: engine.hh:55
Space * snapshot(Space *s, const Options &o, bool share=true)
Clone space s dependening on options o.
Definition: support.hh:73
volatile unsigned int _n_reset_not_ack
Number of workers that have not yet acknowledged reset.
Definition: engine.hh:145
Engine & engine(void) const
Provide access to engine.
Definition: engine.hh:201
Gecode toplevel namespace
const unsigned int nogoods_limit
Depth limit for no-good generation during search.
Definition: search.hh:107
Space is failed
Definition: core.hpp:1301
void ack_reset_start(void)
For worker to acknowledge start of reset cycle.
Definition: engine.hh:369
NoGoods & nogoods(void)
Return no-goods.
Definition: engine.hh:418
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
Support::Mutex m
Mutex for access to worker.
Definition: engine.hh:57
bool signal(void) const
Whether search state changed such that signal is needed.
Definition: engine.hh:289
Support::Mutex m_search
Mutex for search.
Definition: engine.hh:165