Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add complicated computation of v ~_E w to Execution
[simgrid.git] / src / mc / explo / odpor / Execution.cpp
1 /* Copyright (c) 2008-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 #include "src/mc/explo/odpor/Execution.hpp"
7 #include "xbt/asserts.h"
8 #include <algorithm>
9 #include <limits>
10 #include <vector>
11
12 namespace simgrid::mc::odpor {
13
14 void Execution::push_transition(const Transition* t)
15 {
16   if (t == nullptr) {
17     throw std::invalid_argument("Unexpectedly received `nullptr`");
18   }
19   ClockVector max_clock_vector;
20   for (const Event& e : this->contents_) {
21     if (e.get_transition()->depends(t)) {
22       max_clock_vector = ClockVector::max(max_clock_vector, e.get_clock_vector());
23     }
24   }
25   max_clock_vector[t->aid_] = this->size();
26   contents_.push_back(Event({t, max_clock_vector}));
27 }
28
29 std::unordered_set<Execution::EventHandle> Execution::get_racing_events_of(Execution::EventHandle target) const
30 {
31   std::unordered_set<Execution::EventHandle> racing_events;
32   std::unordered_set<Execution::EventHandle> disqualified_events;
33
34   // For each event of the execution
35   for (auto e_i = target; e_i != std::numeric_limits<Execution::EventHandle>::max(); e_i--) {
36     // We need `e_i -->_E target` as a necessary condition
37     if (not happens_before(e_i, target)) {
38       continue;
39     }
40
41     // Further, `proc(e_i) != proc(target)`
42     if (get_actor_with_handle(e_i) == get_actor_with_handle(target)) {
43       disqualified_events.insert(e_i);
44       continue;
45     }
46
47     // There could an event that "happens-between" the two events which would discount `e_i` as a race
48     for (auto e_j = e_i; e_j < target; e_j++) {
49       // If both:
50       // 1. e_i --->_E e_j; and
51       // 2. disqualified_events.count(e_j) > 0
52       // then e_i --->_E target indirectly (either through
53       // e_j directly, or transitively through e_j)
54       if (happens_before(e_i, e_j) and disqualified_events.count(e_j) > 0) {
55         disqualified_events.insert(e_i);
56         break;
57       }
58     }
59
60     // If `e_i` wasn't disqualified in the last round,
61     // it's in a race with `target`. After marking it
62     // as such, we ensure no other event `e` can happen-before
63     // it (since this would transitively make it the event
64     // which "happens-between" `target` and `e`)
65     if (disqualified_events.count(e_i) == 0) {
66       racing_events.insert(e_i);
67       disqualified_events.insert(e_i);
68     }
69   }
70
71   return racing_events;
72 }
73
74 Execution Execution::get_prefix_up_to(Execution::EventHandle handle) const
75 {
76   return Execution(std::vector<Event>{contents_.begin(), contents_.begin() + handle});
77 }
78
79 std::optional<aid_t> Execution::get_first_sdpor_initial_from(EventHandle e,
80                                                              std::unordered_set<aid_t> disqualified_actors) const
81 {
82   // If this execution is empty, there are no initials
83   // relative to the last transition added to the execution
84   // since such a transition does not exist
85   if (empty()) {
86     return std::nullopt;
87   }
88
89   // To actually compute `I_[E'](v) ∩ backtrack(E')`, we must
90   // first compute `E'` and "move" in the direction of `v`.
91   // We perform a scan over `E` (this execution) and make
92   // note of any events which occur after `e` but don't
93   // "happen-after" `e` by pushing them onto `E'`. Note that
94   // correctness is still preserved in computing `v` "on-the-fly"
95   // to determine if an actor `q` is an initial for `E'` after `v`:
96   // only those events that "occur-before" `v`
97   // could happen-before `v` for any valid happens-before relation.
98
99   // First, grab `E' := pre(e, E)` and determine what actor `p` is
100   const auto next_E_p = get_latest_event_handle().value();
101   Execution E_prime_v = get_prefix_up_to(e);
102   std::vector<sdpor::Execution::EventHandle> v;
103
104   // Note `e + 1` here: `notdep(e, E)` is defined as the
105   // set of events that *occur-after* but don't *happen-after* `e`
106   for (auto e_prime = e + 1; e_prime <= next_E_p; ++e_prime) {
107     // Any event `e*` which occurs after `e` but which does not
108     // happen after `e` is a member of `v`. In addition to marking
109     // the event in `v`, we also "simulate" running the action `v`
110     // from E'
111     if (not happens_before(e, e_prime) or e_prime == next_E_p) {
112       // First, push the transition onto the hypothetical execution
113       E_prime_v.push_transition(get_event_with_handle(e_prime).get_transition());
114       const EventHandle e_prime_in_E_prime_v = E_prime_v.get_latest_event_handle().value();
115
116       // When checking whether any event in `dom_[E'](v)` happens before
117       // `next_[E'](q)` below for thread `q`, we must consider that the
118       // events relative to `E` (this execution) are different than those
119       // relative to `E'.v`. Thus e.g. event `7` in `E` may be event `4`
120       // in `E'.v`. Since we are asking about "happens-before"
121       // `-->_[E'.v]` about `E'.v`, we must build `v` relative to `E'`
122       v.push_back(e_prime_in_E_prime_v);
123
124       // Note that we add `q` to v regardless of whether `q` itself has been
125       // disqualified since `q` may itself disqualify other actors
126       // (i.e. even if `q` is disqualified from being an initial, it
127       // is still contained in the sequence `v`)
128       const aid_t q = E_prime_v.get_actor_with_handle(e_prime_in_E_prime_v);
129       if (disqualified_actors.count(q) > 0) {
130         continue;
131       }
132       const bool is_initial = std::none_of(v.begin(), v.end(), [&](const auto& e_star) {
133         return E_prime_v.happens_before(e_star, e_prime_in_E_prime_v);
134       });
135       if (is_initial) {
136         return q;
137       } else {
138         // If `q` is disqualified as a candidate, clearly
139         // no event occurring after `e_prime` in `E` executed
140         // by actor `q` will qualify since any (valid) happens-before
141         // relation orders actions taken by each actor
142         disqualified_actors.insert(q);
143       }
144     }
145   }
146   return std::nullopt;
147 }
148
149 bool Execution::is_initial_after_execution(const PartialExecution& w, aid_t p) const
150 {
151   auto E_w = *this;
152   std::vector<EventHandle> w_handles;
153   for (const auto& w_i : w) {
154     // Take one step in the direction of `w`
155     E_w.push_transition(w_i.get());
156
157     // If that step happened to be executed by `p`,
158     // great: we know that `p` is contained in `w`.
159     // We now need to verify that it doens't "happen-after"
160     // any events which occur before it
161     if (w_i->aid_ == p) {
162       const auto p_handle = E_w.get_latest_event_handle().value();
163       return std::none_of(w_handles.begin(), w_handles.end(),
164                           [&](const auto handle) { return E_w.happens_before(handle, p_handle); });
165     } else {
166       w_handles.push_back(E_w.get_latest_event_handle().value());
167     }
168   }
169   return false;
170 }
171
172 bool Execution::is_independent_with_execution(const PartialExecution& w, const Transition* next_E_p) const
173 {
174   // INVARIANT: Here, we assume that for any process `p_i` of `w`,
175   // the events of this execution followed by the execution of all
176   // actors occurring before `p_i` in `v` (`p_j`, `0 <= j < i`)
177   // are sufficient to enable `p_i`. This is fortunately the case
178   // with what ODPOR requires of us, viz. to ask the question about
179   // `v := notdep(e, E)` for some execution `E` and event `e` of
180   // that execution.
181   auto E_p_w = *this;
182   E_p_w.push_transition(next_E_p);
183   const auto p_handle = E_p_w.get_latest_event_handle().value();
184
185   // As we add events to `w`, verify that none
186   // of them "happen-after" the event associated with
187   // the step `next_E_p` (viz. p_handle)
188   for (const auto& w_i : w) {
189     E_p_w.push_transition(w_i.get());
190     const auto w_i_handle = E_p_w.get_latest_event_handle().value();
191     if (E_p_w.happens_before(p_handle, w_i_handle)) {
192       return false;
193     }
194   }
195   return true;
196 }
197
198 std::optional<PartialExecution> Execution::get_shortest_odpor_sq_subset_insertion(const PartialExecution& v,
199                                                                                   const PartialExecution& w) const
200 {
201   // See section 4 of Abdulla. et al.'s 2017 ODPOR paper for details (specifically
202   // where the [iterative] computation of `v ~_[E] w` is described)
203   auto E_v   = *this;
204   auto w_now = w;
205
206   for (const auto& next_p_E : v) {
207     const aid_t p = next_p_E->aid_;
208
209     // Is `p in `I_[E](w)`?
210     if (E_v.is_initial_after_execution(w_now, p)) {
211       // Remove `p` from w and continue
212
213       // TODO: If `p` occurs in `w`, it had better refer to the same
214       // transition referenced by `v`. Unfortunately, we have two
215       // sources of truth here which can be manipulated at the same
216       // time as arguments to the function. If ODPOR works correctly,
217       // they should always refer to the same value; but as a sanity check,
218       // we have an assert that tests that at least the types are the same.
219       const auto action_by_p_in_w =
220           std::find_if(w_now.begin(), w_now.end(), [=](const auto& action) { return action->aid_ == p; });
221       xbt_assert(action_by_p_in_w != w_now.end(), "Invariant violated: actor `p` "
222                                                   "is claimed to be an initial after `w` but is "
223                                                   "not actually contained in `w`. This indicates that there "
224                                                   "is a bug computing initials");
225       const auto& w_action = *action_by_p_in_w;
226       xbt_assert(w_action->type_ == next_p_E->type_,
227                  "Invariant violated: `v` claims that actor `%ld` executes '%s' while "
228                  "`w` claims that it executes '%s'. These two partial executions both "
229                  "refer to `next_[E](p)`, which should be the same",
230                  p, next_p_E->to_string(false).c_str(), w_action->to_string(false).c_str());
231       w_now.erase(action_by_p_in_w);
232     }
233     // Is `E ⊢ p ◇ w`?
234     else if (E_v.is_independent_with_execution(w, next_p_E.get())) {
235       // INVARIANT: Note that it is impossible for `p` to be
236       // excluded from the set `I_[E](w)` BUT ALSO be contained in
237       // `w` itself if `E ⊢ p ◇ w`. We assert this is the case here
238       const auto action_by_p_in_w =
239           std::find_if(w_now.begin(), w_now.end(), [=](const auto& action) { return action->aid_ == p; });
240       xbt_assert(action_by_p_in_w == w_now.end(),
241                  "Invariant violated: We claimed that actor `%ld` is not an initial "
242                  "after `w`, yet it's independent with all actions of `w` AND occurs in `w`."
243                  "This indicates that there is a bug computing initials",
244                  p);
245     } else {
246       // Neither of the two above conditions hold, so the relation fails
247       return std::nullopt;
248     }
249
250     // Move one step forward in the direction of `v` and repeat
251     E_v.push_transition(next_p_E.get());
252   }
253
254   // Construct, finally, v.w' by adding `v` to the front of
255   // what remains of `w` after removing `v` as above
256   for (auto it = v.rbegin(); it != v.rend(); ++it) {
257     w_now.push_front(*it);
258   }
259
260   return std::optional<PartialExecution>{std::move(w_now)};
261 }
262
263 bool Execution::happens_before(Execution::EventHandle e1_handle, Execution::EventHandle e2_handle) const
264 {
265   // 1. "happens-before" (-->_E) is a subset of "occurs before" (<_E)
266   // and is an irreflexive relation
267   if (e1_handle >= e2_handle) {
268     return false;
269   }
270
271   // Each execution maintains a stack of clock vectors which are updated
272   // according to the procedure outlined in section 4 of the original DPOR paper
273   const Event& e2     = get_event_with_handle(e2_handle);
274   const aid_t proc_e1 = get_actor_with_handle(e1_handle);
275
276   if (const auto e1_in_e2_clock = e2.get_clock_vector().get(proc_e1); e1_in_e2_clock.has_value()) {
277     return e1_handle <= e1_in_e2_clock.value();
278   }
279   // If `e1` does not appear in e2's clock vector, this implies
280   // not only that the transitions associated with `e1` and `e2
281   // are independent, but further that there are no transitive
282   // dependencies between e1 and e2
283   return false;
284 }
285
286 } // namespace simgrid::mc::odpor