/usr/include/gecode/kernel/global-afc.hpp is in libgecode-dev 4.4.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 2009
*
* Last modified:
* $Date: 2013-07-01 06:38:48 +0200 (Mon, 01 Jul 2013) $ by $Author: tack $
* $Revision: 13740 $
*
* 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.
*
*/
#include <cmath>
namespace Gecode {
/// Globally shared object for propagator information
class GlobalAFC {
public:
/// Class for storing timed-decay value
class Counter {
public:
/// The counter value
double c;
/// The time-stamp
unsigned long int t;
/// Initialize
void init(void);
};
private:
/// Block of propagator information (of variable size)
class Block {
public:
/// Next block
Block* next;
/// Start of counter entries
Counter c[1];
/// Allocate block with \a n entries and previous block \a p
static Block* allocate(unsigned int n, Block* p=NULL);
};
/// Class for managing timed-decay values
class DecayManager {
protected:
/// The decay factor
double d;
/// The number of precomputed decay powers
static const unsigned int n_dpow = 8U;
/// Precomputed decay powers
double dpow[n_dpow];
/// The global time-stamp
unsigned long int t;
/// Decay counter value
void decay(Counter& c);
public:
/// Initialize
DecayManager(void);
/// Set decay factor to \a d
void decay(double d);
/// Return current decay factor
double decay(void) const;
/// Increment counter and perform decay
void inc(Counter& c);
/// Set failure count to \a a
void set(Counter& c, double a);
/// Return counter value
double val(Counter& c);
/// Allocate memory from heap
static void* operator new(size_t s);
/// Free memory allocated from heap
static void operator delete(void* p);
};
/// Initial smallest number of entries per block
static const unsigned int size_min = 32;
/// Largest possible number of entries per block
static const unsigned int size_max = 32 * 1024;
/// The actual object to store the required information
class Object {
public:
/// Mutex to synchronize globally shared access
Support::FastMutex* mutex;
/// Pointer to timed decay manager
DecayManager* decay;
/// Link to previous object (NULL if none)
Object* parent;
/// How many spaces or objects use this object
unsigned int use_cnt;
/// Size of current block
unsigned int size;
/// Number of free entries in current block
unsigned int free;
/// Currently used block
Block* cur;
/// Constructor
Object(Support::FastMutex* m, Object* p=NULL);
/// Allocate memory from heap
static void* operator new(size_t s);
/// Free memory allocated from heap
static void operator delete(void* p);
};
/// Pointer to object, possibly marked
void* mo;
/// Pointer to object without mark
Object* object(void) const;
/// Whether pointer points to local object
bool local(void) const;
/// Set pointer to local object
void local(Object* o);
/// Set global pointer to possibly marked object
void global(void* mo);
public:
/// Initialize
GlobalAFC(void);
/// Copy during cloning
GlobalAFC(const GlobalAFC& ga);
/// Destructor
~GlobalAFC(void);
/// Set decay factor to \a d
void decay(double d);
/// Return decay factor
double decay(void) const;
/// Increment failure count
void fail(Counter& c);
/// Set failure count to \a a
void set(Counter& c, double a);
/// Return failure count
double afc(Counter& c);
/// Allocate new propagator info
Counter& allocate(void);
};
forceinline void
GlobalAFC::Counter::init(void) {
c=1.0; t=0UL;
}
forceinline void
GlobalAFC::DecayManager::decay(double d0) {
d = d0;
if (d != 1.0) {
double p = d;
unsigned int i=0;
do {
dpow[i++]=p; p*=d;
} while (i<n_dpow);
}
}
forceinline
GlobalAFC::DecayManager::DecayManager(void)
: d(1.0), t(0UL) {}
forceinline double
GlobalAFC::DecayManager::decay(void) const {
return d;
}
forceinline void
GlobalAFC::DecayManager::decay(Counter& c) {
assert((t >= c.t) && (d != 1.0));
unsigned int n = t - c.t;
if (n > 0) {
if (n <= n_dpow) {
c.c *= dpow[n-1];
} else {
c.c *= pow(d,static_cast<double>(n));
}
c.t = t;
}
}
forceinline void
GlobalAFC::DecayManager::inc(Counter& c) {
if (d == 1.0) {
c.c += 1.0;
} else {
decay(c);
c.c += 1.0; c.t = ++t;
}
}
forceinline double
GlobalAFC::DecayManager::val(Counter& c) {
if (d != 1.0)
decay(c);
return c.c;
}
forceinline void
GlobalAFC::DecayManager::set(Counter& c, double a) {
c.c = a;
}
forceinline void*
GlobalAFC::DecayManager::operator new(size_t s) {
return Gecode::heap.ralloc(s);
}
forceinline void
GlobalAFC::DecayManager::operator delete(void* p) {
Gecode::heap.rfree(p);
}
/*
* Global AFC information
*
*/
forceinline void*
GlobalAFC::Object::operator new(size_t s) {
return Gecode::heap.ralloc(s);
}
forceinline void
GlobalAFC::Object::operator delete(void* p) {
Gecode::heap.rfree(p);
}
forceinline GlobalAFC::Block*
GlobalAFC::Block::allocate(unsigned int n, GlobalAFC::Block* p) {
Block* b = static_cast<Block*>(heap.ralloc(sizeof(Block)+
(n-1)*sizeof(Counter)));
b->next = p;
return b;
}
forceinline
GlobalAFC::Object::Object(Support::FastMutex* m, Object* p)
: mutex(m), parent(p), use_cnt(1), size(size_min), free(size_min),
cur(Block::allocate(size)) {
if (parent == NULL) {
decay = new DecayManager;
} else {
decay = parent->decay;
}
}
forceinline GlobalAFC::Object*
GlobalAFC::object(void) const {
return static_cast<Object*>(Support::funmark(mo));
}
forceinline bool
GlobalAFC::local(void) const {
return !Support::marked(mo);
}
forceinline void
GlobalAFC::local(Object* o) {
assert(!Support::marked(o));
mo = o;
}
forceinline void
GlobalAFC::global(void* o) {
mo = Support::fmark(o);
}
forceinline
GlobalAFC::GlobalAFC(void) {
// No synchronization needed as single thread is creating this object
local(new Object(new Support::FastMutex));
}
forceinline
GlobalAFC::GlobalAFC(const GlobalAFC& gpi) {
global(gpi.mo);
Object* o = object();
o->mutex->acquire();
o->use_cnt++;
o->mutex->release();
}
forceinline
GlobalAFC::~GlobalAFC(void) {
Support::FastMutex* m = object()->mutex;
m->acquire();
Object* c = object();
DecayManager* decay = c->decay;
while ((c != NULL) && (--c->use_cnt == 0)) {
// Delete all blocks for c
Block* b = c->cur;
while (b != NULL) {
Block* d = b; b=b->next;
heap.rfree(d);
}
// Delete object
Object* d = c; c = c->parent;
delete d;
}
m->release();
// All objects are deleted, so also delete mutex and decya info
if (c == NULL) {
delete decay;
delete m;
}
}
forceinline void
GlobalAFC::fail(Counter& c) {
Support::FastMutex& m = *object()->mutex;
m.acquire();
object()->decay->inc(c);
m.release();
}
forceinline void
GlobalAFC::set(Counter& c, double a) {
Support::FastMutex& m = *object()->mutex;
m.acquire();
object()->decay->set(c,a);
m.release();
}
forceinline double
GlobalAFC::afc(Counter& c) {
Support::FastMutex& m = *object()->mutex;
double d;
m.acquire();
d = object()->decay->val(c);
m.release();
return d;
}
forceinline double
GlobalAFC::decay(void) const {
Support::FastMutex& m = *object()->mutex;
double d;
m.acquire();
d = object()->decay->decay();
m.release();
return d;
}
forceinline void
GlobalAFC::decay(double d) {
Support::FastMutex& m = *object()->mutex;
m.acquire();
object()->decay->decay(d);
m.release();
}
forceinline GlobalAFC::Counter&
GlobalAFC::allocate(void) {
/*
* If there is no local object, create one.
*
* There is no synchronization needed as only ONE space has access
* to the marked pointer AND the local object.
*/
if (!local())
local(new Object(object()->mutex,object()));
assert(local());
Object* o = object();
if (o->free == 0) {
if (2*o->size <= size_max)
o->size *= 2;
o->free = o->size;
o->cur = Block::allocate(o->size,o->cur);
}
Counter* c = &o->cur->c[--o->free];
c->init();
return *c;
}
}
// STATISTICS: kernel-prop
|