Logo AND Algorithmique Numérique Distribuée

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