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