Generated on Wed Sep 5 2012 18:52:05 for Gecode by doxygen 1.8.1.1
bab.cpp
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.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2009
8  *
9  * Last modified:
10  * $Date: 2009-08-27 05:10:00 +1000 (Thu, 27 Aug 2009) $ by $Author: schulte $
11  * $Revision: 9632 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
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 #include <gecode/support.hh>
39 
40 #ifdef GECODE_HAS_THREADS
41 
43 
44 namespace Gecode { namespace Search { namespace Parallel {
45 
46  /*
47  * Statistics
48  */
49  Statistics
50  BAB::statistics(void) const {
51  Statistics s;
52  for (unsigned int i=0; i<workers(); i++)
53  s += worker(i)->statistics();
54  return s;
55  }
56 
57  /*
58  * Actual work
59  */
60  void
62  // Peform initial delay, if not first worker
63  if (this != engine().worker(0))
65  // Okay, we are in business, start working
66  while (true) {
67  switch (engine().cmd()) {
68  case C_WAIT:
69  // Wait
70  engine().wait();
71  break;
72  case C_TERMINATE:
73  // Acknowledge termination request
75  // Wait until termination can proceed
77  // Terminate thread
78  engine().terminated();
79  return;
80  case C_WORK:
81  // Perform exploration work
82  {
83  m.acquire();
84  if (idle) {
85  m.release();
86  // Try to find new work
87  find();
88  } else if (cur != NULL) {
89  start();
90  if (stop(engine().opt(),path.size())) {
91  // Report stop
92  m.release();
93  engine().stop();
94  } else {
95  /*
96  * The invariant maintained by the engine is:
97  * For all nodes stored at a depth less than mark, there
98  * is no guarantee of betterness. For those above the mark,
99  * betterness is guaranteed.
100  */
101  node++;
102  switch (cur->status(*this)) {
103  case SS_FAILED:
104  fail++;
105  delete cur;
106  cur = NULL;
107  Worker::current(NULL);
108  m.release();
109  break;
110  case SS_SOLVED:
111  {
112  // Deletes all pending branchers
113  (void) cur->choice();
114  Space* s = cur->clone(false);
115  delete cur;
116  cur = NULL;
117  Worker::current(NULL);
118  m.release();
119  engine().solution(s);
120  }
121  break;
122  case SS_BRANCH:
123  {
124  Space* c;
125  if ((d == 0) || (d >= engine().opt().c_d)) {
126  c = cur->clone();
127  d = 1;
128  } else {
129  c = NULL;
130  d++;
131  }
132  const Choice* ch = path.push(*this,cur,c);
133  Worker::push(c,ch);
134  cur->commit(*ch,0);
135  m.release();
136  }
137  break;
138  default:
139  GECODE_NEVER;
140  }
141  }
142  } else if (path.next(*this)) {
143  cur = path.recompute(d,engine().opt().a_d,*this,best,mark);
145  m.release();
146  } else {
147  idle = true;
148  m.release();
149  // Report that worker is idle
150  engine().idle();
151  }
152  }
153  break;
154  default:
155  GECODE_NEVER;
156  }
157  }
158  }
159 
160 
161  /*
162  * Termination and deletion
163  */
165  delete best;
166  }
167 
168  BAB::~BAB(void) {
169  terminate();
170  delete best;
171  heap.rfree(_worker);
172  }
173 
174 }}}
175 
176 #endif
177 
178 // STATISTICS: search-parallel