Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add conflict detection to EventSet
[simgrid.git] / src / mc / explo / udpor / EventSet.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/EventSet.hpp"
7 #include "src/mc/explo/udpor/Configuration.hpp"
8 #include "src/mc/explo/udpor/History.hpp"
9 #include "src/mc/explo/udpor/UnfoldingEvent.hpp"
10 #include "src/mc/explo/udpor/maximal_subsets_iterator.hpp"
11
12 #include <stack>
13 #include <vector>
14
15 namespace simgrid::mc::udpor {
16
17 EventSet::EventSet(Configuration&& config) : EventSet(config.get_events()) {}
18
19 void EventSet::remove(const UnfoldingEvent* e)
20 {
21   this->events_.erase(e);
22 }
23
24 void EventSet::subtract(const EventSet& other)
25 {
26   this->events_ = std::move(subtracting(other).events_);
27 }
28
29 void EventSet::subtract(const Configuration& config)
30 {
31   subtract(config.get_events());
32 }
33
34 EventSet EventSet::subtracting(const EventSet& other) const
35 {
36   std::unordered_set<const UnfoldingEvent*> result = this->events_;
37
38   for (const UnfoldingEvent* e : other.events_)
39     result.erase(e);
40
41   return EventSet(std::move(result));
42 }
43
44 EventSet EventSet::subtracting(const Configuration& config) const
45 {
46   return subtracting(config.get_events());
47 }
48
49 EventSet EventSet::subtracting(const UnfoldingEvent* e) const
50 {
51   auto result = this->events_;
52   result.erase(e);
53   return EventSet(std::move(result));
54 }
55
56 void EventSet::insert(const UnfoldingEvent* e)
57 {
58   this->events_.insert(e);
59 }
60
61 void EventSet::form_union(const EventSet& other)
62 {
63   this->events_ = std::move(make_union(other).events_);
64 }
65
66 void EventSet::form_union(const Configuration& config)
67 {
68   form_union(config.get_events());
69 }
70
71 EventSet EventSet::make_union(const UnfoldingEvent* e) const
72 {
73   auto result = this->events_;
74   result.insert(e);
75   return EventSet(std::move(result));
76 }
77
78 EventSet EventSet::make_union(const EventSet& other) const
79 {
80   std::unordered_set<const UnfoldingEvent*> result = this->events_;
81
82   for (const UnfoldingEvent* e : other.events_)
83     result.insert(e);
84
85   return EventSet(std::move(result));
86 }
87
88 EventSet EventSet::make_union(const Configuration& config) const
89 {
90   return make_union(config.get_events());
91 }
92
93 size_t EventSet::size() const
94 {
95   return this->events_.size();
96 }
97
98 bool EventSet::empty() const
99 {
100   return this->events_.empty();
101 }
102
103 bool EventSet::contains(const UnfoldingEvent* e) const
104 {
105   return this->events_.find(e) != this->events_.end();
106 }
107
108 bool EventSet::is_subset_of(const EventSet& other) const
109 {
110   // If there is some element not contained in `other`, then
111   // the set difference will contain that element and the
112   // result won't be empty
113   return subtracting(other).empty();
114 }
115
116 bool EventSet::is_valid_configuration() const
117 {
118   /// @invariant: A collection of events `E` is a configuration
119   /// if and only if following while following the history of
120   /// each event `e` of `E` you remain in `E`. In other words, you
121   /// only see events from set `E`
122   ///
123   /// The simple proof is based on the definition of a configuration
124   /// which requires that all events have their history contained
125   /// in the set
126   const History history(*this);
127   return this->contains(history);
128 }
129
130 bool EventSet::contains(const History& history) const
131 {
132   return std::all_of(history.begin(), history.end(), [=](const UnfoldingEvent* e) { return this->contains(e); });
133 }
134
135 bool EventSet::is_maximal() const
136 {
137   // A set of events is maximal if no event from
138   // the original set is ruled out when traversing
139   // the history of the events
140   const History history(*this);
141   return *this == history.get_all_maximal_events();
142 }
143
144 bool EventSet::is_conflict_free() const
145 {
146   const auto begin = maximal_subsets_iterator(*this, std::nullopt, {2});
147   const auto end   = maximal_subsets_iterator();
148   return std::none_of(begin, end, [](const EventSet event_pair) {
149     const auto events        = std::move(event_pair).move_into_vector();
150     const UnfoldingEvent* e1 = events[0];
151     const UnfoldingEvent* e2 = events[1];
152     return e1->conflicts_with(e2);
153   });
154 }
155
156 std::vector<const UnfoldingEvent*> EventSet::get_topological_ordering() const
157 {
158   // This is essentially an implementation of detecting cycles
159   // in a graph with coloring, except it makes a topological
160   // ordering out of it
161   if (empty()) {
162     return std::vector<const UnfoldingEvent*>();
163   }
164
165   std::stack<const UnfoldingEvent*> event_stack;
166   std::vector<const UnfoldingEvent*> topological_ordering;
167   EventSet unknown_events = *this;
168   EventSet temporarily_marked_events;
169   EventSet permanently_marked_events;
170
171   while (not unknown_events.empty()) {
172     EventSet discovered_events;
173     event_stack.push(*unknown_events.begin());
174
175     while (not event_stack.empty()) {
176       const UnfoldingEvent* evt = event_stack.top();
177       discovered_events.insert(evt);
178
179       if (not temporarily_marked_events.contains(evt)) {
180         // If this event hasn't yet been marked, do
181         // so now so that if we both see it
182         // again in a child we can detect a cycle
183         temporarily_marked_events.insert(evt);
184
185         EventSet immediate_causes = evt->get_immediate_causes();
186         if (!immediate_causes.empty() && immediate_causes.is_subset_of(temporarily_marked_events)) {
187           throw std::invalid_argument("Attempted to perform a topological sort on a configuration "
188                                       "whose contents contain a cycle. The configuration (and the graph "
189                                       "connecting all of the events) is an invalid event structure");
190         }
191         immediate_causes.subtract(discovered_events);
192         immediate_causes.subtract(permanently_marked_events);
193         std::for_each(immediate_causes.begin(), immediate_causes.end(),
194                       [&event_stack](const UnfoldingEvent* cause) { event_stack.push(cause); });
195       } else {
196         unknown_events.remove(evt);
197         temporarily_marked_events.remove(evt);
198         permanently_marked_events.insert(evt);
199
200         // In moving this event to the end of the list,
201         // we are saying this events "happens before" other
202         // events that are added later.
203         if (this->contains(evt)) {
204           topological_ordering.push_back(evt);
205         }
206
207         // Only now do we remove the event, i.e. once
208         // we've processed the same event twice
209         event_stack.pop();
210       }
211     }
212   }
213   return topological_ordering;
214 }
215
216 std::vector<const UnfoldingEvent*> EventSet::get_topological_ordering_of_reverse_graph() const
217 {
218   // The implementation exploits the property that
219   // a topological sorting S^R of the reverse graph G^R
220   // of some graph G is simply the reverse of any
221   // topological sorting S of G.
222   auto topological_events = get_topological_ordering();
223   std::reverse(topological_events.begin(), topological_events.end());
224   return topological_events;
225 }
226
227 std::vector<const UnfoldingEvent*> EventSet::move_into_vector() const&&
228 {
229   std::vector<const UnfoldingEvent*> contents;
230   contents.reserve(size());
231
232   for (auto&& event : *this) {
233     contents.push_back(event);
234   }
235
236   return contents;
237 }
238
239 } // namespace simgrid::mc::udpor