Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Typo found by lintian
[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   /** Strategies can have values shared from parent to children state.
25    *  This method should copy all value the strategy needs from its parent. */
26   virtual void copy_from(const Strategy*) = 0;
27     
28   Strategy()                              = default;
29   virtual ~Strategy()                     = default;
30
31   /** Returns the best transition among according to the strategy for this state.
32    *  The strategy should consider only enabled transition not already done.
33    *  Furthermore, if must_be_todo is set to true, only transitions marked as todo
34    *  should be considered. */
35   virtual std::pair<aid_t, int> best_transition(bool must_be_todo) const = 0;
36
37   /** Returns the best transition among those that should be interleaved. */
38   std::pair<aid_t, int> next_transition() { return best_transition(true); }
39
40   /** Allows for the strategy to update its fields knowing that the actor aid will
41    *  be executed and a children strategy will then be created. */  
42   virtual void execute_next(aid_t aid, RemoteApp& app)  = 0;
43
44   /** Ensure at least one transition is marked as todo among the enabled ones not done.
45    *  If required, it marks as todo the best transition according to the strategy. */
46   void consider_best() {
47     for (auto& [_, actor] :actors_to_run_)
48           if (actor.is_todo())
49               return;
50     aid_t best_aid = best_transition(false).first;
51     if (best_aid != -1)
52         actors_to_run_.at(best_aid).mark_todo();
53   }
54
55   // Mark aid as todo. If it makes no sense, ie. if it is already done or not enabled,
56   // else raise an error
57   void consider_one(aid_t aid)
58   {
59     xbt_assert(actors_to_run_.at(aid).is_enabled() and not actors_to_run_.at(aid).is_done(),
60                "Tried to mark as TODO actor %ld but it is either not enabled or already done", aid);
61     actors_to_run_.at(aid).mark_todo();
62   }
63   // Mark as todo all actors enabled that are not done yet and return the number
64   // of marked actors
65   unsigned long consider_all()
66   {
67     unsigned long count = 0;
68     for (auto& [_, actor] : actors_to_run_)
69       if (actor.is_enabled() and not actor.is_done()) {
70         actor.mark_todo();
71         count++;
72       }
73     return count;
74   }
75
76   friend class State;
77 };
78
79 } // namespace simgrid::mc
80
81 #endif