1 /* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_MC_STRATEGY_HPP
7 #define SIMGRID_MC_STRATEGY_HPP
9 #include "simgrid/forward.h"
10 #include "src/mc/api/RemoteApp.hpp"
11 #include "src/mc/mc_config.hpp"
12 #include "xbt/asserts.h"
16 namespace simgrid::mc {
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_;
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;
29 virtual ~Strategy() = default;
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;
37 /** Returns the best transition among those that should be interleaved. */
38 std::pair<aid_t, int> next_transition() { return best_transition(true); }
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;
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_)
50 aid_t best_aid = best_transition(false).first;
52 actors_to_run_.at(best_aid).mark_todo();
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)
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();
63 // Mark as todo all actors enabled that are not done yet and return the number
65 unsigned long consider_all()
67 unsigned long count = 0;
68 for (auto& [_, actor] : actors_to_run_)
69 if (actor.is_enabled() and not actor.is_done()) {
79 } // namespace simgrid::mc