1 /* Copyright (c) 2009-2011, 2013-2017. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "cpu_cas01.hpp"
9 #include "simgrid/sg_config.h"
10 #include "src/kernel/lmm/maxmin.hpp"
11 #include "xbt/utility.hpp"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_cas, surf_cpu, "Logging specific to the SURF CPU IMPROVED module");
19 void surf_cpu_model_init_Cas01()
21 xbt_assert(not surf_cpu_model_pm);
22 xbt_assert(not surf_cpu_model_vm);
24 if (xbt_cfg_get_string("cpu/optim") == "TI") {
25 surf_cpu_model_init_ti();
29 surf_cpu_model_pm = new simgrid::surf::CpuCas01Model();
30 all_existing_models->push_back(surf_cpu_model_pm);
32 surf_cpu_model_vm = new simgrid::surf::CpuCas01Model();
33 all_existing_models->push_back(surf_cpu_model_vm);
39 CpuCas01Model::CpuCas01Model() : simgrid::surf::CpuModel()
41 std::string optim = xbt_cfg_get_string("cpu/optim");
42 bool select = xbt_cfg_get_boolean("cpu/maxmin-selective-update");
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!");
53 xbt_die("Unsupported optimization (%s) for this model", optim.c_str());
56 p_cpuRunningActionSetThatDoesNotNeedBeingChecked = new ActionList();
57 maxminSystem_ = new simgrid::kernel::lmm::s_lmm_system_t(selectiveUpdate_);
59 if (getUpdateMechanism() == UM_LAZY) {
60 modifiedSet_ = new ActionLmmList();
61 maxminSystem_->keep_track = modifiedSet_;
65 CpuCas01Model::~CpuCas01Model()
68 maxminSystem_ = nullptr;
71 surf_cpu_model_pm = nullptr;
73 delete p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
76 Cpu *CpuCas01Model::createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
78 return new CpuCas01(this, host, speedPerPstate, core);
84 CpuCas01::CpuCas01(CpuCas01Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
85 : Cpu(model, host, model->getMaxminSystem()->constraint_new(this, core * speedPerPstate->front()), speedPerPstate,
92 if (model() == surf_cpu_model_pm)
93 speedPerPstate_.clear();
96 std::vector<double> * CpuCas01::getSpeedPeakList(){
97 return &speedPerPstate_;
100 bool CpuCas01::isUsed()
102 return model()->getMaxminSystem()->constraint_used(constraint());
105 /** @brief take into account changes of speed (either load or max) */
106 void CpuCas01::onSpeedChange() {
107 lmm_variable_t var = nullptr;
108 const_lmm_element_t elem = nullptr;
110 model()->getMaxminSystem()->update_constraint_bound(constraint(), coresAmount_ * speed_.scale * speed_.peak);
111 while ((var = constraint()->get_variable(&elem))) {
112 CpuCas01Action* action = static_cast<CpuCas01Action*>(var->get_id());
114 model()->getMaxminSystem()->update_variable_bound(action->getVariable(),
115 action->requestedCore() * speed_.scale * speed_.peak);
118 Cpu::onSpeedChange();
121 void CpuCas01::apply_event(tmgr_trace_event_t event, double value)
123 if (event == speed_.event) {
124 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
125 xbt_assert(coresAmount_ == 1, "FIXME: add speed scaling code also for constraint_core[i]");
127 speed_.scale = value;
130 tmgr_trace_event_unref(&speed_.event);
131 } else if (event == stateEvent_) {
132 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
133 xbt_assert(coresAmount_ == 1, "FIXME: add state change code also for constraint_core[i]");
137 host_that_restart.push_back(getHost());
140 lmm_constraint_t cnst = constraint();
141 lmm_variable_t var = nullptr;
142 const_lmm_element_t elem = nullptr;
143 double date = surf_get_clock();
147 while ((var = cnst->get_variable(&elem))) {
148 Action* action = static_cast<Action*>(var->get_id());
150 if (action->getState() == Action::State::running ||
151 action->getState() == Action::State::ready ||
152 action->getState() == Action::State::not_in_the_system) {
153 action->setFinishTime(date);
154 action->setState(Action::State::failed);
158 tmgr_trace_event_unref(&stateEvent_);
161 xbt_die("Unknown event!\n");
165 /** @brief Start a new execution on this CPU lasting @param size flops and using one core */
166 CpuAction *CpuCas01::execution_start(double size)
168 return new CpuCas01Action(model(), size, isOff(), speed_.scale * speed_.peak, constraint());
170 CpuAction* CpuCas01::execution_start(double size, int requestedCores)
172 return new CpuCas01Action(model(), size, isOff(), speed_.scale * speed_.peak, constraint(), requestedCores);
175 CpuAction *CpuCas01::sleep(double duration)
178 duration = std::max(duration, sg_surf_precision);
180 XBT_IN("(%s,%g)", getCname(), duration);
181 CpuCas01Action* action = new CpuCas01Action(model(), 1.0, isOff(), speed_.scale * speed_.peak, constraint());
183 // FIXME: sleep variables should not consume 1.0 in lmm_expand
184 action->setMaxDuration(duration);
185 action->suspended_ = 2;
186 if (duration < 0) { // NO_MAX_DURATION
187 /* Move to the *end* of the corresponding action set. This convention is used to speed up update_resource_state */
188 simgrid::xbt::intrusive_erase(*action->getStateSet(), *action);
189 action->stateSet_ = static_cast<CpuCas01Model*>(model())->p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
190 action->getStateSet()->push_back(*action);
193 model()->getMaxminSystem()->update_variable_weight(action->getVariable(), 0.0);
194 if (model()->getUpdateMechanism() == UM_LAZY) { // remove action from the heap
195 action->heapRemove(model()->getActionHeap());
196 // this is necessary for a variable with weight 0 since such variables are ignored in lmm and we need to set its
197 // max_duration correctly at the next call to share_resources
198 model()->getModifiedSet()->push_front(*action);
208 CpuCas01Action::CpuCas01Action(Model* model, double cost, bool failed, double speed, lmm_constraint_t constraint,
210 : CpuAction(model, cost, failed,
211 model->getMaxminSystem()->variable_new(this, 1.0 / requestedCore, requestedCore * speed, 1))
212 , requestedCore_(requestedCore)
214 if (model->getUpdateMechanism() == UM_LAZY) {
218 model->getMaxminSystem()->expand(constraint, getVariable(), 1.0);
221 CpuCas01Action::CpuCas01Action(Model* model, double cost, bool failed, double speed, lmm_constraint_t constraint)
222 : CpuCas01Action(model, cost, failed, speed, constraint, 1)
226 int CpuCas01Action::requestedCore()
228 return requestedCore_;
231 CpuCas01Action::~CpuCas01Action()=default;