Logo AND Algorithmique Numérique Distribuée

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