Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a53078962478425816f73af582f52da979edcc83
[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 #include "xbt/asserts.h"
7
8 #ifndef SIMGRID_MC_STRATEGY_HPP
9 #define SIMGRID_MC_STRATEGY_HPP
10
11 namespace simgrid::mc {
12
13 class Strategy {
14 protected:
15   /** State's exploration status by actor. Not all the actors are there, only the ones that are ready-to-run in this
16    * state */
17   std::map<aid_t, ActorState> actors_to_run_;
18
19 public:
20   Strategy()                = default;
21   virtual ~Strategy()       = default;
22   Strategy(const Strategy&) = delete;
23   Strategy& operator=(const Strategy&)
24   { /* nothing to copy over while cloning */
25     return *this;
26   }
27
28   virtual std::pair<aid_t, int> next_transition() const = 0;
29   virtual void execute_next(aid_t aid, RemoteApp& app)  = 0;
30   virtual void consider_best()                          = 0;
31
32   // Mark the first enabled and not yet done transition as todo
33   // If there's already a transition marked as todo, does nothing
34   void consider_one(aid_t aid)
35   {
36     xbt_assert(actors_to_run_.at(aid).is_enabled() and not actors_to_run_.at(aid).is_done(),
37                "Tried to mark as TODO actor %ld but it is either not enabled or already done", aid);
38     actors_to_run_.at(aid).mark_todo();
39   }
40   // Matk as todo all actors enabled that are not done yet
41   unsigned long consider_all()
42   {
43     unsigned long count = 0;
44     for (auto& [_, actor] : actors_to_run_)
45       if (actor.is_enabled() and not actor.is_done()) {
46         actor.mark_todo();
47         count++;
48       }
49     return count;
50   }
51
52   friend class State;
53 };
54
55 } // namespace simgrid::mc
56
57 #endif