45 for (
unsigned int i=0U; i<n_slaves; i++) {
55 for (
int i=0; i<sebs.
size(); i++)
70#ifdef GECODE_HAS_THREADS
72 return std::min(
static_cast<int>(opt.
threads),sebs.
size());
82#ifdef GECODE_HAS_THREADS
83 const bool parallel = opt.
threads > 1.0;
85 const bool parallel =
false;
88 for (
int i=0; i<admitted; i++) {
95 for (
int j=0; j<i; j++) {
110 static constexpr bool value =
false;
117 template<
class T,
template<
class>
class E>
126 template<
class T,
template<
class>
class E>
159 last = (--s->
n == 0U);
178 unsigned long long int v;
238 template<
class T,
template<
class>
class E>
248 template<
class T,
template<
class>
class E>
253 template<
class T,
template<
class>
class E>
263namespace Gecode {
namespace Search {
namespace Seq {
275 pbsengine(Engine** slaves, Stop** stops,
unsigned int n_slaves,
276 const Statistics& stat,
const Search::Options& opt,
bool best);
284 pbsstop(Stop* so,
const WorkerControl& control);
288 pbsstop(Stop* so, Stop* po,
const WorkerControl& control);
293 const Statistics& stat,
bool best);
299 template<
class T,
template<
class>
class E>
309 static_cast<double>(opt.
assets)),1.0);
311 unsigned int n_slaves = opt.
assets;
313 Stop** stops = r.alloc<
Stop*>(n_slaves);
314 for (
unsigned int i=0U; i<n_slaves; i++) {
322 Space* unowned_master = master;
325 for (
unsigned int i=0U; i<n_slaves; i++) {
327 std::unique_ptr<Space> slave((i == n_slaves-1) ?
328 master : master->clone());
329 if (slave.get() == master)
330 unowned_master =
nullptr;
331 (void) slave->
slave(i);
332 std::unique_ptr<Cutoff> cutoff;
333 if (cutoffs !=
nullptr) {
335 opt.
cutoff = cutoff.get();
339 (void) cutoff.release();
342 slaves,stops,n_slaves,stat,opt,E<T>::best);
346 pbscleanup(slaves,stops,n_slaves,unowned_master);
352 template<
class T,
template<
class>
class E>
358 int n_slaves = sebs.
size();
360 Stop** stops = r.alloc<
Stop*>(n_slaves);
361 for (
int i=0; i<n_slaves; i++) {
368 static_cast<unsigned int>(n_slaves));
370 Space* unowned_master = master;
371 bool builders_owned =
true;
373 for (
int i=0; i<n_slaves; i++) {
376 sebs[i]->options().stop = stops[i];
377 sebs[i]->options().clone =
true;
378 std::unique_ptr<Space> slave((i == n_slaves-1) ?
379 master : master->clone());
380 if (slave.get() == master)
381 unowned_master =
nullptr;
382 (void) slave->
slave(
static_cast<unsigned int>(i));
383 slaves[i] = (*sebs[i])(slave.get());
386 builders_owned =
false;
388 slaves,stops,
static_cast<unsigned int>(n_slaves),stat,opt,best);
392 pbscleanup(slaves,stops,
static_cast<unsigned int>(n_slaves),
398#ifdef GECODE_HAS_THREADS
400 template<
class T,
template<
class>
class E>
409 unsigned int n_slaves = std::min(
static_cast<unsigned int>(opt.
threads),
418 Stop** stops = r.alloc<
Stop*>(n_slaves);
419 for (
unsigned int i=0U; i<n_slaves; i++) {
424 Space* unowned_master = master;
427 for (
unsigned int i=0U; i<n_slaves; i++) {
429 std::unique_ptr<Space> slave((i == n_slaves-1) ?
430 master : master->clone());
431 if (slave.get() == master)
432 unowned_master =
nullptr;
433 (void) slave->
slave(
static_cast<unsigned int>(i));
434 std::unique_ptr<Cutoff> cutoff;
435 if (cutoffs !=
nullptr) {
437 opt.
cutoff = cutoff.get();
441 (void) cutoff.release();
447 pbscleanup(slaves,stops,n_slaves,unowned_master);
453 template<
class T,
template<
class>
class E>
460 int n_slaves = std::min(
static_cast<int>(opt.
threads),
465 static_cast<unsigned int>(n_slaves));
468 Stop** stops = r.alloc<
Stop*>(n_slaves);
469 for (
int i=0; i<n_slaves; i++) {
474 Space* unowned_master = master;
475 bool builders_owned =
true;
477 for (
int i=0; i<n_slaves; i++) {
480 sebs[i]->options().worker_control);
481 sebs[i]->options().stop = stops[i];
482 sebs[i]->options().clone =
true;
483 std::unique_ptr<Space> slave((i == n_slaves-1) ?
484 master : master->clone());
485 if (slave.get() == master)
486 unowned_master =
nullptr;
487 (void) slave->
slave(
static_cast<unsigned int>(i));
488 slaves[i] = (*sebs[i])(slave.get());
491 builders_owned =
false;
493 slaves,stops,
static_cast<unsigned int>(n_slaves),stat,best);
497 pbscleanup(slaves,stops,
static_cast<unsigned int>(n_slaves),
509 template<
class T,
template<
class>
class E>
535 T* master = opt.clone ?
536 dynamic_cast<T*
>(s->clone()) : s;
540 (void) master->master(0);
544 std::unique_ptr<T> seed(master);
545 (void) master->slave(0);
552#ifdef GECODE_HAS_THREADS
560 template<
class T,
template<
class>
class E>
570 for (
int i=0; i<sebs.
size(); i++)
571 b += sebs[i]->
best() ? 1 : 0;
572 if ((b > 0) && (b < sebs.
size()))
589 T* master = opt.clone ?
590 dynamic_cast<T*
>(s->clone()) : s;
594 (void) master->master(0);
596#ifdef GECODE_HAS_THREADS
604 template<
class T,
template<
class>
class E>
610 template<
class T,
template<
class>
class E>
616 template<
class T,
template<
class>
class E>
623 template<
class T,
template<
class>
class E>
int size(void) const
Return size of array (number of elements).
Base class for heap allocated objects.
void assets(unsigned int n)
Set default number of assets in a portfolio.
void threads(double n)
Set number of parallel threads.
Meta engine using a portfolio of search engines.
void constrain(const T &b)
Constrain future portfolio solutions to be better than b.
void build(T *s, SEBs &sebs, const Search::Options &o)
The actual build function.
PBS(T *s, const Search::Options &o=Search::Options::def)
Initialize with engines running copies of s with options o.
static const bool best
Whether engine does best solution search.
Meta-engine performing restart-based search.
Passing search engine builder arguments.
@ PBS
Engine is a PBS engine.
Engine * e
The actual search engine.
Options opt
Stored and already expanded options.
Builder(const Options &opt, bool best)
Initialize with options opt and best solution search support.
bool best(void) const
Whether engine is a best solution search engine.
Base class for cutoff generators for restart-based meta engine.
Cutoff(void)
Default constructor.
Search engine implementation interface
Exception: Mixed non-best and best solution search requested
Exception: No assets requested for portfolio-based search
WorkerControl worker_control
bool clone
Whether engines create a clone when being initialized.
Cutoff * cutoff
Cutoff for restart-based search.
Options expand(void) const
Expand with real number of threads.
Stop * stop
Stop object for stopping search.
SearchTracer * tracer
Tracer object for tracing search.
unsigned int assets
Number of assets (engines) in a portfolio.
double threads
Number of threads to use.
Parallel depth-first search engine
PbsBuilder(const Options &opt)
The constructor.
virtual Engine * operator()(Space *s) const
The actual build function.
Cutoff of a portfolio asset, the assets share the sequence of values.
unsigned long long int operator()(void) const override
Return the current cutoff value.
unsigned long long int v
The current cutoff value of this asset.
~PbsCutoff(void) override
Release the reference to the shared sequence.
PbsCutoffs * s
The shared sequence.
unsigned long long int operator++(void) override
Take the next value of the shared sequence and return it.
PbsCutoff(PbsCutoffs *s0)
Initialize as a new reference to s0.
Cutoff sequence shared by the assets of a portfolio.
Support::Mutex m
Mutex protecting the sequence, assets can run in parallel.
unsigned int n
Number of references.
PbsCutoffs(Cutoff *c0)
Initialize for cutoff c0 with the reference held while building.
Cutoff * c
The shared cutoff sequence.
static void release(PbsCutoffs *s)
Release a reference.
bool owner
Whether the cutoff sequence is disposed with the last reference.
unsigned long long int fail
Number of failed nodes in search tree.
Base-class for Stop-object.
static bool same_identity(const WorkerControl &x, const WorkerControl &y)
Return whether x and y share one control identity.
Exception: Worker control is already bound or used in an unsupported engine
External control for the requested number of search workers.
bool engaged(void) const noexcept
Whether this handle is engaged.
static void engine(SearchTracer *tracer, SearchTracer::EngineType t, unsigned int n)
Register engine.
A lock as a scoped frontend for a mutex.
A mutex for mutual exclausion among several threads.
virtual bool slave(const MetaInfo &mi)
Slave configuration function for meta search engines.
T * pbs(T *s, const Search::Options &o=Search::Options::def)
Run a portfolio of search engines.
@ SS_FAILED
Space is failed
Engine * pbsengine(Engine **slaves, Stop **stops, unsigned int n_slaves, const Statistics &stat, bool best)
Create parallel portfolio engine.
Stop * pbsstop(Stop *so, const WorkerControl &control)
Create stop object.
Engine * dead(const Options &o, const Statistics &stat)
Stop * pbsstop(Stop *so)
Create stop object.
Engine * pbsengine(Engine **slaves, Stop **stops, unsigned int n_slaves, const Statistics &stat, const Search::Options &opt, bool best)
Create sequential portfolio engine.
int pbsadmitted(const SEBs &sebs, const Options &opt)
Return the number of explicit builders admitted by this portfolio.
void release_pbs_cutoffs(PbsCutoffs *cs, bool built)
Release the cutoffs cs held while building.
Engine * build(Space *s, const Options &opt)
Build an engine of type E for a script T.
PbsCutoffs * make_pbs_cutoffs(const Options &opt, unsigned int n_slaves)
Return the cutoffs shared by the assets of a portfolio.
void pbscleanup(Engine **slaves, Stop **stops, unsigned int n_slaves, Space *master)
Dispose partially constructed portfolio assets.
Engine * pbspar(T *master, const Search::Statistics &stat, Options &opt)
void pbscontrol(const Options &opt)
Validate worker-control placement for a homogeneous portfolio.
Engine * pbsseq(T *master, const Search::Statistics &stat, Options &opt)
Gecode toplevel namespace
Search::Builder * SEB
Type for a search engine builder.
#define GECODE_SEARCH_EXPORT
static constexpr bool value
static constexpr bool value
Whether engine E takes ownership of the cutoff in its options.
static constexpr bool value