1 /* Copyright (c) 2009-2018. The SimGrid Team. All rights reserved. */
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. */
6 #include "cpu_cas01.hpp"
8 #include "simgrid/sg_config.hpp"
9 #include "src/kernel/lmm/maxmin.hpp"
10 #include "xbt/config.hpp"
11 #include "xbt/utility.hpp"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_cas, surf_cpu, "Logging specific to the SURF CPU module");
21 static simgrid::config::Flag<std::string>
22 cpu_optim_opt("cpu/optim", "Optimization algorithm to use for CPU resources. ", "Lazy",
24 std::map<std::string, std::string>({
25 {"Lazy", "Lazy action management (partial invalidation in lmm + heap in action remaining)."},
26 {"TI", "Trace integration. Highly optimized mode when using availability traces (only available "
27 "for the Cas01 CPU model for now)."},
28 {"Full", "Full update of remaining and variables. Slow but may be useful when debugging."},
31 [](std::string const& val) {
32 xbt_assert(_sg_cfg_init_status < 2,
33 "Cannot change the optimization algorithm after the initialization");
39 void surf_cpu_model_init_Cas01()
41 xbt_assert(not surf_cpu_model_pm);
42 xbt_assert(not surf_cpu_model_vm);
44 if (cpu_optim_opt == "TI") {
45 surf_cpu_model_init_ti();
49 simgrid::kernel::resource::Model::UpdateAlgo algo;
50 if (cpu_optim_opt == "Lazy")
51 algo = simgrid::kernel::resource::Model::UpdateAlgo::Lazy;
53 algo = simgrid::kernel::resource::Model::UpdateAlgo::Full;
55 surf_cpu_model_pm = new simgrid::surf::CpuCas01Model(algo);
56 all_existing_models->push_back(surf_cpu_model_pm);
58 surf_cpu_model_vm = new simgrid::surf::CpuCas01Model(algo);
59 all_existing_models->push_back(surf_cpu_model_vm);
65 CpuCas01Model::CpuCas01Model(kernel::resource::Model::UpdateAlgo algo) : simgrid::surf::CpuModel(algo)
67 bool select = xbt_cfg_get_boolean("cpu/maxmin-selective-update");
69 if (algo == Model::UpdateAlgo::Lazy) {
70 xbt_assert(select || xbt_cfg_is_default_value("cpu/maxmin-selective-update"),
71 "You cannot disable cpu selective update when using the lazy update mechanism");
75 set_maxmin_system(new simgrid::kernel::lmm::System(select));
77 if (algo == Model::UpdateAlgo::Lazy)
78 get_maxmin_system()->modified_set_ = new kernel::resource::Action::ModifiedSet();
81 CpuCas01Model::~CpuCas01Model()
83 surf_cpu_model_pm = nullptr;
86 Cpu *CpuCas01Model::createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
88 return new CpuCas01(this, host, speedPerPstate, core);
94 CpuCas01::CpuCas01(CpuCas01Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
95 : Cpu(model, host, model->get_maxmin_system()->constraint_new(this, core * speedPerPstate->front()), speedPerPstate,
100 CpuCas01::~CpuCas01()
102 if (model() == surf_cpu_model_pm)
103 speedPerPstate_.clear();
106 std::vector<double> * CpuCas01::getSpeedPeakList(){
107 return &speedPerPstate_;
110 bool CpuCas01::isUsed()
112 return model()->get_maxmin_system()->constraint_used(constraint());
115 /** @brief take into account changes of speed (either load or max) */
116 void CpuCas01::onSpeedChange() {
117 kernel::lmm::Variable* var = nullptr;
118 const_lmm_element_t elem = nullptr;
120 model()->get_maxmin_system()->update_constraint_bound(constraint(), coresAmount_ * speed_.scale * speed_.peak);
121 while ((var = constraint()->get_variable(&elem))) {
122 CpuCas01Action* action = static_cast<CpuCas01Action*>(var->get_id());
124 model()->get_maxmin_system()->update_variable_bound(action->get_variable(),
125 action->requestedCore() * speed_.scale * speed_.peak);
128 Cpu::onSpeedChange();
131 void CpuCas01::apply_event(tmgr_trace_event_t event, double value)
133 if (event == speed_.event) {
134 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
135 xbt_assert(coresAmount_ == 1, "FIXME: add speed scaling code also for constraint_core[i]");
137 speed_.scale = value;
140 tmgr_trace_event_unref(&speed_.event);
141 } else if (event == stateEvent_) {
142 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
143 xbt_assert(coresAmount_ == 1, "FIXME: add state change code also for constraint_core[i]");
147 host_that_restart.push_back(getHost());
150 kernel::lmm::Constraint* cnst = constraint();
151 kernel::lmm::Variable* var = nullptr;
152 const_lmm_element_t elem = nullptr;
153 double date = surf_get_clock();
157 while ((var = cnst->get_variable(&elem))) {
158 kernel::resource::Action* action = static_cast<kernel::resource::Action*>(var->get_id());
160 if (action->get_state() == kernel::resource::Action::State::running ||
161 action->get_state() == kernel::resource::Action::State::ready ||
162 action->get_state() == kernel::resource::Action::State::not_in_the_system) {
163 action->set_finish_time(date);
164 action->set_state(kernel::resource::Action::State::failed);
168 tmgr_trace_event_unref(&stateEvent_);
171 xbt_die("Unknown event!\n");
175 /** @brief Start a new execution on this CPU lasting @param size flops and using one core */
176 CpuAction* CpuCas01::execution_start(double size)
178 return new CpuCas01Action(model(), size, isOff(), speed_.scale * speed_.peak, constraint());
181 CpuAction* CpuCas01::execution_start(double size, int requestedCores)
183 return new CpuCas01Action(model(), size, isOff(), speed_.scale * speed_.peak, constraint(), requestedCores);
186 CpuAction *CpuCas01::sleep(double duration)
189 duration = std::max(duration, sg_surf_precision);
191 XBT_IN("(%s,%g)", getCname(), duration);
192 CpuCas01Action* action = new CpuCas01Action(model(), 1.0, isOff(), speed_.scale * speed_.peak, constraint());
194 // FIXME: sleep variables should not consume 1.0 in System::expand()
195 action->set_max_duration(duration);
196 action->suspended_ = kernel::resource::Action::SuspendStates::sleeping;
197 if (duration < 0) { // NO_MAX_DURATION
198 /* Move to the *end* of the corresponding action set. This convention is used to speed up update_resource_state */
199 simgrid::xbt::intrusive_erase(*action->get_state_set(), *action);
200 action->state_set_ = &static_cast<CpuCas01Model*>(model())->cpuRunningActionSetThatDoesNotNeedBeingChecked_;
201 action->get_state_set()->push_back(*action);
204 model()->get_maxmin_system()->update_variable_weight(action->get_variable(), 0.0);
205 if (model()->getUpdateMechanism() == kernel::resource::Model::UpdateAlgo::Lazy) { // remove action from the heap
206 action->heapRemove();
207 // this is necessary for a variable with weight 0 since such variables are ignored in lmm and we need to set its
208 // max_duration correctly at the next call to share_resources
209 model()->get_modified_set()->push_front(*action);
219 CpuCas01Action::CpuCas01Action(kernel::resource::Model* model, double cost, bool failed, double speed,
220 kernel::lmm::Constraint* constraint, int requestedCore)
221 : CpuAction(model, cost, failed,
222 model->get_maxmin_system()->variable_new(this, 1.0 / requestedCore, requestedCore * speed, 1))
223 , requestedCore_(requestedCore)
225 if (model->getUpdateMechanism() == kernel::resource::Model::UpdateAlgo::Lazy) {
229 model->get_maxmin_system()->expand(constraint, get_variable(), 1.0);
232 CpuCas01Action::CpuCas01Action(kernel::resource::Model* model, double cost, bool failed, double speed,
233 kernel::lmm::Constraint* constraint)
234 : CpuCas01Action(model, cost, failed, speed, constraint, 1)
238 int CpuCas01Action::requestedCore()
240 return requestedCore_;
243 CpuCas01Action::~CpuCas01Action()=default;