1 /* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #ifndef SIMGRID_MC_ODPOR_EXECUTION_HPP
7 #define SIMGRID_MC_ODPOR_EXECUTION_HPP
9 #include "src/mc/api/ClockVector.hpp"
10 #include "src/mc/explo/odpor/odpor_forward.hpp"
11 #include "src/mc/mc_forward.hpp"
12 #include "src/mc/mc_record.hpp"
13 #include "src/mc/transition/Transition.hpp"
17 #include <unordered_set>
20 namespace simgrid::mc::odpor {
22 std::vector<std::string> get_textual_trace(const PartialExecution& w);
25 * @brief The occurrence of a transition in an execution
27 * An execution is set of *events*, where each element represents
28 * the occurrence or execution of the `i`th step of a particular
32 std::pair<std::shared_ptr<Transition>, ClockVector> contents_;
36 Event(Event&&) = default;
37 Event(const Event&) = default;
38 Event& operator=(const Event&) = default;
39 explicit Event(std::pair<std::shared_ptr<Transition>, ClockVector> pair) : contents_(std::move(pair)) {}
41 std::shared_ptr<Transition> get_transition() const { return std::get<0>(contents_); }
42 const ClockVector& get_clock_vector() const { return std::get<1>(contents_); }
46 * @brief An ordered sequence of transitions which describe
47 * the evolution of a process undergoing model checking
49 * An execution conceptually is just a string of actors
50 * ids (e.g. "1.2.3.1.2.2.1.1"), where the `i`th occurrence
51 * of actor id `j` corresponds to the `i`th action executed
52 * by the actor with id `j` (viz. the `i`th step of actor `j`).
53 * Executions can stand alone on their own or can extend
54 * the execution of other sequences
56 * Executions are conceived based on the following papers:
57 * 1. "Source Sets: A Foundation for Optimal Dynamic Partial Order Reduction"
60 * In addition to representing an actual steps taken,
61 * an execution keeps track of the "happens-before"
62 * relation among the transitions in the execution
63 * by following the procedure outlined in section 4 of the
64 * original DPOR paper with clock vectors.
65 * As new transitions are added to the execution, clock vectors are
66 * computed as appropriate and associated with the corresponding position
67 * in the execution. This allows us to determine “happens-before” in
68 * constant-time between points in the execution (called events
69 * [which is unfortunately the same name used in UDPOR for a slightly
70 * different concept]), albeit for an up-front cost of traversing the
71 * execution stack. The happens-before relation is important in many
72 * places in SDPOR and ODPOR.
74 * @note: For more nuanced happens-before relations, clock
75 * vectors may not always suffice. Clock vectors work
76 * well with transition-based dependencies like that used in
77 * SimGrid; but to have a more refined independence relation,
78 * an event-based dependency approach is needed. See the section 2
79 * in the ODPOR paper [1] concerning event-based dependencies and
80 * how the happens-before relation can be refined in a
81 * computation model much like that of SimGrid. In fact, the same issue
82 * arrises with UDPOR with context-sensitive dependencies:
83 * the two concepts are analogous if not identical
87 std::vector<Event> contents_;
88 Execution(std::vector<Event>&& contents) : contents_(std::move(contents)) {}
91 using EventHandle = uint32_t;
93 Execution() = default;
94 Execution(const Execution&) = default;
95 Execution& operator=(Execution const&) = default;
96 Execution(Execution&&) = default;
97 Execution(const PartialExecution&);
99 std::vector<std::string> get_textual_trace() const;
101 size_t size() const { return this->contents_.size(); }
102 bool empty() const { return this->contents_.empty(); }
103 auto begin() const { return this->contents_.begin(); }
104 auto end() const { return this->contents_.end(); }
107 * @brief Computes the "core" portion the SDPOR algorithm,
108 * viz. the intersection of the backtracking set and the
109 * set of initials with respect to the *last* event added
112 * The "core" portion of the SDPOR algorithm is found on
113 * lines 6-9 of the pseudocode:
115 * 6 | let E' := pre(E, e)
116 * 7 | let v := notdep(e, E).p
117 * 8 | if I_[E'](v) ∩ backtrack(E') = empty then
118 * 9 | --> add some q in I_[E'](v) to backtrack(E')
120 * This method computes all of the lines simultaneously,
121 * returning the set `I_[E'](v)` if condition on line 8 holds.
122 * The event `e` and the set `backtrack(E')` are the provided
123 * arguments to the method.
125 * @param e the event with respect to which to determine
126 * whether a backtrack point needs to be added for the
127 * prefix corresponding to the execution prior to `e`
129 * @param backtrack_set The set of actors which should
130 * not be considered for selection as an SDPOR initial.
131 * While this set need not necessarily correspond to the
132 * backtrack set `backtrack(E')`, doing so provides what
133 * is expected for SDPOR
135 * See the SDPOR algorithm pseudocode in [1] for more
136 * details for the context of the function.
138 * @precondition: This method assumes that events `e` and
139 * `e' := get_latest_event_handle()` are in a *reversible* race,
140 * as is explicitly the case in SDPOR
142 * @returns a set of actors not already contained in `backtrack_set`
143 * which serve as an initials to reverse the race between `e`
144 * and `e' := get_latest_event_handle()`; that is, an initial that is
145 * not already contained in the set `backtrack_set`.
147 std::unordered_set<aid_t> get_missing_source_set_actors_from(EventHandle e,
148 const std::unordered_set<aid_t>& backtrack_set) const;
151 * @brief Computes the analogous lines from the SDPOR algorithm
152 * in the ODPOR algorithm, viz. the intersection of the sleep set
153 * and the set of weak initials with respect to the given pair
156 * This method computes lines 4-6 of the ODPOR pseudocode, viz.:
158 * 4 | let E' := pre(E, e)
159 * 5 | let v := notdep(e, E).e'^
160 * 6 | if sleep(E') ∩ WI_[E'](v) = empty then
161 * 7 | --> wut(E') := insert_[E'](v, wut(E'))
163 * The sequence `v` is computed and returned as needed, based on whether
164 * the check on line 6 passes.
166 * @precondition: This method assumes that events `e` and
167 * `e_prime` are in a *reversible* race, as is the case
170 * @returns a partial execution `v := notdep(e, E)` (where `E` refers
171 * to this execution) that should be inserted into a wakeup tree with
172 * respect to this execution if `sleep(E') ∩ WI_[E'](v) = empty`, and
173 * `std::nullopt` otherwise
175 std::optional<PartialExecution> get_odpor_extension_from(EventHandle e, EventHandle e_prime,
176 const State& state_at_e) const;
179 * @brief For a given sequence of actors `v` and a sequence of transitions `w`,
180 * computes the sequence, if any, that should be inserted as a child in wakeup tree for
183 * Recall that the procedure for implementing the insertion
184 * is outlined in section 6.2 of Abdulla et al. 2017 as follows:
186 * | Let `v` be the smallest (w.r.t to "<") sequence in [the tree] B
187 * | such that `v ~_[E] w`. If `v` is a leaf node, the tree can be left
190 * | Otherwise let `w'` be the shortest sequence such that `w [=_[E] v.w'`
191 * | and add `v.w'` as a new leaf, ordered after all already existing nodes
192 * | of the form `v.w''`
194 * The procedure for determining whether `v ~_[E] w` is given as Lemma 4.6 of
195 * Abdulla et al. 2017:
197 * | The relation `v ~_[E] w` holds if either
199 * | (2) v := p.v' and either
200 * | (a) p in I_[E](w) and `v' ~_[E.p] (w \ p)`
201 * | (b) E ⊢ p ◊ w and `v' ~_[E.p] w`
203 * This method computes the result `v.w'` as needed (viz. only if `v ~_[E] w`
204 * with respect to this execution `E`). The implementation takes advantage
205 * of the fact that determining whether `v ~_[E] w` yields "for free" the
206 * the shortest such `w'` we are looking for; if we ultimately determine
207 * that `v ~_[E] w`, the work we did to do so leaves us precisely with `w'`,
208 * so we can simply prepend `v` to it and call it a day
210 * @precondition: This method assumes that `E.v` is a valid execution, viz.
211 * that the events of `E` are sufficient to enabled `v_0` and that
212 * `v_0, ..., v_{i - 1}` are sufficient to enable `v_i`. This is the
213 * case when e.g. `v := notdep(e, E).p` for example in ODPOR
215 * @returns a partial execution `v.w'` that should be inserted
216 * as a child of a wakeup tree node representing the sequence `v`
217 * if `v ~_[E] w`, or `std::nullopt` if that relation does not hold
218 * between the two sequences `v` and `w`
220 std::optional<PartialExecution> get_shortest_odpor_sq_subset_insertion(const PartialExecution& v,
221 const PartialExecution& w) const;
224 * @brief For a given sequence `w`, determines whether p in I_[E](w)
226 * @note: You may notice that some of the other methods compute this
227 * value as well. What we notice, though, in those cases is that
228 * we are repeatedly asking about initials with respect to an execution.
229 * It is better, then, to bunch the work together in those cases to
230 * get asymptotically better results (e.g. instead of calling with all
231 * `N` actors, we can process them "in-parallel" as is done with the
232 * computation of SDPOR initials)
234 bool is_initial_after_execution_of(const PartialExecution& w, aid_t p) const;
237 * @brief Determines whether `E ⊢ p ◊ w` given the next action taken by `p`
239 bool is_independent_with_execution_of(const PartialExecution& w, std::shared_ptr<Transition> next_E_p) const;
242 * @brief Determines the event associated with the given handle `handle`
244 const Event& get_event_with_handle(EventHandle handle) const { return contents_[handle]; }
247 * @brief Determines the actor associated with the given event handle `handle`
249 aid_t get_actor_with_handle(EventHandle handle) const { return get_event_with_handle(handle).get_transition()->aid_; }
252 * @brief Determines the transition associated with the given handle `handle`
254 const Transition* get_transition_for_handle(EventHandle handle) const
256 return get_event_with_handle(handle).get_transition().get();
260 * @brief Returns a handle to the newest event of the execution, if such an event exists
262 * @returns the handle to the last event of the execution.
263 * If the sequence is empty, no such handle exists and the
264 * method returns `std::nullopt`
266 std::optional<EventHandle> get_latest_event_handle() const
268 return contents_.empty() ? std::nullopt : std::optional<EventHandle>{static_cast<EventHandle>(size() - 1)};
272 * @brief Returns a set of events which are in
273 * "immediate conflict" (according to the definition given
274 * in the ODPOR paper) with the given event
276 * Two events `e` and `e'` in an execution `E` are said to
279 * 1. `proc(e) != proc(e')`; that is, the events correspond to
280 * the execution of different actors
281 * 2. `e -->_E e'` and there is no `e''` in `E` such that
282 * `e -->_E e''` and `e'' -->_E e'`; that is, the two events
283 * "happen-before" one another in `E` and no other event in
284 * `E` "happens-between" `e` and `e'`
286 * @param handle the event with respect to which races are
288 * @returns a set of event handles, each element of which is an
289 * event in this execution which is in a *race* with event `handle`
291 std::unordered_set<EventHandle> get_racing_events_of(EventHandle handle) const;
294 * @brief Returns a set of events which are in a reversible
295 * race with the given event handle `handle`
297 * Two events `e` and `e'` in an execution `E` are said to
298 * be in a *reversible race* iff
300 * 1. `e` and `e'` race
301 * 2. In any equivalent execution sequence `E'` to `E`
302 * where `e` occurs immediately before `e'`, the actor
303 * running `e'` was enabled in the state prior to `e`
305 * @param handle the event with respect to which
306 * reversible races are computed
307 * @returns a set of event handles, each element of which is an event
308 * in this execution which is in a *reversible race* with event `handle`
310 std::unordered_set<EventHandle> get_reversible_races_of(EventHandle handle) const;
313 * @brief Computes `pre(e, E)` as described in ODPOR [1]
315 * The execution `pre(e, E)` for an event `e` in an
316 * execution `E` is the contiguous prefix of events
317 * `E' <= E` up to but excluding the event `e` itself.
318 * Roughly speaking, the prefix intuitively represents
319 * the "history" of causes which permitted event `e`
322 Execution get_prefix_before(EventHandle) const;
325 * @brief Whether the event represented by `e1`
326 * "happens-before" the event represented by
327 * `e2` in the context of this execution
329 * In the terminology of the ODPOR paper,
330 * this function computes
334 * where `E` is this execution
336 * @note: The happens-before relation computed by this
337 * execution is "coarse" in the sense that context-sensitive
338 * independence is not exploited. To include such context-sensitive
339 * dependencies requires a new method of keeping track of
340 * the happens-before procedure, which is nontrivial...
342 bool happens_before(EventHandle e1, EventHandle e2) const;
345 * @brief Extends the execution by one more step
347 * Intutively, pushing a transition `t` onto execution `E`
348 * is equivalent to making the execution become (using the
349 * notation of [1]) `E.proc(t)` where `proc(t)` is the
350 * actor which executed transition `t`.
352 void push_transition(std::shared_ptr<Transition>);
355 * @brief Extends the execution by a sequence of steps
357 * This method has the equivalent effect of pushing the
358 * transitions of the partial execution one-by-one onto
361 void push_partial_execution(const PartialExecution&);
364 } // namespace simgrid::mc::odpor