Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
42e1a48dad4d0edc8fb7ccc872a5a2134a5e7368
[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 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 XBT_PUBLIC_CLASS Cpu : public simgrid::kernel::resource::Resource
51 {
52 public:
53   /**
54    * @brief Cpu constructor
55    *
56    * @param model The CpuModel associated to this Cpu
57    * @param host The host in which this Cpu should be plugged
58    * @param constraint The lmm constraint associated to this Cpu if it is part of a LMM component
59    * @param speedPerPstate Processor speed (in flop per second) for each pstate
60    * @param core The number of core of this Cpu
61    */
62   Cpu(simgrid::surf::Model * model, simgrid::s4u::Host * host, kernel::lmm::Constraint * constraint,
63       std::vector<double> * speedPerPstate, int core);
64
65   /**
66    * @brief Cpu constructor
67    *
68    * @param model The CpuModel associated to this Cpu
69    * @param host The host in which this Cpu should be plugged
70    * @param speedPerPstate Processor speed (in flop per second) for each pstate
71    * @param core The number of core of this Cpu
72    */
73   Cpu(simgrid::surf::Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, 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::surf::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::surf::Action* execution_start(double size, int requestedCores)
93   {
94     THROW_UNIMPLEMENTED;
95     return nullptr;
96   }
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::surf::Action *sleep(double duration)=0;
105
106   /** @brief Get the amount of cores */
107   virtual int coreCount();
108
109   /** @brief Get the speed, accounting for the trace load and provided process load instead of the real current one */
110   virtual double getSpeed(double load);
111
112 protected:
113   /** @brief Take speed changes (either load or max) into account */
114   virtual void onSpeedChange();
115
116 public:
117   /** @brief Get the available speed of the current Cpu */
118   virtual double getAvailableSpeed();
119
120   /** @brief Get the current Cpu computational speed */
121   virtual double getPstateSpeed(int pstate_index);
122
123   virtual int getNbPStates();
124   virtual void setPState(int pstate_index);
125   virtual int  getPState();
126
127   simgrid::s4u::Host* getHost() { return host_; }
128
129   int coresAmount_ = 1;
130   simgrid::s4u::Host* host_;
131
132   std::vector<double> speedPerPstate_; /*< List of supported CPU capacities (pstate related) */
133   int pstate_ = 0;                     /*< Current pstate (index in the speedPeakList)*/
134
135   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). */
136   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) */
137
138   tmgr_trace_event_t stateEvent_ = nullptr;
139   Metric speed_                  = {1.0, 0, nullptr};
140 };
141
142 /**********
143  * Action *
144  **********/
145
146  /** @ingroup SURF_cpu_interface
147  * @brief A CpuAction represents the execution of code on one or several Cpus
148  */
149 XBT_PUBLIC_CLASS CpuAction : public simgrid::surf::Action {
150   friend XBT_PUBLIC(Cpu*) getActionCpu(CpuAction* action);
151
152 public:
153   /** @brief Signal emitted when the action state changes (ready/running/done, etc)
154    *  Signature: `void(CpuAction *action, simgrid::surf::Action::State previous)`
155    */
156   static simgrid::xbt::signal<void(simgrid::surf::CpuAction*, simgrid::surf::Action::State)> onStateChange;
157   /** @brief Signal emitted when the action share changes (amount of flops it gets)
158    *  Signature: `void(CpuAction *action)`
159    */
160   static simgrid::xbt::signal<void(simgrid::surf::CpuAction*)> onShareChange;
161
162   CpuAction(simgrid::surf::Model* model, double cost, bool failed) : Action(model, cost, failed) {}
163   CpuAction(simgrid::surf::Model * model, double cost, bool failed, kernel::lmm::Variable* var)
164       : Action(model, cost, failed, var)
165   {
166   }
167
168   void setState(simgrid::surf::Action::State state) override;
169
170   void updateRemainingLazy(double now) override;
171   std::list<Cpu*> cpus();
172   
173   void suspend() override;
174   void resume() override;
175 };
176
177 }
178 }
179
180 #endif /* SURF_CPU_INTERFACE_HPP_ */