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 "xbt/asserts.h"
17 namespace simgrid::mc::udpor {
19 Configuration::Configuration(std::initializer_list<const UnfoldingEvent*> events)
20 : Configuration(EventSet(std::move(events)))
24 Configuration::Configuration(const UnfoldingEvent* e) : Configuration(e->get_history())
26 // The local configuration should always be a valid configuration. We
27 // check the invariant regardless as a sanity check
30 Configuration::Configuration(const EventSet& events) : events_(events)
32 if (!events_.is_valid_configuration()) {
33 throw std::invalid_argument("The events do not form a valid configuration");
37 Configuration::Configuration(const History& history) : Configuration(history.get_all_events()) {}
39 void Configuration::add_event(const UnfoldingEvent* e)
42 throw std::invalid_argument("Expected a nonnull `UnfoldingEvent*` but received NULL instead");
45 if (this->events_.contains(e)) {
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");
56 this->events_.insert(e);
57 this->newest_event = e;
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");
66 bool Configuration::is_compatible_with(const UnfoldingEvent* e) const
68 return not e->conflicts_with(*this);
71 bool Configuration::is_compatible_with(const History& history) const
73 return std::none_of(history.begin(), history.end(),
74 [&](const UnfoldingEvent* e) { return e->conflicts_with(*this); });
77 std::vector<const UnfoldingEvent*> Configuration::get_topologically_sorted_events() const
79 return this->events_.get_topological_ordering();
82 std::vector<const UnfoldingEvent*> Configuration::get_topologically_sorted_events_of_reverse_graph() const
84 return this->events_.get_topological_ordering_of_reverse_graph();
87 EventSet Configuration::get_minimally_reproducible_events() const
89 // The implementation exploits the following observations:
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!
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();
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;
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;
115 return minimally_reproducible_events;
118 std::optional<Configuration> Configuration::compute_alternative_to(const EventSet& D, const Unfolding& U) const
120 // A full alternative can be computed by checking against everything in D
121 return compute_k_partial_alternative_to(D, U, D.size());
124 std::optional<Configuration> Configuration::compute_k_partial_alternative_to(const EventSet& D, const Unfolding& U,
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
137 // For now, simply pick the first `k` events
138 std::copy_n(D.begin(), size, D_hat.begin());
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`
145 // 3. EXCEPT those events e' for which [e'] + C is not a configuration or
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...
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);
165 // 4. Find any such combination <e_1', ..., e_k'> in comb satisfying
166 // ~(e_i' # e_j') for i != j
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);
178 return EventSet(std::move(events));
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(); });
184 // No such alternative exists
185 if (alternative == comb.combinations_end()) {
189 // 5. J := [e_1] + [e_2] + ... + [e_k] is a k-partial alternative
190 return Configuration(History(map_events(*alternative)));
193 } // namespace simgrid::mc::udpor