Logo AND Algorithmique Numérique Distribuée

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