Logo AND Algorithmique Numérique Distribuée

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