Generated on for Gecode by doxygen 1.17.0
engine.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, 2009
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_ENGINE_HH
39#define GECODE_SEARCH_PAR_ENGINE_HH
40
41#include <gecode/search.hh>
45#include <atomic>
46
47namespace Gecode { namespace Search { namespace Par {
48
50 template<class Tracer>
51 class Engine : public Search::Engine, public Support::Terminator {
52 protected:
54 class Worker : public Search::Worker, public Support::Runnable {
55 public:
58 protected:
68 unsigned int d;
70 bool idle;
71 public:
73 Worker(Space* s, Engine& e);
75 Space* steal(unsigned long int& d, Tracer& myt, Tracer& ot);
79 Engine& engine(void) const;
81 NoGoods& nogoods(void);
83 virtual ~Worker(void);
85 virtual Support::Terminator* terminator(void) const;
86 };
87
89 public:
91 const Options& opt(void) const;
93 unsigned int workers(void) const;
94
96
97
104 protected:
106 std::atomic<Cmd> _cmd;
109 public:
111 Cmd cmd(void) const;
113 void block(void);
115 void release(Cmd c);
117 void wait(void);
119
121
122 protected:
126 volatile unsigned int _n_term_not_ack;
132 volatile unsigned int _n_not_terminated;
135 public:
137 void ack_terminate(void);
139 virtual void terminated(void);
141 void wait_terminate(void);
143 void terminate(void);
145
147
148 protected:
152 volatile unsigned int _n_reset_not_ack;
159 public:
161 void ack_reset_start(void);
163 void ack_reset_stop(void);
165 void wait_reset(void);
167
169
170 protected:
178 volatile unsigned int n_busy;
180 std::atomic<bool> has_stopped;
182 bool signal(void) const;
183 public:
185 void idle(void);
187 void busy(void);
189 void stop(void);
191
193
194
195 Engine(const Options& o);
197 virtual Space* next(void);
199 virtual bool stopped(void) const;
201
203 virtual ~Engine(void);
204 };
205
206}}}
207
209
210#endif
211
212// STATISTICS: search-par
No-goods recorded from restarts.
Definition core.hpp:1599
Search engine implementation interface
Definition search.hh:943
Search engine options
Definition search.hh:751
Support::Mutex m
Mutex for access to worker.
Definition engine.hh:62
Engine & _engine
Reference to engine.
Definition engine.hh:60
NoGoods & nogoods(void)
Return no-goods.
Definition engine.hpp:292
Statistics statistics(void)
Return statistics.
Definition engine.hpp:138
Space * cur
Current space being explored.
Definition engine.hh:66
virtual ~Worker(void)
Destructor.
Definition engine.hpp:361
Tracer tracer
Search tracer.
Definition engine.hh:57
Worker(Space *s, Engine &e)
Initialize for space s with engine e.
Definition engine.hpp:99
Space * steal(unsigned long int &d, Tracer &myt, Tracer &ot)
Hand over some work (nullptr if no work available).
Definition engine.hpp:270
unsigned int d
Distance until next clone.
Definition engine.hh:68
bool idle
Whether the worker is idle.
Definition engine.hh:70
Path< Tracer > path
Current path ins search tree.
Definition engine.hh:64
virtual Support::Terminator * terminator(void) const
Terminator (engine).
Definition engine.hpp:353
Engine & engine(void) const
Provide access to engine.
Definition engine.hpp:46
void idle(void)
Report that worker is idle.
Definition engine.hpp:157
void wait(void)
Ensure that worker waits.
Definition engine.hpp:89
Support::Event _e_terminate
Event for termination (all threads have terminated).
Definition engine.hh:134
void ack_reset_stop(void)
For worker to acknowledge stop of reset cycle.
Definition engine.hpp:249
const Options & opt(void) const
Provide access to search options.
Definition engine.hpp:51
Support::Mutex _m_wait_terminate
Mutex for waiting for termination.
Definition engine.hh:130
Support::Event e_reset_ack_stop
Event for reset acknowledgment stopped.
Definition engine.hh:156
Support::Mutex _m_wait
Mutex for forcing workers to wait.
Definition engine.hh:108
Support::Event e_search
Event for search (solution found, no more solutions, search stopped).
Definition engine.hh:174
virtual Space * next(void)
Return next solution (nullptr, if none exists or search has been stopped).
Definition engine.hpp:301
Support::Event _e_term_ack
Event for termination acknowledgment.
Definition engine.hh:128
Support::DynamicQueue< Space *, Heap > solutions
Queue of solutions.
Definition engine.hh:176
virtual bool stopped(void) const
Check whether engine has been stopped.
Definition engine.hpp:61
void stop(void)
Report that worker has been stopped.
Definition engine.hpp:177
void block(void)
Block all workers.
Definition engine.hpp:77
Support::Mutex m_wait_reset
Mutex for waiting for reset.
Definition engine.hh:158
Support::Mutex m_search
Mutex for search.
Definition engine.hh:172
Support::Event e_reset_ack_start
Event for reset acknowledgment started.
Definition engine.hh:154
Support::Mutex _m_term
Mutex for access to termination information.
Definition engine.hh:124
std::atomic< bool > has_stopped
Whether a worker had been stopped.
Definition engine.hh:180
Cmd cmd(void) const
Return current command.
Definition engine.hpp:72
virtual ~Engine(void)
Destructor.
Definition engine.hpp:371
Engine(const Options &o)
Initialize with options o.
Definition engine.hpp:120
Options _opt
Search options.
Definition engine.hh:88
void busy(void)
Report that worker is busy.
Definition engine.hpp:168
volatile unsigned int _n_reset_not_ack
Number of workers that have not yet acknowledged reset.
Definition engine.hh:152
void release(Cmd c)
Release all workers.
Definition engine.hpp:83
bool signal(void) const
Whether search state changed such that signal is needed.
Definition engine.hpp:151
void ack_reset_start(void)
For worker to acknowledge start of reset cycle.
Definition engine.hpp:240
volatile unsigned int _n_term_not_ack
Number of workers that have not yet acknowledged termination.
Definition engine.hh:126
void wait_reset(void)
For worker to wait for all workers to reset.
Definition engine.hpp:258
volatile unsigned int _n_not_terminated
Number of not yet terminated workers.
Definition engine.hh:132
virtual void terminated(void)
For worker to register termination.
Definition engine.hpp:192
void terminate(void)
For engine to perform thread termination.
Definition engine.hpp:221
std::atomic< Cmd > _cmd
The current command.
Definition engine.hh:106
Support::Mutex _m_reset
Mutex for access to reset information.
Definition engine.hh:150
volatile unsigned int n_busy
Number of busy workers.
Definition engine.hh:178
void wait_terminate(void)
For worker to wait until termination is legal.
Definition engine.hpp:214
unsigned int workers(void) const
Return number of workers.
Definition engine.hpp:56
Cmd
Commands from engine to workers.
Definition engine.hh:98
@ C_WORK
Perform work.
Definition engine.hh:99
@ C_RESET
Perform reset operation.
Definition engine.hh:101
@ C_WAIT
Run into wait lock.
Definition engine.hh:100
void ack_terminate(void)
For worker to acknowledge termination command.
Definition engine.hpp:205
Depth-first path (stack of edges) supporting recomputation.
Definition path.hh:64
Statistics(void)
Initialize.
Search worker statistics
Definition worker.hh:44
Worker(void)
Initialize.
Definition worker.hh:70
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