Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Discard the wakeup tree when ODPOR reaches a disabled transition
[simgrid.git] / src / mc / explo / odpor / WakeupTree.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_ODPOR_WAKEUP_TREE_HPP
7 #define SIMGRID_MC_ODPOR_WAKEUP_TREE_HPP
8
9 #include "src/mc/explo/odpor/WakeupTreeIterator.hpp"
10 #include "src/mc/explo/odpor/odpor_forward.hpp"
11 #include "src/mc/transition/Transition.hpp"
12
13 #include <memory>
14 #include <optional>
15 #include <string>
16 #include <unordered_map>
17 #include <vector>
18
19 namespace simgrid::mc::odpor {
20
21 /**
22  * @brief A single node in a wakeup tree
23  *
24  * Each node in a wakeup tree represents a single step
25  * taken in an extension of the execution represented
26  * by the tree within which the node is contained. That is,
27  * a node in the tree is one step on a "pre-defined"
28  * path forward for some execution sequence. The partial
29  * execution that is implicitly represented by the node
30  * is that formed by taking each step on the (unique)
31  * path in the tree from the root node to this node.
32  * Thus, the tree itself contains all of the paths
33  * that "should be" searched, while each node is
34  * simply a step on each path.
35  */
36 class WakeupTreeNode {
37 private:
38   WakeupTreeNode* parent_ = nullptr;
39
40   /** An ordered list of children of for this node in the tree */
41   std::list<WakeupTreeNode*> children_;
42
43   /** @brief The contents of the node */
44   std::shared_ptr<Transition> action_;
45
46   /** @brief Removes the node as a child from the parent */
47   void detatch_from_parent();
48
49   /** Allows the owning tree to insert directly into the child */
50   friend WakeupTree;
51   friend WakeupTreeIterator;
52
53 public:
54   explicit WakeupTreeNode(std::shared_ptr<Transition> u) : action_(u) {}
55
56   WakeupTreeNode()                                 = default;
57   ~WakeupTreeNode()                                = default;
58   WakeupTreeNode(const WakeupTreeNode&)            = delete;
59   WakeupTreeNode(WakeupTreeNode&&)                 = default;
60   WakeupTreeNode& operator=(const WakeupTreeNode&) = delete;
61   WakeupTreeNode& operator=(WakeupTreeNode&&)      = default;
62
63   auto begin() const { return this->children_.begin(); }
64   auto end() const { return this->children_.end(); }
65   auto rbegin() const { return this->children_.rbegin(); }
66   auto rend() const { return this->children_.rend(); }
67
68   bool is_leaf() const { return children_.empty(); }
69   bool is_root() const { return parent_ == nullptr; }
70   aid_t get_actor() const { return action_->aid_; }
71   PartialExecution get_sequence() const;
72   std::shared_ptr<Transition> get_action() const { return action_; }
73   const std::list<WakeupTreeNode*>& get_ordered_children() const { return children_; }
74
75   std::string string_of_whole_tree(int indentation_level) const;
76
77   /** Insert a node `node` as a new child of this node */
78   void add_child(WakeupTreeNode* node);
79 };
80
81 /**
82  * @brief The structure used by ODPOR to maintains paths of execution
83  * that should be followed in the future
84  *
85  * The wakeup tree data structure is formally defined in the Abdulla et al.
86  * 2017 ODPOR paper. Conceptually, the tree consists of nodes which are
87  * mapped to actions. Each node represents a partial extension of an execution,
88  * the complete extension being the transitions taken in sequence from
89  * the root of the tree to the node itself. Leaf nodes in the tree conceptually,
90  * then, represent paths that are guaranteed to explore different parts
91  * of the search space.
92  *
93  * Iteration over a wakeup tree occurs as a post-order traversal of its nodes
94  *
95  * @note A wakeup tree is defined relative to some execution `E`. The
96  * structure itself does not hold onto a reference of the execution with
97  * respect to which it is a wakeup tree.
98  *
99  * @todo: If the idea of execution "views"  is ever added -- viz. being able
100  * to share the contents of a single execution -- then a wakeup tree could
101  * contain a reference to such a view which would then be maintained by the
102  * manipulator of the tree
103  */
104 class WakeupTree {
105 private:
106   WakeupTreeNode* root_;
107
108   /**
109    * @brief All of the nodes that are currently are a part of the tree
110    *
111    * @invariant Each node event maps itself to the owner of that node,
112    * i.e. the unique pointer that manages the data at the address. The tree owns all
113    * of the addresses that are referenced by the nodes WakeupTreeNode.
114    * ODPOR guarantees that nodes are persisted as long as needed.
115    */
116   std::unordered_map<WakeupTreeNode*, std::unique_ptr<WakeupTreeNode>> nodes_;
117
118   void insert_node(std::unique_ptr<WakeupTreeNode> node);
119   void insert_sequence_after(WakeupTreeNode* node, const PartialExecution& w);
120   void remove_node(WakeupTreeNode* node);
121   bool contains(const WakeupTreeNode* node) const;
122
123   /**
124    * @brief Removes the node `root` and all of its descendants from
125    * this wakeup tree
126    *
127    * @throws: If the node `root` is not contained in this tree, an
128    * exception is raised
129    */
130   void remove_subtree_rooted_at(WakeupTreeNode* root);
131
132   /**
133    * @brief Adds a new node to the tree, disconnected from
134    * any other, which represents the partial execution
135    * "fragment" `u`
136    */
137   WakeupTreeNode* make_node(std::shared_ptr<Transition> u);
138
139   /* Allow the iterator to access the contents of the tree */
140   friend WakeupTreeIterator;
141
142 public:
143   WakeupTree();
144   explicit WakeupTree(std::unique_ptr<WakeupTreeNode> root);
145
146   /**
147    * @brief Creates a copy of the subtree whose root is the node
148    * `root` in this tree
149    */
150   static WakeupTree make_subtree_rooted_at(WakeupTreeNode* root);
151
152   auto begin() const { return WakeupTreeIterator(*this); }
153   auto end() const { return WakeupTreeIterator(); }
154
155   std::vector<std::string> get_single_process_texts() const;
156
157   std::string string_of_whole_tree() const;
158
159   /**
160    * @brief Remove the subtree of the smallest (with respect
161    * to the tree's "<" relation) single-process node.
162    *
163    * A "single-process" node is one whose execution represents
164    * taking a single action (i.e. those of the root node). The
165    * smallest under "<" is that which is continuously selected and
166    * removed by ODPOR.
167    *
168    * If the tree is empty, this method has no effect.
169    */
170   void remove_min_single_process_subtree();
171
172   void remove_subtree_at_aid(aid_t proc);
173
174   /**
175    * @brief Whether or not this tree is considered empty
176    *
177    * @note Unlike other collection types, a wakeup tree is
178    * considered "empty" if it only contains the root node;
179    * that is, if it is "uninteresting". In such a case,
180    */
181   bool empty() const { return nodes_.size() == static_cast<size_t>(1); }
182
183   /**
184    * @brief Returns the number of *non-empty* entries in the tree, viz. the
185    * number of nodes in the tree that have an action mapped to them
186    */
187   size_t get_num_entries() const { return not empty() ? (nodes_.size() - 1) : static_cast<size_t>(0); }
188
189   /**
190    * @brief Returns the number of nodes in the tree, including the root node
191    */
192   size_t get_num_nodes() const { return nodes_.size(); }
193
194   /**
195    * @brief Gets the actor of the node that is the "smallest" (with respect
196    * to the tree's "<" relation) single-process node.
197    *
198    * If the tree is empty, returns std::nullopt
199    */
200   std::optional<aid_t> get_min_single_process_actor() const;
201
202   /**
203    * @brief Gets the node itself that is the "smallest" (with respect
204    * to the tree's "<" relation) single-process node.
205    *
206    * If the tree is empty, returns std::nullopt
207    */
208   std::optional<WakeupTreeNode*> get_min_single_process_node() const;
209
210   /** @brief Describes how a tree insertion was carried out */
211   enum class InsertionResult { leaf, interior_node, root };
212
213   /**
214    * @brief Inserts an sequence `seq` of processes into the tree
215    * such that that this tree is a wakeup tree relative to the
216    * given execution
217    *
218    * A key component of managing wakeup trees in ODPOR is
219    * determining what should be inserted into a wakeup tree.
220    * The procedure for implementing the insertion is outlined in section 6.2
221    * of Abdulla et al. 2017 as follows:
222    *
223    * | Let `v` be the smallest (w.r.t to "<") sequence in [the tree] B
224    * | such that `v ~_[E] w`. If `v` is a leaf node, the tree can be left
225    * | unmodified.
226    * |
227    * | Otherwise let `w'` be the shortest sequence such that `w [=_[E] v.w'`
228    * | and add `v.w'` as a new leaf, ordered after all already existing nodes
229    * | of the form `v.w''`
230    *
231    * This method performs the post-order search of part one and the insertion of
232    * `v.w'` of part two of the above procedure. Note that the execution will
233    * provide `v.w'` (see `Execution::get_shortest_odpor_sq_subset_insertion()`).
234    *
235    * @invariant: It is assumed that this tree is a wakeup tree
236    * with respect to the given execution `E`
237    *
238    * @return Whether a sequence equivalent to `seq` is already contained
239    * as a leaf node in the tree
240    */
241   InsertionResult insert(const Execution& E, const PartialExecution& seq);
242 };
243
244 } // namespace simgrid::mc::odpor
245 #endif