Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add more documentation to essential SDPOR methods
[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 <exception>
8 #include <limits>
9
10 namespace simgrid::mc::odpor {
11
12 void Execution::push_transition(const Transition* t)
13 {
14   if (t == nullptr) {
15     throw std::invalid_argument("Unexpectedly received `nullptr`");
16   }
17   ClockVector max_clock_vector;
18   for (const Event& e : this->contents_) {
19     if (e.get_transition()->depends(t)) {
20       max_clock_vector = ClockVector::max(max_clock_vector, e.get_clock_vector());
21     }
22   }
23   max_clock_vector[t->aid_] = this->size();
24   contents_.push_back(Event({t, max_clock_vector}));
25 }
26
27 void Execution::pop_latest()
28 {
29   contents_.pop_back();
30 }
31
32 std::unordered_set<Execution::EventHandle> Execution::get_racing_events_of(Execution::EventHandle target) const
33 {
34   std::unordered_set<Execution::EventHandle> racing_events;
35   std::unordered_set<Execution::EventHandle> disqualified_events;
36
37   // For each event of the execution
38   for (auto e_i = target; e_i != std::numeric_limits<Execution::EventHandle>::max(); e_i--) {
39     // We need `e_i -->_E target` as a necessary condition
40     if (not happens_before(e_i, target)) {
41       continue;
42     }
43
44     // Further, `proc(e_i) != proc(target)`
45     if (get_actor_with_handle(e_i) == get_actor_with_handle(target)) {
46       disqualified_events.insert(e_i);
47       continue;
48     }
49
50     // There could an event that "happens-between" the two events which would discount `e_i` as a race
51     for (auto e_j = e_i; e_j < target; e_j++) {
52       // If both:
53       // 1. e_i --->_E e_j; and
54       // 2. disqualified_events.count(e_j) > 0
55       // then e_i --->_E target indirectly (either through
56       // e_j directly, or transitively through e_j)
57       if (happens_before(e_i, e_j) and disqualified_events.count(e_j) > 0) {
58         disqualified_events.insert(e_i);
59         break;
60       }
61     }
62
63     // If `e_i` wasn't disqualified in the last round,
64     // it's in a race with `target`. After marking it
65     // as such, we ensure no other event `e` can happen-before
66     // it (since this would transitively make it the event
67     // which "happens-between" `target` and `e`)
68     if (disqualified_events.count(e_i) == 0) {
69       racing_events.insert(e_i);
70       disqualified_events.insert(e_i);
71     }
72   }
73
74   return racing_events;
75 }
76
77 Execution Execution::get_prefix_up_to(Execution::EventHandle handle) const
78 {
79   return Execution(std::vector<Event>{contents_.begin(), contents_.begin() + handle});
80 }
81
82 std::optional<aid_t> Execution::get_first_sdpor_initial_from(EventHandle e,
83                                                              std::unordered_set<aid_t> disqualified_actors) const
84 {
85   // If this execution is empty, there are no initials
86   // relative to the last transition added to the execution
87   // since such a transition does not exist
88   if (empty()) {
89     return std::nullopt;
90   }
91
92   // First, grab `E' := pre(e, E)` and determine what actor `p` is
93   // TODO: Instead of copying around these big structs, it
94   // would behoove us to incorporate some way to reference
95   // portions of an execution. For simplicity and for a
96   // "proof of concept" version, we opt to simply copy
97   // the contents instead of making a view into the execution
98   const auto next_E_p = get_latest_event_handle().value();
99   Execution E_prime_v = get_prefix_up_to(e);
100   std::vector<sdpor::Execution::EventHandle> v;
101
102   // Note `e + 1` here: `notdep(e, E)` is defined as the
103   // set of events that *occur-after* but don't *happen-after* `e`
104   for (auto e_prime = e + 1; e_prime <= next_E_p; ++e_prime) {
105     // Any event `e*` which occurs after `e` but which does not
106     // happen after `e` is a member of `v`. In addition to marking
107     // the event in `v`, we also "simulate" running the action `v`
108     // from E'
109     if (not happens_before(e, e_prime) or e_prime == next_E_p) {
110       // First, push the transition onto the hypothetical execution
111       E_prime_v.push_transition(get_event_with_handle(e_prime).get_transition());
112       const EventHandle e_prime_in_E_prime_v = E_prime_v.get_latest_event_handle().value();
113
114       // When checking whether any event in `dom_[E'](v)` happens before
115       // `next_[E'](q)` below for thread `q`, we must consider that the
116       // events relative to `E` (this execution) are different than those
117       // relative to `E'.v`. Thus e.g. event `7` in `E` may be event `4`
118       // in `E'.v`. Since we are asking about "happens-before"
119       // `-->_[E'.v]` about `E'.v`, we must build `v` relative to `E'`
120       v.push_back(e_prime_in_E_prime_v);
121
122       // Note that we add `q` to v regardless of whether `q` itself has been
123       // disqualified since `q` may itself disqualify other actors
124       // (i.e. even if `q` is disqualified from being an initial, it
125       // is still contained in the sequence `v`)
126       const aid_t q = E_prime_v.get_actor_with_handle(e_prime_in_E_prime_v);
127       if (disqualified_actors.count(q) > 0) {
128         continue;
129       }
130       const bool is_initial = std::none_of(v.begin(), v.end(), [&](const auto& e_star) {
131         return E_prime_v.happens_before(e_star, e_prime_in_E_prime_v);
132       });
133       if (is_initial) {
134         return q;
135       } else {
136         // If `q` is disqualified as a candidate, clearly
137         // no event occurring after `e_prime` in `E` executed
138         // by actor `q` will qualify since any (valid) happens-before
139         // relation orders actions taken by each actor
140         disqualified_actors.insert(q);
141       }
142     }
143   }
144   return std::nullopt;
145 }
146
147 bool Execution::happens_before(Execution::EventHandle e1_handle, Execution::EventHandle e2_handle) const
148 {
149   // 1. "happens-before" (-->_E) is a subset of "occurs before" (<_E)
150   // and is an irreflexive relation
151   if (e1_handle >= e2_handle) {
152     return false;
153   }
154
155   // Each execution maintains a stack of clock vectors which are updated
156   // according to the procedure outlined in section 4 of the original DPOR paper
157   const Event& e2     = get_event_with_handle(e2_handle);
158   const aid_t proc_e1 = get_actor_with_handle(e1_handle);
159
160   if (const auto e1_in_e2_clock = e2.get_clock_vector().get(proc_e1); e1_in_e2_clock.has_value()) {
161     return e1_handle <= e1_in_e2_clock.value();
162   }
163   // If `e1` does not appear in e2's clock vector, this implies
164   // not only that the transitions associated with `e1` and `e2
165   // are independent, but further that there are no transitive
166   // dependencies between e1 and e2
167   return false;
168 }
169
170 } // namespace simgrid::mc::odpor