Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f1b2a857e31afb95920ec5e6679f2bb443243d51
[simgrid.git] / src / mc / api / strategy / WaitStrategy.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_WAITSTRATEGY_HPP
7 #define SIMGRID_MC_WAITSTRATEGY_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 WaitStrategy : public Strategy {
16   int taken_wait_   = 0;
17   bool taking_wait_ = false;
18
19 public:
20   WaitStrategy()                     = default;
21   ~WaitStrategy() override           = default;
22   WaitStrategy(const BasicStrategy&) = delete;
23   WaitStrategy& operator=(const WaitStrategy& guide)
24   {
25     taken_wait_ = guide.taken_wait_;
26     return *this;
27   }
28
29   bool is_transition_wait(Transition::Type type) const
30   {
31     return type == Transition::Type::WAITANY or type == Transition::Type::BARRIER_WAIT or
32            type == Transition::Type::MUTEX_WAIT or type == Transition::Type::SEM_WAIT;
33   }
34
35   std::pair<aid_t, int> next_transition() const override
36   {
37     std::pair<aid_t, int> if_no_wait = std::make_pair(-1, 0);
38     for (auto const& [aid, actor] : actors_to_run_) {
39       if (not actor.is_todo() || not actor.is_enabled() || actor.is_done())
40         continue;
41       if (is_transition_wait(actor.get_transition(actor.get_times_considered())->type_))
42         return std::make_pair(aid, -(taken_wait_ + 1));
43       if_no_wait = std::make_pair(aid, -taken_wait_);
44     }
45     return if_no_wait;
46   }
47
48   /** If we are taking a wait transition, and last transition wasn't a wait, we need to increment the number
49    *  of wait taken. On the opposite, if we took a wait before, and now we are taking another transition, we need
50    *  to decrease the count. */
51   void execute_next(aid_t aid, RemoteApp& app) override
52   {
53     auto const& actor = actors_to_run_.at(aid);
54     if ((not taking_wait_) and is_transition_wait(actor.get_transition(actor.get_times_considered())->type_)) {
55       taken_wait_++;
56       taking_wait_ = true;
57       return;
58     }
59     if (taking_wait_ and (not is_transition_wait(actor.get_transition(actor.get_times_considered())->type_))) {
60       taken_wait_--;
61       taking_wait_ = false;
62       return;
63     }
64   }
65
66   void consider_best() override
67   {
68     aid_t aid = next_transition().first;
69     if (auto actor = actors_to_run_.find(aid); actor != actors_to_run_.end()) {
70       actor->second.mark_todo();
71       return;
72     }
73     for (auto& [_, actor] : actors_to_run_) {
74       if (actor.is_todo())
75         return;
76       if (actor.is_enabled() and not actor.is_done()) {
77         actor.mark_todo();
78         return;
79       }
80     }
81   }
82 };
83
84 } // namespace simgrid::mc
85
86 #endif