Logo AND Algorithmique Numérique Distribuée

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