Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add more documentation to essential SDPOR methods
[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 using ProcessSequence   = std::list<aid_t>;
21 using ExecutionSequence = std::list<const State*>;
22 using Hypothetical      = ExecutionSequence;
23
24 /**
25  * @brief The occurrence of a transition in an execution
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
36   explicit Event(std::pair<const Transition*, ClockVector> pair) : contents_(std::move(pair)) {}
37
38   const 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 the
61  * original DPOR paper with clock vectors
62  *
63  * @note: For more nuanced happens-before relations, clock
64  * vectors may not always suffice. Clock vectors work
65  * well with transition-based dependencies like that used in
66  * SimGrid; but to have a more refined independence relation,
67  * an event-based dependency approach is needed. See the section 2
68  * in the ODPOR paper [1] concerning event-based dependencies and
69  * how the happens-before relation can be refined in a
70  * computation model much like that of SimGrid. In fact, the same issue
71  * arrises with UDPOR with context-sensitive dependencies:
72  * the two concepts are analogous if not identical
73  */
74 class Execution {
75 private:
76   /**
77    * @brief The actual steps that are taken by the process
78    * during exploration, relative to the
79    */
80   std::vector<Event> contents_;
81
82   Execution(std::vector<Event>&& contents) : contents_(std::move(contents)) {}
83
84 public:
85   using Handle      = decltype(contents_)::const_iterator;
86   using EventHandle = uint32_t;
87
88   Execution()                            = default;
89   Execution(const Execution&)            = default;
90   Execution& operator=(Execution const&) = default;
91   Execution(Execution&&)                 = default;
92   Execution(ExecutionSequence&& seq);
93   Execution(const ExecutionSequence& seq);
94
95   size_t size() const { return this->contents_.size(); }
96   bool empty() const { return this->contents_.empty(); }
97   auto begin() const { return this->contents_.begin(); }
98   auto end() const { return this->contents_.end(); }
99
100   /**
101    * @brief Computes the "core" portion the SDPOR algorithm,
102    * viz. the intersection of the backtracking set and the
103    * set of initials with respect to the end
104    *
105    * See the SDPOR algorithm pseudocode in [1] for more
106    * details for the context of the function.
107    *
108    * @invariant: This method assumes that events `e` and
109    * `e' := get_latest_event_handle()` are in a *reversible* race
110    * as is the case in SDPOR
111    *
112    * @returns an actor not contained in `disqualified` which
113    * can serve as an initial to reverse the race between `e`
114    * and `e'`
115    */
116   std::optional<aid_t> get_first_sdpor_initial_from(EventHandle e, std::unordered_set<aid_t> disqualified) const;
117
118   /**
119    * @brief Determines the event associated with
120    * the given handle `handle`
121    */
122   const Event& get_event_with_handle(EventHandle handle) const { return contents_[handle]; }
123
124   /**
125    * @brief Determines the actor associated with
126    * the given event handle `handle`
127    */
128   aid_t get_actor_with_handle(EventHandle handle) const { return get_event_with_handle(handle).get_transition()->aid_; }
129
130   /**
131    * @brief Returns a set of events which are in
132    * "immediate conflict" (according to the definition given
133    * in the ODPOR paper) with the given event
134    *
135    * Two events `e` and `e'` in an execution `E` are said to
136    * race iff
137    *
138    * 1. `proc(e) != proc(e')`; that is, the events correspond to
139    * the execution of different actors
140    * 2. `e -->_E e'` and there is no `e''` in `E` such that
141    *  `e -->_E e''` and `e'' -->_E e'`; that is, the two events
142    * "happen-before" one another in `E` and no other event in
143    * `E` "happens-between" `e` and `e'`
144    *
145    * @param handle the event with respect to which races are
146    * computed
147    * @returns a set of event handles from which race with `handle`
148    */
149   std::unordered_set<EventHandle> get_racing_events_of(EventHandle handle) const;
150
151   /**
152    * @brief Returns a handle to the newest event of the execution,
153    * if such an event exists
154    */
155   std::optional<EventHandle> get_latest_event_handle() const
156   {
157     return contents_.empty() ? std::nullopt : std::optional<EventHandle>{static_cast<EventHandle>(size() - 1)};
158   }
159
160   /**
161    * @brief Computes `pre(e, E)` as described in ODPOR [1]
162    *
163    * The execution `pre(e, E)` for an event `e` in an
164    * execution `E` is the contiguous prefix of events
165    * `E' <= E` up to by excluding the event `e` itself.
166    * The prefix intuitively represents the "history" of
167    * causes that permitted event `e` to exist (roughly
168    * speaking)
169    */
170   Execution get_prefix_up_to(EventHandle) const;
171
172   /**
173    * @brief Whether the event represented by `e1`
174    * "happens-before" the event represented by
175    * `e2` in the context of this execution
176    *
177    * In the terminology of the ODPOR paper,
178    * this function computes
179    *
180    * `e1 --->_E e2`
181    *
182    * where `E` is this execution
183    *
184    * @note: The happens-before relation computed by this
185    * execution is "coarse" in the sense that context-sensitive
186    * independence is not exploited. To include such context-sensitive
187    * dependencies requires a new method of keeping track of
188    * the happens-before procedure, which is nontrivial...
189    */
190   bool happens_before(EventHandle e1, EventHandle e2) const;
191
192   /**
193    * @brief Removes the last event of the execution,
194    * if such an event exists
195    *
196    * @note: When you remove events from an execution, any views
197    * of the execution referring to those removed events
198    * become invalidated
199    */
200   void pop_latest();
201
202   /**
203    * @brief Extends the execution by one more step
204    *
205    * Intutively, pushing a transition `t` onto execution `E`
206    * is equivalent to making the execution become (using the
207    * notation of [1]) `E.proc(t)` where `proc(t)` is the
208    * actor which executed transition `t`.
209    */
210   void push_transition(const Transition*);
211 };
212
213 } // namespace simgrid::mc::odpor
214 #endif