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