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"
9 #include "xbt/asserts.h"
15 namespace simgrid::mc::udpor {
17 Configuration::Configuration(std::initializer_list<const UnfoldingEvent*> events)
18 : Configuration(EventSet(std::move(events)))
22 Configuration::Configuration(const EventSet& events) : events_(events)
24 if (!events_.is_valid_configuration()) {
25 throw std::invalid_argument("The events do not form a valid configuration");
29 void Configuration::add_event(const UnfoldingEvent* e)
32 throw std::invalid_argument("Expected a nonnull `UnfoldingEvent*` but received NULL instead");
35 if (this->events_.contains(e)) {
39 this->events_.insert(e);
40 this->newest_event = e;
42 // Preserves the property that the configuration is valid
44 if (!this->events_.contains(history)) {
45 throw std::invalid_argument("The newly added event has dependencies "
46 "which are missing from this configuration");
50 std::vector<const UnfoldingEvent*> Configuration::get_topologically_sorted_events() const
52 if (events_.empty()) {
53 return std::vector<const UnfoldingEvent*>();
56 std::stack<const UnfoldingEvent*> event_stack;
57 std::vector<const UnfoldingEvent*> topological_ordering;
58 EventSet unknown_events = events_;
59 EventSet temporarily_marked_events;
60 EventSet permanently_marked_events;
62 while (not unknown_events.empty()) {
63 EventSet discovered_events;
64 event_stack.push(*unknown_events.begin());
66 while (not event_stack.empty()) {
67 const UnfoldingEvent* evt = event_stack.top();
68 discovered_events.insert(evt);
70 if (not temporarily_marked_events.contains(evt)) {
71 // If this event hasn't yet been marked, do
72 // so now so that if we see it again in a child we can
73 // detect a cycle and if we see it again here
74 // we can detect that the node is re-processed
75 temporarily_marked_events.insert(evt);
77 EventSet immediate_causes = evt->get_immediate_causes();
78 if (!immediate_causes.empty() && immediate_causes.is_subset_of(temporarily_marked_events)) {
79 throw std::invalid_argument("Attempted to perform a topological sort on a configuration "
80 "whose contents contain a cycle. The configuration (and the graph "
81 "connecting all of the events) is an invalid event structure");
83 immediate_causes.subtract(discovered_events);
84 immediate_causes.subtract(permanently_marked_events);
85 const EventSet undiscovered_causes = std::move(immediate_causes);
87 for (const auto cause : undiscovered_causes) {
88 event_stack.push(cause);
91 // Mark this event as:
92 // 1. discovered across all DFSs performed
93 // 2. permanently marked
94 // 3. part of the topological search
95 unknown_events.remove(evt);
96 temporarily_marked_events.remove(evt);
97 permanently_marked_events.insert(evt);
99 // In moving this event to the end of the list,
100 // we are saying this events "happens before" other
101 // events that are added later.
102 topological_ordering.push_back(evt);
104 // Only now do we remove the event, i.e. once
105 // we've processed the same event again
110 return topological_ordering;
113 std::vector<const UnfoldingEvent*> Configuration::get_topologically_sorted_events_of_reverse_graph() const
115 // The method exploits the property that
116 // a topological sorting S^R of the reverse graph G^R
117 // of some graph G is simply the reverse of any
118 // topological sorting S of G.
119 auto topological_events = get_topologically_sorted_events();
120 std::reverse(topological_events.begin(), topological_events.end());
121 return topological_events;
124 } // namespace simgrid::mc::udpor