Logo AND Algorithmique Numérique Distribuée

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