1 /* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_MC_WAITSTRATEGY_HPP
7 #define SIMGRID_MC_WAITSTRATEGY_HPP
9 #include "src/mc/transition/TransitionComm.hpp"
11 namespace simgrid::mc {
13 /** Wait MC guiding class that aims at minimizing the number of in-fly communication.
14 * When possible, it will try to match corresponding in-fly communications. */
15 class MaxMatchComm : public Strategy {
17 /** Stores for each mailbox what kind of transition is waiting on it.
18 * Negative number means that much recv are waiting on that mailbox, while
19 * a positiv number means that much send are waiting there. */
20 std::map<unsigned, int> mailbox_;
21 int value_of_state_ = 0; // used to valuate the state. Corresponds to the number of in-fly communications
22 // The two next values are used to save the operation we execute so the next strategy can update its field accordingly
23 Transition::Type last_transition_;
24 unsigned last_mailbox_ = 0;
27 void copy_from(const Strategy* strategy) override
29 const MaxMatchComm* cast_strategy = dynamic_cast<MaxMatchComm const*>(strategy);
30 xbt_assert(cast_strategy != nullptr);
31 for (auto& [id, val] : cast_strategy->mailbox_)
33 if (cast_strategy->last_transition_ == Transition::Type::COMM_ASYNC_RECV)
34 mailbox_[cast_strategy->last_mailbox_]--;
35 if (cast_strategy->last_transition_ == Transition::Type::COMM_ASYNC_SEND)
36 mailbox_[cast_strategy->last_mailbox_]++;
38 for (auto const& [_, val] : mailbox_)
39 value_of_state_ += std::abs(val);
41 MaxMatchComm() = default;
42 ~MaxMatchComm() override = default;
44 std::pair<aid_t, int> best_transition(bool must_be_todo) const override
46 std::pair<aid_t, int> min_found = std::make_pair(-1, value_of_state_+2);
47 for (auto const& [aid, actor] : actors_to_run_) {
48 if ((not actor.is_todo() && must_be_todo) || not actor.is_enabled() || actor.is_done())
51 int aid_value = value_of_state_;
52 const Transition* transition = actor.get_transition(actor.get_times_considered()).get();
54 const CommRecvTransition* cast_recv = dynamic_cast<CommRecvTransition const*>(transition);
55 if (cast_recv != nullptr) {
56 if (mailbox_.count(cast_recv->get_mailbox()) > 0 and
57 mailbox_.at(cast_recv->get_mailbox()) > 0) {
58 aid_value--; // This means we have waiting recv corresponding to this recv
65 const CommSendTransition* cast_send = dynamic_cast<CommSendTransition const*>(transition);
66 if (cast_send != nullptr) {
67 if (mailbox_.count(cast_send->get_mailbox()) > 0 and
68 mailbox_.at(cast_send->get_mailbox()) < 0) {
69 aid_value--; // This means we have waiting recv corresponding to this send
75 if (aid_value < min_found.second)
76 min_found = std::make_pair(aid, aid_value);
82 void execute_next(aid_t aid, RemoteApp& app) override
84 const Transition* transition = actors_to_run_.at(aid).get_transition(actors_to_run_.at(aid).get_times_considered()).get();
85 last_transition_ = transition->type_;
87 const CommRecvTransition* cast_recv = dynamic_cast<CommRecvTransition const*>(transition);
88 if (cast_recv != nullptr)
89 last_mailbox_ = cast_recv->get_mailbox();
91 const CommSendTransition* cast_send = dynamic_cast<CommSendTransition const*>(transition);
92 if (cast_send != nullptr)
93 last_mailbox_ = cast_send->get_mailbox();
98 } // namespace simgrid::mc