Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f02256ca99b0621a73d1ad94716d1424bbc6c29d
[simgrid.git] / src / kernel / EngineImpl.hpp
1 /* Copyright (c) 2016-2022. 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_KERNEL_ENGINEIMPL_HPP
7 #define SIMGRID_KERNEL_ENGINEIMPL_HPP
8
9 #include <simgrid/kernel/resource/Model.hpp>
10 #include <simgrid/s4u/Engine.hpp>
11 #include <simgrid/s4u/NetZone.hpp>
12 #include <simgrid/simix.hpp>
13 #include <xbt/dynar.h>
14 #include <xbt/functional.hpp>
15
16 #include "src/kernel/activity/ExecImpl.hpp"
17 #include "src/kernel/activity/IoImpl.hpp"
18 #include "src/kernel/activity/MailboxImpl.hpp"
19 #include "src/kernel/activity/SleepImpl.hpp"
20 #include "src/kernel/activity/Synchro.hpp"
21 #include "src/kernel/actor/ActorImpl.hpp"
22 #include "src/kernel/resource/SplitDuplexLinkImpl.hpp"
23
24 #include <boost/intrusive/list.hpp>
25 #include <map>
26 #include <mutex>
27 #include <set>
28 #include <string>
29 #include <unordered_map>
30 #include <vector>
31
32 namespace simgrid {
33 namespace kernel {
34
35 class EngineImpl {
36   std::map<std::string, s4u::Host*, std::less<>> hosts_;
37   std::unordered_map<std::string, routing::NetPoint*> netpoints_;
38   std::unordered_map<std::string, activity::MailboxImpl*> mailboxes_;
39
40   std::unordered_map<std::string, actor::ActorCodeFactory> registered_functions; // Maps function names to actor code
41   actor::ActorCodeFactory default_function; // Function to use as a fallback when the provided name matches nothing
42   std::vector<resource::Model*> models_;
43   std::unordered_map<std::string, std::shared_ptr<resource::Model>> models_prio_;
44   routing::NetZoneImpl* netzone_root_ = nullptr;
45   std::set<actor::ActorImpl*> daemons_;
46   std::vector<actor::ActorImpl*> actors_to_run_;
47   std::vector<actor::ActorImpl*> actors_that_ran_;
48   std::map<aid_t, actor::ActorImpl*> actor_list_;
49   boost::intrusive::list<actor::ActorImpl,
50                          boost::intrusive::member_hook<actor::ActorImpl, boost::intrusive::list_member_hook<>,
51                                                        &actor::ActorImpl::kernel_destroy_list_hook>>
52       actors_to_destroy_;
53 #if SIMGRID_HAVE_MC
54   /* MCer cannot read members actor_list_ above in the remote process, so we copy the info it needs in a dynar.
55    * FIXME: This is supposed to be a temporary hack.
56    * A better solution would be to change the split between MCer and MCed, where the responsibility
57    *   to compute the list of the enabled transitions goes to the MCed.
58    * That way, the MCer would not need to have the list of actors on its side.
59    * These info could be published by the MCed to the MCer in a way inspired of vd.so
60    */
61   xbt_dynar_t actors_vector_      = xbt_dynar_new(sizeof(actor::ActorImpl*), nullptr);
62 #endif
63
64   static double now_;
65   static EngineImpl* instance_;
66   actor::ActorImpl* maestro_ = nullptr;
67   context::ContextFactory* context_factory_ = nullptr;
68
69   std::unique_ptr<void, std::function<int(void*)>> platf_handle_; //!< handle for platform library
70   friend s4u::Engine;
71
72 public:
73   EngineImpl() = default;
74
75   /* Currently, only one instance is allowed to exist. This is why you can't copy or move it */
76 #ifndef DOXYGEN
77   EngineImpl(const EngineImpl&) = delete;
78   EngineImpl& operator=(const EngineImpl&) = delete;
79   virtual ~EngineImpl();
80   static void shutdown();
81 #endif
82
83   void initialize(int* argc, char** argv);
84   void load_platform(const std::string& platf);
85   void load_deployment(const std::string& file) const;
86   void seal_platform() const;
87   void register_function(const std::string& name, const actor::ActorCodeFactory& code);
88   void register_default(const actor::ActorCodeFactory& code);
89
90   bool is_maestro(const actor::ActorImpl* actor) const { return actor == maestro_; }
91   void set_maestro(actor::ActorImpl* actor) { maestro_ = actor; }
92   actor::ActorImpl* get_maestro() const { return maestro_; }
93
94   context::ContextFactory* get_context_factory() const { return context_factory_; }
95   void set_context_factory(context::ContextFactory* factory) { context_factory_ = factory; }
96   bool has_context_factory() const { return context_factory_ != nullptr; }
97
98   void context_mod_init() const;
99   /**
100    * @brief Add a model to engine list
101    *
102    * @param model Pointer to model
103    * @param list  List of dependencies for this model
104    */
105   void add_model(std::shared_ptr<simgrid::kernel::resource::Model> model,
106                  const std::vector<resource::Model*>& dep_models = {});
107
108   /** @brief Get list of all models managed by this engine */
109   const std::vector<resource::Model*>& get_all_models() const { return models_; }
110
111   static bool has_instance() { return s4u::Engine::has_instance(); }
112   static EngineImpl* get_instance() { return s4u::Engine::get_instance()->pimpl; }
113   static EngineImpl* get_instance(int* argc, char** argv) { return s4u::Engine::get_instance(argc, argv)->pimpl; }
114
115   actor::ActorCodeFactory get_function(const std::string& name)
116   {
117     auto res = registered_functions.find(name);
118     if (res == registered_functions.end())
119       return default_function;
120     else
121       return res->second;
122   }
123
124   routing::NetZoneImpl* get_netzone_root() const { return netzone_root_; }
125
126   void add_daemon(actor::ActorImpl* d) { daemons_.insert(d); }
127   void remove_daemon(actor::ActorImpl* d);
128   void add_actor_to_run_list(actor::ActorImpl* actor);
129   void add_actor_to_run_list_no_check(actor::ActorImpl* actor);
130   void add_actor_to_destroy_list(actor::ActorImpl& actor) { actors_to_destroy_.push_back(actor); }
131
132   bool has_actors_to_run() const { return not actors_to_run_.empty(); }
133   const actor::ActorImpl* get_first_actor_to_run() const { return actors_to_run_.front(); }
134   const actor::ActorImpl* get_actor_to_run_at(unsigned long int i) const { return actors_to_run_[i]; }
135   unsigned long int get_actor_to_run_count() const { return actors_to_run_.size(); }
136   size_t get_actor_count() const { return actor_list_.size(); }
137   actor::ActorImpl* get_actor_by_pid(aid_t pid);
138   void add_actor(aid_t pid, actor::ActorImpl* actor) { actor_list_[pid] = actor; }
139   void remove_actor(aid_t pid) { actor_list_.erase(pid); }
140
141 #if SIMGRID_HAVE_MC
142   void reset_actor_dynar() { xbt_dynar_reset(actors_vector_); }
143   void add_actor_to_dynar(actor::ActorImpl* actor) { xbt_dynar_push_as(actors_vector_, actor::ActorImpl*, actor); }
144 #endif
145
146   const std::map<aid_t, actor::ActorImpl*>& get_actor_list() const { return actor_list_; }
147   const std::vector<actor::ActorImpl*>& get_actors_to_run() const { return actors_to_run_; }
148   const std::vector<actor::ActorImpl*>& get_actors_that_ran() const { return actors_that_ran_; }
149
150   void handle_ended_actions() const;
151   /**
152    * Garbage collection
153    *
154    * Should be called some time to time to free the memory allocated for actors that have finished (or killed).
155    */
156   void empty_trash();
157   void display_all_actor_status() const;
158   void run_all_actors();
159
160   /*  @brief Finish simulation initialization
161    *  This function must be called before the first call to solve()
162    */
163   void presolve() const;
164   /** @brief Performs a part of the simulation
165    *  @param max_date Maximum date to update the simulation to, or -1
166    *  @return the elapsed time, or -1.0 if no event could be executed
167    *
168    *  This function execute all possible events, update the action states  and returns the time elapsed.
169    *  When you call execute or communicate on a model, the corresponding actions are not executed immediately but only
170    *  when you call solve().
171    *  Note that the returned elapsed time can be zero.
172    */
173   double solve(double max_date) const;
174
175   /** @brief Run the main simulation loop until the specified date (or infinitly if max_date is negative). */
176   void run(double max_date);
177
178   /** @brief Return the current time in milliseconds. */
179   static double get_clock();
180 };
181
182 } // namespace kernel
183 } // namespace simgrid
184
185 #endif