Logo AND Algorithmique Numérique Distribuée

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