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"
24 /** @ingroup SURF_cpu_interface
25 * @brief SURF cpu model interface class
26 * @details A model is an object which handle the interactions between its Resources and its Actions
28 class XBT_PUBLIC CpuModel : public Model {
30 explicit CpuModel(Model::UpdateAlgo algo) : Model(algo) {}
35 * @param host The host that will have this CPU
36 * @param speed_per_pstate Processor speed (in Flops) of each pstate.
37 * This ignores any potential external load coming from a trace.
38 * @param core The number of core of this Cpu
40 virtual Cpu* create_cpu(s4u::Host* host, const std::vector<double>& speed_per_pstate, int core) = 0;
42 void update_actions_state_lazy(double now, double delta) override;
43 void update_actions_state_full(double now, double delta) override;
52 /** @ingroup SURF_cpu_interface
53 * @brief SURF cpu resource interface class
54 * @details A Cpu represent a cpu associated to a host
56 class XBT_PUBLIC Cpu : public Resource {
59 int pstate_ = 0; /*< Current pstate (index in the speed_per_pstate_)*/
60 const std::vector<double> speed_per_pstate_; /*< List of supported CPU capacities (pstate related) */
64 * @brief Cpu constructor
66 * @param model The CpuModel associated to this Cpu
67 * @param host The host in which this Cpu should be plugged
68 * @param constraint The lmm constraint associated to this Cpu if it is part of a LMM component
69 * @param speedPerPstate Processor speed (in flop per second) for each pstate
70 * @param core The number of core of this Cpu
72 Cpu(Model* model, s4u::Host* host, lmm::Constraint* constraint, const std::vector<double>& speed_per_pstate,
76 * @brief Cpu constructor
78 * @param model The CpuModel associated to this Cpu
79 * @param host The host in which this Cpu should be plugged
80 * @param speedPerPstate Processor speed (in flop per second) for each pstate
81 * @param core The number of core of this Cpu
83 Cpu(Model* model, s4u::Host* host, const std::vector<double>& speed_per_pstate, int core);
85 Cpu(const Cpu&) = delete;
86 Cpu& operator=(const Cpu&) = delete;
89 * @brief Execute some quantity of computation
91 * @param size The value of the processing amount (in flop) needed to process
92 * @return The CpuAction corresponding to the processing
94 virtual CpuAction* execution_start(double size) = 0;
97 * @brief Execute some quantity of computation on more than one core
99 * @param size The value of the processing amount (in flop) needed to process
100 * @param requested_cores The desired amount of cores. Must be >= 1
101 * @return The CpuAction corresponding to the processing
103 virtual CpuAction* execution_start(double size, int requested_cores) = 0;
106 * @brief Make a process sleep for duration (in seconds)
108 * @param duration The number of seconds to sleep
109 * @return The CpuAction corresponding to the sleeping
111 virtual CpuAction* sleep(double duration) = 0;
113 /** @brief Get the amount of cores */
114 virtual int get_core_count();
116 /** @brief Get a forecast of the speed (in flops/s) if the load were as provided.
118 * The provided load should encompasses both the application's activities and the external load that come from a trace.
120 * Use a load of 1.0 to compute the amount of flops that the Cpu would deliver with one CPU-bound task.
121 * If you use a load of 0, this function will return 0: when nobody is using the Cpu, it delivers nothing.
123 * If you want to know the amount of flops currently delivered, use load = get_load()*get_speed_ratio()
125 virtual double get_speed(double load) const;
128 /** @brief Take speed changes (either load or max) into account */
129 virtual void on_speed_change();
132 /** @brief Get the available speed ratio, between 0 and 1.
134 * This accounts for external load (see @ref set_speed_trace()).
136 virtual double get_speed_ratio();
138 /** @brief Get the peak processor speed (in flops/s), at the specified pstate */
139 virtual double get_pstate_peak_speed(int pstate_index) const;
141 virtual int get_pstate_count() const;
142 virtual void set_pstate(int pstate_index);
143 virtual int get_pstate() const;
145 s4u::Host* get_host() { return host_; }
147 /*< @brief Setup the trace file with availability events (peak speed changes due to external load).
148 * Trace must contain relative values (ratio between 0 and 1)
150 virtual void set_speed_profile(profile::Profile* profile);
153 Metric speed_ = {1.0, 0, nullptr};
160 /** @ingroup SURF_cpu_interface
161 * @brief A CpuAction represents the execution of code on one or several Cpus
163 class XBT_PUBLIC CpuAction : public Action {
165 /** @brief Signal emitted when the action state changes (ready/running/done, etc)
166 * Signature: `void(CpuAction const& action, simgrid::kernel::resource::Action::State previous)`
168 static xbt::signal<void(CpuAction const&, Action::State)> on_state_change;
170 CpuAction(Model* model, double cost, bool failed) : Action(model, cost, failed) {}
171 CpuAction(Model* model, double cost, bool failed, lmm::Variable* var) : Action(model, cost, failed, var) {}
173 void set_state(Action::State state) override;
175 void update_remains_lazy(double now) override;
176 std::list<Cpu*> cpus() const;
178 void suspend() override;
179 void resume() override;
181 } // namespace resource
182 } // namespace kernel
183 } // namespace simgrid
185 #endif /* SURF_CPU_INTERFACE_HPP_ */