Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
lmm::System: rename some fields, and initialize them at declaration, not in ctor
[simgrid.git] / src / surf / cpu_cas01.cpp
1 /* Copyright (c) 2009-2018. 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 #include "cpu_cas01.hpp"
7 #include "cpu_ti.hpp"
8 #include "simgrid/sg_config.hpp"
9 #include "src/kernel/lmm/maxmin.hpp"
10 #include "xbt/config.hpp"
11 #include "xbt/utility.hpp"
12
13 #include <algorithm>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_cas, surf_cpu, "Logging specific to the SURF CPU module");
16
17 /***********
18  * Options *
19  ***********/
20
21 static simgrid::config::Flag<std::string>
22     cpu_optim_opt("cpu/optim", "Optimization algorithm to use for CPU resources. ", "Lazy",
23
24                   std::map<std::string, std::string>({
25                       {"Lazy", "Lazy action management (partial invalidation in lmm + heap in action remaining)."},
26                       {"TI", "Trace integration. Highly optimized mode when using availability traces (only available "
27                              "for the Cas01 CPU model for now)."},
28                       {"Full", "Full update of remaining and variables. Slow but may be useful when debugging."},
29                   }),
30
31                   [](std::string const& val) {
32                     xbt_assert(_sg_cfg_init_status < 2,
33                                "Cannot change the optimization algorithm after the initialization");
34                   });
35
36 /*********
37  * Model *
38  *********/
39 void surf_cpu_model_init_Cas01()
40 {
41   xbt_assert(not surf_cpu_model_pm);
42   xbt_assert(not surf_cpu_model_vm);
43
44   if (cpu_optim_opt == "TI") {
45     surf_cpu_model_init_ti();
46     return;
47   }
48
49   simgrid::kernel::resource::Model::UpdateAlgo algo;
50   if (cpu_optim_opt == "Lazy")
51     algo = simgrid::kernel::resource::Model::UpdateAlgo::Lazy;
52   else
53     algo = simgrid::kernel::resource::Model::UpdateAlgo::Full;
54
55   surf_cpu_model_pm = new simgrid::surf::CpuCas01Model(algo);
56   all_existing_models->push_back(surf_cpu_model_pm);
57
58   surf_cpu_model_vm = new simgrid::surf::CpuCas01Model(algo);
59   all_existing_models->push_back(surf_cpu_model_vm);
60 }
61
62 namespace simgrid {
63 namespace surf {
64
65 CpuCas01Model::CpuCas01Model(kernel::resource::Model::UpdateAlgo algo) : simgrid::surf::CpuModel(algo)
66 {
67   bool select = xbt_cfg_get_boolean("cpu/maxmin-selective-update");
68
69   if (algo == Model::UpdateAlgo::Lazy) {
70     xbt_assert(select || xbt_cfg_is_default_value("cpu/maxmin-selective-update"),
71                "You cannot disable cpu selective update when using the lazy update mechanism");
72     select = true;
73   }
74
75   set_maxmin_system(new simgrid::kernel::lmm::System(select));
76
77   if (algo == Model::UpdateAlgo::Lazy)
78     get_maxmin_system()->modified_set_ = new kernel::resource::Action::ModifiedSet();
79 }
80
81 CpuCas01Model::~CpuCas01Model()
82 {
83   surf_cpu_model_pm = nullptr;
84 }
85
86 Cpu *CpuCas01Model::createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
87 {
88   return new CpuCas01(this, host, speedPerPstate, core);
89 }
90
91 /************
92  * Resource *
93  ************/
94 CpuCas01::CpuCas01(CpuCas01Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
95     : Cpu(model, host, model->get_maxmin_system()->constraint_new(this, core * speedPerPstate->front()), speedPerPstate,
96           core)
97 {
98 }
99
100 CpuCas01::~CpuCas01()
101 {
102   if (model() == surf_cpu_model_pm)
103     speedPerPstate_.clear();
104 }
105
106 std::vector<double> * CpuCas01::getSpeedPeakList(){
107   return &speedPerPstate_;
108 }
109
110 bool CpuCas01::isUsed()
111 {
112   return model()->get_maxmin_system()->constraint_used(constraint());
113 }
114
115 /** @brief take into account changes of speed (either load or max) */
116 void CpuCas01::onSpeedChange() {
117   kernel::lmm::Variable* var = nullptr;
118   const_lmm_element_t elem = nullptr;
119
120   model()->get_maxmin_system()->update_constraint_bound(constraint(), coresAmount_ * speed_.scale * speed_.peak);
121   while ((var = constraint()->get_variable(&elem))) {
122     CpuCas01Action* action = static_cast<CpuCas01Action*>(var->get_id());
123
124     model()->get_maxmin_system()->update_variable_bound(action->get_variable(),
125                                                         action->requestedCore() * speed_.scale * speed_.peak);
126   }
127
128   Cpu::onSpeedChange();
129 }
130
131 void CpuCas01::apply_event(tmgr_trace_event_t event, double value)
132 {
133   if (event == speed_.event) {
134     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
135     xbt_assert(coresAmount_ == 1, "FIXME: add speed scaling code also for constraint_core[i]");
136
137     speed_.scale = value;
138     onSpeedChange();
139
140     tmgr_trace_event_unref(&speed_.event);
141   } else if (event == stateEvent_) {
142     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
143     xbt_assert(coresAmount_ == 1, "FIXME: add state change code also for constraint_core[i]");
144
145     if (value > 0) {
146       if(isOff())
147         host_that_restart.push_back(getHost());
148       turnOn();
149     } else {
150       kernel::lmm::Constraint* cnst = constraint();
151       kernel::lmm::Variable* var    = nullptr;
152       const_lmm_element_t elem = nullptr;
153       double date              = surf_get_clock();
154
155       turnOff();
156
157       while ((var = cnst->get_variable(&elem))) {
158         kernel::resource::Action* action = static_cast<kernel::resource::Action*>(var->get_id());
159
160         if (action->get_state() == kernel::resource::Action::State::running ||
161             action->get_state() == kernel::resource::Action::State::ready ||
162             action->get_state() == kernel::resource::Action::State::not_in_the_system) {
163           action->set_finish_time(date);
164           action->set_state(kernel::resource::Action::State::failed);
165         }
166       }
167     }
168     tmgr_trace_event_unref(&stateEvent_);
169
170   } else {
171     xbt_die("Unknown event!\n");
172   }
173 }
174
175 /** @brief Start a new execution on this CPU lasting @param size flops and using one core */
176 CpuAction* CpuCas01::execution_start(double size)
177 {
178   return new CpuCas01Action(model(), size, isOff(), speed_.scale * speed_.peak, constraint());
179 }
180
181 CpuAction* CpuCas01::execution_start(double size, int requestedCores)
182 {
183   return new CpuCas01Action(model(), size, isOff(), speed_.scale * speed_.peak, constraint(), requestedCores);
184 }
185
186 CpuAction *CpuCas01::sleep(double duration)
187 {
188   if (duration > 0)
189     duration = std::max(duration, sg_surf_precision);
190
191   XBT_IN("(%s,%g)", getCname(), duration);
192   CpuCas01Action* action = new CpuCas01Action(model(), 1.0, isOff(), speed_.scale * speed_.peak, constraint());
193
194   // FIXME: sleep variables should not consume 1.0 in System::expand()
195   action->set_max_duration(duration);
196   action->suspended_ = kernel::resource::Action::SuspendStates::sleeping;
197   if (duration < 0) { // NO_MAX_DURATION
198     /* Move to the *end* of the corresponding action set. This convention is used to speed up update_resource_state */
199     simgrid::xbt::intrusive_erase(*action->get_state_set(), *action);
200     action->state_set_ = &static_cast<CpuCas01Model*>(model())->cpuRunningActionSetThatDoesNotNeedBeingChecked_;
201     action->get_state_set()->push_back(*action);
202   }
203
204   model()->get_maxmin_system()->update_variable_weight(action->get_variable(), 0.0);
205   if (model()->getUpdateMechanism() == kernel::resource::Model::UpdateAlgo::Lazy) { // remove action from the heap
206     action->heapRemove();
207     // this is necessary for a variable with weight 0 since such variables are ignored in lmm and we need to set its
208     // max_duration correctly at the next call to share_resources
209     model()->get_modified_set()->push_front(*action);
210   }
211
212   XBT_OUT();
213   return action;
214 }
215
216 /**********
217  * Action *
218  **********/
219 CpuCas01Action::CpuCas01Action(kernel::resource::Model* model, double cost, bool failed, double speed,
220                                kernel::lmm::Constraint* constraint, int requestedCore)
221     : CpuAction(model, cost, failed,
222                 model->get_maxmin_system()->variable_new(this, 1.0 / requestedCore, requestedCore * speed, 1))
223     , requestedCore_(requestedCore)
224 {
225   if (model->getUpdateMechanism() == kernel::resource::Model::UpdateAlgo::Lazy) {
226     set_last_update();
227     set_last_value(0.0);
228   }
229   model->get_maxmin_system()->expand(constraint, get_variable(), 1.0);
230 }
231
232 CpuCas01Action::CpuCas01Action(kernel::resource::Model* model, double cost, bool failed, double speed,
233                                kernel::lmm::Constraint* constraint)
234     : CpuCas01Action(model, cost, failed, speed, constraint, 1)
235 {
236 }
237
238 int CpuCas01Action::requestedCore()
239 {
240   return requestedCore_;
241 }
242
243 CpuCas01Action::~CpuCas01Action()=default;
244
245 }
246 }