Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move the host list into the Engine
[simgrid.git] / src / surf / cpu_cas01.cpp
1 /* Copyright (c) 2009-2011, 2013-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "cpu_cas01.hpp"
8 #include "cpu_ti.hpp"
9 #include "simgrid/sg_config.h"
10 #include "src/kernel/lmm/maxmin.hpp"
11 #include "xbt/utility.hpp"
12 #include <algorithm>
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_cas, surf_cpu, "Logging specific to the SURF CPU IMPROVED module");
15
16 /*********
17  * Model *
18  *********/
19 void surf_cpu_model_init_Cas01()
20 {
21   xbt_assert(not surf_cpu_model_pm);
22   xbt_assert(not surf_cpu_model_vm);
23
24   if (xbt_cfg_get_string("cpu/optim") == "TI") {
25     surf_cpu_model_init_ti();
26     return;
27   }
28
29   surf_cpu_model_pm = new simgrid::surf::CpuCas01Model();
30   all_existing_models->push_back(surf_cpu_model_pm);
31
32   surf_cpu_model_vm  = new simgrid::surf::CpuCas01Model();
33   all_existing_models->push_back(surf_cpu_model_vm);
34 }
35
36 namespace simgrid {
37 namespace surf {
38
39 CpuCas01Model::CpuCas01Model() : simgrid::surf::CpuModel()
40 {
41   std::string optim = xbt_cfg_get_string("cpu/optim");
42   bool select = xbt_cfg_get_boolean("cpu/maxmin-selective-update");
43
44   if (optim == "Full") {
45     setUpdateMechanism(UM_FULL);
46     selectiveUpdate_ = select;
47   } else if (optim == "Lazy") {
48     setUpdateMechanism(UM_LAZY);
49     selectiveUpdate_ = true;
50     xbt_assert(select || (xbt_cfg_is_default_value("cpu/maxmin-selective-update")),
51                "Disabling selective update while using the lazy update mechanism is dumb!");
52   } else {
53     xbt_die("Unsupported optimization (%s) for this model", optim.c_str());
54   }
55
56   maxminSystem_ = new simgrid::kernel::lmm::System(selectiveUpdate_);
57
58   if (getUpdateMechanism() == UM_LAZY) {
59     modifiedSet_ = new ActionLmmList();
60     maxminSystem_->keep_track = modifiedSet_;
61   }
62 }
63
64 CpuCas01Model::~CpuCas01Model()
65 {
66   surf_cpu_model_pm = nullptr;
67 }
68
69 Cpu *CpuCas01Model::createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
70 {
71   return new CpuCas01(this, host, speedPerPstate, core);
72 }
73
74 /************
75  * Resource *
76  ************/
77 CpuCas01::CpuCas01(CpuCas01Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
78     : Cpu(model, host, model->getMaxminSystem()->constraint_new(this, core * speedPerPstate->front()), speedPerPstate,
79           core)
80 {
81 }
82
83 CpuCas01::~CpuCas01()
84 {
85   if (model() == surf_cpu_model_pm)
86     speedPerPstate_.clear();
87 }
88
89 std::vector<double> * CpuCas01::getSpeedPeakList(){
90   return &speedPerPstate_;
91 }
92
93 bool CpuCas01::isUsed()
94 {
95   return model()->getMaxminSystem()->constraint_used(constraint());
96 }
97
98 /** @brief take into account changes of speed (either load or max) */
99 void CpuCas01::onSpeedChange() {
100   lmm_variable_t var       = nullptr;
101   const_lmm_element_t elem = nullptr;
102
103   model()->getMaxminSystem()->update_constraint_bound(constraint(), coresAmount_ * speed_.scale * speed_.peak);
104   while ((var = constraint()->get_variable(&elem))) {
105     CpuCas01Action* action = static_cast<CpuCas01Action*>(var->get_id());
106
107     model()->getMaxminSystem()->update_variable_bound(action->getVariable(),
108                                                       action->requestedCore() * speed_.scale * speed_.peak);
109   }
110
111   Cpu::onSpeedChange();
112 }
113
114 void CpuCas01::apply_event(tmgr_trace_event_t event, double value)
115 {
116   if (event == speed_.event) {
117     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
118     xbt_assert(coresAmount_ == 1, "FIXME: add speed scaling code also for constraint_core[i]");
119
120     speed_.scale = value;
121     onSpeedChange();
122
123     tmgr_trace_event_unref(&speed_.event);
124   } else if (event == stateEvent_) {
125     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
126     xbt_assert(coresAmount_ == 1, "FIXME: add state change code also for constraint_core[i]");
127
128     if (value > 0) {
129       if(isOff())
130         host_that_restart.push_back(getHost());
131       turnOn();
132     } else {
133       lmm_constraint_t cnst    = constraint();
134       lmm_variable_t var       = nullptr;
135       const_lmm_element_t elem = nullptr;
136       double date              = surf_get_clock();
137
138       turnOff();
139
140       while ((var = cnst->get_variable(&elem))) {
141         Action* action = static_cast<Action*>(var->get_id());
142
143         if (action->getState() == Action::State::running ||
144             action->getState() == Action::State::ready ||
145             action->getState() == Action::State::not_in_the_system) {
146           action->setFinishTime(date);
147           action->setState(Action::State::failed);
148         }
149       }
150     }
151     tmgr_trace_event_unref(&stateEvent_);
152
153   } else {
154     xbt_die("Unknown event!\n");
155   }
156 }
157
158 /** @brief Start a new execution on this CPU lasting @param size flops and using one core */
159 CpuAction *CpuCas01::execution_start(double size)
160 {
161   return new CpuCas01Action(model(), size, isOff(), speed_.scale * speed_.peak, constraint());
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_ = 2;
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(Model* model, double cost, bool failed, double speed, lmm_constraint_t constraint,
202                                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(Model* model, double cost, bool failed, double speed, lmm_constraint_t 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 }