Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Useless include.
[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 "maxmin_private.hpp"
10 #include "simgrid/sg_config.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_cas, surf_cpu, "Logging specific to the SURF CPU IMPROVED module");
13
14 /*********
15  * Model *
16  *********/
17 void surf_cpu_model_init_Cas01()
18 {
19   xbt_assert(not surf_cpu_model_pm);
20   xbt_assert(not surf_cpu_model_vm);
21
22   if (xbt_cfg_get_string("cpu/optim") == "TI") {
23     surf_cpu_model_init_ti();
24     return;
25   }
26
27   surf_cpu_model_pm = new simgrid::surf::CpuCas01Model();
28   all_existing_models->push_back(surf_cpu_model_pm);
29
30   surf_cpu_model_vm  = new simgrid::surf::CpuCas01Model();
31   all_existing_models->push_back(surf_cpu_model_vm);
32 }
33
34 namespace simgrid {
35 namespace surf {
36
37 CpuCas01Model::CpuCas01Model() : simgrid::surf::CpuModel()
38 {
39   std::string optim = xbt_cfg_get_string("cpu/optim");
40   bool select = xbt_cfg_get_boolean("cpu/maxmin-selective-update");
41
42   if (optim == "Full") {
43     setUpdateMechanism(UM_FULL);
44     selectiveUpdate_ = select;
45   } else if (optim == "Lazy") {
46     setUpdateMechanism(UM_LAZY);
47     selectiveUpdate_ = 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   p_cpuRunningActionSetThatDoesNotNeedBeingChecked = new ActionList();
55   maxminSystem_ = lmm_system_new(selectiveUpdate_);
56
57   if (getUpdateMechanism() == UM_LAZY) {
58     modifiedSet_ = new ActionLmmList();
59     maxminSystem_->keep_track = modifiedSet_;
60   }
61 }
62
63 CpuCas01Model::~CpuCas01Model()
64 {
65   lmm_system_free(maxminSystem_);
66   maxminSystem_ = nullptr;
67   delete modifiedSet_;
68
69   surf_cpu_model_pm = nullptr;
70
71   delete p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
72 }
73
74 Cpu *CpuCas01Model::createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
75 {
76   return new CpuCas01(this, host, speedPerPstate, core);
77 }
78
79 /************
80  * Resource *
81  ************/
82 CpuCas01::CpuCas01(CpuCas01Model *model, simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
83 : Cpu(model, host, lmm_constraint_new(model->getMaxminSystem(), this, core * speedPerPstate->front()),
84     speedPerPstate, core)
85 {
86 }
87
88 CpuCas01::~CpuCas01()
89 {
90   if (model() == surf_cpu_model_pm)
91     speedPerPstate_.clear();
92 }
93
94 std::vector<double> * CpuCas01::getSpeedPeakList(){
95   return &speedPerPstate_;
96 }
97
98 bool CpuCas01::isUsed()
99 {
100   return lmm_constraint_used(model()->getMaxminSystem(), constraint());
101 }
102
103 /** @brief take into account changes of speed (either load or max) */
104 void CpuCas01::onSpeedChange() {
105   lmm_variable_t var = nullptr;
106   lmm_element_t elem = nullptr;
107
108   lmm_update_constraint_bound(model()->getMaxminSystem(), constraint(), coresAmount_ * speed_.scale * speed_.peak);
109   while ((var = lmm_get_var_from_cnst(model()->getMaxminSystem(), constraint(), &elem))) {
110     CpuCas01Action* action = static_cast<CpuCas01Action*>(lmm_variable_id(var));
111
112     lmm_update_variable_bound(model()->getMaxminSystem(), action->getVariable(),
113                               action->requestedCore() * speed_.scale * speed_.peak);
114   }
115
116   Cpu::onSpeedChange();
117 }
118
119 void CpuCas01::apply_event(tmgr_trace_event_t event, double value)
120 {
121   if (event == speed_.event) {
122     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
123     xbt_assert(coresAmount_ == 1, "FIXME: add speed scaling code also for constraint_core[i]");
124
125     speed_.scale = value;
126     onSpeedChange();
127
128     tmgr_trace_event_unref(&speed_.event);
129   } else if (event == stateEvent_) {
130     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
131     xbt_assert(coresAmount_ == 1, "FIXME: add state change code also for constraint_core[i]");
132
133     if (value > 0) {
134       if(isOff())
135         host_that_restart.push_back(getHost());
136       turnOn();
137     } else {
138       lmm_constraint_t cnst = constraint();
139       lmm_variable_t var = nullptr;
140       lmm_element_t elem = nullptr;
141       double date = surf_get_clock();
142
143       turnOff();
144
145       while ((var = lmm_get_var_from_cnst(model()->getMaxminSystem(), cnst, &elem))) {
146         Action *action = static_cast<Action*>(lmm_variable_id(var));
147
148         if (action->getState() == Action::State::running ||
149             action->getState() == Action::State::ready ||
150             action->getState() == Action::State::not_in_the_system) {
151           action->setFinishTime(date);
152           action->setState(Action::State::failed);
153         }
154       }
155     }
156     tmgr_trace_event_unref(&stateEvent_);
157
158   } else {
159     xbt_die("Unknown event!\n");
160   }
161 }
162
163 /** @brief Start a new execution on this CPU lasting @param size flops and using one core */
164 CpuAction *CpuCas01::execution_start(double size)
165 {
166   return new CpuCas01Action(model(), size, isOff(), speed_.scale * speed_.peak, constraint());
167 }
168 CpuAction* CpuCas01::execution_start(double size, int requestedCores)
169 {
170   return new CpuCas01Action(model(), size, isOff(), speed_.scale * speed_.peak, constraint(), requestedCores);
171 }
172
173 CpuAction *CpuCas01::sleep(double duration)
174 {
175   if (duration > 0)
176     duration = MAX(duration, sg_surf_precision);
177
178   XBT_IN("(%s,%g)", getCname(), duration);
179   CpuCas01Action* action = new CpuCas01Action(model(), 1.0, isOff(), speed_.scale * speed_.peak, constraint());
180
181   // FIXME: sleep variables should not consume 1.0 in lmm_expand
182   action->setMaxDuration(duration);
183   action->suspended_ = 2;
184   if (duration < 0) { // NO_MAX_DURATION
185     /* Move to the *end* of the corresponding action set. This convention is used to speed up update_resource_state */
186     action->getStateSet()->erase(action->getStateSet()->iterator_to(*action));
187     action->stateSet_ = static_cast<CpuCas01Model*>(model())->p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
188     action->getStateSet()->push_back(*action);
189   }
190
191   lmm_update_variable_weight(model()->getMaxminSystem(), action->getVariable(), 0.0);
192   if (model()->getUpdateMechanism() == UM_LAZY) { // remove action from the heap
193     action->heapRemove(model()->getActionHeap());
194     // this is necessary for a variable with weight 0 since such variables are ignored in lmm and we need to set its
195     // max_duration correctly at the next call to share_resources
196     model()->getModifiedSet()->push_front(*action);
197   }
198
199   XBT_OUT();
200   return action;
201 }
202
203 /**********
204  * Action *
205  **********/
206 CpuCas01Action::CpuCas01Action(Model* model, double cost, bool failed, double speed, lmm_constraint_t constraint,
207                                int requestedCore)
208     : CpuAction(model, cost, failed,
209                 lmm_variable_new(model->getMaxminSystem(), this, 1.0 / requestedCore, requestedCore * speed, 1))
210     , requestedCore_(requestedCore)
211 {
212   if (model->getUpdateMechanism() == UM_LAZY) {
213     updateIndexHeap(-1);
214     refreshLastUpdate();
215     setLastValue(0.0);
216   }
217   lmm_expand(model->getMaxminSystem(), 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 }