Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Fix MC with the class-hierarchification of simgrid::simix::Synchro
[simgrid.git] / src / surf / cpu_cas01.cpp
1 /* Copyright (c) 2009-2011, 2013-2016. 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(!surf_cpu_model_pm);
20   xbt_assert(!surf_cpu_model_vm);
21
22   char *optim = xbt_cfg_get_string("cpu/optim");
23   if (!strcmp(optim, "TI")) {
24     surf_cpu_model_init_ti();
25     return;
26   }
27
28   surf_cpu_model_pm = new simgrid::surf::CpuCas01Model();
29   xbt_dynar_push(all_existing_models, &surf_cpu_model_pm);
30
31   surf_cpu_model_vm  = new simgrid::surf::CpuCas01Model();
32   xbt_dynar_push(all_existing_models, &surf_cpu_model_vm);
33 }
34
35 namespace simgrid {
36 namespace surf {
37
38 CpuCas01Model::CpuCas01Model() : simgrid::surf::CpuModel()
39 {
40   char *optim = xbt_cfg_get_string("cpu/optim");
41   int select = xbt_cfg_get_boolean("cpu/maxmin-selective-update");
42
43   if (!strcmp(optim, "Full")) {
44     updateMechanism_ = UM_FULL;
45     selectiveUpdate_ = select;
46   } else if (!strcmp(optim, "Lazy")) {
47     updateMechanism_ = UM_LAZY;
48     selectiveUpdate_ = 1;
49     xbt_assert((select == 1)
50                || (xbt_cfg_is_default_value("cpu/maxmin-selective-update")),
51                "Disabling selective update while using the lazy update mechanism is dumb!");
52   } else {
53     xbt_die("Unsupported optimization (%s) for this model", optim);
54   }
55
56   p_cpuRunningActionSetThatDoesNotNeedBeingChecked = new ActionList();
57
58   maxminSystem_ = lmm_system_new(selectiveUpdate_);
59
60   if (getUpdateMechanism() == UM_LAZY) {
61     actionHeap_ = xbt_heap_new(8, NULL);
62     xbt_heap_set_update_callback(actionHeap_,  surf_action_lmm_update_index_heap);
63     modifiedSet_ = new ActionLmmList();
64     maxminSystem_->keep_track = modifiedSet_;
65   }
66 }
67
68 CpuCas01Model::~CpuCas01Model()
69 {
70   lmm_system_free(maxminSystem_);
71   maxminSystem_ = NULL;
72   xbt_heap_free(actionHeap_);
73   delete modifiedSet_;
74
75   surf_cpu_model_pm = NULL;
76
77   delete p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
78 }
79
80 Cpu *CpuCas01Model::createCpu(simgrid::s4u::Host *host, xbt_dynar_t speedPerPstate, int core)
81 {
82   return new CpuCas01(this, host, speedPerPstate, core);
83 }
84
85 double CpuCas01Model::next_occuring_event_full(double /*now*/)
86 {
87   return Model::shareResourcesMaxMin(getRunningActionSet(), maxminSystem_, lmm_solve);
88 }
89
90 /************
91  * Resource *
92  ************/
93 CpuCas01::CpuCas01(CpuCas01Model *model, simgrid::s4u::Host *host, xbt_dynar_t speedPerPstate, int core)
94 : Cpu(model, host,
95     lmm_constraint_new(model->getMaxminSystem(), this, core * xbt_dynar_get_as(speedPerPstate, 0/*pstate*/, double)),
96     speedPerPstate, core)
97 {
98 }
99
100 CpuCas01::~CpuCas01()
101 {
102   if (getModel() == surf_cpu_model_pm)
103     xbt_dynar_free(&speedPerPstate_);
104 }
105
106 xbt_dynar_t CpuCas01::getSpeedPeakList(){
107   return speedPerPstate_;
108 }
109
110 bool CpuCas01::isUsed()
111 {
112   return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
113 }
114
115 /** @brief take into account changes of speed (either load or max) */
116 void CpuCas01::onSpeedChange() {
117   lmm_variable_t var = NULL;
118   lmm_element_t elem = NULL;
119
120     lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(),
121                                 coresAmount_ * speed_.scale * speed_.peak);
122     while ((var = lmm_get_var_from_cnst
123             (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
124       CpuCas01Action *action = static_cast<CpuCas01Action*>(lmm_variable_id(var));
125
126       lmm_update_variable_bound(getModel()->getMaxminSystem(),
127                                 action->getVariable(),
128                                 speed_.scale * speed_.peak);
129     }
130
131   Cpu::onSpeedChange();
132 }
133
134 void CpuCas01::apply_event(tmgr_trace_iterator_t event, double value)
135 {
136   if (event == speed_.event) {
137     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
138     xbt_assert(coresAmount_ == 1, "FIXME: add speed scaling code also for constraint_core[i]");
139
140     speed_.scale = value;
141     onSpeedChange();
142
143     tmgr_trace_event_unref(&speed_.event);
144   } else if (event == stateEvent_) {
145     /* TODO (Hypervisor): do the same thing for constraint_core[i] */
146     xbt_assert(coresAmount_ == 1, "FIXME: add state change code also for constraint_core[i]");
147
148     if (value > 0) {
149       if(isOff())
150         xbt_dynar_push_as(host_that_restart, char*, (char *)getName());
151       turnOn();
152     } else {
153       lmm_constraint_t cnst = getConstraint();
154       lmm_variable_t var = NULL;
155       lmm_element_t elem = NULL;
156       double date = surf_get_clock();
157
158       turnOff();
159
160       while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), cnst, &elem))) {
161         Action *action = static_cast<Action*>(lmm_variable_id(var));
162
163         if (action->getState() == Action::State::running ||
164             action->getState() == Action::State::ready ||
165             action->getState() == Action::State::not_in_the_system) {
166           action->setFinishTime(date);
167           action->setState(Action::State::failed);
168         }
169       }
170     }
171     tmgr_trace_event_unref(&stateEvent_);
172
173   } else {
174     xbt_die("Unknown event!\n");
175   }
176 }
177
178 CpuAction *CpuCas01::execution_start(double size)
179 {
180
181   XBT_IN("(%s,%g)", getName(), size);
182   CpuCas01Action *action = new CpuCas01Action(getModel(), size, isOff(),
183       speed_.scale * speed_.peak, getConstraint());
184
185   XBT_OUT();
186   return action;
187 }
188
189 CpuAction *CpuCas01::sleep(double duration)
190 {
191   if (duration > 0)
192     duration = MAX(duration, sg_surf_precision);
193
194   XBT_IN("(%s,%g)", getName(), duration);
195   CpuCas01Action *action = new CpuCas01Action(getModel(), 1.0, isOff(),
196       speed_.scale * speed_.peak, getConstraint());
197
198   // FIXME: sleep variables should not consume 1.0 in lmm_expand
199   action->maxDuration_ = duration;
200   action->suspended_ = 2;
201   if (duration == NO_MAX_DURATION) {
202     /* Move to the *end* of the corresponding action set. This convention
203        is used to speed up update_resource_state  */
204     action->getStateSet()->erase(action->getStateSet()->iterator_to(*action));
205     action->stateSet_ = static_cast<CpuCas01Model*>(getModel())->p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
206     action->getStateSet()->push_back(*action);
207   }
208
209   lmm_update_variable_weight(getModel()->getMaxminSystem(),
210       action->getVariable(), 0.0);
211   if (getModel()->getUpdateMechanism() == UM_LAZY) {     // remove action from the heap
212     action->heapRemove(getModel()->getActionHeap());
213     // this is necessary for a variable with weight 0 since such
214     // variables are ignored in lmm and we need to set its max_duration
215     // correctly at the next call to share_resources
216     getModel()->getModifiedSet()->push_front(*action);
217   }
218
219   XBT_OUT();
220   return action;
221 }
222
223 /**********
224  * Action *
225  **********/
226
227 CpuCas01Action::CpuCas01Action(Model *model, double cost, bool failed, double speed, lmm_constraint_t constraint)
228  : CpuAction(model, cost, failed,
229          lmm_variable_new(model->getMaxminSystem(), this,
230          1.0, speed, 1))
231 {
232   if (model->getUpdateMechanism() == UM_LAZY) {
233     indexHeap_ = -1;
234     lastUpdate_ = surf_get_clock();
235     lastValue_ = 0.0;
236   }
237   lmm_expand(model->getMaxminSystem(), constraint, getVariable(), 1.0);
238 }
239
240 }
241 }