Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
deprecate Engine::shutdown()
[simgrid.git] / include / simgrid / s4u / Engine.hpp
1 /* Copyright (c) 2006-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_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
60   void load_platform(const std::string& platf) const;
61   void seal_platform() const;
62
63   /** @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 */
64   void register_function(const std::string& name, const std::function<void(int, char**)>& code);
65   /** @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 */
66   void register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code);
67   void register_function(const std::string& name, const kernel::actor::ActorCodeFactory& factory);
68
69   /** @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 */
70   void register_default(const std::function<void(int, char**)>& code);
71   void register_default(const kernel::actor::ActorCodeFactory& factory);
72
73   /** @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 */
74   template <class F> void register_actor(const std::string& name)
75   {
76     kernel::actor::ActorCodeFactory code_factory = [](std::vector<std::string> args) {
77       return kernel::actor::ActorCode([args = std::move(args)]() mutable {
78         F code(std::move(args));
79         code();
80       });
81     };
82     register_function(name, code_factory);
83   }
84   /** @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 */
85   template <class F> void register_actor(const std::string& name, F code)
86   {
87     kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
88       return kernel::actor::ActorCode([code, args = std::move(args)]() mutable { code(std::move(args)); });
89     };
90     register_function(name, code_factory);
91   }
92
93   /** If non-null, the provided set will be filled with all activities that fail to start because of a veto */
94   void track_vetoed_activities(std::set<Activity*>* vetoed_activities) const;
95
96   /** @verbatim embed:rst:inline Load a deployment file. See:ref:`deploy` and the :ref:`example <s4u_ex_actors_create>`. @endverbatim */
97   void load_deployment(const std::string& deploy) const;
98
99 protected:
100 #ifndef DOXYGEN
101   friend Host;
102   friend Link;
103   friend Disk;
104   friend kernel::routing::NetPoint;
105   friend kernel::routing::NetZoneImpl;
106   friend kernel::resource::HostImpl;
107   friend kernel::resource::StandardLinkImpl;
108   void host_register(const std::string& name, Host* host);
109   void host_unregister(const std::string& name);
110   void link_register(const std::string& name, const Link* link);
111   void link_unregister(const std::string& name);
112   void netpoint_register(simgrid::kernel::routing::NetPoint* card);
113   void netpoint_unregister(simgrid::kernel::routing::NetPoint* card);
114   void set_netzone_root(const NetZone* netzone);
115 #endif /*DOXYGEN*/
116
117 public:
118   /** Returns the amount of hosts existing in the platform. */
119   size_t get_host_count() const;
120   /** Returns a vector of all hosts found in the platform.
121    *
122    * The order is generally different from the creation/declaration order in the XML platform because we use a hash
123    * table internally.
124    */
125   std::vector<Host*> get_all_hosts() const;
126   std::vector<Host*> get_filtered_hosts(const std::function<bool(Host*)>& filter) const;
127   Host* host_by_name(const std::string& name) const;
128   Host* host_by_name_or_null(const std::string& name) const;
129
130   size_t get_link_count() const;
131   std::vector<Link*> get_all_links() const;
132   std::vector<Link*> get_filtered_links(const std::function<bool(Link*)>& filter) const;
133   Link* link_by_name(const std::string& name) const;
134   /**
135    * @brief Find a split-duplex link from its name.
136    * @throw std::invalid_argument if the searched link does not exist.
137    */
138   SplitDuplexLink* split_duplex_link_by_name(const std::string& name) const;
139   Link* link_by_name_or_null(const std::string& name) const;
140
141   Mailbox* mailbox_by_name_or_create(const std::string& name) const;
142
143   size_t get_actor_count() const;
144   std::vector<ActorPtr> get_all_actors() const;
145   std::vector<ActorPtr> get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const;
146
147   std::vector<kernel::routing::NetPoint*> get_all_netpoints() const;
148   kernel::routing::NetPoint* netpoint_by_name_or_null(const std::string& name) const;
149   /**
150    * @brief Get netpoint by its name
151    *
152    * @param name Netpoint name
153    * @throw std::invalid_argument if netpoint doesn't exist
154    */
155   kernel::routing::NetPoint* netpoint_by_name(const std::string& name) const;
156
157   NetZone* get_netzone_root() const;
158
159   NetZone* netzone_by_name_or_null(const std::string& name) const;
160
161   /**
162    * @brief Add a model to engine list
163    *
164    * @param model        Pointer to model
165    * @param dependencies List of dependencies for this model (optional)
166    */
167   void add_model(std::shared_ptr<simgrid::kernel::resource::Model> model,
168                  const std::vector<kernel::resource::Model*>& dependencies = {});
169
170   /** @brief Get list of all models managed by this engine */
171   const std::vector<simgrid::kernel::resource::Model*>& get_all_models() const;
172
173   /** @brief Retrieves all netzones of the type indicated by the template argument */
174   template <class T> std::vector<T*> get_filtered_netzones() const
175   {
176     static_assert(std::is_base_of<kernel::routing::NetZoneImpl, T>::value,
177                   "Filtering netzones is only possible for subclasses of kernel::routing::NetZoneImpl");
178     std::vector<T*> res;
179     get_filtered_netzones_recursive(get_netzone_root(), &res);
180     return res;
181   }
182
183   kernel::EngineImpl* get_impl() const { return pimpl; }
184
185   /** Returns whether SimGrid was initialized yet -- mostly for internal use */
186   static bool is_initialized();
187   /** @brief set a configuration variable
188    *
189    * @beginrst
190    * Do --help on any simgrid binary to see the list of currently existing configuration variables
191    * (see also :ref:`options`).
192    * @endrst
193    *
194    * Example:
195    * simgrid::s4u::Engine::set_config("host/model:ptask_L07");
196    */
197   static void set_config(const std::string& str);
198   static void set_config(const std::string& name, int value);
199   static void set_config(const std::string& name, bool value);
200   static void set_config(const std::string& name, double value);
201   static void set_config(const std::string& name, const std::string& value);
202
203   Engine* set_default_comm_data_copy_callback(void (*callback)(kernel::activity::CommImpl*, void*, size_t));
204
205   /** Add a callback fired when the platform is created (ie, the xml file parsed),
206    * right before the actual simulation starts. */
207   static void on_platform_created_cb(const std::function<void()>& cb) { on_platform_created.connect(cb); }
208   /** Add a callback fired when the platform is about to be created
209    * (ie, after any configuration change and just before the resource creation) */
210   static void on_platform_creation_cb(const std::function<void()>& cb) { on_platform_creation.connect(cb); }
211   /** Add a callback fired when the main simulation loop ends, just before the end of Engine::run() */
212   static void on_simulation_end_cb(const std::function<void()>& cb) { on_simulation_end.connect(cb); }
213
214   /** Add a callback fired when the time jumps into the future */
215   static void on_time_advance_cb(const std::function<void(double)>& cb) { on_time_advance.connect(cb); }
216
217   /** Add a callback fired when the time cannot advance because of inter-actors deadlock. Note that the on_exit of each
218    * actor is also executed on deadlock. */
219   static void on_deadlock_cb(const std::function<void(void)>& cb) { on_deadlock.connect(cb); }
220
221 #ifndef DOXYGEN
222   /* FIXME signals should be private */
223   static xbt::signal<void()> on_platform_created;
224   static xbt::signal<void()> on_platform_creation;
225 #endif
226
227 private:
228   static xbt::signal<void()> on_simulation_end;
229   static xbt::signal<void(double)> on_time_advance;
230   static xbt::signal<void(void)> on_deadlock;
231   kernel::EngineImpl* const pimpl;
232   static Engine* instance_;
233   void initialize(int* argc, char** argv);
234 };
235
236 std::vector<ActivityPtr> create_DAG_from_dot(const std::string& filename);
237 std::vector<ActivityPtr> create_DAG_from_DAX(const std::string& filename);
238
239 #ifndef DOXYGEN /* Internal use only, no need to expose it */
240 template <class T>
241 XBT_PRIVATE void get_filtered_netzones_recursive(const s4u::NetZone* current, std::vector<T*>* whereto)
242 {
243   static_assert(std::is_base_of<kernel::routing::NetZoneImpl, T>::value,
244                 "Filtering netzones is only possible for subclasses of kernel::routing::NetZoneImpl");
245   for (auto const& elem : current->get_children()) {
246     get_filtered_netzones_recursive(elem, whereto);
247     auto* elem_impl = dynamic_cast<T*>(elem->get_impl());
248     if (elem_impl != nullptr)
249       whereto->push_back(elem_impl);
250   }
251 }
252 #endif
253 } // namespace s4u
254 } // namespace simgrid
255
256 #endif /* SIMGRID_S4U_ENGINE_HPP */