Generated on for Gecode by doxygen 1.18.0
pbs.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.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#ifndef GECODE_SEARCH_PAR_PBS_HH
39#define GECODE_SEARCH_PAR_PBS_HH
40
41#include <gecode/search.hh>
42#include <atomic>
43
44namespace Gecode { namespace Search { namespace Par {
45
48 private:
50 Stop* so;
52 WorkerControl worker_control;
54 std::atomic<bool>* tostop;
55 public:
57 PortfolioStop(Stop* so, const WorkerControl& control);
59 void share(std::atomic<bool>* ts);
61 virtual bool stop(const Statistics& s, const Options& o);
63 void wake(void);
65 void stop(bool s);
67 bool stop(void) const;
68 };
69
70 // Forward declaration
71 template<class Collect>
72 class PBS;
73
75 template<class Collect>
77 protected:
80 protected:
83 public:
85 Completion(void);
87 virtual void terminated(void);
89 void wait(void);
90 } completion;
97 public:
99 Slave(PBS<Collect>* m, Engine* s, Stop* so);
101 Statistics statistics(void) const;
103 bool stopped(void) const;
105 bool limited(void) const;
107 virtual Support::Terminator* terminator(void) const;
109 void wait(void);
111 void wake(void);
113 void constrain(const Space& b);
115 virtual void run(void);
117 virtual ~Slave(void);
118 };
119
122 protected:
125 public:
127 static const bool best = false;
129 CollectAll(void);
131 bool add(Space* s, Slave<CollectAll>* r);
133 bool constrain(const Space& b);
135 bool empty(void) const;
139 ~CollectAll(void);
140 };
141
144 protected:
149 public:
151 static const bool best = true;
153 CollectBest(void);
155 bool add(Space* s, Slave<CollectBest>* r);
157 bool constrain(const Space& b);
159 bool empty(void) const;
164 };
165
167 template<class Collect>
169 friend class Slave<Collect>;
170 protected:
176 unsigned int n_slaves;
178 unsigned int n_active;
180 std::atomic<bool> slave_stop;
184 std::atomic<bool> tostop;
186 Collect solutions;
190 unsigned int n_busy;
194 bool report(Slave<Collect>* slave, Space* s);
203 public:
205 PBS(Engine** s, Stop** so, unsigned int n, const Statistics& stat);
207 virtual Space* next(void);
209 virtual Statistics statistics(void) const;
211 virtual bool stopped(void) const;
213 virtual void constrain(const Space& b);
215 virtual ~PBS(void);
216 };
217
218}}}
219
221
222#endif
223
224// STATISTICS: search-par
Search engine options
Definition search.hh:848
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
static const bool best
Whether it collects best solutions.
Definition pbs.hh:127
bool constrain(const Space &b)
Dummy function.
Definition pbs.hpp:73
Collect best solutions.
Definition pbs.hh:143
CollectBest(void)
Initialize.
Definition pbs.hpp:93
static const bool best
Whether it collects best solutions.
Definition pbs.hh:151
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
Space * b
Currently best solution.
Definition pbs.hh:146
Parallel depth-first search engine
Definition engine.hh:52
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
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
bool incomplete
Whether a slave stopped at a local limit.
Definition pbs.hh:182
Collect solutions
Collect solutions in this.
Definition pbs.hh:186
Support::Mutex m
Mutex for synchronization.
Definition pbs.hh:188
Slave< Collect > ** slaves
Slave engines.
Definition pbs.hh:174
std::atomic< bool > tostop
Shared stop flag.
Definition pbs.hh:184
unsigned int n_active
Number of active slave engines.
Definition pbs.hh:178
unsigned int n_slaves
Number of slave engines.
Definition pbs.hh:176
Statistics stat
Master statistics.
Definition pbs.hh:172
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
std::atomic< bool > slave_stop
Whether a slave has been stopped.
Definition pbs.hh:180
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
unsigned int n_busy
Number of busy slaves.
Definition pbs.hh:190
void wake(void)
Wake workers so that a paused asset can observe the portfolio stop.
void share(std::atomic< bool > *ts)
Set pointer to shared tostop variable.
Definition pbs.hpp:146
void stop(bool s)
Signal whether search must be stopped.
virtual bool stop(const Statistics &s, const Options &o)
Return true if portfolio engine must be stopped.
PortfolioStop(Stop *so, const WorkerControl &control)
Initialize.
Definition pbs.hpp:142
bool stop(void) const
Whether search must be stopped.
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
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
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
Queue with arbitrary number of elements.
An event for synchronization.
Definition thread.hpp:138
A mutex for mutual exclausion among several threads.
Definition thread.hpp:78
An interface for objects that can be run by a thread.
Definition thread.hpp:181
An interface for objects that can be called after a thread has terminated (after running the thread's...
Definition thread.hpp:168
Search engines
Gecode toplevel namespace
#define GECODE_SEARCH_EXPORT
Definition search.hh:75