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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
/* -*- 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 {
/*
* Engine: basic access routines
*/
template<class Tracer>
forceinline BAB<Tracer>&
BAB<Tracer>::Worker::engine(void) const {
return static_cast<BAB&>(_engine);
}
template<class Tracer>
forceinline typename BAB<Tracer>::Worker*
BAB<Tracer>::worker(unsigned int i) const {
return _worker[i];
}
template<class Tracer>
forceinline void
BAB<Tracer>::Worker::reset(Space* s, unsigned int ngdl) {
tracer.round();
delete cur;
delete best;
best = nullptr;
path.reset((s == nullptr) ? 0 : ngdl);
d = 0;
mark = 0;
idle = false;
if ((s == nullptr) || (s->status(*this) == SS_FAILED)) {
delete s;
cur = nullptr;
} else {
cur = s;
}
Search::Worker::reset();
}
/*
* Engine: initialization
*/
template<class Tracer>
forceinline
BAB<Tracer>::Worker::Worker(Space* s, BAB& e)
: Engine<Tracer>::Worker(s,e), mark(0), best(nullptr) {}
template<class Tracer>
forceinline
BAB<Tracer>::BAB(Space* s, const Options& o)
: Engine<Tracer>(o), best(nullptr) {
WrapTraceRecorder::engine(o.tracer, SearchTracer::EngineType::DFS,
workers());
// Create workers
_worker = static_cast<Worker**>
(heap.ralloc(workers() * sizeof(Worker*)));
// The first worker gets the entire search tree
_worker[0] = new Worker(s,*this);
// All other workers start with no work
for (unsigned int i=1U; i<workers(); i++)
_worker[i] = new Worker(nullptr,*this);
// Block all workers
block();
// Create and start threads
for (unsigned int i=0U; i<workers(); i++)
Support::Thread::run(_worker[i]);
}
/*
* Engine: search control
*/
template<class Tracer>
forceinline void
BAB<Tracer>::Worker::better(Space* b) {
m.acquire();
delete best;
best = b->clone();
mark = path.entries();
if (cur != nullptr)
cur->constrain(*best);
m.release();
}
template<class Tracer>
forceinline void
BAB<Tracer>::solution(Space* s) {
m_search.acquire();
if (best != nullptr) {
s->constrain(*best);
if (s->status() == SS_FAILED) {
delete s;
m_search.release();
return;
} else {
delete best;
best = s->clone();
}
} else {
best = s->clone();
}
// Announce better solutions
for (unsigned int i=0U; i<workers(); i++)
worker(i)->better(best);
bool bs = signal();
solutions.push(s);
if (bs)
e_search.signal();
m_search.release();
}
/*
* Worker: finding and stealing working
*/
template<class Tracer>
forceinline void
BAB<Tracer>::Worker::find(void) {
// Try to find new work (even if there is none)
for (unsigned int i=0U; i<engine().workers(); i++) {
unsigned long int r_d = 0ul;
typename Engine<Tracer>::Worker* wi = engine().worker(i);
if (Space* s = wi->steal(r_d,wi->tracer,tracer)) {
// Reset this guy
m.acquire();
idle = false;
// Not idle but also does not have the root of the tree
path.ngdl(0);
d = 0;
cur = s;
mark = 0;
if (best != nullptr)
cur->constrain(*best);
Statistics t = *this;
Search::Worker::reset(r_d);
(*this) += t;
m.release();
return;
}
}
}
/*
* Statistics
*/
template<class Tracer>
Statistics
BAB<Tracer>::statistics(void) const {
Statistics s;
for (unsigned int i=0U; i<workers(); i++)
s += worker(i)->statistics();
return s;
}
template<class Tracer>
void
BAB<Tracer>::constrain(const Space& b) {
m_search.acquire();
if (best != nullptr) {
best->constrain(b);
if (best->status() != SS_FAILED) {
m_search.release();
return;
}
delete best;
}
best = b.clone();
// Announce better solutions
for (unsigned int i=0U; i<workers(); i++)
worker(i)->better(best);
m_search.release();
}
/*
* Actual work
*/
template<class Tracer>
void
BAB<Tracer>::Worker::run(void) {
/*
* The engine maintains the following invariant:
* - If the current space (cur) is not nullptr, the path always points
* to exactly that space.
* - If the current space (cur) is nullptr, the path always points
* to the next space (if there is any).
*
* This invariant is needed so that no-goods can be extracted properly
* when the engine is stopped or has found a solution.
*
* An additional invariant maintained by the engine is:
* For all nodes stored at a depth less than mark, there
* is no guarantee of betterness. For those above the mark,
* betterness is guaranteed.
*
*/
// Perform initial delay, if not first worker
if (this != engine().worker(0))
Support::Thread::sleep(Config::initial_delay);
// Okay, we are in business, start working
while (true) {
switch (engine().cmd()) {
case C_WAIT:
// Wait
engine().wait();
break;
case C_TERMINATE:
// Acknowledge termination request
engine().ack_terminate();
// Wait until termination can proceed
engine().wait_terminate();
// Thread will be terminated by returning from run
return;
case C_RESET:
// Acknowledge reset request
engine().ack_reset_start();
// Wait until reset has been performed
engine().wait_reset();
// Acknowledge that reset cycle is over
engine().ack_reset_stop();
break;
case C_WORK:
// Perform exploration work
{
m.acquire();
if (idle) {
m.release();
// Try to find new work
find();
} else if (cur != nullptr) {
start();
if (stop(engine().opt())) {
// Report stop
m.release();
engine().stop();
} else {
node++;
SearchTracer::EdgeInfo ei;
if (tracer) {
if (path.entries() > 0) {
typename Path<Tracer>::Edge& top = path.top();
ei.init(tracer.wid(), top.nid(), top.truealt(),
*cur, *top.choice());
} else if (*tracer.ei()) {
ei = *tracer.ei();
tracer.invalidate();
}
}
unsigned int nid = tracer.nid();
switch (cur->status(*this)) {
case SS_FAILED:
if (tracer) {
SearchTracer::NodeInfo ni(SearchTracer::NodeType::FAILED,
tracer.wid(), nid, *cur);
tracer.node(ei,ni);
}
fail++;
delete cur;
cur = nullptr;
path.next();
m.release();
break;
case SS_SOLVED:
{
if (tracer) {
SearchTracer::NodeInfo ni(SearchTracer::NodeType::SOLVED,
tracer.wid(), nid, *cur);
tracer.node(ei,ni);
}
// Deletes all pending branchers
(void) cur->choice();
Space* s = cur->clone();
delete cur;
cur = nullptr;
path.next();
m.release();
engine().solution(s);
}
break;
case SS_BRANCH:
{
Space* c;
if ((d == 0) || (d >= engine().opt().c_d)) {
c = cur->clone();
d = 1;
} else {
c = nullptr;
d++;
}
const Choice* ch = path.push(*this,cur,c,nid);
if (tracer) {
SearchTracer::NodeInfo ni(SearchTracer::NodeType::BRANCH,
tracer.wid(), nid, *cur, ch);
tracer.node(ei,ni);
}
cur->commit(*ch,0);
m.release();
}
break;
default:
GECODE_NEVER;
}
}
} else if (!path.empty()) {
cur = path.recompute(d,engine().opt().a_d,*this,*best,mark,tracer);
if (cur == nullptr)
path.next();
m.release();
} else {
idle = true;
path.ngdl(0);
m.release();
// Report that worker is idle
engine().idle();
}
}
break;
default:
GECODE_NEVER;
}
}
}
/*
* Perform reset
*
*/
template<class Tracer>
void
BAB<Tracer>::reset(Space* s) {
// Grab wait lock for reset
m_wait_reset.acquire();
// Release workers for reset
release(C_RESET);
// Wait for reset cycle started
e_reset_ack_start.wait();
// All workers are marked as busy again
delete best;
best = nullptr;
n_busy = workers();
for (unsigned int i=1U; i<workers(); i++)
worker(i)->reset(nullptr,0);
worker(0)->reset(s,opt().nogoods_limit);
// Block workers again to ensure invariant
block();
// Release reset lock
m_wait_reset.release();
// Wait for reset cycle stopped
e_reset_ack_stop.wait();
}
/*
* Create no-goods
*
*/
template<class Tracer>
NoGoods&
BAB<Tracer>::nogoods(void) {
NoGoods* ng;
// Grab wait lock for reset
m_wait_reset.acquire();
// Release workers for reset
release(C_RESET);
// Wait for reset cycle started
e_reset_ack_start.wait();
ng = &worker(0)->nogoods();
// Block workers again to ensure invariant
block();
// Release reset lock
m_wait_reset.release();
// Wait for reset cycle stopped
e_reset_ack_stop.wait();
return *ng;
}
/*
* Termination and deletion
*/
template<class Tracer>
BAB<Tracer>::Worker::~Worker(void) {
delete best;
}
template<class Tracer>
BAB<Tracer>::~BAB(void) {
terminate();
delete best;
heap.rfree(_worker);
}
}}}
// STATISTICS: search-par
|