Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[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/Comb.hpp"
8 #include "src/mc/explo/udpor/History.hpp"
9 #include "src/mc/explo/udpor/Unfolding.hpp"
10 #include "src/mc/explo/udpor/UnfoldingEvent.hpp"
11 #include "src/mc/explo/udpor/maximal_subsets_iterator.hpp"
12 #include "xbt/asserts.h"
13
14 #include <algorithm>
15 #include <stdexcept>
16
17 namespace simgrid::mc::udpor {
18
19 Configuration::Configuration(std::initializer_list<const UnfoldingEvent*> events)
20     : Configuration(EventSet(std::move(events)))
21 {
22 }
23
24 Configuration::Configuration(const UnfoldingEvent* e) : Configuration(e->get_history())
25 {
26   // The local configuration should always be a valid configuration. We
27   // check the invariant regardless as a sanity check
28 }
29
30 Configuration::Configuration(const EventSet& events) : events_(events)
31 {
32   if (!events_.is_valid_configuration()) {
33     throw std::invalid_argument("The events do not form a valid configuration");
34   }
35 }
36
37 Configuration::Configuration(const History& history) : Configuration(history.get_all_events()) {}
38
39 void Configuration::add_event(const UnfoldingEvent* e)
40 {
41   if (e == nullptr) {
42     throw std::invalid_argument("Expected a nonnull `UnfoldingEvent*` but received NULL instead");
43   }
44
45   if (this->events_.contains(e)) {
46     return;
47   }
48
49   // Preserves the property that the configuration is conflict-free
50   if (e->conflicts_with(*this)) {
51     throw std::invalid_argument("The newly added event conflicts with the events already "
52                                 "contained in the configuration. Adding this event violates "
53                                 "the property that a configuration is conflict-free");
54   }
55
56   this->events_.insert(e);
57   this->newest_event = e;
58
59   // Preserves the property that the configuration is causally closed
60   if (auto history = History(e); !this->events_.contains(history)) {
61     throw std::invalid_argument("The newly added event has dependencies "
62                                 "which are missing from this configuration");
63   }
64 }
65
66 bool Configuration::is_compatible_with(const UnfoldingEvent* e) const
67 {
68   return not e->conflicts_with(*this);
69 }
70
71 bool Configuration::is_compatible_with(const History& history) const
72 {
73   return std::none_of(history.begin(), history.end(),
74                       [&](const UnfoldingEvent* e) { return e->conflicts_with(*this); });
75 }
76
77 std::vector<const UnfoldingEvent*> Configuration::get_topologically_sorted_events() const
78 {
79   return this->events_.get_topological_ordering();
80 }
81
82 std::vector<const UnfoldingEvent*> Configuration::get_topologically_sorted_events_of_reverse_graph() const
83 {
84   return this->events_.get_topological_ordering_of_reverse_graph();
85 }
86
87 EventSet Configuration::get_minimally_reproducible_events() const
88 {
89   // The implementation exploits the following observations:
90   //
91   // To select the smallest reproducible set of events, we want
92   // to pick events that "knock out" a lot of others. Furthermore,
93   // we need to ensure that the events furthest down in the
94   // causality graph are also selected. If you combine these ideas,
95   // you're basically left with traversing the set of maximal
96   // subsets of C! And we have an iterator for that already!
97   //
98   // The next observation is that the moment we don't increase in size
99   // the current maximal set (or decrease the number of events),
100   // we know that the prior set `S` covered the entire history of C and
101   // was maximal. Subsequent sets will miss events earlier in the
102   // topological ordering that appear in `S`
103   EventSet minimally_reproducible_events = EventSet();
104
105   for (const auto& maximal_set : maximal_subsets_iterator_wrapper<Configuration>(*this)) {
106     if (maximal_set.size() > minimally_reproducible_events.size()) {
107       minimally_reproducible_events = maximal_set;
108     } else {
109       // The moment we see the iterator generate a set of size
110       // that is not monotonically increasing, we can stop:
111       // the set prior was the minimally-reproducible one
112       return minimally_reproducible_events;
113     }
114   }
115   return minimally_reproducible_events;
116 }
117
118 std::optional<Configuration> Configuration::compute_alternative_to(const EventSet& D, const Unfolding& U) const
119 {
120   // A full alternative can be computed by checking against everything in D
121   return compute_k_partial_alternative_to(D, U, D.size());
122 }
123
124 std::optional<Configuration> Configuration::compute_k_partial_alternative_to(const EventSet& D, const Unfolding& U,
125                                                                              size_t k) const
126 {
127   // 1. Select k (of |D|, whichever is smaller) arbitrary events e_1, ..., e_k from D
128   const auto D_hat = [&]() {
129     const size_t size = std::min(k, D.size());
130     std::vector<const UnfoldingEvent*> D_hat(size);
131     // TODO: Since any subset suffices for computing `k`-partial alternatives,
132     // potentially select intelligently here (e.g. perhaps pick events
133     // with transitions that we know are totally independent). This may be
134     // especially important if the enumeration is the slowest part of
135     // UDPOR
136     //
137     // For now, simply pick the first `k` events
138     std::copy_n(D.begin(), size, D_hat.begin());
139     return D_hat;
140   }();
141
142   // 2. Build a U-comb <s_1, ..., s_k> of size k, where spike `s_i` contains
143   // all events in conflict with `e_i`
144   //
145   // 3. EXCEPT those events e' for which [e'] + C is not a configuration or
146   // [e'] intersects D
147   //
148   // NOTE: This is an expensive operation as we must traverse the entire unfolding
149   // and compute `C.is_compatible_with(History)` for every event in the structure :/.
150   // A later performance improvement would be to incorporate the work of Nguyen et al.
151   // into SimGrid which associated additonal data structures with each unfolding event.
152   // Since that is a rather complicated addition, we defer it to a later time...
153   Comb comb(k);
154
155   for (const auto* e : U) {
156     for (unsigned i = 0; i < k; i++) {
157       const UnfoldingEvent* e_i = D_hat[i];
158       if (const auto e_local_config = History(e);
159           e_i->conflicts_with(e) and (not D.intersects(e_local_config)) and is_compatible_with(e_local_config)) {
160         comb[i].push_back(e);
161       }
162     }
163   }
164
165   // 4. Find any such combination <e_1', ..., e_k'> in comb satisfying
166   // ~(e_i' # e_j') for i != j
167   //
168   // NOTE: This is a VERY expensive operation: it enumerates all possible
169   // ways to select an element from each spike. Unfortunately there's no
170   // way around the enumeration, as computing a full alternative in general is
171   // NP-complete (although computing the k-partial alternative is polynomial in
172   // the number of events)
173   const auto map_events = [](const std::vector<Spike::const_iterator>& spikes) {
174     std::vector<const UnfoldingEvent*> events;
175     for (const auto& event_in_spike : spikes) {
176       events.push_back(*event_in_spike);
177     }
178     return EventSet(std::move(events));
179   };
180   const auto alternative =
181       std::find_if(comb.combinations_begin(), comb.combinations_end(),
182                    [&map_events](const auto& vector) { return map_events(vector).is_conflict_free(); });
183
184   // No such alternative exists
185   if (alternative == comb.combinations_end()) {
186     return std::nullopt;
187   }
188
189   // 5. J := [e_1] + [e_2] + ... + [e_k] is a k-partial alternative
190   return Configuration(History(map_events(*alternative)));
191 }
192
193 } // namespace simgrid::mc::udpor