Logo AND Algorithmique Numérique Distribuée

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