Logo AND Algorithmique Numérique Distribuée

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