Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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
78 CpuCas01Model::~CpuCas01Model()
79 {
80   surf_cpu_model_pm = nullptr;
81 }
82
83 Cpu *CpuCas01Model::createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
84 {
85   return new CpuCas01(this, host, speedPerPstate, core);
86 }
87
88 /************
89  * Resource *
90  ************/
91 CpuCas01::CpuCas01(CpuCas01Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
92     : Cpu(model, host, model->get_maxmin_system()->constraint_new(this, core * speedPerPstate->front()), speedPerPstate,
93           core)
94 {
95 }
96
97 CpuCas01::~CpuCas01()
98 {
99   if (model() == surf_cpu_model_pm)
100     speedPerPstate_.clear();
101 }
102
103 std::vector<double> * CpuCas01::getSpeedPeakList(){
104   return &speedPerPstate_;
105 }
106
107 bool CpuCas01::is_used()
108 {
109   return model()->get_maxmin_system()->constraint_used(constraint());
110 }
111
112 /** @brief take into account changes of speed (either load or max) */
113 void CpuCas01::onSpeedChange() {
114   kernel::lmm::Variable* var = nullptr;
115   const_lmm_element_t elem = nullptr;
116
117   model()->get_maxmin_system()->update_constraint_bound(constraint(), coresAmount_ * speed_.scale * speed_.peak);
118   while ((var = constraint()->get_variable(&elem))) {
119     CpuCas01Action* action = static_cast<CpuCas01Action*>(var->get_id());
120
121     model()->get_maxmin_system()->update_variable_bound(action->get_variable(),
122                                                         action->requestedCore() * speed_.scale * speed_.peak);
123   }
124
125   Cpu::onSpeedChange();
126 }
127
128 void CpuCas01::apply_event(tmgr_trace_event_t event, double value)
129 {
130   if (event == speed_.event) {
131     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
132     xbt_assert(coresAmount_ == 1, "FIXME: add speed scaling code also for constraint_core[i]");
133
134     speed_.scale = value;
135     onSpeedChange();
136
137     tmgr_trace_event_unref(&speed_.event);
138   } else if (event == stateEvent_) {
139     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
140     xbt_assert(coresAmount_ == 1, "FIXME: add state change code also for constraint_core[i]");
141
142     if (value > 0) {
143       if(isOff())
144         host_that_restart.push_back(getHost());
145       turnOn();
146     } else {
147       kernel::lmm::Constraint* cnst = constraint();
148       kernel::lmm::Variable* var    = nullptr;
149       const_lmm_element_t elem = nullptr;
150       double date              = surf_get_clock();
151
152       turnOff();
153
154       while ((var = cnst->get_variable(&elem))) {
155         kernel::resource::Action* action = static_cast<kernel::resource::Action*>(var->get_id());
156
157         if (action->get_state() == kernel::resource::Action::State::running ||
158             action->get_state() == kernel::resource::Action::State::ready ||
159             action->get_state() == kernel::resource::Action::State::not_in_the_system) {
160           action->set_finish_time(date);
161           action->set_state(kernel::resource::Action::State::failed);
162         }
163       }
164     }
165     tmgr_trace_event_unref(&stateEvent_);
166
167   } else {
168     xbt_die("Unknown event!\n");
169   }
170 }
171
172 /** @brief Start a new execution on this CPU lasting @param size flops and using one core */
173 CpuAction* CpuCas01::execution_start(double size)
174 {
175   return new CpuCas01Action(model(), size, isOff(), speed_.scale * speed_.peak, constraint());
176 }
177
178 CpuAction* CpuCas01::execution_start(double size, int requestedCores)
179 {
180   return new CpuCas01Action(model(), size, isOff(), speed_.scale * speed_.peak, constraint(), requestedCores);
181 }
182
183 CpuAction *CpuCas01::sleep(double duration)
184 {
185   if (duration > 0)
186     duration = std::max(duration, sg_surf_precision);
187
188   XBT_IN("(%s,%g)", getCname(), duration);
189   CpuCas01Action* action = new CpuCas01Action(model(), 1.0, isOff(), speed_.scale * speed_.peak, constraint());
190
191   // FIXME: sleep variables should not consume 1.0 in System::expand()
192   action->set_max_duration(duration);
193   action->suspended_ = kernel::resource::Action::SuspendStates::sleeping;
194   if (duration < 0) { // NO_MAX_DURATION
195     /* Move to the *end* of the corresponding action set. This convention is used to speed up update_resource_state */
196     simgrid::xbt::intrusive_erase(*action->get_state_set(), *action);
197     action->state_set_ = &static_cast<CpuCas01Model*>(model())->cpuRunningActionSetThatDoesNotNeedBeingChecked_;
198     action->get_state_set()->push_back(*action);
199   }
200
201   model()->get_maxmin_system()->update_variable_weight(action->get_variable(), 0.0);
202   if (model()->getUpdateMechanism() == kernel::resource::Model::UpdateAlgo::Lazy) { // remove action from the heap
203     action->heapRemove();
204     // this is necessary for a variable with weight 0 since such variables are ignored in lmm and we need to set its
205     // max_duration correctly at the next call to share_resources
206     model()->get_modified_set()->push_front(*action);
207   }
208
209   XBT_OUT();
210   return action;
211 }
212
213 /**********
214  * Action *
215  **********/
216 CpuCas01Action::CpuCas01Action(kernel::resource::Model* model, double cost, bool failed, double speed,
217                                kernel::lmm::Constraint* constraint, int requestedCore)
218     : CpuAction(model, cost, failed,
219                 model->get_maxmin_system()->variable_new(this, 1.0 / requestedCore, requestedCore * speed, 1))
220     , requestedCore_(requestedCore)
221 {
222   if (model->getUpdateMechanism() == kernel::resource::Model::UpdateAlgo::Lazy) {
223     set_last_update();
224     set_last_value(0.0);
225   }
226   model->get_maxmin_system()->expand(constraint, get_variable(), 1.0);
227 }
228
229 CpuCas01Action::CpuCas01Action(kernel::resource::Model* model, double cost, bool failed, double speed,
230                                kernel::lmm::Constraint* constraint)
231     : CpuCas01Action(model, cost, failed, speed, constraint, 1)
232 {
233 }
234
235 int CpuCas01Action::requestedCore()
236 {
237   return requestedCore_;
238 }
239
240 CpuCas01Action::~CpuCas01Action()=default;
241
242 }
243 }