Logo AND Algorithmique Numérique Distribuée

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