Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
give s_val PJ_value as a constructor
[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/surf/maxmin_private.hpp"
11
12 #include <list>
13
14 /***********
15  * Classes *
16  ***********/
17
18 namespace simgrid {
19 namespace surf {
20
21 class CpuModel;
22 class Cpu;
23 class CpuAction;
24
25  /** @ingroup SURF_cpu_interface
26  * @brief SURF cpu model interface class
27  * @details A model is an object which handle the interactions between its Resources and its Actions
28  */
29 XBT_PUBLIC_CLASS CpuModel : public Model {
30 public:
31   /**
32    * @brief Create a Cpu
33    *
34    * @param host The host that will have this CPU
35    * @param speedPerPstate Processor speed (in Flops) of each pstate. This ignores any potential external load coming from a trace.
36    * @param core The number of core of this Cpu
37    */
38   virtual Cpu *createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)=0;
39
40   void updateActionsStateLazy(double now, double delta) override;
41   void updateActionsStateFull(double now, double delta) override;
42 };
43
44 /************
45  * Resource *
46  ************/
47
48 /** @ingroup SURF_cpu_interface
49 * @brief SURF cpu resource interface class
50 * @details A Cpu represent a cpu associated to a host
51 */
52 XBT_PUBLIC_CLASS Cpu : public simgrid::surf::Resource {
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::surf::Model *model, simgrid::s4u::Host *host, lmm_constraint_t 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::surf::Model *model, simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core);
75
76   ~Cpu();
77
78   /**
79    * @brief Execute some quantity of computation
80    *
81    * @param size The value of the processing amount (in flop) needed to process
82    * @return The CpuAction corresponding to the processing
83    */
84   virtual simgrid::surf::Action *execution_start(double size)=0;
85
86   /**
87    * @brief Execute some quantity of computation on more than one core
88    *
89    * @param size The value of the processing amount (in flop) needed to process
90    * @param requestedCores The desired amount of cores. Must be >= 1
91    * @return The CpuAction corresponding to the processing
92    */
93   virtual simgrid::surf::Action* execution_start(double size, int requestedCores)
94   {
95     THROW_UNIMPLEMENTED;
96     return nullptr;
97   }
98
99   /**
100    * @brief Make a process sleep for duration (in seconds)
101    *
102    * @param duration The number of seconds to sleep
103    * @return The CpuAction corresponding to the sleeping
104    */
105   virtual simgrid::surf::Action *sleep(double duration)=0;
106
107   /** @brief Get the amount of cores */
108   virtual int coreCount();
109
110   /** @brief Get the speed, accounting for the trace load and provided process load instead of the real current one */
111   virtual double getSpeed(double load);
112
113 protected:
114   /** @brief Take speed changes (either load or max) into account */
115   virtual void onSpeedChange();
116
117 public:
118   /** @brief Get the available speed of the current Cpu */
119   virtual double getAvailableSpeed();
120
121   /** @brief Get the current Cpu computational speed */
122   virtual double getPstateSpeed(int pstate_index);
123
124   virtual int getNbPStates();
125   virtual void setPState(int pstate_index);
126   virtual int  getPState();
127
128   simgrid::s4u::Host* getHost() { return host_; }
129
130   int coresAmount_ = 1;
131   simgrid::s4u::Host* host_;
132
133   std::vector<double> speedPerPstate_; /*< List of supported CPU capacities (pstate related) */
134   int pstate_ = 0;                     /*< Current pstate (index in the speedPeakList)*/
135
136   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). */
137   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) */
138
139   tmgr_trace_event_t stateEvent_ = nullptr;
140   s_surf_metric_t speed_ = {1.0, 0, nullptr};
141 };
142
143 /**********
144  * Action *
145  **********/
146
147  /** @ingroup SURF_cpu_interface
148  * @brief A CpuAction represents the execution of code on one or several Cpus
149  */
150 XBT_PUBLIC_CLASS CpuAction : public simgrid::surf::Action {
151 friend XBT_PUBLIC(Cpu*) getActionCpu(CpuAction *action);
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)
163   : Action(model, cost, failed) {}
164   CpuAction(simgrid::surf::Model *model, double cost, bool failed, lmm_variable_t var)
165   : Action(model, cost, failed, var) {}
166
167   void setState(simgrid::surf::Action::State state) override;
168
169   void updateRemainingLazy(double now) override;
170   std::list<Cpu*> cpus();
171 };
172
173 }
174 }
175
176 #endif /* SURF_CPU_INTERFACE_HPP_ */