Generated on Sat Feb 7 2015 02:01:29 for Gecode by doxygen 1.8.9.1
engine.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-08 15:03:36 +0200 (Mon, 08 Jul 2013) $ by $Author: schulte $
11  * $Revision: 13823 $
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  * Engine: search control
48  */
49  Space*
50  Engine::next(void) {
51  // Invariant: the worker holds the wait mutex
52  m_search.acquire();
53  if (!solutions.empty()) {
54  // No search needs to be done, take leftover solution
55  Space* s = solutions.pop();
56  m_search.release();
57  return s;
58  }
59  // We ignore stopped (it will be reported again if needed)
60  has_stopped = false;
61  // No more solutions?
62  if (n_busy == 0) {
63  m_search.release();
64  return NULL;
65  }
66  m_search.release();
67  // Okay, now search has to continue, make the guys work
68  release(C_WORK);
69 
70  /*
71  * Wait until a search related event has happened. It might be that
72  * the event has already been signalled in the last run, but the
73  * solution has been removed. So we have to try until there has
74  * something new happened.
75  */
76  while (true) {
77  e_search.wait();
78  m_search.acquire();
79  if (!solutions.empty()) {
80  // Report solution
81  Space* s = solutions.pop();
82  m_search.release();
83  // Make workers wait again
84  block();
85  return s;
86  }
87  // No more solutions or stopped?
88  if ((n_busy == 0) || has_stopped) {
89  m_search.release();
90  // Make workers wait again
91  block();
92  return NULL;
93  }
94  m_search.release();
95  }
97  return NULL;
98  }
99 
100  bool
101  Engine::stopped(void) const {
102  return has_stopped;
103  }
104 
105  /*
106  * Termination and deletion
107  */
109  delete cur;
110  path.reset(0);
111  }
112 
113 }}}
114 
115 #endif
116 
117 // STATISTICS: search-parallel
virtual ~Worker(void)
Destructor.
Definition: engine.cpp:108
Path path
Current path ins search tree.
Definition: engine.hh:59
virtual bool stopped(void) const
Check whether engine has been stopped.
Definition: engine.cpp:101
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
void block(void)
Block all workers.
Definition: engine.hh:222
Computation spaces.
Definition: core.hpp:1362
volatile unsigned int n_busy
Number of busy workers.
Definition: engine.hh:171
void release(void)
Release the mutex.
Definition: none.hpp:52
Space * cur
Current space being explored.
Definition: engine.hh:61
volatile bool has_stopped
Whether a worker had been stopped.
Definition: engine.hh:173
Support::Event e_search
Event for search (solution found, no more solutions, search stopped)
Definition: engine.hh:167
void release(Cmd c)
Release all workers.
Definition: engine.hh:227
Support::DynamicQueue< Space *, Heap > solutions
Queue of solutions.
Definition: engine.hh:169
Gecode toplevel namespace
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
void reset(int l)
Reset stack and set no-good depth limit to l.
Definition: path.hh:309
void wait(void)
Wait until the event becomes signalled.
Definition: none.hpp:65
Support::Mutex m_search
Mutex for search.
Definition: engine.hh:165