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));
78 CpuCas01Model::~CpuCas01Model()
80 surf_cpu_model_pm = nullptr;
83 Cpu *CpuCas01Model::createCpu(simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
85 return new CpuCas01(this, host, speedPerPstate, core);
91 CpuCas01::CpuCas01(CpuCas01Model* model, simgrid::s4u::Host* host, std::vector<double>* speedPerPstate, int core)
92 : Cpu(model, host, model->get_maxmin_system()->constraint_new(this, core * speedPerPstate->front()), speedPerPstate,
99 if (get_model() == surf_cpu_model_pm)
100 speedPerPstate_.clear();
103 std::vector<double> * CpuCas01::getSpeedPeakList(){
104 return &speedPerPstate_;
107 bool CpuCas01::is_used()
109 return get_model()->get_maxmin_system()->constraint_used(get_constraint());
112 /** @brief take into account changes of speed (either load or max) */
113 void CpuCas01::onSpeedChange() {
114 kernel::lmm::Variable* var = nullptr;
115 const_lmm_element_t elem = nullptr;
117 get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(),
118 coresAmount_ * speed_.scale * speed_.peak);
119 while ((var = get_constraint()->get_variable(&elem))) {
120 CpuCas01Action* action = static_cast<CpuCas01Action*>(var->get_id());
122 get_model()->get_maxmin_system()->update_variable_bound(action->get_variable(),
123 action->requestedCore() * speed_.scale * speed_.peak);
126 Cpu::onSpeedChange();
129 void CpuCas01::apply_event(tmgr_trace_event_t event, double value)
131 if (event == speed_.event) {
132 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
133 xbt_assert(coresAmount_ == 1, "FIXME: add speed scaling code also for constraint_core[i]");
135 speed_.scale = value;
138 tmgr_trace_event_unref(&speed_.event);
139 } else if (event == stateEvent_) {
140 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
141 xbt_assert(coresAmount_ == 1, "FIXME: add state change code also for constraint_core[i]");
145 host_that_restart.push_back(getHost());
148 kernel::lmm::Constraint* cnst = get_constraint();
149 kernel::lmm::Variable* var = nullptr;
150 const_lmm_element_t elem = nullptr;
151 double date = surf_get_clock();
155 while ((var = cnst->get_variable(&elem))) {
156 kernel::resource::Action* action = static_cast<kernel::resource::Action*>(var->get_id());
158 if (action->get_state() == kernel::resource::Action::State::running ||
159 action->get_state() == kernel::resource::Action::State::ready ||
160 action->get_state() == kernel::resource::Action::State::not_in_the_system) {
161 action->set_finish_time(date);
162 action->set_state(kernel::resource::Action::State::failed);
166 tmgr_trace_event_unref(&stateEvent_);
169 xbt_die("Unknown event!\n");
173 /** @brief Start a new execution on this CPU lasting @param size flops and using one core */
174 CpuAction* CpuCas01::execution_start(double size)
176 return new CpuCas01Action(get_model(), size, is_off(), speed_.scale * speed_.peak, get_constraint());
179 CpuAction* CpuCas01::execution_start(double size, int requestedCores)
181 return new CpuCas01Action(get_model(), size, is_off(), speed_.scale * speed_.peak, get_constraint(), requestedCores);
184 CpuAction *CpuCas01::sleep(double duration)
187 duration = std::max(duration, sg_surf_precision);
189 XBT_IN("(%s,%g)", get_cname(), duration);
190 CpuCas01Action* action = new CpuCas01Action(get_model(), 1.0, is_off(), speed_.scale * speed_.peak, get_constraint());
192 // FIXME: sleep variables should not consume 1.0 in System::expand()
193 action->set_max_duration(duration);
194 action->suspended_ = kernel::resource::Action::SuspendStates::sleeping;
195 if (duration < 0) { // NO_MAX_DURATION
196 /* Move to the *end* of the corresponding action set. This convention is used to speed up update_resource_state */
197 simgrid::xbt::intrusive_erase(*action->get_state_set(), *action);
198 action->state_set_ = &static_cast<CpuCas01Model*>(get_model())->cpuRunningActionSetThatDoesNotNeedBeingChecked_;
199 action->get_state_set()->push_back(*action);
202 get_model()->get_maxmin_system()->update_variable_weight(action->get_variable(), 0.0);
203 if (get_model()->get_update_algorithm() == kernel::resource::Model::UpdateAlgo::Lazy) { // remove action from the heap
204 get_model()->get_action_heap().remove(action);
205 // this is necessary for a variable with weight 0 since such variables are ignored in lmm and we need to set its
206 // max_duration correctly at the next call to share_resources
207 get_model()->get_modified_set()->push_front(*action);
217 CpuCas01Action::CpuCas01Action(kernel::resource::Model* model, double cost, bool failed, double speed,
218 kernel::lmm::Constraint* constraint, int requestedCore)
219 : CpuAction(model, cost, failed,
220 model->get_maxmin_system()->variable_new(this, 1.0 / requestedCore, requestedCore * speed, 1))
221 , requestedCore_(requestedCore)
223 if (model->get_update_algorithm() == kernel::resource::Model::UpdateAlgo::Lazy) {
227 model->get_maxmin_system()->expand(constraint, get_variable(), 1.0);
230 CpuCas01Action::CpuCas01Action(kernel::resource::Model* model, double cost, bool failed, double speed,
231 kernel::lmm::Constraint* constraint)
232 : CpuCas01Action(model, cost, failed, speed, constraint, 1)
236 int CpuCas01Action::requestedCore()
238 return requestedCore_;
241 CpuCas01Action::~CpuCas01Action()=default;