Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Replace a protected field with a private variable in the child' constructor
[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.h"
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(UM_FULL);
45   } else if (optim == "Lazy") {
46     setUpdateMechanism(UM_LAZY);
47     select = true;
48     xbt_assert(select || (xbt_cfg_is_default_value("cpu/maxmin-selective-update")),
49                "Disabling selective update while using the lazy update mechanism is dumb!");
50   } else {
51     xbt_die("Unsupported optimization (%s) for this model", optim.c_str());
52   }
53
54   maxminSystem_ = new simgrid::kernel::lmm::System(select);
55
56   if (getUpdateMechanism() == UM_LAZY) {
57     modifiedSet_              = new kernel::resource::ActionLmmList();
58     maxminSystem_->keep_track = modifiedSet_;
59   }
60 }
61
62 CpuCas01Model::~CpuCas01Model()
63 {
64   surf_cpu_model_pm = nullptr;
65 }
66
67 Cpu *CpuCas01Model::createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
68 {
69   return new CpuCas01(this, host, speedPerPstate, core);
70 }
71
72 /************
73  * Resource *
74  ************/
75 CpuCas01::CpuCas01(CpuCas01Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
76     : Cpu(model, host, model->getMaxminSystem()->constraint_new(this, core * speedPerPstate->front()), speedPerPstate,
77           core)
78 {
79 }
80
81 CpuCas01::~CpuCas01()
82 {
83   if (model() == surf_cpu_model_pm)
84     speedPerPstate_.clear();
85 }
86
87 std::vector<double> * CpuCas01::getSpeedPeakList(){
88   return &speedPerPstate_;
89 }
90
91 bool CpuCas01::isUsed()
92 {
93   return model()->getMaxminSystem()->constraint_used(constraint());
94 }
95
96 /** @brief take into account changes of speed (either load or max) */
97 void CpuCas01::onSpeedChange() {
98   kernel::lmm::Variable* var = nullptr;
99   const_lmm_element_t elem = nullptr;
100
101   model()->getMaxminSystem()->update_constraint_bound(constraint(), coresAmount_ * speed_.scale * speed_.peak);
102   while ((var = constraint()->get_variable(&elem))) {
103     CpuCas01Action* action = static_cast<CpuCas01Action*>(var->get_id());
104
105     model()->getMaxminSystem()->update_variable_bound(action->getVariable(),
106                                                       action->requestedCore() * speed_.scale * speed_.peak);
107   }
108
109   Cpu::onSpeedChange();
110 }
111
112 void CpuCas01::apply_event(tmgr_trace_event_t event, double value)
113 {
114   if (event == speed_.event) {
115     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
116     xbt_assert(coresAmount_ == 1, "FIXME: add speed scaling code also for constraint_core[i]");
117
118     speed_.scale = value;
119     onSpeedChange();
120
121     tmgr_trace_event_unref(&speed_.event);
122   } else if (event == stateEvent_) {
123     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
124     xbt_assert(coresAmount_ == 1, "FIXME: add state change code also for constraint_core[i]");
125
126     if (value > 0) {
127       if(isOff())
128         host_that_restart.push_back(getHost());
129       turnOn();
130     } else {
131       kernel::lmm::Constraint* cnst = constraint();
132       kernel::lmm::Variable* var    = nullptr;
133       const_lmm_element_t elem = nullptr;
134       double date              = surf_get_clock();
135
136       turnOff();
137
138       while ((var = cnst->get_variable(&elem))) {
139         kernel::resource::Action* action = static_cast<kernel::resource::Action*>(var->get_id());
140
141         if (action->getState() == kernel::resource::Action::State::running ||
142             action->getState() == kernel::resource::Action::State::ready ||
143             action->getState() == kernel::resource::Action::State::not_in_the_system) {
144           action->setFinishTime(date);
145           action->setState(kernel::resource::Action::State::failed);
146         }
147       }
148     }
149     tmgr_trace_event_unref(&stateEvent_);
150
151   } else {
152     xbt_die("Unknown event!\n");
153   }
154 }
155
156 /** @brief Start a new execution on this CPU lasting @param size flops and using one core */
157 CpuAction* CpuCas01::execution_start(double size)
158 {
159   return new CpuCas01Action(model(), size, isOff(), speed_.scale * speed_.peak, constraint());
160 }
161
162 CpuAction* CpuCas01::execution_start(double size, int requestedCores)
163 {
164   return new CpuCas01Action(model(), size, isOff(), speed_.scale * speed_.peak, constraint(), requestedCores);
165 }
166
167 CpuAction *CpuCas01::sleep(double duration)
168 {
169   if (duration > 0)
170     duration = std::max(duration, sg_surf_precision);
171
172   XBT_IN("(%s,%g)", getCname(), duration);
173   CpuCas01Action* action = new CpuCas01Action(model(), 1.0, isOff(), speed_.scale * speed_.peak, constraint());
174
175   // FIXME: sleep variables should not consume 1.0 in System::expand()
176   action->setMaxDuration(duration);
177   action->suspended_ = kernel::resource::Action::SuspendStates::sleeping;
178   if (duration < 0) { // NO_MAX_DURATION
179     /* Move to the *end* of the corresponding action set. This convention is used to speed up update_resource_state */
180     simgrid::xbt::intrusive_erase(*action->getStateSet(), *action);
181     action->stateSet_ = &static_cast<CpuCas01Model*>(model())->p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
182     action->getStateSet()->push_back(*action);
183   }
184
185   model()->getMaxminSystem()->update_variable_weight(action->getVariable(), 0.0);
186   if (model()->getUpdateMechanism() == UM_LAZY) { // remove action from the heap
187     action->heapRemove(model()->getActionHeap());
188     // this is necessary for a variable with weight 0 since such variables are ignored in lmm and we need to set its
189     // max_duration correctly at the next call to share_resources
190     model()->getModifiedSet()->push_front(*action);
191   }
192
193   XBT_OUT();
194   return action;
195 }
196
197 /**********
198  * Action *
199  **********/
200 CpuCas01Action::CpuCas01Action(kernel::resource::Model* model, double cost, bool failed, double speed,
201                                kernel::lmm::Constraint* constraint, int requestedCore)
202     : CpuAction(model, cost, failed,
203                 model->getMaxminSystem()->variable_new(this, 1.0 / requestedCore, requestedCore * speed, 1))
204     , requestedCore_(requestedCore)
205 {
206   if (model->getUpdateMechanism() == UM_LAZY) {
207     refreshLastUpdate();
208     setLastValue(0.0);
209   }
210   model->getMaxminSystem()->expand(constraint, getVariable(), 1.0);
211 }
212
213 CpuCas01Action::CpuCas01Action(kernel::resource::Model* model, double cost, bool failed, double speed,
214                                kernel::lmm::Constraint* constraint)
215     : CpuCas01Action(model, cost, failed, speed, constraint, 1)
216 {
217 }
218
219 int CpuCas01Action::requestedCore()
220 {
221   return requestedCore_;
222 }
223
224 CpuCas01Action::~CpuCas01Action()=default;
225
226 }
227 }