Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ptask_L07 can now do execute and communicate via execute_parallel_task
[simgrid.git] / src / surf / maxmin.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "xbt/mallocator.h"
12 #include "maxmin_private.h"
13 #include <stdlib.h>
14 #include <math.h>
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_maxmin, surf,
16                                 "Logging specific to SURF (maxmin)");
17
18 static void* lmm_variable_mallocator_new_f(void);
19 static void lmm_variable_mallocator_free_f(void *var);
20 static void lmm_variable_mallocator_reset_f(void *var);
21
22 lmm_system_t lmm_system_new(void)
23 {
24   lmm_system_t l = NULL;
25   s_lmm_variable_t var;
26   s_lmm_constraint_t cnst;
27
28   l = xbt_new0(s_lmm_system_t, 1);
29
30   l->modified = 0;
31   xbt_swag_init(&(l->variable_set),
32                 xbt_swag_offset(var, variable_set_hookup));
33   xbt_swag_init(&(l->constraint_set),
34                 xbt_swag_offset(cnst, constraint_set_hookup));
35
36   xbt_swag_init(&(l->active_constraint_set),
37                 xbt_swag_offset(cnst, active_constraint_set_hookup));
38
39   xbt_swag_init(&(l->saturated_variable_set),
40                 xbt_swag_offset(var, saturated_variable_set_hookup));
41   xbt_swag_init(&(l->saturated_constraint_set),
42                 xbt_swag_offset(cnst, saturated_constraint_set_hookup));
43
44   l->variable_mallocator = xbt_mallocator_new(64,
45                                               lmm_variable_mallocator_new_f,
46                                               lmm_variable_mallocator_free_f,
47                                               lmm_variable_mallocator_reset_f);
48
49   return l;
50 }
51
52 void lmm_system_free(lmm_system_t sys)
53 {
54   lmm_variable_t var = NULL;
55   lmm_constraint_t cnst = NULL;
56
57   while ((var = extract_variable(sys)))
58     lmm_var_free(sys, var);
59
60   while ((cnst = extract_constraint(sys)))
61     lmm_cnst_free(sys, cnst);
62
63   xbt_mallocator_free(sys->variable_mallocator);
64   free(sys);
65 }
66
67 void lmm_variable_disable(lmm_system_t sys, lmm_variable_t var)
68 {
69   int i;
70   lmm_element_t elem = NULL;
71
72   XBT_IN2("(sys=%p, var=%p)",sys,var);
73   sys->modified = 1;
74
75   for (i = 0; i < var->cnsts_number; i++) {
76     elem = &var->cnsts[i];
77     xbt_swag_remove(elem, &(elem->constraint->element_set));
78     xbt_swag_remove(elem, &(elem->constraint->active_element_set));
79     if (!xbt_swag_size(&(elem->constraint->element_set)))
80       make_constraint_inactive(sys, elem->constraint);
81   }
82   var->cnsts_number = 0;
83   XBT_OUT;
84 }
85
86 static void lmm_var_free(lmm_system_t sys, lmm_variable_t var)
87 {
88
89   lmm_variable_disable(sys, var);
90   free(var->cnsts);
91   xbt_mallocator_release(sys->variable_mallocator, var);
92 }
93
94 static void lmm_cnst_free(lmm_system_t sys, lmm_constraint_t cnst)
95 {
96 /*   xbt_assert0(xbt_swag_size(&(cnst->element_set)), */
97 /*            "This list should be empty!"); */
98   remove_active_constraint(sys, cnst);
99   free(cnst);
100 }
101
102 lmm_constraint_t lmm_constraint_new(lmm_system_t sys, void *id,
103                                     double bound_value)
104 {
105   lmm_constraint_t cnst = NULL;
106   s_lmm_element_t elem;
107
108   cnst = xbt_new0(s_lmm_constraint_t, 1);
109   cnst->id = id;
110   xbt_swag_init(&(cnst->element_set),
111                 xbt_swag_offset(elem, element_set_hookup));
112   xbt_swag_init(&(cnst->active_element_set),
113                 xbt_swag_offset(elem, active_element_set_hookup));
114
115   cnst->bound = bound_value;
116   cnst->usage = 0;
117   cnst->shared = 1;
118   insert_constraint(sys, cnst);
119
120   return cnst;
121 }
122
123 void lmm_constraint_shared(lmm_constraint_t cnst)
124 {
125   cnst->shared = 0;
126 }
127
128 void lmm_constraint_free(lmm_system_t sys, lmm_constraint_t cnst)
129 {
130   remove_constraint(sys, cnst);
131   lmm_cnst_free(sys, cnst);
132 }
133
134 static void* lmm_variable_mallocator_new_f(void) {
135   return xbt_new(s_lmm_variable_t, 1);
136 }
137
138 static void lmm_variable_mallocator_free_f(void *var) {
139   xbt_free(var);
140 }
141
142 static void lmm_variable_mallocator_reset_f(void *var) {
143   /* memset to zero like calloc */
144   memset(var, 0, sizeof(s_lmm_variable_t));
145 }
146
147 lmm_variable_t lmm_variable_new(lmm_system_t sys, void *id,
148                                 double weight,
149                                 double bound, int number_of_constraints)
150 {
151   lmm_variable_t var = NULL;
152   int i;
153
154   XBT_IN5("(sys=%p, id=%p, weight=%f, bound=%f, num_cons =%d)",
155           sys,id,weight,bound,number_of_constraints);
156
157   var = xbt_mallocator_get(sys->variable_mallocator);
158   var->id = id;
159   var->cnsts = xbt_new0(s_lmm_element_t, number_of_constraints);
160   for(i=0; i<number_of_constraints; i++) {
161     /* Should be useless because of the 
162        calloc but it seems to help valgrind... */
163     var->cnsts[i].element_set_hookup.next = NULL;
164     var->cnsts[i].element_set_hookup.prev = NULL;
165     var->cnsts[i].active_element_set_hookup.next = NULL;
166     var->cnsts[i].active_element_set_hookup.prev = NULL;
167     var->cnsts[i].constraint = NULL;
168     var->cnsts[i].variable = NULL;
169     var->cnsts[i].value = 0.0;
170   }
171   var->cnsts_size = number_of_constraints;
172   var->cnsts_number = 0; /* Should be useless because of the 
173                             calloc but it seems to help valgrind... */
174   var->weight = weight;
175   var->bound = bound;
176   var->value = 0.0;
177   var->df    = 0.0;
178
179   var->func_f  = func_f_def;
180   var->func_fp  = func_fp_def;
181   var->func_fpi  = func_fpi_def;
182
183   if(weight) xbt_swag_insert_at_head(var,&(sys->variable_set));
184   else xbt_swag_insert_at_tail(var,&(sys->variable_set));
185   XBT_OUT;
186   return var;
187 }
188
189 void lmm_variable_free(lmm_system_t sys, lmm_variable_t var)
190 {
191   remove_variable(sys, var);
192   lmm_var_free(sys, var);
193 }
194
195 double lmm_variable_getvalue(lmm_variable_t var)
196 {
197   return (var->value);
198 }
199
200 void lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
201                 lmm_variable_t var, double value)
202 {
203   lmm_element_t elem = NULL;
204
205   sys->modified = 1;
206
207   xbt_assert0(var->cnsts_number < var->cnsts_size,
208               "Too much constraints");
209
210   elem = &(var->cnsts[var->cnsts_number++]);
211
212   elem->value = value;
213   elem->constraint = cnst;
214   elem->variable = var;
215
216   if(var->weight) xbt_swag_insert_at_head(elem,&(elem->constraint->element_set));
217   else xbt_swag_insert_at_tail(elem,&(elem->constraint->element_set));
218
219   make_constraint_active(sys, cnst);
220 }
221
222 void lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst,
223                     lmm_variable_t var, double value)
224 {
225   int i ; 
226   sys->modified = 1;
227
228   for(i=0; i< var->cnsts_number ; i++)
229     if(var->cnsts[i].constraint == cnst) break;
230   
231   if(i<var->cnsts_number) var->cnsts[i].value +=value;
232   else lmm_expand(sys,cnst,var,value);
233 }
234
235 void lmm_elem_set_value(lmm_system_t sys, lmm_constraint_t cnst,
236                         lmm_variable_t var, double value)
237 {
238   int i ; 
239
240   for(i=0; i< var->cnsts_number ; i++)
241     if(var->cnsts[i].constraint == cnst) break;
242
243   if(i<var->cnsts_number) {
244     var->cnsts[i].value =value;
245     sys->modified = 1;
246   } else DIE_IMPOSSIBLE;
247 }
248
249 lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t sys,
250                                        lmm_variable_t var, int num)
251 {
252   if (num < var->cnsts_number)
253     return (var->cnsts[num].constraint);
254   else
255     return NULL;
256 }
257
258 int lmm_get_number_of_cnst_from_var(lmm_system_t sys, lmm_variable_t var)
259 {
260   return (var->cnsts_number);
261 }
262
263 lmm_variable_t lmm_get_var_from_cnst(lmm_system_t sys,
264                                      lmm_constraint_t cnst,
265                                      lmm_variable_t * var)
266 {
267   if (!(*var))
268     xbt_swag_getFirst(&(cnst->element_set));
269   else
270     *var = xbt_swag_getNext(*var, cnst->element_set.offset);
271   return *var;
272 }
273
274 void *lmm_constraint_id(lmm_constraint_t cnst)
275 {
276   return cnst->id;
277 }
278
279 void *lmm_variable_id(lmm_variable_t var)
280 {
281   return var->id;
282 }
283
284 static void saturated_constraint_set_update(lmm_system_t sys,
285                                             lmm_constraint_t cnst,
286                                             double *min_usage)
287 {
288   lmm_constraint_t useless_cnst = NULL;
289
290   XBT_IN3("sys=%p, cnst=%p, min_usage=%f",sys,cnst,*min_usage);
291   if (cnst->usage <= 0) {
292     XBT_OUT;
293     return;
294   }
295   if (cnst->remaining <= 0) {
296     XBT_OUT;
297     return;
298   }
299   if ((*min_usage < 0) || (*min_usage > cnst->remaining / cnst->usage)) {
300     *min_usage = cnst->remaining / cnst->usage;
301     LOG3(xbt_log_priority_trace, 
302          "min_usage=%f (cnst->remaining=%f, cnst->usage=%f)",*min_usage, 
303          cnst->remaining, cnst->usage);
304     while ((useless_cnst =
305             xbt_swag_getFirst(&(sys->saturated_constraint_set))))
306       xbt_swag_remove(useless_cnst, &(sys->saturated_constraint_set));
307
308     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
309   } else if (*min_usage == cnst->remaining / cnst->usage) {
310     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
311   }
312   XBT_OUT;
313 }
314
315 static void saturated_variable_set_update(lmm_system_t sys)
316 {
317   lmm_constraint_t cnst = NULL;
318   xbt_swag_t cnst_list = NULL;
319   lmm_element_t elem = NULL;
320   xbt_swag_t elem_list = NULL;
321
322   cnst_list = &(sys->saturated_constraint_set);
323   while ((cnst = xbt_swag_getFirst(cnst_list))) {
324     elem_list = &(cnst->active_element_set);
325     xbt_swag_foreach(elem, elem_list) {
326       if(elem->variable->weight<=0) break;
327       if ((elem->value > 0))
328         xbt_swag_insert(elem->variable, &(sys->saturated_variable_set));
329     }
330     xbt_swag_remove(cnst, cnst_list);
331   }
332 }
333
334 void lmm_print(lmm_system_t sys)
335
336 {
337   lmm_constraint_t cnst = NULL;
338   lmm_element_t elem = NULL;
339   lmm_variable_t var = NULL;
340   xbt_swag_t cnst_list = NULL;
341   xbt_swag_t var_list = NULL;
342   xbt_swag_t elem_list = NULL;
343   char print_buf[1024];
344   char *trace_buf=xbt_malloc0(sizeof(char));
345   double sum=0.0;
346
347   /* Printing Objective */
348   var_list = &(sys->variable_set);
349   sprintf(print_buf,"MAX-MIN ( ");
350   trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
351   strcat(trace_buf, print_buf);
352   xbt_swag_foreach(var, var_list) {
353     sprintf(print_buf,"'%p'(%f) ",var,var->weight);
354     trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
355     strcat(trace_buf, print_buf);
356   }
357   sprintf(print_buf,")");
358   trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
359   strcat(trace_buf, print_buf);
360   DEBUG1("%s",trace_buf);
361   trace_buf[0]='\000';
362
363   /* Printing Constraints */
364   cnst_list = &(sys->active_constraint_set);
365   xbt_swag_foreach(cnst, cnst_list) {
366     sum=0.0;
367     elem_list = &(cnst->element_set);
368     sprintf(print_buf,"\t");
369     trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
370     strcat(trace_buf, print_buf);
371     xbt_swag_foreach(elem, elem_list) {
372       sprintf(print_buf,"%f.'%p'(%f) + ",elem->value, 
373               elem->variable,elem->variable->value);
374       trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
375       strcat(trace_buf, print_buf);
376       sum += elem->value * elem->variable->value;
377     }
378     sprintf(print_buf,"0 <= %f ('%p')",cnst->bound,cnst);
379     trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
380     strcat(trace_buf, print_buf);
381
382     if(!cnst->shared) {
383       sprintf(print_buf," [MAX-Constraint]");
384       trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
385       strcat(trace_buf, print_buf);
386     }
387     DEBUG1("%s",trace_buf);
388     trace_buf[0]='\000';
389     if(double_positive(sum-cnst->bound))
390       WARN3("Incorrect value (%f is not smaller than %f): %g",
391                sum,cnst->bound,sum-cnst->bound);
392   }
393
394   /* Printing Result */
395   xbt_swag_foreach(var, var_list) {
396     if(var->bound>0) {
397       DEBUG4("'%p'(%f) : %f (<=%f)",var,var->weight,var->value, var->bound);
398       if(double_positive(var->value-var->bound))
399          WARN2("Incorrect value (%f is not smaller than %f",
400                   var->value, var->bound);
401     }
402     else 
403       DEBUG3("'%p'(%f) : %f",var,var->weight,var->value);
404   }
405
406   free(trace_buf);
407 }
408
409 void lmm_solve(lmm_system_t sys)
410 {
411   lmm_variable_t var = NULL;
412   lmm_constraint_t cnst = NULL;
413   lmm_element_t elem = NULL;
414   xbt_swag_t cnst_list = NULL;
415   xbt_swag_t var_list = NULL;
416   xbt_swag_t elem_list = NULL;
417   double min_usage = -1;
418
419   if (!(sys->modified))
420     return;
421
422   /* Init */
423   var_list = &(sys->variable_set);
424   DEBUG1("Variable set : %d", xbt_swag_size(var_list));
425   xbt_swag_foreach(var, var_list) {
426     var->value = 0.0;
427   }
428
429   /* 
430    * Compute Usage and store the variables that reach the maximum.
431    */
432   cnst_list = &(sys->active_constraint_set);
433   DEBUG1("Active constraints : %d", xbt_swag_size(cnst_list));
434   xbt_swag_foreach(cnst, cnst_list) {
435     /* INIT */
436     cnst->remaining = cnst->bound;
437     cnst->usage = 0;
438     elem_list = &(cnst->element_set);
439     cnst->usage = 0.0;
440     xbt_swag_foreach(elem, elem_list) {
441       if(elem->variable->weight <=0) break;
442       if ((elem->value > 0)) {
443         if(cnst->shared)
444           cnst->usage += elem->value / elem->variable->weight;
445         else 
446           if(cnst->usage<elem->value / elem->variable->weight)
447             cnst->usage = elem->value / elem->variable->weight;
448         DEBUG2("Constraint Usage %p : %f",cnst,cnst->usage);
449         make_elem_active(elem);
450       }
451     }
452     /* Saturated constraints update */
453     saturated_constraint_set_update(sys, cnst, &min_usage);
454   }
455   saturated_variable_set_update(sys);
456
457   /* Saturated variables update */
458
459   do {
460     /* Fix the variables that have to be */
461     var_list = &(sys->saturated_variable_set);
462
463     xbt_swag_foreach(var, var_list) {
464       /* First check if some of these variables have reach their upper
465          bound and update min_usage accordingly. */
466       DEBUG5("var=%p, var->bound=%f, var->weight=%f, min_usage=%f, var->bound*var->weight=%f",
467              var, var->bound, var->weight, min_usage,var->bound * var->weight);
468       if ((var->bound > 0) && (var->bound * var->weight < min_usage)) {
469         min_usage = var->bound * var->weight;
470         DEBUG1("Updated min_usage=%f",min_usage);
471       }
472     }
473
474
475     while ((var = xbt_swag_getFirst(var_list))) {
476       int i;
477
478       var->value = min_usage / var->weight;
479       DEBUG5("Min usage: %f, Var(%p)->weight: %f, Var(%p)->value: %f ",min_usage,var,var->weight,var,var->value);
480
481
482       /* Update usage */
483
484       for (i = 0; i < var->cnsts_number; i++) {
485         elem = &var->cnsts[i];
486         cnst = elem->constraint;
487         if(cnst->shared) {
488           double_update(&(cnst->remaining), elem->value * var->value);
489           double_update(&(cnst->usage), elem->value / var->weight);
490           make_elem_inactive(elem);
491         } else { /* FIXME one day: We recompute usage.... :( */
492           cnst->usage = 0.0;
493           make_elem_inactive(elem);
494           xbt_swag_foreach(elem, elem_list) {
495             if(elem->variable->weight <=0) break;
496             if(elem->variable->value > 0) break;
497             if ((elem->value > 0)) {
498               if(cnst->usage<elem->value / elem->variable->weight)
499                 cnst->usage = elem->value / elem->variable->weight;
500               DEBUG2("Constraint Usage %p : %f",cnst,cnst->usage);
501               make_elem_active(elem);
502             }
503           } 
504         }
505       }
506       xbt_swag_remove(var, var_list);
507     }
508
509     /* Find out which variables reach the maximum */
510     cnst_list = &(sys->active_constraint_set);
511     min_usage = -1;
512     xbt_swag_foreach(cnst, cnst_list) {
513       saturated_constraint_set_update(sys, cnst, &min_usage);
514     }
515     saturated_variable_set_update(sys);
516
517   } while (xbt_swag_size(&(sys->saturated_variable_set)));
518
519   sys->modified = 0;
520   if(XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
521     lmm_print(sys);
522   }
523 }
524
525 /* Not a O(1) function */
526
527 void lmm_update(lmm_system_t sys, lmm_constraint_t cnst,
528                 lmm_variable_t var, double value)
529 {
530   int i;
531
532   sys->modified = 1;
533   for (i = 0; i < var->cnsts_number; i++)
534     if (var->cnsts[i].constraint == cnst) {
535       var->cnsts[i].value = value;
536       return;
537     }
538 }
539
540 /** \brief Attribute the value bound to var->bound.
541  * 
542  *  \param sys the lmm_system_t
543  *  \param var the lmm_variable_t
544  *  \param bound the new bound to associate with var
545  * 
546  *  Makes var->bound equal to bound. Whenever this function is called 
547  *  a change is  signed in the system. To
548  *  avoid false system changing detection it is a good idea to test 
549  *  (bound != 0) before calling it.
550  *
551  */
552 void lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
553                                double bound)
554 {
555   sys->modified = 1;
556   var->bound = bound;
557 }
558
559 /** \brief Add the value delta to var->df (the sum of latencies).
560  * 
561  *  \param sys the lmm_system_t associated
562  *  \param var the lmm_variable_t which need to updated
563  *  \param delta the variation of the latency
564  * 
565  *  Add the value delta to var->df (the sum of latencys associated to the
566  *  flow). Whenever this function is called a change is  signed in the system. To
567  *  avoid false system changing detection it is a good idea to test 
568  *  (delta != 0) before calling it.
569  *
570  */
571 void lmm_update_variable_latency(lmm_system_t sys, lmm_variable_t var,
572                                double delta)
573 {
574   sys->modified = 1;
575   var->df += delta;
576 }
577
578 void lmm_update_variable_weight(lmm_system_t sys, lmm_variable_t var,
579                                 double weight)
580 {
581   int i ;
582   lmm_element_t elem;
583
584   XBT_IN3("(sys=%p, var=%p, weight=%f)",sys,var,weight);
585   sys->modified = 1;
586   var->weight = weight;
587   xbt_swag_remove(var,&(sys->variable_set));
588   if(weight) xbt_swag_insert_at_head(var,&(sys->variable_set));
589   else xbt_swag_insert_at_tail(var,&(sys->variable_set));
590
591   for (i = 0; i < var->cnsts_number; i++) {
592     elem = &var->cnsts[i];
593     xbt_swag_remove(elem, &(elem->constraint->element_set));
594     if(weight) xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
595     else xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
596   }
597   XBT_OUT;
598 }
599
600 double lmm_get_variable_weight(lmm_variable_t var)
601                                   
602 {
603   return var->weight;
604 }
605
606 void lmm_update_constraint_bound(lmm_system_t sys, lmm_constraint_t cnst,
607                                  double bound)
608 {
609   sys->modified = 1;
610   cnst->bound = bound;
611 }
612
613 int lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst)
614 {
615   return xbt_swag_belongs(cnst, &(sys->active_constraint_set));
616 }
617
618 lmm_constraint_t lmm_get_first_active_constraint(lmm_system_t sys)
619 {
620   return xbt_swag_getFirst(&(sys->active_constraint_set));
621 }
622
623 lmm_constraint_t lmm_get_next_active_constraint(lmm_system_t sys, lmm_constraint_t cnst)
624 {
625   return xbt_swag_getNext(cnst,(sys->active_constraint_set).offset);
626 }
627
628