Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
13571d6c839323cb76de0e7dda13daf73ad25afa
[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/transition/Transition.hpp"
12
13 #include <list>
14 #include <optional>
15 #include <unordered_set>
16 #include <vector>
17
18 namespace simgrid::mc::odpor {
19
20 /**
21  * @brief The occurrence of a transition in an execution
22  *
23  * An execution is set of *events*, where each element represents
24  * the occurrence or execution of the `i`th step of a particular
25  * actor `j`
26  */
27 class Event {
28   std::pair<const Transition*, ClockVector> contents_;
29
30 public:
31   Event()                        = default;
32   Event(Event&&)                 = default;
33   Event(const Event&)            = default;
34   Event& operator=(const Event&) = default;
35   explicit Event(std::pair<const Transition*, ClockVector> pair) : contents_(std::move(pair)) {}
36
37   const Transition* get_transition() const { return std::get<0>(contents_); }
38   const ClockVector& get_clock_vector() const { return std::get<1>(contents_); }
39 };
40
41 /**
42  * @brief An ordered sequence of transitions which describe
43  * the evolution of a process undergoing model checking
44  *
45  * An execution conceptually is just a string of actors
46  * ids (e.g. "1.2.3.1.2.2.1.1"), where the `i`th occurrence
47  * of actor id `j` corresponds to the `i`th action executed
48  * by the actor with id `j` (viz. the `i`th step of actor `j`).
49  * Executions can stand alone on their own or can extend
50  * the execution of other sequences
51  *
52  * Executions are conceived based on the following papers:
53  * 1. "Source Sets: A Foundation for Optimal Dynamic Partial Order Reduction"
54  * by Abdulla et al.
55  *
56  * In addition to representing an actual steps taken,
57  * an execution keeps track of the "happens-before"
58  * relation among the transitions in the execution
59  * by following the procedure outlined in section 4 of the
60  * original DPOR paper with clock vectors.
61  * As new transitions are added to the execution, clock vectors are
62  * computed as appropriate and associated with the corresponding position
63  * in the execution. This allows us to determine “happens-before” in
64  * constant-time between points in the execution (called events
65  * [which is unfortunately the same name used in UDPOR for a slightly
66  * different concept]), albeit for an up-front cost of traversing the
67  * execution stack. The happens-before relation is important in many
68  * places in SDPOR and ODPOR.
69  *
70  * @note: For more nuanced happens-before relations, clock
71  * vectors may not always suffice. Clock vectors work
72  * well with transition-based dependencies like that used in
73  * SimGrid; but to have a more refined independence relation,
74  * an event-based dependency approach is needed. See the section 2
75  * in the ODPOR paper [1] concerning event-based dependencies and
76  * how the happens-before relation can be refined in a
77  * computation model much like that of SimGrid. In fact, the same issue
78  * arrises with UDPOR with context-sensitive dependencies:
79  * the two concepts are analogous if not identical
80  */
81 class Execution {
82 private:
83   /**
84    * @brief The actual steps that are taken by the process
85    * during exploration, relative to the
86    */
87   std::vector<Event> contents_;
88
89   Execution(std::vector<Event>&& contents) : contents_(std::move(contents)) {}
90
91 public:
92   using Handle      = decltype(contents_)::const_iterator;
93   using EventHandle = uint32_t;
94
95   Execution()                            = default;
96   Execution(const Execution&)            = default;
97   Execution& operator=(Execution const&) = default;
98   Execution(Execution&&)                 = default;
99
100   size_t size() const { return this->contents_.size(); }
101   bool empty() const { return this->contents_.empty(); }
102   auto begin() const { return this->contents_.begin(); }
103   auto end() const { return this->contents_.end(); }
104
105   /**
106    * @brief Computes the "core" portion the SDPOR algorithm,
107    * viz. the intersection of the backtracking set and the
108    * set of initials with respect to the *last* event added
109    * to the execution
110    *
111    * The "core" portion of the SDPOR algorithm is found on
112    * lines 6-9 of the pseudocode:
113    *
114    * 6 | let E' := pre(E, e)
115    * 7 | let v :=  notdep(e, E).p
116    * 8 | if I_[E'](v) ∩ backtrack(E') = empty then
117    * 9 |    --> add some q in I_[E'](v) to backtrack(E')
118    *
119    * This method computes all of the lines simultaneously,
120    * returning some actor `q` if it passes line 8 and exists.
121    * The event `e` and the set `backtrack(E')` are the provided
122    * arguments to the method.
123    *
124    * @param e the event with respect to which to determine
125    * whether a backtrack point needs to be added for the
126    * prefix corresponding to the execution prior to `e`
127    *
128    * @param backtrack_set The set of actors which should
129    * not be considered for selection as an SDPOR initial.
130    * While this set need not necessarily correspond to the
131    * backtrack set `backtrack(E')`, doing so provides what
132    * is expected for SDPOR
133    *
134    * See the SDPOR algorithm pseudocode in [1] for more
135    * details for the context of the function.
136    *
137    * @invariant: This method assumes that events `e` and
138    * `e' := get_latest_event_handle()` are in a *reversible* race
139    * as is explicitly the case in SDPOR
140    *
141    * @returns an actor not contained in `disqualified` which
142    * can serve as an initial to reverse the race between `e`
143    * and `e'`
144    */
145   std::optional<aid_t> get_first_sdpor_initial_from(EventHandle e, std::unordered_set<aid_t> backtrack_set) const;
146
147   bool is_initial_after_execution(const PartialExecution& w, aid_t p) const;
148   bool is_independent_with_execution(const PartialExecution& w, const Transition* next_E_p) const;
149
150   /**
151    * @brief For a given sequence of actors `v` and a sequence of transitions `w`,
152    * computes the sequence, if any, that should be inserted as a child a WakeupTree for
153    * this execution
154    */
155   std::optional<PartialExecution> get_shortest_odpor_sq_subset_insertion(const PartialExecution& v,
156                                                                          const PartialExecution& w) const;
157
158   /**
159    * @brief Determines the event associated with
160    * the given handle `handle`
161    */
162   const Event& get_event_with_handle(EventHandle handle) const { return contents_[handle]; }
163
164   /**
165    * @brief Determines the actor associated with
166    * the given event handle `handle`
167    */
168   aid_t get_actor_with_handle(EventHandle handle) const { return get_event_with_handle(handle).get_transition()->aid_; }
169
170   /**
171    * @brief Returns a set of events which are in
172    * "immediate conflict" (according to the definition given
173    * in the ODPOR paper) with the given event
174    *
175    * Two events `e` and `e'` in an execution `E` are said to
176    * race iff
177    *
178    * 1. `proc(e) != proc(e')`; that is, the events correspond to
179    * the execution of different actors
180    * 2. `e -->_E e'` and there is no `e''` in `E` such that
181    *  `e -->_E e''` and `e'' -->_E e'`; that is, the two events
182    * "happen-before" one another in `E` and no other event in
183    * `E` "happens-between" `e` and `e'`
184    *
185    * @param handle the event with respect to which races are
186    * computed
187    * @returns a set of event handles from which race with `handle`
188    */
189   std::unordered_set<EventHandle> get_racing_events_of(EventHandle handle) const;
190
191   /**
192    * @brief Returns a handle to the newest event of the execution,
193    * if such an event exists
194    */
195   std::optional<EventHandle> get_latest_event_handle() const
196   {
197     return contents_.empty() ? std::nullopt : std::optional<EventHandle>{static_cast<EventHandle>(size() - 1)};
198   }
199
200   /**
201    * @brief Computes `pre(e, E)` as described in ODPOR [1]
202    *
203    * The execution `pre(e, E)` for an event `e` in an
204    * execution `E` is the contiguous prefix of events
205    * `E' <= E` up to by excluding the event `e` itself.
206    * The prefix intuitively represents the "history" of
207    * causes that permitted event `e` to exist (roughly
208    * speaking)
209    */
210   Execution get_prefix_up_to(EventHandle) const;
211
212   /**
213    * @brief Whether the event represented by `e1`
214    * "happens-before" the event represented by
215    * `e2` in the context of this execution
216    *
217    * In the terminology of the ODPOR paper,
218    * this function computes
219    *
220    * `e1 --->_E e2`
221    *
222    * where `E` is this execution
223    *
224    * @note: The happens-before relation computed by this
225    * execution is "coarse" in the sense that context-sensitive
226    * independence is not exploited. To include such context-sensitive
227    * dependencies requires a new method of keeping track of
228    * the happens-before procedure, which is nontrivial...
229    */
230   bool happens_before(EventHandle e1, EventHandle e2) const;
231
232   /**
233    * @brief Extends the execution by one more step
234    *
235    * Intutively, pushing a transition `t` onto execution `E`
236    * is equivalent to making the execution become (using the
237    * notation of [1]) `E.proc(t)` where `proc(t)` is the
238    * actor which executed transition `t`.
239    */
240   void push_transition(const Transition*);
241 };
242
243 } // namespace simgrid::mc::odpor
244 #endif