Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[simgrid.git] / src / mc / api / strategy / Strategy.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_STRATEGY_HPP
7 #define SIMGRID_MC_STRATEGY_HPP
8
9 #include "simgrid/forward.h"
10 #include "src/mc/api/RemoteApp.hpp"
11 #include "src/mc/mc_config.hpp"
12 #include "xbt/asserts.h"
13 #include <map>
14 #include <utility>
15
16 namespace simgrid::mc {
17
18 class Strategy {
19 protected:
20   /** State's exploration status by actor. All actors should be present, eventually disabled for now. */
21   std::map<aid_t, ActorState> actors_to_run_;
22
23 public:
24   virtual void copy_from(const Strategy*) = 0;
25   Strategy()                              = default;
26   virtual ~Strategy()                     = default;
27
28   virtual std::pair<aid_t, int> best_transition(bool must_be_todo) const = 0;
29     
30   std::pair<aid_t, int> next_transition() { return best_transition(true); }
31   virtual void execute_next(aid_t aid, RemoteApp& app)  = 0;
32
33   // Mark the first enabled and not yet done transition as todo
34   // If there's already a transition marked as todo, does nothing
35   void consider_best() {
36         for (auto& [_, actor] :actors_to_run_)
37             if (actor.is_todo())
38                 return;
39         aid_t best_aid = best_transition(false).first;
40         if (best_aid != -1)
41             actors_to_run_.at(best_aid).mark_todo();
42   }
43
44   // Mark aid as todo. If it makes no sense, ie. if it is already done or not enabled,
45   // raise an error
46   void consider_one(aid_t aid)
47   {
48     xbt_assert(actors_to_run_.at(aid).is_enabled() and not actors_to_run_.at(aid).is_done(),
49                "Tried to mark as TODO actor %ld but it is either not enabled or already done", aid);
50     actors_to_run_.at(aid).mark_todo();
51   }
52   // Mark as todo all actors enabled that are not done yet and return the number
53   // of marked actors
54   unsigned long consider_all()
55   {
56     unsigned long count = 0;
57     for (auto& [_, actor] : actors_to_run_)
58       if (actor.is_enabled() and not actor.is_done()) {
59         actor.mark_todo();
60         count++;
61       }
62     return count;
63   }
64
65   friend class State;
66 };
67
68 } // namespace simgrid::mc
69
70 #endif