Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bfcec9cf5cf63d8af311874d480af69b97d5d191
[simgrid.git] / src / mc / udpor_global.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_UDPOR_GLOBAL_HPP
7 #define SIMGRID_MC_UDPOR_GLOBAL_HPP
8
9 #include "src/mc/api/State.hpp"
10
11 #include <iostream>
12 #include <optional>
13 #include <queue>
14 #include <set>
15 #include <string_view>
16
17 /* TODO: many method declared in this module are not implemented */
18
19 namespace simgrid::mc::udpor {
20
21 class UnfoldingEvent;
22 class Configuration;
23 using StateHandle = uint64_t;
24
25 class EventSet {
26 private:
27   std::set<UnfoldingEvent*> events_;
28   explicit EventSet(std::set<UnfoldingEvent*>&& raw_events) : events_(raw_events) {}
29
30 public:
31   EventSet()                           = default;
32   EventSet(const EventSet&)            = default;
33   EventSet& operator=(const EventSet&) = default;
34   EventSet& operator=(EventSet&&)      = default;
35   EventSet(EventSet&&)                 = default;
36
37   inline auto begin() const { return this->events_.begin(); }
38   inline auto end() const { return this->events_.end(); }
39
40   void remove(UnfoldingEvent* e);
41   void subtract(const EventSet& other);
42   void subtract(const Configuration& other);
43   EventSet subtracting(UnfoldingEvent* e) const;
44   EventSet subtracting(const EventSet& e) const;
45   EventSet subtracting(const Configuration* e) const;
46
47   void insert(UnfoldingEvent* e);
48   void form_union(const EventSet&);
49   void form_union(const Configuration&);
50   EventSet make_union(UnfoldingEvent* e) const;
51   EventSet make_union(const EventSet&) const;
52   EventSet make_union(const Configuration& e) const;
53
54   size_t size() const;
55   bool empty() const;
56   bool contains(UnfoldingEvent* e) const;
57   bool is_subset_of(const EventSet& other) const;
58
59   // // TODO: What is this used for?
60   // UnfoldingEvent* find(const UnfoldingEvent* e) const;
61
62   // // TODO: What is this used for
63   // bool depends(const EventSet& other) const;
64
65   // // TODO: What is this used for
66   // bool is_empty_intersection(EventSet evtS) const;
67 };
68
69 struct s_evset_in_t {
70   EventSet causuality_events;
71   EventSet cause;
72   EventSet ancestorSet;
73 };
74
75 class Configuration {
76 public:
77   Configuration()                                = default;
78   Configuration(const Configuration&)            = default;
79   Configuration& operator=(Configuration const&) = default;
80   Configuration(Configuration&&)                 = default;
81
82   // EventSet maxEvent;         // Events recently added to events_
83   // EventSet actorMaxEvent;    // maximal events of the actors in current configuration
84   // UnfoldingEvent* lastEvent; // The last added event
85
86   inline auto begin() const { return this->events_.begin(); }
87   inline auto end() const { return this->events_.end(); }
88   inline const EventSet& get_events() const { return this->events_; }
89   inline const EventSet& get_maxmimal_events() const { return this->max_events_; }
90
91   void add_event(UnfoldingEvent*);
92
93   // Configuration plus_config(UnfoldingEvent*) const;
94
95   // void createEvts(Configuration C, EventSet& result, const std::string& trans_tag, s_evset_in_t ev_sets, bool chk,
96   //                 UnfoldingEvent* immPreEvt);
97
98   // void updateMaxEvent(UnfoldingEvent*);         // update maximal events of the configuration and actors
99   // UnfoldingEvent* findActorMaxEvt(int actorId); // find maximal event of a Actor whose id = actorId
100
101   // UnfoldingEvent* findTestedComm(const UnfoldingEvent* testEvt); // find comm tested by action testTrans
102 private:
103   EventSet events_;
104
105   /**
106    * The <-maxmimal events of the configuration. These are
107    * dynamically adjusted as events are added to the configuration
108    *
109    * @invariant: Each event that is part of this set is
110    *
111    * 1. A <-maxmimal event of the configuration, in the sense that
112    * there is no event in the configuration that is "greater" than it
113    */
114   EventSet max_events_;
115
116 private:
117   void recompute_maxmimal_events(UnfoldingEvent* new_event);
118 };
119
120 class UnfoldingEvent {
121 public:
122   UnfoldingEvent(unsigned int nb_events, std::string const& trans_tag, EventSet const& causes, StateHandle sid);
123   UnfoldingEvent(const UnfoldingEvent&)            = default;
124   UnfoldingEvent& operator=(UnfoldingEvent const&) = default;
125   UnfoldingEvent(UnfoldingEvent&&)                 = default;
126
127   EventSet get_history() const;
128   bool in_history(const UnfoldingEvent* otherEvent) const;
129
130   // bool concernSameComm(const UnfoldingEvent* event, const UnfoldingEvent* otherEvent) const;
131
132   bool conflicts_with(const UnfoldingEvent* otherEvent) const;
133   bool conflicts_with(const Configuration& config) const;
134   bool immediately_conflicts_with(const UnfoldingEvent* otherEvt) const;
135
136   bool operator==(const UnfoldingEvent&) const { return false; };
137
138   void print() const;
139
140   inline StateHandle get_state_id() const { return state_id; }
141   inline void set_state_id(StateHandle sid) { state_id = sid; }
142
143   // inline std::string get_transition_tag() const { return transition_tag; }
144   // inline void set_transition_tag(std::string_view tr_tag) { transition_tag = tr_tag; }
145
146 private:
147   EventSet causes; // used to store directed ancestors of event e
148   int id = -1;
149   StateHandle state_id;
150   // std::string transition_tag{""}; // The tag of the last transition that lead to creating the event
151   // bool transition_is_IReceive(const UnfoldingEvent* testedEvt, const UnfoldingEvent* SdRcEvt) const;
152   // bool transition_is_ISend(const UnfoldingEvent* testedEvt, const UnfoldingEvent* SdRcEvt) const;
153   // bool check_tr_concern_same_comm(bool& chk1, bool& chk2, UnfoldingEvent* evt1, UnfoldingEvent* evt2) const;
154 };
155
156 class StateManager {
157 private:
158   using Handle = StateHandle;
159
160   Handle current_handle_ = 0ul;
161   std::unordered_map<Handle, std::unique_ptr<State>> state_map_;
162
163 public:
164   Handle record_state(std::unique_ptr<State>);
165   std::optional<std::reference_wrapper<State>> get_state(Handle);
166 };
167
168 } // namespace simgrid::mc::udpor
169 #endif