Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1923345630e5b4a1281d5c165a0b9da1394793a7
[simgrid.git] / src / mc / explo / udpor / Configuration.cpp
1 /* Copyright (c) 2008-2023. The SimGrid Team. All rights reserved.          */
2
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. */
5
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"
10
11 #include <algorithm>
12 #include <stack>
13 #include <stdexcept>
14
15 namespace simgrid::mc::udpor {
16
17 Configuration::Configuration(std::initializer_list<UnfoldingEvent*> events) : Configuration(EventSet(std::move(events)))
18 {
19 }
20
21 Configuration::Configuration(const EventSet& events) : events_(events)
22 {
23   if (!events_.is_valid_configuration()) {
24     throw std::invalid_argument("The events do not form a valid configuration");
25   }
26 }
27
28 void Configuration::add_event(UnfoldingEvent* e)
29 {
30   if (e == nullptr) {
31     throw std::invalid_argument("Expected a nonnull `UnfoldingEvent*` but received NULL instead");
32   }
33
34   if (this->events_.contains(e)) {
35     return;
36   }
37
38   this->events_.insert(e);
39   this->newest_event = e;
40
41   // Preserves the property that the configuration is valid
42   History history(e);
43   if (!this->events_.contains(history)) {
44     throw std::invalid_argument("The newly added event has dependencies "
45                                 "which are missing from this configuration");
46   }
47 }
48
49 std::vector<UnfoldingEvent*> Configuration::get_topologically_sorted_events() const
50 {
51   if (events_.empty()) {
52     return std::vector<UnfoldingEvent*>();
53   }
54
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;
60
61   while (not unknown_events.empty()) {
62     EventSet discovered_events;
63     event_stack.push(*unknown_events.begin());
64
65     while (not event_stack.empty()) {
66       UnfoldingEvent* evt = event_stack.top();
67       discovered_events.insert(evt);
68
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);
75
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");
81         }
82         immediate_causes.subtract(discovered_events);
83         immediate_causes.subtract(permanently_marked_events);
84         const EventSet undiscovered_causes = std::move(immediate_causes);
85
86         for (const auto cause : undiscovered_causes) {
87           event_stack.push(cause);
88         }
89       } else {
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);
97
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);
102
103         // Only now do we remove the event, i.e. once
104         // we've processed the same event again
105         event_stack.pop();
106       }
107     }
108   }
109   return topological_ordering;
110 }
111
112 std::vector<UnfoldingEvent*> Configuration::get_topologically_sorted_events_of_reverse_graph() const
113 {
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;
121 }
122
123 std::unique_ptr<CompatibilityGraph>
124 Configuration::make_compatibility_graph_filtered_on(std::function<bool(const UnfoldingEvent*)> pred) const
125 {
126   auto G = std::make_unique<CompatibilityGraph>();
127
128   struct UnfoldingEventSearchData {
129     int immediate_children_count                          = 0;
130     CompatibilityGraphNode* potential_placement           = nullptr;
131     std::unordered_set<CompatibilityGraphNode*> conflicts = std::unordered_set<CompatibilityGraphNode*>();
132   };
133   std::unordered_map<UnfoldingEvent*, UnfoldingEventSearchData> search_data;
134
135   for (auto* e : get_topologically_sorted_events_of_reverse_graph()) {
136
137     // 1. Figure out where to place `e` in `G`
138
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
142
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);
146
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;
150
151     const bool e_should_appear          = pred(e);
152     CompatibilityGraphNode* e_placement = nullptr;
153
154     if (e_should_appear) {
155       // The justification is as follows:
156       //
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
160       //
161       // child_count >= 2:
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
165       //
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;
172
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));
177       } else {
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);
185       }
186     }
187
188     // 2. Update the children of `e`
189
190     const EventSet& e_immediate_causes = e->get_immediate_causes();
191
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`).
195     //
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();
200
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
207       // as any of them
208       if (e_should_appear) {
209         search_data[only_ancestor].potential_placement = e_placement;
210       } else {
211         search_data[only_ancestor].potential_placement = e_child_count == 1 ? e_potential_placement : nullptr;
212       }
213     }
214
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);
220     }
221     for (auto* cause : e_immediate_causes) {
222       search_data[cause].immediate_children_count += 1;
223
224       for (auto parent_conflict : parent_conflicts) {
225         search_data[cause].conflicts.insert(parent_conflict);
226       }
227     }
228
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);
233   }
234
235   return G;
236 }
237
238 std::unique_ptr<CompatibilityGraph> Configuration::make_compatibility_graph() const
239 {
240   return make_compatibility_graph_filtered_on([=](const UnfoldingEvent*) { return true; });
241 }
242
243 } // namespace simgrid::mc::udpor