Logo AND Algorithmique Numérique Distribuée

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