Logo AND Algorithmique Numérique Distribuée

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