Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move topological ordering to EventSet
[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 EventSet& events) : events_(events)
23 {
24   if (!events_.is_valid_configuration()) {
25     throw std::invalid_argument("The events do not form a valid configuration");
26   }
27 }
28
29 void Configuration::add_event(const UnfoldingEvent* e)
30 {
31   if (e == nullptr) {
32     throw std::invalid_argument("Expected a nonnull `UnfoldingEvent*` but received NULL instead");
33   }
34
35   if (this->events_.contains(e)) {
36     return;
37   }
38
39   this->events_.insert(e);
40   this->newest_event = e;
41
42   // Preserves the property that the configuration is valid
43   History history(e);
44   if (!this->events_.contains(history)) {
45     throw std::invalid_argument("The newly added event has dependencies "
46                                 "which are missing from this configuration");
47   }
48 }
49
50 std::vector<const UnfoldingEvent*> Configuration::get_topologically_sorted_events() const
51 {
52   return this->events_.get_topological_ordering();
53 }
54
55 std::vector<const UnfoldingEvent*> Configuration::get_topologically_sorted_events_of_reverse_graph() const
56 {
57   return this->events_.get_topological_ordering_of_reverse_graph();
58 }
59
60 EventSet Configuration::get_minimally_reproducible_events() const
61 {
62   // The implementation exploits the following observations:
63   //
64   // To select the smallest reproducible set of events, we want
65   // to pick events that "knock out" a lot of others. Furthermore,
66   // we need to ensure that the events furthest down in the
67   // causality graph are also selected. If you combine these ideas,
68   // you're basically left with traversing the set of maximal
69   // subsets of C! And we have an iterator for that already!
70   //
71   // The next observation is that the moment we don't increase in size
72   // the current maximal set (or decrease the number of events),
73   // we know that the prior set `S` covered the entire history of C and
74   // was maximal. Subsequent sets will miss events earlier in the
75   // topological ordering that appear in `S`
76   EventSet minimally_reproducible_events = EventSet();
77
78   for (const auto& maximal_set : maximal_subsets_iterator_wrapper(*this)) {
79     if (maximal_set.size() > minimally_reproducible_events.size()) {
80       minimally_reproducible_events = maximal_set;
81     } else {
82       // The moment we see the iterator generate a set of size
83       // that is not monotonically increasing, we can stop:
84       // the set prior was the minimally-reproducible one
85       return minimally_reproducible_events;
86     }
87   }
88   return minimally_reproducible_events;
89 }
90
91 } // namespace simgrid::mc::udpor