Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7b73e6c2b24bf6a118385dc100a9ceb5904fb928
[simgrid.git] / src / mc / explo / odpor / WakeupTree.cpp
1 /* Copyright (c) 2008-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 #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"
10
11 #include <algorithm>
12 #include <exception>
13 #include <queue>
14
15 namespace simgrid::mc::odpor {
16
17 void WakeupTreeNode::add_child(WakeupTreeNode* node)
18 {
19   this->children_.push_back(node);
20   node->parent_ = this;
21 }
22
23 PartialExecution WakeupTreeNode::get_sequence() const
24 {
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_;
32   }
33   return seq_;
34 }
35
36 void WakeupTreeNode::detatch_from_parent()
37 {
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);
46   }
47 }
48
49 WakeupTree::WakeupTree() : WakeupTree(std::unique_ptr<WakeupTreeNode>(new WakeupTreeNode({}))) {}
50 WakeupTree::WakeupTree(std::unique_ptr<WakeupTreeNode> root) : root_(root.get())
51 {
52   this->insert_node(std::move(root));
53 }
54
55 std::vector<std::string> WakeupTree::get_single_process_texts() const
56 {
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));
62   }
63   return trace;
64 }
65
66 std::optional<aid_t> WakeupTree::get_min_single_process_actor() const
67 {
68   if (const auto node = get_min_single_process_node(); node.has_value()) {
69     return node.value()->get_actor();
70   }
71   return std::nullopt;
72 }
73
74 std::optional<WakeupTreeNode*> WakeupTree::get_min_single_process_node() const
75 {
76   if (empty()) {
77     return std::nullopt;
78   }
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();
85 }
86
87 WakeupTree WakeupTree::make_subtree_rooted_at(WakeupTreeNode* root)
88 {
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
93   // appropriately
94   auto subtree = WakeupTree();
95
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();
99     frontier.pop_front();
100
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));
108     }
109   }
110   return subtree;
111 }
112
113 void WakeupTree::remove_subtree_rooted_at(WakeupTreeNode* root)
114 {
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");
118   }
119
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);
128     }
129   }
130
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);
138   }
139 }
140
141 void WakeupTree::remove_min_single_process_subtree()
142 {
143   if (const auto node = get_min_single_process_node(); node.has_value()) {
144     remove_subtree_rooted_at(node.value());
145   }
146 }
147
148 bool WakeupTree::contains(WakeupTreeNode* node) const
149 {
150   return std::find_if(this->nodes_.begin(), this->nodes_.end(), [=](const auto& pair) { return pair.first == node; }) !=
151          this->nodes_.end();
152 }
153
154 WakeupTreeNode* WakeupTree::make_node(std::shared_ptr<Transition> u)
155 {
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);
159   return node_handle;
160 }
161
162 void WakeupTree::insert_node(std::unique_ptr<WakeupTreeNode> node)
163 {
164   auto* node_handle         = node.get();
165   this->nodes_[node_handle] = std::move(node);
166 }
167
168 void WakeupTree::remove_node(WakeupTreeNode* node)
169 {
170   this->nodes_.erase(node);
171 }
172
173 WakeupTree::InsertionResult WakeupTree::insert(const Execution& E, const PartialExecution& w)
174 {
175   // See section 6.2 of Abdulla. et al.'s 2017 ODPOR paper for details
176
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:
188         //
189         // 1. `w` is itself empty. Evidently, insertion succeeds but nothing needs
190         // to happen
191         //
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;
197       }
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;
201     }
202   }
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");
207 }
208
209 void WakeupTree::insert_sequence_after(WakeupTreeNode* node, const PartialExecution& w)
210 {
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);
215     cur_node = new_node;
216   }
217 }
218
219 } // namespace simgrid::mc::odpor