Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix dynamic_cast<> typo for computation for CommRecv
[simgrid.git] / src / mc / explo / udpor / Configuration.cpp
index c7e125dcc49a40be7b53df5faefcd110ced22c98..8d114e66430d8b2d5e08464d6955a782e81a6eb9 100644 (file)
@@ -27,14 +27,20 @@ Configuration::Configuration(const UnfoldingEvent* e) : Configuration(e->get_his
   // check the invariant regardless as a sanity check
 }
 
+Configuration::Configuration(const History& history) : Configuration(history.get_all_events()) {}
+
 Configuration::Configuration(const EventSet& events) : events_(events)
 {
   if (!events_.is_valid_configuration()) {
     throw std::invalid_argument("The events do not form a valid configuration");
   }
-}
 
-Configuration::Configuration(const History& history) : Configuration(history.get_all_events()) {}
+  // Since we add in topological order under `<`, we know that the "most-recent"
+  // transition executed by each actor will appear last
+  for (const UnfoldingEvent* e : get_topologically_sorted_events()) {
+    this->latest_event_mapping[e->get_actor()] = e;
+  }
+}
 
 void Configuration::add_event(const UnfoldingEvent* e)
 {
@@ -54,7 +60,8 @@ void Configuration::add_event(const UnfoldingEvent* e)
   }
 
   this->events_.insert(e);
-  this->newest_event = e;
+  this->newest_event                         = e;
+  this->latest_event_mapping[e->get_actor()] = e;
 
   // Preserves the property that the configuration is causally closed
   if (auto history = History(e); !this->events_.contains(history)) {
@@ -125,14 +132,17 @@ std::optional<Configuration> Configuration::compute_k_partial_alternative_to(con
                                                                              size_t k) const
 {
   // 1. Select k (of |D|, whichever is smaller) arbitrary events e_1, ..., e_k from D
-  const auto D_hat = [&]() {
-    const size_t size = std::min(k, D.size());
-    std::vector<const UnfoldingEvent*> D_hat(size);
-    // Potentially select intelligently here (e.g. perhaps pick events
-    // with transitions that we know are totally independent)...
+  const size_t k_alt_size = std::min(k, D.size());
+  const auto D_hat        = [&k_alt_size, &D]() {
+    std::vector<const UnfoldingEvent*> D_hat(k_alt_size);
+    // TODO: Since any subset suffices for computing `k`-partial alternatives,
+    // potentially select intelligently here (e.g. perhaps pick events
+    // with transitions that we know are totally independent). This may be
+    // especially important if the enumeration is the slowest part of
+    // UDPOR
     //
-    // For now, simply pick the first `k` events (any subset suffices)
-    std::copy_n(D.begin(), size, D_hat.begin());
+    // For now, simply pick the first `k` events
+    std::copy_n(D.begin(), k_alt_size, D_hat.begin());
     return D_hat;
   }();
 
@@ -145,15 +155,15 @@ std::optional<Configuration> Configuration::compute_k_partial_alternative_to(con
   // NOTE: This is an expensive operation as we must traverse the entire unfolding
   // and compute `C.is_compatible_with(History)` for every event in the structure :/.
   // A later performance improvement would be to incorporate the work of Nguyen et al.
-  // into SimGrid. Since that is a rather complicated addition, we defer to the addition
-  // for a later time...
+  // into SimGrid which associated additonal data structures with each unfolding event.
+  // Since that is a rather complicated addition, we defer it to a later time...
   Comb comb(k);
 
   for (const auto* e : U) {
-    for (unsigned i = 0; i < k; i++) {
+    for (size_t i = 0; i < k_alt_size; i++) {
       const UnfoldingEvent* e_i = D_hat[i];
       if (const auto e_local_config = History(e);
-          e_i->conflicts_with(e) and (not D.contains(e_local_config)) and is_compatible_with(e_local_config)) {
+          e_i->conflicts_with(e) and (not D.intersects(e_local_config)) and is_compatible_with(e_local_config)) {
         comb[i].push_back(e);
       }
     }
@@ -165,7 +175,8 @@ std::optional<Configuration> Configuration::compute_k_partial_alternative_to(con
   // NOTE: This is a VERY expensive operation: it enumerates all possible
   // ways to select an element from each spike. Unfortunately there's no
   // way around the enumeration, as computing a full alternative in general is
-  // NP-complete (although computing the k-partial alternative is polynomial in n)
+  // NP-complete (although computing the k-partial alternative is polynomial in
+  // the number of events)
   const auto map_events = [](const std::vector<Spike::const_iterator>& spikes) {
     std::vector<const UnfoldingEvent*> events;
     for (const auto& event_in_spike : spikes) {
@@ -186,4 +197,20 @@ std::optional<Configuration> Configuration::compute_k_partial_alternative_to(con
   return Configuration(History(map_events(*alternative)));
 }
 
+std::optional<const UnfoldingEvent*> Configuration::get_latest_event_of(aid_t aid) const
+{
+  if (const auto latest_event = latest_event_mapping.find(aid); latest_event != latest_event_mapping.end()) {
+    return std::optional<const UnfoldingEvent*>{latest_event->second};
+  }
+  return std::nullopt;
+}
+
+std::optional<const Transition*> Configuration::get_latest_action_of(aid_t aid) const
+{
+  if (const auto latest_event = get_latest_event_of(aid); latest_event.has_value()) {
+    return std::optional<const Transition*>{latest_event.value()->get_transition()};
+  }
+  return std::nullopt;
+}
+
 } // namespace simgrid::mc::udpor