main page
modules
namespaces
classes
files
Gecode home
Generated on Sat Nov 9 2013 19:18:31 for Gecode by
doxygen
1.8.4
gecode
search
parallel
bab.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_BAB_HH__
39
#define __GECODE_SEARCH_PARALLEL_BAB_HH__
40
41
#include <
gecode/search/parallel/engine.hh
>
42
43
namespace
Gecode {
namespace
Search {
namespace
Parallel {
44
46
class
BAB
:
public
Engine
{
47
protected
:
49
class
Worker
:
public
Engine::Worker
{
50
protected
:
52
int
mark
;
54
Space
*
best
;
55
public
:
57
Worker
(
Space
* s,
BAB
& e);
59
BAB
&
engine
(
void
)
const
;
61
virtual
void
run
(
void
);
63
void
better
(
Space
*
b
);
65
void
find
(
void
);
67
void
reset
(
Space
* s,
int
ngdl);
69
virtual
~Worker
(
void
);
70
};
72
Worker
**
_worker
;
74
Space
*
best
;
75
public
:
77
Worker
*
worker
(
unsigned
int
i
)
const
;
78
80
81
void
solution
(
Space
* s);
84
86
87
BAB
(
Space
* s,
const
Options
& o);
90
virtual
Statistics
statistics
(
void
)
const
;
92
virtual
void
reset
(
Space
* s);
94
virtual
NoGoods
&
nogoods
(
void
);
96
virtual
~BAB
(
void
);
98
};
99
100
101
102
/*
103
* Engine: basic access routines
104
*/
105
forceinline
BAB
&
106
BAB::Worker::engine
(
void
)
const
{
107
return
static_cast<
BAB
&
>
(
_engine
);
108
}
109
forceinline
BAB::Worker
*
110
BAB::worker
(
unsigned
int
i
)
const
{
111
return
_worker
[
i
];
112
}
113
114
forceinline
void
115
BAB::Worker::reset
(
Space
* s,
int
ngdl) {
116
delete
cur;
117
delete
best
;
118
best
= NULL;
119
path
.reset((s == NULL) ? 0 : ngdl);
120
d
=
mark
= 0;
121
idle
=
false
;
122
if
((s == NULL) || (s->
status
(*
this
) ==
SS_FAILED
)) {
123
delete
s;
124
cur = NULL;
125
}
else
{
126
cur = s;
127
}
128
Search::Worker::reset
();
129
}
130
131
132
/*
133
* Engine: initialization
134
*/
135
forceinline
136
BAB::Worker::Worker
(
Space
* s,
BAB
& e)
137
:
Engine
::
Worker
(s,e),
mark
(0),
best
(NULL) {}
138
139
forceinline
140
BAB::BAB
(
Space
* s,
const
Options
& o)
141
:
Engine
(o),
best
(NULL) {
142
// Create workers
143
_worker
=
static_cast<
Worker
**
>
144
(
heap
.
ralloc
(
workers
() *
sizeof
(
Worker
*)));
145
// The first worker gets the entire search tree
146
_worker
[0] =
new
Worker
(s,*
this
);
147
// All other workers start with no work
148
for
(
unsigned
int
i
=1;
i
<
workers
();
i
++)
149
_worker
[
i
] =
new
Worker
(NULL,*
this
);
150
// Block all workers
151
block
();
152
// Create and start threads
153
for
(
unsigned
int
i
=0;
i
<
workers
();
i
++)
154
Support::Thread::run
(
_worker
[
i
]);
155
}
156
157
158
/*
159
* Engine: search control
160
*/
161
forceinline
void
162
BAB::Worker::better
(
Space
*
b
) {
163
m
.
acquire
();
164
delete
best
;
165
best
= b->
clone
(
false
);
166
mark
=
path
.
entries
();
167
if
(
cur
!= NULL)
168
cur
->
constrain
(*
best
);
169
m
.
release
();
170
}
171
forceinline
void
172
BAB::solution
(
Space
* s) {
173
m_search
.
acquire
();
174
if
(
best
!= NULL) {
175
s->
constrain
(*
best
);
176
if
(s->
status
() ==
SS_FAILED
) {
177
delete
s;
178
m_search
.
release
();
179
return
;
180
}
else
{
181
delete
best
;
182
best
= s->
clone
();
183
}
184
}
else
{
185
best
= s->
clone
();
186
}
187
// Announce better solutions
188
for
(
unsigned
int
i
=0;
i
<
workers
();
i
++)
189
worker
(
i
)->
better
(
best
);
190
bool
bs =
signal
();
191
solutions
.push(s);
192
if
(bs)
193
e_search
.
signal
();
194
m_search
.
release
();
195
}
196
197
198
/*
199
* Worker: finding and stealing working
200
*/
201
forceinline
void
202
BAB::Worker::find
(
void
) {
203
// Try to find new work (even if there is none)
204
for
(
unsigned
int
i
=0;
i
<engine().workers();
i
++) {
205
unsigned
long
int
r_d = 0ul;
206
if
(
Space
* s = engine().worker(
i
)->steal(r_d)) {
207
// Reset this guy
208
m.acquire();
209
idle
=
false
;
210
// Not idle but also does not have the root of the tree
211
path
.ngdl(0);
212
d
= 0;
213
cur = s;
214
mark
= 0;
215
if
(
best
!= NULL)
216
cur->constrain(*
best
);
217
Search::Worker::reset
(r_d);
218
m.release();
219
return
;
220
}
221
}
222
}
223
224
}}}
225
226
#endif
227
228
// STATISTICS: search-parallel