Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'udpor-phase5' into 'master'
[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   // 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");
44   }
45
46   this->events_.insert(e);
47   this->newest_event = e;
48
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");
53   }
54 }
55
56 std::vector<const UnfoldingEvent*> Configuration::get_topologically_sorted_events() const
57 {
58   return this->events_.get_topological_ordering();
59 }
60
61 std::vector<const UnfoldingEvent*> Configuration::get_topologically_sorted_events_of_reverse_graph() const
62 {
63   return this->events_.get_topological_ordering_of_reverse_graph();
64 }
65
66 EventSet Configuration::get_minimally_reproducible_events() const
67 {
68   // The implementation exploits the following observations:
69   //
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!
76   //
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();
83
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;
87     } else {
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;
92     }
93   }
94   return minimally_reproducible_events;
95 }
96
97 } // namespace simgrid::mc::udpor