Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[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/xbt/utils/iter/variable_for_loop.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 EventSet EventSet::make_intersection(const EventSet& other) const
94 {
95   std::unordered_set<const UnfoldingEvent*> result;
96
97   for (const UnfoldingEvent* e : other.events_) {
98     if (contains(e)) {
99       result.insert(e);
100     }
101   }
102
103   return EventSet(std::move(result));
104 }
105
106 EventSet EventSet::get_local_config() const
107 {
108   return History(*this).get_all_events();
109 }
110
111 size_t EventSet::size() const
112 {
113   return this->events_.size();
114 }
115
116 bool EventSet::empty() const
117 {
118   return this->events_.empty();
119 }
120
121 bool EventSet::contains(const UnfoldingEvent* e) const
122 {
123   return this->events_.find(e) != this->events_.end();
124 }
125
126 bool EventSet::contains_equivalent_to(const UnfoldingEvent* e) const
127 {
128   return std::find_if(begin(), end(), [=](const UnfoldingEvent* e_in_set) { return *e == *e_in_set; }) != end();
129 }
130
131 bool EventSet::is_subset_of(const EventSet& other) const
132 {
133   // If there is some element not contained in `other`, then
134   // the set difference will contain that element and the
135   // result won't be empty
136   return subtracting(other).empty();
137 }
138
139 bool EventSet::is_valid_configuration() const
140 {
141   /// @invariant: A collection of events `E` is a configuration
142   /// if and only if following while following the history of
143   /// each event `e` of `E` you remain in `E`. In other words, you
144   /// only see events from set `E`
145   ///
146   /// The simple proof is based on the definition of a configuration
147   /// which requires that all events have their history contained
148   /// in the set
149   const History history(*this);
150   return contains(history) && is_conflict_free();
151 }
152
153 bool EventSet::contains(const History& history) const
154 {
155   return std::all_of(history.begin(), history.end(), [=](const UnfoldingEvent* e) { return this->contains(e); });
156 }
157
158 bool EventSet::intersects(const History& history) const
159 {
160   return std::any_of(history.begin(), history.end(), [=](const UnfoldingEvent* e) { return this->contains(e); });
161 }
162
163 bool EventSet::intersects(const EventSet& other) const
164 {
165   return std::any_of(other.begin(), other.end(), [=](const UnfoldingEvent* e) { return this->contains(e); });
166 }
167
168 EventSet EventSet::get_largest_maximal_subset() const
169 {
170   const History history(*this);
171   return history.get_all_maximal_events();
172 }
173
174 bool EventSet::is_maximal() const
175 {
176   // A set of events is maximal if no event from
177   // the original set is ruled out when traversing
178   // the history of the events
179   return *this == this->get_largest_maximal_subset();
180 }
181
182 bool EventSet::is_conflict_free() const
183 {
184   const auto begin = simgrid::xbt::variable_for_loop<const EventSet>{{*this}, {*this}};
185   const auto end   = simgrid::xbt::variable_for_loop<const EventSet>();
186   return std::none_of(begin, end, [=](const auto event_pair) {
187     const UnfoldingEvent* e1 = *event_pair[0];
188     const UnfoldingEvent* e2 = *event_pair[1];
189     return e1->conflicts_with(e2);
190   });
191 }
192
193 std::vector<const UnfoldingEvent*> EventSet::get_topological_ordering() const
194 {
195   // This is essentially an implementation of detecting cycles
196   // in a graph with coloring, except it makes a topological
197   // ordering out of it
198   if (empty()) {
199     return std::vector<const UnfoldingEvent*>();
200   }
201
202   std::stack<const UnfoldingEvent*> event_stack;
203   std::vector<const UnfoldingEvent*> topological_ordering;
204   EventSet unknown_events = *this;
205   EventSet temporarily_marked_events;
206   EventSet permanently_marked_events;
207
208   while (not unknown_events.empty()) {
209     EventSet discovered_events;
210     event_stack.push(*unknown_events.begin());
211
212     while (not event_stack.empty()) {
213       const UnfoldingEvent* evt = event_stack.top();
214       discovered_events.insert(evt);
215
216       if (not temporarily_marked_events.contains(evt)) {
217         // If this event hasn't yet been marked, do
218         // so now so that if we both see it
219         // again in a child we can detect a cycle
220         temporarily_marked_events.insert(evt);
221
222         EventSet immediate_causes = evt->get_immediate_causes();
223         if (not immediate_causes.empty() && immediate_causes.is_subset_of(temporarily_marked_events)) {
224           throw std::invalid_argument("Attempted to perform a topological sort on a configuration "
225                                       "whose contents contain a cycle. The configuration (and the graph "
226                                       "connecting all of the events) is an invalid event structure");
227         }
228         immediate_causes.subtract(discovered_events);
229         immediate_causes.subtract(permanently_marked_events);
230         std::for_each(immediate_causes.begin(), immediate_causes.end(),
231                       [&event_stack](const UnfoldingEvent* cause) { event_stack.push(cause); });
232       } else {
233         unknown_events.remove(evt);
234         temporarily_marked_events.remove(evt);
235         permanently_marked_events.insert(evt);
236
237         // In moving this event to the end of the list,
238         // we are saying this events "happens before" other
239         // events that are added later.
240         if (this->contains(evt)) {
241           topological_ordering.push_back(evt);
242         }
243
244         // Only now do we remove the event, i.e. once
245         // we've processed the same event twice
246         event_stack.pop();
247       }
248     }
249   }
250   return topological_ordering;
251 }
252
253 std::vector<const UnfoldingEvent*> EventSet::get_topological_ordering_of_reverse_graph() const
254 {
255   // The implementation exploits the property that
256   // a topological sorting S^R of the reverse graph G^R
257   // of some graph G is simply the reverse of any
258   // topological sorting S of G.
259   auto topological_events = get_topological_ordering();
260   std::reverse(topological_events.begin(), topological_events.end());
261   return topological_events;
262 }
263
264 std::string EventSet::to_string() const
265 {
266   std::string contents;
267
268   for (const auto* event : *this) {
269     contents += event->to_string();
270     contents += " + ";
271   }
272
273   return contents;
274 }
275
276 std::vector<const UnfoldingEvent*> EventSet::move_into_vector() const&&
277 {
278   std::vector<const UnfoldingEvent*> contents;
279   contents.reserve(size());
280
281   for (auto&& event : *this) {
282     contents.push_back(event);
283   }
284
285   return contents;
286 }
287
288 } // namespace simgrid::mc::udpor