Logo AND Algorithmique Numérique Distribuée

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