Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add first go at implementation of K-partial alternatives
[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/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"
11
12 #include <algorithm>
13 #include <stdexcept>
14
15 namespace simgrid::mc::udpor {
16
17 Configuration::Configuration(std::initializer_list<const UnfoldingEvent*> events)
18     : Configuration(EventSet(std::move(events)))
19 {
20 }
21
22 Configuration::Configuration(const UnfoldingEvent* e) : Configuration(e->get_history())
23 {
24   // The local configuration should always be a valid configuration. We
25   // check the invariant regardless as a sanity check
26 }
27
28 Configuration::Configuration(const EventSet& events) : events_(events)
29 {
30   if (!events_.is_valid_configuration()) {
31     throw std::invalid_argument("The events do not form a valid configuration");
32   }
33 }
34
35 void Configuration::add_event(const UnfoldingEvent* e)
36 {
37   if (e == nullptr) {
38     throw std::invalid_argument("Expected a nonnull `UnfoldingEvent*` but received NULL instead");
39   }
40
41   if (this->events_.contains(e)) {
42     return;
43   }
44
45   // Preserves the property that the configuration is conflict-free
46   if (e->conflicts_with(*this)) {
47     throw std::invalid_argument("The newly added event conflicts with the events already "
48                                 "contained in the configuration. Adding this event violates "
49                                 "the property that a configuration is conflict-free");
50   }
51
52   this->events_.insert(e);
53   this->newest_event = e;
54
55   // Preserves the property that the configuration is causally closed
56   if (auto history = History(e); !this->events_.contains(history)) {
57     throw std::invalid_argument("The newly added event has dependencies "
58                                 "which are missing from this configuration");
59   }
60 }
61
62 bool Configuration::is_compatible_with(const History& history) const
63 {
64   return false;
65 }
66
67 std::vector<const UnfoldingEvent*> Configuration::get_topologically_sorted_events() const
68 {
69   return this->events_.get_topological_ordering();
70 }
71
72 std::vector<const UnfoldingEvent*> Configuration::get_topologically_sorted_events_of_reverse_graph() const
73 {
74   return this->events_.get_topological_ordering_of_reverse_graph();
75 }
76
77 EventSet Configuration::get_minimally_reproducible_events() const
78 {
79   // The implementation exploits the following observations:
80   //
81   // To select the smallest reproducible set of events, we want
82   // to pick events that "knock out" a lot of others. Furthermore,
83   // we need to ensure that the events furthest down in the
84   // causality graph are also selected. If you combine these ideas,
85   // you're basically left with traversing the set of maximal
86   // subsets of C! And we have an iterator for that already!
87   //
88   // The next observation is that the moment we don't increase in size
89   // the current maximal set (or decrease the number of events),
90   // we know that the prior set `S` covered the entire history of C and
91   // was maximal. Subsequent sets will miss events earlier in the
92   // topological ordering that appear in `S`
93   EventSet minimally_reproducible_events = EventSet();
94
95   for (const auto& maximal_set : maximal_subsets_iterator_wrapper<Configuration>(*this)) {
96     if (maximal_set.size() > minimally_reproducible_events.size()) {
97       minimally_reproducible_events = maximal_set;
98     } else {
99       // The moment we see the iterator generate a set of size
100       // that is not monotonically increasing, we can stop:
101       // the set prior was the minimally-reproducible one
102       return minimally_reproducible_events;
103     }
104   }
105   return minimally_reproducible_events;
106 }
107
108 } // namespace simgrid::mc::udpor