Logo AND Algorithmique Numérique Distribuée

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