Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add topological sort of configuration events
[simgrid.git] / src / mc / explo / udpor / Configuration.hpp
index 18c5c6f..eb365d5 100644 (file)
@@ -9,9 +9,10 @@
 #include "src/mc/explo/udpor/EventSet.hpp"
 #include "src/mc/explo/udpor/udpor_forward.hpp"
 
-namespace simgrid::mc::udpor {
+#include <initializer_list>
+#include <vector>
 
-using StateHandle = uint64_t;
+namespace simgrid::mc::udpor {
 
 class Configuration {
 public:
@@ -20,94 +21,63 @@ public:
   Configuration& operator=(Configuration const&) = default;
   Configuration(Configuration&&)                 = default;
 
-  inline auto begin() const { return this->events_.begin(); }
-  inline auto end() const { return this->events_.end(); }
-  inline const EventSet& get_events() const { return this->events_; }
-  inline const EventSet& get_maximal_events() const { return this->max_events_; }
+  Configuration(EventSet events);
+  Configuration(std::initializer_list<UnfoldingEvent*> events);
 
-  void add_event(UnfoldingEvent*);
-  UnfoldingEvent* get_latest_event() const;
+  auto begin() const { return this->events_.begin(); }
+  auto end() const { return this->events_.end(); }
 
-private:
-  /**
-   * @brief The most recent event added to the configuration
-   */
-  UnfoldingEvent* newest_event = nullptr;
-  EventSet events_;
+  bool contains(UnfoldingEvent* e) const { return this->events_.contains(e); }
+  const EventSet& get_events() const { return this->events_; }
+  UnfoldingEvent* get_latest_event() const { return this->newest_event; }
 
   /**
-   * The <-maxmimal events of the configuration. These are
-   * dynamically adjusted as events are added to the configuration
+   * @brief Insert a new event into the configuration
+   *
+   * When the newly added event is inserted into the configuration,
+   * an assertion is made to ensure that the configuration remains
+   * valid, i.e. that the newly added event's dependencies are contained
+   * within the configuration.
    *
-   * @invariant: Each event that is part of this set is
+   * @param e the event to add to the configuration. If the event is
+   * already a part of the configuration, calling this method has no
+   * effect.
    *
-   * 1. a <-maxmimal event of the configuration, in the sense that
-   * there is no event in the configuration that is "greater" than it.
-   * In UDPOR terminology, each event does not "cause" another event
+   * @throws an invalid argument exception is raised should the event
+   * be missing one of its dependencies
    *
-   * 2. contained in the set of events `events_` which comprise
-   * the configuration
+   * @note: UDPOR technically enforces the invariant that all newly-added events
+   * will ensure that the configuration is valid. We perform extra checks to ensure
+   * that UDPOR is implemented as expected. There is a performance penalty that
+   * should be noted: checking for maximality requires ensuring that all events in the
+   * configuration have their dependencies containes within the configuration, which
+   * essentially means performing a BFS/DFS over the configuration using a History object.
+   * However, since the slowest part of UDPOR involves enumerating all
+   * subsets of maximal events and computing k-partial alternatives (the
+   * latter definitively an NP-hard problem when optimal), Amdahl's law suggests
+   * we shouldn't focus so much on this (let alone the additional benefit of the
+   * assertions)
    */
-  EventSet max_events_;
-
-private:
-  void recompute_maxmimal_events(UnfoldingEvent* new_event);
-};
-
-class UnfoldingEvent {
-public:
-  UnfoldingEvent(unsigned int nb_events, std::string const& trans_tag, EventSet const& immediate_causes,
-                 StateHandle sid);
-  UnfoldingEvent(unsigned int nb_events, std::string const& trans_tag, EventSet const& immediate_causes);
-  UnfoldingEvent(const UnfoldingEvent&)            = default;
-  UnfoldingEvent& operator=(UnfoldingEvent const&) = default;
-  UnfoldingEvent(UnfoldingEvent&&)                 = default;
-
-  EventSet get_history() const;
-  bool in_history(const UnfoldingEvent* otherEvent) const;
-
-  bool conflicts_with(const UnfoldingEvent* otherEvent) const;
-  bool conflicts_with(const Configuration& config) const;
-  bool immediately_conflicts_with(const UnfoldingEvent* otherEvt) const;
-
-  bool operator==(const UnfoldingEvent&) const { return false; };
-
-  inline StateHandle get_state_id() const { return state_id; }
-  inline void set_state_id(StateHandle sid) { state_id = sid; }
-
-private:
-  int id = -1;
+  void add_event(UnfoldingEvent* e);
 
   /**
-   * @brief The "immediate" causes of this event.
-   *
-   * An event `e` is an immediate cause of an event `e'` if
    *
-   * 1. e < e'
-   * 2. There is no event `e''` in E such that
-   * `e < e''` and `e'' < e'`
-   *
-   * Intuitively, an immediate cause "happened right before"
-   * this event was executed. It is sufficient to store
-   * only the immediate causes of any event `e`, as any indirect
-   * causes of that event would either be an indirect cause
-   * or an immediate cause of the immediate causes of `e`, and
-   * so on.
    */
-  EventSet immediate_causes;
-  StateHandle state_id = 0ul;
-};
+  std::vector<UnfoldingEvent*> get_topogolically_sorted_events_of_reverse_graph() const;
 
-class StateManager {
 private:
-  using Handle = StateHandle;
-
-  Handle current_handle_ = 1ul;
-  std::unordered_map<Handle, std::unique_ptr<State>> state_map_;
+  /**
+   * @brief The most recent event added to the configuration
+   */
+  UnfoldingEvent* newest_event = nullptr;
 
-public:
-  Handle record_state(std::unique_ptr<State>);
-  std::optional<std::reference_wrapper<State>> get_state(Handle);
+  /**
+   * @brief The events which make up this configuration
+   *
+   * @invariant For each event `e` in `events_`, the set of
+   * dependencies of `e` is also contained in `events_`
+   */
+  EventSet events_;
 };
 
 } // namespace simgrid::mc::udpor