Logo AND Algorithmique Numérique Distribuée

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