main page
modules
namespaces
classes
files
Gecode home
Generated on Tue Jan 28 2020 00:00:00 for Gecode by
doxygen
1.8.17
gecode
search
parallel
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: 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
42
#include <
gecode/search/parallel/engine.hh
>
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
}
96
GECODE_NEVER
;
97
return
NULL;
98
}
99
100
/*
101
* Termination and deletion
102
*/
103
Engine::Worker::~Worker
(
void
) {
104
delete
cur
;
105
path
.
reset
(0);
106
}
107
108
}}}
109
110
#endif
111
112
// STATISTICS: search-parallel
Gecode::Support::Mutex::acquire
void acquire(void)
Acquire the mutex and possibly block.
Definition:
none.hpp:46
Gecode::Search::Parallel::Engine::n_busy
volatile unsigned int n_busy
Number of busy workers.
Definition:
engine.hh:171
Gecode::Search::Parallel::Engine::next
virtual Space * next(void)
Return next solution (NULL, if none exists or search has been stopped)
Definition:
engine.cpp:50
Gecode::Search::Parallel::Engine::Worker::path
Path path
Current path ins search tree.
Definition:
engine.hh:59
Gecode::Space
Computation spaces.
Definition:
core.hpp:1748
Gecode::Search::Parallel::Engine::C_WORK
@ C_WORK
Perform work.
Definition:
engine.hh:92
Gecode::Search::Parallel::Engine::Worker::cur
Space * cur
Current space being explored.
Definition:
engine.hh:61
Gecode
Gecode toplevel namespace
engine.hh
Gecode::Search::Parallel::Engine::block
void block(void)
Block all workers.
Definition:
engine.hh:227
Gecode::Search::Parallel::Engine::has_stopped
volatile bool has_stopped
Whether a worker had been stopped.
Definition:
engine.hh:173
Gecode::Search::Parallel::Path::reset
void reset(unsigned int l)
Reset stack and set no-good depth limit to l.
Definition:
path.hh:309
Gecode::Search::Parallel::Engine::e_search
Support::Event e_search
Event for search (solution found, no more solutions, search stopped)
Definition:
engine.hh:167
GECODE_NEVER
#define GECODE_NEVER
Assert that this command is never executed.
Definition:
macros.hpp:60
Gecode::Search::Parallel::Engine::release
void release(Cmd c)
Release all workers.
Definition:
engine.hh:232
support.hh
Gecode::Search::Parallel::Engine::solutions
Support::DynamicQueue< Space *, Heap > solutions
Queue of solutions.
Definition:
engine.hh:169
Gecode::Search::Parallel::Engine::m_search
Support::Mutex m_search
Mutex for search.
Definition:
engine.hh:165
Gecode::Search::Parallel::Engine::Worker::~Worker
virtual ~Worker(void)
Destructor.
Definition:
engine.cpp:103
Gecode::Support::Mutex::release
void release(void)
Release the mutex.
Definition:
none.hpp:52
Gecode::Support::Event::wait
void wait(void)
Wait until the event becomes signalled.
Definition:
none.hpp:65