1 /* Copyright (c) 2008-2023. The SimGrid Team. All rights reserved. */
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. */
6 #include "src/mc/explo/odpor/Execution.hpp"
10 namespace simgrid::mc::odpor {
12 std::unordered_set<Execution::EventHandle> Execution::get_racing_events_of(Execution::EventHandle target) const
14 std::unordered_set<Execution::EventHandle> racing_events;
15 std::unordered_set<Execution::EventHandle> disqualified_events;
17 // For each event of the execution
18 for (auto e_i = target; e_i > 0 && e_i != std::numeric_limits<Execution::EventHandle>::max(); e_i--) {
19 // We need `e_i -->_E target` as a necessary condition
20 if (not happens_before(e_i, target)) {
24 // Further, `proc(e_i) != proc(target)`
25 if (get_actor_with_handle(e_i) == get_actor_with_handle(target)) {
26 disqualified_events.insert(e_i);
30 // There could an event that "happens-between" the two events which would discount `e_i` as a race
31 for (auto e_j = e_i; e_j < target; e_j++) {
33 // 1. e_i --->_E e_j; and
34 // 2. disqualified_events.count(e_j) > 0
35 // then e_i --->_E target indirectly (either through
36 // e_j directly, or transitively through e_j)
37 if (happens_before(e_i, e_j) and disqualified_events.count(e_j) > 0) {
38 disqualified_events.insert(e_i);
43 // If `e_i` wasn't disqualified in the last round,
44 // it's in a race with `target`. After marking it
45 // as such, we ensure no other event `e` can happen-before
46 // it (since this would transitively make it the event
47 // which "happens-between" `target` and `e`)
48 if (disqualified_events.count(e_i) == 0) {
49 racing_events.insert(e_i);
50 disqualified_events.insert(e_i);
57 bool Execution::happens_before(Execution::EventHandle e1_handle, Execution::EventHandle e2_handle) const
59 // 1. "happens-before" is a subset of "occurs before"
60 if (e1_handle > e2_handle) {
64 // Each execution maintains a stack of clock vectors which are updated
65 // according to the procedure outlined in section 4 of the original DPOR paper
66 const Event& e2 = get_event_with_handle(e2_handle);
67 const aid_t proc_e1 = get_actor_with_handle(e1_handle);
68 return e1_handle <= e2.get_clock_vector().get(proc_e1).value_or(0);
71 Execution Execution::get_prefix_up_to(Execution::EventHandle handle)
73 if (handle == static_cast<Execution::EventHandle>(0)) {
76 return Execution(std::vector<Event>{contents_.begin(), contents_.begin() + (handle - 1)});
79 void Execution::push_transition(const Transition* t)
82 throw std::invalid_argument("Unexpectedly received `nullptr`");
84 ClockVector max_clock_vector;
85 for (const Event& e : this->contents_) {
86 if (e.get_transition()->depends(t)) {
87 max_clock_vector = ClockVector::max(max_clock_vector, e.get_clock_vector());
90 // The entry in the vector for `t->aid_` is the size
91 // of the new stack, which will have a size one greater
92 // than that before we insert the new events
93 max_clock_vector[t->aid_] = this->size() + 1;
94 contents_.push_back(Event({t, max_clock_vector}));
97 void Execution::pop_latest()
102 } // namespace simgrid::mc::odpor