File: recorder.hpp

package info (click to toggle)
gecode-snapshot 6.2.0%2Bgit20240207-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 35,308 kB
  • sloc: cpp: 475,516; perl: 2,077; makefile: 1,816; sh: 198
file content (422 lines) | stat: -rwxr-xr-x 12,424 bytes parent folder | download | duplicates (3)
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
 *  Main authors:
 *     Christian Schulte <schulte@gecode.org>
 *
 *  Copyright:
 *     Christian Schulte, 2016
 *
 *  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.
 *
 */

namespace Gecode {

  /**
   * \brief Which events to trace
   * \ingroup TaskTrace
   */
  enum TraceEvent {
    /// \name Events for view tracers
    //@{
    TE_INIT  = 1 << 0, ///< Trace init events
    TE_PRUNE = 1 << 1, ///< Trace prune events
    TE_FIX   = 1 << 2, ///< Trace fixpoint events
    TE_FAIL  = 1 << 3, ///< Trace fail events
    TE_DONE  = 1 << 4, ///< Trace done events
    //@}
    /// \name Events for general tracers
    TE_PROPAGATE = 1 << 5, ///< Trace propagator executions
    TE_COMMIT    = 1 << 6, ///< Trace commit operations by branchers
    TE_POST      = 1 << 7  ///< Trace propagator posting
  };

  /**
   * \brief Propagator for recording view trace information
   * \ingroup TaskTrace
   */
  template<class View>
  class ViewTraceRecorder : public Propagator {
  public:
    /// The corresponding duplicate view type
    typedef typename TraceTraits<View>::TraceView TraceView;
    /// The corresponding trace delta type
    typedef typename TraceTraits<View>::TraceDelta TraceDelta;
    /// The corresponding slack value type
    typedef typename TraceTraits<View>::SlackValue SlackValue;
    /// Collection of slack values
    class Slack {
      template<class ViewForTraceRecorder> friend class ViewTraceRecorder;
    protected:
      /// The initial slack value
      SlackValue i;
      /// Slack value at previous event (fixpoint or init)
      SlackValue p;
      /// Current slack value
      SlackValue c;
    public:
      /// Return initial slack value
      SlackValue initial(void) const;
      /// Return previous slack value
      SlackValue previous(void) const;
      /// Return current slack value
      SlackValue current(void) const;
    };
  protected:
    /// Advisor with index information
    class Idx : public Advisor {
    protected:
      /// Index information
      int _idx;
    public:
      /// Constructor for creation
      Idx(Space& home, Propagator& p, Council<Idx>& c, int i);
      /// Constructor for cloning \a a
      Idx(Space& home, Idx& a);
      /// Get index of view
      int idx(void) const;
    };
    /// Duplicate views (old information)
    ViewArray<TraceView> o;
    /// Original views (new information)
    ViewArray<View> n;
    /// The advisor council
    Council<Idx> c;
    /// The trace filter
    TraceFilter tf;
    /// Which events to trace
    int te;
    /// The actual tracer
    ViewTracer<View>& t;
    /// Slack information
    Slack s;
    /// Constructor for cloning \a p
    ViewTraceRecorder(Space& home, ViewTraceRecorder& p);
  public:
    /// Constructor for creation
    ViewTraceRecorder(Home home, ViewArray<View>& x,
                      TraceFilter tf, int te, ViewTracer<View>& t);
    /// Copy propagator during cloning
    virtual Propagator* copy(Space& home);
    /// Cost function (record so that propagator runs last)
    virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
    /// Schedule function
    virtual void reschedule(Space& home);
    /// Give advice to propagator
    virtual ExecStatus advise(Space& home, Advisor& a, const Delta& d);
    /// Perform propagation
    virtual ExecStatus propagate(Space& home, const ModEventDelta& med);
    /// Delete propagator and return its size
    virtual size_t dispose(Space& home);
    /// Post recorder propagator
    static ExecStatus post(Home home, ViewArray<View>& x,
                           TraceFilter tf, int te, ViewTracer<View>& t);
    /// \name Trace information
    //@{
    /// Return variable being traced at position \a i
    const typename View::VarType operator [](int i) const;
    /// Return number of variables being traced
    int size(void) const;
    /// Provide access to slack information
    const Slack& slack(void) const;
    //@}
  };

  /**
   * \brief Propagator for recording trace information
   *
   * This propagator is actually never run. It only provides access
   * to information needed for tracing.
   *
   * \ingroup TaskTrace
   */
  class TraceRecorder : public Propagator {
  public:
    /// The trace filter
    TraceFilter tf;
    /// Which events to trace
    int te;
    /// The actual tracer
    Tracer& t;
    /// Constructor for cloning \a p
    TraceRecorder(Space& home, TraceRecorder& p);
  public:
    /// Constructor for creation
    TraceRecorder(Home home, TraceFilter tf, int te, Tracer& t);
    /// Copy propagator during cloning
    virtual Propagator* copy(Space& home);
    /// Cost function (record so that propagator runs last)
    virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
    /// Schedule function
    virtual void reschedule(Space& home);
    /// Perform propagation
    virtual ExecStatus propagate(Space& home, const ModEventDelta& med);
    /// Delete propagator and return its size
    virtual size_t dispose(Space& home);
    /// Post propagator
    static ExecStatus post(Home home, TraceFilter tf, int te, Tracer& t);
    /// \name Trace information
    //@{
    /// Return trace filter
    const TraceFilter& filter(void) const;
    /// Which events to trace
    int events(void) const;
    /// Return tracer
    Tracer& tracer(void) const;
    //@}
  };


  /*
   * Functions for trace support
   *
   */
  template<class View>
  forceinline const typename View::VarType
  ViewTraceRecorder<View>::operator [](int i) const {
    const typename View::VarType x(n[i].varimp());
    return x;
  }
  template<class View>
  forceinline int
  ViewTraceRecorder<View>::size(void) const {
    return n.size();
  }
  template<class View>
  forceinline const typename ViewTraceRecorder<View>::Slack&
  ViewTraceRecorder<View>::slack(void) const {
    return s;
  }


  /*
   * Functions for access to slack
   *
   */
  template<class View>
  forceinline typename ViewTraceRecorder<View>::SlackValue
  ViewTraceRecorder<View>::Slack::initial(void) const {
    return i;
  }
  template<class View>
  forceinline typename ViewTraceRecorder<View>::SlackValue
  ViewTraceRecorder<View>::Slack::previous(void) const {
    return p;
  }
  template<class View>
  forceinline typename ViewTraceRecorder<View>::SlackValue
  ViewTraceRecorder<View>::Slack::current(void) const {
    return c;
  }


  /*
   * Advisor for tracer
   *
   */

  template<class View>
  forceinline
  ViewTraceRecorder<View>::Idx::Idx(Space& home, Propagator& p,
                                    Council<Idx>& c, int i)
    : Advisor(home,p,c), _idx(i) {}
  template<class View>
  forceinline
  ViewTraceRecorder<View>::Idx::Idx(Space& home, Idx& a)
    : Advisor(home,a), _idx(a._idx) {
  }
  template<class View>
  forceinline int
  ViewTraceRecorder<View>::Idx::idx(void) const {
    return _idx;
  }


  /*
   * Posting of tracer propagator
   *
   */
  template<class View>
  forceinline
  ViewTraceRecorder<View>::ViewTraceRecorder(Home home, ViewArray<View>& x,
                                             TraceFilter tf0, int te0,
                                             ViewTracer<View>& t0)
    : Propagator(home), o(home,x.size()), n(x), c(home),
      tf(tf0), te(te0), t(t0) {
    home.notice(*this, AP_VIEW_TRACE);
    home.notice(*this, AP_DISPOSE);
    for (int i=0; i<n.size(); i++) {
      o[i] = TraceView(home,n[i]);
      if (!n[i].assigned())
        n[i].subscribe(home,*new (home) Idx(home,*this,c,i));
    }
    View::schedule(home,*this,ME_GEN_ASSIGNED);
    s.i = TraceView::slack(n[0]);
    for (int i=1; i<n.size(); i++)
      s.i += TraceView::slack(n[i]);
    s.p = s.i;
    if ((te & TE_INIT) != 0)
      t._init(home,*this);
  }


  template<class View>
  forceinline ExecStatus
  ViewTraceRecorder<View>::post(Home home, ViewArray<View>& x,
                                TraceFilter tf, int te, ViewTracer<View>& t) {
    if ((x.size() > 0) &&
        (te & (TE_INIT | TE_PRUNE | TE_FIX | TE_FAIL | TE_DONE)))
      (void) new (home) ViewTraceRecorder(home,x,tf,te,t);
    return ES_OK;
  }


  /*
   * Propagation for trace recorder
   *
   */
  template<class View>
  forceinline
  ViewTraceRecorder<View>::ViewTraceRecorder(Space& home, ViewTraceRecorder& p)
    : Propagator(home,p), tf(p.tf), te(p.te), t(p.t), s(p.s) {
    o.update(home, p.o);
    n.update(home, p.n);
    c.update(home, p.c);
  }

  template<class View>
  Propagator*
  ViewTraceRecorder<View>::copy(Space& home) {
    return new (home) ViewTraceRecorder(home, *this);
  }

  template<class View>
  inline size_t
  ViewTraceRecorder<View>::dispose(Space& home) {
    home.ignore(*this, AP_VIEW_TRACE);
    home.ignore(*this, AP_DISPOSE);
    tf.~TraceFilter();
    // Cancel remaining advisors
    for (Advisors<Idx> as(c); as(); ++as)
      n[as.advisor().idx()].cancel(home,as.advisor());
    c.dispose(home);
    (void) Propagator::dispose(home);
    return sizeof(*this);
  }

  template<class View>
  PropCost
  ViewTraceRecorder<View>::cost(const Space&, const ModEventDelta&) const {
    return PropCost::record();
  }

  template<class View>
  void
  ViewTraceRecorder<View>::reschedule(Space& home) {
    View::schedule(home,*this,ME_GEN_ASSIGNED);
  }

  template<class View>
  ExecStatus
  ViewTraceRecorder<View>::advise(Space& home, Advisor& _a, const Delta& d) {
    Idx& a = static_cast<Idx&>(_a);
    int i = a.idx();
    if (((te & TE_PRUNE) != 0) && !disabled() && tf(a(home)) ) {
      TraceDelta td(o[i],n[i],d);
      t._prune(home,*this,a(home),i,td);
    }
    o[i].prune(home,n[i],d);
    if (n[a.idx()].assigned())
      a.dispose(home,c);
    return ES_NOFIX;
  }

  template<class View>
  ExecStatus
  ViewTraceRecorder<View>::propagate(Space& home, const ModEventDelta&) {
    s.c = TraceView::slack(n[0]);
    for (int i=1; i<n.size(); i++)
      s.c += TraceView::slack(n[i]);
    if (home.failed() && ((te & TE_FAIL) != 0) && !disabled()) {
      t._fail(home,*this);
      return ES_FIX;
    }
    if ((te & TE_FIX) != 0)
      t._fix(home,*this);
    s.p = s.c;
    if (c.empty()) {
      if ((te & TE_DONE) != 0)
        t._done(home,*this);
      return home.ES_SUBSUMED(*this);
    }
    return ES_FIX;
  }



  /*
   * Functions for trace support
   *
   */
  forceinline const TraceFilter&
  TraceRecorder::filter(void) const {
    return tf;
  }
  forceinline int
  TraceRecorder::events(void) const {
    return te;
  }
  forceinline Tracer&
  TraceRecorder::tracer(void) const {
    return t;
  }


  /*
   * Trace recorder propagator
   *
   */
  forceinline
  TraceRecorder::TraceRecorder(Home home, TraceFilter tf0, int te0,
                               Tracer& t0)
    : Propagator(home), tf(tf0), te(te0), t(t0) {
    home.notice(*this, AP_DISPOSE);
    home.notice(*this, AP_TRACE);
  }

  forceinline ExecStatus
  TraceRecorder::post(Home home, TraceFilter tf, int te, Tracer& t) {
    if (te & (TE_PROPAGATE | TE_COMMIT | TE_POST))
      (void) new (home) TraceRecorder(home,tf,te,t);
    return ES_OK;
  }

  forceinline
  TraceRecorder::TraceRecorder(Space& home, TraceRecorder& p)
    : Propagator(home,p), tf(p.tf), te(p.te), t(p.t) {
  }

}

// STATISTICS: kernel-trace