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"
9 #include "xbt/string.hpp"
15 namespace simgrid::mc::odpor {
17 void WakeupTreeNode::add_child(WakeupTreeNode* node)
19 this->children_.push_back(node);
23 PartialExecution WakeupTreeNode::get_sequence() const
25 // TODO: Prevent having to compute this at the node level
26 // and instead track this with the iterator
27 PartialExecution seq_;
28 const WakeupTreeNode* cur_node = this;
29 while (cur_node != nullptr and !cur_node->is_root()) {
30 seq_.push_front(cur_node->action_);
31 cur_node = cur_node->parent_;
36 void WakeupTreeNode::detatch_from_parent()
38 if (parent_ != nullptr) {
39 // TODO: There may be a better method
40 // of keeping track of a node's reference to
41 // its parent, perhaps keeping track
42 // of a std::list<>::iterator instead.
43 // This would allow us to detach a node
44 // in O(1) instead of O(|children|) time
45 parent_->children_.remove(this);
49 WakeupTree::WakeupTree() : WakeupTree(std::unique_ptr<WakeupTreeNode>(new WakeupTreeNode({}))) {}
50 WakeupTree::WakeupTree(std::unique_ptr<WakeupTreeNode> root) : root_(root.get())
52 this->insert_node(std::move(root));
55 std::vector<std::string> WakeupTree::get_single_process_texts() const
57 std::vector<std::string> trace;
58 for (const auto* child : root_->children_) {
59 const auto t = child->get_action();
60 const auto message = xbt::string_printf("Actor %ld: %s", t->aid_, t->to_string(true).c_str());
61 trace.push_back(std::move(message));
66 std::optional<aid_t> WakeupTree::get_min_single_process_actor() const
68 if (const auto node = get_min_single_process_node(); node.has_value()) {
69 return node.value()->get_actor();
74 std::optional<WakeupTreeNode*> WakeupTree::get_min_single_process_node() const
79 // INVARIANT: The induced post-order relation always places children
80 // in order before the parent. The list of children maintained by
81 // each node represents that ordering, and the first child of
82 // the root is by definition the smallest of the single-process nodes
83 xbt_assert(not this->root_->children_.empty(), "What the");
84 return this->root_->children_.front();
87 WakeupTree WakeupTree::make_subtree_rooted_at(WakeupTreeNode* root)
89 // Perform a BFS search to perform a deep copy of the portion
90 // of the tree underneath and including `root`. Note that `root`
91 // is contained within the context of a *different* wakeup tree;
92 // hence, we have to be careful to update each node's children
94 auto subtree = WakeupTree();
96 std::list<std::pair<WakeupTreeNode*, WakeupTreeNode*>> frontier{std::make_pair(root, subtree.root_)};
97 while (not frontier.empty()) {
98 auto [node_in_other_tree, subtree_equivalent] = frontier.front();
101 // For each child of the node corresponding to that in `subtree`,
102 // make clones of each of its children and add them to `frontier`
103 // to that their children are added, and so on.
104 for (WakeupTreeNode* child_in_other_tree : node_in_other_tree->get_ordered_children()) {
105 WakeupTreeNode* child_equivalent = subtree.make_node(child_in_other_tree->get_action());
106 subtree_equivalent->add_child(child_equivalent);
107 frontier.push_back(std::make_pair(child_in_other_tree, child_equivalent));
113 void WakeupTree::remove_subtree_rooted_at(WakeupTreeNode* root)
115 if (not contains(root)) {
116 throw std::invalid_argument("Attempting to remove a subtree pivoted from a node "
117 "that is not contained in this wakeup tree");
120 std::list<WakeupTreeNode*> subtree_contents{root};
121 std::list<WakeupTreeNode*> frontier{root};
122 while (not frontier.empty()) {
123 auto node = frontier.front();
124 frontier.pop_front();
125 for (const auto& child : node->get_ordered_children()) {
126 frontier.push_back(child);
127 subtree_contents.push_back(child);
131 // After having found each node with BFS, now we can
132 // remove them. This prevents the "joys" of iteration during mutation.
133 // We also remove the `root` from being referenced by its own parent (since
134 // it will soon be destroyed)
135 root->detatch_from_parent();
136 for (WakeupTreeNode* node_to_remove : subtree_contents) {
137 this->remove_node(node_to_remove);
141 void WakeupTree::remove_min_single_process_subtree()
143 if (const auto node = get_min_single_process_node(); node.has_value()) {
144 remove_subtree_rooted_at(node.value());
148 bool WakeupTree::contains(WakeupTreeNode* node) const
150 return std::find_if(this->nodes_.begin(), this->nodes_.end(), [=](const auto& pair) { return pair.first == node; }) !=
154 WakeupTreeNode* WakeupTree::make_node(std::shared_ptr<Transition> u)
156 auto node = std::unique_ptr<WakeupTreeNode>(new WakeupTreeNode(std::move(u)));
157 auto* node_handle = node.get();
158 this->nodes_[node_handle] = std::move(node);
162 void WakeupTree::insert_node(std::unique_ptr<WakeupTreeNode> node)
164 auto* node_handle = node.get();
165 this->nodes_[node_handle] = std::move(node);
168 void WakeupTree::remove_node(WakeupTreeNode* node)
170 this->nodes_.erase(node);
173 WakeupTree::InsertionResult WakeupTree::insert(const Execution& E, const PartialExecution& w)
175 // See section 6.2 of Abdulla. et al.'s 2017 ODPOR paper for details
177 // Find the first node `v` in the tree such that
178 // `v ~_[E] w` and `v` is not a leaf node
179 for (WakeupTreeNode* node : *this) {
180 if (const auto shortest_sequence = E.get_shortest_odpor_sq_subset_insertion(node->get_sequence(), w);
181 shortest_sequence.has_value()) {
182 // Insert the sequence as a child of `node`, but only
183 // if the node is not already a leaf
184 if (not node->is_leaf() or node == this->root_) {
185 // NOTE: It's entirely possible that the shortest
186 // sequence we are inserting is empty. Consider the
187 // following two cases:
189 // 1. `w` is itself empty. Evidently, insertion succeeds but nothing needs
192 // 2. a leaf node in the tree already contains `w` exactly.
193 // In this case, the empty `w'` returned (viz. `shortest_seq`)
194 // such that `w [=_[E] v.w'` would be empty
195 this->insert_sequence_after(node, shortest_sequence.value());
196 return node == this->root_ ? InsertionResult::root : InsertionResult::interior_node;
198 // Since we're following the post-order traversal of the tree,
199 // the first such node we see is the smallest w.r.t "<"
200 return InsertionResult::leaf;
203 xbt_die("Insertion should always succeed with the root node (which contains no "
204 "prior execution). If we've reached this point, this implies either that "
205 "the wakeup tree traversal is broken or that computation of the shortest "
206 "sequence to insert into the tree is broken");
209 void WakeupTree::insert_sequence_after(WakeupTreeNode* node, const PartialExecution& w)
211 WakeupTreeNode* cur_node = node;
212 for (const auto& w_i : w) {
213 WakeupTreeNode* new_node = this->make_node(w_i);
214 cur_node->add_child(new_node);
219 } // namespace simgrid::mc::odpor