Logo AND Algorithmique Numérique Distribuée

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