Logo AND Algorithmique Numérique Distribuée

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