Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f45a0914ca03cedda6121f60d251f344d1345fd5
[simgrid.git] / src / mc / explo / odpor / WakeupTree_test.cpp
1 /* Copyright (c) 2017-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/3rd-party/catch.hpp"
7 #include "src/mc/explo/odpor/Execution.hpp"
8 #include "src/mc/explo/odpor/WakeupTree.hpp"
9 #include "src/mc/explo/udpor/udpor_tests_private.hpp"
10 #include "src/xbt/utils/iter/LazyPowerset.hpp"
11
12 using namespace simgrid::mc;
13 using namespace simgrid::mc::odpor;
14 using namespace simgrid::mc::udpor;
15
16 static void test_tree_iterator(const WakeupTree& tree, const std::vector<PartialExecution>& expected)
17 {
18   uint64_t num_nodes_traversed = 0;
19   auto tree_iter               = tree.begin();
20   for (auto expected_iter = expected.begin(); expected_iter != expected.end();
21        ++expected_iter, ++tree_iter, ++num_nodes_traversed) {
22     REQUIRE(tree_iter != tree.end());
23     REQUIRE((*tree_iter)->get_sequence() == *expected_iter);
24   }
25   REQUIRE(num_nodes_traversed == tree.get_num_nodes());
26 }
27
28 static void test_tree_empty(const WakeupTree& tree)
29 {
30   REQUIRE(tree.empty());
31   REQUIRE(tree.get_num_entries() == 0);
32   REQUIRE(tree.get_num_nodes() == 1);
33   REQUIRE_FALSE(tree.get_min_single_process_node().has_value());
34   REQUIRE_FALSE(tree.get_min_single_process_actor().has_value());
35   test_tree_iterator(tree, std::vector<PartialExecution>{PartialExecution{}});
36 }
37
38 TEST_CASE("simgrid::mc::odpor::WakeupTree: Constructing Trees")
39 {
40   SECTION("Constructing empty trees")
41   {
42     test_tree_empty(WakeupTree());
43   }
44
45   SECTION("Testing subtree creation and manipulation")
46   {
47     // Here, we make everything dependent. This will ensure that each unique sequence
48     // inserted into the tree never "eventually looks like"
49     const auto a0 = std::make_shared<DependentAction>(Transition::Type::UNKNOWN, 1);
50     const auto a1 = std::make_shared<DependentAction>(Transition::Type::UNKNOWN, 2);
51     const auto a2 = std::make_shared<DependentAction>(Transition::Type::UNKNOWN, 3);
52     const auto a3 = std::make_shared<DependentAction>(Transition::Type::UNKNOWN, 4);
53     const auto a4 = std::make_shared<DependentAction>(Transition::Type::UNKNOWN, 5);
54     const auto a5 = std::make_shared<DependentAction>(Transition::Type::UNKNOWN, 6);
55
56     Execution execution;
57     execution.push_transition(a0);
58     execution.push_transition(a1);
59     execution.push_transition(a2);
60     execution.push_transition(a3);
61     execution.push_transition(a4);
62     execution.push_transition(a5);
63
64     // The tree is as follows:
65     //                  {}
66     //               /     /
67     //             a1       a4
68     //           /    /       /
69     //          a2    a3       a1
70     //         /     /   /      /
71     //        a3    a2   a5     a2
72     //       /     /             /
73     //      a4    a4             a3
74     //
75     // Recall that new nodes (in this case the one with
76     // action `a2`) are added such that they are "greater than" (under
77     // the tree's `<` relation) all those that exist under the given parent
78     WakeupTree tree;
79     tree.insert(Execution(), {a1, a2, a3, a4});
80     tree.insert(Execution(), {a1, a3, a2, a4});
81     tree.insert(Execution(), {a1, a3, a2, a4, a5});
82     tree.insert(Execution(), {a1, a3, a5});
83     tree.insert(Execution(), {a4, a2, a1, a3});
84     REQUIRE(tree.get_num_nodes() == 13);
85     test_tree_iterator(tree, std::vector<PartialExecution>{
86                                  PartialExecution{a1, a2, a3, a4}, PartialExecution{a1, a2, a3},
87                                  PartialExecution{a1, a2}, PartialExecution{a1, a3, a2, a4},
88                                  PartialExecution{a1, a3, a2}, PartialExecution{a1, a3, a5}, PartialExecution{a1, a3},
89                                  PartialExecution{a1}, PartialExecution{a4, a2, a1, a3}, PartialExecution{a4, a2, a1},
90                                  PartialExecution{a4, a2}, PartialExecution{a4}, PartialExecution{}});
91
92     SECTION("Cloning a tree from the root produces the same tree")
93     {
94       // The root node is the last node
95       auto tree_root = tree.begin();
96       std::advance(tree_root, tree.get_num_nodes() - 1);
97
98       WakeupTree clone = WakeupTree::make_subtree_rooted_at(*tree_root);
99       REQUIRE(clone.empty() == tree.empty());
100       REQUIRE(clone.get_num_entries() == tree.get_num_entries());
101       REQUIRE(clone.get_num_nodes() == tree.get_num_nodes());
102
103       auto tree_iter = tree.begin();
104       for (auto clone_iter = clone.begin(); clone_iter != clone.end(); ++clone_iter, ++tree_iter) {
105         REQUIRE(tree_iter != tree.end());
106         REQUIRE((*tree_iter)->get_sequence() == (*clone_iter)->get_sequence());
107       }
108     }
109
110     SECTION("Cloning a subtree from a leaf gives an empty tree")
111     {
112       // Let's pick the first leaf
113       WakeupTree clone = WakeupTree::make_subtree_rooted_at(*tree.begin());
114       REQUIRE(clone.empty());
115       REQUIRE(clone.get_num_entries() == 0);
116       REQUIRE(clone.get_num_nodes() == 1);
117     }
118
119     SECTION("Cloning a subtree from an interior node gives the subtree underneath")
120     {
121       // Here, we pick the second-to-last node in the
122       // series, which is the right-most child of the root
123       auto right_most = tree.begin();
124       std::advance(right_most, tree.get_num_nodes() - 2);
125
126       WakeupTree clone = WakeupTree::make_subtree_rooted_at(*right_most);
127       REQUIRE_FALSE(clone.empty());
128       REQUIRE(clone.get_num_entries() == 3);
129       REQUIRE(clone.get_num_nodes() == 4);
130       // Importantly, note that action `a4` is not included in
131       // any of the executions; for in the subtree `clone` `a4`
132       // is now the root.
133       test_tree_iterator(clone, std::vector<PartialExecution>{PartialExecution{a2, a1, a3}, PartialExecution{a2, a1},
134                                                               PartialExecution{a2}, PartialExecution{}});
135     }
136
137     SECTION("Removing the first single-process subtree")
138     {
139       // Prior to removal, the first `a1` was the first single-process node
140       REQUIRE(tree.get_min_single_process_node().has_value());
141       REQUIRE(tree.get_min_single_process_actor().has_value());
142       REQUIRE(tree.get_min_single_process_actor().value() == a1->aid_);
143
144       tree.remove_min_single_process_subtree();
145
146       // Now the first `a4` is
147       REQUIRE(tree.get_min_single_process_node().has_value());
148       REQUIRE(tree.get_min_single_process_actor().has_value());
149       REQUIRE(tree.get_min_single_process_actor().value() == a4->aid_);
150
151       REQUIRE(tree.get_num_nodes() == 5);
152       test_tree_iterator(tree, std::vector<PartialExecution>{PartialExecution{a4, a2, a1, a3},
153                                                              PartialExecution{a4, a2, a1}, PartialExecution{a4, a2},
154                                                              PartialExecution{a4}, PartialExecution{}});
155       tree.remove_min_single_process_subtree();
156
157       // At this point, we've removed each single-process subtree, so
158       // the tree should be empty
159       REQUIRE(tree.empty());
160     }
161
162     SECTION("Removing the first single-process subtree from an empty tree has no effect")
163     {
164       WakeupTree empty_tree;
165       test_tree_empty(empty_tree);
166
167       empty_tree.remove_min_single_process_subtree();
168
169       // There should be no effect: the tree should still be empty
170       // and the function should have no effect
171       test_tree_empty(empty_tree);
172     }
173   }
174 }
175
176 TEST_CASE("simgrid::mc::odpor::WakeupTree: Testing Insertion for Empty Executions")
177 {
178   SECTION("Following an execution")
179   {
180     // We imagine the following completed execution `E`
181     // consisting of executing actions a0-a3. Execution
182     // `E` is the first such maximal execution explored
183     // by ODPOR, which implies that a) all sleep sets are
184     // empty and b) all wakeup trees (i.e. for each prefix) consist of the root
185     // node with a single leaf containing the action
186     // taken, save for the wakeup tree of the execution itself
187     // which is empty.
188
189     // We first notice that there's a reversible race between
190     // events 0 and 3.
191
192     const auto a0 = std::make_shared<DependentAction>(Transition::Type::UNKNOWN, 3);
193     const auto a1 = std::make_shared<IndependentAction>(Transition::Type::UNKNOWN, 4);
194     const auto a2 = std::make_shared<ConditionallyDependentAction>(Transition::Type::UNKNOWN, 1);
195     const auto a3 = std::make_shared<ConditionallyDependentAction>(Transition::Type::UNKNOWN, 4);
196
197     Execution execution;
198     execution.push_transition(a0);
199     execution.push_transition(a1);
200     execution.push_transition(a2);
201     execution.push_transition(a3);
202
203     REQUIRE(execution.get_racing_events_of(2) == std::unordered_set<Execution::EventHandle>{0});
204     REQUIRE(execution.get_racing_events_of(3) == std::unordered_set<Execution::EventHandle>{0});
205
206     WakeupTree tree;
207
208     SECTION("Attempting to insert the empty sequence into an empty tree should have no effect")
209     {
210       tree.insert(Execution(), {});
211       test_tree_iterator(tree, std::vector<PartialExecution>{PartialExecution{}});
212     }
213
214     // First, we initialize the tree to how it looked prior
215     // to the insertion of the race.
216     tree.insert(Execution(), {a0});
217
218     // Then, after insertion, we ensure that the node was
219     // indeed added to the tree.
220     tree.insert(Execution(), {a1, a3});
221     test_tree_iterator(tree, std::vector<PartialExecution>{PartialExecution{a0}, PartialExecution{a1, a3},
222                                                            PartialExecution{a1}, PartialExecution{}});
223
224     SECTION("Attempting to re-insert the same EXACT sequence should have no effect")
225     {
226       tree.insert(Execution(), {a1, a3});
227       test_tree_iterator(tree, std::vector<PartialExecution>{PartialExecution{a0}, PartialExecution{a1, a3},
228                                                              PartialExecution{a1}, PartialExecution{}});
229     }
230
231     SECTION("Attempting to re-insert an equivalent sequence should have no effect")
232     {
233       // a3 and a1 are interchangeable since `a1` is independent with everything.
234       // Since we found an equivalent sequence that is a leaf, nothing should result..
235       tree.insert(Execution(), {a3, a1});
236       test_tree_iterator(tree, std::vector<PartialExecution>{PartialExecution{a0}, PartialExecution{a1, a3},
237                                                              PartialExecution{a1}, PartialExecution{}});
238     }
239
240     SECTION("Attempting to insert the empty sequence should have no effect")
241     {
242       tree.insert(Execution(), {});
243       test_tree_iterator(tree, std::vector<PartialExecution>{PartialExecution{a0}, PartialExecution{a1, a3},
244                                                              PartialExecution{a1}, PartialExecution{}});
245     }
246
247     SECTION("Inserting an extension should create a branch point")
248     {
249       // `a1.a2` shares the same `a1` prefix as `a1.a3`. Thus, the tree
250       // should now look as follows:
251       //
252       //                 {}
253       //               /    /
254       //             a0     a1
255       //                   /   /
256       //                  a3   a2
257       //
258       // Recall that new nodes (in this case the one with
259       // action `a2`) are added such that they are "greater than" (under
260       // the tree's `<` relation) all those that exist under the given parent.
261       tree.insert(Execution(), {a1, a2});
262       test_tree_iterator(tree, std::vector<PartialExecution>{PartialExecution{a0}, PartialExecution{a1, a3},
263                                                              PartialExecution{a1, a2}, PartialExecution{a1},
264                                                              PartialExecution{}});
265     }
266   }
267
268   SECTION("Performing Arbitrary Insertions")
269   {
270     const auto a0 = std::make_shared<DependentAction>(Transition::Type::UNKNOWN, 2);
271     const auto a1 = std::make_shared<IndependentAction>(Transition::Type::UNKNOWN, 4);
272     const auto a2 = std::make_shared<ConditionallyDependentAction>(Transition::Type::UNKNOWN, 3);
273     const auto a3 = std::make_shared<DependentAction>(Transition::Type::UNKNOWN, 1);
274     const auto a4 = std::make_shared<IndependentAction>(Transition::Type::UNKNOWN, 2);
275     const auto a5 = std::make_shared<ConditionallyDependentAction>(Transition::Type::UNKNOWN, 4);
276     WakeupTree tree;
277
278     SECTION("Attempting to insert the empty sequence into an empty tree should have no effect")
279     {
280       tree.insert(Execution(), {});
281       test_tree_iterator(tree, std::vector<PartialExecution>{PartialExecution{}});
282     }
283
284     SECTION("Attempting to re-insert the same sequence multiple times should have no extra effect")
285     {
286       tree.insert(Execution(), {a4});
287       tree.insert(Execution(), {a4});
288       tree.insert(Execution(), {a4});
289       REQUIRE(tree.get_num_nodes() == 2);
290       test_tree_iterator(tree, std::vector<PartialExecution>{PartialExecution{a4}, PartialExecution{}});
291     }
292
293     SECTION("Attempting to insert an independent sequence same should have no extra effect")
294     {
295       // a4 and a1 are independent actions. Intuitively, then, we need only
296       // search one ordering of the two actions. The wakeup tree handles
297       // this by computing the `~` relation. The relation itself determines
298       // whether the `a1` is an initial of `a3`, which it is not. It then
299       // checks whether `a1` is independent with everything in the sequence
300       // (in this case, consisting only of `a1`) which IS true. Since `a4`
301       // is already a leaf node of the tree, it suffices to only have `a4`
302       // in this tree to guide ODPOR.
303       tree.insert(Execution(), {a4});
304       tree.insert(Execution(), {a1});
305       REQUIRE(tree.get_num_nodes() == 2);
306       test_tree_iterator(tree, std::vector<PartialExecution>{PartialExecution{a4}, PartialExecution{}});
307     }
308
309     SECTION(
310         "Attempting to insert a progression of executions should have no extra effect when the first process is a leaf")
311     {
312       // All progressions starting with `a0` are effectively already accounted
313       // for by inserting `a0` since we `a0` "can always be made to look like"
314       // (viz. the `~` relation) `a0.*` where `*` is some sequence of actions
315       tree.insert(Execution(), {a0});
316       tree.insert(Execution(), {a0, a3});
317       tree.insert(Execution(), {a0, a3, a2});
318       tree.insert(Execution(), {a0, a3, a2, a4});
319       tree.insert(Execution(), {a0, a3, a2, a4});
320       REQUIRE(tree.get_num_nodes() == 2);
321       test_tree_iterator(tree, std::vector<PartialExecution>{PartialExecution{a0}, PartialExecution{}});
322     }
323
324     SECTION("Stress test with multiple branch points: `~_E` with different looking sequences")
325     {
326       // After the insertions below, the tree looks like the following:
327       //                {}
328       //              /    /
329       //            a0     a2
330       //                 /  |   /
331       //               a0  a3   a5
332       tree.insert(Execution(), {a0});
333       tree.insert(Execution(), {a2, a0});
334       tree.insert(Execution(), {a2, a3});
335       tree.insert(Execution(), {a2, a5});
336       REQUIRE(tree.get_num_nodes() == 6);
337       test_tree_iterator(tree, std::vector<PartialExecution>{PartialExecution{a0}, PartialExecution{a2, a0},
338                                                              PartialExecution{a2, a3}, PartialExecution{a2, a5},
339                                                              PartialExecution{a2}, PartialExecution{}});
340       SECTION("Adding more stress")
341       {
342         // In this case, `a2` and `a1` can be interchanged with each other.
343         // Thus `a2.a1 == a1.a2`. Since there is already an interior node
344         // containing `a2`, we attempt to add the what remains (viz. `a1`) to the
345         // series. HOWEVER: we notice that `a2.a5` is "eventually equivalent to"
346         // (that is `~` with) `a1.a2` since `a2` is an initial of the latter and
347         // `a1` and `a5` are independent of each other.
348         tree.insert(Execution(), {a1, a2});
349         REQUIRE(tree.get_num_nodes() == 6);
350         test_tree_iterator(tree, std::vector<PartialExecution>{PartialExecution{a0}, PartialExecution{a2, a0},
351                                                                PartialExecution{a2, a3}, PartialExecution{a2, a5},
352                                                                PartialExecution{a2}, PartialExecution{}});
353
354         // With a3.a0, we notice that starting a sequence with `a3` is
355         // always different than starting one with either `a0` or
356         //
357         // After the insertion, the tree looks like the following:
358         //                     {}
359         //              /     /        /
360         //            a0     a2        a3
361         //                 /  |  /     |
362         //               a0  a3  a5    a0
363         tree.insert(Execution(), {a3, a0});
364         REQUIRE(tree.get_num_nodes() == 8);
365         test_tree_iterator(tree, std::vector<PartialExecution>{PartialExecution{a0}, PartialExecution{a2, a0},
366                                                                PartialExecution{a2, a3}, PartialExecution{a2, a5},
367                                                                PartialExecution{a2}, PartialExecution{a3, a0},
368                                                                PartialExecution{a3}, PartialExecution{}});
369       }
370     }
371   }
372
373   SECTION("Insertion with non-obvious equivalent leaf")
374   {
375     const auto cd_1 = std::make_shared<ConditionallyDependentAction>(Transition::Type::UNKNOWN, 1);
376     const auto i_2  = std::make_shared<IndependentAction>(Transition::Type::UNKNOWN, 2);
377     const auto i_3  = std::make_shared<IndependentAction>(Transition::Type::UNKNOWN, 3);
378     const auto d_1  = std::make_shared<DependentAction>(Transition::Type::UNKNOWN, 1);
379     const auto d_2  = std::make_shared<DependentAction>(Transition::Type::UNKNOWN, 2);
380     WakeupTree complex_tree;
381     // After the insertions below, the tree looks like the following:
382     //                {}
383     //              /    /
384     //            a0     a2
385     //                 /  |  /
386     //               a0  a3   a5
387     complex_tree.insert(Execution(), {cd_1, i_2, d_1, i_3, d_2, d_2});
388     complex_tree.insert(Execution(), {i_2, d_2, cd_1, d_1, i_3, d_2});
389     complex_tree.insert(Execution(), {i_2, d_2, cd_1, i_3, d_2, d_1});
390     REQUIRE(complex_tree.get_num_nodes() == 16);
391   }
392 }