this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.org>
5 *
6 * Copyright:
7 * Christian Schulte, 2016
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34namespace Gecode {
35
36 /// Class to provide synchronization
37 class TracerBase : public HeapAllocated {
38 protected:
39 /// Mutex to provide synchronization
40 GECODE_KERNEL_EXPORT
41 static Support::Mutex m;
42 };
43
44 template<class View> class ViewTraceRecorder;
45
46 /**
47 * \brief Tracer that process view trace information
48 * \ingroup TaskTrace
49 */
50 template<class View>
51 class ViewTracer : public TracerBase {
52 template<class ViewForTraceRecorder> friend class ViewTraceRecorder;
53 private:
54 /**
55 * \brief Init function synchronization
56 *
57 * Just calls the actual init function protected by a mutex.
58 *
59 */
60 void _init(const Space& home, const ViewTraceRecorder<View>& t);
61 /**
62 * \brief Prune function synchronization
63 *
64 * Just calls the actual prune function protected by a mutex.
65 *
66 */
67 void _prune(const Space& home, const ViewTraceRecorder<View>& t,
68 const ViewTraceInfo& vti,
69 int i, typename TraceTraits<View>::TraceDelta& d);
70 /**
71 * \brief Fail function synchronization
72 *
73 * Just calls the actual fail function protected by a mutex.
74 *
75 */
76 void _fail(const Space& home, const ViewTraceRecorder<View>& t);
77 /**
78 * \brief Fixpoint function synchronization
79 *
80 * Just calls the actual fixpoint function protected by a mutex.
81 */
82 void _fix(const Space& home, const ViewTraceRecorder<View>& t);
83 /**
84 * \brief Done function synchronization
85 *
86 * Just calls the actual done function protected by a mutex.
87 */
88 void _done(const Space& home, const ViewTraceRecorder<View>& t);
89 public:
90 /// Constructor
91 ViewTracer(void);
92 /**
93 * \brief Init function
94 *
95 * The init function is called when the trace collector has
96 * been initialized.
97 */
98 virtual void init(const Space& home,
99 const ViewTraceRecorder<View>& t) = 0;
100 /**
101 * \brief Prune function
102 *
103 * The variable at position \a i has been modified where
104 * the modification is described by \a d. Additional
105 * information about how the variable has been changed is
106 * provided by the trace collector \a t and execution
107 * information \a ei.
108 */
109 virtual void prune(const Space& home,
110 const ViewTraceRecorder<View>& t,
111 const ViewTraceInfo& vti,
112 int i, typename TraceTraits<View>::TraceDelta& d) = 0;
113 /**
114 * \brief Fail function
115 *
116 * The fail function is called whenever \a home containing the
117 * trace collector \a t has been failed.
118 */
119 virtual void fail(const Space& home,
120 const ViewTraceRecorder<View>& t) = 0;
121 /**
122 * \brief Fixpoint function
123 *
124 * The fixpoint function is called whenever \a home containing
125 * the trace collector \a t reaches a fixpoint (and fixpoint
126 * tracing is enabled).
127 */
128 virtual void fix(const Space& home,
129 const ViewTraceRecorder<View>& t) = 0;
130 /**
131 * \brief Done function
132 *
133 * The done function is called whenever the trace collector \a t
134 * is done and will terminate.
135 */
136 virtual void done(const Space& home,
137 const ViewTraceRecorder<View>& t) = 0;
138 /// Destructor
139 virtual ~ViewTracer(void);
140 };
141
142
143
144
145 /**
146 * \brief Tracer
147 * \ingroup TaskTrace
148 */
149 class Tracer : public TracerBase {
150 friend class Space;
151 friend class PostInfo;
152 private:
153 /**
154 * \brief Propagate function synchronization
155 *
156 * Just calls the actual propagate function protected by a mutex.
157 *
158 */
159 void _propagate(const Space& home, const PropagateTraceInfo& pti);
160 /**
161 * \brief Commit function synchronization
162 *
163 * Just calls the actual commit function protected by a mutex.
164 *
165 */
166 void _commit(const Space& home, const CommitTraceInfo& cti);
167 /**
168 * \brief Post function synchronization
169 *
170 * Just calls the actual post function protected by a mutex.
171 *
172 */
173 void _post(const Space& home, const PostTraceInfo& pti);
174 public:
175 /// Constructor
176 Tracer(void);
177 /**
178 * \brief Propagate function
179 *
180 * The propagate function is called when a propagator has been
181 * executed.
182 */
183 virtual void propagate(const Space& home,
184 const PropagateTraceInfo& pti) = 0;
185 /**
186 * \brief Commit function
187 *
188 * The commit function is called when a brancher has executed
189 * a commit operation.
190 */
191 virtual void commit(const Space& home,
192 const CommitTraceInfo& cti) = 0;
193 /**
194 * \brief Post function
195 *
196 * The post function is called when an attempt to post a propagator
197 * has been executed.
198 */
199 virtual void post(const Space& home,
200 const PostTraceInfo& pti) = 0;
201 /// Destructor
202 virtual ~Tracer(void);
203 };
204
205
206 /**
207 * \brief Default tracer
208 * \ingroup TaskTrace
209 */
210 class GECODE_KERNEL_EXPORT StdTracer : public Tracer {
211 protected:
212 /// Output stream to use
213 std::ostream& os;
214 public:
215 /// Initialize with output stream \a os
216 StdTracer(std::ostream& os = std::cerr);
217 /**
218 * \brief Propagate function
219 *
220 * The propagate function is called when a propagator has been
221 * executed.
222 */
223 virtual void propagate(const Space& home,
224 const PropagateTraceInfo& pti);
225 /**
226 * \brief Commit function
227 *
228 * The commit function is called when a brancher has executed
229 * a commit operation.
230 */
231 virtual void commit(const Space& home,
232 const CommitTraceInfo& cti);
233 /**
234 * \brief Post function
235 *
236 * The post function is called when an attempt to post a propagator
237 * has been executed.
238 */
239 virtual void post(const Space& home,
240 const PostTraceInfo& pti);
241 /// Default tracer (printing to std::cerr)
242 static StdTracer def;
243 };
244
245
246 /*
247 * View tracer
248 */
249
250 template<class View>
251 forceinline
252 ViewTracer<View>::ViewTracer(void) {
253 }
254
255 template<class View>
256 forceinline void
257 ViewTracer<View>::_init(const Space& home,
258 const ViewTraceRecorder<View>& t) {
259 Support::Lock l(m);
260 init(home,t);
261 }
262 template<class View>
263 forceinline void
264 ViewTracer<View>::_prune(const Space& home,
265 const ViewTraceRecorder<View>& t,
266 const ViewTraceInfo& vti,
267 int i, typename TraceTraits<View>::TraceDelta& d) {
268 Support::Lock l(m);
269 prune(home,t,vti,i,d);
270 }
271 template<class View>
272 forceinline void
273 ViewTracer<View>::_fail(const Space& home,
274 const ViewTraceRecorder<View>& t) {
275 Support::Lock l(m);
276 fail(home,t);
277 }
278 template<class View>
279 forceinline void
280 ViewTracer<View>::_fix(const Space& home,
281 const ViewTraceRecorder<View>& t) {
282 Support::Lock l(m);
283 fix(home,t);
284 }
285 template<class View>
286 forceinline void
287 ViewTracer<View>::_done(const Space& home,
288 const ViewTraceRecorder<View>& t) {
289 Support::Lock l(m);
290 done(home,t);
291 }
292
293 template<class View>
294 forceinline
295 ViewTracer<View>::~ViewTracer(void) {
296 }
297
298
299 /*
300 * Tracer
301 */
302
303 forceinline
304 Tracer::Tracer(void) {
305 }
306
307 forceinline void
308 Tracer::_propagate(const Space& home,
309 const PropagateTraceInfo& pti) {
310 Support::Lock l(m);
311 propagate(home,pti);
312 }
313 forceinline void
314 Tracer::_commit(const Space& home,
315 const CommitTraceInfo& cti) {
316 Support::Lock l(m);
317 commit(home,cti);
318 }
319 forceinline void
320 Tracer::_post(const Space& home,
321 const PostTraceInfo& pti) {
322 Support::Lock l(m);
323 post(home,pti);
324 }
325
326 forceinline
327 Tracer::~Tracer(void) {
328 }
329
330}
331
332// STATISTICS: kernel-trace