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/Configuration.hpp"
7 #include "src/mc/explo/udpor/History.hpp"
8 #include "src/mc/explo/udpor/UnfoldingEvent.hpp"
14 namespace simgrid::mc::udpor {
16 Configuration::Configuration(std::initializer_list<UnfoldingEvent*> events) : Configuration(EventSet(std::move(events)))
20 Configuration::Configuration(EventSet events) : events_(events)
22 if (!events_.is_valid_configuration()) {
23 throw std::invalid_argument("The events do not form a valid configuration");
27 void Configuration::add_event(UnfoldingEvent* e)
30 throw std::invalid_argument("Expected a nonnull `UnfoldingEvent*` but received NULL instead");
33 if (this->events_.contains(e)) {
37 this->events_.insert(e);
38 this->newest_event = e;
40 // Preserves the property that the configuration is valid
42 if (!this->events_.contains(history)) {
43 throw std::invalid_argument("The newly added event has dependencies "
44 "which are missing from this configuration");
48 std::vector<UnfoldingEvent*> Configuration::get_topologically_sorted_events() const
50 if (events_.empty()) {
51 return std::vector<UnfoldingEvent*>();
54 std::stack<UnfoldingEvent*> event_stack;
55 std::vector<UnfoldingEvent*> topological_ordering;
56 EventSet unknown_events = events_, temporarily_marked_events, permanently_marked_events;
58 while (not unknown_events.empty()) {
59 EventSet discovered_events;
60 event_stack.push(*unknown_events.begin());
62 while (not event_stack.empty()) {
63 UnfoldingEvent* evt = event_stack.top();
64 discovered_events.insert(evt);
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);
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");
79 immediate_causes.subtract(discovered_events);
80 immediate_causes.subtract(permanently_marked_events);
81 const EventSet undiscovered_causes = std::move(immediate_causes);
83 for (const auto cause : undiscovered_causes) {
84 event_stack.push(cause);
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);
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);
100 // Only now do we remove the event, i.e. once
101 // we've processed the same event again
106 return topological_ordering;
109 std::vector<UnfoldingEvent*> Configuration::get_topologically_sorted_events_of_reverse_graph() const
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;
120 } // namespace simgrid::mc::udpor