Logo AND Algorithmique Numérique Distribuée

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