gpi.hpp
Go to the documentation of this file.
/* -*- 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.
*
*/
#include <cmath>
namespace Gecode { namespace Kernel {
class GPI {
public:
class Info {
public:
unsigned int pid;
unsigned int gid;
double afc;
void init(unsigned int pid, unsigned int gid);
};
private:
class Block : public HeapAllocated {
public:
static const int n_info = 8192;
Info info[n_info];
Block* next;
int free;
Block(void);
void rescale(void);
};
Block* b;
double invd;
unsigned int npid;
bool us;
Block fst;
GECODE_KERNEL_EXPORT static Support::Mutex m;
public:
GPI(void);
void decay(double d);
double decay(void) const;
void fail(Info& c);
Info* allocate(unsigned int p, unsigned int gid);
Info* allocate(unsigned int gid);
unsigned int pid(void) const;
bool unshare(void);
~GPI(void);
};
forceinline void
GPI::Info::init(unsigned int pid0, unsigned int gid0) {
pid=pid0; gid=gid0; afc=1.0;
}
forceinline
GPI::Block::Block(void)
: next(NULL), free(n_info) {}
forceinline void
GPI::Block::rescale(void) {
for (int i=free; i < n_info; i++)
info[i].afc *= Kernel::Config::rescale;
}
forceinline
GPI::GPI(void)
: b(&fst), invd(1.0), npid(0U), us(false) {}
forceinline void
GPI::fail(Info& c) {
m.acquire();
c.afc = invd * (c.afc + 1.0);
if (c.afc > Kernel::Config::rescale_limit)
for (Block* i = b; i != NULL; i = i->next)
i->rescale();
m.release();
}
forceinline double
GPI::decay(void) const {
double d;
const_cast<GPI&>(*this).m.acquire();
d = 1.0 / invd;
const_cast<GPI&>(*this).m.release();
return d;
}
forceinline unsigned int
GPI::pid(void) const {
unsigned int p;
const_cast<GPI&>(*this).m.acquire();
p = npid;
const_cast<GPI&>(*this).m.release();
return p;
}
forceinline bool
GPI::unshare(void) {
bool u;
m.acquire();
u = us; us = true;
m.release();
return u;
}
forceinline void
GPI::decay(double d) {
m.acquire();
invd = 1.0 / d;
m.release();
}
forceinline GPI::Info*
GPI::allocate(unsigned int p, unsigned int gid) {
Info* c;
m.acquire();
if (b->free == 0) {
Block* n = new Block;
n->next = b; b = n;
}
c = &b->info[--b->free];
m.release();
c->init(p,gid);
return c;
}
forceinline GPI::Info*
GPI::allocate(unsigned int gid) {
Info* c;
m.acquire();
if (b->free == 0) {
Block* n = new Block;
n->next = b; b = n;
}
c = &b->info[--b->free];
c->init(npid++,gid);
m.release();
return c;
}
forceinline
GPI::~GPI(void) {
Block* n = b;
while (n != &fst) {
Block* d = n;
n = n->next;
delete d;
}
}
}}
// STATISTICS: kernel-prop
Gecode::HeapAllocated
Base class for heap allocated objects.
Definition heap.hpp:340
Gecode::Kernel::GPI::Info
Class for storing propagator information.
Definition gpi.hpp:42
Gecode::Kernel::GPI::Info::init
void init(unsigned int pid, unsigned int gid)
Initialize.
Definition gpi.hpp:105
Gecode::Kernel::GPI::Info::pid
unsigned int pid
Propagator identifier.
Definition gpi.hpp:45
Gecode::Kernel::GPI::Info::gid
unsigned int gid
Group identifier.
Definition gpi.hpp:47
Gecode::Kernel::GPI::Info::afc
double afc
The afc value.
Definition gpi.hpp:49
Gecode::Kernel::GPI::GPI
GPI(void)
Initialize.
Definition gpi.hpp:122
Gecode::Kernel::GPI::pid
unsigned int pid(void) const
Return next free propagator id.
Definition gpi.hpp:145
Gecode::Kernel::GPI::decay
double decay(void) const
Return decay factor.
Definition gpi.hpp:136
Gecode::Kernel::GPI::fail
void fail(Info &c)
Increment failure count.
Definition gpi.hpp:126
Gecode::Kernel::GPI::~GPI
~GPI(void)
Delete.
Definition gpi.hpp:198
Gecode::Kernel::GPI::unshare
bool unshare(void)
Provide access to unshare info and set to true.
Definition gpi.hpp:154
Gecode::Kernel::GPI::allocate
Info * allocate(unsigned int p, unsigned int gid)
Allocate info for existing propagator with pid p.
Definition gpi.hpp:170
Gecode::Support::Mutex
A mutex for mutual exclausion among several threads.
Definition thread.hpp:96
GECODE_KERNEL_EXPORT
#define GECODE_KERNEL_EXPORT
Definition kernel.hh:70
Gecode::Kernel::Config::rescale_limit
const double rescale_limit
Rescale action and afc values when larger than this.
Definition kernel.hh:101
Gecode::Kernel::Config::rescale
const double rescale
Rescale factor for action and afc values.
Definition kernel.hh:99
Gecode::Kernel
Kernel functionality
Gecode
Gecode toplevel namespace
forceinline
#define forceinline
Definition config.hpp:187