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<UnfoldingEvent*> events) : Configuration(EventSet(std::move(events)))
21 Configuration::Configuration(const EventSet& events) : events_(events)
23 if (!events_.is_valid_configuration()) {
24 throw std::invalid_argument("The events do not form a valid configuration");
28 void Configuration::add_event(UnfoldingEvent* e)
31 throw std::invalid_argument("Expected a nonnull `UnfoldingEvent*` but received NULL instead");
34 if (this->events_.contains(e)) {
38 this->events_.insert(e);
39 this->newest_event = e;
41 // Preserves the property that the configuration is valid
43 if (!this->events_.contains(history)) {
44 throw std::invalid_argument("The newly added event has dependencies "
45 "which are missing from this configuration");
49 std::vector<UnfoldingEvent*> Configuration::get_topologically_sorted_events() const
51 if (events_.empty()) {
52 return std::vector<UnfoldingEvent*>();
55 std::stack<UnfoldingEvent*> event_stack;
56 std::vector<UnfoldingEvent*> topological_ordering;
57 EventSet unknown_events = events_, temporarily_marked_events, permanently_marked_events;
59 while (not unknown_events.empty()) {
60 EventSet discovered_events;
61 event_stack.push(*unknown_events.begin());
63 while (not event_stack.empty()) {
64 UnfoldingEvent* evt = event_stack.top();
65 discovered_events.insert(evt);
67 if (not temporarily_marked_events.contains(evt)) {
68 // If this event hasn't yet been marked, do
69 // so now so that if we see it again in a child we can
70 // detect a cycle and if we see it again here
71 // we can detect that the node is re-processed
72 temporarily_marked_events.insert(evt);
74 EventSet immediate_causes = evt->get_immediate_causes();
75 if (!immediate_causes.empty() && immediate_causes.is_subset_of(temporarily_marked_events)) {
76 throw std::invalid_argument("Attempted to perform a topological sort on a configuration "
77 "whose contents contain a cycle. The configuration (and the graph "
78 "connecting all of the events) is an invalid event structure");
80 immediate_causes.subtract(discovered_events);
81 immediate_causes.subtract(permanently_marked_events);
82 const EventSet undiscovered_causes = std::move(immediate_causes);
84 for (const auto cause : undiscovered_causes) {
85 event_stack.push(cause);
88 // Mark this event as:
89 // 1. discovered across all DFSs performed
90 // 2. permanently marked
91 // 3. part of the topological search
92 unknown_events.remove(evt);
93 temporarily_marked_events.remove(evt);
94 permanently_marked_events.insert(evt);
96 // In moving this event to the end of the list,
97 // we are saying this events "happens before" other
98 // events that are added later.
99 topological_ordering.push_back(evt);
101 // Only now do we remove the event, i.e. once
102 // we've processed the same event again
107 return topological_ordering;
110 std::vector<UnfoldingEvent*> Configuration::get_topologically_sorted_events_of_reverse_graph() const
112 // The method exploits the property that
113 // a topological sorting S^R of the reverse graph G^R
114 // of some graph G is simply the reverse of any
115 // topological sorting S of G.
116 auto topological_events = get_topologically_sorted_events();
117 std::reverse(topological_events.begin(), topological_events.end());
118 return topological_events;
121 std::unique_ptr<CompatibilityGraph>
122 Configuration::make_compatibility_graph_filtered_on(std::function<bool(UnfoldingEvent*)> pred) const
124 auto G = std::make_unique<CompatibilityGraph>();
126 struct UnfoldingEventSearchData {
127 int immediate_children_count = 0;
128 CompatibilityGraphNode* potential_placement = nullptr;
129 std::unordered_set<CompatibilityGraphNode*> conflicts = std::unordered_set<CompatibilityGraphNode*>();
131 std::unordered_map<UnfoldingEvent*, UnfoldingEventSearchData> search_data;
133 for (auto* e : get_topologically_sorted_events_of_reverse_graph()) {
135 // 1. Figure out where to place `e` in `G`
137 // Determine which nodes in the graph are in conflict
138 // with this event. These nodes would have been added by child
139 // events while iterating over the topological ordering of the reverse graph
141 const auto e_search_data_loc = search_data.find(e);
142 const bool e_has_no_search_data = e_search_data_loc == search_data.end();
143 const auto e_search_data = e_has_no_search_data ? UnfoldingEventSearchData() : std::move(e_search_data_loc->second);
145 const auto& e_conflicts = e_search_data.conflicts;
146 const auto& e_potential_placement = e_search_data.potential_placement;
147 const auto e_child_count = e_search_data.immediate_children_count;
149 CompatibilityGraphNode* e_placement = nullptr;
151 // The justification is as follows:
153 // e_has_no_search_data:
154 // If nobody told us about a placement, we must either be a leaf event
155 // OR be the cause of an event that itself has more than one cause.
158 // If there are two or more events that this event causes,
159 // then we certainly must be part of a different compatibility
161 const bool new_placement_required = e_has_no_search_data || e_child_count >= 2;
163 if (new_placement_required) {
164 auto new_graph_node = std::make_unique<CompatibilityGraphNode>(e_conflicts, EventSet({e}));
165 e_placement = new_graph_node.get();
166 G->insert(std::move(new_graph_node));
168 xbt_assert(e_child_count == 1, "An event was informed by an immediate child of placement in "
169 "the same compatibility graph node, yet the child did not inform "
170 "the parent about its precense");
171 // A child event told us this node can be in the
172 // same compatibility node in the graph G. Add ourselves now
173 e_placement = e_potential_placement;
174 e_placement->add_event(e);
177 // 2. Update the children of `e`
179 const EventSet& e_immediate_causes = e->get_immediate_causes();
180 if (e_immediate_causes.size() == 1) {
181 // If there is only a single ancestor, then it MAY BE in
182 // the same "chain" of events as us. Note that the ancestor must
183 // also have only a single child (see the note on `new_placement_required`)
184 UnfoldingEvent* only_ancestor = *e_immediate_causes.begin();
185 search_data[only_ancestor].potential_placement = e_placement;
188 // Our ancestors conflict with everyone `e` does else PLUS `e` itself
189 auto parent_conflicts = std::move(e_conflicts);
190 parent_conflicts.insert(e_placement);
191 for (auto* cause : e_immediate_causes) {
192 search_data[cause].immediate_children_count += 1;
194 for (auto parent_conflict : parent_conflicts) {
195 search_data[cause].conflicts.insert(parent_conflict);
199 // This event will only ever be seen once in the
200 // topological ordering. Hence, its resources do not
201 // need to be kept around
202 search_data.erase(e);
208 } // namespace simgrid::mc::udpor