1 /* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_MC_ODPOR_WAKEUP_TREE_HPP
7 #define SIMGRID_MC_ODPOR_WAKEUP_TREE_HPP
9 #include "src/mc/explo/odpor/WakeupTreeIterator.hpp"
10 #include "src/mc/explo/odpor/odpor_forward.hpp"
14 #include <unordered_map>
16 namespace simgrid::mc::odpor {
18 class WakeupTreeNode {
20 explicit WakeupTreeNode(const PartialExecution& u) : seq_(u) {}
21 explicit WakeupTreeNode(PartialExecution&& u) : seq_(std::move(u)) {}
23 WakeupTreeNode* parent_ = nullptr;
25 /** An ordered list of children of for this node in the tree */
26 std::list<WakeupTreeNode*> children_;
28 /** @brief The contents of the node */
29 PartialExecution seq_;
31 /** Allows the owning tree to insert directly into the child */
33 friend WakeupTreeIterator;
37 WakeupTreeNode(const WakeupTreeNode&) = delete;
38 WakeupTreeNode(WakeupTreeNode&&) = default;
39 WakeupTreeNode& operator=(const WakeupTreeNode&) = delete;
40 WakeupTreeNode& operator=(WakeupTreeNode&&) = default;
42 const auto begin() const { return this->children_.begin(); }
43 const auto end() const { return this->children_.end(); }
44 const auto rbegin() const { return this->children_.rbegin(); }
45 const auto rend() const { return this->children_.rend(); }
47 const PartialExecution& get_sequence() const { return seq_; }
48 const std::list<WakeupTreeNode*>& get_ordered_children() const { return children_; }
49 bool is_leaf() const { return children_.empty(); }
50 bool is_single_process() const { return seq_.size() == static_cast<size_t>(1); }
51 aid_t get_first_actor() const;
53 /** Insert a node `node` as a new child of this node */
54 void add_child(WakeupTreeNode* node);
59 WakeupTreeNode* root_;
62 * @brief All of the nodes that are currently are a part of the tree
64 * @invariant Each node event maps itself to the owner of that node,
65 * i.e. the unique pointer that manages the data at the address. The tree owns all
66 * of the addresses that are referenced by the nodes WakeupTreeNode.
67 * ODPOR guarantees that nodes are persisted as long as needed.
69 std::unordered_map<WakeupTreeNode*, std::unique_ptr<WakeupTreeNode>> nodes_;
71 void insert_node(std::unique_ptr<WakeupTreeNode> node);
72 void remove_node(WakeupTreeNode* node);
73 bool contains(WakeupTreeNode* node) const;
76 * @brief Adds a new node to the tree, disconnected from
77 * any other, which represents the partial execution
80 WakeupTreeNode* make_node(const PartialExecution& u);
82 /* Allow the iterator to access the contents of the tree */
83 friend WakeupTreeIterator;
87 explicit WakeupTree(std::unique_ptr<WakeupTreeNode> root);
89 auto begin() const { return WakeupTreeIterator(*this); }
90 auto end() const { return WakeupTreeIterator(); }
92 void remove_subtree_rooted_at(WakeupTreeNode* root);
93 static WakeupTree make_subtree_rooted_at(WakeupTreeNode* root);
96 * @brief Whether or not this tree is considered empty
98 * @note Unlike other collection types, a wakeup tree is
99 * considered "empty" if it only contains the root node;
100 * that is, if it is "uninteresting". In such a case,
102 bool empty() const { return nodes_.size() == static_cast<size_t>(1); }
105 * @brief Inserts an sequence `seq` of processes into the tree
106 * such that that this tree is a wakeup tree relative to the
109 * A key component of managing wakeup trees in ODPOR is
110 * determining what should be inserted into a wakeup tree.
111 * The procedure for implementing the insertion is outlined in section 6.2
112 * of Abdulla et al. 2017 as follows:
114 * | Let `v` be the smallest (w.r.t to "<") sequence in [the tree] B
115 * | such that `v ~_[E] w`. If `v` is a leaf node, the tree can be left
118 * | Otherwise let `w'` be the shortest sequence such that `w [=_[E] v.w'`
119 * | and add `v.w'` as a new leaf, ordered after all already existing nodes
120 * | of the form `v.w''`
122 * This method performs the postorder search of part one and the insertion of
123 * `v.w'` of part two of the above procedure. Note that the execution will
124 * provide `v.w'` (see `Execution::get_shortest_odpor_sq_subset_insertion()`).
126 * @invariant: It is assumed that this tree is a wakeup tree
127 * with respect to the given execution `E`
129 void insert(const Execution& E, const PartialExecution& seq);
132 } // namespace simgrid::mc::odpor