Logo AND Algorithmique Numérique Distribuée

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