Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a18f6e3d86021d249015cbf384747b240912989b
[simgrid.git] / src / mc / api / guide / WaitGuide.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_WAITGUIDE_HPP
7 #define SIMGRID_MC_WAITGUIDE_HPP
8
9 #include "src/mc/transition/Transition.hpp"
10
11 namespace simgrid::mc {
12
13 /** Wait MC guiding class that aims at minimizing the number of in-fly communication.
14  *  When possible, it will try to take the wait transition. */
15 class WaitGuide : public GuidedState {
16   double taken_wait_ = 0;
17   bool taking_wait_  = false;
18
19 public:
20   void operator=(const WaitGuide& guide) { taken_wait_ = guide.taken_wait_; }
21
22   bool is_transition_wait(Transition::Type type) const
23   {
24     return type == Transition::Type::WAITANY or type == Transition::Type::BARRIER_WAIT or
25            type == Transition::Type::MUTEX_WAIT or type == Transition::Type::SEM_WAIT;
26   }
27
28   std::pair<aid_t, double> next_transition() const override
29   {
30     std::pair<aid_t, double> if_no_wait = std::make_pair(-1, 0.0);
31     for (auto const& [aid, actor] : actors_to_run_) {
32       if (not actor.is_todo() || not actor.is_enabled() || actor.is_done())
33         continue;
34       if (is_transition_wait(actor.get_transition(actor.get_times_considered())->type_))
35         return std::make_pair(aid, -(taken_wait_ + 1));
36       if_no_wait = std::make_pair(aid, -taken_wait_);
37     }
38     return if_no_wait;
39   }
40
41   /** If we are taking a wait transition, and last transition wasn't a wait, we need to increment the number
42    *  of wait taken. On the opposite, if we took a wait before, and now we are taking another transition, we need
43    *  to decrease the count. */
44   void execute_next(aid_t aid, RemoteApp& app) override
45   {
46     auto& actor = actors_to_run_.at(aid);
47     if ((not taking_wait_) and is_transition_wait(actor.get_transition(actor.get_times_considered())->type_)) {
48       taken_wait_++;
49       taking_wait_ = true;
50       return;
51     }
52     if (taking_wait_ and (not is_transition_wait(actor.get_transition(actor.get_times_considered())->type_))) {
53       taken_wait_--;
54       taking_wait_ = false;
55       return;
56     }
57   }
58
59   void consider_best() override
60   {
61     const auto& [aid, _] = this->next_transition();
62     auto actor           = actors_to_run_.find(aid);
63     if (actor != actors_to_run_.end()) {
64       actor->second.mark_todo();
65       return;
66     }
67     for (auto& [_, actor] : actors_to_run_) {
68       if (actor.is_todo())
69         return;
70       if (actor.is_enabled() and not actor.is_done()) {
71         actor.mark_todo();
72         return;
73       }
74     }
75   }
76 };
77
78 } // namespace simgrid::mc
79
80 #endif