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/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"
15 namespace simgrid::mc::udpor {
17 EventSet::EventSet(Configuration&& config) : EventSet(config.get_events()) {}
19 void EventSet::remove(const UnfoldingEvent* e)
21 this->events_.erase(e);
24 void EventSet::subtract(const EventSet& other)
26 this->events_ = std::move(subtracting(other).events_);
29 void EventSet::subtract(const Configuration& config)
31 subtract(config.get_events());
34 EventSet EventSet::subtracting(const EventSet& other) const
36 std::unordered_set<const UnfoldingEvent*> result = this->events_;
38 for (const UnfoldingEvent* e : other.events_)
41 return EventSet(std::move(result));
44 EventSet EventSet::subtracting(const Configuration& config) const
46 return subtracting(config.get_events());
49 EventSet EventSet::subtracting(const UnfoldingEvent* e) const
51 auto result = this->events_;
53 return EventSet(std::move(result));
56 void EventSet::insert(const UnfoldingEvent* e)
58 this->events_.insert(e);
61 void EventSet::form_union(const EventSet& other)
63 this->events_ = std::move(make_union(other).events_);
66 void EventSet::form_union(const Configuration& config)
68 form_union(config.get_events());
71 EventSet EventSet::make_union(const UnfoldingEvent* e) const
73 auto result = this->events_;
75 return EventSet(std::move(result));
78 EventSet EventSet::make_union(const EventSet& other) const
80 std::unordered_set<const UnfoldingEvent*> result = this->events_;
82 for (const UnfoldingEvent* e : other.events_)
85 return EventSet(std::move(result));
88 EventSet EventSet::make_union(const Configuration& config) const
90 return make_union(config.get_events());
93 EventSet EventSet::get_local_config() const
95 return History(*this).get_all_events();
98 size_t EventSet::size() const
100 return this->events_.size();
103 bool EventSet::empty() const
105 return this->events_.empty();
108 bool EventSet::contains(const UnfoldingEvent* e) const
110 return this->events_.find(e) != this->events_.end();
113 bool EventSet::is_subset_of(const EventSet& other) const
115 // If there is some element not contained in `other`, then
116 // the set difference will contain that element and the
117 // result won't be empty
118 return subtracting(other).empty();
121 bool EventSet::is_valid_configuration() const
123 /// @invariant: A collection of events `E` is a configuration
124 /// if and only if following while following the history of
125 /// each event `e` of `E` you remain in `E`. In other words, you
126 /// only see events from set `E`
128 /// The simple proof is based on the definition of a configuration
129 /// which requires that all events have their history contained
131 const History history(*this);
132 return contains(history) && is_conflict_free();
135 bool EventSet::contains(const History& history) const
137 return std::all_of(history.begin(), history.end(), [=](const UnfoldingEvent* e) { return this->contains(e); });
140 bool EventSet::intersects(const History& history) const
142 return std::any_of(history.begin(), history.end(), [=](const UnfoldingEvent* e) { return this->contains(e); });
145 bool EventSet::intersects(const EventSet& other) const
147 return std::any_of(other.begin(), other.end(), [=](const UnfoldingEvent* e) { return this->contains(e); });
150 EventSet EventSet::get_largest_maximal_subset() const
152 const History history(*this);
153 return history.get_all_maximal_events();
156 bool EventSet::is_maximal() const
158 // A set of events is maximal if no event from
159 // the original set is ruled out when traversing
160 // the history of the events
161 return *this == this->get_largest_maximal_subset();
164 bool EventSet::is_conflict_free() const
166 const auto begin = simgrid::xbt::variable_for_loop<const EventSet>{{*this}, {*this}};
167 const auto end = simgrid::xbt::variable_for_loop<const EventSet>();
168 return std::none_of(begin, end, [=](const auto event_pair) {
169 const UnfoldingEvent* e1 = *event_pair[0];
170 const UnfoldingEvent* e2 = *event_pair[1];
171 return e1->conflicts_with(e2);
175 std::vector<const UnfoldingEvent*> EventSet::get_topological_ordering() const
177 // This is essentially an implementation of detecting cycles
178 // in a graph with coloring, except it makes a topological
179 // ordering out of it
181 return std::vector<const UnfoldingEvent*>();
184 std::stack<const UnfoldingEvent*> event_stack;
185 std::vector<const UnfoldingEvent*> topological_ordering;
186 EventSet unknown_events = *this;
187 EventSet temporarily_marked_events;
188 EventSet permanently_marked_events;
190 while (not unknown_events.empty()) {
191 EventSet discovered_events;
192 event_stack.push(*unknown_events.begin());
194 while (not event_stack.empty()) {
195 const UnfoldingEvent* evt = event_stack.top();
196 discovered_events.insert(evt);
198 if (not temporarily_marked_events.contains(evt)) {
199 // If this event hasn't yet been marked, do
200 // so now so that if we both see it
201 // again in a child we can detect a cycle
202 temporarily_marked_events.insert(evt);
204 EventSet immediate_causes = evt->get_immediate_causes();
205 if (!immediate_causes.empty() && immediate_causes.is_subset_of(temporarily_marked_events)) {
206 throw std::invalid_argument("Attempted to perform a topological sort on a configuration "
207 "whose contents contain a cycle. The configuration (and the graph "
208 "connecting all of the events) is an invalid event structure");
210 immediate_causes.subtract(discovered_events);
211 immediate_causes.subtract(permanently_marked_events);
212 std::for_each(immediate_causes.begin(), immediate_causes.end(),
213 [&event_stack](const UnfoldingEvent* cause) { event_stack.push(cause); });
215 unknown_events.remove(evt);
216 temporarily_marked_events.remove(evt);
217 permanently_marked_events.insert(evt);
219 // In moving this event to the end of the list,
220 // we are saying this events "happens before" other
221 // events that are added later.
222 if (this->contains(evt)) {
223 topological_ordering.push_back(evt);
226 // Only now do we remove the event, i.e. once
227 // we've processed the same event twice
232 return topological_ordering;
235 std::vector<const UnfoldingEvent*> EventSet::get_topological_ordering_of_reverse_graph() const
237 // The implementation exploits the property that
238 // a topological sorting S^R of the reverse graph G^R
239 // of some graph G is simply the reverse of any
240 // topological sorting S of G.
241 auto topological_events = get_topological_ordering();
242 std::reverse(topological_events.begin(), topological_events.end());
243 return topological_events;
246 std::string EventSet::to_string() const
248 std::string contents;
250 for (const auto* event : *this) {
251 contents += event->to_string();
258 std::vector<const UnfoldingEvent*> EventSet::move_into_vector() const&&
260 std::vector<const UnfoldingEvent*> contents;
261 contents.reserve(size());
263 for (auto&& event : *this) {
264 contents.push_back(event);
270 } // namespace simgrid::mc::udpor