Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into 'master'
[simgrid.git] / src / mc / explo / udpor / Configuration.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_CONFIGURATION_HPP
7 #define SIMGRID_MC_UDPOR_CONFIGURATION_HPP
8
9 #include "src/mc/explo/udpor/EventSet.hpp"
10 #include "src/mc/explo/udpor/udpor_forward.hpp"
11
12 #include <initializer_list>
13 #include <vector>
14
15 namespace simgrid::mc::udpor {
16
17 class Configuration {
18 public:
19   Configuration()                                = default;
20   Configuration(const Configuration&)            = default;
21   Configuration& operator=(Configuration const&) = default;
22   Configuration(Configuration&&)                 = default;
23
24   Configuration(EventSet events);
25   Configuration(std::initializer_list<UnfoldingEvent*> events);
26
27   auto begin() const { return this->events_.begin(); }
28   auto end() const { return this->events_.end(); }
29
30   bool contains(UnfoldingEvent* e) const { return this->events_.contains(e); }
31   const EventSet& get_events() const { return this->events_; }
32   UnfoldingEvent* get_latest_event() const { return this->newest_event; }
33
34   /**
35    * @brief Insert a new event into the configuration
36    *
37    * When the newly added event is inserted into the configuration,
38    * an assertion is made to ensure that the configuration remains
39    * valid, i.e. that the newly added event's dependencies are contained
40    * within the configuration.
41    *
42    * @param e the event to add to the configuration. If the event is
43    * already a part of the configuration, calling this method has no
44    * effect.
45    *
46    * @throws an invalid argument exception is raised should the event
47    * be missing one of its dependencies
48    *
49    * @note: UDPOR technically enforces the invariant that all newly-added events
50    * will ensure that the configuration is valid. We perform extra checks to ensure
51    * that UDPOR is implemented as expected. There is a performance penalty that
52    * should be noted: checking for maximality requires ensuring that all events in the
53    * configuration have their dependencies containes within the configuration, which
54    * essentially means performing a BFS/DFS over the configuration using a History object.
55    * However, since the slowest part of UDPOR involves enumerating all
56    * subsets of maximal events and computing k-partial alternatives (the
57    * latter definitively an NP-hard problem when optimal), Amdahl's law suggests
58    * we shouldn't focus so much on this (let alone the additional benefit of the
59    * assertions)
60    */
61   void add_event(UnfoldingEvent* e);
62
63   /**
64    * @brief Orders the events of the configuration such that
65    * "more recent" events (i.e. those that are farther down in
66    * the event structure's dependency chain) come after those
67    * that appeared "farther in the past"
68    *
69    * @returns a vector `V` with the following property:
70    *
71    * 1. Let i(e) := C -> I map events to their indices in `V`.
72    * For every pair of events e, e' in C, if e < e' then i(e) < i(e')
73    *
74    * Intuitively, events that are closer to the "bottom" of the event
75    * structure appear farther along in the list than those that appear
76    * closer to the "top"
77    */
78   std::vector<UnfoldingEvent*> get_topologically_sorted_events() const;
79
80   /**
81    * @brief Orders the events of the configuration such that
82    * "more recent" events (i.e. those that are farther down in
83    * the event structure's dependency chain) come before those
84    * that appear "farther in the past"
85    *
86    * @note The events of the event structure are arranged such that
87    * e < e' implies a directed edge from e to e'. However, it is
88    * also useful to be able to traverse the *reverse* graph (for
89    * example when computing the compatibility graph of a configuration),
90    * hence the distinction between "reversed" and the method
91    * "Configuration::get_topologically_sorted_events()"
92    *
93    * @returns a vector `V` with the following property:
94    *
95    * 1. Let i(e) := C -> I map events to their indices in `V`.
96    * For every pair of events e, e' in C, if e < e' then i(e) > i(e')
97    *
98    * Intuitively, events that are closer to the "top" of the event
99    * structure appear farther along in the list than those that appear
100    * closer to the "bottom"
101    */
102   std::vector<UnfoldingEvent*> get_topologically_sorted_events_of_reverse_graph() const;
103
104 private:
105   /**
106    * @brief The most recent event added to the configuration
107    */
108   UnfoldingEvent* newest_event = nullptr;
109
110   /**
111    * @brief The events which make up this configuration
112    *
113    * @invariant For each event `e` in `events_`, the set of
114    * dependencies of `e` is also contained in `events_`
115    */
116   EventSet events_;
117 };
118
119 } // namespace simgrid::mc::udpor
120 #endif