Logo AND Algorithmique Numérique Distribuée

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