Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'maximal-subset-search' into 'master'
[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<const UnfoldingEvent*> events)
18     : Configuration(EventSet(std::move(events)))
19 {
20 }
21
22 Configuration::Configuration(const EventSet& events) : events_(events)
23 {
24   if (!events_.is_valid_configuration()) {
25     throw std::invalid_argument("The events do not form a valid configuration");
26   }
27 }
28
29 void Configuration::add_event(const UnfoldingEvent* e)
30 {
31   if (e == nullptr) {
32     throw std::invalid_argument("Expected a nonnull `UnfoldingEvent*` but received NULL instead");
33   }
34
35   if (this->events_.contains(e)) {
36     return;
37   }
38
39   this->events_.insert(e);
40   this->newest_event = e;
41
42   // Preserves the property that the configuration is valid
43   History history(e);
44   if (!this->events_.contains(history)) {
45     throw std::invalid_argument("The newly added event has dependencies "
46                                 "which are missing from this configuration");
47   }
48 }
49
50 std::vector<const UnfoldingEvent*> Configuration::get_topologically_sorted_events() const
51 {
52   if (events_.empty()) {
53     return std::vector<const UnfoldingEvent*>();
54   }
55
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;
61
62   while (not unknown_events.empty()) {
63     EventSet discovered_events;
64     event_stack.push(*unknown_events.begin());
65
66     while (not event_stack.empty()) {
67       const UnfoldingEvent* evt = event_stack.top();
68       discovered_events.insert(evt);
69
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);
76
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");
82         }
83         immediate_causes.subtract(discovered_events);
84         immediate_causes.subtract(permanently_marked_events);
85         const EventSet undiscovered_causes = std::move(immediate_causes);
86
87         for (const auto cause : undiscovered_causes) {
88           event_stack.push(cause);
89         }
90       } else {
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);
98
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);
103
104         // Only now do we remove the event, i.e. once
105         // we've processed the same event again
106         event_stack.pop();
107       }
108     }
109   }
110   return topological_ordering;
111 }
112
113 std::vector<const UnfoldingEvent*> Configuration::get_topologically_sorted_events_of_reverse_graph() const
114 {
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;
122 }
123
124 } // namespace simgrid::mc::udpor