Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics: make more fields private
[simgrid.git] / src / surf / cpu_interface.hpp
1 /* Copyright (c) 2004-2019. 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 SURF_CPU_INTERFACE_HPP_
7 #define SURF_CPU_INTERFACE_HPP_
8
9 #include "simgrid/kernel/resource/Model.hpp"
10 #include "simgrid/kernel/resource/Resource.hpp"
11 #include "simgrid/s4u/Host.hpp"
12 #include "src/kernel/lmm/maxmin.hpp"
13 #include "src/kernel/resource/profile/trace_mgr.hpp"
14
15 #include <list>
16
17 /***********
18  * Classes *
19  ***********/
20
21 namespace simgrid {
22 namespace kernel {
23 namespace resource {
24
25 /** @ingroup SURF_cpu_interface
26  * @brief SURF cpu model interface class
27  * @details A model is an object which handle the interactions between its Resources and its Actions
28  */
29 class XBT_PUBLIC CpuModel : public Model {
30 public:
31   explicit CpuModel(Model::UpdateAlgo algo) : Model(algo) {}
32
33   /**
34    * @brief Create a Cpu
35    *
36    * @param host The host that will have this CPU
37    * @param speed_per_pstate Processor speed (in Flops) of each pstate.
38    *                         This ignores any potential external load coming from a trace.
39    * @param core The number of core of this Cpu
40    */
41   virtual Cpu* create_cpu(s4u::Host* host, const std::vector<double>& speed_per_pstate, int core) = 0;
42
43   void update_actions_state_lazy(double now, double delta) override;
44   void update_actions_state_full(double now, double delta) override;
45 };
46
47 /************
48  * Resource *
49  ************/
50
51 /** @ingroup SURF_cpu_interface
52 * @brief SURF cpu resource interface class
53 * @details A Cpu represent a cpu associated to a host
54 */
55 class XBT_PUBLIC Cpu : public Resource {
56   int core_count_ = 1;
57   s4u::Host* host_;
58   int pstate_ = 0;                             /*< Current pstate (index in the speed_per_pstate_)*/
59   const std::vector<double> speed_per_pstate_; /*< List of supported CPU capacities (pstate related) */
60
61 public:
62   /**
63    * @brief Cpu constructor
64    *
65    * @param model The CpuModel associated to this Cpu
66    * @param host The host in which this Cpu should be plugged
67    * @param constraint The lmm constraint associated to this Cpu if it is part of a LMM component
68    * @param speedPerPstate Processor speed (in flop per second) for each pstate
69    * @param core The number of core of this Cpu
70    */
71   Cpu(Model* model, s4u::Host* host, lmm::Constraint* constraint, const std::vector<double>& speed_per_pstate,
72       int core);
73
74   /**
75    * @brief Cpu constructor
76    *
77    * @param model The CpuModel associated to this Cpu
78    * @param host The host in which this Cpu should be plugged
79    * @param speedPerPstate Processor speed (in flop per second) for each pstate
80    * @param core The number of core of this Cpu
81    */
82   Cpu(Model* model, s4u::Host* host, const std::vector<double>& speed_per_pstate, int core);
83
84   Cpu(const Cpu&) = delete;
85   Cpu& operator=(const Cpu&) = delete;
86
87   /**
88    * @brief Execute some quantity of computation
89    *
90    * @param size The value of the processing amount (in flop) needed to process
91    * @return The CpuAction corresponding to the processing
92    */
93   virtual Action* execution_start(double size) = 0;
94
95   /**
96    * @brief Execute some quantity of computation on more than one core
97    *
98    * @param size The value of the processing amount (in flop) needed to process
99    * @param requested_cores The desired amount of cores. Must be >= 1
100    * @return The CpuAction corresponding to the processing
101    */
102   virtual Action* execution_start(double size, int requested_cores) = 0;
103
104   /**
105    * @brief Make a process sleep for duration (in seconds)
106    *
107    * @param duration The number of seconds to sleep
108    * @return The CpuAction corresponding to the sleeping
109    */
110   virtual Action* sleep(double duration) = 0;
111
112   /** @brief Get the amount of cores */
113   virtual int get_core_count();
114
115   /** @brief Get a forecast of the speed (in flops/s) if the load were as provided.
116    *
117    * The provided load should encompasses both the application's activities and the external load that come from a trace.
118    *
119    * Use a load of 1.0 to compute the amount of flops that the Cpu would deliver with one CPU-bound task.
120    * If you use a load of 0, this function will return 0: when nobody is using the Cpu, it delivers nothing.
121    *
122    * If you want to know the amount of flops currently delivered, use  load = get_load()*get_speed_ratio()
123    */
124   virtual double get_speed(double load) const;
125
126 protected:
127   /** @brief Take speed changes (either load or max) into account */
128   virtual void on_speed_change();
129
130 public:
131   /** @brief Get the available speed ratio, between 0 and 1.
132    *
133    * This accounts for external load (see @ref set_speed_trace()).
134    */
135   virtual double get_speed_ratio();
136
137   /** @brief Get the peak processor speed (in flops/s), at the specified pstate */
138   virtual double get_pstate_peak_speed(int pstate_index) const;
139
140   virtual int get_pstate_count() const;
141   virtual void set_pstate(int pstate_index);
142   virtual int get_pstate() const;
143
144   s4u::Host* get_host() { return host_; }
145
146   /*< @brief Setup the trace file with availability events (peak speed changes due to external load).
147    * Trace must contain relative values (ratio between 0 and 1)
148    */
149   virtual void set_speed_profile(profile::Profile* profile);
150
151 protected:
152   Metric speed_                  = {1.0, 0, nullptr};
153 };
154
155 /**********
156  * Action *
157  **********/
158
159  /** @ingroup SURF_cpu_interface
160  * @brief A CpuAction represents the execution of code on one or several Cpus
161  */
162 class XBT_PUBLIC CpuAction : public Action {
163 public:
164   /** @brief Signal emitted when the action state changes (ready/running/done, etc)
165    *  Signature: `void(CpuAction const& action, simgrid::kernel::resource::Action::State previous)`
166    */
167   static xbt::signal<void(CpuAction const&, Action::State)> on_state_change;
168
169   CpuAction(Model* model, double cost, bool failed) : Action(model, cost, failed) {}
170   CpuAction(Model* model, double cost, bool failed, lmm::Variable* var) : Action(model, cost, failed, var) {}
171
172   void set_state(Action::State state) override;
173
174   void update_remains_lazy(double now) override;
175   std::list<Cpu*> cpus() const;
176
177   void suspend() override;
178   void resume() override;
179 };
180 } // namespace resource
181 } // namespace kernel
182 } // namespace simgrid
183
184 #endif /* SURF_CPU_INTERFACE_HPP_ */