Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix a bug
[simgrid.git] / src / surf / workstation_ptask_L07.cpp
1 /* Copyright (c) 2007-2010, 2013. 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 "workstation_ptask_L07.hpp"
8 #include "cpu_interface.hpp"
9 #include "surf_routing.hpp"
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_workstation);
12
13 static int ptask_host_count = 0;
14 static xbt_dict_t ptask_parallel_task_link_set = NULL;
15 lmm_system_t ptask_maxmin_system = NULL;
16
17 WorkstationL07Model::WorkstationL07Model() : WorkstationModel("Workstation ptask_L07") {
18   if (!ptask_maxmin_system)
19         ptask_maxmin_system = lmm_system_new(1);
20   surf_workstation_model = NULL;
21   surf_network_model = new NetworkL07Model();
22   surf_cpu_model_pm = new CpuL07Model();
23   routing_model_create(static_cast<ResourcePtr>(surf_network_model->createResource("__loopback__",
24                                                           498000000, NULL,
25                                                           0.000015, NULL,
26                                                           SURF_RESOURCE_ON, NULL,
27                                                           SURF_LINK_FATPIPE, NULL)));
28   p_cpuModel = surf_cpu_model_pm;
29 }
30
31 WorkstationL07Model::~WorkstationL07Model() {
32   xbt_dict_free(&ptask_parallel_task_link_set);
33
34   delete surf_cpu_model_pm;
35   delete surf_network_model;
36   ptask_host_count = 0;
37
38   if (ptask_maxmin_system) {
39     lmm_system_free(ptask_maxmin_system);
40     ptask_maxmin_system = NULL;
41   }
42 }
43
44 double WorkstationL07Model::shareResources(double /*now*/)
45 {
46   WorkstationL07ActionPtr action;
47
48   ActionListPtr running_actions = getRunningActionSet();
49   double min = this->shareResourcesMaxMin(running_actions,
50                                               ptask_maxmin_system,
51                                               bottleneck_solve);
52
53   for(ActionList::iterator it(running_actions->begin()), itend(running_actions->end())
54          ; it != itend ; ++it) {
55         action = static_cast<WorkstationL07ActionPtr>(&*it);
56     if (action->m_latency > 0) {
57       if (min < 0) {
58         min = action->m_latency;
59         XBT_DEBUG("Updating min (value) with %p (start %f): %f", action,
60                action->getStartTime(), min);
61       } else if (action->m_latency < min) {
62         min = action->m_latency;
63         XBT_DEBUG("Updating min (latency) with %p (start %f): %f", action,
64                action->getStartTime(), min);
65       }
66     }
67   }
68
69   XBT_DEBUG("min value : %f", min);
70
71   return min;
72 }
73
74 void WorkstationL07Model::updateActionsState(double /*now*/, double delta)
75 {
76   double deltap = 0.0;
77   WorkstationL07ActionPtr action;
78
79   ActionListPtr actionSet = getRunningActionSet();
80
81   for(ActionList::iterator it(actionSet->begin()), itNext = it, itend(actionSet->end())
82          ; it != itend ; it=itNext) {
83         ++itNext;
84     action = static_cast<WorkstationL07ActionPtr>(&*it);
85     deltap = delta;
86     if (action->m_latency > 0) {
87       if (action->m_latency > deltap) {
88         double_update(&(action->m_latency), deltap);
89         deltap = 0.0;
90       } else {
91         double_update(&(deltap), action->m_latency);
92         action->m_latency = 0.0;
93       }
94       if ((action->m_latency == 0.0) && (action->isSuspended() == 0)) {
95         action->updateBound();
96         lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 1.0);
97       }
98     }
99     XBT_DEBUG("Action (%p) : remains (%g) updated by %g.",
100            action, action->getRemains(), lmm_variable_getvalue(action->getVariable()) * delta);
101     action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
102
103     if (action->getMaxDuration() != NO_MAX_DURATION)
104       action->updateMaxDuration(delta);
105
106     XBT_DEBUG("Action (%p) : remains (%g).",
107            action, action->getRemains());
108     if ((action->getRemains() <= 0) &&
109         (lmm_get_variable_weight(action->getVariable()) > 0)) {
110       action->finish();
111       action->setState(SURF_ACTION_DONE);
112     } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
113                (action->getMaxDuration() <= 0)) {
114       action->finish();
115      action->setState(SURF_ACTION_DONE);
116     } else {
117       /* Need to check that none of the model has failed */
118       lmm_constraint_t cnst = NULL;
119       int i = 0;
120       void *constraint_id = NULL;
121
122       while ((cnst = lmm_get_cnst_from_var(ptask_maxmin_system, action->getVariable(),
123                                     i++))) {
124         constraint_id = lmm_constraint_id(cnst);
125
126         if (static_cast<WorkstationPtr>(constraint_id)->getState() == SURF_RESOURCE_OFF) {
127           XBT_DEBUG("Action (%p) Failed!!", action);
128           action->finish();
129           action->setState(SURF_ACTION_FAILED);
130           break;
131         }
132       }
133     }
134   }
135   return;
136 }
137
138 ActionPtr WorkstationL07Model::executeParallelTask(int workstation_nb,
139                                                    void **workstation_list,
140                                                  double
141                                                  *computation_amount, double
142                                                  *communication_amount,
143                                                  double rate)
144 {
145   WorkstationL07ActionPtr action;
146   int i, j;
147   unsigned int cpt;
148   int nb_link = 0;
149   int nb_host = 0;
150   double latency = 0.0;
151
152   if (ptask_parallel_task_link_set == NULL)
153     ptask_parallel_task_link_set = xbt_dict_new_homogeneous(NULL);
154
155   xbt_dict_reset(ptask_parallel_task_link_set);
156
157   /* Compute the number of affected resources... */
158   for (i = 0; i < workstation_nb; i++) {
159     for (j = 0; j < workstation_nb; j++) {
160       xbt_dynar_t route=NULL;
161
162       if (communication_amount[i * workstation_nb + j] > 0) {
163         double lat=0.0;
164         unsigned int cpt;
165         void *_link;
166         LinkL07Ptr link;
167
168         routing_platf->getRouteAndLatency(static_cast<WorkstationL07Ptr>(workstation_list[i])->p_netElm,
169                                                   static_cast<WorkstationL07Ptr>(workstation_list[j])->p_netElm,
170                                                   &route,
171                                                   &lat);
172         latency = MAX(latency, lat);
173
174         xbt_dynar_foreach(route, cpt, _link) {
175            link = static_cast<LinkL07Ptr>(_link);
176            xbt_dict_set(ptask_parallel_task_link_set, link->getName(), link, NULL);
177         }
178       }
179     }
180   }
181
182   nb_link = xbt_dict_length(ptask_parallel_task_link_set);
183   xbt_dict_reset(ptask_parallel_task_link_set);
184
185   for (i = 0; i < workstation_nb; i++)
186     if (computation_amount[i] > 0)
187       nb_host++;
188
189   action = new WorkstationL07Action(this, 1, 0);
190   XBT_DEBUG("Creating a parallel task (%p) with %d cpus and %d links.",
191          action, workstation_nb, nb_link);
192   action->m_suspended = 0;        /* Should be useless because of the
193                                    calloc but it seems to help valgrind... */
194   action->m_workstationNb = workstation_nb;
195   action->p_workstationList = (WorkstationPtr *) workstation_list;
196   action->p_computationAmount = computation_amount;
197   action->p_communicationAmount = communication_amount;
198   action->m_latency = latency;
199   action->m_rate = rate;
200
201   action->p_variable = lmm_variable_new(ptask_maxmin_system, action, 1.0,
202                        (action->m_rate > 0) ? action->m_rate : -1.0,
203                        workstation_nb + nb_link);
204
205   if (action->m_latency > 0)
206     lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 0.0);
207
208   for (i = 0; i < workstation_nb; i++)
209     lmm_expand(ptask_maxmin_system,
210                static_cast<CpuPtr>(static_cast<WorkstationL07Ptr>(workstation_list[i])->p_cpu)->getConstraint(),
211                action->getVariable(), computation_amount[i]);
212
213   for (i = 0; i < workstation_nb; i++) {
214     for (j = 0; j < workstation_nb; j++) {
215       void *_link;
216       LinkL07Ptr link;
217
218       xbt_dynar_t route=NULL;
219       if (communication_amount[i * workstation_nb + j] == 0.0)
220         continue;
221
222       routing_platf->getRouteAndLatency(static_cast<WorkstationL07Ptr>(workstation_list[i])->p_netElm,
223                                         static_cast<WorkstationL07Ptr>(workstation_list[j])->p_netElm,
224                                             &route, NULL);
225
226       xbt_dynar_foreach(route, cpt, _link) {
227         link = static_cast<LinkL07Ptr>(_link);
228         lmm_expand_add(ptask_maxmin_system, link->getConstraint(),
229                        action->getVariable(),
230                        communication_amount[i * workstation_nb + j]);
231       }
232     }
233   }
234
235   if (nb_link + nb_host == 0) {
236     action->setCost(1.0);
237     action->setRemains(0.0);
238   }
239
240   return static_cast<ActionPtr>(action);
241 }
242
243 ResourcePtr WorkstationL07Model::createResource(const char *name, double /*power_scale*/,
244                                                 double /*power_initial*/,
245                                                 tmgr_trace_t /*power_trace*/,
246                                                 e_surf_resource_state_t /*state_initial*/,
247                                                 tmgr_trace_t /*state_trace*/,
248                                                 xbt_dict_t /*cpu_properties*/)
249 {
250   WorkstationL07Ptr wk = NULL;
251   xbt_assert(!surf_workstation_resource_priv(surf_workstation_resource_by_name(name)),
252               "Host '%s' declared several times in the platform file.",
253               name);
254
255   wk = new WorkstationL07(this, name, NULL,
256                                   static_cast<RoutingEdgePtr>(xbt_lib_get_or_null(host_lib, name, ROUTING_HOST_LEVEL)),
257                                   static_cast<CpuPtr>(xbt_lib_get_or_null(host_lib, name, SURF_CPU_LEVEL)));
258
259   xbt_lib_set(host_lib, name, SURF_WKS_LEVEL, static_cast<ResourcePtr>(wk));
260
261   return wk;//FIXME:xbt_lib_get_elm_or_null(host_lib, name);
262 }
263
264 ActionPtr WorkstationL07Model::communicate(WorkstationPtr src, WorkstationPtr dst,
265                                        double size, double rate)
266 {
267   void **workstation_list = xbt_new0(void *, 2);
268   double *computation_amount = xbt_new0(double, 2);
269   double *communication_amount = xbt_new0(double, 4);
270   ActionPtr res = NULL;
271
272   workstation_list[0] = static_cast<ResourcePtr>(src);
273   workstation_list[1] = static_cast<ResourcePtr>(dst);
274   communication_amount[1] = size;
275
276   res = executeParallelTask(2, workstation_list,
277                                     computation_amount,
278                                     communication_amount, rate);
279
280   return res;
281 }
282
283 xbt_dynar_t WorkstationL07Model::getRoute(WorkstationPtr src, WorkstationPtr dst)
284 {
285   xbt_dynar_t route=NULL;
286   routing_platf->getRouteAndLatency(src->p_netElm, dst->p_netElm, &route, NULL);
287   return route;
288 }
289
290 ResourcePtr CpuL07Model::createResource(const char *name, double power_scale,
291                                double power_initial,
292                                tmgr_trace_t power_trace,
293                                e_surf_resource_state_t state_initial,
294                                tmgr_trace_t state_trace,
295                                xbt_dict_t cpu_properties)
296 {
297   xbt_assert(!surf_workstation_resource_priv(surf_workstation_resource_by_name(name)),
298               "Host '%s' declared several times in the platform file.",
299               name);
300
301   CpuL07Ptr cpu = new CpuL07(this, name, cpu_properties,
302                                      power_scale, power_initial, power_trace,state_initial, state_trace);
303
304   xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, static_cast<ResourcePtr>(cpu));
305
306   return cpu;//FIXME:xbt_lib_get_elm_or_null(host_lib, name);
307 }
308
309 NetworkLinkPtr NetworkL07Model::createResource(const char *name,
310                                  double bw_initial,
311                                  tmgr_trace_t bw_trace,
312                                  double lat_initial,
313                                  tmgr_trace_t lat_trace,
314                                  e_surf_resource_state_t
315                                  state_initial,
316                                  tmgr_trace_t state_trace,
317                                  e_surf_link_sharing_policy_t policy,
318                                  xbt_dict_t properties)
319 {
320   xbt_assert(!xbt_lib_get_or_null(link_lib, name, SURF_LINK_LEVEL),
321                       "Link '%s' declared several times in the platform file.",
322                       name);
323
324   LinkL07Ptr nw_link = new LinkL07(this, name, properties,
325                                        bw_initial, bw_trace,
326                                        lat_initial, lat_trace,
327                                        state_initial, state_trace,
328                                        policy);
329
330   xbt_lib_set(link_lib, name, SURF_LINK_LEVEL, static_cast<ResourcePtr>(nw_link));
331   return nw_link;
332 }
333
334 void WorkstationL07Model::addTraces()
335 {
336   xbt_dict_cursor_t cursor = NULL;
337   char *trace_name, *elm;
338
339   if (!trace_connect_list_host_avail)
340     return;
341
342   /* Connect traces relative to cpu */
343   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
344     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
345     CpuL07Ptr host = static_cast<CpuL07Ptr>(surf_cpu_resource_priv(surf_cpu_resource_by_name(elm)));
346
347     xbt_assert(host, "Host %s undefined", elm);
348     xbt_assert(trace, "Trace %s undefined", trace_name);
349
350     host->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(host));
351   }
352
353   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
354     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
355     CpuL07Ptr host = static_cast<CpuL07Ptr>(surf_cpu_resource_priv(surf_cpu_resource_by_name(elm)));
356
357     xbt_assert(host, "Host %s undefined", elm);
358     xbt_assert(trace, "Trace %s undefined", trace_name);
359
360     host->p_power.event = tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(host));
361   }
362
363   /* Connect traces relative to network */
364   xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
365     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
366     LinkL07Ptr link = static_cast<LinkL07Ptr>(xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL));
367
368     xbt_assert(link, "Link %s undefined", elm);
369     xbt_assert(trace, "Trace %s undefined", trace_name);
370
371     link->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(link));
372   }
373
374   xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
375     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
376     LinkL07Ptr link = static_cast<LinkL07Ptr>(xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL));
377
378     xbt_assert(link, "Link %s undefined", elm);
379     xbt_assert(trace, "Trace %s undefined", trace_name);
380
381     link->p_bwEvent = tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(link));
382   }
383
384   xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
385     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
386     LinkL07Ptr link = static_cast<LinkL07Ptr>(xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL));
387
388     xbt_assert(link, "Link %s undefined", elm);
389     xbt_assert(trace, "Trace %s undefined", trace_name);
390
391     link->p_latEvent = tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(link));
392   }
393 }
394
395 /************
396  * Resource *
397  ************/
398
399 WorkstationL07::WorkstationL07(WorkstationModelPtr model, const char* name, xbt_dict_t props, RoutingEdgePtr netElm, CpuPtr cpu)
400   : Workstation(model, name, props, NULL, netElm, cpu)
401 {
402 }
403
404 double WorkstationL07::getPowerPeakAt(int /*pstate_index*/)
405 {
406         XBT_DEBUG("[ws_get_power_peak_at] Not implemented for workstation_ptask_L07");
407         return 0.0;
408 }
409
410 int WorkstationL07::getNbPstates()
411 {
412         XBT_DEBUG("[ws_get_nb_pstates] Not implemented for workstation_ptask_L07");
413         return 0.0;
414 }
415
416 void WorkstationL07::setPowerPeakAt(int /*pstate_index*/)
417 {
418         XBT_DEBUG("[ws_set_power_peak_at] Not implemented for workstation_ptask_L07");
419 }
420
421 double WorkstationL07::getConsumedEnergy()
422 {
423         XBT_DEBUG("[ws_get_consumed_energy] Not implemented for workstation_ptask_L07");
424         return 0.0;
425 }
426
427 CpuL07::CpuL07(CpuL07ModelPtr model, const char* name, xbt_dict_t props,
428                    double power_scale,
429                        double power_initial, tmgr_trace_t power_trace,
430                        e_surf_resource_state_t state_initial, tmgr_trace_t state_trace)
431  : Cpu(model, name, props, lmm_constraint_new(ptask_maxmin_system, this, power_initial * power_scale),
432            1, 0, 0)
433 {
434   p_power.scale = power_scale;
435   xbt_assert(p_power.scale > 0, "Power has to be >0");
436
437   m_powerCurrent = power_initial;
438   if (power_trace)
439         p_power.event = tmgr_history_add_trace(history, power_trace, 0.0, 0, static_cast<ResourcePtr>(this));
440
441   m_stateCurrent = state_initial;
442   if (state_trace)
443         p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, static_cast<ResourcePtr>(this));
444 }
445
446 LinkL07::LinkL07(NetworkL07ModelPtr model, const char* name, xbt_dict_t props,
447                          double bw_initial,
448                          tmgr_trace_t bw_trace,
449                          double lat_initial,
450                          tmgr_trace_t lat_trace,
451                          e_surf_resource_state_t state_initial,
452                          tmgr_trace_t state_trace,
453                          e_surf_link_sharing_policy_t policy)
454  : NetworkLink(model, name, props, lmm_constraint_new(ptask_maxmin_system, this, bw_initial), history, state_trace)
455 {
456   m_bwCurrent = bw_initial;
457   if (bw_trace)
458     p_bwEvent = tmgr_history_add_trace(history, bw_trace, 0.0, 0, static_cast<ResourcePtr>(this));
459
460   m_stateCurrent = state_initial;
461   m_latCurrent = lat_initial;
462
463   if (lat_trace)
464         p_latEvent = tmgr_history_add_trace(history, lat_trace, 0.0, 0, static_cast<ResourcePtr>(this));
465
466   if (policy == SURF_LINK_FATPIPE)
467         lmm_constraint_shared(getConstraint());
468 }
469
470 bool CpuL07::isUsed(){
471   return lmm_constraint_used(ptask_maxmin_system, getConstraint());
472 }
473
474 bool LinkL07::isUsed(){
475   return lmm_constraint_used(ptask_maxmin_system, getConstraint());
476 }
477
478 void CpuL07::updateState(tmgr_trace_event_t event_type, double value, double /*date*/){
479   XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
480   if (event_type == p_power.event) {
481         m_powerCurrent = value;
482     lmm_update_constraint_bound(ptask_maxmin_system, getConstraint(), m_powerCurrent * p_power.scale);
483     if (tmgr_trace_event_free(event_type))
484       p_power.event = NULL;
485   } else if (event_type == p_stateEvent) {
486     if (value > 0)
487       m_stateCurrent = SURF_RESOURCE_ON;
488     else
489       m_stateCurrent = SURF_RESOURCE_OFF;
490     if (tmgr_trace_event_free(event_type))
491       p_stateEvent = NULL;
492   } else {
493     XBT_CRITICAL("Unknown event ! \n");
494     xbt_abort();
495   }
496   return;
497 }
498
499 void LinkL07::updateState(tmgr_trace_event_t event_type, double value, double date){
500   XBT_DEBUG("Updating link %s (%p) with value=%f for date=%g", getName(), this, value, date);
501   if (event_type == p_bwEvent) {
502     m_bwCurrent = value;
503     lmm_update_constraint_bound(ptask_maxmin_system, getConstraint(), m_bwCurrent);
504     if (tmgr_trace_event_free(event_type))
505       p_bwEvent = NULL;
506   } else if (event_type == p_latEvent) {
507     lmm_variable_t var = NULL;
508     WorkstationL07ActionPtr action;
509     lmm_element_t elem = NULL;
510
511     m_latCurrent = value;
512     while ((var = lmm_get_var_from_cnst(ptask_maxmin_system, getConstraint(), &elem))) {
513       action = (WorkstationL07ActionPtr) lmm_variable_id(var);
514       action->updateBound();
515     }
516     if (tmgr_trace_event_free(event_type))
517       p_latEvent = NULL;
518   } else if (event_type == p_stateEvent) {
519     if (value > 0)
520       m_stateCurrent = SURF_RESOURCE_ON;
521     else
522       m_stateCurrent = SURF_RESOURCE_OFF;
523     if (tmgr_trace_event_free(event_type))
524       p_stateEvent = NULL;
525   } else {
526     XBT_CRITICAL("Unknown event ! \n");
527     xbt_abort();
528   }
529   return;
530 }
531
532 e_surf_resource_state_t WorkstationL07::getState()
533 {
534   return p_cpu->getState();
535 }
536
537 e_surf_resource_state_t CpuL07::getState()
538 {
539   return m_stateCurrent;
540 }
541
542 double CpuL07::getSpeed(double load)
543 {
544   return load * p_power.scale;
545 }
546
547 double CpuL07::getAvailableSpeed()
548 {
549   return m_powerCurrent;
550 }
551
552 ActionPtr WorkstationL07::execute(double size)
553 {
554   void **workstation_list = xbt_new0(void *, 1);
555   double *computation_amount = xbt_new0(double, 1);
556   double *communication_amount = xbt_new0(double, 1);
557
558   workstation_list[0] = static_cast<ResourcePtr>(this);
559   communication_amount[0] = 0.0;
560   computation_amount[0] = size;
561
562   return static_cast<WorkstationL07ModelPtr>(getModel())->executeParallelTask(1, workstation_list,
563                                               computation_amount,
564                                      communication_amount, -1);
565 }
566
567 ActionPtr WorkstationL07::sleep(double duration)
568 {
569   WorkstationL07ActionPtr action = NULL;
570
571   XBT_IN("(%s,%g)", getName(), duration);
572
573   action = static_cast<WorkstationL07ActionPtr>(execute(1.0));
574   action->m_maxDuration = duration;
575   action->m_suspended = 2;
576   lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 0.0);
577
578   XBT_OUT();
579   return action;
580 }
581
582 double LinkL07::getBandwidth()
583 {
584   return m_bwCurrent;
585 }
586
587 double LinkL07::getLatency()
588 {
589   return m_latCurrent;
590 }
591
592 bool LinkL07::isShared()
593 {
594   return lmm_constraint_is_shared(getConstraint());
595 }
596
597 /**********
598  * Action *
599  **********/
600
601 WorkstationL07Action::~WorkstationL07Action(){
602   free(p_workstationList);
603   free(p_communicationAmount);
604   free(p_computationAmount);
605 }
606
607 void WorkstationL07Action::updateBound()
608 {
609   double lat_current = 0.0;
610   double lat_bound = -1.0;
611   int i, j;
612
613   for (i = 0; i < m_workstationNb; i++) {
614     for (j = 0; j < m_workstationNb; j++) {
615       xbt_dynar_t route=NULL;
616
617       if (p_communicationAmount[i * m_workstationNb + j] > 0) {
618         double lat = 0.0;
619         routing_platf->getRouteAndLatency(static_cast<WorkstationL07Ptr>(((void**)p_workstationList)[i])->p_netElm,
620                                           static_cast<WorkstationL07Ptr>(((void**)p_workstationList)[j])->p_netElm,
621                                                           &route, &lat);
622
623         lat_current = MAX(lat_current, lat * p_communicationAmount[i * m_workstationNb + j]);
624       }
625     }
626   }
627   lat_bound = sg_tcp_gamma / (2.0 * lat_current);
628   XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
629   if ((m_latency == 0.0) && (m_suspended == 0)) {
630     if (m_rate < 0)
631       lmm_update_variable_bound(ptask_maxmin_system, getVariable(), lat_bound);
632     else
633       lmm_update_variable_bound(ptask_maxmin_system, getVariable(), min(m_rate, lat_bound));
634   }
635 }
636
637 int WorkstationL07Action::unref()
638 {
639   m_refcount--;
640   if (!m_refcount) {
641     if (actionHook::is_linked())
642           p_stateSet->erase(p_stateSet->iterator_to(*this));
643     if (getVariable())
644       lmm_variable_free(ptask_maxmin_system, getVariable());
645     delete this;
646     return 1;
647   }
648   return 0;
649 }
650
651 void WorkstationL07Action::cancel()
652 {
653   setState(SURF_ACTION_FAILED);
654   return;
655 }
656
657 void WorkstationL07Action::suspend()
658 {
659   XBT_IN("(%p))", this);
660   if (m_suspended != 2) {
661     m_suspended = 1;
662     lmm_update_variable_weight(ptask_maxmin_system, getVariable(), 0.0);
663   }
664   XBT_OUT();
665 }
666
667 void WorkstationL07Action::resume()
668 {
669   XBT_IN("(%p)", this);
670   if (m_suspended != 2) {
671     lmm_update_variable_weight(ptask_maxmin_system, getVariable(), 1.0);
672     m_suspended = 0;
673   }
674   XBT_OUT();
675 }
676
677 bool WorkstationL07Action::isSuspended()
678 {
679   return m_suspended == 1;
680 }
681
682 void WorkstationL07Action::setMaxDuration(double duration)
683 {                               /* FIXME: should inherit */
684   XBT_IN("(%p,%g)", this, duration);
685   m_maxDuration = duration;
686   XBT_OUT();
687 }
688
689 void WorkstationL07Action::setPriority(double priority)
690 {                               /* FIXME: should inherit */
691   XBT_IN("(%p,%g)", this, priority);
692   m_priority = priority;
693   XBT_OUT();
694 }
695
696 double WorkstationL07Action::getRemains()
697 {
698   XBT_IN("(%p)", this);
699   XBT_OUT();
700   return m_remains;
701 }
702
703 /*FIXME:remove static void ptask_finalize(void)
704 {
705   xbt_dict_free(&ptask_parallel_task_link_set);
706
707   delete surf_workstation_model;
708   surf_workstation_model = NULL;
709   delete surf_network_model;
710   surf_network_model = NULL;
711
712   ptask_host_count = 0;
713
714   if (ptask_maxmin_system) {
715     lmm_system_free(ptask_maxmin_system);
716     ptask_maxmin_system = NULL;
717   }
718   }*/
719
720 /**************************************/
721 /******* Resource Private    **********/
722 /**************************************/
723
724 /**************************************/
725 /*** Resource Creation & Destruction **/
726 /**************************************/
727
728 static void ptask_parse_workstation_init(sg_platf_host_cbarg_t host)
729 {
730   double power_peak = xbt_dynar_get_as(host->power_peak, host->pstate, double);
731   //cpu->power_peak = power_peak;
732   xbt_dynar_free(&(host->power_peak));  /* kill memory leak */
733   static_cast<WorkstationL07ModelPtr>(surf_workstation_model)->createResource(
734       host->id,
735       power_peak,
736       host->power_scale,
737       host->power_trace,
738       host->initial_state,
739       host->state_trace,
740       host->properties);
741 }
742
743 static void ptask_parse_cpu_init(sg_platf_host_cbarg_t host)
744 {
745   double power_peak = xbt_dynar_get_as(host->power_peak, host->pstate, double);
746   static_cast<CpuL07ModelPtr>(surf_cpu_model_pm)->createResource(
747       host->id,
748       power_peak,
749       host->power_scale,
750       host->power_trace,
751       host->initial_state,
752       host->state_trace,
753       host->properties);
754 }
755
756
757
758 static void ptask_parse_link_init(sg_platf_link_cbarg_t link)
759 {
760   if (link->policy == SURF_LINK_FULLDUPLEX) {
761     char *link_id;
762     link_id = bprintf("%s_UP", link->id);
763     static_cast<NetworkL07ModelPtr>(surf_network_model)->createResource(link_id,
764                                link->bandwidth,
765                                link->bandwidth_trace,
766                                link->latency,
767                                link->latency_trace,
768                                link->state,
769                                link->state_trace,
770                                link->policy,
771                                link->properties);
772     xbt_free(link_id);
773     link_id = bprintf("%s_DOWN", link->id);
774     static_cast<NetworkL07ModelPtr>(surf_network_model)->createResource(link_id,
775                                link->bandwidth,
776                                link->bandwidth_trace,
777                                link->latency,
778                                link->latency_trace,
779                                link->state,
780                                link->state_trace,
781                                link->policy,
782                                NULL); /* FIXME: We need to deep copy the
783                                        * properties or we won't be able to free
784                                        * it */
785     xbt_free(link_id);
786   } else {
787           static_cast<NetworkL07ModelPtr>(surf_network_model)->createResource(link->id,
788                                link->bandwidth,
789                                link->bandwidth_trace,
790                                link->latency,
791                                link->latency_trace,
792                                link->state,
793                                link->state_trace,
794                                link->policy,
795                                link->properties);
796   }
797
798   current_property_set = NULL;
799 }
800
801 static void ptask_add_traces(){
802   static_cast<WorkstationL07ModelPtr>(surf_workstation_model)->addTraces();
803 }
804
805 static void ptask_define_callbacks()
806 {
807   sg_platf_host_add_cb(ptask_parse_cpu_init);
808   sg_platf_host_add_cb(ptask_parse_workstation_init);
809   sg_platf_link_add_cb(ptask_parse_link_init);
810   sg_platf_postparse_add_cb(ptask_add_traces);
811 }
812
813 /**************************************/
814 /*************** Generic **************/
815 /**************************************/
816 void surf_workstation_model_init_ptask_L07(void)
817 {
818   XBT_INFO("surf_workstation_model_init_ptask_L07");
819   xbt_assert(!surf_cpu_model_pm, "CPU model type already defined");
820   xbt_assert(!surf_network_model, "network model type already defined");
821   ptask_define_callbacks();
822   surf_workstation_model = new WorkstationL07Model();
823   ModelPtr model = static_cast<ModelPtr>(surf_workstation_model);
824   xbt_dynar_push(model_list, &model);
825   xbt_dynar_push(model_list_invoke, &model);
826 }