Logo AND Algorithmique Numérique Distribuée

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