Logo AND Algorithmique Numérique Distribuée

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