Generated on Sat Oct 20 2018 12:43:45 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  * This file is part of Gecode, the generic constraint
10  * development environment:
11  * http://www.gecode.org
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining
14  * a copy of this software and associated documentation files (the
15  * "Software"), to deal in the Software without restriction, including
16  * without limitation the rights to use, copy, modify, merge, publish,
17  * distribute, sublicense, and/or sell copies of the Software, and to
18  * permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be
22  * included in all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31  *
32  */
33 
34 #include <cmath>
35 
36 namespace Gecode { namespace Kernel {
37 
39  class GPI {
40  public:
42  class Info {
43  public:
45  unsigned int pid;
47  unsigned int gid;
49  double afc;
51  void init(unsigned int pid, unsigned int gid);
52  };
53  private:
55  class Block : public HeapAllocated {
56  public:
58  static const int n_info = 8192;
60  Info info[n_info];
62  Block* next;
64  int free;
66  Block(void);
68  void rescale(void);
69  };
71  Block* b;
73  double invd;
75  unsigned int pid;
77  bool us;
79  Block fst;
82  public:
84  GPI(void);
86  void decay(double d);
88  double decay(void) const;
90  void fail(Info& c);
92  Info* allocate(unsigned int p, unsigned int gid);
94  Info* allocate(unsigned int gid);
96  bool unshare(void);
98  ~GPI(void);
99  };
100 
101 
102  forceinline void
103  GPI::Info::init(unsigned int pid0, unsigned int gid0) {
104  pid=pid0; gid=gid0; afc=1.0;
105  }
106 
107 
109  GPI::Block::Block(void)
110  : next(NULL), free(n_info) {}
111 
112  forceinline void
113  GPI::Block::rescale(void) {
114  for (int i=free; i < n_info; i++)
115  info[i].afc *= Kernel::Config::rescale;
116  }
117 
118 
120  GPI::GPI(void)
121  : b(&fst), invd(1.0), pid(0U), us(false) {}
122 
123  forceinline void
125  m.acquire();
126  c.afc = invd * (c.afc + 1.0);
128  for (Block* i = b; i != NULL; i = i->next)
129  i->rescale();
130  m.release();
131  }
132 
133  forceinline double
134  GPI::decay(void) const {
135  double d;
136  const_cast<GPI&>(*this).m.acquire();
137  d = 1.0 / invd;
138  const_cast<GPI&>(*this).m.release();
139  return d;
140  }
141 
142  forceinline bool
143  GPI::unshare(void) {
144  bool u;
145  m.acquire();
146  u = us; us = true;
147  m.release();
148  return u;
149  }
150 
151  forceinline void
152  GPI::decay(double d) {
153  m.acquire();
154  invd = 1.0 / d;
155  m.release();
156  }
157 
159  GPI::allocate(unsigned int p, unsigned int gid) {
160  Info* c;
161  m.acquire();
162  if (b->free == 0) {
163  Block* n = new Block;
164  n->next = b; b = n;
165  }
166  c = &b->info[--b->free];
167  m.release();
168  c->init(p,gid);
169  return c;
170  }
171 
173  GPI::allocate(unsigned int gid) {
174  Info* c;
175  m.acquire();
176  if (b->free == 0) {
177  Block* n = new Block;
178  n->next = b; b = n;
179  }
180  c = &b->info[--b->free];
181  c->init(pid++,gid);
182  m.release();
183  return c;
184  }
185 
187  GPI::~GPI(void) {
188  Block* n = b;
189  while (n != &fst) {
190  Block* d = n;
191  n = n->next;
192  delete d;
193  }
194  }
195 
196 }}
197 
198 // STATISTICS: kernel-prop
Info * allocate(unsigned int p, unsigned int gid)
Allocate info for existing propagator with pid p.
Definition: gpi.hpp:159
void fail(Info &c)
Increment failure count.
Definition: gpi.hpp:124
const double rescale_limit
Rescale action and afc values when larger than this.
Definition: kernel.hh:101
bool unshare(void)
Provide access to unshare info and set to true.
Definition: gpi.hpp:143
void acquire(void)
Acquire the mutex and possibly block.
Definition: none.hpp:42
double decay(void) const
Return decay factor.
Definition: gpi.hpp:134
#define forceinline
Definition: config.hpp:185
A mutex for mutual exclausion among several threads.
Definition: thread.hpp:96
Gecode::IntSet d(v, 7)
Global propagator information.
Definition: gpi.hpp:39
void release(void)
Release the mutex.
Definition: none.hpp:48
Gecode::FloatVal c(-8, 8)
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:232
GPI(void)
Initialize.
Definition: gpi.hpp:120
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
Gecode::IntArgs i({1, 2, 3, 4})
struct Gecode::@593::NNF::@62::@63 b
For binary nodes (and, or, eqv)
#define GECODE_KERNEL_EXPORT
Definition: kernel.hh:70
union Gecode::@593::NNF::@62 u
Union depending on nodetype t.
Class for storing propagator information.
Definition: gpi.hpp:42
void init(unsigned int pid, unsigned int gid)
Initialize.
Definition: gpi.hpp:103
const double rescale
Rescale factor for action and afc values.
Definition: kernel.hh:99
double afc
The afc value.
Definition: gpi.hpp:49
~GPI(void)
Delete.
Definition: gpi.hpp:187
Gecode toplevel namespace
unsigned int pid
Propagator identifier.
Definition: gpi.hpp:45
unsigned int gid
Group identifier.
Definition: gpi.hpp:47
Base class for heap allocated objects.
Definition: heap.hpp:340