Logo AND Algorithmique Numérique Distribuée

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