Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d92987dfa56262ccebac354afc7a5a9c8759bb49
[simgrid.git] / include / simgrid / s4u / Engine.hpp
1 /* Copyright (c) 2006-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_S4U_ENGINE_HPP
7 #define SIMGRID_S4U_ENGINE_HPP
8
9 #include <xbt/base.h>
10
11 #include <simgrid/forward.h>
12
13 #include <simgrid/kernel/resource/Model.hpp>
14 #include <simgrid/s4u/NetZone.hpp>
15
16 #include <set>
17 #include <string>
18 #include <utility>
19 #include <vector>
20
21 namespace simgrid::s4u {
22 /** @brief Simulation engine
23  *
24  * This is a singleton containing all the main functions of the simulation.
25  */
26 class XBT_PUBLIC Engine {
27 #ifndef DOXYGEN
28   friend simgrid::kernel::EngineImpl;
29 #endif
30
31 public:
32   /** Constructor, taking only the name of your main function */
33   explicit Engine(std::string name);
34   /** Constructor, taking the command line parameters of your main function */
35   explicit Engine(int* argc, char** argv);
36   /* Currently, only one instance is allowed to exist. This is why you can't copy or move it */
37 #ifndef DOXYGEN
38   Engine(const Engine&) = delete;
39   Engine(Engine&&)      = delete;
40   ~Engine();
41 #endif
42
43   /** Run the simulation until its end */
44   void run() const;
45
46   /** Run the simulation until the specified date */
47   void run_until(double max_date) const;
48
49   /** @brief Retrieve the simulation time (in seconds) */
50   static double get_clock();
51   /** @brief Retrieve the engine singleton */
52   static s4u::Engine* get_instance();
53   static s4u::Engine* get_instance(int* argc, char** argv);
54   static bool has_instance() { return instance_ != nullptr; }
55   const std::vector<std::string>& get_cmdline() const;
56
57   /**
58    * Creates a new platform, including hosts, links, and the routing table.
59    *
60    * @beginrst
61    * See also: :ref:`platform`.
62    * @endrst
63    */
64   void load_platform(const std::string& platf) const;
65   /**
66    * @brief Seals the platform, finishing the creation of its resources.
67    *
68    * This method is optional. The seal() is done automatically when you call Engine::run.
69    */
70   void seal_platform() const;
71   /** @brief Get a debug output of the platform.
72    *
73    * It looks like a XML platform file, but it may be very different from the input platform file: All netzones are
74    * flatified into a unique zone. This representation is mostly useful to debug your platform configuration and ensure
75    * that your assumptions over your configuration hold. This enables you to verify the exact list of links traversed
76    * between any two hosts, and the characteristics of every host and link. But you should not use the resulting file as
77    * an input platform file: it is very verbose, and thus much less efficient (in parsing time and runtime performance)
78    * than a regular platform file with the sufficient amount of intermediary netzones. Even if you use one zone only,
79    * specialized zones (such as clusters) are more efficient than the one with fully explicit routing used here.
80    */
81   std::string flatify_platform() const;
82
83   /** @verbatim embed:rst:inline Bind an actor name that could be found in :ref:`pf_tag_actor` tag to a function taking classical argc/argv parameters. See the :ref:`example <s4u_ex_actors_create>`. @endverbatim */
84   void register_function(const std::string& name, const std::function<void(int, char**)>& code);
85   /** @verbatim embed:rst:inline Bind an actor name that could be found in :ref:`pf_tag_actor` tag to a function taking a vector of strings as a parameter. See the :ref:`example <s4u_ex_actors_create>`. @endverbatim */
86   void register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code);
87   void register_function(const std::string& name, const kernel::actor::ActorCodeFactory& factory);
88
89   /** @verbatim embed:rst:inline Provide a default function to be used when the name used in a :ref:`pf_tag_actor` tag was not binded with ``register_function`` nor ``register_actor``. @endverbatim */
90   void register_default(const std::function<void(int, char**)>& code);
91   void register_default(const kernel::actor::ActorCodeFactory& factory);
92
93   /** @verbatim embed:rst:inline Bind an actor name that could be found in :ref:`pf_tag_actor` tag to a class name passed as a template parameter. See the :ref:`example <s4u_ex_actors_create>`. @endverbatim */
94   template <class F> void register_actor(const std::string& name)
95   {
96     kernel::actor::ActorCodeFactory code_factory = [](std::vector<std::string> args) {
97       return kernel::actor::ActorCode([args = std::move(args)]() mutable {
98         F code(std::move(args));
99         code();
100       });
101     };
102     register_function(name, code_factory);
103   }
104   /** @verbatim embed:rst:inline Bind an actor name that could be found in :ref:`pf_tag_actor` tag to a function name passed as a parameter. See the :ref:`example <s4u_ex_actors_create>`. @endverbatim */
105   template <class F> void register_actor(const std::string& name, F code)
106   {
107     kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
108       return kernel::actor::ActorCode([code, args = std::move(args)]() mutable { code(std::move(args)); });
109     };
110     register_function(name, code_factory);
111   }
112
113   /** If non-null, the provided set will be filled with all activities that fail to start because of a veto */
114   void track_vetoed_activities(std::set<Activity*>* vetoed_activities) const;
115
116   /** @verbatim embed:rst:inline Load a deployment file. See:ref:`deploy` and the :ref:`example <s4u_ex_actors_create>`. @endverbatim */
117   void load_deployment(const std::string& deploy) const;
118
119 protected:
120 #ifndef DOXYGEN
121   friend Host;
122   friend Link;
123   friend Disk;
124   friend kernel::routing::NetPoint;
125   friend kernel::routing::NetZoneImpl;
126   friend kernel::resource::HostImpl;
127   friend kernel::resource::StandardLinkImpl;
128   void netpoint_register(simgrid::kernel::routing::NetPoint* card);
129   void netpoint_unregister(simgrid::kernel::routing::NetPoint* card);
130   void set_netzone_root(const NetZone* netzone);
131 #endif /*DOXYGEN*/
132
133 public:
134   /** Returns the amount of hosts existing in the platform. */
135   size_t get_host_count() const;
136   /** Returns a vector of all hosts found in the platform.
137    *
138    * The order is generally different from the creation/declaration order in the XML platform because we use a hash
139    * table internally.
140    */
141   std::vector<Host*> get_all_hosts() const;
142   std::vector<Host*> get_filtered_hosts(const std::function<bool(Host*)>& filter) const;
143   Host* host_by_name(const std::string& name) const;
144   Host* host_by_name_or_null(const std::string& name) const;
145
146   size_t get_link_count() const;
147   std::vector<Link*> get_all_links() const;
148   std::vector<Link*> get_filtered_links(const std::function<bool(Link*)>& filter) const;
149   Link* link_by_name(const std::string& name) const;
150   /**
151    * @brief Find a split-duplex link from its name.
152    * @throw std::invalid_argument if the searched link does not exist.
153    */
154   SplitDuplexLink* split_duplex_link_by_name(const std::string& name) const;
155   Link* link_by_name_or_null(const std::string& name) const;
156
157   Mailbox* mailbox_by_name_or_create(const std::string& name) const;
158
159   size_t get_actor_count() const;
160   std::vector<ActorPtr> get_all_actors() const;
161   std::vector<ActorPtr> get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const;
162
163   std::vector<kernel::routing::NetPoint*> get_all_netpoints() const;
164   kernel::routing::NetPoint* netpoint_by_name_or_null(const std::string& name) const;
165   /**
166    * @brief Get netpoint by its name
167    *
168    * @param name Netpoint name
169    * @throw std::invalid_argument if netpoint doesn't exist
170    */
171   kernel::routing::NetPoint* netpoint_by_name(const std::string& name) const;
172
173   NetZone* get_netzone_root() const;
174
175   NetZone* netzone_by_name_or_null(const std::string& name) const;
176
177   /**
178    * @brief Add a model to engine list
179    *
180    * @param model        Pointer to model
181    * @param dependencies List of dependencies for this model (optional)
182    */
183   void add_model(std::shared_ptr<simgrid::kernel::resource::Model> model,
184                  const std::vector<kernel::resource::Model*>& dependencies = {});
185
186   /** @brief Get list of all models managed by this engine */
187   const std::vector<simgrid::kernel::resource::Model*>& get_all_models() const;
188
189   /** @brief Retrieves all netzones of the type indicated by the template argument */
190   template <class T> std::vector<T*> get_filtered_netzones() const
191   {
192     static_assert(std::is_base_of_v<kernel::routing::NetZoneImpl, T>,
193                   "Filtering netzones is only possible for subclasses of kernel::routing::NetZoneImpl");
194     std::vector<T*> res;
195     get_filtered_netzones_recursive(get_netzone_root(), &res);
196     return res;
197   }
198
199   kernel::EngineImpl* get_impl() const
200   {
201     return pimpl_;
202   }
203
204   /** Returns whether SimGrid was initialized yet -- mostly for internal use */
205   static bool is_initialized();
206   /** @brief set a configuration variable
207    *
208    * @beginrst
209    * Do --help on any SimGrid binary to see the list of currently existing configuration variables
210    * (see also :ref:`options`).
211    * @endrst
212    *
213    * Example:
214    * simgrid::s4u::Engine::set_config("host/model:ptask_L07");
215    */
216   static void set_config(const std::string& str);
217   static void set_config(const std::string& name, int value);
218   static void set_config(const std::string& name, bool value);
219   static void set_config(const std::string& name, double value);
220   static void set_config(const std::string& name, const std::string& value);
221
222   Engine*
223   set_default_comm_data_copy_callback(const std::function<void(kernel::activity::CommImpl*, void*, size_t)>& callback);
224
225   /** Add a callback fired when the platform is created (ie, the xml file parsed),
226    * right before the actual simulation starts. */
227   static void on_platform_created_cb(const std::function<void()>& cb) { on_platform_created.connect(cb); }
228   /** Add a callback fired when the platform is about to be created
229    * (ie, after any configuration change and just before the resource creation) */
230   static void on_platform_creation_cb(const std::function<void()>& cb) { on_platform_creation.connect(cb); }
231   /** Add a callback fired when the main simulation loop starts, at the beginning of the first call to Engine::run() */
232   static void on_simulation_start_cb(const std::function<void()>& cb) { on_simulation_start.connect(cb); }
233   /** Add a callback fired when the main simulation loop ends, just before the end of Engine::run() */
234   static void on_simulation_end_cb(const std::function<void()>& cb) { on_simulation_end.connect(cb); }
235
236   /** Add a callback fired when the time jumps into the future.
237    *
238    * It is fired right after the time change (use get_clock() to get the new timestamp).
239    * The callback parameter is the time delta since previous timestamp. */
240   static void on_time_advance_cb(const std::function<void(double)>& cb) { on_time_advance.connect(cb); }
241
242   /** Add a callback fired when the time cannot advance because of inter-actors deadlock. Note that the on_exit of each
243    * actor is also executed on deadlock. */
244   static void on_deadlock_cb(const std::function<void(void)>& cb) { on_deadlock.connect(cb); }
245
246 #ifndef DOXYGEN
247   /* FIXME signals should be private */
248   static xbt::signal<void()> on_platform_created;
249   static xbt::signal<void()> on_platform_creation;
250 #endif
251
252 private:
253   static xbt::signal<void()> on_simulation_start;
254   static xbt::signal<void(double)> on_time_advance;
255   static xbt::signal<void(void)> on_deadlock;
256   static xbt::signal<void()> on_simulation_end;
257
258   kernel::EngineImpl* const pimpl_;
259   static Engine* instance_;
260   void initialize(int* argc, char** argv);
261 };
262
263 std::vector<ActivityPtr> create_DAG_from_dot(const std::string& filename);
264 std::vector<ActivityPtr> create_DAG_from_DAX(const std::string& filename);
265 std::vector<ActivityPtr> create_DAG_from_json(const std::string& filename);
266
267 #ifndef DOXYGEN /* Internal use only, no need to expose it */
268 template <class T>
269 XBT_PRIVATE void get_filtered_netzones_recursive(const s4u::NetZone* current, std::vector<T*>* whereto)
270 {
271   static_assert(std::is_base_of_v<kernel::routing::NetZoneImpl, T>,
272                 "Filtering netzones is only possible for subclasses of kernel::routing::NetZoneImpl");
273   for (auto const& elem : current->get_children()) {
274     get_filtered_netzones_recursive(elem, whereto);
275     auto* elem_impl = dynamic_cast<T*>(elem->get_impl());
276     if (elem_impl != nullptr)
277       whereto->push_back(elem_impl);
278   }
279 }
280 #endif
281 } // namespace simgrid::s4u
282
283 #endif /* SIMGRID_S4U_ENGINE_HPP */