Generated on for Gecode by doxygen 1.18.0
pbs.hpp
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.dev>
5 *
6 * Contributing authors:
7 * Mikael Zayenz Lagerkvist <lagerkvist@gecode.dev>
8 *
9 * Copyright:
10 * Christian Schulte, 2015
11 * Mikael Zayenz Lagerkvist, 2026
12 *
13 * This file is part of Gecode, the generic constraint
14 * development environment:
15 * http://www.gecode.dev
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 <algorithm>
39
40namespace Gecode { namespace Search { namespace Par {
41
42 template<class Collect>
43 forceinline
45 // A slave has not been submitted yet. This initial signal lets the
46 // first submission use the same consume-before-reuse handshake as all
47 // subsequent submissions.
48 done.signal();
49 }
50
51 template<class Collect>
52 forceinline void
56
57 template<class Collect>
58 forceinline void
60 done.wait();
61 }
62
63
64 forceinline
67 forceinline bool
69 solutions.push(s);
70 return true;
71 }
72 forceinline bool
74 (void) b;
75 return false;
76 }
77 forceinline bool
78 CollectAll::empty(void) const {
79 return solutions.empty();
80 }
81 forceinline Space*
83 return solutions.pop();
84 }
85 forceinline
87 while (!solutions.empty())
88 delete solutions.pop();
89 }
90
91
92 forceinline
94 : b(nullptr), reporter(nullptr) {}
95 forceinline bool
97 if (b != nullptr) {
98 b->constrain(*s);
99 if (b->status() == SS_FAILED) {
100 delete b;
101 } else {
102 delete s;
103 return false;
104 }
106 b = s;
108 return true;
110 forceinline bool
112 if (b != nullptr) {
114 if (b->status() == SS_FAILED) {
115 delete b;
116 } else {
117 return false;
118 }
119 }
120 b = s.clone();
121 reporter = nullptr;
122 return true;
123 }
124 forceinline bool
125 CollectBest::empty(void) const {
126 return reporter == nullptr;
127 }
128 forceinline Space*
130 assert(!empty());
131 r = reporter;
132 reporter = nullptr;
133 return b->clone();
134 }
135 forceinline
137 delete b;
138 }
139
140
141 forceinline
143 : so(so0), worker_control(control), tostop(nullptr) {}
144
145 forceinline void
146 PortfolioStop::share(std::atomic<bool>* ts) {
147 tostop = ts;
148 }
149
150
151 template<class Collect>
152 forceinline
154 : Support::Runnable(false), master(m), slave(s), stop(so) {}
155 template<class Collect>
156 forceinline Statistics
158 return slave->statistics();
159 }
160 template<class Collect>
161 forceinline bool
163 return slave->stopped();
164 }
165 template<class Collect>
166 forceinline bool
168#ifdef GECODE_HAS_EXPERIMENTAL
169 return slave->limited();
170#else
171 return false;
172#endif
173 }
174 template<class Collect>
175 forceinline Support::Terminator*
177 return const_cast<Completion*>(&completion);
178 }
179 template<class Collect>
180 forceinline void
184 template<class Collect>
185 forceinline void
187 static_cast<PortfolioStop*>(stop)->wake();
188 }
189 template<class Collect>
190 forceinline void
192 slave->constrain(b);
193 }
194 template<class Collect>
196 delete slave;
197 delete stop;
198 }
199
200
201
202 template<class Collect>
203 forceinline
204 PBS<Collect>::PBS(Engine** engines, Stop** stops, unsigned int n,
205 const Statistics& stat0)
206 : stat(stat0), slaves(heap.alloc<Slave<Collect>*>(n)),
208 slave_stop(false), incomplete(false), tostop(false), n_busy(0) {
209 // Initialize slaves
210 for (unsigned int i=0U; i<n_slaves; i++) {
211 slaves[i] = new Slave<Collect>(this,engines[i],stops[i]);
212 static_cast<PortfolioStop*>(stops[i])->share(&tostop);
214 }
216
217 template<class Collect>
218 forceinline bool
220 // If b is false the report should be repeated (solution was worse)
221 bool b = true;
222 bool wake = false;
223 m.acquire();
224 if (s != nullptr) {
225 b = solutions.add(s,slave);
226 if (b) {
227 tostop.store(true, std::memory_order_release);
228 wake = true;
229 }
230 } else if (slave->stopped() && !slave->limited()) {
231 if (!tostop.load(std::memory_order_acquire)) {
232 slave_stop.store(true, std::memory_order_release);
233 tostop.store(true, std::memory_order_release);
234 wake = true;
235 }
236 } else {
237 // The slave is done or stopped at a local limit (then search is incomplete)
238 if (slave->limited())
239 incomplete = true;
240 // Move slave to inactive
241 unsigned int i=0;
242 while (slaves[i] != slave)
243 i++;
244 assert(i < n_active);
245 assert(n_active > 0);
246 std::swap(slaves[i],slaves[--n_active]);
247 tostop.store(true, std::memory_order_release);
248 wake = true;
249 }
250 if (wake)
251 for (unsigned int i=0U; i<n_active; i++)
252 slaves[i]->wake();
253 if (b) {
254 if (--n_busy == 0)
255 idle.signal();
256 }
257 m.release();
258 return b;
259 }
260
261 template<class Collect>
262 void
264 Space* s;
265 do {
266 s = slave->next();
267 } while (!master->report(this,s));
268 }
269
270 template<class Collect>
271 Space*
273 m.acquire();
274 if (solutions.empty())
275 slave_stop.store(false, std::memory_order_release);
276 while (solutions.empty() && (n_active > 0) &&
277 !slave_stop.load(std::memory_order_acquire)) {
278 // Clear the internal stop used to interrupt sibling slaves
279 tostop.store(false, std::memory_order_release);
280
281 // Invariant: all slaves are idle!
282 assert(n_busy == 0);
283 assert(!tostop.load(std::memory_order_acquire));
284
285 // Run all active slaves
287 for (unsigned int i=0U; i<n_active; i++) {
288 // Consume the previous completion before reusing this slave. The
289 // initial signal handles the first submission.
290 slaves[i]->wait();
292 }
293 m.release();
294 // Wait for all slaves to become idle
295 idle.wait();
296 m.acquire();
297 }
298
299 // Invariant all slaves are idle!
300 assert(n_busy == 0);
301
302 if (solutions.empty() && (n_active == 0) && incomplete)
303 slave_stop.store(true, std::memory_order_release);
304
305 Space* s;
306
307 // Process solutions
308 if (solutions.empty()) {
309 s = nullptr;
310 } else {
312 s = solutions.get(r);
313 if (Collect::best)
314 for (unsigned int i=0U; i<n_active; i++)
315 if (slaves[i] != r)
316 slaves[i]->constrain(*s);
317 }
318
319 m.release();
320 return s;
321 }
322
323 template<class Collect>
324 bool
326 return slave_stop.load(std::memory_order_acquire);
327 }
328
329 template<class Collect>
332 assert(n_busy == 0);
333 Statistics s(stat);
334 for (unsigned int i=0U; i<n_slaves; i++)
335 s += slaves[i]->statistics();
336 return s;
337 }
338
339 template<class Collect>
340 void
342 assert(n_busy == 0);
343 if (!Collect::best)
344 throw NoBest("PBS::constrain");
345 if (solutions.constrain(b)) {
346 // The solution is better
347 for (unsigned int i=0U; i<n_active; i++)
348 slaves[i]->constrain(b);
349 }
350 }
351
352 template<class Collect>
354 // A report can make n_busy zero before Slave::run and Thread::Run::exec
355 // have returned. Wait for the completion handshake before deleting the
356 // PBS-owned slaves.
357 for (unsigned int i=0U; i<n_slaves; i++)
358 slaves[i]->wait();
359 assert(n_busy == 0);
360 for (unsigned int i=0U; i<n_slaves; i++)
361 delete slaves[i];
363 }
364
365}}}
366
367// STATISTICS: search-par
void free(T *b, long unsigned int n)
Delete n objects starting at b.
Definition heap.hpp:467
NoBest(const char *l)
Initialize with location l.
Space * get(Slave< CollectAll > *&r)
Return solution reported by r.
Definition pbs.hpp:82
bool empty(void) const
Check whether there is any solution left.
Definition pbs.hpp:78
Support::DynamicQueue< Space *, Heap > solutions
Queue of solutions.
Definition pbs.hh:124
bool add(Space *s, Slave< CollectAll > *r)
Add a solution a reported by r and always return true.
Definition pbs.hpp:68
CollectAll(void)
Initialize.
Definition pbs.hpp:65
~CollectAll(void)
Destructor.
Definition pbs.hpp:86
bool constrain(const Space &b)
Dummy function.
Definition pbs.hpp:73
CollectBest(void)
Initialize.
Definition pbs.hpp:93
bool constrain(const Space &b)
Check whether b better and update accordingly.
Definition pbs.hpp:111
bool empty(void) const
Check whether there is any solution left.
Definition pbs.hpp:125
bool add(Space *s, Slave< CollectBest > *r)
Add a solution s by r and return whether is was better.
Definition pbs.hpp:96
Space * get(Slave< CollectBest > *&r)
Return solution reported by r (only if a better one was found).
Definition pbs.hpp:129
Slave< CollectBest > * reporter
Who has reported the best solution (nullptr if solution has already been reported).
Definition pbs.hh:148
~CollectBest(void)
Destructor.
Definition pbs.hpp:136
Space * b
Currently best solution.
Definition pbs.hh:146
Parallel depth-first search engine
Definition engine.hh:52
void wait(void)
Ensure that worker waits.
Definition engine.hpp:92
Engine(const Options &o)
Initialize with options o.
Definition engine.hpp:123
Parallel portfolio engine implementation.
Definition pbs.hh:168
virtual bool stopped(void) const
Check whether engine has been stopped.
Definition pbs.hpp:325
virtual ~PBS(void)
Destructor.
Definition pbs.hpp:353
Support::Event idle
Signal that number of busy slaves becomes zero.
Definition pbs.hh:192
bool report(Slave< Collect > *slave, Space *s)
Process report from slave, return false if solution was ignored.
Definition pbs.hpp:219
Collect solutions
Collect solutions in this.
Definition pbs.hh:186
Support::Mutex m
Mutex for synchronization.
Definition pbs.hh:188
Slave< Gecode::Search::Par::CollectBest > ** slaves
Definition pbs.hh:174
PBS(Engine **s, Stop **so, unsigned int n, const Statistics &stat)
Initialize.
Definition pbs.hpp:204
virtual void constrain(const Space &b)
Constrain future solutions to be better than b.
Definition pbs.hpp:341
virtual Space * next(void)
Return next solution (nullptr, if none exists or search has been stopped).
Definition pbs.hpp:272
virtual Statistics statistics(void) const
Return statistics.
Definition pbs.hpp:331
Stop object used for controlling slaves in a portfolio.
Definition pbs.hh:47
void share(std::atomic< bool > *ts)
Set pointer to shared tostop variable.
Definition pbs.hpp:146
PortfolioStop(Stop *so, const WorkerControl &control)
Initialize.
Definition pbs.hpp:142
Completion event for the current run.
Definition pbs.hh:79
virtual void terminated(void)
Signal completion.
Definition pbs.hpp:53
void wait(void)
Wait for completion and consume the signal.
Definition pbs.hpp:59
Completion(void)
Initialize as completed.
Definition pbs.hpp:44
Support::Event done
The completion event.
Definition pbs.hh:82
Runnable slave of a portfolio master.
Definition pbs.hh:76
Engine * slave
The slave engine.
Definition pbs.hh:94
PBS< Collect > * master
The master engine.
Definition pbs.hh:92
void wait(void)
Wait for the slave to complete its current run.
Definition pbs.hpp:181
virtual Support::Terminator * terminator(void) const
Return the completion terminator.
Definition pbs.hpp:176
virtual void run(void)
Perform one run.
Definition pbs.hpp:263
bool stopped(void) const
Check whether slave has been stopped.
Definition pbs.hpp:162
virtual ~Slave(void)
Delete slave.
Definition pbs.hpp:195
Statistics statistics(void) const
Return statistics of slave.
Definition pbs.hpp:157
Slave(PBS< Collect > *m, Engine *s, Stop *so)
Initialize with master m, slave s, and its stop object so.
Definition pbs.hpp:153
Gecode::Search::Par::Slave::Completion completion
void wake(void)
Wake a paused slave so that it can observe the portfolio stop.
Definition pbs.hpp:186
void constrain(const Space &b)
Constrain with better solution b.
Definition pbs.hpp:191
Stop * stop
Stop object.
Definition pbs.hh:96
bool limited(void) const
Check whether slave stopped at a local limit.
Definition pbs.hpp:167
Search engine statistics
Definition search.hh:155
Base-class for Stop-object.
Definition search.hh:919
Stop(void)
Default constructor.
Definition stop.hpp:45
External control for the requested number of search workers.
Definition search.hh:773
Computation spaces.
Definition core.hpp:1782
void signal(void)
Signal the event.
Definition thread.hpp:133
Runnable(bool d=true)
Initialize, d defines whether object is deleted when terminated.
Definition thread.hpp:40
An interface for objects that can be called after a thread has terminated (after running the thread's...
Definition thread.hpp:168
static void run(Runnable *r)
Construct a new thread and run r.
Definition thread.hpp:177
Heap heap
The single global heap.
virtual void constrain(const Space &best)
Constrain function for best solution search.
Space * clone(void) const
Clone space.
Definition core.hpp:3374
SpaceStatus status(StatusStatistics &stat)
Query space status.
@ SS_FAILED
Space is failed
Definition core.hpp:1722
Search engines
Support algorithms and datastructures
Gecode toplevel namespace