Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c93b29da9d421d3360e53e396e2797afaa2a88cf
[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   WorkstationL07ActionLmmPtr 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<WorkstationL07ActionLmmPtr>(&*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   WorkstationL07ActionLmmPtr 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<WorkstationL07ActionLmmPtr>(&*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<WorkstationLmmPtr>(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   WorkstationL07ActionLmmPtr 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 WorkstationL07ActionLmm(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<CpuLmmPtr>(dynamic_cast<WorkstationL07Ptr>(
215                                            static_cast<ResourcePtr>(workstation_list[i]))->p_cpu)->constraint(),
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->constraint(),
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   : Resource(model, name, props),
414     Workstation(NULL, netElm, cpu),
415     WorkstationLmm()
416 {
417 }
418
419 double WorkstationL07::getPowerPeakAt(int /*pstate_index*/)
420 {
421         XBT_DEBUG("[ws_get_power_peak_at] Not implemented for workstation_ptask_L07");
422         return 0.0;
423 }
424
425 int WorkstationL07::getNbPstates()
426 {
427         XBT_DEBUG("[ws_get_nb_pstates] Not implemented for workstation_ptask_L07");
428         return 0.0;
429 }
430
431 void WorkstationL07::setPowerPeakAt(int /*pstate_index*/)
432 {
433         XBT_DEBUG("[ws_set_power_peak_at] Not implemented for workstation_ptask_L07");
434 }
435
436 double WorkstationL07::getConsumedEnergy()
437 {
438         XBT_DEBUG("[ws_get_consumed_energy] Not implemented for workstation_ptask_L07");
439         return 0.0;
440 }
441
442 CpuL07::CpuL07(CpuL07ModelPtr model, const char* name, xbt_dict_t props,
443                    double power_scale,
444                        double power_initial, tmgr_trace_t power_trace,
445                        e_surf_resource_state_t state_initial, tmgr_trace_t state_trace)
446  : Resource(model, name, props)
447  , CpuLmm(lmm_constraint_new(ptask_maxmin_system, this, power_initial * power_scale))
448 {
449   p_power.scale = power_scale;
450   xbt_assert(p_power.scale > 0, "Power has to be >0");
451
452   m_powerCurrent = power_initial;
453   if (power_trace)
454         p_power.event = tmgr_history_add_trace(history, power_trace, 0.0, 0, static_cast<ResourcePtr>(this));
455
456   m_stateCurrent = state_initial;
457   if (state_trace)
458         p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, static_cast<ResourcePtr>(this));
459 }
460
461 LinkL07::LinkL07(NetworkL07ModelPtr model, const char* name, xbt_dict_t props,
462                          double bw_initial,
463                          tmgr_trace_t bw_trace,
464                          double lat_initial,
465                          tmgr_trace_t lat_trace,
466                          e_surf_resource_state_t state_initial,
467                          tmgr_trace_t state_trace,
468                          e_surf_link_sharing_policy_t policy)
469  : Resource(model, name, props)
470  , NetworkLinkLmm(lmm_constraint_new(ptask_maxmin_system, this, bw_initial), history, state_trace)
471 {
472   m_bwCurrent = bw_initial;
473   if (bw_trace)
474     p_bwEvent = tmgr_history_add_trace(history, bw_trace, 0.0, 0, static_cast<ResourcePtr>(this));
475
476   m_stateCurrent = state_initial;
477   m_latCurrent = lat_initial;
478
479   if (lat_trace)
480         p_latEvent = tmgr_history_add_trace(history, lat_trace, 0.0, 0, static_cast<ResourcePtr>(this));
481
482   if (policy == SURF_LINK_FATPIPE)
483         lmm_constraint_shared(constraint());
484 }
485
486 bool CpuL07::isUsed(){
487   return lmm_constraint_used(ptask_maxmin_system, constraint());
488 }
489
490 bool LinkL07::isUsed(){
491   return lmm_constraint_used(ptask_maxmin_system, constraint());
492 }
493
494 void CpuL07::updateState(tmgr_trace_event_t event_type, double value, double /*date*/){
495   XBT_DEBUG("Updating cpu %s (%p) with value %g", getName(), this, value);
496   if (event_type == p_power.event) {
497         m_powerCurrent = value;
498     lmm_update_constraint_bound(ptask_maxmin_system, constraint(), m_powerCurrent * p_power.scale);
499     if (tmgr_trace_event_free(event_type))
500       p_power.event = NULL;
501   } else if (event_type == p_stateEvent) {
502     if (value > 0)
503       m_stateCurrent = SURF_RESOURCE_ON;
504     else
505       m_stateCurrent = SURF_RESOURCE_OFF;
506     if (tmgr_trace_event_free(event_type))
507       p_stateEvent = NULL;
508   } else {
509     XBT_CRITICAL("Unknown event ! \n");
510     xbt_abort();
511   }
512   return;
513 }
514
515 void LinkL07::updateState(tmgr_trace_event_t event_type, double value, double date){
516   XBT_DEBUG("Updating link %s (%p) with value=%f for date=%g", getName(), this, value, date);
517   if (event_type == p_bwEvent) {
518     m_bwCurrent = value;
519     lmm_update_constraint_bound(ptask_maxmin_system, constraint(), m_bwCurrent);
520     if (tmgr_trace_event_free(event_type))
521       p_bwEvent = NULL;
522   } else if (event_type == p_latEvent) {
523     lmm_variable_t var = NULL;
524     WorkstationL07ActionLmmPtr action;
525     lmm_element_t elem = NULL;
526
527     m_latCurrent = value;
528     while ((var = lmm_get_var_from_cnst(ptask_maxmin_system, constraint(), &elem))) {
529       action = (WorkstationL07ActionLmmPtr) lmm_variable_id(var);
530       action->updateBound();
531     }
532     if (tmgr_trace_event_free(event_type))
533       p_latEvent = NULL;
534   } else if (event_type == p_stateEvent) {
535     if (value > 0)
536       m_stateCurrent = SURF_RESOURCE_ON;
537     else
538       m_stateCurrent = SURF_RESOURCE_OFF;
539     if (tmgr_trace_event_free(event_type))
540       p_stateEvent = NULL;
541   } else {
542     XBT_CRITICAL("Unknown event ! \n");
543     xbt_abort();
544   }
545   return;
546 }
547
548 e_surf_resource_state_t WorkstationL07::getState()
549 {
550   return p_cpu->getState();
551 }
552
553 e_surf_resource_state_t CpuL07::getState()
554 {
555   return m_stateCurrent;
556 }
557
558 double CpuL07::getSpeed(double load)
559 {
560   return load * p_power.scale;
561 }
562
563 double CpuL07::getAvailableSpeed()
564 {
565   return m_powerCurrent;
566 }
567
568 ActionPtr WorkstationL07::execute(double size)
569 {
570   void **workstation_list = xbt_new0(void *, 1);
571   double *computation_amount = xbt_new0(double, 1);
572   double *communication_amount = xbt_new0(double, 1);
573
574   workstation_list[0] = static_cast<ResourcePtr>(this);
575   communication_amount[0] = 0.0;
576   computation_amount[0] = size;
577
578   return static_cast<WorkstationL07ModelPtr>(getModel())->executeParallelTask(1, workstation_list,
579                                               computation_amount,
580                                      communication_amount, -1);
581 }
582
583 ActionPtr WorkstationL07::sleep(double duration)
584 {
585   WorkstationL07ActionLmmPtr action = NULL;
586
587   XBT_IN("(%s,%g)", getName(), duration);
588
589   action = dynamic_cast<WorkstationL07ActionLmmPtr>(execute(1.0));
590   action->m_maxDuration = duration;
591   action->m_suspended = 2;
592   lmm_update_variable_weight(ptask_maxmin_system, action->getVariable(), 0.0);
593
594   XBT_OUT();
595   return action;
596 }
597
598 double LinkL07::getBandwidth()
599 {
600   return m_bwCurrent;
601 }
602
603 double LinkL07::getLatency()
604 {
605   return m_latCurrent;
606 }
607
608 bool LinkL07::isShared()
609 {
610   return lmm_constraint_is_shared(constraint());
611 }
612
613 /**********
614  * Action *
615  **********/
616
617 WorkstationL07ActionLmm::~WorkstationL07ActionLmm(){
618   free(p_workstationList);
619   free(p_communicationAmount);
620   free(p_computationAmount);
621 }
622
623 void WorkstationL07ActionLmm::updateBound()
624 {
625   double lat_current = 0.0;
626   double lat_bound = -1.0;
627   int i, j;
628
629   for (i = 0; i < m_workstationNb; i++) {
630     for (j = 0; j < m_workstationNb; j++) {
631       xbt_dynar_t route=NULL;
632
633       if (p_communicationAmount[i * m_workstationNb + j] > 0) {
634         double lat = 0.0;
635         routing_platf->getRouteAndLatency(dynamic_cast<WorkstationL07Ptr>(
636                                            static_cast<ResourcePtr>(((void**)p_workstationList)[i]))->p_netElm,
637                                           dynamic_cast<WorkstationL07Ptr>(
638                                            static_cast<ResourcePtr>(((void**)p_workstationList)[j]))->p_netElm,
639                                                           &route, &lat);
640
641         lat_current = MAX(lat_current, lat * p_communicationAmount[i * m_workstationNb + j]);
642       }
643     }
644   }
645   lat_bound = sg_tcp_gamma / (2.0 * lat_current);
646   XBT_DEBUG("action (%p) : lat_bound = %g", this, lat_bound);
647   if ((m_latency == 0.0) && (m_suspended == 0)) {
648     if (m_rate < 0)
649       lmm_update_variable_bound(ptask_maxmin_system, getVariable(), lat_bound);
650     else
651       lmm_update_variable_bound(ptask_maxmin_system, getVariable(), min(m_rate, lat_bound));
652   }
653 }
654
655 int WorkstationL07ActionLmm::unref()
656 {
657   m_refcount--;
658   if (!m_refcount) {
659     if (actionHook::is_linked())
660           p_stateSet->erase(p_stateSet->iterator_to(*this));
661     if (getVariable())
662       lmm_variable_free(ptask_maxmin_system, getVariable());
663     delete this;
664     return 1;
665   }
666   return 0;
667 }
668
669 void WorkstationL07ActionLmm::cancel()
670 {
671   setState(SURF_ACTION_FAILED);
672   return;
673 }
674
675 void WorkstationL07ActionLmm::suspend()
676 {
677   XBT_IN("(%p))", this);
678   if (m_suspended != 2) {
679     m_suspended = 1;
680     lmm_update_variable_weight(ptask_maxmin_system, getVariable(), 0.0);
681   }
682   XBT_OUT();
683 }
684
685 void WorkstationL07ActionLmm::resume()
686 {
687   XBT_IN("(%p)", this);
688   if (m_suspended != 2) {
689     lmm_update_variable_weight(ptask_maxmin_system, getVariable(), 1.0);
690     m_suspended = 0;
691   }
692   XBT_OUT();
693 }
694
695 bool WorkstationL07ActionLmm::isSuspended()
696 {
697   return m_suspended == 1;
698 }
699
700 void WorkstationL07ActionLmm::setMaxDuration(double duration)
701 {                               /* FIXME: should inherit */
702   XBT_IN("(%p,%g)", this, duration);
703   m_maxDuration = duration;
704   XBT_OUT();
705 }
706
707 void WorkstationL07ActionLmm::setPriority(double priority)
708 {                               /* FIXME: should inherit */
709   XBT_IN("(%p,%g)", this, priority);
710   m_priority = priority;
711   XBT_OUT();
712 }
713
714 double WorkstationL07ActionLmm::getRemains()
715 {
716   XBT_IN("(%p)", this);
717   XBT_OUT();
718   return m_remains;
719 }
720
721 /*FIXME:remove static void ptask_finalize(void)
722 {
723   xbt_dict_free(&ptask_parallel_task_link_set);
724
725   delete surf_workstation_model;
726   surf_workstation_model = NULL;
727   delete surf_network_model;
728   surf_network_model = NULL;
729
730   ptask_host_count = 0;
731
732   if (ptask_maxmin_system) {
733     lmm_system_free(ptask_maxmin_system);
734     ptask_maxmin_system = NULL;
735   }
736   }*/
737
738 /**************************************/
739 /******* Resource Private    **********/
740 /**************************************/
741
742 /**************************************/
743 /*** Resource Creation & Destruction **/
744 /**************************************/
745
746 static void ptask_parse_workstation_init(sg_platf_host_cbarg_t host)
747 {
748   double power_peak = xbt_dynar_get_as(host->power_peak, host->pstate, double);
749   //cpu->power_peak = power_peak;
750   xbt_dynar_free(&(host->power_peak));  /* kill memory leak */
751   static_cast<WorkstationL07ModelPtr>(surf_workstation_model)->createResource(
752       host->id,
753       power_peak,
754       host->power_scale,
755       host->power_trace,
756       host->initial_state,
757       host->state_trace,
758       host->properties);
759 }
760
761 static void ptask_parse_cpu_init(sg_platf_host_cbarg_t host)
762 {
763   double power_peak = xbt_dynar_get_as(host->power_peak, host->pstate, double);
764   static_cast<CpuL07ModelPtr>(surf_cpu_model_pm)->createResource(
765       host->id,
766       power_peak,
767       host->power_scale,
768       host->power_trace,
769       host->initial_state,
770       host->state_trace,
771       host->properties);
772 }
773
774
775
776 static void ptask_parse_link_init(sg_platf_link_cbarg_t link)
777 {
778   if (link->policy == SURF_LINK_FULLDUPLEX) {
779     char *link_id;
780     link_id = bprintf("%s_UP", link->id);
781     static_cast<NetworkL07ModelPtr>(surf_network_model)->createResource(link_id,
782                                link->bandwidth,
783                                link->bandwidth_trace,
784                                link->latency,
785                                link->latency_trace,
786                                link->state,
787                                link->state_trace,
788                                link->policy,
789                                link->properties);
790     xbt_free(link_id);
791     link_id = bprintf("%s_DOWN", link->id);
792     static_cast<NetworkL07ModelPtr>(surf_network_model)->createResource(link_id,
793                                link->bandwidth,
794                                link->bandwidth_trace,
795                                link->latency,
796                                link->latency_trace,
797                                link->state,
798                                link->state_trace,
799                                link->policy,
800                                NULL); /* FIXME: We need to deep copy the
801                                        * properties or we won't be able to free
802                                        * it */
803     xbt_free(link_id);
804   } else {
805           static_cast<NetworkL07ModelPtr>(surf_network_model)->createResource(link->id,
806                                link->bandwidth,
807                                link->bandwidth_trace,
808                                link->latency,
809                                link->latency_trace,
810                                link->state,
811                                link->state_trace,
812                                link->policy,
813                                link->properties);
814   }
815
816   current_property_set = NULL;
817 }
818
819 static void ptask_add_traces(){
820   static_cast<WorkstationL07ModelPtr>(surf_workstation_model)->addTraces();
821 }
822
823 static void ptask_define_callbacks()
824 {
825   sg_platf_host_add_cb(ptask_parse_cpu_init);
826   sg_platf_host_add_cb(ptask_parse_workstation_init);
827   sg_platf_link_add_cb(ptask_parse_link_init);
828   sg_platf_postparse_add_cb(ptask_add_traces);
829 }
830
831 /**************************************/
832 /*************** Generic **************/
833 /**************************************/
834 void surf_workstation_model_init_ptask_L07(void)
835 {
836   XBT_INFO("surf_workstation_model_init_ptask_L07");
837   xbt_assert(!surf_cpu_model_pm, "CPU model type already defined");
838   xbt_assert(!surf_network_model, "network model type already defined");
839   ptask_define_callbacks();
840   surf_workstation_model = new WorkstationL07Model();
841   ModelPtr model = static_cast<ModelPtr>(surf_workstation_model);
842   xbt_dynar_push(model_list, &model);
843   xbt_dynar_push(model_list_invoke, &model);
844 }