Generated on Wed Jan 24 2018 21:22:26 for Gecode by doxygen 1.8.13
gpi.hpp
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: 2017-04-01 20:27:10 +0200 (Sat, 01 Apr 2017) $ by $Author: schulte $
11  * $Revision: 15623 $
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 <cmath>
39 
40 namespace Gecode {
41 
43  class GPI {
44  public:
46  class Info {
47  public:
49  unsigned int pid;
51  unsigned int gid;
53  double afc;
55  void init(unsigned int pid, unsigned int gid);
56  };
57  private:
59  class Block : public HeapAllocated {
60  public:
62  static const int n_info = 8192;
64  Info info[n_info];
66  Block* next;
68  int free;
70  Block(void);
72  void rescale(void);
73  };
75  class Object : public HeapAllocated {
76  private:
78  Block fst;
80  Block* b;
84  double invd;
86  unsigned int pid;
88  unsigned int use_cnt;
89  public:
91  Object(void);
93  void inc(void);
95  void decay(double d);
97  double decay(void) const;
99  void fail(Info& c);
101  Info* allocate(unsigned int gid);
103  void dispose(void);
104  };
106  Object* o;
107  public:
109  GPI(void);
111  GPI(const GPI& ga);
113  ~GPI(void);
115  void decay(double d);
117  double decay(void) const;
119  void fail(Info& c);
121  Info* allocate(unsigned int gid);
122  };
123 
124 
125  forceinline void
126  GPI::Info::init(unsigned int pid0, unsigned int gid0) {
127  pid=pid0; gid=gid0; afc=1.0;
128  }
129 
130 
132  GPI::Block::Block(void)
133  : next(NULL), free(n_info) {}
134 
135  forceinline void
136  GPI::Block::rescale(void) {
137  for (int i=free; i < n_info; i++)
138  info[i].afc *= Kernel::Config::rescale;
139  }
140 
141 
143  GPI::Object::Object(void)
144  : b(&fst), invd(1.0), pid(0U), use_cnt(1U) {}
145 
146  forceinline void
147  GPI::Object::inc(void) {
148  m.acquire();
149  use_cnt++;
150  m.release();
151  }
152 
153  forceinline void
154  GPI::Object::fail(Info& c) {
155  m.acquire();
156  c.afc = invd * (c.afc + 1.0);
158  for (Block* i = b; i != NULL; i = i->next)
159  i->rescale();
160  m.release();
161  }
162 
163  forceinline double
164  GPI::Object::decay(void) const {
165  double d;
166  const_cast<GPI::Object&>(*this).m.acquire();
167  d = 1.0 / invd;
168  const_cast<GPI::Object&>(*this).m.release();
169  return d;
170  }
171 
172  forceinline void
173  GPI::Object::decay(double d) {
174  m.acquire();
175  invd = 1.0 / d;
176  m.release();
177  }
178 
180  GPI::Object::allocate(unsigned int gid) {
181  Info* c;
182  m.acquire();
183  if (b->free == 0) {
184  Block* n = new Block;
185  n->next = b; b = n;
186  }
187  c = &b->info[--b->free];
188  m.release();
189  c->init(pid++,gid);
190  return c;
191  }
192 
193  forceinline void
194  GPI::Object::dispose(void) {
195  m.acquire();
196  if (--use_cnt > 0) {
197  m.release();
198  return;
199  }
200  // We are on our own here!
201  m.release();
202  Block* n = b;
203  while (n != &fst) {
204  Block* d = n;
205  n = n->next;
206  delete d;
207  }
208  delete this;
209  }
210 
211 
212 
214  GPI::GPI(void) : o(new Object) {}
215 
217  GPI::GPI(const GPI& gpi) : o(gpi.o) {
218  o->inc();
219  }
220 
222  GPI::~GPI(void) {
223  o->dispose();
224  }
225 
226  forceinline void
228  o->fail(c);
229  }
230 
231  forceinline double
232  GPI::decay(void) const {
233  return o->decay();
234  }
235 
236  forceinline void
237  GPI::decay(double d) {
238  o->decay(d);
239  }
240 
242  GPI::allocate(unsigned int gid) {
243  return o->allocate(gid);
244  }
245 
246 }
247 
248 // STATISTICS: kernel-prop
const double rescale_limit
Rescale action and afc values when larger than this.
Definition: kernel.hh:105
A mutex for mutual exclausion among several threads.
Definition: thread.hpp:99
Gecode::IntSet d(v, 7)
Gecode::FloatVal c(-8, 8)
~GPI(void)
Destructor.
Definition: gpi.hpp:222
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Global propagator information.
Definition: gpi.hpp:43
double afc
The afc value.
Definition: gpi.hpp:53
unsigned int pid
Propagator identifier.
Definition: gpi.hpp:49
Info * allocate(unsigned int gid)
Allocate new actor info.
Definition: gpi.hpp:242
struct Gecode::@579::NNF::@61::@62 b
For binary nodes (and, or, eqv)
double decay(void) const
Return decay factor.
Definition: gpi.hpp:232
unsigned int gid
Group identifier.
Definition: gpi.hpp:51
void init(unsigned int pid, unsigned int gid)
Initialize.
Definition: gpi.hpp:126
#define forceinline
Definition: config.hpp:173
const double rescale
Rescale factor for action and afc values.
Definition: kernel.hh:103
void fail(Info &c)
Increment failure count.
Definition: gpi.hpp:227
Gecode toplevel namespace
Class for storing timed-decay value.
Definition: gpi.hpp:46
GPI(void)
Initialize.
Definition: gpi.hpp:214
Base class for heap allocated objects.
Definition: heap.hpp:344