Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use forwarding references with std::forward (sonar).
[simgrid.git] / src / mc / explo / udpor / Unfolding.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_UNFOLDING_HPP
7 #define SIMGRID_MC_UDPOR_UNFOLDING_HPP
8
9 #include "src/mc/explo/udpor/EventSet.hpp"
10 #include "src/mc/explo/udpor/UnfoldingEvent.hpp"
11 #include "src/mc/explo/udpor/udpor_forward.hpp"
12
13 #include <memory>
14 #include <unordered_map>
15
16 namespace simgrid::mc::udpor {
17
18 class Unfolding {
19 public:
20   Unfolding()                       = default;
21   Unfolding& operator=(Unfolding&&) = default;
22   Unfolding(Unfolding&&)            = default;
23
24   auto begin() const { return this->event_handles.begin(); }
25   auto end() const { return this->event_handles.end(); }
26   auto cbegin() const { return this->event_handles.cbegin(); }
27   auto cend() const { return this->event_handles.cend(); }
28   size_t size() const { return this->event_handles.size(); }
29   bool empty() const { return this->event_handles.empty(); }
30
31   /**
32    * @brief Moves an event from UDPOR's global set `U` to
33    * the global set `G`
34    */
35   void mark_finished(const UnfoldingEvent* e);
36
37   /**
38    * @brief Moves all events in a set from UDPOR's global
39    * set `U` to the global set `G`
40    */
41   void mark_finished(const EventSet& events);
42
43   /// @brief Adds a new event `e` to the Unfolding if that
44   /// event is not equivalent to any of those already contained
45   /// in the unfolding
46   const UnfoldingEvent* insert(std::unique_ptr<UnfoldingEvent> e);
47
48   /**
49    * @brief Informs the unfolding of a (potentially) new event
50    *
51    * The unfolding of a concurrent program is a well-defined
52    * structure. Given the labeled transition system (LTS) of
53    * a program, the unfolding of that program can be determined
54    * algorithmically. However, UDPOR does not a priori know the structure of the
55    * unfolding as it performs its exploration. Thus, events in the
56    * unfolding are "discovered" as they are encountered, specifically
57    * when computing the extension sets of the configurations that
58    * UDPOR decides to search.
59    *
60    * This lends itself to the following problem: the extension sets
61    * of two different configurations may overlap one another. That
62    * is, for two configurations C and C' explored by UDPOR where C != C',
63    *
64    * ex(C) - ex(C') != empty
65    *
66    * Hence, when extending both `C` and `C'`, any events contained in
67    * the intersection of ex(C) and ex(C') will be attempted to be added
68    * twice. The unfolding will notice that these events have already
69    * been added and simply return the event already added to the unfolding
70    *
71    * @tparam ...Args arguments passed to the `UnfoldingEvent` constructor
72    * @return the handle to either the newly created event OR
73    * to an equivalent event that was already noted by the unfolding
74    * at some point in the past
75    */
76   template <typename... Args> const UnfoldingEvent* discover_event(Args&&... args)
77   {
78     auto candidate_event = std::make_unique<UnfoldingEvent>(std::forward<Args>(args)...);
79     return insert(std::move(candidate_event));
80   }
81
82   /// @brief Computes "#ⁱ_U(e)" for the given event, where `U` is the set
83   /// of the events in this unfolding
84   EventSet get_immediate_conflicts_of(const UnfoldingEvent*) const;
85
86 private:
87   /**
88    * @brief All of the events that are currently are a part of the unfolding
89    *
90    * @invariant Each unfolding event maps itself to the owner of that event,
91    * i.e. the unique pointer that manages the data at the address. The Unfolding owns all
92    * of the addresses that are referenced by EventSet instances and Configuration
93    * instances. UDPOR guarantees that events are persisted for as long as necessary
94    */
95   std::unordered_map<const UnfoldingEvent*, std::unique_ptr<UnfoldingEvent>> global_events_;
96
97   /**
98    * @brief: The collection of events in the unfolding
99    *
100    * @invariant: All of the events in this set are elements of `global_events_`
101    * and is kept updated at the same time as `global_events_`
102    *
103    * @note: This is for the convenience of iteration over the unfolding
104    */
105   EventSet event_handles;
106
107   /**
108    * @brief: The collection of events in the unfolding that are "important"
109    */
110   EventSet U;
111
112   /**
113    * @brief The "irrelevant" portions of the unfolding that do not need to be kept
114    * around to ensure that UDPOR functions correctly
115    *
116    * The set `G` is another global variable maintained by the UDPOR algorithm which
117    * is used to keep track of all events which used to be important to UDPOR.
118    */
119   EventSet G;
120 };
121
122 } // namespace simgrid::mc::udpor
123 #endif