Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add first steps for ex(C) for CommWait
[simgrid.git] / src / mc / explo / udpor / ExtensionSetCalculator.cpp
index 2486c98758e3f241f9d4ce2098501cf0b72e74fb..704866f3e9866ab228a22d2ed42277fc06b75a56 100644 (file)
@@ -25,7 +25,8 @@ EventSet ExtensionSetCalculator::partially_extend(const Configuration& C, Unfold
   using HandlerMap = std::unordered_map<Action, Handler>;
 
   const static HandlerMap handlers =
-      HandlerMap{{Action::COMM_ASYNC_RECV, &ExtensionSetCalculator::partially_extend_CommRecv}};
+      HandlerMap{{Action::COMM_ASYNC_RECV, &ExtensionSetCalculator::partially_extend_CommRecv},
+                 {Action::COMM_ASYNC_SEND, &ExtensionSetCalculator::partially_extend_CommSend}};
 
   if (const auto handler = handlers.find(action->type_); handler != handlers.end()) {
     return handler->second(C, U, std::move(action));
@@ -47,60 +48,142 @@ EventSet ExtensionSetCalculator::partially_extend_CommSend(const Configuration&
 {
   EventSet exC;
 
-  // TODO: if this is the first action by the actor, no such previous event exists.
-  // How do we react here? Do we say we're dependent with the root event?
   const auto send_action        = std::static_pointer_cast<CommSendTransition>(std::move(action));
+  const auto pre_event_a_C      = C.pre_event(send_action->aid_);
   const unsigned sender_mailbox = send_action->get_mailbox();
-  const auto pre_event_a_C      = C.pre_event(send_action->aid_).value();
 
   // 1. Create `e' := <a, config(preEvt(a, C))>` and add `e'` to `ex(C)`
-  const auto e_prime = U->discover_event(EventSet({pre_event_a_C}), send_action);
-  exC.insert(e_prime);
+  // NOTE: If `preEvt(a, C)` doesn't exist, we're effectively asking
+  // about `config({})`
+  if (pre_event_a_C.has_value()) {
+    const auto e_prime = U->discover_event(EventSet({pre_event_a_C.value()}), send_action);
+    exC.insert(e_prime);
+  } else {
+    const auto e_prime = U->discover_event(EventSet(), send_action);
+    exC.insert(e_prime);
+  }
 
   // 2. foreach e ∈ C s.t. λ(e) ∈ {AsyncSend(m, _), TestAny(Com)} where
   // Com contains a matching c' = AsyncReceive(m, _) with a
   for (const auto e : C) {
     const bool transition_type_check = [&]() {
       if (const auto* async_send = dynamic_cast<const CommSendTransition*>(e->get_transition());
-          e != nullptr && async_send->get_mailbox() == sender_mailbox) {
-        return true;
-      } else if (const auto* e_test_any = dynamic_cast<const CommTestTransition*>(e->get_transition());
-                 e_test_any != nullptr) {
-        // TODO: Check if there's a way to find a matching AsyncReceive -> matching when? in the history?
-        return true;
+          async_send != nullptr) {
+        return async_send->get_mailbox() == sender_mailbox;
       }
+      // TODO: Add `TestAny` dependency
       return false;
     }();
 
     if (transition_type_check) {
-      const EventSet K = EventSet({e, pre_event_a_C}).get_largest_maximal_subset();
+      const EventSet K = EventSet({e, pre_event_a_C.value_or(e)}).get_largest_maximal_subset();
 
-      // TODO: How do we compute D_K(a, b)? There's a specialized
-      // function for each case it appears, but I don't see where
-      // `config(K)` comes in
-      if (false) {
+      // TODO: Check D_K(a, lambda(e))
+      if (true) {
         const auto e_prime = U->discover_event(std::move(K), send_action);
         exC.insert(e_prime);
       }
     }
   }
 
+  // TODO: Add `TestAny` dependency case
   return exC;
 }
 
 EventSet ExtensionSetCalculator::partially_extend_CommRecv(const Configuration& C, Unfolding* U,
-                                                           const std::shared_ptr<Transition> recv_action)
+                                                           const std::shared_ptr<Transition> action)
 {
-  return EventSet();
+  EventSet exC;
+
+  // TODO: if this is the first action by the actor, no such previous event exists.
+  // How do we react here? Do we say we're dependent with the root event?
+  const auto recv_action      = std::static_pointer_cast<CommRecvTransition>(std::move(action));
+  const unsigned recv_mailbox = recv_action->get_mailbox();
+  const auto pre_event_a_C    = C.pre_event(recv_action->aid_);
+
+  // 1. Create `e' := <a, config(preEvt(a, C))>` and add `e'` to `ex(C)`
+  if (pre_event_a_C.has_value()) {
+    const auto e_prime = U->discover_event(EventSet({pre_event_a_C.value()}), recv_action);
+    exC.insert(e_prime);
+  } else {
+    const auto e_prime = U->discover_event(EventSet(), recv_action);
+    exC.insert(e_prime);
+  }
+
+  // 2. foreach e ∈ C s.t. λ(e) ∈ {AsyncSend(m, _), TestAny(Com)} where
+  // Com contains a matching c' = AsyncReceive(m, _) with a
+  for (const auto e : C) {
+    const bool transition_type_check = [&]() {
+      if (const auto* async_recv = dynamic_cast<const CommSendTransition*>(e->get_transition());
+          async_recv != nullptr && async_recv->get_mailbox() == recv_mailbox) {
+        return true;
+      }
+      // TODO: Add `TestAny` dependency
+      return false;
+    }();
+
+    if (transition_type_check) {
+      const EventSet K = EventSet({e, pre_event_a_C.value_or(e)}).get_largest_maximal_subset();
+
+      // TODO: Check D_K(a, lambda(e))
+      if (true) {
+        const auto e_prime = U->discover_event(std::move(K), recv_action);
+        exC.insert(e_prime);
+      }
+    }
+  }
+
+  // TODO: Add `TestAny` dependency case
+  return exC;
 }
 
-EventSet ExtensionSetCalculator::partially_extend_CommWait(const Configuration&, Unfolding* U,
-                                                           std::shared_ptr<Transition> wait_action)
+EventSet ExtensionSetCalculator::partially_extend_CommWait(const Configuration& C, Unfolding* U,
+                                                           std::shared_ptr<Transition> action)
 {
-  return EventSet();
+  EventSet exC;
+
+  const auto wait_action   = std::static_pointer_cast<CommWaitTransition>(std::move(action));
+  const auto pre_event_a_C = C.pre_event(wait_action->aid_);
+
+  // 1. if `a` is enabled at state(config({preEvt(a,C)})), then
+  // create `e' := <a, config({preEvt(a,C)})>` and add `e'` to `ex(C)`
+  //
+  // First, if `pre_event_a_C == std::nullopt`, then there is nothing to
+  // do: `CommWait` will never be enabled in the empty configuration (at
+  // least two actions must be executed before)
+  if (pre_event_a_C.has_value(); const auto unwrapped_pre_event = pre_event_a_C.value()) {
+
+    // Determine the _issuer_ of the communication of the `CommWait` event
+    // in `C`. The issuer of the `CommWait` in `C` is the event in `C`
+    // whose transition is the `CommRecv` or `CommSend` whose resutling
+    // communication this `CommWait` waits on
+    const auto issuer = std::find_if(C.begin(), C.end(), [=](const UnfoldingEvent* e) { return false; });
+    xbt_assert(issuer != C.end(),
+               "Invariant violation! A (supposedly) enabled `CommWait` transition "
+               "waiting on commiunication %lu should not be enabled: the receive/send "
+               "transition which generated the communication is not an action taken "
+               "to reach state(C) (the state of the configuration), which should "
+               "be an impossibility if `%s` is enabled. Please report this as "
+               "a bug in SimGrid's UDPOR implementation",
+               wait_action->get_comm(), wait_action->to_string(false).c_str());
+
+    // A necessary condition is that the issuer be present in
+    // config({preEvt(a, C)}); otherwise, the `CommWait` could not
+    // be enabled since the communication on which it waits would not
+    // have been created for it!
+    if (const auto config_pre_event = History(unwrapped_pre_event); config_pre_event.contains(*issuer)) {
+      // If the issuer is a `CommRecv` (resp. `CommSend`), then we check that there
+      // are at least as many `CommSend` (resp. `CommRecv`) transitions in `config_pre_event`
+      // as needed to reach the receive/send number that is `issuer`.
+      //...
+      //...
+    }
+  }
+
+  return exC;
 }
 
-EventSet ExtensionSetCalculator::partially_extend_CommTest(const Configuration&, Unfolding* U,
+EventSet ExtensionSetCalculator::partially_extend_CommTest(const Configuration& C, Unfolding* U,
                                                            std::shared_ptr<Transition> test_action)
 {
   return EventSet();