Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge lmm into base to avoid diamond inheritance
[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 = dynamic_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 = dynamic_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(dynamic_cast<WorkstationL07Ptr>(
169                                                    static_cast<ResourcePtr>(
170                                                     workstation_list[i]))->p_netElm,
171                                                   dynamic_cast<WorkstationL07Ptr>(
172                                                    static_cast<ResourcePtr>(
173                                                         workstation_list[j]))->p_netElm,
174                                                   &route,
175                                                   &lat);
176         latency = MAX(latency, lat);
177
178         xbt_dynar_foreach(route, cpt, _link) {
179            link = dynamic_cast<LinkL07Ptr>(static_cast<ResourcePtr>(_link));
180            xbt_dict_set(ptask_parallel_task_link_set, link->getName(), link, NULL);
181         }
182       }
183     }
184   }
185
186   nb_link = xbt_dict_length(ptask_parallel_task_link_set);
187   xbt_dict_reset(ptask_parallel_task_link_set);
188
189   for (i = 0; i < workstation_nb; i++)
190     if (computation_amount[i] > 0)
191       nb_host++;
192
193   action = new WorkstationL07Action(this, 1, 0);
194   XBT_DEBUG("Creating a parallel task (%p) with %d cpus and %d links.",
195          action, workstation_nb, nb_link);
196   action->m_suspended = 0;        /* Should be useless because of the
197                                    calloc but it seems to help valgrind... */
198   action->m_workstationNb = workstation_nb;
199   action->p_workstationList = (WorkstationPtr *) workstation_list;
200   action->p_computationAmount = computation_amount;
201   action->p_communicationAmount = communication_amount;
202   action->m_latency = latency;
203   action->m_rate = rate;
204
205   action->p_variable = lmm_variable_new(ptask_maxmin_system, action, 1.0,
206                        (action->m_rate > 0) ? action->m_rate : -1.0,
207                        workstation_nb + nb_link);
208
209   if (action->m_latency > 0)
210     lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 0.0);
211
212   for (i = 0; i < workstation_nb; i++)
213     lmm_expand(ptask_maxmin_system,
214                static_cast<CpuPtr>(dynamic_cast<WorkstationL07Ptr>(
215                                            static_cast<ResourcePtr>(workstation_list[i]))->p_cpu)->getConstraint(),
216                action->getVariable(), computation_amount[i]);
217
218   for (i = 0; i < workstation_nb; i++) {
219     for (j = 0; j < workstation_nb; j++) {
220       void *_link;
221       LinkL07Ptr link;
222
223       xbt_dynar_t route=NULL;
224       if (communication_amount[i * workstation_nb + j] == 0.0)
225         continue;
226
227       routing_platf->getRouteAndLatency(dynamic_cast<WorkstationL07Ptr>(
228                                          static_cast<ResourcePtr>(workstation_list[i]))->p_netElm,
229                                         dynamic_cast<WorkstationL07Ptr>(
230                                          static_cast<ResourcePtr>(workstation_list[j]))->p_netElm,
231                                             &route, NULL);
232
233       xbt_dynar_foreach(route, cpt, _link) {
234         link = dynamic_cast<LinkL07Ptr>(static_cast<ResourcePtr>(_link));
235         lmm_expand_add(ptask_maxmin_system, link->getConstraint(),
236                        action->getVariable(),
237                        communication_amount[i * workstation_nb + j]);
238       }
239     }
240   }
241
242   if (nb_link + nb_host == 0) {
243     action->setCost(1.0);
244     action->setRemains(0.0);
245   }
246
247   return static_cast<ActionPtr>(action);
248 }
249
250 ResourcePtr WorkstationL07Model::createResource(const char *name, double /*power_scale*/,
251                                                 double /*power_initial*/,
252                                                 tmgr_trace_t /*power_trace*/,
253                                                 e_surf_resource_state_t /*state_initial*/,
254                                                 tmgr_trace_t /*state_trace*/,
255                                                 xbt_dict_t /*cpu_properties*/)
256 {
257   WorkstationL07Ptr wk = NULL;
258   xbt_assert(!surf_workstation_resource_priv(surf_workstation_resource_by_name(name)),
259               "Host '%s' declared several times in the platform file.",
260               name);
261
262   wk = new WorkstationL07(this, name, NULL,
263                                   static_cast<RoutingEdgePtr>(xbt_lib_get_or_null(host_lib, name, ROUTING_HOST_LEVEL)),
264                                   dynamic_cast<CpuPtr>(static_cast<ResourcePtr>(xbt_lib_get_or_null(host_lib, name, SURF_CPU_LEVEL))));
265
266   xbt_lib_set(host_lib, name, SURF_WKS_LEVEL, static_cast<ResourcePtr>(wk));
267
268   return wk;//FIXME:xbt_lib_get_elm_or_null(host_lib, name);
269 }
270
271 ActionPtr WorkstationL07Model::communicate(WorkstationPtr src, WorkstationPtr dst,
272                                        double size, double rate)
273 {
274   void **workstation_list = xbt_new0(void *, 2);
275   double *computation_amount = xbt_new0(double, 2);
276   double *communication_amount = xbt_new0(double, 4);
277   ActionPtr res = NULL;
278
279   workstation_list[0] = static_cast<ResourcePtr>(src);
280   workstation_list[1] = static_cast<ResourcePtr>(dst);
281   communication_amount[1] = size;
282
283   res = executeParallelTask(2, workstation_list,
284                                     computation_amount,
285                                     communication_amount, rate);
286
287   return res;
288 }
289
290 xbt_dynar_t WorkstationL07Model::getRoute(WorkstationPtr src, WorkstationPtr dst)
291 {
292   xbt_dynar_t route=NULL;
293   routing_platf->getRouteAndLatency(src->p_netElm, dst->p_netElm, &route, NULL);
294   return route;
295 }
296
297 ResourcePtr CpuL07Model::createResource(const char *name, double power_scale,
298                                double power_initial,
299                                tmgr_trace_t power_trace,
300                                e_surf_resource_state_t state_initial,
301                                tmgr_trace_t state_trace,
302                                xbt_dict_t cpu_properties)
303 {
304   xbt_assert(!surf_workstation_resource_priv(surf_workstation_resource_by_name(name)),
305               "Host '%s' declared several times in the platform file.",
306               name);
307
308   CpuL07Ptr cpu = new CpuL07(this, name, cpu_properties,
309                                      power_scale, power_initial, power_trace,state_initial, state_trace);
310
311   xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, static_cast<ResourcePtr>(cpu));
312
313   return cpu;//FIXME:xbt_lib_get_elm_or_null(host_lib, name);
314 }
315
316 NetworkLinkPtr NetworkL07Model::createResource(const char *name,
317                                  double bw_initial,
318                                  tmgr_trace_t bw_trace,
319                                  double lat_initial,
320                                  tmgr_trace_t lat_trace,
321                                  e_surf_resource_state_t
322                                  state_initial,
323                                  tmgr_trace_t state_trace,
324                                  e_surf_link_sharing_policy_t policy,
325                                  xbt_dict_t properties)
326 {
327   xbt_assert(!xbt_lib_get_or_null(link_lib, name, SURF_LINK_LEVEL),
328                       "Link '%s' declared several times in the platform file.",
329                       name);
330
331   LinkL07Ptr nw_link = new LinkL07(this, name, properties,
332                                        bw_initial, bw_trace,
333                                        lat_initial, lat_trace,
334                                        state_initial, state_trace,
335                                        policy);
336
337   xbt_lib_set(link_lib, name, SURF_LINK_LEVEL, static_cast<ResourcePtr>(nw_link));
338   return nw_link;
339 }
340
341 void WorkstationL07Model::addTraces()
342 {
343   xbt_dict_cursor_t cursor = NULL;
344   char *trace_name, *elm;
345
346   if (!trace_connect_list_host_avail)
347     return;
348
349   /* Connect traces relative to cpu */
350   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
351     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
352     CpuL07Ptr host = dynamic_cast<CpuL07Ptr>(
353                            static_cast<ResourcePtr>(
354                              surf_cpu_resource_priv(
355                                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_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(host));
361   }
362
363   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
364     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
365     CpuL07Ptr host = dynamic_cast<CpuL07Ptr>(
366                            static_cast<ResourcePtr>(
367                              surf_cpu_resource_priv(
368                                surf_cpu_resource_by_name(elm))));
369
370     xbt_assert(host, "Host %s undefined", elm);
371     xbt_assert(trace, "Trace %s undefined", trace_name);
372
373     host->p_power.event = tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(host));
374   }
375
376   /* Connect traces relative to network */
377   xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
378     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
379     LinkL07Ptr link = dynamic_cast<LinkL07Ptr>(static_cast<ResourcePtr>(xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL)));
380
381     xbt_assert(link, "Link %s undefined", elm);
382     xbt_assert(trace, "Trace %s undefined", trace_name);
383
384     link->p_stateEvent = tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(link));
385   }
386
387   xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
388     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
389     LinkL07Ptr link = dynamic_cast<LinkL07Ptr>(static_cast<ResourcePtr>(xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL)));
390
391     xbt_assert(link, "Link %s undefined", elm);
392     xbt_assert(trace, "Trace %s undefined", trace_name);
393
394     link->p_bwEvent = tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(link));
395   }
396
397   xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
398     tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
399     LinkL07Ptr link = dynamic_cast<LinkL07Ptr>(static_cast<ResourcePtr>(xbt_lib_get_or_null(link_lib, elm, SURF_LINK_LEVEL)));
400
401     xbt_assert(link, "Link %s undefined", elm);
402     xbt_assert(trace, "Trace %s undefined", trace_name);
403
404     link->p_latEvent = tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(link));
405   }
406 }
407
408 /************
409  * Resource *
410  ************/
411
412 WorkstationL07::WorkstationL07(WorkstationModelPtr model, const char* name, xbt_dict_t props, RoutingEdgePtr netElm, CpuPtr cpu)
413   : Workstation(model, name, props, NULL, netElm, cpu)
414 {
415 }
416
417 double WorkstationL07::getPowerPeakAt(int /*pstate_index*/)
418 {
419         XBT_DEBUG("[ws_get_power_peak_at] Not implemented for workstation_ptask_L07");
420         return 0.0;
421 }
422
423 int WorkstationL07::getNbPstates()
424 {
425         XBT_DEBUG("[ws_get_nb_pstates] Not implemented for workstation_ptask_L07");
426         return 0.0;
427 }
428
429 void WorkstationL07::setPowerPeakAt(int /*pstate_index*/)
430 {
431         XBT_DEBUG("[ws_set_power_peak_at] Not implemented for workstation_ptask_L07");
432 }
433
434 double WorkstationL07::getConsumedEnergy()
435 {
436         XBT_DEBUG("[ws_get_consumed_energy] Not implemented for workstation_ptask_L07");
437         return 0.0;
438 }
439
440 CpuL07::CpuL07(CpuL07ModelPtr model, const char* name, xbt_dict_t props,
441                    double power_scale,
442                        double power_initial, tmgr_trace_t power_trace,
443                        e_surf_resource_state_t state_initial, tmgr_trace_t state_trace)
444  : Cpu(model, name, props, lmm_constraint_new(ptask_maxmin_system, this, power_initial * power_scale),
445            1, 0, 0)
446 {
447   p_power.scale = power_scale;
448   xbt_assert(p_power.scale > 0, "Power has to be >0");
449
450   m_powerCurrent = power_initial;
451   if (power_trace)
452         p_power.event = tmgr_history_add_trace(history, power_trace, 0.0, 0, static_cast<ResourcePtr>(this));
453
454   m_stateCurrent = state_initial;
455   if (state_trace)
456         p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, static_cast<ResourcePtr>(this));
457 }
458
459 LinkL07::LinkL07(NetworkL07ModelPtr model, const char* name, xbt_dict_t props,
460                          double bw_initial,
461                          tmgr_trace_t bw_trace,
462                          double lat_initial,
463                          tmgr_trace_t lat_trace,
464                          e_surf_resource_state_t state_initial,
465                          tmgr_trace_t state_trace,
466                          e_surf_link_sharing_policy_t policy)
467  : NetworkLink(model, name, props, lmm_constraint_new(ptask_maxmin_system, this, bw_initial), history, state_trace)
468 {
469   m_bwCurrent = bw_initial;
470   if (bw_trace)
471     p_bwEvent = tmgr_history_add_trace(history, bw_trace, 0.0, 0, static_cast<ResourcePtr>(this));
472
473   m_stateCurrent = state_initial;
474   m_latCurrent = lat_initial;
475
476   if (lat_trace)
477         p_latEvent = tmgr_history_add_trace(history, lat_trace, 0.0, 0, static_cast<ResourcePtr>(this));
478
479   if (policy == SURF_LINK_FATPIPE)
480         lmm_constraint_shared(getConstraint());
481 }
482
483 bool CpuL07::isUsed(){
484   return lmm_constraint_used(ptask_maxmin_system, getConstraint());
485 }
486
487 bool LinkL07::isUsed(){
488   return lmm_constraint_used(ptask_maxmin_system, getConstraint());
489 }
490
491 void CpuL07::updateState(tmgr_trace_event_t event_type, double value, double /*date*/){
492   XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
493   if (event_type == p_power.event) {
494         m_powerCurrent = value;
495     lmm_update_constraint_bound(ptask_maxmin_system, getConstraint(), m_powerCurrent * p_power.scale);
496     if (tmgr_trace_event_free(event_type))
497       p_power.event = NULL;
498   } else if (event_type == p_stateEvent) {
499     if (value > 0)
500       m_stateCurrent = SURF_RESOURCE_ON;
501     else
502       m_stateCurrent = SURF_RESOURCE_OFF;
503     if (tmgr_trace_event_free(event_type))
504       p_stateEvent = NULL;
505   } else {
506     XBT_CRITICAL("Unknown event ! \n");
507     xbt_abort();
508   }
509   return;
510 }
511
512 void LinkL07::updateState(tmgr_trace_event_t event_type, double value, double date){
513   XBT_DEBUG("Updating link %s (%p) with value=%f for date=%g", getName(), this, value, date);
514   if (event_type == p_bwEvent) {
515     m_bwCurrent = value;
516     lmm_update_constraint_bound(ptask_maxmin_system, getConstraint(), m_bwCurrent);
517     if (tmgr_trace_event_free(event_type))
518       p_bwEvent = NULL;
519   } else if (event_type == p_latEvent) {
520     lmm_variable_t var = NULL;
521     WorkstationL07ActionPtr action;
522     lmm_element_t elem = NULL;
523
524     m_latCurrent = value;
525     while ((var = lmm_get_var_from_cnst(ptask_maxmin_system, getConstraint(), &elem))) {
526       action = (WorkstationL07ActionPtr) lmm_variable_id(var);
527       action->updateBound();
528     }
529     if (tmgr_trace_event_free(event_type))
530       p_latEvent = NULL;
531   } else if (event_type == p_stateEvent) {
532     if (value > 0)
533       m_stateCurrent = SURF_RESOURCE_ON;
534     else
535       m_stateCurrent = SURF_RESOURCE_OFF;
536     if (tmgr_trace_event_free(event_type))
537       p_stateEvent = NULL;
538   } else {
539     XBT_CRITICAL("Unknown event ! \n");
540     xbt_abort();
541   }
542   return;
543 }
544
545 e_surf_resource_state_t WorkstationL07::getState()
546 {
547   return p_cpu->getState();
548 }
549
550 e_surf_resource_state_t CpuL07::getState()
551 {
552   return m_stateCurrent;
553 }
554
555 double CpuL07::getSpeed(double load)
556 {
557   return load * p_power.scale;
558 }
559
560 double CpuL07::getAvailableSpeed()
561 {
562   return m_powerCurrent;
563 }
564
565 ActionPtr WorkstationL07::execute(double size)
566 {
567   void **workstation_list = xbt_new0(void *, 1);
568   double *computation_amount = xbt_new0(double, 1);
569   double *communication_amount = xbt_new0(double, 1);
570
571   workstation_list[0] = static_cast<ResourcePtr>(this);
572   communication_amount[0] = 0.0;
573   computation_amount[0] = size;
574
575   return static_cast<WorkstationL07ModelPtr>(getModel())->executeParallelTask(1, workstation_list,
576                                               computation_amount,
577                                      communication_amount, -1);
578 }
579
580 ActionPtr WorkstationL07::sleep(double duration)
581 {
582   WorkstationL07ActionPtr action = NULL;
583
584   XBT_IN("(%s,%g)", getName(), duration);
585
586   action = dynamic_cast<WorkstationL07ActionPtr>(execute(1.0));
587   action->m_maxDuration = duration;
588   action->m_suspended = 2;
589   lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 0.0);
590
591   XBT_OUT();
592   return action;
593 }
594
595 double LinkL07::getBandwidth()
596 {
597   return m_bwCurrent;
598 }
599
600 double LinkL07::getLatency()
601 {
602   return m_latCurrent;
603 }
604
605 bool LinkL07::isShared()
606 {
607   return lmm_constraint_is_shared(getConstraint());
608 }
609
610 /**********
611  * Action *
612  **********/
613
614 WorkstationL07Action::~WorkstationL07Action(){
615   free(p_workstationList);
616   free(p_communicationAmount);
617   free(p_computationAmount);
618 }
619
620 void WorkstationL07Action::updateBound()
621 {
622   double lat_current = 0.0;
623   double lat_bound = -1.0;
624   int i, j;
625
626   for (i = 0; i < m_workstationNb; i++) {
627     for (j = 0; j < m_workstationNb; j++) {
628       xbt_dynar_t route=NULL;
629
630       if (p_communicationAmount[i * m_workstationNb + j] > 0) {
631         double lat = 0.0;
632         routing_platf->getRouteAndLatency(dynamic_cast<WorkstationL07Ptr>(
633                                            static_cast<ResourcePtr>(((void**)p_workstationList)[i]))->p_netElm,
634                                           dynamic_cast<WorkstationL07Ptr>(
635                                            static_cast<ResourcePtr>(((void**)p_workstationList)[j]))->p_netElm,
636                                                           &route, &lat);
637
638         lat_current = MAX(lat_current, lat * p_communicationAmount[i * m_workstationNb + j]);
639       }
640     }
641   }
642   lat_bound = sg_tcp_gamma / (2.0 * lat_current);
643   XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
644   if ((m_latency == 0.0) && (m_suspended == 0)) {
645     if (m_rate < 0)
646       lmm_update_variable_bound(ptask_maxmin_system, getVariable(), lat_bound);
647     else
648       lmm_update_variable_bound(ptask_maxmin_system, getVariable(), min(m_rate, lat_bound));
649   }
650 }
651
652 int WorkstationL07Action::unref()
653 {
654   m_refcount--;
655   if (!m_refcount) {
656     if (actionHook::is_linked())
657           p_stateSet->erase(p_stateSet->iterator_to(*this));
658     if (getVariable())
659       lmm_variable_free(ptask_maxmin_system, getVariable());
660     delete this;
661     return 1;
662   }
663   return 0;
664 }
665
666 void WorkstationL07Action::cancel()
667 {
668   setState(SURF_ACTION_FAILED);
669   return;
670 }
671
672 void WorkstationL07Action::suspend()
673 {
674   XBT_IN("(%p))", this);
675   if (m_suspended != 2) {
676     m_suspended = 1;
677     lmm_update_variable_weight(ptask_maxmin_system, getVariable(), 0.0);
678   }
679   XBT_OUT();
680 }
681
682 void WorkstationL07Action::resume()
683 {
684   XBT_IN("(%p)", this);
685   if (m_suspended != 2) {
686     lmm_update_variable_weight(ptask_maxmin_system, getVariable(), 1.0);
687     m_suspended = 0;
688   }
689   XBT_OUT();
690 }
691
692 bool WorkstationL07Action::isSuspended()
693 {
694   return m_suspended == 1;
695 }
696
697 void WorkstationL07Action::setMaxDuration(double duration)
698 {                               /* FIXME: should inherit */
699   XBT_IN("(%p,%g)", this, duration);
700   m_maxDuration = duration;
701   XBT_OUT();
702 }
703
704 void WorkstationL07Action::setPriority(double priority)
705 {                               /* FIXME: should inherit */
706   XBT_IN("(%p,%g)", this, priority);
707   m_priority = priority;
708   XBT_OUT();
709 }
710
711 double WorkstationL07Action::getRemains()
712 {
713   XBT_IN("(%p)", this);
714   XBT_OUT();
715   return m_remains;
716 }
717
718 /*FIXME:remove static void ptask_finalize(void)
719 {
720   xbt_dict_free(&ptask_parallel_task_link_set);
721
722   delete surf_workstation_model;
723   surf_workstation_model = NULL;
724   delete surf_network_model;
725   surf_network_model = NULL;
726
727   ptask_host_count = 0;
728
729   if (ptask_maxmin_system) {
730     lmm_system_free(ptask_maxmin_system);
731     ptask_maxmin_system = NULL;
732   }
733   }*/
734
735 /**************************************/
736 /******* Resource Private    **********/
737 /**************************************/
738
739 /**************************************/
740 /*** Resource Creation & Destruction **/
741 /**************************************/
742
743 static void ptask_parse_workstation_init(sg_platf_host_cbarg_t host)
744 {
745   double power_peak = xbt_dynar_get_as(host->power_peak, host->pstate, double);
746   //cpu->power_peak = power_peak;
747   xbt_dynar_free(&(host->power_peak));  /* kill memory leak */
748   static_cast<WorkstationL07ModelPtr>(surf_workstation_model)->createResource(
749       host->id,
750       power_peak,
751       host->power_scale,
752       host->power_trace,
753       host->initial_state,
754       host->state_trace,
755       host->properties);
756 }
757
758 static void ptask_parse_cpu_init(sg_platf_host_cbarg_t host)
759 {
760   double power_peak = xbt_dynar_get_as(host->power_peak, host->pstate, double);
761   static_cast<CpuL07ModelPtr>(surf_cpu_model_pm)->createResource(
762       host->id,
763       power_peak,
764       host->power_scale,
765       host->power_trace,
766       host->initial_state,
767       host->state_trace,
768       host->properties);
769 }
770
771
772
773 static void ptask_parse_link_init(sg_platf_link_cbarg_t link)
774 {
775   if (link->policy == SURF_LINK_FULLDUPLEX) {
776     char *link_id;
777     link_id = bprintf("%s_UP", link->id);
778     static_cast<NetworkL07ModelPtr>(surf_network_model)->createResource(link_id,
779                                link->bandwidth,
780                                link->bandwidth_trace,
781                                link->latency,
782                                link->latency_trace,
783                                link->state,
784                                link->state_trace,
785                                link->policy,
786                                link->properties);
787     xbt_free(link_id);
788     link_id = bprintf("%s_DOWN", link->id);
789     static_cast<NetworkL07ModelPtr>(surf_network_model)->createResource(link_id,
790                                link->bandwidth,
791                                link->bandwidth_trace,
792                                link->latency,
793                                link->latency_trace,
794                                link->state,
795                                link->state_trace,
796                                link->policy,
797                                NULL); /* FIXME: We need to deep copy the
798                                        * properties or we won't be able to free
799                                        * it */
800     xbt_free(link_id);
801   } else {
802           static_cast<NetworkL07ModelPtr>(surf_network_model)->createResource(link->id,
803                                link->bandwidth,
804                                link->bandwidth_trace,
805                                link->latency,
806                                link->latency_trace,
807                                link->state,
808                                link->state_trace,
809                                link->policy,
810                                link->properties);
811   }
812
813   current_property_set = NULL;
814 }
815
816 static void ptask_add_traces(){
817   static_cast<WorkstationL07ModelPtr>(surf_workstation_model)->addTraces();
818 }
819
820 static void ptask_define_callbacks()
821 {
822   sg_platf_host_add_cb(ptask_parse_cpu_init);
823   sg_platf_host_add_cb(ptask_parse_workstation_init);
824   sg_platf_link_add_cb(ptask_parse_link_init);
825   sg_platf_postparse_add_cb(ptask_add_traces);
826 }
827
828 /**************************************/
829 /*************** Generic **************/
830 /**************************************/
831 void surf_workstation_model_init_ptask_L07(void)
832 {
833   XBT_INFO("surf_workstation_model_init_ptask_L07");
834   xbt_assert(!surf_cpu_model_pm, "CPU model type already defined");
835   xbt_assert(!surf_network_model, "network model type already defined");
836   ptask_define_callbacks();
837   surf_workstation_model = new WorkstationL07Model();
838   ModelPtr model = static_cast<ModelPtr>(surf_workstation_model);
839   xbt_dynar_push(model_list, &model);
840   xbt_dynar_push(model_list_invoke, &model);
841 }