Generated on for Gecode by doxygen 1.17.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 std::atomic<bool>* tostop;
53 public:
57 void share(std::atomic<bool>* ts);
59 virtual bool stop(const Statistics& s, const Options& o);
61 void stop(bool s);
63 bool stop(void) const;
64 };
65
66 // Forward declaration
67 template<class Collect>
68 class PBS;
69
71 template<class Collect>
73 protected:
76 protected:
79 public:
81 Completion(void);
83 virtual void terminated(void);
85 void wait(void);
86 } completion;
93 public:
95 Slave(PBS<Collect>* m, Engine* s, Stop* so);
97 Statistics statistics(void) const;
99 bool stopped(void) const;
101 virtual Support::Terminator* terminator(void) const;
103 void wait(void);
105 void constrain(const Space& b);
107 virtual void run(void);
109 virtual ~Slave(void);
110 };
111
114 protected:
117 public:
119 static const bool best = false;
121 CollectAll(void);
123 bool add(Space* s, Slave<CollectAll>* r);
125 bool constrain(const Space& b);
127 bool empty(void) const;
131 ~CollectAll(void);
132 };
133
136 protected:
141 public:
143 static const bool best = true;
145 CollectBest(void);
147 bool add(Space* s, Slave<CollectBest>* r);
149 bool constrain(const Space& b);
151 bool empty(void) const;
156 };
157
159 template<class Collect>
161 friend class Slave<Collect>;
162 protected:
168 unsigned int n_slaves;
170 unsigned int n_active;
172 std::atomic<bool> slave_stop;
174 std::atomic<bool> tostop;
176 Collect solutions;
180 unsigned int n_busy;
184 bool report(Slave<Collect>* slave, Space* s);
193 public:
195 PBS(Engine** s, Stop** so, unsigned int n, const Statistics& stat);
197 virtual Space* next(void);
199 virtual Statistics statistics(void) const;
201 virtual bool stopped(void) const;
203 virtual void constrain(const Space& b);
205 virtual ~PBS(void);
206 };
207
208}}}
209
211
212#endif
213
214// STATISTICS: search-par
Search engine options
Definition search.hh:751
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:116
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
static const bool best
Whether it collects best solutions.
Definition pbs.hh:119
bool constrain(const Space &b)
Dummy function.
Definition pbs.hpp:73
CollectBest(void)
Initialize.
Definition pbs.hpp:93
static const bool best
Whether it collects best solutions.
Definition pbs.hh:143
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:140
~CollectBest(void)
Destructor.
Definition pbs.hpp:136
Space * b
Currently best solution.
Definition pbs.hh:138
Parallel depth-first search engine
Definition engine.hh:51
Engine(const Options &o)
Initialize with options o.
Definition engine.hpp:120
Parallel portfolio engine implementation.
Definition pbs.hh:160
virtual bool stopped(void) const
Check whether engine has been stopped.
Definition pbs.hpp:295
Support::Event idle
Signal that number of busy slaves becomes zero.
Definition pbs.hh:182
bool report(Slave< Collect > *slave, Space *s)
Process report from slave, return false if solution was ignored.
Definition pbs.hpp:205
Collect solutions
Collect solutions in this.
Definition pbs.hh:176
Support::Mutex m
Mutex for synchronization.
Definition pbs.hh:178
Slave< Collect > ** slaves
Slave engines.
Definition pbs.hh:166
std::atomic< bool > tostop
Shared stop flag.
Definition pbs.hh:174
unsigned int n_active
Number of active slave engines.
Definition pbs.hh:170
unsigned int n_slaves
Number of slave engines.
Definition pbs.hh:168
Statistics stat
Master statistics.
Definition pbs.hh:164
PBS(Engine **s, Stop **so, unsigned int n, const Statistics &stat)
Initialize.
Definition pbs.hpp:190
virtual void constrain(const Space &b)
Constrain future solutions to be better than b.
Definition pbs.hpp:311
std::atomic< bool > slave_stop
Whether a slave has been stopped.
Definition pbs.hh:172
virtual Space * next(void)
Return next solution (nullptr, if none exists or search has been stopped).
Definition pbs.hpp:245
virtual Statistics statistics(void) const
Return statistics.
Definition pbs.hpp:301
unsigned int n_busy
Number of busy slaves.
Definition pbs.hh:180
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)
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:78
Runnable slave of a portfolio master.
Definition pbs.hh:72
Engine * slave
The slave engine.
Definition pbs.hh:90
PBS< Collect > * master
The master engine.
Definition pbs.hh:88
void wait(void)
Wait for the slave to complete its current run.
Definition pbs.hpp:172
virtual Support::Terminator * terminator(void) const
Return the completion terminator.
Definition pbs.hpp:167
virtual void run(void)
Perform one run.
Definition pbs.hpp:236
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 constrain(const Space &b)
Constrain with better solution b.
Definition pbs.hpp:177
Stop * stop
Stop object.
Definition pbs.hh:92
Search engine statistics
Definition search.hh:151
Base-class for Stop-object.
Definition search.hh:804
Stop(void)
Default constructor.
Definition stop.hpp:45
Computation spaces.
Definition core.hpp:1775
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:71