1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 2009
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
namespace Gecode { namespace Search { namespace Par {
/*
* Basic access routines
*/
template<class Tracer>
forceinline Engine<Tracer>&
Engine<Tracer>::Worker::engine(void) const {
return _engine;
}
template<class Tracer>
forceinline const Options&
Engine<Tracer>::opt(void) const {
return _opt;
}
template<class Tracer>
forceinline unsigned int
Engine<Tracer>::workers(void) const {
return static_cast<unsigned int>(opt().threads);
}
template<class Tracer>
forceinline bool
Engine<Tracer>::stopped(void) const {
return has_stopped;
}
/*
* Engine: command and wait handling
*/
template<class Tracer>
forceinline typename Engine<Tracer>::Cmd
Engine<Tracer>::cmd(void) const {
return _cmd;
}
template<class Tracer>
forceinline void
Engine<Tracer>::block(void) {
_cmd = C_WAIT;
Support::Thread::acquireGlobalMutex(&_m_wait);
}
template<class Tracer>
forceinline void
Engine<Tracer>::release(Cmd c) {
_cmd = c;
Support::Thread::releaseGlobalMutex(&_m_wait);
}
template<class Tracer>
forceinline void
Engine<Tracer>::wait(void) {
_m_wait.acquire(); _m_wait.release();
}
/*
* Engine: initialization
*/
template<class Tracer>
forceinline
Engine<Tracer>::Worker::Worker(Space* s, Engine& e)
: tracer(e.opt().tracer), _engine(e),
path(s == nullptr ? 0 : e.opt().nogoods_limit), d(0),
idle(false) {
tracer.worker();
if (s != nullptr) {
if (s->status(*this) == SS_FAILED) {
fail++;
cur = nullptr;
if (!engine().opt().clone)
delete s;
} else {
cur = snapshot(s,engine().opt());
}
} else {
cur = nullptr;
}
}
template<class Tracer>
forceinline
Engine<Tracer>::Engine(const Options& o)
: _opt(o), solutions(heap) {
// Initialize termination information
_n_term_not_ack = workers();
_n_not_terminated = workers();
// Initialize search information
n_busy = workers();
has_stopped = false;
// Initialize reset information
_n_reset_not_ack = workers();
}
/*
* Statistics
*/
template<class Tracer>
forceinline Statistics
Engine<Tracer>::Worker::statistics(void) {
m.acquire();
Statistics s = *this;
m.release();
return s;
}
/*
* Engine: search control
*/
template<class Tracer>
forceinline bool
Engine<Tracer>::signal(void) const {
return solutions.empty() && (n_busy > 0) && !has_stopped;
}
template<class Tracer>
forceinline void
Engine<Tracer>::idle(void) {
m_search.acquire();
bool bs = signal();
n_busy--;
if (bs && (n_busy == 0))
e_search.signal();
m_search.release();
}
template<class Tracer>
forceinline void
Engine<Tracer>::busy(void) {
m_search.acquire();
assert(n_busy > 0);
n_busy++;
m_search.release();
}
template<class Tracer>
forceinline void
Engine<Tracer>::stop(void) {
m_search.acquire();
bool bs = signal();
has_stopped = true;
if (bs)
e_search.signal();
m_search.release();
}
/*
* Engine: termination control
*/
template<class Tracer>
forceinline void
Engine<Tracer>::terminated(void) {
unsigned int n;
_m_term.acquire();
n = --_n_not_terminated;
_m_term.release();
// The signal must be outside of the look, otherwise a thread might be
// terminated that still holds a mutex.
if (n == 0)
_e_terminate.signal();
}
template<class Tracer>
forceinline void
Engine<Tracer>::ack_terminate(void) {
_m_term.acquire();
if (--_n_term_not_ack == 0)
_e_term_ack.signal();
_m_term.release();
}
template<class Tracer>
forceinline void
Engine<Tracer>::wait_terminate(void) {
_m_wait_terminate.acquire();
_m_wait_terminate.release();
}
template<class Tracer>
forceinline void
Engine<Tracer>::terminate(void) {
// Grab the wait mutex for termination
_m_wait_terminate.acquire();
// Release all threads
release(C_TERMINATE);
// Wait until all threads have acknowledged termination request
_e_term_ack.wait();
// Release waiting threads
_m_wait_terminate.release();
// Wait until all threads have in fact terminated
_e_terminate.wait();
// Now all threads are terminated!
}
/*
* Engine: reset control
*/
template<class Tracer>
forceinline void
Engine<Tracer>::ack_reset_start(void) {
_m_reset.acquire();
if (--_n_reset_not_ack == 0)
e_reset_ack_start.signal();
_m_reset.release();
}
template<class Tracer>
forceinline void
Engine<Tracer>::ack_reset_stop(void) {
_m_reset.acquire();
if (++_n_reset_not_ack == workers())
e_reset_ack_stop.signal();
_m_reset.release();
}
template<class Tracer>
forceinline void
Engine<Tracer>::wait_reset(void) {
m_wait_reset.acquire();
m_wait_reset.release();
}
/*
* Worker: finding and stealing working
*/
template<class Tracer>
forceinline Space*
Engine<Tracer>::Worker::steal(unsigned long int& d,
Tracer& myt, Tracer& ot) {
/*
* Make a quick check whether the worker might have work
*
* If that is not true any longer, the worker will be asked
* again eventually.
*/
if (!path.steal())
return nullptr;
m.acquire();
Space* s = path.steal(*this,d,myt,ot);
m.release();
// Tell that there will be one more busy worker
if (s != nullptr)
engine().busy();
return s;
}
/*
* Return No-Goods
*/
template<class Tracer>
forceinline NoGoods&
Engine<Tracer>::Worker::nogoods(void) {
return path;
}
/*
* Engine: search control
*/
template<class Tracer>
Space*
Engine<Tracer>::next(void) {
// Invariant: the worker holds the wait mutex
m_search.acquire();
if (!solutions.empty()) {
// No search needs to be done, take leftover solution
Space* s = solutions.pop();
m_search.release();
return s;
}
// We ignore stopped (it will be reported again if needed)
has_stopped = false;
// No more solutions?
if (n_busy == 0) {
m_search.release();
return nullptr;
}
m_search.release();
// Okay, now search has to continue, make the guys work
release(C_WORK);
/*
* Wait until a search related event has happened. It might be that
* the event has already been signalled in the last run, but the
* solution has been removed. So we have to try until there has
* something new happened.
*/
while (true) {
e_search.wait();
m_search.acquire();
if (!solutions.empty()) {
// Report solution
Space* s = solutions.pop();
m_search.release();
// Make workers wait again
block();
return s;
}
// No more solutions or stopped?
if ((n_busy == 0) || has_stopped) {
m_search.release();
// Make workers wait again
block();
return nullptr;
}
m_search.release();
}
GECODE_NEVER;
return nullptr;
}
template<class Tracer>
Support::Terminator*
Engine<Tracer>::Worker::terminator(void) const {
return &_engine;
}
/*
* Termination and deletion
*/
template<class Tracer>
Engine<Tracer>::Worker::~Worker(void) {
delete cur;
path.reset(0);
tracer.done();
}
/*
* Destructor
*/
template<class Tracer>
Engine<Tracer>::~Engine(void) {
while (!solutions.empty())
delete solutions.pop();
}
}}}
// STATISTICS: search-par
|