Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix most cosmetics and code warnings
[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_, temporarily_marked_events, permanently_marked_events;
58
59   while (not unknown_events.empty()) {
60     EventSet discovered_events;
61     event_stack.push(*unknown_events.begin());
62
63     while (not event_stack.empty()) {
64       UnfoldingEvent* evt = event_stack.top();
65       discovered_events.insert(evt);
66
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);
73
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");
79         }
80         immediate_causes.subtract(discovered_events);
81         immediate_causes.subtract(permanently_marked_events);
82         const EventSet undiscovered_causes = std::move(immediate_causes);
83
84         for (const auto cause : undiscovered_causes) {
85           event_stack.push(cause);
86         }
87       } else {
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);
95
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);
100
101         // Only now do we remove the event, i.e. once
102         // we've processed the same event again
103         event_stack.pop();
104       }
105     }
106   }
107   return topological_ordering;
108 }
109
110 std::vector<UnfoldingEvent*> Configuration::get_topologically_sorted_events_of_reverse_graph() const
111 {
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;
119 }
120
121 std::unique_ptr<CompatibilityGraph>
122 Configuration::make_compatibility_graph_filtered_on(std::function<bool(UnfoldingEvent*)> pred) const
123 {
124   auto G = std::make_unique<CompatibilityGraph>();
125
126   struct UnfoldingEventSearchData {
127     int immediate_children_count                          = 0;
128     CompatibilityGraphNode* potential_placement           = nullptr;
129     std::unordered_set<CompatibilityGraphNode*> conflicts = std::unordered_set<CompatibilityGraphNode*>();
130   };
131   std::unordered_map<UnfoldingEvent*, UnfoldingEventSearchData> search_data;
132
133   for (auto* e : get_topologically_sorted_events_of_reverse_graph()) {
134
135     // 1. Figure out where to place `e` in `G`
136
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
140
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);
144
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;
148
149     CompatibilityGraphNode* e_placement = nullptr;
150
151     // The justification is as follows:
152     //
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.
156     //
157     // child_count >= 2:
158     //  If there are two or more events that this event causes,
159     //  then we certainly must be part of a different compatibility
160     //  graph node since
161     const bool new_placement_required = e_has_no_search_data || e_child_count >= 2;
162
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));
167     } else {
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);
175     }
176
177     // 2. Update the children of `e`
178
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;
186     }
187
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;
193
194       for (auto parent_conflict : parent_conflicts) {
195         search_data[cause].conflicts.insert(parent_conflict);
196       }
197     }
198
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);
203   }
204
205   return G;
206 }
207
208 } // namespace simgrid::mc::udpor