Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add ODPOR extension computation (lines 4-6)
[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/transition/Transition.hpp"
13
14 #include <list>
15 #include <optional>
16 #include <unordered_set>
17 #include <vector>
18
19 namespace simgrid::mc::odpor {
20
21 /**
22  * @brief The occurrence of a transition in an execution
23  *
24  * An execution is set of *events*, where each element represents
25  * the occurrence or execution of the `i`th step of a particular
26  * actor `j`
27  */
28 class Event {
29   std::pair<std::shared_ptr<Transition>, ClockVector> contents_;
30
31 public:
32   Event()                        = default;
33   Event(Event&&)                 = default;
34   Event(const Event&)            = default;
35   Event& operator=(const Event&) = default;
36   explicit Event(std::pair<std::shared_ptr<Transition>, ClockVector> pair) : contents_(std::move(pair)) {}
37
38   std::shared_ptr<Transition> get_transition() const { return std::get<0>(contents_); }
39   const ClockVector& get_clock_vector() const { return std::get<1>(contents_); }
40 };
41
42 /**
43  * @brief An ordered sequence of transitions which describe
44  * the evolution of a process undergoing model checking
45  *
46  * An execution conceptually is just a string of actors
47  * ids (e.g. "1.2.3.1.2.2.1.1"), where the `i`th occurrence
48  * of actor id `j` corresponds to the `i`th action executed
49  * by the actor with id `j` (viz. the `i`th step of actor `j`).
50  * Executions can stand alone on their own or can extend
51  * the execution of other sequences
52  *
53  * Executions are conceived based on the following papers:
54  * 1. "Source Sets: A Foundation for Optimal Dynamic Partial Order Reduction"
55  * by Abdulla et al.
56  *
57  * In addition to representing an actual steps taken,
58  * an execution keeps track of the "happens-before"
59  * relation among the transitions in the execution
60  * by following the procedure outlined in section 4 of the
61  * original DPOR paper with clock vectors.
62  * As new transitions are added to the execution, clock vectors are
63  * computed as appropriate and associated with the corresponding position
64  * in the execution. This allows us to determine “happens-before” in
65  * constant-time between points in the execution (called events
66  * [which is unfortunately the same name used in UDPOR for a slightly
67  * different concept]), albeit for an up-front cost of traversing the
68  * execution stack. The happens-before relation is important in many
69  * places in SDPOR and ODPOR.
70  *
71  * @note: For more nuanced happens-before relations, clock
72  * vectors may not always suffice. Clock vectors work
73  * well with transition-based dependencies like that used in
74  * SimGrid; but to have a more refined independence relation,
75  * an event-based dependency approach is needed. See the section 2
76  * in the ODPOR paper [1] concerning event-based dependencies and
77  * how the happens-before relation can be refined in a
78  * computation model much like that of SimGrid. In fact, the same issue
79  * arrises with UDPOR with context-sensitive dependencies:
80  * the two concepts are analogous if not identical
81  */
82 class Execution {
83 private:
84   std::vector<Event> contents_;
85   Execution(std::vector<Event>&& contents) : contents_(std::move(contents)) {}
86
87 public:
88   using Handle      = decltype(contents_)::const_iterator;
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 For a given sequence of actors `v` and a sequence of transitions `w`,
145    * computes the sequence, if any, that should be inserted as a child a wakeup tree for
146    * this execution
147    */
148   std::optional<PartialExecution> get_shortest_odpor_sq_subset_insertion(const PartialExecution& v,
149                                                                          const PartialExecution& w) const;
150
151   /**
152    * @brief For a given reversible race
153    *
154    * @invariant: This method assumes that events `e` and
155    * `e_prime` are in a *reversible* race as is the case
156    * in ODPOR
157    */
158   std::optional<PartialExecution> get_odpor_extension_from(EventHandle e, EventHandle e_prime,
159                                                            const State& state_at_e) const;
160
161   bool is_initial_after_execution(const PartialExecution& w, aid_t p) const;
162   bool is_independent_with_execution(const PartialExecution& w, std::shared_ptr<Transition> next_E_p) const;
163
164   /**
165    * @brief Determines the event associated with
166    * the given handle `handle`
167    */
168   const Event& get_event_with_handle(EventHandle handle) const { return contents_[handle]; }
169
170   /**
171    * @brief Determines the actor associated with
172    * the given event handle `handle`
173    */
174   aid_t get_actor_with_handle(EventHandle handle) const { return get_event_with_handle(handle).get_transition()->aid_; }
175
176   /**
177    * @brief Returns a handle to the newest event of the execution,
178    * if such an event exists
179    */
180   std::optional<EventHandle> get_latest_event_handle() const
181   {
182     return contents_.empty() ? std::nullopt : std::optional<EventHandle>{static_cast<EventHandle>(size() - 1)};
183   }
184
185   /**
186    * @brief Returns a set of events which are in
187    * "immediate conflict" (according to the definition given
188    * in the ODPOR paper) with the given event
189    *
190    * Two events `e` and `e'` in an execution `E` are said to
191    * race iff
192    *
193    * 1. `proc(e) != proc(e')`; that is, the events correspond to
194    * the execution of different actors
195    * 2. `e -->_E e'` and there is no `e''` in `E` such that
196    *  `e -->_E e''` and `e'' -->_E e'`; that is, the two events
197    * "happen-before" one another in `E` and no other event in
198    * `E` "happens-between" `e` and `e'`
199    *
200    * @param handle the event with respect to which races are
201    * computed
202    * @returns a set of event handles from which race with `handle`
203    */
204   std::unordered_set<EventHandle> get_racing_events_of(EventHandle handle) const;
205
206   /**
207    * @brief Computes `pre(e, E)` as described in ODPOR [1]
208    *
209    * The execution `pre(e, E)` for an event `e` in an
210    * execution `E` is the contiguous prefix of events
211    * `E' <= E` up to by excluding the event `e` itself.
212    * The prefix intuitively represents the "history" of
213    * causes that permitted event `e` to exist (roughly
214    * speaking)
215    */
216   Execution get_prefix_before(EventHandle) const;
217
218   /**
219    * @brief Whether the event represented by `e1`
220    * "happens-before" the event represented by
221    * `e2` in the context of this execution
222    *
223    * In the terminology of the ODPOR paper,
224    * this function computes
225    *
226    * `e1 --->_E e2`
227    *
228    * where `E` is this execution
229    *
230    * @note: The happens-before relation computed by this
231    * execution is "coarse" in the sense that context-sensitive
232    * independence is not exploited. To include such context-sensitive
233    * dependencies requires a new method of keeping track of
234    * the happens-before procedure, which is nontrivial...
235    */
236   bool happens_before(EventHandle e1, EventHandle e2) const;
237
238   /**
239    * @brief Extends the execution by one more step
240    *
241    * Intutively, pushing a transition `t` onto execution `E`
242    * is equivalent to making the execution become (using the
243    * notation of [1]) `E.proc(t)` where `proc(t)` is the
244    * actor which executed transition `t`.
245    */
246   void push_transition(std::shared_ptr<Transition>);
247 };
248
249 } // namespace simgrid::mc::odpor
250 #endif