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_;
58 EventSet temporarily_marked_events;
59 EventSet permanently_marked_events;
61 while (not unknown_events.empty()) {
62 EventSet discovered_events;
63 event_stack.push(*unknown_events.begin());
65 while (not event_stack.empty()) {
66 UnfoldingEvent* evt = event_stack.top();
67 discovered_events.insert(evt);
69 if (not temporarily_marked_events.contains(evt)) {
70 // If this event hasn't yet been marked, do
71 // so now so that if we see it again in a child we can
72 // detect a cycle and if we see it again here
73 // we can detect that the node is re-processed
74 temporarily_marked_events.insert(evt);
76 EventSet immediate_causes = evt->get_immediate_causes();
77 if (!immediate_causes.empty() && immediate_causes.is_subset_of(temporarily_marked_events)) {
78 throw std::invalid_argument("Attempted to perform a topological sort on a configuration "
79 "whose contents contain a cycle. The configuration (and the graph "
80 "connecting all of the events) is an invalid event structure");
82 immediate_causes.subtract(discovered_events);
83 immediate_causes.subtract(permanently_marked_events);
84 const EventSet undiscovered_causes = std::move(immediate_causes);
86 for (const auto cause : undiscovered_causes) {
87 event_stack.push(cause);
90 // Mark this event as:
91 // 1. discovered across all DFSs performed
92 // 2. permanently marked
93 // 3. part of the topological search
94 unknown_events.remove(evt);
95 temporarily_marked_events.remove(evt);
96 permanently_marked_events.insert(evt);
98 // In moving this event to the end of the list,
99 // we are saying this events "happens before" other
100 // events that are added later.
101 topological_ordering.push_back(evt);
103 // Only now do we remove the event, i.e. once
104 // we've processed the same event again
109 return topological_ordering;
112 std::vector<UnfoldingEvent*> Configuration::get_topologically_sorted_events_of_reverse_graph() const
114 // The method exploits the property that
115 // a topological sorting S^R of the reverse graph G^R
116 // of some graph G is simply the reverse of any
117 // topological sorting S of G.
118 auto topological_events = get_topologically_sorted_events();
119 std::reverse(topological_events.begin(), topological_events.end());
120 return topological_events;
123 std::unique_ptr<CompatibilityGraph>
124 Configuration::make_compatibility_graph_filtered_on(std::function<bool(const UnfoldingEvent*)> pred) const
126 auto G = std::make_unique<CompatibilityGraph>();
128 struct UnfoldingEventSearchData {
129 int immediate_children_count = 0;
130 CompatibilityGraphNode* potential_placement = nullptr;
131 std::unordered_set<CompatibilityGraphNode*> conflicts = std::unordered_set<CompatibilityGraphNode*>();
133 std::unordered_map<UnfoldingEvent*, UnfoldingEventSearchData> search_data;
135 for (auto* e : get_topologically_sorted_events_of_reverse_graph()) {
137 // 1. Figure out where to place `e` in `G`
139 // Determine which nodes in the graph are in conflict
140 // with this event. These nodes would have been added by child
141 // events while iterating over the topological ordering of the reverse graph
143 const auto e_search_data_loc = search_data.find(e);
144 const bool e_has_no_search_data = e_search_data_loc == search_data.end();
145 const auto e_search_data = e_has_no_search_data ? UnfoldingEventSearchData() : std::move(e_search_data_loc->second);
147 const auto& e_conflicts = e_search_data.conflicts;
148 const auto& e_potential_placement = e_search_data.potential_placement;
149 const auto e_child_count = e_search_data.immediate_children_count;
151 const bool e_should_appear = pred(e);
152 CompatibilityGraphNode* e_placement = nullptr;
154 if (e_should_appear) {
155 // The justification is as follows:
157 // e_has_no_search_data:
158 // The event `e` is a leaf node, so there are no prior
159 // nodes in `G` to join
162 // If there are two or more events that this event causes,
163 // then we certainly must be part of a compatibility
164 // graph node that conflicts with each of our children
166 // e_potential_placement == nullptr:
167 // If nobody told us about a placement and yet still have search
168 // data, this means means that our child `C` had more than one child itself,
169 // so it we could not have moved into `C`'s _potential_ placement.
170 const bool new_placement_required =
171 e_has_no_search_data || e_child_count >= 2 || e_potential_placement == nullptr;
173 if (new_placement_required) {
174 auto new_graph_node = std::make_unique<CompatibilityGraphNode>(e_conflicts, EventSet({e}));
175 e_placement = new_graph_node.get();
176 G->insert(std::move(new_graph_node));
178 xbt_assert(e_child_count == 1, "An event was informed by an immediate child of placement in "
179 "the same compatibility graph node, yet the child did not inform "
180 "the parent about its presence");
181 // A child event told us this node can be in the
182 // same compatibility node in the graph G. Add ourselves now
183 e_placement = e_potential_placement;
184 e_placement->add_event(e);
188 // 2. Update the children of `e`
190 const EventSet& e_immediate_causes = e->get_immediate_causes();
192 // If there is only a single ancestor, then it MAY BE in
193 // the same "chain" of events as us. Note that the ancestor must
194 // also have only a single child (see the note on `new_placement_required`).
196 // If there is more than one child, then each child is in conflict with `e`
197 // so we don't potentially place it
198 if (e_immediate_causes.size() == 1) {
199 UnfoldingEvent* only_ancestor = *e_immediate_causes.begin();
201 // If `e` is included in the graph, forward its placement on to
202 // the sole child. Otherwise attempt to forward `e`'s _potential_
203 // (potential is stressed) placement. We can only forward `e`'s
204 // potential placement iff `e` has only a single child; for if
205 // `e` had more children, then our sole ancestor would conflict with
206 // each one of `e`'s children and thus couldn't be in the same group
208 if (e_should_appear) {
209 search_data[only_ancestor].potential_placement = e_placement;
211 search_data[only_ancestor].potential_placement = e_child_count == 1 ? e_potential_placement : nullptr;
215 // Our ancestors conflict with everyone `e` does else PLUS `e` itself
216 // ONLY IF e actually was placed
217 auto parent_conflicts = std::move(e_conflicts);
218 if (e_should_appear) {
219 parent_conflicts.insert(e_placement);
221 for (auto* cause : e_immediate_causes) {
222 search_data[cause].immediate_children_count += 1;
224 for (auto parent_conflict : parent_conflicts) {
225 search_data[cause].conflicts.insert(parent_conflict);
229 // This event will only ever be seen once in the
230 // topological ordering. Hence, its resources do not
231 // need to be kept around
232 search_data.erase(e);
238 std::unique_ptr<CompatibilityGraph> Configuration::make_compatibility_graph() const
240 return make_compatibility_graph_filtered_on([=](const UnfoldingEvent*) { return true; });
243 } // namespace simgrid::mc::udpor