Logo AND Algorithmique Numérique Distribuée

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