Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix compilation error with clang 11.0.1.
[simgrid.git] / src / mc / explo / udpor / maximal_subsets_iterator.hpp
1 /* Copyright (c) 2007-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 #ifndef SIMGRID_MC_UDPOR_MAXIMAL_SUBSETS_ITERATOR_HPP
7 #define SIMGRID_MC_UDPOR_MAXIMAL_SUBSETS_ITERATOR_HPP
8
9 #include "src/mc/explo/udpor/Configuration.hpp"
10
11 #include <boost/iterator/iterator_facade.hpp>
12 #include <optional>
13 #include <stack>
14 #include <unordered_map>
15
16 namespace simgrid::mc::udpor {
17
18 /**
19  * @brief An iterator over the tree of sets of (non-empty) maximal events that
20  * can be generated from a given configuration
21  *
22  * This iterator traverses all possible sets of maximal events that
23  * can be formed from a configuration, each of which satisfy a predicate.
24  *
25  * Iteration over the maximal events of a configuration is an important
26  * step in computing the extension set of a configuration for an action
27  * whose identity is not "exploitable" (i.e. one whose type information cannot
28  * help us narrow down our search).
29  */
30 struct maximal_subsets_iterator
31     : public boost::iterator_facade<maximal_subsets_iterator, const EventSet, boost::forward_traversal_tag> {
32 public:
33   // A function which answers the question "do I need to consider maximal sets
34   // that contain this node?"
35   using node_filter_function       = std::function<bool(const UnfoldingEvent*)>;
36   using topological_order_position = std::vector<const UnfoldingEvent*>::const_iterator;
37
38   maximal_subsets_iterator() = default;
39   explicit maximal_subsets_iterator(const Configuration& config) : maximal_subsets_iterator(config, std::nullopt) {}
40   maximal_subsets_iterator(const Configuration& config, std::optional<node_filter_function> filter);
41
42 private:
43   const std::optional<std::reference_wrapper<const Configuration>> config = std::nullopt;
44   std::vector<const UnfoldingEvent*> topological_ordering;
45
46   // The boolean is a bit of an annoyance, but it works. Effectively,
47   // there's no way to distinguish between "we're starting the search
48   // after the empty set" and "we've finished the search" since the resulting
49   // maximal set and backtracking point stack will both be empty in both cases
50   bool has_started_searching                              = false;
51   std::optional<EventSet> current_maximal_set             = std::nullopt;
52   std::stack<topological_order_position> backtrack_points = std::stack<topological_order_position>();
53
54   /**
55    * @brief A small class which provides functionality for managing
56    * the "counts" as the iterator proceeds forward in time
57    *
58    * As an instance of the `maximal_subsets_iterator` traverses
59    * the configuration, it keeps track of how many events
60    * further down in the causality tree have been signaled as in-conflict
61    * with events that are its current maximal event set (i.e.
62    * its `current_maximal_set`)
63    */
64   struct bookkeeper {
65   public:
66     using topological_order_position = maximal_subsets_iterator::topological_order_position;
67
68     void mark_included_in_maximal_set(const UnfoldingEvent*);
69     void mark_removed_from_maximal_set(const UnfoldingEvent*);
70     topological_order_position find_next_candidate_event(topological_order_position first,
71                                                          topological_order_position last) const;
72
73   private:
74     std::unordered_map<const UnfoldingEvent*, unsigned> event_counts;
75
76     /// @brief Whether or not the given event, according to the
77     /// bookkeeping that has been done thus far, can be added to the
78     /// current candidate maximal set
79     bool is_candidate_event(const UnfoldingEvent*) const;
80   } bookkeeper;
81
82   void add_element_to_current_maximal_set(const UnfoldingEvent*);
83   void remove_element_from_current_maximal_set(const UnfoldingEvent*);
84
85   /**
86    * @brief Moves to the next node in the topological ordering
87    * by continuing the search in the tree of maximal event sets
88    * from where we currently believe we are in the tree
89    *
90    * At each stage of the iteration, the iterator points to
91    * a maximal event set that can be thought of as `R` + `A`:
92    *
93    * |   R    | A
94    * +--------+
95    *
96    * where `R` is some set of events and `A` is another event.
97    *
98    * The iterator first tries expansion from `R` + `A`. If it finds
99    * node `B` to expand, this means that there is a node in the tree of
100    * maximal event sets of `C` (the configuration traversed) such that
101    * `R` + `A` + `B` needs to be checked.
102    *
103    * If no such node is found, then the iterator must check `R` +
104    * some other node AFTER `A`. The new set of possibilities potentially
105    * includes some of `A`'s dependencies, so their counts are decremented
106    * prior to searching.
107    *
108    * @note: This method is a mutating method: it manipulates the
109    * iterator such that the iterator refers to the next maximal
110    * set sans the element returned. The `increment()` function performs
111    * the rest of the work needed to actually complete the transition
112    *
113    * @returns an iterator poiting to the event that should next
114    * be added to the set of maximal events if such an event exists,
115    * or to the end of the topological ordering if no such event exists
116    */
117   topological_order_position continue_traversal_of_maximal_events_tree();
118
119   // boost::iterator_facade<...> interface to implement
120   void increment();
121   bool equal(const maximal_subsets_iterator& other) const { return current_maximal_set == other.current_maximal_set; }
122   const EventSet& dereference() const
123   {
124     static const EventSet empty_set = EventSet();
125     if (current_maximal_set.has_value()) {
126       return current_maximal_set.value();
127     }
128     return empty_set;
129   }
130
131   // Allows boost::iterator_facade<...> to function properly
132   friend class boost::iterator_core_access;
133 };
134
135 } // namespace simgrid::mc::udpor
136 #endif