Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
snake_case another method
[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
14 #include <list>
15
16 /***********
17  * Classes *
18  ***********/
19
20 namespace simgrid {
21 namespace surf {
22
23  /** @ingroup SURF_cpu_interface
24  * @brief SURF cpu model interface class
25  * @details A model is an object which handle the interactions between its Resources and its Actions
26  */
27 class XBT_PUBLIC CpuModel : public kernel::resource::Model {
28 public:
29   /**
30    * @brief Create a Cpu
31    *
32    * @param host The host that will have this CPU
33    * @param speedPerPstate Processor speed (in Flops) of each pstate. This ignores any potential external load coming from a trace.
34    * @param core The number of core of this Cpu
35    */
36   virtual Cpu *createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)=0;
37
38   void updateActionsStateLazy(double now, double delta) override;
39   void updateActionsStateFull(double now, double delta) override;
40 };
41
42 /************
43  * Resource *
44  ************/
45
46 /** @ingroup SURF_cpu_interface
47 * @brief SURF cpu resource interface class
48 * @details A Cpu represent a cpu associated to a host
49 */
50 class XBT_PUBLIC Cpu : public simgrid::kernel::resource::Resource {
51 public:
52   /**
53    * @brief Cpu constructor
54    *
55    * @param model The CpuModel associated to this Cpu
56    * @param host The host in which this Cpu should be plugged
57    * @param constraint The lmm constraint associated to this Cpu if it is part of a LMM component
58    * @param speedPerPstate Processor speed (in flop per second) for each pstate
59    * @param core The number of core of this Cpu
60    */
61   Cpu(simgrid::kernel::resource::Model * model, simgrid::s4u::Host * host, kernel::lmm::Constraint * constraint,
62       std::vector<double> * speedPerPstate, int core);
63
64   /**
65    * @brief Cpu constructor
66    *
67    * @param model The CpuModel associated to this Cpu
68    * @param host The host in which this Cpu should be plugged
69    * @param speedPerPstate Processor speed (in flop per second) for each pstate
70    * @param core The number of core of this Cpu
71    */
72   Cpu(simgrid::kernel::resource::Model * model, simgrid::s4u::Host * host, std::vector<double> * speedPerPstate,
73       int core);
74
75   ~Cpu();
76
77   /**
78    * @brief Execute some quantity of computation
79    *
80    * @param size The value of the processing amount (in flop) needed to process
81    * @return The CpuAction corresponding to the processing
82    */
83   virtual simgrid::kernel::resource::Action* execution_start(double size) = 0;
84
85   /**
86    * @brief Execute some quantity of computation on more than one core
87    *
88    * @param size The value of the processing amount (in flop) needed to process
89    * @param requestedCores The desired amount of cores. Must be >= 1
90    * @return The CpuAction corresponding to the processing
91    */
92   virtual simgrid::kernel::resource::Action* execution_start(double size, int requestedCores) = 0;
93
94   /**
95    * @brief Make a process sleep for duration (in seconds)
96    *
97    * @param duration The number of seconds to sleep
98    * @return The CpuAction corresponding to the sleeping
99    */
100   virtual simgrid::kernel::resource::Action* sleep(double duration) = 0;
101
102   /** @brief Get the amount of cores */
103   virtual int coreCount();
104
105   /** @brief Get the speed, accounting for the trace load and provided process load instead of the real current one */
106   virtual double getSpeed(double load);
107
108 protected:
109   /** @brief Take speed changes (either load or max) into account */
110   virtual void onSpeedChange();
111
112 public:
113   /** @brief Get the available speed of the current Cpu */
114   virtual double getAvailableSpeed();
115
116   /** @brief Get the current Cpu computational speed */
117   virtual double getPstateSpeed(int pstate_index);
118
119   virtual int getNbPStates();
120   virtual void setPState(int pstate_index);
121   virtual int  getPState();
122
123   simgrid::s4u::Host* getHost() { return host_; }
124
125   int coresAmount_ = 1;
126   simgrid::s4u::Host* host_;
127
128   std::vector<double> speedPerPstate_; /*< List of supported CPU capacities (pstate related) */
129   int pstate_ = 0;                     /*< Current pstate (index in the speedPeakList)*/
130
131   virtual void setStateTrace(tmgr_trace_t trace); /*< setup the trace file with states events (ON or OFF). Trace must contain boolean values (0 or 1). */
132   virtual void setSpeedTrace(tmgr_trace_t trace); /*< setup the trace file with availability events (peak speed changes due to external load). Trace must contain relative values (ratio between 0 and 1) */
133
134   tmgr_trace_event_t stateEvent_ = nullptr;
135   Metric speed_                  = {1.0, 0, nullptr};
136 };
137
138 /**********
139  * Action *
140  **********/
141
142  /** @ingroup SURF_cpu_interface
143  * @brief A CpuAction represents the execution of code on one or several Cpus
144  */
145 class XBT_PUBLIC CpuAction : public simgrid::kernel::resource::Action {
146   friend XBT_PUBLIC Cpu* getActionCpu(CpuAction * action);
147
148 public:
149   /** @brief Signal emitted when the action state changes (ready/running/done, etc)
150    *  Signature: `void(CpuAction *action, simgrid::kernel::resource::Action::State previous)`
151    */
152   static simgrid::xbt::signal<void(simgrid::surf::CpuAction*, simgrid::kernel::resource::Action::State)> onStateChange;
153   /** @brief Signal emitted when the action share changes (amount of flops it gets)
154    *  Signature: `void(CpuAction *action)`
155    */
156   static simgrid::xbt::signal<void(simgrid::surf::CpuAction*)> onShareChange;
157
158   CpuAction(simgrid::kernel::resource::Model * model, double cost, bool failed) : Action(model, cost, failed) {}
159   CpuAction(simgrid::kernel::resource::Model * model, double cost, bool failed, kernel::lmm::Variable* var)
160       : Action(model, cost, failed, var)
161   {
162   }
163
164   void set_state(simgrid::kernel::resource::Action::State state) override;
165
166   void update_remains_lazy(double now) override;
167   std::list<Cpu*> cpus();
168   
169   void suspend() override;
170   void resume() override;
171 };
172
173 }
174 }
175
176 #endif /* SURF_CPU_INTERFACE_HPP_ */