Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix few typos and add random seed intializer
[simgrid.git] / src / mc / explo / UdporChecker.hpp
1 /* Copyright (c) 2007-2023. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef SIMGRID_MC_UDPOR_CHECKER_HPP
8 #define SIMGRID_MC_UDPOR_CHECKER_HPP
9
10 #include "src/mc/explo/Exploration.hpp"
11 #include "src/mc/explo/udpor/Configuration.hpp"
12 #include "src/mc/explo/udpor/EventSet.hpp"
13 #include "src/mc/explo/udpor/Unfolding.hpp"
14 #include "src/mc/explo/udpor/UnfoldingEvent.hpp"
15 #include "src/mc/mc_record.hpp"
16
17 #include <functional>
18 #include <list>
19 #include <optional>
20
21 namespace simgrid::mc::udpor {
22
23 /**
24  * @brief Performs exploration of a concurrent system via the
25  * UDPOR algorithm
26  *
27  * The `UdporChecker` implementation is based primarily off three papers,
28  * herein referred to as [1], [2], and [3] respectively, as well as the
29  * current implementation of `tiny_simgrid`:
30  *
31  * 1. "Unfolding-based Partial Order Reduction" by Rodriguez et al.
32  * 2. "Quasi-Optimal Partial Order Reduction" by Nguyen et al.
33  * 3. The Anh Pham's Thesis "Exploration efficace de l'espace ..."
34  */
35 class XBT_PRIVATE UdporChecker : public Exploration {
36 public:
37   explicit UdporChecker(const std::vector<char*>& args);
38
39   void run() override;
40   RecordTrace get_record_trace() override;
41   std::unique_ptr<State> get_current_state() { return std::make_unique<State>(get_remote_app()); }
42
43 private:
44   Unfolding unfolding = Unfolding();
45
46   // The current sequence of states that the checker has
47   // visited in order to reach the current configuration
48   std::list<std::unique_ptr<State>> state_stack;
49
50   /**
51    * @brief Explores the unfolding of the concurrent system
52    * represented by the ModelChecker instance "mcmodel_checker"
53    *
54    * This function performs the actual search following the
55    * UDPOR algorithm according to [1].
56    *
57    * @param C the current configuration from which UDPOR will be used
58    * to explore expansions of the concurrent system being modeled
59    * @param D the set of events that should not be considered by UDPOR
60    * while performing its searches, in order to avoid sleep-set blocked
61    * executions. See [1] for more details
62    * @param A the set of events to "guide" UDPOR in the correct direction
63    * when it returns back to a node in the unfolding and must decide among
64    * events to select from `ex(C)`. See [1] for more details
65    *
66    * TODO: Add the optimization where we can check if e == e_prior
67    * to prevent repeated work when computing ex(C)
68    */
69   void explore(const Configuration& C, EventSet D, EventSet A, EventSet prev_exC);
70
71   /**
72    * @brief Identifies the next event from the unfolding of the concurrent system
73    * that should next be explored as an extension of a configuration with
74    * enabled events `enC`
75    *
76    * @param A The set of events `A` maintained by the UDPOR algorithm to help
77    * determine how events should be selected. See the original paper [1] for more details
78    *
79    * @param enC The set `enC` of enabled events from the extension set `exC` used
80    * by the UDPOR algorithm to select new events to search. See the original
81    * paper [1] for more details
82    */
83   UnfoldingEvent* select_next_unfolding_event(const EventSet& A, const EventSet& enC);
84
85   /**
86    * @brief Computes the sets `ex(C)` and `en(C)` of the given configuration
87    * `C` as an incremental computation from the the previous computation of `ex(C)`
88    *
89    * A central component to UDPOR is the computation of the set `ex(C)`. The
90    * extension set `ex(C)` of a configuration `C` is defined as the set of events
91    * outside of `C` whose full dependency chain is contained in `C` (see [1]
92    * for more details).
93    *
94    * In general, computing `ex(C)` is very expensive. In paper [3], The Anh Pham
95    * shows a method of incremental computation of the set `ex(C)` under the
96    * conclusions afforded under the computation model in consideration, of which
97    * SimGrid is apart, which allow for `ex(C)` to be computed much more efficiently.
98    * Intuitively, the idea is to take advantage of the fact that you can avoid a lot
99    * of repeated computation by exploiting the aforementioned properties (in [3]) in
100    * what is akin to a dynamic programming optimization. See [3] for more details
101    *
102    * @param C the configuration based on which the two sets `ex(C)` and `en(C)` are
103    * computed
104    * @param stateC the state of the program after having executed C (viz. `state(C)`)
105    * @param prev_exC the previous value of `ex(C)`, viz. that which was computed for
106    * the configuration `C' := C - {e}`
107    * @returns the extension set `ex(C)` of `C`
108    */
109   EventSet compute_exC(const Configuration& C, const State& stateC, const EventSet& prev_exC);
110   EventSet compute_enC(const Configuration& C, const EventSet& exC) const;
111
112   /**
113    *
114    */
115   void move_to_stateCe(State* stateC, UnfoldingEvent* e);
116
117   /**
118    * @brief Creates a new snapshot of the state of the application
119    * as it currently looks
120    */
121   std::unique_ptr<State> record_current_state();
122
123   /**
124    * @brief Move the application side into the state at the top of the
125    * state stack provided
126    *
127    * As UDPOR performs its search, it moves the application-side along with
128    * it so that the application is always providing the checker with
129    * the correct information about what each actor is running (and whether
130    * those actors are enabled) at the state reached by the configuration it
131    * decides to search.
132    *
133    * When UDPOR decides to "backtrack" (e.g. after reaching a configuration
134    * whose en(C) is empty), before it attempts to continue the search by taking
135    * a different path from a configuration it visited in the past, it must ensure
136    * that the application-side has moved back into `state(C)`.
137    *
138    * The search may have moved the application arbitrarily deep into its execution,
139    * and the search may backtrack arbitrarily closer to the beginning of the execution.
140    * The UDPOR implementation in SimGrid ensures that the stack is updated appropriately,
141    * but the process must still be regenerated.
142    */
143   void restore_program_state_with_current_stack();
144
145   /**
146    * @brief Perform the functionality of the `Remove(e, C, D)` function in [1]
147    */
148   void clean_up_explore(const UnfoldingEvent* e, const Configuration& C, const EventSet& D);
149 };
150 } // namespace simgrid::mc::udpor
151
152 #endif