Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'udpor-phase3' into 'master'
[simgrid.git] / src / mc / api / ActorState.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_PATTERN_H
7 #define SIMGRID_MC_PATTERN_H
8
9 #include "src/kernel/activity/CommImpl.hpp"
10 #include "src/mc/remote/RemotePtr.hpp"
11
12 #include <exception>
13 #include <vector>
14
15 namespace simgrid::mc {
16
17 /* On every state, each actor has an entry of the following type.
18  * This usually represents both the actor and its transition because
19  * most of the time an actor cannot have more than one enabled transition
20  * at a given time. However, certain transitions have multiple "paths"
21  * that can be followed, which means that a given actor may be able
22  * to do more than one thing at a time.
23  *
24  * Formally, at this state multiple transitions would exist all of
25  * which happened to be executed by the same actor. This distinction
26  * is important in cases
27  */
28 class ActorState {
29   /**
30    * @brief The transitions that the actor is allowed to execute from this
31    * state, viz. those that are enabled for this actor
32    *
33    * Most actors can take only a single action from any given state.
34    * However, when an actor executes a transition with multiple
35    * possible variations (e.g. an MC_Random() [see class: RandomTransition]
36    * for more details]), multiple enabled actions are defined
37    *
38    * @invariant The transitions are arranged such that an actor
39    * with multiple possible paths of execution will contain all
40    * such transitions such that `pending_transitions_[i]` represents
41    * the variation of the transition with `times_considered = i`.
42    *
43    * TODO: If only a subset of transitions of an actor that can
44    * take multiple transitions in some state are truly enabled,
45    * we would instead need to map `times_considered` to a transition,
46    * as the map is currently implicit in the ordering of the transitions
47    * in the vector
48    *
49    * TODO: If a single transition is taken at a time in a concurrent system,
50    * then nearly all of the transitions from in a state `s'` after taking
51    * an action `t` from state `s`  (i.e. s -- t --> s') are the same
52    * sans for the new transition of the actor which just executed t.
53    * This means there may be a way to store the list once and apply differences
54    * rather than repeating elements frequently.
55    */
56   std::vector<std::shared_ptr<Transition>> pending_transitions_;
57
58   /* Possible exploration status of an actor transition in a state.
59    * Either the checker did not consider the transition, or it was considered and still to do, or considered and
60    * done.
61    */
62   enum class InterleavingType {
63     /** This actor transition is not considered by the checker (yet?) */
64     disabled = 0,
65     /** The checker algorithm decided that this actor transitions should be done at some point */
66     todo,
67     /** The checker algorithm decided that this should be done, but it was done in the meanwhile */
68     done,
69   };
70
71   /** Exploration control information */
72   InterleavingType state_ = InterleavingType::disabled;
73
74   /** The ID of that actor */
75   const aid_t aid_;
76
77   /** Number of times that the actor was considered to be executed in previous explorations of the state space */
78   unsigned int times_considered_ = 0;
79   /** Maximal amount of times that the actor can be considered for execution in this state.
80    * If times_considered==max_consider, we fully explored that part of the state space */
81   unsigned int max_consider_ = 0;
82
83   /** Whether that actor is initially enabled in this state */
84   bool enabled_;
85
86 public:
87   ActorState(aid_t aid, bool enabled, unsigned int max_consider) : ActorState(aid, enabled, max_consider, {}) {}
88
89   ActorState(aid_t aid, bool enabled, unsigned int max_consider, std::vector<std::shared_ptr<Transition>> transitions)
90       : pending_transitions_(std::move(transitions)), aid_(aid), max_consider_(max_consider), enabled_(enabled)
91   {
92   }
93
94   unsigned int do_consider()
95   {
96     if (max_consider_ <= times_considered_ + 1)
97       mark_done();
98     return times_considered_++;
99   }
100   unsigned int get_times_considered() const { return times_considered_; }
101   aid_t get_aid() const { return aid_; }
102
103   /* returns whether the actor is marked as enabled in the application side */
104   bool is_enabled() const { return enabled_; }
105   /* returns whether the actor is marked as disabled by the exploration algorithm */
106   bool is_disabled() const { return this->state_ == InterleavingType::disabled; }
107   bool is_done() const { return this->state_ == InterleavingType::done; }
108   bool is_todo() const { return this->state_ == InterleavingType::todo; }
109   /** Mark that we should try executing this process at some point in the future of the checker algorithm */
110   void mark_todo()
111   {
112     this->state_            = InterleavingType::todo;
113     this->times_considered_ = 0;
114   }
115   void mark_done() { this->state_ = InterleavingType::done; }
116
117   inline Transition* get_transition(unsigned times_considered)
118   {
119     xbt_assert(times_considered < this->pending_transitions_.size(),
120                "Actor %ld does not have a state available transition with `times_considered = %u`,\n"
121                "yet one was asked for",
122                aid_, times_considered);
123     return this->pending_transitions_[times_considered].get();
124   }
125
126   inline void set_transition(std::unique_ptr<Transition> t, unsigned times_considered)
127   {
128     xbt_assert(times_considered < this->pending_transitions_.size(),
129                "Actor %ld does not have a state available transition with `times_considered = %u`, "
130                "yet one was attempted to be set",
131                aid_, times_considered);
132     this->pending_transitions_[times_considered] = std::move(t);
133   }
134
135   const std::vector<std::shared_ptr<Transition>>& get_enabled_transitions() const
136   {
137     return this->pending_transitions_;
138   };
139 };
140
141 } // namespace simgrid::mc
142
143 #endif