Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / src / surf / cpu_ti.hpp
1 /* Copyright (c) 2013-2021. 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_MODEL_CPUTI_H_
7 #define SURF_MODEL_CPUTI_H_
8
9 #include "src/kernel/resource/profile/Profile.hpp"
10 #include "src/surf/cpu_interface.hpp"
11
12 #include <boost/intrusive/list.hpp>
13 #include <memory>
14
15 namespace simgrid {
16 namespace kernel {
17 namespace resource {
18
19 /***********
20  * Classes *
21  ***********/
22 class XBT_PRIVATE CpuTiModel;
23 class XBT_PRIVATE CpuTi;
24
25 /*********
26  * Trace *
27  *********/
28 class CpuTiProfile {
29 public:
30   explicit CpuTiProfile(const profile::Profile* profile);
31
32   double integrate_simple(double a, double b) const;
33   double integrate_simple_point(double a) const;
34   double solve_simple(double a, double amount) const;
35
36   std::vector<double> time_points_;
37   std::vector<double> integral_;
38   static int binary_search(const std::vector<double>& array, double a);
39 };
40
41 class CpuTiTmgr {
42   enum class Type {
43     FIXED,  /*< Trace fixed, no availability file */
44     DYNAMIC /*< Dynamic, have an availability file */
45   };
46
47 public:
48   explicit CpuTiTmgr(double value) : value_(value){};
49   CpuTiTmgr(profile::Profile* speed_profile, double value);
50   CpuTiTmgr(const CpuTiTmgr&) = delete;
51   CpuTiTmgr& operator=(const CpuTiTmgr&) = delete;
52
53   double integrate(double a, double b) const;
54   double solve(double a, double amount) const;
55   double get_power_scale(double a) const;
56
57 private:
58   Type type_ = Type::FIXED;
59   double value_;                 /*< Percentage of cpu speed available. Value fixed between 0 and 1 */
60
61   /* Dynamic */
62   double last_time_ = 0.0;             /*< Integral interval last point (discrete time) */
63   double total_     = 0.0;             /*< Integral total between 0 and last point */
64
65   std::unique_ptr<CpuTiProfile> profile_ = nullptr;
66   profile::Profile* speed_profile_       = nullptr;
67 };
68
69 /**********
70  * Action *
71  **********/
72
73 class XBT_PRIVATE CpuTiAction : public CpuAction {
74   friend class CpuTi;
75 public:
76   CpuTiAction(CpuTi* cpu, double cost);
77   CpuTiAction(const CpuTiAction&) = delete;
78   CpuTiAction& operator=(const CpuTiAction&) = delete;
79   ~CpuTiAction() override;
80
81   void set_state(Action::State state) override;
82   void cancel() override;
83   void suspend() override;
84   void resume() override;
85   void set_max_duration(double duration) override;
86   void set_sharing_penalty(double sharing_penalty) override;
87   double get_remains() override;
88
89   CpuTi *cpu_;
90
91   boost::intrusive::list_member_hook<> action_ti_hook;
92 };
93
94 using ActionTiListOptions =
95     boost::intrusive::member_hook<CpuTiAction, boost::intrusive::list_member_hook<>, &CpuTiAction::action_ti_hook>;
96 using ActionTiList = boost::intrusive::list<CpuTiAction, ActionTiListOptions>;
97
98 /************
99  * Resource *
100  ************/
101 class CpuTi : public Cpu {
102 public:
103   CpuTi(CpuTiModel* model, s4u::Host* host, const std::vector<double>& speed_per_pstate, int core);
104   CpuTi(const CpuTi&)            = delete;
105   CpuTi& operator&(const CpuTi&) = delete;
106   ~CpuTi() override;
107
108   void set_speed_profile(profile::Profile* profile) override;
109
110   void apply_event(profile::Event* event, double value) override;
111   void update_actions_finish_time(double now);
112   void update_remaining_amount(double now);
113
114   bool is_used() const override;
115   CpuAction* execution_start(double size) override;
116   CpuAction* execution_start(double, int) override
117   {
118     THROW_UNIMPLEMENTED;
119     return nullptr;
120   }
121   CpuAction* sleep(double duration) override;
122   double get_speed_ratio() override;
123
124   void set_modified(bool modified);
125
126   CpuTiTmgr* speed_integrated_trace_ = nullptr; /*< Structure with data needed to integrate trace file */
127   ActionTiList action_set_;                     /*< set with all actions running on cpu */
128   double sum_priority_ = 0;                  /*< the sum of actions' priority that are running on cpu */
129   double last_update_  = 0;                  /*< last update of actions' remaining amount done */
130
131   boost::intrusive::list_member_hook<> cpu_ti_hook;
132 };
133
134 using CpuTiListOptions =
135     boost::intrusive::member_hook<CpuTi, boost::intrusive::list_member_hook<>, &CpuTi::cpu_ti_hook>;
136 using CpuTiList = boost::intrusive::list<CpuTi, CpuTiListOptions>;
137
138 /*********
139  * Model *
140  *********/
141 class CpuTiModel : public CpuModel {
142 public:
143   static void create_pm_vm_models(); // Make both models be TI models
144
145   CpuTiModel();
146   CpuTiModel(const CpuTiModel&) = delete;
147   CpuTiModel& operator=(const CpuTiModel&) = delete;
148   ~CpuTiModel() override;
149   Cpu* create_cpu(s4u::Host* host, const std::vector<double>& speed_per_pstate, int core) override;
150   double next_occurring_event(double now) override;
151   void update_actions_state(double now, double delta) override;
152
153   CpuTiList modified_cpus_;
154 };
155
156 } // namespace resource
157 } // namespace kernel
158 } // namespace simgrid
159
160 #endif /* SURF_MODEL_CPUTI_H_ */