Skip to content

Commit 8660eed

Browse files
authored
Added post events for general tracers. (#40)
* Generate post events for general tracers * Also report number of posted propagators
1 parent a8e7241 commit 8660eed

File tree

9 files changed

+193
-14
lines changed

9 files changed

+193
-14
lines changed

changelog.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ Date: ?-?-?
6868
[DESCRIPTION]
6969
New stuff!
7070

71+
[ENTRY]
72+
Module: kernel
73+
What: new
74+
Rank: major
75+
[DESCRIPTION]
76+
General tracers now support post events, see MPG for details.
77+
7178
[ENTRY]
7279
Module: other
7380
What: change

gecode/driver/options.cpp

100644100755
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ namespace Gecode {
371371
else if (!strncmp("done",a,e)) { cur |= TE_DONE ; }
372372
else if (!strncmp("propagate",a,e)) { cur |= TE_PROPAGATE; }
373373
else if (!strncmp("commit",a,e)) { cur |= TE_COMMIT; }
374+
else if (!strncmp("post",a,e)) { cur |= TE_POST; }
374375
else if (!strncmp("none",a,e) ||
375376
!strncmp("false",a,e) ||
376377
!strncmp("0",a,e)) { cur = 0; }
@@ -381,14 +382,16 @@ namespace Gecode {
381382
TE_FAIL |
382383
TE_DONE |
383384
TE_PROPAGATE |
384-
TE_COMMIT); }
385+
TE_COMMIT |
386+
TE_POST); }
385387
else if (!strncmp("variable",a,e)) { cur = (TE_INIT |
386388
TE_PRUNE |
387389
TE_FIX |
388390
TE_FAIL |
389391
TE_DONE); }
390392
else if (!strncmp("general",a,e)) { cur = (TE_PROPAGATE |
391-
TE_COMMIT); }
393+
TE_COMMIT |
394+
TE_POST); }
392395
else {
393396
std::cerr << "Wrong argument \"" << a
394397
<< "\" for option \"" << iopt << "\""
@@ -409,16 +412,16 @@ namespace Gecode {
409412
TraceOption::help(void) {
410413
using namespace std;
411414
cerr << '\t' << iopt
412-
<< " (init,prune,fix,fail,done,propagate,commit,none,all,variable,general)"
415+
<< " (init,prune,fix,fail,done,propagate,commit,post,none,all,variable,general)"
413416
<< " default: ";
414417
if (cur == 0) {
415418
cerr << "none";
416419
} else if (cur == (TE_INIT | TE_PRUNE | TE_FIX | TE_FAIL | TE_DONE |
417-
TE_PROPAGATE | TE_COMMIT)) {
420+
TE_PROPAGATE | TE_COMMIT | TE_POST)) {
418421
cerr << "all";
419422
} else if (cur == (TE_INIT | TE_PRUNE | TE_FIX | TE_FAIL | TE_DONE)) {
420423
cerr << "variable";
421-
} else if (cur == (TE_PROPAGATE | TE_COMMIT)) {
424+
} else if (cur == (TE_PROPAGATE | TE_COMMIT | TE_POST)) {
422425
cerr << "general";
423426
} else {
424427
int f = cur;
@@ -454,6 +457,11 @@ namespace Gecode {
454457
}
455458
if ((f & TE_COMMIT) != 0) {
456459
cerr << "commit";
460+
f -= TE_COMMIT;
461+
if (f != 0) cerr << ',';
462+
}
463+
if ((f & TE_POST) != 0) {
464+
cerr << "post";
457465
}
458466
}
459467
cerr << endl << "\t\t" << exp << endl;

gecode/kernel/core.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,25 @@ namespace Gecode {
228228
return nullptr;
229229
}
230230

231+
void
232+
Space::post(const PostInfo& pi) {
233+
assert(pc.p.bid_sc & sc_trace);
234+
TraceRecorder* tr = findtracerecorder();
235+
if ((tr != NULL) && (tr->events() & TE_POST)) {
236+
GECODE_ASSUME(ssd.data().gpi.pid() >= pi.pid);
237+
unsigned int n = ssd.data().gpi.pid() - pi.pid;
238+
PostTraceInfo::Status s;
239+
if (failed())
240+
s = PostTraceInfo::FAILED;
241+
else if (n == 0)
242+
s = PostTraceInfo::SUBSUMED;
243+
else
244+
s = PostTraceInfo::POSTED;
245+
PostTraceInfo pti(pi.pg,s,n);
246+
tr->tracer()._post(*this,pti);
247+
}
248+
}
249+
231250
SpaceStatus
232251
Space::status(StatusStatistics& stat) {
233252
// Check whether space is failed

gecode/kernel/core.hpp

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ namespace Gecode {
147147
class ViewTraceInfo;
148148
class PropagateTraceInfo;
149149
class CommitTraceInfo;
150+
class PostTraceInfo;
150151
class TraceRecorder;
151152
class TraceFilter;
152153
class Tracer;
@@ -676,6 +677,7 @@ namespace Gecode {
676677
friend class ViewTraceInfo;
677678
friend class PropagateTraceInfo;
678679
friend class CommitTraceInfo;
680+
friend class PostTraceInfo;
679681
protected:
680682
/// Fake id for group of all actors
681683
static const unsigned int GROUPID_ALL = 0U;
@@ -726,6 +728,7 @@ namespace Gecode {
726728
friend class Propagator;
727729
friend class ViewTraceInfo;
728730
friend class PropagateTraceInfo;
731+
friend class PostTraceInfo;
729732
protected:
730733
/// Initialize with group id \a gid
731734
PropagatorGroup(unsigned int gid);
@@ -943,9 +946,14 @@ namespace Gecode {
943946
* \brief Class to set group information when a post function is executed
944947
*/
945948
class PostInfo {
949+
friend class Space;
946950
protected:
947951
/// The home space
948952
Space& h;
953+
/// The propagator group
954+
PropagatorGroup pg;
955+
/// Next free propagator id
956+
unsigned int pid;
949957
public:
950958
/// Set information
951959
PostInfo(Home home);
@@ -1016,6 +1024,37 @@ namespace Gecode {
10161024
unsigned int alternative(void) const;
10171025
};
10181026

1027+
/**
1028+
* \brief Post trace information
1029+
*/
1030+
class PostTraceInfo {
1031+
friend class Space;
1032+
friend class PostInfo;
1033+
public:
1034+
/// Post status
1035+
enum Status {
1036+
POSTED, ///< Propagator was posted
1037+
FAILED, ///< Posting failed
1038+
SUBSUMED ///< Propagator not posted as already subsumed
1039+
};
1040+
protected:
1041+
/// Propagator group
1042+
PropagatorGroup g;
1043+
/// Status
1044+
Status s;
1045+
/// Number of posted propagators
1046+
unsigned int n;
1047+
/// Initialize
1048+
PostTraceInfo(PropagatorGroup g, Status s, unsigned int n);
1049+
public:
1050+
/// Return post status
1051+
Status status(void) const;
1052+
/// Return propagator group
1053+
PropagatorGroup group(void) const;
1054+
/// Return number of posted propagators
1055+
unsigned int propagators(void) const;
1056+
};
1057+
10191058
/**
10201059
* \brief Base-class for propagators
10211060
* \ingroup TaskActor
@@ -1931,6 +1970,9 @@ namespace Gecode {
19311970
/// Find trace recorder if exists
19321971
GECODE_KERNEL_EXPORT
19331972
TraceRecorder* findtracerecorder(void);
1973+
/// Trace posting event
1974+
GECODE_KERNEL_EXPORT
1975+
void post(const PostInfo& pi);
19341976

19351977
/**
19361978
* \brief Notice that an actor must be disposed
@@ -1948,7 +1990,6 @@ namespace Gecode {
19481990
*/
19491991
GECODE_KERNEL_EXPORT
19501992
void ap_ignore_dispose(Actor* a, bool d);
1951-
19521993
public:
19531994
/**
19541995
* \brief Default constructor
@@ -3310,14 +3351,20 @@ namespace Gecode {
33103351
* Post information
33113352
*/
33123353
forceinline
3313-
PostInfo::PostInfo(Home home) : h(home) {
3314-
h.pc.p.vti.post(home.propagatorgroup());
3354+
PostInfo::PostInfo(Home home)
3355+
: h(home), pg(home.propagatorgroup()), pid(h.ssd.data().gpi.pid()) {
3356+
assert(!home.failed());
3357+
h.pc.p.vti.post(pg);
33153358
}
3359+
33163360
forceinline
33173361
PostInfo::~PostInfo(void) {
3362+
if (h.pc.p.bid_sc & Space::sc_trace)
3363+
h.post(*this);
33183364
h.pc.p.vti.other();
33193365
}
33203366

3367+
33213368
/*
33223369
* Propagate trace information
33233370
*
@@ -3374,6 +3421,27 @@ namespace Gecode {
33743421
}
33753422

33763423

3424+
/*
3425+
* Post trace information
3426+
*
3427+
*/
3428+
forceinline
3429+
PostTraceInfo::PostTraceInfo(PropagatorGroup g0, Status s0, unsigned int n0)
3430+
: g(g0), s(s0), n(n0) {}
3431+
forceinline PropagatorGroup
3432+
PostTraceInfo::group(void) const {
3433+
return g;
3434+
}
3435+
forceinline PostTraceInfo::Status
3436+
PostTraceInfo::status(void) const {
3437+
return s;
3438+
}
3439+
forceinline unsigned int
3440+
PostTraceInfo::propagators(void) const {
3441+
return n;
3442+
}
3443+
3444+
33773445
/*
33783446
* Propagator
33793447
*
@@ -3955,12 +4023,12 @@ namespace Gecode {
39554023

39564024
forceinline void
39574025
Space::fail(void) {
4026+
pc.p.active = &pc.p.queue[PropCost::AC_MAX+1]+1;
39584027
/*
39594028
* Now active points beyond the last queue. This is essential as
39604029
* enqueuing a propagator in a failed space keeps the space
39614030
* failed.
39624031
*/
3963-
pc.p.active = &pc.p.queue[PropCost::AC_MAX+1]+1;
39644032
}
39654033
forceinline void
39664034
Home::fail(void) {

gecode/kernel/gpi.hpp

100644100755
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ namespace Gecode { namespace Kernel {
7272
/// The inverse decay factor
7373
double invd;
7474
/// Next free propagator id
75-
unsigned int pid;
75+
unsigned int npid;
7676
/// Whether to unshare
7777
bool us;
7878
/// The first block
@@ -92,6 +92,8 @@ namespace Gecode { namespace Kernel {
9292
Info* allocate(unsigned int p, unsigned int gid);
9393
/// Allocate new actor info
9494
Info* allocate(unsigned int gid);
95+
/// Return next free propagator id
96+
unsigned int pid(void) const;
9597
/// Provide access to unshare info and set to true
9698
bool unshare(void);
9799
/// Delete
@@ -118,7 +120,7 @@ namespace Gecode { namespace Kernel {
118120

119121
forceinline
120122
GPI::GPI(void)
121-
: b(&fst), invd(1.0), pid(0U), us(false) {}
123+
: b(&fst), invd(1.0), npid(0U), us(false) {}
122124

123125
forceinline void
124126
GPI::fail(Info& c) {
@@ -139,6 +141,15 @@ namespace Gecode { namespace Kernel {
139141
return d;
140142
}
141143

144+
forceinline unsigned int
145+
GPI::pid(void) const {
146+
unsigned int p;
147+
const_cast<GPI&>(*this).m.acquire();
148+
p = npid;
149+
const_cast<GPI&>(*this).m.release();
150+
return p;
151+
}
152+
142153
forceinline bool
143154
GPI::unshare(void) {
144155
bool u;
@@ -178,7 +189,7 @@ namespace Gecode { namespace Kernel {
178189
n->next = b; b = n;
179190
}
180191
c = &b->info[--b->free];
181-
c->init(pid++,gid);
192+
c->init(npid++,gid);
182193
m.release();
183194
return c;
184195
}

gecode/kernel/trace/print.hpp

100644100755
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,34 @@ namespace Gecode {
121121
return os << s.str();
122122
}
123123

124+
/**
125+
* \brief Print post trace information
126+
* \relates PostTraceInfo
127+
*/
128+
template<class Char, class Traits>
129+
std::basic_ostream<Char,Traits>&
130+
operator <<(std::basic_ostream<Char,Traits>& os,
131+
const PostTraceInfo& pti) {
132+
std::basic_ostringstream<Char,Traits> s;
133+
s.copyfmt(os); s.width(0);
134+
s << "post(";
135+
if (pti.group().in())
136+
s << "g:" << pti.group().id() << ",";
137+
s << "s:";
138+
switch (pti.status()) {
139+
case PostTraceInfo::POSTED:
140+
s << "posted(" << pti.propagators() << ")"; break;
141+
case PostTraceInfo::FAILED:
142+
s << "failed"; break;
143+
case PostTraceInfo::SUBSUMED:
144+
s << "subsumed"; break;
145+
default:
146+
GECODE_NEVER;
147+
}
148+
s << ')';
149+
return os << s.str();
150+
}
151+
124152
}
125153

126154
// STATISTICS: kernel-trace

gecode/kernel/trace/recorder.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ namespace Gecode {
4848
//@}
4949
/// \name Events for tracers
5050
TE_PROPAGATE = 1 << 5, ///< Trace propagator executions
51-
TE_COMMIT = 1 << 6 ///< Trace commit operations by branchers
51+
TE_COMMIT = 1 << 6, ///< Trace commit operations by branchers
52+
TE_POST = 1 << 7 ///< Trace propagator posting
5253
};
5354

5455
/**
@@ -406,7 +407,7 @@ namespace Gecode {
406407

407408
forceinline ExecStatus
408409
TraceRecorder::post(Home home, TraceFilter tf, int te, Tracer& t) {
409-
if (te & (TE_PROPAGATE | TE_COMMIT))
410+
if (te & (TE_PROPAGATE | TE_COMMIT | TE_POST))
410411
(void) new (home) TraceRecorder(home,tf,te,t);
411412
return ES_OK;
412413
}

gecode/kernel/trace/tracer.cpp

100644100755
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ namespace Gecode {
5555
os << std::endl;
5656
}
5757

58+
void
59+
StdTracer::post(const Space&,
60+
const PostTraceInfo& pti) {
61+
os << "trace::" << pti << std::endl;
62+
}
63+
5864
StdTracer StdTracer::def;
5965

6066
}

0 commit comments

Comments
 (0)