Logo AND Algorithmique Numérique Distribuée

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