Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Debug cosmetics.
[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   var->func_fpip = func_fpip_def;
183
184   if(weight) xbt_swag_insert_at_head(var,&(sys->variable_set));
185   else xbt_swag_insert_at_tail(var,&(sys->variable_set));
186   XBT_OUT;
187   return var;
188 }
189
190 void lmm_variable_free(lmm_system_t sys, lmm_variable_t var)
191 {
192   remove_variable(sys, var);
193   lmm_var_free(sys, var);
194 }
195
196 double lmm_variable_getvalue(lmm_variable_t var)
197 {
198   return (var->value);
199 }
200
201 void lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
202                 lmm_variable_t var, double value)
203 {
204   lmm_element_t elem = NULL;
205
206   sys->modified = 1;
207
208   xbt_assert0(var->cnsts_number < var->cnsts_size,
209               "Too much constraints");
210
211   elem = &(var->cnsts[var->cnsts_number++]);
212
213   elem->value = value;
214   elem->constraint = cnst;
215   elem->variable = var;
216
217   if(var->weight) xbt_swag_insert_at_head(elem,&(elem->constraint->element_set));
218   else xbt_swag_insert_at_tail(elem,&(elem->constraint->element_set));
219
220   make_constraint_active(sys, cnst);
221 }
222
223 void lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst,
224                     lmm_variable_t var, double value)
225 {
226   int i ; 
227   sys->modified = 1;
228
229   for(i=0; i< var->cnsts_number ; i++)
230     if(var->cnsts[i].constraint == cnst) break;
231   
232   if(i<var->cnsts_number) var->cnsts[i].value +=value;
233   else lmm_expand(sys,cnst,var,value);
234 }
235
236 void lmm_elem_set_value(lmm_system_t sys, lmm_constraint_t cnst,
237                         lmm_variable_t var, double value)
238 {
239   int i ; 
240
241   for(i=0; i< var->cnsts_number ; i++)
242     if(var->cnsts[i].constraint == cnst) break;
243
244   if(i<var->cnsts_number) {
245     var->cnsts[i].value =value;
246     sys->modified = 1;
247   } else DIE_IMPOSSIBLE;
248 }
249
250 lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t sys,
251                                        lmm_variable_t var, int num)
252 {
253   if (num < var->cnsts_number)
254     return (var->cnsts[num].constraint);
255   else
256     return NULL;
257 }
258
259 int lmm_get_number_of_cnst_from_var(lmm_system_t sys, lmm_variable_t var)
260 {
261   return (var->cnsts_number);
262 }
263
264 lmm_variable_t lmm_get_var_from_cnst(lmm_system_t sys,
265                                      lmm_constraint_t cnst,
266                                      lmm_variable_t * var)
267 {
268   if (!(*var))
269     xbt_swag_getFirst(&(cnst->element_set));
270   else
271     *var = xbt_swag_getNext(*var, cnst->element_set.offset);
272   return *var;
273 }
274
275 void *lmm_constraint_id(lmm_constraint_t cnst)
276 {
277   return cnst->id;
278 }
279
280 void *lmm_variable_id(lmm_variable_t var)
281 {
282   return var->id;
283 }
284
285 static void saturated_constraint_set_update(lmm_system_t sys,
286                                             lmm_constraint_t cnst,
287                                             double *min_usage)
288 {
289   lmm_constraint_t useless_cnst = NULL;
290
291   XBT_IN3("sys=%p, cnst=%p, min_usage=%f",sys,cnst,*min_usage);
292   if (cnst->usage <= 0) {
293     XBT_OUT;
294     return;
295   }
296   if (cnst->remaining <= 0) {
297     XBT_OUT;
298     return;
299   }
300   if ((*min_usage < 0) || (*min_usage > cnst->remaining / cnst->usage)) {
301     *min_usage = cnst->remaining / cnst->usage;
302     LOG3(xbt_log_priority_trace, 
303          "min_usage=%f (cnst->remaining=%f, cnst->usage=%f)",*min_usage, 
304          cnst->remaining, cnst->usage);
305     while ((useless_cnst =
306             xbt_swag_getFirst(&(sys->saturated_constraint_set))))
307       xbt_swag_remove(useless_cnst, &(sys->saturated_constraint_set));
308
309     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
310   } else if (*min_usage == cnst->remaining / cnst->usage) {
311     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
312   }
313   XBT_OUT;
314 }
315
316 static void saturated_variable_set_update(lmm_system_t sys)
317 {
318   lmm_constraint_t cnst = NULL;
319   xbt_swag_t cnst_list = NULL;
320   lmm_element_t elem = NULL;
321   xbt_swag_t elem_list = NULL;
322
323   cnst_list = &(sys->saturated_constraint_set);
324   while ((cnst = xbt_swag_getFirst(cnst_list))) {
325     elem_list = &(cnst->active_element_set);
326     xbt_swag_foreach(elem, elem_list) {
327       if(elem->variable->weight<=0) break;
328       if ((elem->value > 0))
329         xbt_swag_insert(elem->variable, &(sys->saturated_variable_set));
330     }
331     xbt_swag_remove(cnst, cnst_list);
332   }
333 }
334
335 void lmm_print(lmm_system_t sys)
336
337 {
338   lmm_constraint_t cnst = NULL;
339   lmm_element_t elem = NULL;
340   lmm_variable_t var = NULL;
341   xbt_swag_t cnst_list = NULL;
342   xbt_swag_t var_list = NULL;
343   xbt_swag_t elem_list = NULL;
344   char print_buf[1024];
345   char *trace_buf=xbt_malloc0(sizeof(char));
346   double sum=0.0;
347
348   /* Printing Objective */
349   var_list = &(sys->variable_set);
350   sprintf(print_buf,"MAX-MIN ( ");
351   trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
352   strcat(trace_buf, print_buf);
353   xbt_swag_foreach(var, var_list) {
354     sprintf(print_buf,"'%p'(%f) ",var,var->weight);
355     trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
356     strcat(trace_buf, print_buf);
357   }
358   sprintf(print_buf,")");
359   trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
360   strcat(trace_buf, print_buf);
361   DEBUG1("%s",trace_buf);
362   trace_buf[0]='\000';
363
364   /* Printing Constraints */
365   cnst_list = &(sys->active_constraint_set);
366   xbt_swag_foreach(cnst, cnst_list) {
367     sum=0.0;
368     elem_list = &(cnst->element_set);
369     sprintf(print_buf,"\t");
370     trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
371     strcat(trace_buf, print_buf);
372     xbt_swag_foreach(elem, elem_list) {
373       sprintf(print_buf,"%f.'%p'(%f) + ",elem->value, 
374               elem->variable,elem->variable->value);
375       trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
376       strcat(trace_buf, print_buf);
377       sum += elem->value * elem->variable->value;
378     }
379     sprintf(print_buf,"0 <= %f ('%p')",cnst->bound,cnst);
380     trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
381     strcat(trace_buf, print_buf);
382
383     if(!cnst->shared) {
384       sprintf(print_buf," [MAX-Constraint]");
385       trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
386       strcat(trace_buf, print_buf);
387     }
388     DEBUG1("%s",trace_buf);
389     trace_buf[0]='\000';
390     xbt_assert3(!double_positive(sum-cnst->bound), "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       xbt_assert2(!double_positive(var->value-var->bound), "Incorrect value (%f is not smaller than %f",
399                   var->value, var->bound);
400     }
401     else 
402       DEBUG3("'%p'(%f) : %f",var,var->weight,var->value);
403   }
404
405   free(trace_buf);
406 }
407
408 void lmm_solve(lmm_system_t sys)
409 {
410   lmm_variable_t var = NULL;
411   lmm_constraint_t cnst = NULL;
412   lmm_element_t elem = NULL;
413   xbt_swag_t cnst_list = NULL;
414   xbt_swag_t var_list = NULL;
415   xbt_swag_t elem_list = NULL;
416   double min_usage = -1;
417
418   if (!(sys->modified))
419     return;
420
421   /* Init */
422   var_list = &(sys->variable_set);
423   DEBUG1("Variable set : %d", xbt_swag_size(var_list));
424   xbt_swag_foreach(var, var_list) {
425     var->value = 0.0;
426   }
427
428   /* 
429    * Compute Usage and store the variables that reach the maximum.
430    */
431   cnst_list = &(sys->active_constraint_set);
432   DEBUG1("Active constraints : %d", xbt_swag_size(cnst_list));
433   xbt_swag_foreach(cnst, cnst_list) {
434     /* INIT */
435     cnst->remaining = cnst->bound;
436     cnst->usage = 0;
437     elem_list = &(cnst->element_set);
438     cnst->usage = 0.0;
439     xbt_swag_foreach(elem, elem_list) {
440       if(elem->variable->weight <=0) break;
441       if ((elem->value > 0)) {
442         if(cnst->shared)
443           cnst->usage += elem->value / elem->variable->weight;
444         else 
445           if(cnst->usage<elem->value / elem->variable->weight)
446             cnst->usage = elem->value / elem->variable->weight;
447         DEBUG2("Constraint Usage %p : %f",cnst,cnst->usage);
448         make_elem_active(elem);
449       }
450     }
451     /* Saturated constraints update */
452     saturated_constraint_set_update(sys, cnst, &min_usage);
453   }
454   saturated_variable_set_update(sys);
455
456   /* Saturated variables update */
457
458   do {
459     /* Fix the variables that have to be */
460     var_list = &(sys->saturated_variable_set);
461
462     xbt_swag_foreach(var, var_list) {
463       /* First check if some of these variables have reach their upper
464          bound and update min_usage accordingly. */
465       DEBUG5("var=%p, var->bound=%f, var->weight=%f, min_usage=%f, var->bound*var->weight=%f",
466              var, var->bound, var->weight, min_usage,var->bound * var->weight);
467       if ((var->bound > 0) && (var->bound * var->weight < min_usage)) {
468         min_usage = var->bound * var->weight;
469         DEBUG1("Updated min_usage=%f",min_usage);
470       }
471     }
472
473
474     while ((var = xbt_swag_getFirst(var_list))) {
475       int i;
476
477       var->value = min_usage / var->weight;
478       DEBUG5("Min usage: %f, Var(%p)->weight: %f, Var(%p)->value: %f ",min_usage,var,var->weight,var,var->value);
479
480
481       /* Update usage */
482
483       for (i = 0; i < var->cnsts_number; i++) {
484         elem = &var->cnsts[i];
485         cnst = elem->constraint;
486         if(cnst->shared) {
487           double_update(&(cnst->remaining), elem->value * var->value);
488           double_update(&(cnst->usage), elem->value / var->weight);
489           make_elem_inactive(elem);
490         } else { /* FIXME one day: We recompute usage.... :( */
491           cnst->usage = 0.0;
492           make_elem_inactive(elem);
493           xbt_swag_foreach(elem, elem_list) {
494             if(elem->variable->weight <=0) break;
495             if(elem->variable->value > 0) break;
496             if ((elem->value > 0)) {
497               if(cnst->usage<elem->value / elem->variable->weight)
498                 cnst->usage = elem->value / elem->variable->weight;
499               DEBUG2("Constraint Usage %p : %f",cnst,cnst->usage);
500               make_elem_active(elem);
501             }
502           } 
503         }
504       }
505       xbt_swag_remove(var, var_list);
506     }
507
508     /* Find out which variables reach the maximum */
509     cnst_list = &(sys->active_constraint_set);
510     min_usage = -1;
511     xbt_swag_foreach(cnst, cnst_list) {
512       saturated_constraint_set_update(sys, cnst, &min_usage);
513     }
514     saturated_variable_set_update(sys);
515
516   } while (xbt_swag_size(&(sys->saturated_variable_set)));
517
518   sys->modified = 0;
519   if(XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
520     lmm_print(sys);
521   }
522 }
523
524 /* Not a O(1) function */
525
526 void lmm_update(lmm_system_t sys, lmm_constraint_t cnst,
527                 lmm_variable_t var, double value)
528 {
529   int i;
530
531   sys->modified = 1;
532   for (i = 0; i < var->cnsts_number; i++)
533     if (var->cnsts[i].constraint == cnst) {
534       var->cnsts[i].value = value;
535       return;
536     }
537 }
538
539 /** \brief Attribute the value bound to var->bound.
540  * 
541  *  \param sys the lmm_system_t
542  *  \param var the lmm_variable_t
543  *  \param bound the new bound to associate with var
544  * 
545  *  Makes var->bound equal to bound. Whenever this function is called 
546  *  a change is  signed in the system. To
547  *  avoid false system changing detection it is a good idea to test 
548  *  (bound != 0) before calling it.
549  *
550  */
551 void lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
552                                double bound)
553 {
554   sys->modified = 1;
555   var->bound = bound;
556 }
557
558 /** \brief Add the value delta to var->df (the sum of latencies).
559  * 
560  *  \param sys the lmm_system_t associated
561  *  \param var the lmm_variable_t which need to updated
562  *  \param delta the variation of the latency
563  * 
564  *  Add the value delta to var->df (the sum of latencys associated to the
565  *  flow). Whenever this function is called a change is  signed in the system. To
566  *  avoid false system changing detection it is a good idea to test 
567  *  (delta != 0) before calling it.
568  *
569  */
570 void lmm_update_variable_latency(lmm_system_t sys, lmm_variable_t var,
571                                double delta)
572 {
573   sys->modified = 1;
574   var->df += delta;
575 }
576
577 void lmm_update_variable_weight(lmm_system_t sys, lmm_variable_t var,
578                                 double weight)
579 {
580   int i ;
581   lmm_element_t elem;
582
583   XBT_IN3("(sys=%p, var=%p, weight=%f)",sys,var,weight);
584   sys->modified = 1;
585   var->weight = weight;
586   xbt_swag_remove(var,&(sys->variable_set));
587   if(weight) xbt_swag_insert_at_head(var,&(sys->variable_set));
588   else xbt_swag_insert_at_tail(var,&(sys->variable_set));
589
590   for (i = 0; i < var->cnsts_number; i++) {
591     elem = &var->cnsts[i];
592     xbt_swag_remove(elem, &(elem->constraint->element_set));
593     if(weight) xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
594     else xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
595   }
596   XBT_OUT;
597 }
598
599 double lmm_get_variable_weight(lmm_variable_t var)
600                                   
601 {
602   return var->weight;
603 }
604
605 void lmm_update_constraint_bound(lmm_system_t sys, lmm_constraint_t cnst,
606                                  double bound)
607 {
608   sys->modified = 1;
609   cnst->bound = bound;
610 }
611
612 int lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst)
613 {
614   return xbt_swag_belongs(cnst, &(sys->active_constraint_set));
615 }
616
617 lmm_constraint_t lmm_get_first_active_constraint(lmm_system_t sys)
618 {
619   return xbt_swag_getFirst(&(sys->active_constraint_set));
620 }
621
622 lmm_constraint_t lmm_get_next_active_constraint(lmm_system_t sys, lmm_constraint_t cnst)
623 {
624   return xbt_swag_getNext(cnst,(sys->active_constraint_set).offset);
625 }
626
627
628
629 /** \brief Attribute the value bound to var->bound.
630  * 
631  *  \param func_f    default function f associated with the chosen protocol flavor
632  *  \param func_fp   partial differential of f (f prime, f')
633  *  \param func_fpi  inverse of the partial differential of f (f prime inverse, (f')^{-1})
634  *  \param func_fpip partial differential of the inverse of the partial differential of f (f prime inverse prime, ((f')^{-1})')
635  * 
636  *  Set default functions to the ones passed as parameters. This is a polimorfism in C pure, enjoy the roots of programming.
637  *
638  */
639 void lmm_set_default_protocol_functions(double (* func_f)    (lmm_variable_t var, double x),
640                                         double (* func_fp)   (lmm_variable_t var, double x),
641                                         double (* func_fpi)  (lmm_variable_t var, double x),
642                                         double (* func_fpip) (lmm_variable_t var, double x))
643                                         
644 {
645   func_f_def    = func_f;
646   func_fp_def   = func_fp;
647   func_fpi_def  = func_fpi;
648   func_fpip_def = func_fpip;
649 }
650