Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ef1ea5455fc9acb12dccb58a1de1b11de6dfc501
[simgrid.git] / src / mc / explo / odpor / Execution.hpp
1 /* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #ifndef SIMGRID_MC_ODPOR_EXECUTION_HPP
7 #define SIMGRID_MC_ODPOR_EXECUTION_HPP
8
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"
14
15 #include <list>
16 #include <optional>
17 #include <unordered_set>
18 #include <vector>
19
20 namespace simgrid::mc::odpor {
21
22 /**
23  * @brief The occurrence of a transition in an execution
24  *
25  * An execution is set of *events*, where each element represents
26  * the occurrence or execution of the `i`th step of a particular
27  * actor `j`
28  */
29 class Event {
30   std::pair<std::shared_ptr<Transition>, ClockVector> contents_;
31
32 public:
33   Event()                        = default;
34   Event(Event&&)                 = default;
35   Event(const Event&)            = default;
36   Event& operator=(const Event&) = default;
37   explicit Event(std::pair<std::shared_ptr<Transition>, ClockVector> pair) : contents_(std::move(pair)) {}
38
39   std::shared_ptr<Transition> get_transition() const { return std::get<0>(contents_); }
40   const ClockVector& get_clock_vector() const { return std::get<1>(contents_); }
41 };
42
43 /**
44  * @brief An ordered sequence of transitions which describe
45  * the evolution of a process undergoing model checking
46  *
47  * An execution conceptually is just a string of actors
48  * ids (e.g. "1.2.3.1.2.2.1.1"), where the `i`th occurrence
49  * of actor id `j` corresponds to the `i`th action executed
50  * by the actor with id `j` (viz. the `i`th step of actor `j`).
51  * Executions can stand alone on their own or can extend
52  * the execution of other sequences
53  *
54  * Executions are conceived based on the following papers:
55  * 1. "Source Sets: A Foundation for Optimal Dynamic Partial Order Reduction"
56  * by Abdulla et al.
57  *
58  * In addition to representing an actual steps taken,
59  * an execution keeps track of the "happens-before"
60  * relation among the transitions in the execution
61  * by following the procedure outlined in section 4 of the
62  * original DPOR paper with clock vectors.
63  * As new transitions are added to the execution, clock vectors are
64  * computed as appropriate and associated with the corresponding position
65  * in the execution. This allows us to determine “happens-before” in
66  * constant-time between points in the execution (called events
67  * [which is unfortunately the same name used in UDPOR for a slightly
68  * different concept]), albeit for an up-front cost of traversing the
69  * execution stack. The happens-before relation is important in many
70  * places in SDPOR and ODPOR.
71  *
72  * @note: For more nuanced happens-before relations, clock
73  * vectors may not always suffice. Clock vectors work
74  * well with transition-based dependencies like that used in
75  * SimGrid; but to have a more refined independence relation,
76  * an event-based dependency approach is needed. See the section 2
77  * in the ODPOR paper [1] concerning event-based dependencies and
78  * how the happens-before relation can be refined in a
79  * computation model much like that of SimGrid. In fact, the same issue
80  * arrises with UDPOR with context-sensitive dependencies:
81  * the two concepts are analogous if not identical
82  */
83 class Execution {
84 private:
85   std::vector<Event> contents_;
86   Execution(std::vector<Event>&& contents) : contents_(std::move(contents)) {}
87
88 public:
89   using EventHandle = uint32_t;
90
91   Execution()                            = default;
92   Execution(const Execution&)            = default;
93   Execution& operator=(Execution const&) = default;
94   Execution(Execution&&)                 = default;
95
96   size_t size() const { return this->contents_.size(); }
97   bool empty() const { return this->contents_.empty(); }
98   auto begin() const { return this->contents_.begin(); }
99   auto end() const { return this->contents_.end(); }
100
101   /**
102    * @brief Computes the "core" portion the SDPOR algorithm,
103    * viz. the intersection of the backtracking set and the
104    * set of initials with respect to the *last* event added
105    * to the execution
106    *
107    * The "core" portion of the SDPOR algorithm is found on
108    * lines 6-9 of the pseudocode:
109    *
110    * 6 | let E' := pre(E, e)
111    * 7 | let v :=  notdep(e, E).p
112    * 8 | if I_[E'](v) ∩ backtrack(E') = empty then
113    * 9 |    --> add some q in I_[E'](v) to backtrack(E')
114    *
115    * This method computes all of the lines simultaneously,
116    * returning some actor `q` if it passes line 8 and exists.
117    * The event `e` and the set `backtrack(E')` are the provided
118    * arguments to the method.
119    *
120    * @param e the event with respect to which to determine
121    * whether a backtrack point needs to be added for the
122    * prefix corresponding to the execution prior to `e`
123    *
124    * @param backtrack_set The set of actors which should
125    * not be considered for selection as an SDPOR initial.
126    * While this set need not necessarily correspond to the
127    * backtrack set `backtrack(E')`, doing so provides what
128    * is expected for SDPOR
129    *
130    * See the SDPOR algorithm pseudocode in [1] for more
131    * details for the context of the function.
132    *
133    * @invariant: This method assumes that events `e` and
134    * `e' := get_latest_event_handle()` are in a *reversible* race
135    * as is explicitly the case in SDPOR
136    *
137    * @returns an actor not contained in `disqualified` which
138    * can serve as an initial to reverse the race between `e`
139    * and `e'`
140    */
141   std::optional<aid_t> get_first_sdpor_initial_from(EventHandle e, std::unordered_set<aid_t> backtrack_set) const;
142
143   /**
144    * @brief Computes the analogous lines from the SDPOR algorithm
145    * in the ODPOR algorithm, viz. the intersection of the slee set
146    * and the set of weak initials with respect to the given pair
147    * of racing events
148    *
149    * This method computes lines 4-6 of the ODPOR pseudocode, viz.:
150    *
151    * 4 | let E' := pre(E, e)
152    * 5 | let v := notdep(e, E).e'^
153    * 6 | if sleep(E') ∩ WI_[E'](v) = empty then ...
154    *
155    * The sequence `v` is computed and returned as needed, based on whether
156    * the check on line 6 passes.
157    *
158    * @invariant: This method assumes that events `e` and
159    * `e_prime` are in a *reversible* race as is the case
160    * in ODPOR
161    */
162   std::optional<PartialExecution> get_odpor_extension_from(EventHandle e, EventHandle e_prime,
163                                                            const State& state_at_e) const;
164
165   /**
166    * @brief For a given sequence of actors `v` and a sequence of transitions `w`,
167    * computes the sequence, if any, that should be inserted as a child in wakeup tree for
168    * this execution
169    *
170    * Recall that the procedure for implementing the insertion
171    * is outlined in section 6.2 of Abdulla et al. 2017 as follows:
172    *
173    * | Let `v` be the smallest (w.r.t to "<") sequence in [the tree] B
174    * | such that `v ~_[E] w`. If `v` is a leaf node, the tree can be left
175    * | unmodified.
176    * |
177    * | Otherwise let `w'` be the shortest sequence such that `w [=_[E] v.w'`
178    * | and add `v.w'` as a new leaf, ordered after all already existing nodes
179    * | of the form `v.w''`
180    *
181    * This method computes the result `v.w'` as needed (viz. only if `v ~_[E] w`
182    * with respect to this execution `E`)
183    *
184    * The procedure for determining `v ~_[E] w` is given as Lemma 4.6 of
185    * Abdulla et al. 2017:
186    *
187    * | The relation `v ~_[E] w` holds if either
188    * | (1) v = <>, or
189    * | (2) v := p.v' and either
190    * |     (a) p in I_[E](w) and `v' ~_[E.p] (w \ p)`
191    * |     (b) E ⊢ p ◊ w and `v' ~_[E.p] w`
192    *
193    * @invariant: This method assumes that `E.v` is a valid execution, viz.
194    * that the events of `E` are sufficient to enabled `v_0` and that
195    * `v_0, ..., v_{i - 1}` are sufficient to enable `v_i`. This is the
196    * case when e.g. `v := notdep(e, E).p` for example in ODPOR
197    *
198    * @returns a partial execution `w'` that should be inserted
199    * as a child of a wakeup tree node with the associated sequence `v`.
200    */
201   std::optional<PartialExecution> get_shortest_odpor_sq_subset_insertion(const PartialExecution& v,
202                                                                          const PartialExecution& w) const;
203
204   /**
205    * @brief For a given sequence `w`, determines whether p in I_[E](w)
206    *
207    * @note: You may notice that some of the other methods compute this
208    * value as well. What we notice, though, in those cases is that
209    * we are repeatedly asking about initials with respect to an execution.
210    * It is better, then, to bunch the work together in those cases to
211    * get asymptotically better results (e.g. instead of calling with all
212    * `N` actors, we can process them "in-parallel" as is done with the
213    * computation of SDPOR initials)
214    */
215   bool is_initial_after_execution_of(const PartialExecution& w, aid_t p) const;
216
217   /**
218    * @brief Determines whether `E ⊢ p ◊ w` given the next action taken by `p`
219    */
220   bool is_independent_with_execution_of(const PartialExecution& w, std::shared_ptr<Transition> next_E_p) const;
221
222   /**
223    * @brief Determines the event associated with
224    * the given handle `handle`
225    */
226   const Event& get_event_with_handle(EventHandle handle) const { return contents_[handle]; }
227
228   /**
229    * @brief Determines the actor associated with
230    * the given event handle `handle`
231    */
232   aid_t get_actor_with_handle(EventHandle handle) const { return get_event_with_handle(handle).get_transition()->aid_; }
233
234   /**
235    * @brief Determines the transition associated with the given handle `handle`
236    */
237   const Transition* get_transition_for_handle(EventHandle handle) const
238   {
239     return get_event_with_handle(handle).get_transition().get();
240   }
241
242   /**
243    * @brief Returns a handle to the newest event of the execution,
244    * if such an event exists
245    */
246   std::optional<EventHandle> get_latest_event_handle() const
247   {
248     return contents_.empty() ? std::nullopt : std::optional<EventHandle>{static_cast<EventHandle>(size() - 1)};
249   }
250
251   /**
252    * @brief Returns a set of events which are in
253    * "immediate conflict" (according to the definition given
254    * in the ODPOR paper) with the given event
255    *
256    * Two events `e` and `e'` in an execution `E` are said to
257    * race iff
258    *
259    * 1. `proc(e) != proc(e')`; that is, the events correspond to
260    * the execution of different actors
261    * 2. `e -->_E e'` and there is no `e''` in `E` such that
262    *  `e -->_E e''` and `e'' -->_E e'`; that is, the two events
263    * "happen-before" one another in `E` and no other event in
264    * `E` "happens-between" `e` and `e'`
265    *
266    * @param handle the event with respect to which races are
267    * computed
268    * @returns a set of event handles from which race with `handle`
269    */
270   std::unordered_set<EventHandle> get_racing_events_of(EventHandle handle) const;
271
272   /**
273    * @brief Returns a set of events which are in a reversible
274    * race with the given event handle `handle`
275    *
276    * Two events `e` and `e'` in an execution `E` are said to
277    * be in a reversible race iff
278    *
279    * 1. `e` and `e'` race
280    * 2. In any equivalent execution sequence `E'` to `E`
281    * where `e` occurs immediately before `e'`, the actor
282    * running `e'` was enabled in the state prior to `e`
283    *
284    * @param handle the event with respect to which
285    * reversible races are computed
286    * @returns a set of event handles from which are in a reversible
287    * race with `handle`
288    */
289   std::unordered_set<EventHandle> get_reversible_races_of(EventHandle handle) const;
290
291   /**
292    * @brief Computes `pre(e, E)` as described in ODPOR [1]
293    *
294    * The execution `pre(e, E)` for an event `e` in an
295    * execution `E` is the contiguous prefix of events
296    * `E' <= E` up to by excluding the event `e` itself.
297    * The prefix intuitively represents the "history" of
298    * causes that permitted event `e` to exist (roughly
299    * speaking)
300    */
301   Execution get_prefix_before(EventHandle) const;
302
303   /**
304    * @brief Whether the event represented by `e1`
305    * "happens-before" the event represented by
306    * `e2` in the context of this execution
307    *
308    * In the terminology of the ODPOR paper,
309    * this function computes
310    *
311    * `e1 --->_E e2`
312    *
313    * where `E` is this execution
314    *
315    * @note: The happens-before relation computed by this
316    * execution is "coarse" in the sense that context-sensitive
317    * independence is not exploited. To include such context-sensitive
318    * dependencies requires a new method of keeping track of
319    * the happens-before procedure, which is nontrivial...
320    */
321   bool happens_before(EventHandle e1, EventHandle e2) const;
322
323   /**
324    * @brief Extends the execution by one more step
325    *
326    * Intutively, pushing a transition `t` onto execution `E`
327    * is equivalent to making the execution become (using the
328    * notation of [1]) `E.proc(t)` where `proc(t)` is the
329    * actor which executed transition `t`.
330    */
331   void push_transition(std::shared_ptr<Transition>);
332 };
333
334 } // namespace simgrid::mc::odpor
335 #endif