Logo AND Algorithmique Numérique Distribuée

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