Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove CompatibilityGraph and friends
[simgrid.git] / src / mc / explo / udpor / Configuration.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/udpor/Configuration.hpp"
7 #include "src/mc/explo/udpor/History.hpp"
8 #include "src/mc/explo/udpor/UnfoldingEvent.hpp"
9 #include "xbt/asserts.h"
10
11 #include <algorithm>
12 #include <stack>
13 #include <stdexcept>
14
15 namespace simgrid::mc::udpor {
16
17 Configuration::Configuration(std::initializer_list<UnfoldingEvent*> events) : Configuration(EventSet(std::move(events)))
18 {
19 }
20
21 Configuration::Configuration(const EventSet& events) : events_(events)
22 {
23   if (!events_.is_valid_configuration()) {
24     throw std::invalid_argument("The events do not form a valid configuration");
25   }
26 }
27
28 void Configuration::add_event(UnfoldingEvent* e)
29 {
30   if (e == nullptr) {
31     throw std::invalid_argument("Expected a nonnull `UnfoldingEvent*` but received NULL instead");
32   }
33
34   if (this->events_.contains(e)) {
35     return;
36   }
37
38   this->events_.insert(e);
39   this->newest_event = e;
40
41   // Preserves the property that the configuration is valid
42   History history(e);
43   if (!this->events_.contains(history)) {
44     throw std::invalid_argument("The newly added event has dependencies "
45                                 "which are missing from this configuration");
46   }
47 }
48
49 std::vector<UnfoldingEvent*> Configuration::get_topologically_sorted_events() const
50 {
51   if (events_.empty()) {
52     return std::vector<UnfoldingEvent*>();
53   }
54
55   std::stack<UnfoldingEvent*> event_stack;
56   std::vector<UnfoldingEvent*> topological_ordering;
57   EventSet unknown_events = events_;
58   EventSet temporarily_marked_events;
59   EventSet permanently_marked_events;
60
61   while (not unknown_events.empty()) {
62     EventSet discovered_events;
63     event_stack.push(*unknown_events.begin());
64
65     while (not event_stack.empty()) {
66       UnfoldingEvent* evt = event_stack.top();
67       discovered_events.insert(evt);
68
69       if (not temporarily_marked_events.contains(evt)) {
70         // If this event hasn't yet been marked, do
71         // so now so that if we see it again in a child we can
72         // detect a cycle and if we see it again here
73         // we can detect that the node is re-processed
74         temporarily_marked_events.insert(evt);
75
76         EventSet immediate_causes = evt->get_immediate_causes();
77         if (!immediate_causes.empty() && immediate_causes.is_subset_of(temporarily_marked_events)) {
78           throw std::invalid_argument("Attempted to perform a topological sort on a configuration "
79                                       "whose contents contain a cycle. The configuration (and the graph "
80                                       "connecting all of the events) is an invalid event structure");
81         }
82         immediate_causes.subtract(discovered_events);
83         immediate_causes.subtract(permanently_marked_events);
84         const EventSet undiscovered_causes = std::move(immediate_causes);
85
86         for (const auto cause : undiscovered_causes) {
87           event_stack.push(cause);
88         }
89       } else {
90         // Mark this event as:
91         // 1. discovered across all DFSs performed
92         // 2. permanently marked
93         // 3. part of the topological search
94         unknown_events.remove(evt);
95         temporarily_marked_events.remove(evt);
96         permanently_marked_events.insert(evt);
97
98         // In moving this event to the end of the list,
99         // we are saying this events "happens before" other
100         // events that are added later.
101         topological_ordering.push_back(evt);
102
103         // Only now do we remove the event, i.e. once
104         // we've processed the same event again
105         event_stack.pop();
106       }
107     }
108   }
109   return topological_ordering;
110 }
111
112 std::vector<UnfoldingEvent*> Configuration::get_topologically_sorted_events_of_reverse_graph() const
113 {
114   // The method exploits the property that
115   // a topological sorting S^R of the reverse graph G^R
116   // of some graph G is simply the reverse of any
117   // topological sorting S of G.
118   auto topological_events = get_topologically_sorted_events();
119   std::reverse(topological_events.begin(), topological_events.end());
120   return topological_events;
121 }
122
123 } // namespace simgrid::mc::udpor