Logo AND Algorithmique Numérique Distribuée

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