1 #include "src/mc/explo/udpor/maximal_subsets_iterator.hpp"
2 #include "src/mc/explo/udpor/UnfoldingEvent.hpp"
3 #include "xbt/asserts.h"
7 namespace simgrid::mc::udpor {
9 maximal_subsets_iterator::maximal_subsets_iterator(const EventSet& events, std::optional<node_filter_function> filter,
10 std::optional<size_t> maximum_subset_size)
11 : maximum_subset_size(maximum_subset_size), current_maximal_set({EventSet()})
13 const auto candidate_ordering = events.get_topological_ordering_of_reverse_graph();
14 if (filter.has_value()) {
15 // Only store the events in the ordering that "matter" to us
16 std::copy_if(std::move_iterator(candidate_ordering.begin()), std::move_iterator(candidate_ordering.end()),
17 std::back_inserter(topological_ordering), filter.value());
19 topological_ordering = std::move(candidate_ordering);
23 void maximal_subsets_iterator::increment()
25 // Termination condition
26 if (current_maximal_set == std::nullopt) {
30 // Stop immediately if there's nothing to search
31 if (topological_ordering.empty()) {
32 current_maximal_set = std::nullopt;
36 const auto next_event_ref = [&]() {
37 if (!has_started_searching) {
38 has_started_searching = true;
39 return bookkeeper.find_next_candidate_event(topological_ordering.begin(), topological_ordering.end());
41 return continue_traversal_of_maximal_events_tree();
45 // Out of events: we've finished
46 if (next_event_ref == topological_ordering.end()) {
47 current_maximal_set = std::nullopt;
51 // We found some other event `e'` which is not in causally related with anything
52 // that currently exists in `current_maximal_set`, so add it in
53 add_element_to_current_maximal_set(*next_event_ref);
54 backtrack_points.push(next_event_ref);
57 maximal_subsets_iterator::topological_order_position
58 maximal_subsets_iterator::continue_traversal_of_maximal_events_tree()
60 // Nothing needs to be done if there isn't anyone to search for...
61 if (backtrack_points.empty()) {
62 return topological_ordering.end();
65 xbt_assert(current_maximal_set.has_value(), "Traversal continued even after the termination condition "
66 "was met. Please verify that the termination condition "
67 "of the iterator has not been modified");
69 // 1. First, check if we can keep expanding from the
70 // maximal set that we currently have
71 if (can_grow_maximal_set()) {
72 // This is an iterator which points to the latest event `e` that
73 // was added to what is currently the maximal set
74 const auto latest_event_ref = backtrack_points.top();
76 // Look for the next event to test with what we currently
77 // have based on the conflicts we've already kept track of.
79 // NOTE: We only need to search FROM `e` and not
80 // from the beginning of the topological sort. The fact that the
81 // set is topologically ordered ensures that removing `e`
82 // will not change whether or not to now allow someone before `e`
83 // in the ordering (otherwise, they would have to be in `e`'s history
84 // and therefore would come after `e`)
85 const auto next_event_ref = bookkeeper.find_next_candidate_event(latest_event_ref, topological_ordering.end());
87 // If we can expand from what we currently have, we can stop
88 if (next_event_ref != topological_ordering.end()) {
89 return next_event_ref;
93 // Otherwise, we backtrack: we repeatedly pop off events that we know we
95 while (not backtrack_points.empty()) {
96 // Note: it is important to remove the element FIRST before performing
97 // the search, as removal may enable dependencies of `e` to be selected
98 const auto latest_event_ref = backtrack_points.top();
99 remove_element_from_current_maximal_set(*latest_event_ref);
100 backtrack_points.pop();
102 // We begin the search AFTER the event we popped: we only want
103 // to consider those events that could be added AFTER `e` and
104 // not `e` itself again
105 const auto next_event_ref = bookkeeper.find_next_candidate_event(latest_event_ref + 1, topological_ordering.end());
106 if (next_event_ref != topological_ordering.end()) {
107 return next_event_ref;
110 return topological_ordering.end();
113 bool maximal_subsets_iterator::bookkeeper::is_candidate_event(const UnfoldingEvent* e) const
115 if (const auto e_count = event_counts.find(e); e_count != event_counts.end()) {
116 return e_count->second == 0;
121 void maximal_subsets_iterator::add_element_to_current_maximal_set(const UnfoldingEvent* e)
123 xbt_assert(can_grow_maximal_set(), "Attempting to add an event to the maximal set "
124 "when doing so would increase the size past the "
125 "prescribed limit. This indicates that detecting when "
126 "to stop growing the maximal set when continuing the "
128 xbt_assert(current_maximal_set.has_value(), "Attempting to add an event to the maximal set "
129 "when iteration has completed. This indicates that "
130 "the termination condition for the iterator is broken");
131 current_maximal_set.value().insert(e);
132 bookkeeper.mark_included_in_maximal_set(e);
135 void maximal_subsets_iterator::remove_element_from_current_maximal_set(const UnfoldingEvent* e)
137 xbt_assert(current_maximal_set.has_value(), "Attempting to remove an event to the maximal set "
138 "when iteration has completed. This indicates that "
139 "the termination condition for the iterator is broken");
140 current_maximal_set.value().remove(e);
141 bookkeeper.mark_removed_from_maximal_set(e);
144 bool maximal_subsets_iterator::can_grow_maximal_set() const
146 if (not current_maximal_set.has_value()) {
149 if (maximum_subset_size.has_value()) {
150 return current_maximal_set.value().size() < maximum_subset_size.value();
155 maximal_subsets_iterator::topological_order_position
156 maximal_subsets_iterator::bookkeeper::find_next_candidate_event(topological_order_position first,
157 topological_order_position last) const
159 return std::find_if(first, last, [&](const UnfoldingEvent* e) { return is_candidate_event(e); });
162 void maximal_subsets_iterator::bookkeeper::mark_included_in_maximal_set(const UnfoldingEvent* e)
164 const auto e_history = e->get_history();
165 for (const auto e_hist : e_history) {
166 event_counts[e_hist]++;
170 void maximal_subsets_iterator::bookkeeper::mark_removed_from_maximal_set(const UnfoldingEvent* e)
172 const auto e_history = e->get_history();
173 for (const auto e_hist : e_history) {
174 xbt_assert(event_counts.find(e_hist) != event_counts.end(),
175 "Invariant Violation: Attempted to remove an event which was not previously added");
176 xbt_assert(event_counts[e_hist] > 0, "Invariant Violation: An event `e` had a count of `0` at this point "
177 "of the bookkeeping, which means that it is a candidate maximal event. "
178 "Yet some event that `e'` which contains `e` in its history was removed "
179 "first. This incidates that the topological sorting of events of the "
180 "configuration has failed and should be investigated first");
181 event_counts[e_hist]--;
185 } // namespace simgrid::mc::udpor