1 /* Copyright (c) 2004-2019. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SURF_CPU_INTERFACE_HPP_
7 #define SURF_CPU_INTERFACE_HPP_
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/kernel/resource/profile/trace_mgr.hpp"
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
29 class XBT_PUBLIC CpuModel : public Model {
31 explicit CpuModel(Model::UpdateAlgo algo) : Model(algo) {}
36 * @param host The host that will have this CPU
37 * @param speed_per_pstate Processor speed (in Flops) of each pstate.
38 * This ignores any potential external load coming from a trace.
39 * @param core The number of core of this Cpu
41 virtual Cpu* create_cpu(s4u::Host* host, const std::vector<double>& speed_per_pstate, int core) = 0;
43 void update_actions_state_lazy(double now, double delta) override;
44 void update_actions_state_full(double now, double delta) override;
53 /** @ingroup SURF_cpu_interface
54 * @brief SURF cpu resource interface class
55 * @details A Cpu represent a cpu associated to a host
57 class XBT_PUBLIC Cpu : public Resource {
60 int pstate_ = 0; /*< Current pstate (index in the speed_per_pstate_)*/
61 const std::vector<double> speed_per_pstate_; /*< List of supported CPU capacities (pstate related) */
65 * @brief Cpu constructor
67 * @param model The CpuModel associated to this Cpu
68 * @param host The host in which this Cpu should be plugged
69 * @param constraint The lmm constraint associated to this Cpu if it is part of a LMM component
70 * @param speedPerPstate Processor speed (in flop per second) for each pstate
71 * @param core The number of core of this Cpu
73 Cpu(Model* model, s4u::Host* host, lmm::Constraint* constraint, const std::vector<double>& speed_per_pstate,
77 * @brief Cpu constructor
79 * @param model The CpuModel associated to this Cpu
80 * @param host The host in which this Cpu should be plugged
81 * @param speedPerPstate Processor speed (in flop per second) for each pstate
82 * @param core The number of core of this Cpu
84 Cpu(Model* model, s4u::Host* host, const std::vector<double>& speed_per_pstate, int core);
86 Cpu(const Cpu&) = delete;
87 Cpu& operator=(const Cpu&) = delete;
90 * @brief Execute some quantity of computation
92 * @param size The value of the processing amount (in flop) needed to process
93 * @return The CpuAction corresponding to the processing
95 virtual CpuAction* execution_start(double size) = 0;
98 * @brief Execute some quantity of computation on more than one core
100 * @param size The value of the processing amount (in flop) needed to process
101 * @param requested_cores The desired amount of cores. Must be >= 1
102 * @return The CpuAction corresponding to the processing
104 virtual CpuAction* execution_start(double size, int requested_cores) = 0;
107 * @brief Make a process sleep for duration (in seconds)
109 * @param duration The number of seconds to sleep
110 * @return The CpuAction corresponding to the sleeping
112 virtual CpuAction* sleep(double duration) = 0;
114 /** @brief Get the amount of cores */
115 virtual int get_core_count();
117 /** @brief Get a forecast of the speed (in flops/s) if the load were as provided.
119 * The provided load should encompasses both the application's activities and the external load that come from a trace.
121 * Use a load of 1.0 to compute the amount of flops that the Cpu would deliver with one CPU-bound task.
122 * If you use a load of 0, this function will return 0: when nobody is using the Cpu, it delivers nothing.
124 * If you want to know the amount of flops currently delivered, use load = get_load()*get_speed_ratio()
126 virtual double get_speed(double load) const;
129 /** @brief Take speed changes (either load or max) into account */
130 virtual void on_speed_change();
133 /** @brief Get the available speed ratio, between 0 and 1.
135 * This accounts for external load (see @ref set_speed_trace()).
137 virtual double get_speed_ratio();
139 /** @brief Get the peak processor speed (in flops/s), at the specified pstate */
140 virtual double get_pstate_peak_speed(int pstate_index) const;
142 virtual int get_pstate_count() const;
143 virtual void set_pstate(int pstate_index);
144 virtual int get_pstate() const;
146 s4u::Host* get_host() { return host_; }
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)
151 virtual void set_speed_profile(profile::Profile* profile);
154 Metric speed_ = {1.0, 0, nullptr};
161 /** @ingroup SURF_cpu_interface
162 * @brief A CpuAction represents the execution of code on one or several Cpus
164 class XBT_PUBLIC CpuAction : public Action {
166 /** @brief Signal emitted when the action state changes (ready/running/done, etc)
167 * Signature: `void(CpuAction const& action, simgrid::kernel::resource::Action::State previous)`
169 static xbt::signal<void(CpuAction const&, Action::State)> on_state_change;
171 CpuAction(Model* model, double cost, bool failed) : Action(model, cost, failed) {}
172 CpuAction(Model* model, double cost, bool failed, lmm::Variable* var) : Action(model, cost, failed, var) {}
174 void set_state(Action::State state) override;
176 void update_remains_lazy(double now) override;
177 std::list<Cpu*> cpus() const;
179 void suspend() override;
180 void resume() override;
182 } // namespace resource
183 } // namespace kernel
184 } // namespace simgrid
186 #endif /* SURF_CPU_INTERFACE_HPP_ */