1 /* Copyright (c) 2008-2023. The SimGrid Team. All rights reserved. */
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. */
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"
18 namespace simgrid::mc::udpor {
20 Configuration::Configuration(std::initializer_list<const UnfoldingEvent*> events)
21 : Configuration(EventSet(std::move(events)))
25 Configuration::Configuration(const UnfoldingEvent* e) : Configuration(e->get_local_config())
27 // The local configuration should always be a valid configuration. We
28 // check the invariant regardless as a sanity check
31 Configuration::Configuration(const History& history) : Configuration(history.get_all_events()) {}
33 Configuration::Configuration(const EventSet& events) : events_(events)
35 if (!events_.is_valid_configuration()) {
36 throw std::invalid_argument("The events do not form a valid configuration");
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;
46 void Configuration::add_event(const UnfoldingEvent* e)
49 throw std::invalid_argument("Expected a nonnull `UnfoldingEvent*` but received NULL instead");
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)) {
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");
65 this->events_.insert(e);
66 this->newest_event = e;
67 this->latest_event_mapping[e->get_actor()] = e;
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");
76 bool Configuration::is_compatible_with(const UnfoldingEvent* e) const
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
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_));
88 bool Configuration::is_compatible_with(const History& history) const
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);
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()) {
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);
115 std::vector<const UnfoldingEvent*> Configuration::get_topologically_sorted_events() const
117 return this->events_.get_topological_ordering();
120 std::vector<const UnfoldingEvent*> Configuration::get_topologically_sorted_events_of_reverse_graph() const
122 return this->events_.get_topological_ordering_of_reverse_graph();
125 EventSet Configuration::get_minimally_reproducible_events() const
127 // The implementation exploits the following observations:
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!
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();
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;
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;
153 return minimally_reproducible_events;
156 std::optional<Configuration> Configuration::compute_alternative_to(const EventSet& D, const Unfolding& U) const
158 // A full alternative can be computed by checking against everything in D
159 return compute_k_partial_alternative_to(D, U, D.size());
162 std::optional<Configuration> Configuration::compute_k_partial_alternative_to(const EventSet& D, const Unfolding& U,
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
175 // For now, simply pick the first `k` events
176 std::copy_n(D.begin(), k_alt_size, D_hat.begin());
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`
183 // 3. EXCEPT those events e' for which [e'] + C is not a configuration or
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...
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);
203 // 4. Find any such combination <e_1', ..., e_k'> in comb satisfying
204 // ~(e_i' # e_j') for i != j
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);
216 return EventSet(std::move(events));
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(); });
222 // No such alternative exists
223 if (alternative == comb.combinations_end()) {
227 // 5. J := [e_1] + [e_2] + ... + [e_k] is a k-partial alternative
228 return Configuration(History(map_events(*alternative)));
231 std::optional<const UnfoldingEvent*> Configuration::get_latest_event_of(aid_t aid) const
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};
239 std::optional<const Transition*> Configuration::get_latest_action_of(aid_t aid) const
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()};
247 } // namespace simgrid::mc::udpor