Logo AND Algorithmique Numérique Distribuée

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