Logo AND Algorithmique Numérique Distribuée

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