1 /* Copyright (c) 2008-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 #include "src/mc/explo/odpor/WakeupTree.hpp"
7 #include "src/mc/explo/odpor/Execution.hpp"
8 #include "xbt/asserts.h"
14 namespace simgrid::mc::odpor {
16 aid_t WakeupTreeNode::get_first_actor() const
19 throw std::invalid_argument("Attempted to extract the first actor from "
20 "a node in a wakeup tree representing the "
21 "empty execution (likely the root node)");
23 return get_sequence().front()->aid_;
26 void WakeupTreeNode::add_child(WakeupTreeNode* node)
28 this->children_.push_back(node);
32 WakeupTreeNode::~WakeupTreeNode()
34 if (parent_ != nullptr) {
35 // TODO: We can probably be more clever here: when
36 // we add the child to a node, we could perhaps
37 // try instead to keep a reference to position of the
38 // child in the list of the parent.
39 parent_->children_.remove(this);
43 WakeupTree::WakeupTree() : WakeupTree(std::unique_ptr<WakeupTreeNode>(new WakeupTreeNode({}))) {}
44 WakeupTree::WakeupTree(std::unique_ptr<WakeupTreeNode> root) : root_(root.get())
46 this->insert_node(std::move(root));
49 WakeupTree WakeupTree::make_subtree_rooted_at(WakeupTreeNode* root)
51 if (not root->is_single_process()) {
52 throw std::invalid_argument("Selecting subtrees is only defined for single-process nodes");
55 const aid_t p = root->get_first_actor();
57 // Perform a BFS search to perform a deep copy of the portion
58 // of the tree underneath and including `root`. Note that `root`
59 // is contained within the context of a *different* wakeup tree;
60 // hence, we have to be careful to update each node's children
62 auto subtree = WakeupTree();
64 std::list<std::pair<WakeupTreeNode*, WakeupTreeNode*>> frontier{std::make_pair(root, subtree.root_)};
65 while (not frontier.empty()) {
66 auto [node_in_other_tree, subtree_equivalent] = frontier.front();
69 // For each child of the node corresponding to that in `subtree`,
70 // make clones of each of its children and add them to `frontier`
71 // to that their children are added, and so on. Note that the subtree
72 // **explicitly** removes the first process from each child
73 for (WakeupTreeNode* child_in_other_tree : node_in_other_tree->get_ordered_children()) {
74 auto p_w = child_in_other_tree->get_sequence();
75 const auto p_again = p_w.front()->aid_;
77 // INVARIANT: A nodes of a wakeup tree form a prefix-closed set;
78 // this means that any child node `c` of a node `n` must contain
79 // `n.get_sequence()` as a prefix of `c.get_sequence()`
80 xbt_assert(p_again == p,
81 "Invariant Violation: The wakeup tree from which a subtree with actor "
82 "`%ld` is being taken is not prefix free! The child node starts with "
83 "`%ld` while the parent is `%ld`! This indicates that there "
84 "is a bug in the insertion logic for the wakeup tree",
88 WakeupTreeNode* child_equivalent = subtree.make_node(p_w);
89 frontier.push_back(std::make_pair(child_in_other_tree, child_equivalent));
95 void WakeupTree::remove_subtree_rooted_at(WakeupTreeNode* root)
97 if (not contains(root)) {
98 throw std::invalid_argument("Attempting to remove a subtree pivoted from a node "
99 "that is not contained in this wakeup tree");
102 std::list<WakeupTreeNode*> subtree_contents{root};
103 std::list<WakeupTreeNode*> frontier{root};
104 while (not frontier.empty()) {
105 auto node = frontier.front();
106 frontier.pop_front();
107 for (const auto& child : node->get_ordered_children()) {
108 frontier.push_back(child);
109 subtree_contents.push_back(child);
113 // After having found each node with BFS, now we can
114 // remove them. This prevents the "joys" of iteration during mutation
115 for (WakeupTreeNode* node_to_remove : subtree_contents) {
116 this->remove_node(node_to_remove);
120 bool WakeupTree::contains(WakeupTreeNode* node) const
122 return std::find_if(this->nodes_.begin(), this->nodes_.end(), [=](const auto& pair) { return pair.first == node; }) !=
126 WakeupTreeNode* WakeupTree::make_node(const PartialExecution& u)
128 auto node = std::unique_ptr<WakeupTreeNode>(new WakeupTreeNode(u));
129 auto* node_handle = node.get();
130 this->nodes_[node_handle] = std::move(node);
134 void WakeupTree::insert_node(std::unique_ptr<WakeupTreeNode> node)
136 auto* node_handle = node.get();
137 this->nodes_[node_handle] = std::move(node);
140 void WakeupTree::remove_node(WakeupTreeNode* node)
142 this->nodes_.erase(node);
145 void WakeupTree::insert(const Execution& E, const PartialExecution& w)
147 // See section 6.2 of Abdulla. et al.'s 2017 ODPOR paper for details
149 // Find the first node `v` in the tree such that
150 // `v ~_[E] w` and `v` is not a leaf node
151 for (WakeupTreeNode* node : *this) {
152 if (const auto shortest_sequence = E.get_shortest_odpor_sq_subset_insertion(node->get_sequence(), w);
153 shortest_sequence.has_value()) {
154 // Insert the sequence as a child of `node`, but only
155 // if the node is not already a leaf
156 if (not node->is_leaf() or node == this->root_) {
157 WakeupTreeNode* new_node = this->make_node(shortest_sequence.value());
158 node->add_child(new_node);
160 // Since we're following the post-order traversal of the tree,
161 // the first such node we see is the smallest w.r.t "<"
167 } // namespace simgrid::mc::odpor