Logo AND Algorithmique Numérique Distribuée

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