Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change way Mailboxes are create, stored, and destroyed
[simgrid.git] / include / simgrid / s4u / Engine.hpp
1 /* Copyright (c) 2006-2021. 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 <string>
17 #include <utility>
18 #include <vector>
19
20 namespace simgrid {
21 namespace 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   friend simgrid::kernel::EngineImpl;
28
29 public:
30   /** Constructor, taking only the name of your main function */
31   explicit Engine(std::string name);
32   /** Constructor, taking the command line parameters of your main function */
33   explicit Engine(int* argc, char** argv);
34   /* Currently, only one instance is allowed to exist. This is why you can't copy or move it */
35 #ifndef DOXYGEN
36   Engine(const Engine&) = delete;
37   Engine(Engine&&)      = delete;
38   ~Engine();
39 #endif
40
41   /** Finalize the default engine and all its dependencies */
42   static void shutdown();
43
44   /** Run the simulation after initialization */
45   void run() const;
46
47   /** @brief Retrieve the simulation time (in seconds) */
48   static double get_clock();
49   /** @brief Retrieve the engine singleton */
50   static s4u::Engine* get_instance();
51
52   void load_platform(const std::string& platf) const;
53
54 #ifndef DOXYGEN
55   XBT_ATTRIB_DEPRECATED_v330("Please change the return code of your actors to void") void register_function(
56       const std::string& name, int (*code)(int, char**));
57   XBT_ATTRIB_DEPRECATED_v330("Please change the return code of your actors to void") void register_default(
58       int (*code)(int, char**));
59 #endif
60
61   void register_function(const std::string& name, const std::function<void(int, char**)>& code);
62   void register_function(const std::string& name, const std::function<void(std::vector<std::string>)>& code);
63   void register_function(const std::string& name, const kernel::actor::ActorCodeFactory& factory);
64
65   void register_default(const std::function<void(int, char**)>& code);
66   void register_default(const kernel::actor::ActorCodeFactory& factory);
67
68   template <class F> void register_actor(const std::string& name)
69   {
70     kernel::actor::ActorCodeFactory code_factory = [](std::vector<std::string> args) {
71       return kernel::actor::ActorCode([args = std::move(args)]() mutable {
72         F code(std::move(args));
73         code();
74       });
75     };
76     register_function(name, code_factory);
77   }
78   template <class F> void register_actor(const std::string& name, F code)
79   {
80     kernel::actor::ActorCodeFactory code_factory = [code](std::vector<std::string> args) {
81       return kernel::actor::ActorCode([code, args = std::move(args)]() mutable { code(std::move(args)); });
82     };
83     register_function(name, code_factory);
84   }
85
86   void load_deployment(const std::string& deploy) const;
87
88 protected:
89 #ifndef DOXYGEN
90   friend surf::HostImpl;
91   friend Host;
92   friend Link;
93   friend Disk;
94   friend kernel::routing::NetPoint;
95   friend kernel::routing::NetZoneImpl;
96   friend kernel::resource::LinkImpl;
97   void host_register(const std::string& name, Host* host);
98   void host_unregister(const std::string& name);
99   void link_register(const std::string& name, const Link* link);
100   void link_unregister(const std::string& name);
101   void netpoint_register(simgrid::kernel::routing::NetPoint* card);
102   void netpoint_unregister(simgrid::kernel::routing::NetPoint* card);
103 #endif /*DOXYGEN*/
104
105 public:
106   /** Returns the amount of hosts existing in the platform. */
107   size_t get_host_count() const;
108   /** Returns a vector of all hosts found in the platform.
109    *
110    * The order is generally different from the creation/declaration order in the XML platform because we use a hash
111    * table internally.
112    */
113   std::vector<Host*> get_all_hosts() const;
114   std::vector<Host*> get_filtered_hosts(const std::function<bool(Host*)>& filter) const;
115   Host* host_by_name(const std::string& name) const;
116   Host* host_by_name_or_null(const std::string& name) const;
117
118   size_t get_link_count() const;
119   std::vector<Link*> get_all_links() const;
120   std::vector<Link*> get_filtered_links(const std::function<bool(Link*)>& filter) const;
121   Link* link_by_name(const std::string& name) const;
122   Link* link_by_name_or_null(const std::string& name) const;
123
124   Mailbox* mailbox_by_name_or_create(const std::string& name) const;
125
126   size_t get_actor_count() const;
127   std::vector<ActorPtr> get_all_actors() const;
128   std::vector<ActorPtr> get_filtered_actors(const std::function<bool(ActorPtr)>& filter) const;
129
130   std::vector<kernel::routing::NetPoint*> get_all_netpoints() const;
131   kernel::routing::NetPoint* netpoint_by_name_or_null(const std::string& name) const;
132   /**
133    * @brief Get netpoint by its name
134    *
135    * @param name Netpoint name
136    * @throw std::invalid_argument if netpoint doesn't exist
137    */
138   kernel::routing::NetPoint* netpoint_by_name(const std::string& name) const;
139
140   NetZone* get_netzone_root() const;
141   void set_netzone_root(const NetZone* netzone);
142
143   NetZone* netzone_by_name_or_null(const std::string& name) const;
144
145   /**
146    * @brief Add a model to engine list
147    *
148    * @param model Pointer to model
149    * @param list  List of dependencies for this model (optional)
150    */
151   void add_model(std::shared_ptr<simgrid::kernel::resource::Model> model,
152                  const std::vector<kernel::resource::Model*>& dependencies = {});
153
154   /** @brief Get list of all models managed by this engine */
155   const std::vector<simgrid::kernel::resource::Model*>& get_all_models() const;
156
157   /** @brief Retrieves all netzones of the type indicated by the template argument */
158   template <class T> std::vector<T*> get_filtered_netzones() const
159   {
160     static_assert(std::is_base_of<kernel::routing::NetZoneImpl, T>::value,
161                   "Filtering netzones is only possible for subclasses of kernel::routing::NetZoneImpl");
162     std::vector<T*> res;
163     get_filtered_netzones_recursive(get_netzone_root(), &res);
164     return res;
165   }
166
167   /** Returns whether SimGrid was initialized yet -- mostly for internal use */
168   static bool is_initialized();
169   /** @brief set a configuration variable
170    *
171    * @beginrst
172    * Do --help on any simgrid binary to see the list of currently existing configuration variables
173    * (see also :ref:`options`).
174    * @endrst
175    *
176    * Example:
177    * simgrid::s4u::Engine::set_config("host/model:ptask_L07");
178    */
179   static void set_config(const std::string& str);
180   static void set_config(const std::string& name, int value);
181   static void set_config(const std::string& name, bool value);
182   static void set_config(const std::string& name, double value);
183   static void set_config(const std::string& name, const std::string& value);
184
185   /** Callback fired when the platform is created (ie, the xml file parsed),
186    * right before the actual simulation starts. */
187   static xbt::signal<void()> on_platform_created;
188
189   /** Callback fired when the platform is about to be created
190    * (ie, after any configuration change and just before the resource creation) */
191   static xbt::signal<void()> on_platform_creation;
192
193   /** Callback fired when the main simulation loop ends, just before the end of Engine::run() */
194   static xbt::signal<void()> on_simulation_end;
195
196   /** Callback fired when the time jumps into the future */
197   static xbt::signal<void(double)> on_time_advance;
198
199   /** Callback fired when the time cannot advance because of inter-actors deadlock. Note that the on_exit of each actor
200    * is also executed on deadlock. */
201   static xbt::signal<void(void)> on_deadlock;
202
203 private:
204   kernel::EngineImpl* const pimpl;
205   static Engine* instance_;
206   void initialize(int* argc, char** argv);
207 };
208
209 #ifndef DOXYGEN /* Internal use only, no need to expose it */
210 template <class T>
211 XBT_PRIVATE void get_filtered_netzones_recursive(const s4u::NetZone* current, std::vector<T*>* whereto)
212 {
213   static_assert(std::is_base_of<kernel::routing::NetZoneImpl, T>::value,
214                 "Filtering netzones is only possible for subclasses of kernel::routing::NetZoneImpl");
215   for (auto const& elem : current->get_children()) {
216     get_filtered_netzones_recursive(elem, whereto);
217     auto* elem_impl = dynamic_cast<T*>(elem->get_impl());
218     if (elem_impl != nullptr)
219       whereto->push_back(elem_impl);
220   }
221 }
222 #endif
223 } // namespace s4u
224 } // namespace simgrid
225
226 #endif /* SIMGRID_S4U_ENGINE_HPP */