Logo AND Algorithmique Numérique Distribuée

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