Logo AND Algorithmique Numérique Distribuée

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