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 History& history) : Configuration(history.get_all_events()) {}
32 Configuration::Configuration(const EventSet& events) : events_(events)
34 if (!events_.is_valid_configuration()) {
35 throw std::invalid_argument("The events do not form a valid configuration");
38 // Since we add in topological order under `<`, we know that the "most-recent"
39 // transition executed by each actor will appear last
40 for (const UnfoldingEvent* e : get_topologically_sorted_events()) {
41 this->latest_event_mapping[e->get_actor()] = e;
45 void Configuration::add_event(const UnfoldingEvent* e)
48 throw std::invalid_argument("Expected a nonnull `UnfoldingEvent*` but received NULL instead");
51 if (this->events_.contains(e)) {
55 // Preserves the property that the configuration is conflict-free
56 if (e->conflicts_with(*this)) {
57 throw std::invalid_argument("The newly added event conflicts with the events already "
58 "contained in the configuration. Adding this event violates "
59 "the property that a configuration is conflict-free");
62 this->events_.insert(e);
63 this->newest_event = e;
64 this->latest_event_mapping[e->get_actor()] = e;
66 // Preserves the property that the configuration is causally closed
67 if (auto history = History(e); !this->events_.contains(history)) {
68 throw std::invalid_argument("The newly added event has dependencies "
69 "which are missing from this configuration");
73 bool Configuration::is_compatible_with(const UnfoldingEvent* e) const
75 return not e->conflicts_with(*this);
78 bool Configuration::is_compatible_with(const History& history) const
80 return std::none_of(history.begin(), history.end(),
81 [&](const UnfoldingEvent* e) { return e->conflicts_with(*this); });
84 std::vector<const UnfoldingEvent*> Configuration::get_topologically_sorted_events() const
86 return this->events_.get_topological_ordering();
89 std::vector<const UnfoldingEvent*> Configuration::get_topologically_sorted_events_of_reverse_graph() const
91 return this->events_.get_topological_ordering_of_reverse_graph();
94 EventSet Configuration::get_minimally_reproducible_events() const
96 // The implementation exploits the following observations:
98 // To select the smallest reproducible set of events, we want
99 // to pick events that "knock out" a lot of others. Furthermore,
100 // we need to ensure that the events furthest down in the
101 // causality graph are also selected. If you combine these ideas,
102 // you're basically left with traversing the set of maximal
103 // subsets of C! And we have an iterator for that already!
105 // The next observation is that the moment we don't increase in size
106 // the current maximal set (or decrease the number of events),
107 // we know that the prior set `S` covered the entire history of C and
108 // was maximal. Subsequent sets will miss events earlier in the
109 // topological ordering that appear in `S`
110 EventSet minimally_reproducible_events = EventSet();
112 for (const auto& maximal_set : maximal_subsets_iterator_wrapper<Configuration>(*this)) {
113 if (maximal_set.size() > minimally_reproducible_events.size()) {
114 minimally_reproducible_events = maximal_set;
116 // The moment we see the iterator generate a set of size
117 // that is not monotonically increasing, we can stop:
118 // the set prior was the minimally-reproducible one
119 return minimally_reproducible_events;
122 return minimally_reproducible_events;
125 std::optional<Configuration> Configuration::compute_alternative_to(const EventSet& D, const Unfolding& U) const
127 // A full alternative can be computed by checking against everything in D
128 return compute_k_partial_alternative_to(D, U, D.size());
131 std::optional<Configuration> Configuration::compute_k_partial_alternative_to(const EventSet& D, const Unfolding& U,
134 // 1. Select k (of |D|, whichever is smaller) arbitrary events e_1, ..., e_k from D
135 const auto D_hat = [&]() {
136 const size_t size = std::min(k, D.size());
137 std::vector<const UnfoldingEvent*> D_hat(size);
138 // TODO: Since any subset suffices for computing `k`-partial alternatives,
139 // potentially select intelligently here (e.g. perhaps pick events
140 // with transitions that we know are totally independent). This may be
141 // especially important if the enumeration is the slowest part of
144 // For now, simply pick the first `k` events
145 std::copy_n(D.begin(), size, D_hat.begin());
149 // 2. Build a U-comb <s_1, ..., s_k> of size k, where spike `s_i` contains
150 // all events in conflict with `e_i`
152 // 3. EXCEPT those events e' for which [e'] + C is not a configuration or
155 // NOTE: This is an expensive operation as we must traverse the entire unfolding
156 // and compute `C.is_compatible_with(History)` for every event in the structure :/.
157 // A later performance improvement would be to incorporate the work of Nguyen et al.
158 // into SimGrid which associated additonal data structures with each unfolding event.
159 // Since that is a rather complicated addition, we defer it to a later time...
162 for (const auto* e : U) {
163 for (unsigned i = 0; i < k; i++) {
164 const UnfoldingEvent* e_i = D_hat[i];
165 if (const auto e_local_config = History(e);
166 e_i->conflicts_with(e) and (not D.intersects(e_local_config)) and is_compatible_with(e_local_config)) {
167 comb[i].push_back(e);
172 // 4. Find any such combination <e_1', ..., e_k'> in comb satisfying
173 // ~(e_i' # e_j') for i != j
175 // NOTE: This is a VERY expensive operation: it enumerates all possible
176 // ways to select an element from each spike. Unfortunately there's no
177 // way around the enumeration, as computing a full alternative in general is
178 // NP-complete (although computing the k-partial alternative is polynomial in
179 // the number of events)
180 const auto map_events = [](const std::vector<Spike::const_iterator>& spikes) {
181 std::vector<const UnfoldingEvent*> events;
182 for (const auto& event_in_spike : spikes) {
183 events.push_back(*event_in_spike);
185 return EventSet(std::move(events));
187 const auto alternative =
188 std::find_if(comb.combinations_begin(), comb.combinations_end(),
189 [&map_events](const auto& vector) { return map_events(vector).is_conflict_free(); });
191 // No such alternative exists
192 if (alternative == comb.combinations_end()) {
196 // 5. J := [e_1] + [e_2] + ... + [e_k] is a k-partial alternative
197 return Configuration(History(map_events(*alternative)));
200 std::optional<const UnfoldingEvent*> Configuration::get_latest_event_of(aid_t aid) const
202 if (const auto latest_event = latest_event_mapping.find(aid); latest_event != latest_event_mapping.end()) {
203 return std::optional<const UnfoldingEvent*>{latest_event->second};
208 std::optional<const Transition*> Configuration::get_latest_action_of(aid_t aid) const
210 if (const auto latest_event = get_latest_event_of(aid); latest_event.has_value()) {
211 return std::optional<const Transition*>{latest_event.value()->get_transition()};
216 } // namespace simgrid::mc::udpor