Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
CpuCas01 in C++
[simgrid.git] / src / surf / cpu_cas01.cpp
1 /* Copyright (c) 2009-2011. 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 "surf.hpp"
10 #include "maxmin_private.h"
11 #include "simgrid/sg_config.h"
12
13 //TODO: resolve dependencies
14 static void TRACE_surf_host_set_power(double date, const char *resource, double power) {}
15
16
17 static ModelPtr surf_cpu_model = NULL;
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surfpp_cpu_cas, surfpp,
20                                 "Logging specific to the SURF CPU IMPROVED module");
21
22 static xbt_swag_t
23     cpu_running_action_set_that_does_not_need_being_checked = NULL;
24
25 /*********
26  * Model *
27  *********/
28 void surf_cpu_model_init_Cas01()
29 {
30   char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
31
32   if (surf_cpu_model)
33     return;
34
35   if (!strcmp(optim, "TI")) {
36     surf_cpu_model = new CpuTiModel();
37     return;
38   }
39
40   surf_cpu_model = new CpuCas01Model();
41   xbt_dynar_push(model_list, &surf_cpu_model);
42 }
43
44 CpuCas01Model::CpuCas01Model() : CpuModel("cpu")
45 {
46   CpuCas01ActionLmm action;
47
48   char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
49   int select = xbt_cfg_get_boolean(_sg_cfg_set, "cpu/maxmin_selective_update");
50
51   if (!strcmp(optim, "Full")) {
52     p_updateMechanism = UM_FULL;
53     m_selectiveUpdate = select;
54   } else if (!strcmp(optim, "Lazy")) {
55     p_updateMechanism = UM_LAZY;
56     m_selectiveUpdate = 1;
57     xbt_assert((select == 1)
58                ||
59                (xbt_cfg_is_default_value
60                 (_sg_cfg_set, "cpu/maxmin_selective_update")),
61                "Disabling selective update while using the lazy update mechanism is dumb!");
62   } else {
63     xbt_die("Unsupported optimization (%s) for this model", optim);
64   }
65
66   cpu_running_action_set_that_does_not_need_being_checked =
67       xbt_swag_new(xbt_swag_offset(action, p_stateHookup));
68
69   if (p_updateMechanism == UM_LAZY) {
70         shareResources = &CpuCas01Model::shareResourcesLazy;
71         updateActionsState = &CpuCas01Model::updateActionsStateLazy;
72
73   } else if (p_updateMechanism == UM_FULL) {
74         shareResources = &CpuCas01Model::shareResourcesFull;
75         updateActionsState = &CpuCas01Model::updateActionsStateFull;
76   } else
77     xbt_die("Invalid cpu update mechanism!");
78
79   if (!p_maxminSystem) {
80     p_maxminSystem = lmm_system_new(m_selectiveUpdate);
81   }
82
83   if (p_updateMechanism == UM_LAZY) {
84     p_actionHeap = xbt_heap_new(8, NULL);
85     xbt_heap_set_update_callback(p_actionHeap,  surf_action_lmm_update_index_heap);
86     p_modifiedSet = xbt_swag_new(xbt_swag_offset(action, p_actionListHookup));
87     //TOREPAIR: cpu_model->model_private->maxmin_system->m_keepTrack = cpu_model->model_private->modified_set;
88   }
89   /* Define callbacks */
90   //TODO sg_platf_host_add_cb(parse_cpu_init);
91   //TODO sg_platf_postparse_add_cb(cpu_add_traces_cpu);
92 }
93
94 CpuCas01Model::~CpuCas01Model()
95 {
96   lmm_system_free(p_maxminSystem);
97   p_maxminSystem = NULL;
98
99   if (p_actionHeap)
100     xbt_heap_free(p_actionHeap);
101   xbt_swag_free(p_modifiedSet);
102
103   surf_cpu_model = NULL;
104
105   xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
106   cpu_running_action_set_that_does_not_need_being_checked = NULL;
107 }
108
109 void CpuCas01Model::parseInit(sg_platf_host_cbarg_t host)
110 {
111   createResource(host->id,
112         host->power_peak,
113         host->power_scale,
114         host->power_trace,
115         host->core_amount,
116         host->initial_state,
117         host->state_trace,
118         host->properties);
119 }
120 CpuCas01LmmPtr CpuCas01Model::createResource(const char *name, double power_peak, double power_scale,
121                           tmgr_trace_t power_trace, int core,
122                           e_surf_resource_state_t state_initial,
123                           tmgr_trace_t state_trace,
124                           xbt_dict_t cpu_properties)
125 {
126   CpuCas01LmmPtr cpu = NULL;
127
128   xbt_assert(!surf_cpu_resource_priv(surf_cpu_resource_by_name(name)),
129              "Host '%s' declared several times in the platform file",
130              name);
131   xbt_assert(power_peak > 0, "Power has to be >0");
132   xbt_assert(core > 0, "Invalid number of cores %d", core);
133
134   cpu = new CpuCas01Lmm(this, name, power_peak, power_scale, power_trace, core, state_initial, state_trace, cpu_properties);
135
136   xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, this);
137
138   return (CpuCas01LmmPtr) xbt_lib_get_elm_or_null(host_lib, name);;
139 }
140
141 double CpuCas01Model::shareResourcesFull(double now)
142 {
143   CpuCas01ActionLmm action;
144   Model::shareResourcesFull(p_runningActionSet, 
145                              xbt_swag_offset(action, p_variable),
146                              p_maxminSystem, lmm_solve);
147   return 0;
148 }
149
150 void CpuCas01Model::addTraces()
151 {
152   xbt_dict_cursor_t cursor = NULL;
153   char *trace_name, *elm;
154   static int called = 0;
155   if (called)
156     return;
157   called = 1;
158
159   /* connect all traces relative to hosts */
160   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
161     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
162     CpuCas01LmmPtr host = (CpuCas01LmmPtr) surf_cpu_resource_by_name(elm);
163
164     xbt_assert(host, "Host %s undefined", elm);
165     xbt_assert(trace, "Trace %s undefined", trace_name);
166
167     host->p_stateEvent =
168         tmgr_history_add_trace(history, trace, 0.0, 0, host);
169   }
170
171   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
172     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
173     CpuCas01LmmPtr host = (CpuCas01LmmPtr) surf_cpu_resource_by_name(elm);
174
175     xbt_assert(host, "Host %s undefined", elm);
176     xbt_assert(trace, "Trace %s undefined", trace_name);
177
178     host->p_powerEvent =
179         tmgr_history_add_trace(history, trace, 0.0, 0, host);
180   }
181 }
182
183 /************
184  * Resource *
185  ************/
186 CpuCas01Lmm::CpuCas01Lmm(CpuCas01ModelPtr model, const char *name, double powerPeak,
187         double powerScale, tmgr_trace_t powerTrace, int core,
188         e_surf_resource_state_t stateInitial, tmgr_trace_t stateTrace,
189         xbt_dict_t properties) :  
190         CpuLmm(model, name, properties) {
191   m_powerPeak = powerPeak;
192   m_powerScale = powerScale;
193   m_core = core;
194   p_stateCurrent = stateInitial;
195   if (powerTrace)
196     p_powerEvent = tmgr_history_add_trace(history, powerTrace, 0.0, 0, this);
197
198   if (stateTrace)
199     p_stateEvent = tmgr_history_add_trace(history, stateTrace, 0.0, 0, this);
200
201   p_constraint = lmm_constraint_new(p_model->p_maxminSystem, this, m_core * m_powerScale * m_powerPeak);
202 }
203
204 bool CpuCas01Lmm::isUsed()
205 {
206   return lmm_constraint_used(p_model->p_maxminSystem, p_constraint);
207 }
208
209 void CpuCas01Lmm::updateState(tmgr_trace_event_t event_type, double value, double date)
210 {
211   lmm_variable_t var = NULL;
212   lmm_element_t elem = NULL;
213
214   surf_watched_hosts();
215
216   if (event_type == p_powerEvent) {
217     m_powerScale = value;
218     lmm_update_constraint_bound(surf_cpu_model->p_maxminSystem, p_constraint,
219                                 m_core * m_powerScale *
220                                 m_powerPeak);
221 #ifdef HAVE_TRACING
222     TRACE_surf_host_set_power(date, m_name,
223                               m_core * m_powerScale *
224                               m_powerPeak);
225 #endif
226     while ((var = lmm_get_var_from_cnst
227             (surf_cpu_model->p_maxminSystem, p_constraint, &elem))) {
228       CpuCas01ActionLmmPtr action = (CpuCas01ActionLmmPtr) lmm_variable_id(var);
229       lmm_update_variable_bound(surf_cpu_model->p_maxminSystem,
230                                 action->p_variable,
231                                 m_powerScale * m_powerPeak);
232     }
233     if (tmgr_trace_event_free(event_type))
234       p_powerEvent = NULL;
235   } else if (event_type == p_stateEvent) {
236     if (value > 0)
237       p_stateCurrent = SURF_RESOURCE_ON;
238     else {
239       lmm_constraint_t cnst = p_constraint;
240
241       p_stateCurrent = SURF_RESOURCE_OFF;
242
243       while ((var = lmm_get_var_from_cnst(surf_cpu_model->p_maxminSystem, cnst, &elem))) {
244         ActionPtr action = (ActionPtr) lmm_variable_id(var);
245
246         if (action->getState() == SURF_ACTION_RUNNING ||
247             action->getState() == SURF_ACTION_READY ||
248             action->getState() == SURF_ACTION_NOT_IN_THE_SYSTEM) {
249           action->m_finish = date;
250           action->setState(SURF_ACTION_FAILED);
251         }
252       }
253     }
254     if (tmgr_trace_event_free(event_type))
255       p_stateEvent = NULL;
256   } else {
257     XBT_CRITICAL("Unknown event ! \n");
258     xbt_abort();
259   }
260
261   return;
262 }
263
264 CpuActionPtr CpuCas01Lmm::execute(double size)
265 {
266
267   XBT_IN("(%s,%g)", m_name, size);
268   CpuCas01ActionLmmPtr action = new CpuCas01ActionLmm(surf_cpu_model, size, p_stateCurrent != SURF_RESOURCE_ON);
269
270   action->m_suspended = 0;     /* Should be useless because of the
271                                                    calloc but it seems to help valgrind... */
272
273   action->p_variable =
274       lmm_variable_new(surf_cpu_model->p_maxminSystem, action,
275                        action->m_priority,
276                        m_powerScale * m_powerPeak, 1);
277   if (surf_cpu_model->p_updateMechanism == UM_LAZY) {
278     action->m_indexHeap = -1;
279     action->m_lastUpdate = surf_get_clock();
280     action->m_lastValue = 0.0;
281   }
282   lmm_expand(surf_cpu_model->p_maxminSystem, p_constraint,
283              action->p_variable, 1.0);
284   XBT_OUT();
285   return action;
286 }
287
288 CpuActionPtr CpuCas01Lmm::sleep(double duration)
289 {
290   if (duration > 0)
291     duration = MAX(duration, MAXMIN_PRECISION);
292
293   XBT_IN("(%s,%g)", m_name, duration);
294   CpuCas01ActionLmmPtr action = (CpuCas01ActionLmmPtr) execute(1.0);
295
296   // FIXME: sleep variables should not consume 1.0 in lmm_expand
297   action->m_maxDuration = duration;
298   action->m_suspended = 2;
299   if (duration == NO_MAX_DURATION) {
300     /* Move to the *end* of the corresponding action set. This convention
301        is used to speed up update_resource_state  */
302     xbt_swag_remove(action, action->p_stateSet);
303     action->p_stateSet = cpu_running_action_set_that_does_not_need_being_checked;
304     xbt_swag_insert(action, action->p_stateSet);
305   }
306
307   lmm_update_variable_weight(surf_cpu_model->p_maxminSystem,
308                              action->p_variable, 0.0);
309   if (surf_cpu_model->p_updateMechanism == UM_LAZY) {     // remove action from the heap
310     action->heapRemove(surf_cpu_model->p_actionHeap);
311     // this is necessary for a variable with weight 0 since such
312     // variables are ignored in lmm and we need to set its max_duration
313     // correctly at the next call to share_resources
314     xbt_swag_insert_at_head(action, surf_cpu_model->p_modifiedSet);
315   }
316
317   XBT_OUT();
318   return action;
319 }
320
321
322 /**********
323  * Action *
324  **********/
325
326