Logo AND Algorithmique Numérique Distribuée

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