Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics in the changelog and NEWS file [skip-ci]
[simgrid.git] / include / simgrid / s4u / Host.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_HOST_HPP
7 #define SIMGRID_S4U_HOST_HPP
8
9 #include <simgrid/forward.h>
10 #include <xbt/Extendable.hpp>
11 #include <xbt/signal.hpp>
12 #include <xbt/string.hpp>
13
14 #include <map>
15 #include <unordered_map>
16
17 namespace simgrid {
18
19 extern template class XBT_PUBLIC xbt::Extendable<s4u::Host>;
20
21 namespace s4u {
22 /** @ingroup s4u_api
23  *
24  * @tableofcontents
25  *
26  * Some physical resource with computing and networking capabilities on which Actors execute.
27  *
28  * @beginrst
29  * All hosts are automatically created during the call of the method
30  * :cpp:func:`simgrid::s4u::Engine::load_platform()`.
31  * You cannot create a host yourself.
32  *
33  * You can retrieve a particular host using :cpp:func:`simgrid::s4u::Host::by_name()`
34  * and actors can retrieve the host on which they run using :cpp:func:`simgrid::s4u::Host::current()` or
35  * :cpp:func:`simgrid::s4u::this_actor::get_host()`
36  * @endrst
37  */
38 class XBT_PUBLIC Host : public xbt::Extendable<Host> {
39 #ifndef DOXYGEN
40   friend vm::VMModel;            // Use the pimpl_cpu to compute the VM sharing
41   friend vm::VirtualMachineImpl; // creates the the pimpl_cpu
42   friend kernel::routing::NetZoneImpl;
43   friend surf::HostImpl; // call destructor from private implementation
44
45   // The private implementation, that never changes
46   surf::HostImpl* const pimpl_;
47
48   kernel::resource::CpuImpl* pimpl_cpu_      = nullptr;
49   kernel::routing::NetPoint* pimpl_netpoint_ = nullptr;
50
51 public:
52   explicit Host(surf::HostImpl* pimpl) : pimpl_(pimpl) {}
53
54 protected:
55   virtual ~Host(); // Call destroy() instead of manually deleting it.
56   Host* set_netpoint(kernel::routing::NetPoint* netpoint);
57 #endif
58
59 public:
60   /** Called on each newly created host */
61   static xbt::signal<void(Host&)> on_creation;
62   /** Called just before destructing a host */
63   static xbt::signal<void(Host const&)> on_destruction;
64   /** Called when the machine is turned on or off (called AFTER the change) */
65   static xbt::signal<void(Host const&)> on_state_change;
66   /** Called when the speed of the machine is changed (called AFTER the change)
67    * (either because of a pstate switch or because of an external load event coming from the profile) */
68   static xbt::signal<void(Host const&)> on_speed_change;
69
70   virtual void destroy();
71 #ifndef DOXYGEN
72   // No copy/move
73   Host(Host const&) = delete;
74   Host& operator=(Host const&) = delete;
75 #endif
76
77   /** Retrieve a host from its name, or return nullptr */
78   static Host* by_name_or_null(const std::string& name);
79   /** Retrieve a host from its name, or die */
80   static Host* by_name(const std::string& name);
81   /** Retrieves the host on which the running actor is located */
82   static Host* current();
83
84   /** Retrieves the name of that host as a C++ string */
85   xbt::string const& get_name() const;
86   /** Retrieves the name of that host as a C string */
87   const char* get_cname() const;
88
89   Host* set_cpu(kernel::resource::CpuImpl* cpu);
90   kernel::resource::CpuImpl* get_cpu() const { return pimpl_cpu_; }
91   kernel::routing::NetPoint* get_netpoint() const { return pimpl_netpoint_; }
92   /**
93    * @brief Callback to set CPU factor
94    *
95    * This callback offers a flexible way to create variability in CPU executions
96    *
97    * @param flops Execution size in flops
98    * @return Multiply factor
99    */
100   using CpuFactorCb = double(double flops);
101   /**
102    * @brief Configure the factor callback to the CPU associated to this host
103    */
104   Host* set_factor_cb(const std::function<CpuFactorCb>& cb);
105
106   size_t get_actor_count() const;
107   std::vector<ActorPtr> get_all_actors() const;
108
109   /** Turns that host on if it was previously off
110    *
111    * This call does nothing if the host is already on. If it was off, all actors which were marked 'autorestart' on that
112    * host will be restarted automatically (note that this may differ from the actors that were initially running on the
113    * host).
114    *
115    * All other Host's properties are left unchanged; in particular, the pstate is left unchanged and not reset to its
116    * initial value.
117    */
118   void turn_on();
119   /** Turns that host off. All actors are forcefully stopped. */
120   void turn_off();
121   /** Returns if that host is currently up and running */
122   bool is_on() const;
123
124   const char* get_property(const std::string& key) const;
125   Host* set_property(const std::string& key, const std::string& value);
126   const std::unordered_map<std::string, std::string>* get_properties() const;
127   Host* set_properties(const std::unordered_map<std::string, std::string>& properties);
128
129   Host* set_state_profile(kernel::profile::Profile* p);
130   Host* set_speed_profile(kernel::profile::Profile* p);
131
132   /** @brief Convert the CPU's speed from string to double */
133   static std::vector<double> convert_pstate_speed_vector(const std::vector<std::string>& speed_per_state);
134   /**
135    * @brief Set the CPU's speed
136    *
137    * @param speed_per_state list of powers for this processor (default power is at index 0)
138    */
139   Host* set_pstate_speed(const std::vector<double>& speed_per_state);
140   /**
141    * @brief Set the CPU's speed (string version)
142    *
143    * @throw std::invalid_argument if speed format is incorrect.
144    */
145   Host* set_pstate_speed(const std::vector<std::string>& speed_per_state);
146
147   /** @brief Get the peak computing speed in flops/s at the current pstate, NOT taking the external load into account.
148    *
149    *  The amount of flops per second available for computing depends on several things:
150    *    - The current pstate determines the maximal peak computing speed (use @ref get_pstate_speed() to retrieve the
151    *      computing speed you would get at another pstate)
152    *    - If you declared an external load (with @ref set_speed_profile()), you must multiply the
153    * result of get_speed() by get_available_speed() to retrieve what a new computation would get.
154    *
155    *  The remaining speed is then shared between the executions located on this host.
156    *  You can retrieve the amount of tasks currently running on this host with @ref get_load().
157    *
158    *  The host may have multiple cores, and your executions may be able to use more than a single core.
159    *
160    *  Finally, executions of priority 2 get twice the amount of flops than executions of priority 1.
161    */
162   double get_speed() const;
163   /** @brief Get the available speed ratio, between 0 and 1.
164    *
165    * This accounts for external load (see @ref set_speed_profile()).
166    */
167   double get_available_speed() const;
168
169   /** Returns the number of core of the processor. */
170   int get_core_count() const;
171   Host* set_core_count(int core_count);
172
173   enum class SharingPolicy { NONLINEAR = 1, LINEAR = 0 };
174   /**
175    * @brief Describes how the CPU is shared between concurrent tasks
176    *
177    * Note that the NONLINEAR callback is in the critical path of the solver, so it should be fast.
178    *
179    * @param policy Sharing policy
180    * @param cb Callback for NONLINEAR policies
181    */
182   Host* set_sharing_policy(SharingPolicy policy, const s4u::NonLinearResourceCb& cb = {});
183   SharingPolicy get_sharing_policy() const;
184
185   /** Returns the current computation load (in flops per second)
186    *
187    * The external load (coming from an availability trace) is not taken in account.
188    * You may also be interested in the load plugin.
189    */
190   double get_load() const;
191
192   unsigned long get_pstate_count() const;
193   unsigned long get_pstate() const;
194   double get_pstate_speed(unsigned long pstate_index) const;
195   Host* set_pstate(unsigned long pstate_index);
196   Host* set_coordinates(const std::string& coords);
197
198   std::vector<Disk*> get_disks() const;
199   /**
200    * @brief Create and add disk in the host
201    *
202    * @param name Disk name
203    * @param read_bandwidth Reading speed of the disk
204    * @param write_bandwidth Writing speed of the disk
205    */
206   Disk* create_disk(const std::string& name, double read_bandwidth, double write_bandwidth);
207   /**
208    * @brief Human-friendly version of create_disk function.
209    *
210    * @throw std::invalid_argument if read/write speeds are incorrect
211    */
212   Disk* create_disk(const std::string& name, const std::string& read_bandwidth, const std::string& write_bandwidth);
213   void add_disk(const Disk* disk);
214   void remove_disk(const std::string& disk_name);
215
216   void route_to(const Host* dest, std::vector<Link*>& links, double* latency) const;
217   void route_to(const Host* dest, std::vector<kernel::resource::LinkImpl*>& links, double* latency) const;
218
219   /**
220    * @brief Seal this host
221    * No more configuration is allowed after the seal
222    */
223   Host* seal();
224
225 #ifndef DOXYGEN
226   XBT_ATTRIB_DEPRECATED_v331("Please use Comm::sendto()") void sendto(Host* dest, double byte_amount);
227   XBT_ATTRIB_DEPRECATED_v331("Please use Comm::sendto_async()") CommPtr sendto_async(Host* dest, double byte_amount);
228   XBT_ATTRIB_DEPRECATED_v330("Please use Host::sendto()") void send_to(Host* dest, double byte_amount);
229 #endif
230
231   NetZone* get_englobing_zone() const;
232   /** Block the calling actor on an execution located on the called host
233    *
234    * It is not a problem if the actor is not located on the called host.
235    * The actor will not be migrated in this case. Such remote execution are easy in simulation.
236    */
237   void execute(double flops) const;
238   /** Start an asynchronous computation on that host (possibly remote) */
239   ExecPtr exec_init(double flops_amounts) const;
240   ExecPtr exec_async(double flops_amounts) const;
241
242   /** Block the calling actor on an execution located on the called host (with explicit priority) */
243   void execute(double flops, double priority) const;
244   surf::HostImpl* get_impl() const { return pimpl_; }
245 };
246 } // namespace s4u
247 } // namespace simgrid
248
249 #endif /* SIMGRID_S4U_HOST_HPP */