Generated on Wed Jan 24 2018 21:22:26 for Gecode by doxygen 1.8.13
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: 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 #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  };
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  forceinline bool
213  Engine::stopped(void) const {
214  return has_stopped;
215  }
216 
217 
218 
219  /*
220  * Engine: command and wait handling
221  */
223  Engine::cmd(void) const {
224  return _cmd;
225  }
226  forceinline void
228  _cmd = C_WAIT;
229  _m_wait.acquire();
230  }
231  forceinline void
233  _cmd = c;
234  _m_wait.release();
235  }
236  forceinline void
237  Engine::wait(void) {
239  }
240 
241 
242  /*
243  * Engine: initialization
244  */
247  : _engine(e),
248  path(s == NULL ? 0 : e.opt().nogoods_limit), d(0),
249  idle(false) {
250  if (s != NULL) {
251  if (s->status(*this) == SS_FAILED) {
252  fail++;
253  cur = NULL;
254  if (!engine().opt().clone)
255  delete s;
256  } else {
257  cur = snapshot(s,engine().opt(),false);
258  }
259  } else {
260  cur = NULL;
261  }
262  }
263 
266  : _opt(o), solutions(heap) {
267  // Initialize termination information
270  // Initialize search information
271  n_busy = workers();
272  has_stopped = false;
273  // Initialize reset information
275  }
276 
277 
278  /*
279  * Statistics
280  */
283  m.acquire();
284  Statistics s = *this;
285  m.release();
286  return s;
287  }
288 
289 
290  /*
291  * Engine: search control
292  */
293  forceinline bool
294  Engine::signal(void) const {
295  return solutions.empty() && (n_busy > 0) && !has_stopped;
296  }
297  forceinline void
298  Engine::idle(void) {
299  m_search.acquire();
300  bool bs = signal();
301  n_busy--;
302  if (bs && (n_busy == 0))
303  e_search.signal();
304  m_search.release();
305  }
306  forceinline void
307  Engine::busy(void) {
308  m_search.acquire();
309  assert(n_busy > 0);
310  n_busy++;
311  m_search.release();
312  }
313  forceinline void
314  Engine::stop(void) {
315  m_search.acquire();
316  bool bs = signal();
317  has_stopped = true;
318  if (bs)
319  e_search.signal();
320  m_search.release();
321  }
322 
323 
324  /*
325  * Engine: termination control
326  */
327  forceinline void
329  unsigned int n;
330  _m_term.acquire();
331  n = --_n_not_terminated;
332  _m_term.release();
333  // The signal must be outside of the look, otherwise a thread might be
334  // terminated that still holds a mutex.
335  if (n == 0)
337  }
338 
339  forceinline void
341  _m_term.acquire();
342  if (--_n_term_not_ack == 0)
344  _m_term.release();
345  }
346 
347  forceinline void
351  }
352 
353  forceinline void
355  // Grab the wait mutex for termination
357  // Release all threads
359  // Wait until all threads have acknowledged termination request
360  _e_term_ack.wait();
361  // Release waiting threads
363  // Wait until all threads have in fact terminated
364  _e_terminate.wait();
365  // Now all threads are terminated!
366  }
367 
368 
369 
370  /*
371  * Engine: reset control
372  */
373  forceinline void
375  _m_reset.acquire();
376  if (--_n_reset_not_ack == 0)
378  _m_reset.release();
379  }
380 
381  forceinline void
383  _m_reset.acquire();
384  if (++_n_reset_not_ack == workers())
386  _m_reset.release();
387  }
388 
389  forceinline void
393  }
394 
395 
396 
397  /*
398  * Worker: finding and stealing working
399  */
401  Engine::Worker::steal(unsigned long int& d) {
402  /*
403  * Make a quick check whether the worker might have work
404  *
405  * If that is not true any longer, the worker will be asked
406  * again eventually.
407  */
408  if (!path.steal())
409  return NULL;
410  m.acquire();
411  Space* s = path.steal(*this,d);
412  m.release();
413  // Tell that there will be one more busy worker
414  if (s != NULL)
415  engine().busy();
416  return s;
417  }
418 
419  /*
420  * Return No-Goods
421  */
424  return path;
425  }
426 
427 }}}
428 
429 #endif
430 
431 // STATISTICS: search-parallel
Cmd
Commands from engine to workers.
Definition: engine.hh:91
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
Worker(void)
Initialize.
Definition: worker.hh:74
volatile Cmd _cmd
The current command.
Definition: engine.hh:99
virtual ~Worker(void)
Destructor.
Definition: engine.cpp:103
Search engine implementation interface
Definition: search.hh:601
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:340
void wait_terminate(void)
For worker to wait until termination is legal.
Definition: engine.hh:348
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:390
void terminate(void)
For engine to peform thread termination.
Definition: engine.hh:354
Path path
Current path ins search tree.
Definition: engine.hh:59
Search engine statistics
Definition: search.hh:140
void idle(void)
Report that worker is idle.
Definition: engine.hh:298
Search engine options
Definition: search.hh:446
An interface for objects that can be run by a thread.
Definition: thread.hpp:258
Statistics statistics(void)
Return statistics.
Definition: engine.hh:282
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 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
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
volatile unsigned int _n_not_terminated
Number of not yet terminated workers.
Definition: engine.hh:125
bool stopped(void) const
Check whether engine has been stopped.
Definition: worker.hh:91
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
virtual bool stopped(void) const
Check whether engine has been stopped.
Definition: engine.hh:213
void signal(void)
Signal the event.
Definition: none.hpp:63
Computation spaces.
Definition: core.hpp:1748
Support::Mutex _m_term
Mutex for access to termination information.
Definition: engine.hh:117
const Options & opt(void) const
Provide access to search options.
Definition: engine.hh:205
void busy(void)
Report that worker is busy.
Definition: engine.hh:307
volatile unsigned int n_busy
Number of busy workers.
Definition: engine.hh:171
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:451
void wait(void)
Ensure that worker waits.
Definition: engine.hh:237
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
An event for synchronization.
Definition: thread.hpp:223
Options _opt
Search options.
Definition: engine.hh:81
Support::Mutex m_wait_reset
Mutex for waiting for reset.
Definition: engine.hh:151
Engine & engine(void) const
Provide access to engine.
Definition: engine.hh:201
unsigned int d
Distance until next clone.
Definition: engine.hh:63
bool idle
Whether the worker is idle.
Definition: engine.hh:65
void terminated(void)
For worker to register termination.
Definition: engine.hh:328
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:449
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:401
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
bool signal(void) const
Whether search state changed such that signal is needed.
Definition: engine.hh:294
Parallel depth-first search engine
Definition: engine.hh:49
Queue with arbitrary number of elements.
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:382
Engine(const Options &o)
Initialize with options o.
Definition: engine.hh:265
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
#define forceinline
Definition: config.hpp:173
Parallel depth-first search worker
Definition: engine.hh:52
SpaceStatus status(StatusStatistics &stat=unused_status)
Query space status.
Definition: core.cpp:224
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:75
volatile unsigned int _n_reset_not_ack
Number of workers that have not yet acknowledged reset.
Definition: engine.hh:145
Gecode toplevel namespace
const unsigned int nogoods_limit
Depth limit for no-good generation during search.
Definition: search.hh:127
Space is failed
Definition: core.hpp:1689
void ack_reset_start(void)
For worker to acknowledge start of reset cycle.
Definition: engine.hh:374
NoGoods & nogoods(void)
Return no-goods.
Definition: engine.hh:423
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
Support::Mutex m_search
Mutex for search.
Definition: engine.hh:165