Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add initial outline of SDPOR implementation
[simgrid.git] / src / mc / api / State.hpp
1 /* Copyright (c) 2007-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 #ifndef SIMGRID_MC_STATE_HPP
7 #define SIMGRID_MC_STATE_HPP
8
9 #include "src/mc/api/ActorState.hpp"
10 #include "src/mc/api/ClockVector.hpp"
11 #include "src/mc/api/RemoteApp.hpp"
12 #include "src/mc/api/strategy/Strategy.hpp"
13 #include "src/mc/transition/Transition.hpp"
14
15 #if SIMGRID_HAVE_STATEFUL_MC
16 #include "src/mc/sosp/Snapshot.hpp"
17 #endif
18
19 namespace simgrid::mc {
20
21 /* A node in the exploration graph (kind-of) */
22 class XBT_PRIVATE State : public xbt::Extendable<State> {
23   static long expended_states_; /* Count total amount of states, for stats */
24
25   /** @brief The outgoing transition is the last transition that we took to leave this state.  */
26   std::shared_ptr<Transition> outgoing_transition_ = nullptr;
27
28   /** @brief The incoming transition is what led to this state, coming from its parent  */
29   std::shared_ptr<Transition> incoming_transition_ = nullptr;
30
31   /** Sequential state ID (used for debugging) */
32   long num_ = 0;
33
34   /** Snapshot of system state (if needed) */
35   std::shared_ptr<Snapshot> system_state_;
36
37   /** Unique parent of this state. Required both for sleep set computation
38       and for guided model-checking */
39   std::shared_ptr<State> parent_state_ = nullptr;
40
41   std::shared_ptr<Strategy> strategy_;
42
43   /* Sleep sets are composed of the actor and the corresponding transition that made it being added to the sleep
44    * set. With this information, it is check whether it should be removed from it or not when exploring a new
45    * transition */
46   std::map<aid_t, Transition> sleep_set_;
47
48 public:
49   explicit State(RemoteApp& remote_app);
50   explicit State(RemoteApp& remote_app, std::shared_ptr<State> parent_state);
51   /* Returns a positive number if there is another transition to pick, or -1 if not */
52   aid_t next_transition() const; // this function should disapear as it is redundant with the next one
53
54   /* Same as next_transition, but choice is now guided, and an integer corresponding to the
55    internal cost of the transition is returned */
56   std::pair<aid_t, int> next_transition_guided() const;
57
58   /**
59    * @brief Explore a new path on the remote app; the parameter 'next' must be the result of a previous call to
60    * next_transition()
61    */
62   std::shared_ptr<Transition> execute_next(aid_t next, RemoteApp& app);
63
64   long get_num() const { return num_; }
65   std::size_t count_todo() const;
66   std::size_t count_todo_multiples() const;
67
68   /* Marking as TODO some actor in this state:
69    *  + consider_one mark aid actor (and assert it is possible)
70    *  + consider_best ensure one actor is marked by eventually marking the best regarding its guiding methode
71    *  + conside_all mark all enabled actor that are not done yet */
72   void consider_one(aid_t aid) const { strategy_->consider_one(aid); }
73   void consider_best() const { strategy_->consider_best(); }
74   unsigned long consider_all() const { return strategy_->consider_all(); }
75
76   bool is_actor_done(aid_t actor) const { return strategy_->actors_to_run_.at(actor).is_done(); }
77   std::shared_ptr<Transition> get_transition_out() const { return outgoing_transition_; }
78   std::shared_ptr<Transition> get_transition_in() const { return incoming_transition_; }
79   std::shared_ptr<State> get_parent_state() const { return parent_state_; }
80
81   std::map<aid_t, ActorState> const& get_actors_list() const { return strategy_->actors_to_run_; }
82
83   unsigned long get_actor_count() const { return strategy_->actors_to_run_.size(); }
84   bool is_actor_enabled(aid_t actor) const { return strategy_->actors_to_run_.at(actor).is_enabled(); }
85
86   Snapshot* get_system_state() const { return system_state_.get(); }
87   void set_system_state(std::shared_ptr<Snapshot> state) { system_state_ = std::move(state); }
88
89   std::unordered_set<aid_t> get_todo_actors() const;
90   std::map<aid_t, Transition> const& get_sleep_set() const { return sleep_set_; }
91   void add_sleep_set(std::shared_ptr<Transition> t)
92   {
93     sleep_set_.insert_or_assign(t->aid_, Transition(t->type_, t->aid_, t->times_considered_));
94   }
95
96   /* Returns the total amount of states created so far (for statistics) */
97   static long get_expanded_states() { return expended_states_; }
98 };
99 } // namespace simgrid::mc
100
101 #endif