Skip to content

Commit

Permalink
Added post events for general tracers. (#40)
Browse files Browse the repository at this point in the history
* Generate post events for general tracers

* Also report number of posted propagators
  • Loading branch information
chschulte authored Dec 3, 2018
1 parent a8e7241 commit 8660eed
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 14 deletions.
7 changes: 7 additions & 0 deletions changelog.in
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ Date: ?-?-?
[DESCRIPTION]
New stuff!

[ENTRY]
Module: kernel
What: new
Rank: major
[DESCRIPTION]
General tracers now support post events, see MPG for details.

[ENTRY]
Module: other
What: change
Expand Down
18 changes: 13 additions & 5 deletions gecode/driver/options.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ namespace Gecode {
else if (!strncmp("done",a,e)) { cur |= TE_DONE ; }
else if (!strncmp("propagate",a,e)) { cur |= TE_PROPAGATE; }
else if (!strncmp("commit",a,e)) { cur |= TE_COMMIT; }
else if (!strncmp("post",a,e)) { cur |= TE_POST; }
else if (!strncmp("none",a,e) ||
!strncmp("false",a,e) ||
!strncmp("0",a,e)) { cur = 0; }
Expand All @@ -381,14 +382,16 @@ namespace Gecode {
TE_FAIL |
TE_DONE |
TE_PROPAGATE |
TE_COMMIT); }
TE_COMMIT |
TE_POST); }
else if (!strncmp("variable",a,e)) { cur = (TE_INIT |
TE_PRUNE |
TE_FIX |
TE_FAIL |
TE_DONE); }
else if (!strncmp("general",a,e)) { cur = (TE_PROPAGATE |
TE_COMMIT); }
TE_COMMIT |
TE_POST); }
else {
std::cerr << "Wrong argument \"" << a
<< "\" for option \"" << iopt << "\""
Expand All @@ -409,16 +412,16 @@ namespace Gecode {
TraceOption::help(void) {
using namespace std;
cerr << '\t' << iopt
<< " (init,prune,fix,fail,done,propagate,commit,none,all,variable,general)"
<< " (init,prune,fix,fail,done,propagate,commit,post,none,all,variable,general)"
<< " default: ";
if (cur == 0) {
cerr << "none";
} else if (cur == (TE_INIT | TE_PRUNE | TE_FIX | TE_FAIL | TE_DONE |
TE_PROPAGATE | TE_COMMIT)) {
TE_PROPAGATE | TE_COMMIT | TE_POST)) {
cerr << "all";
} else if (cur == (TE_INIT | TE_PRUNE | TE_FIX | TE_FAIL | TE_DONE)) {
cerr << "variable";
} else if (cur == (TE_PROPAGATE | TE_COMMIT)) {
} else if (cur == (TE_PROPAGATE | TE_COMMIT | TE_POST)) {
cerr << "general";
} else {
int f = cur;
Expand Down Expand Up @@ -454,6 +457,11 @@ namespace Gecode {
}
if ((f & TE_COMMIT) != 0) {
cerr << "commit";
f -= TE_COMMIT;
if (f != 0) cerr << ',';
}
if ((f & TE_POST) != 0) {
cerr << "post";
}
}
cerr << endl << "\t\t" << exp << endl;
Expand Down
19 changes: 19 additions & 0 deletions gecode/kernel/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,25 @@ namespace Gecode {
return nullptr;
}

void
Space::post(const PostInfo& pi) {
assert(pc.p.bid_sc & sc_trace);
TraceRecorder* tr = findtracerecorder();
if ((tr != NULL) && (tr->events() & TE_POST)) {
GECODE_ASSUME(ssd.data().gpi.pid() >= pi.pid);
unsigned int n = ssd.data().gpi.pid() - pi.pid;
PostTraceInfo::Status s;
if (failed())
s = PostTraceInfo::FAILED;
else if (n == 0)
s = PostTraceInfo::SUBSUMED;
else
s = PostTraceInfo::POSTED;
PostTraceInfo pti(pi.pg,s,n);
tr->tracer()._post(*this,pti);
}
}

SpaceStatus
Space::status(StatusStatistics& stat) {
// Check whether space is failed
Expand Down
76 changes: 72 additions & 4 deletions gecode/kernel/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ namespace Gecode {
class ViewTraceInfo;
class PropagateTraceInfo;
class CommitTraceInfo;
class PostTraceInfo;
class TraceRecorder;
class TraceFilter;
class Tracer;
Expand Down Expand Up @@ -676,6 +677,7 @@ namespace Gecode {
friend class ViewTraceInfo;
friend class PropagateTraceInfo;
friend class CommitTraceInfo;
friend class PostTraceInfo;
protected:
/// Fake id for group of all actors
static const unsigned int GROUPID_ALL = 0U;
Expand Down Expand Up @@ -726,6 +728,7 @@ namespace Gecode {
friend class Propagator;
friend class ViewTraceInfo;
friend class PropagateTraceInfo;
friend class PostTraceInfo;
protected:
/// Initialize with group id \a gid
PropagatorGroup(unsigned int gid);
Expand Down Expand Up @@ -943,9 +946,14 @@ namespace Gecode {
* \brief Class to set group information when a post function is executed
*/
class PostInfo {
friend class Space;
protected:
/// The home space
Space& h;
/// The propagator group
PropagatorGroup pg;
/// Next free propagator id
unsigned int pid;
public:
/// Set information
PostInfo(Home home);
Expand Down Expand Up @@ -1016,6 +1024,37 @@ namespace Gecode {
unsigned int alternative(void) const;
};

/**
* \brief Post trace information
*/
class PostTraceInfo {
friend class Space;
friend class PostInfo;
public:
/// Post status
enum Status {
POSTED, ///< Propagator was posted
FAILED, ///< Posting failed
SUBSUMED ///< Propagator not posted as already subsumed
};
protected:
/// Propagator group
PropagatorGroup g;
/// Status
Status s;
/// Number of posted propagators
unsigned int n;
/// Initialize
PostTraceInfo(PropagatorGroup g, Status s, unsigned int n);
public:
/// Return post status
Status status(void) const;
/// Return propagator group
PropagatorGroup group(void) const;
/// Return number of posted propagators
unsigned int propagators(void) const;
};

/**
* \brief Base-class for propagators
* \ingroup TaskActor
Expand Down Expand Up @@ -1931,6 +1970,9 @@ namespace Gecode {
/// Find trace recorder if exists
GECODE_KERNEL_EXPORT
TraceRecorder* findtracerecorder(void);
/// Trace posting event
GECODE_KERNEL_EXPORT
void post(const PostInfo& pi);

/**
* \brief Notice that an actor must be disposed
Expand All @@ -1948,7 +1990,6 @@ namespace Gecode {
*/
GECODE_KERNEL_EXPORT
void ap_ignore_dispose(Actor* a, bool d);

public:
/**
* \brief Default constructor
Expand Down Expand Up @@ -3310,14 +3351,20 @@ namespace Gecode {
* Post information
*/
forceinline
PostInfo::PostInfo(Home home) : h(home) {
h.pc.p.vti.post(home.propagatorgroup());
PostInfo::PostInfo(Home home)
: h(home), pg(home.propagatorgroup()), pid(h.ssd.data().gpi.pid()) {
assert(!home.failed());
h.pc.p.vti.post(pg);
}

forceinline
PostInfo::~PostInfo(void) {
if (h.pc.p.bid_sc & Space::sc_trace)
h.post(*this);
h.pc.p.vti.other();
}


/*
* Propagate trace information
*
Expand Down Expand Up @@ -3374,6 +3421,27 @@ namespace Gecode {
}


/*
* Post trace information
*
*/
forceinline
PostTraceInfo::PostTraceInfo(PropagatorGroup g0, Status s0, unsigned int n0)
: g(g0), s(s0), n(n0) {}
forceinline PropagatorGroup
PostTraceInfo::group(void) const {
return g;
}
forceinline PostTraceInfo::Status
PostTraceInfo::status(void) const {
return s;
}
forceinline unsigned int
PostTraceInfo::propagators(void) const {
return n;
}


/*
* Propagator
*
Expand Down Expand Up @@ -3955,12 +4023,12 @@ namespace Gecode {

forceinline void
Space::fail(void) {
pc.p.active = &pc.p.queue[PropCost::AC_MAX+1]+1;
/*
* Now active points beyond the last queue. This is essential as
* enqueuing a propagator in a failed space keeps the space
* failed.
*/
pc.p.active = &pc.p.queue[PropCost::AC_MAX+1]+1;
}
forceinline void
Home::fail(void) {
Expand Down
17 changes: 14 additions & 3 deletions gecode/kernel/gpi.hpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace Gecode { namespace Kernel {
/// The inverse decay factor
double invd;
/// Next free propagator id
unsigned int pid;
unsigned int npid;
/// Whether to unshare
bool us;
/// The first block
Expand All @@ -92,6 +92,8 @@ namespace Gecode { namespace Kernel {
Info* allocate(unsigned int p, unsigned int gid);
/// Allocate new actor info
Info* allocate(unsigned int gid);
/// Return next free propagator id
unsigned int pid(void) const;
/// Provide access to unshare info and set to true
bool unshare(void);
/// Delete
Expand All @@ -118,7 +120,7 @@ namespace Gecode { namespace Kernel {

forceinline
GPI::GPI(void)
: b(&fst), invd(1.0), pid(0U), us(false) {}
: b(&fst), invd(1.0), npid(0U), us(false) {}

forceinline void
GPI::fail(Info& c) {
Expand All @@ -139,6 +141,15 @@ namespace Gecode { namespace Kernel {
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;
Expand Down Expand Up @@ -178,7 +189,7 @@ namespace Gecode { namespace Kernel {
n->next = b; b = n;
}
c = &b->info[--b->free];
c->init(pid++,gid);
c->init(npid++,gid);
m.release();
return c;
}
Expand Down
28 changes: 28 additions & 0 deletions gecode/kernel/trace/print.hpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,34 @@ namespace Gecode {
return os << s.str();
}

/**
* \brief Print post trace information
* \relates PostTraceInfo
*/
template<class Char, class Traits>
std::basic_ostream<Char,Traits>&
operator <<(std::basic_ostream<Char,Traits>& os,
const PostTraceInfo& pti) {
std::basic_ostringstream<Char,Traits> s;
s.copyfmt(os); s.width(0);
s << "post(";
if (pti.group().in())
s << "g:" << pti.group().id() << ",";
s << "s:";
switch (pti.status()) {
case PostTraceInfo::POSTED:
s << "posted(" << pti.propagators() << ")"; break;
case PostTraceInfo::FAILED:
s << "failed"; break;
case PostTraceInfo::SUBSUMED:
s << "subsumed"; break;
default:
GECODE_NEVER;
}
s << ')';
return os << s.str();
}

}

// STATISTICS: kernel-trace
5 changes: 3 additions & 2 deletions gecode/kernel/trace/recorder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ namespace Gecode {
//@}
/// \name Events for tracers
TE_PROPAGATE = 1 << 5, ///< Trace propagator executions
TE_COMMIT = 1 << 6 ///< Trace commit operations by branchers
TE_COMMIT = 1 << 6, ///< Trace commit operations by branchers
TE_POST = 1 << 7 ///< Trace propagator posting
};

/**
Expand Down Expand Up @@ -406,7 +407,7 @@ namespace Gecode {

forceinline ExecStatus
TraceRecorder::post(Home home, TraceFilter tf, int te, Tracer& t) {
if (te & (TE_PROPAGATE | TE_COMMIT))
if (te & (TE_PROPAGATE | TE_COMMIT | TE_POST))
(void) new (home) TraceRecorder(home,tf,te,t);
return ES_OK;
}
Expand Down
6 changes: 6 additions & 0 deletions gecode/kernel/trace/tracer.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ namespace Gecode {
os << std::endl;
}

void
StdTracer::post(const Space&,
const PostTraceInfo& pti) {
os << "trace::" << pti << std::endl;
}

StdTracer StdTracer::def;

}
Expand Down
Loading

0 comments on commit 8660eed

Please sign in to comment.