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/History.hpp"
8 #include "src/mc/explo/udpor/UnfoldingEvent.hpp"
9 #include "src/mc/explo/udpor/maximal_subsets_iterator.hpp"
10 #include "xbt/asserts.h"
15 namespace simgrid::mc::udpor {
17 Configuration::Configuration(std::initializer_list<const UnfoldingEvent*> events)
18 : Configuration(EventSet(std::move(events)))
22 Configuration::Configuration(const EventSet& events) : events_(events)
24 if (!events_.is_valid_configuration()) {
25 throw std::invalid_argument("The events do not form a valid configuration");
29 void Configuration::add_event(const UnfoldingEvent* e)
32 throw std::invalid_argument("Expected a nonnull `UnfoldingEvent*` but received NULL instead");
35 if (this->events_.contains(e)) {
39 // Preserves the property that the configuration is conflict-free
40 if (e->conflicts_with(*this)) {
41 throw std::invalid_argument("The newly added event conflicts with the events already "
42 "contained in the configuration. Adding this event violates "
43 "the property that a configuration is conflict-free");
46 this->events_.insert(e);
47 this->newest_event = e;
49 // Preserves the property that the configuration is causally closed
50 if (auto history = History(e); !this->events_.contains(history)) {
51 throw std::invalid_argument("The newly added event has dependencies "
52 "which are missing from this configuration");
56 std::vector<const UnfoldingEvent*> Configuration::get_topologically_sorted_events() const
58 return this->events_.get_topological_ordering();
61 std::vector<const UnfoldingEvent*> Configuration::get_topologically_sorted_events_of_reverse_graph() const
63 return this->events_.get_topological_ordering_of_reverse_graph();
66 EventSet Configuration::get_minimally_reproducible_events() const
68 // The implementation exploits the following observations:
70 // To select the smallest reproducible set of events, we want
71 // to pick events that "knock out" a lot of others. Furthermore,
72 // we need to ensure that the events furthest down in the
73 // causality graph are also selected. If you combine these ideas,
74 // you're basically left with traversing the set of maximal
75 // subsets of C! And we have an iterator for that already!
77 // The next observation is that the moment we don't increase in size
78 // the current maximal set (or decrease the number of events),
79 // we know that the prior set `S` covered the entire history of C and
80 // was maximal. Subsequent sets will miss events earlier in the
81 // topological ordering that appear in `S`
82 EventSet minimally_reproducible_events = EventSet();
84 for (const auto& maximal_set : maximal_subsets_iterator_wrapper<Configuration>(*this)) {
85 if (maximal_set.size() > minimally_reproducible_events.size()) {
86 minimally_reproducible_events = maximal_set;
88 // The moment we see the iterator generate a set of size
89 // that is not monotonically increasing, we can stop:
90 // the set prior was the minimally-reproducible one
91 return minimally_reproducible_events;
94 return minimally_reproducible_events;
97 } // namespace simgrid::mc::udpor