Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix a few memory leaks.
[simgrid.git] / src / surf / maxmin.c
1 /* Copyright (c) 2004-2011. 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
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/mallocator.h"
11 #include "maxmin_private.h"
12 #include <stdlib.h>
13 #include <stdio.h>              /* sprintf */
14 #include <math.h>
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_maxmin, surf,
16                                 "Logging specific to SURF (maxmin)");
17
18 typedef struct s_dyn_light {
19   int *data;
20   int pos;
21   int size;
22 } s_dyn_light_t, *dyn_light_t;
23
24 XBT_EXPORT_NO_IMPORT(double) sg_maxmin_precision = 0.00001;
25
26 static void *lmm_variable_mallocator_new_f(void);
27 static void lmm_variable_mallocator_free_f(void *var);
28 #define lmm_variable_mallocator_reset_f ((void_f_pvoid_t)NULL)
29 static void lmm_update_modified_set(lmm_system_t sys,
30                                     lmm_constraint_t cnst);
31 static void lmm_remove_all_modified_set(lmm_system_t sys);
32 static int Global_debug_id = 1;
33 static int Global_const_debug_id = 1;
34
35 static void lmm_var_free(lmm_system_t sys, lmm_variable_t var);
36 static XBT_INLINE void lmm_cnst_free(lmm_system_t sys,
37                                      lmm_constraint_t cnst);
38
39 lmm_system_t lmm_system_new(int selective_update)
40 {
41   lmm_system_t l = NULL;
42   s_lmm_variable_t var;
43   s_lmm_constraint_t cnst;
44
45   l = xbt_new0(s_lmm_system_t, 1);
46
47   l->modified = 0;
48   l->selective_update_active = selective_update;
49   l->visited_counter = 1;
50
51   XBT_DEBUG("Setting selective_update_active flag to %d\n",
52          l->selective_update_active);
53
54   xbt_swag_init(&(l->variable_set),
55                 xbt_swag_offset(var, variable_set_hookup));
56   xbt_swag_init(&(l->constraint_set),
57                 xbt_swag_offset(cnst, constraint_set_hookup));
58
59   xbt_swag_init(&(l->active_constraint_set),
60                 xbt_swag_offset(cnst, active_constraint_set_hookup));
61
62   xbt_swag_init(&(l->modified_constraint_set),
63                 xbt_swag_offset(cnst, modified_constraint_set_hookup));
64   xbt_swag_init(&(l->saturated_variable_set),
65                 xbt_swag_offset(var, saturated_variable_set_hookup));
66   xbt_swag_init(&(l->saturated_constraint_set),
67                 xbt_swag_offset(cnst, saturated_constraint_set_hookup));
68
69   l->variable_mallocator = xbt_mallocator_new(65536,
70                                               lmm_variable_mallocator_new_f,
71                                               lmm_variable_mallocator_free_f,
72                                               lmm_variable_mallocator_reset_f);
73
74   return l;
75 }
76
77 void lmm_system_free(lmm_system_t sys)
78 {
79   lmm_variable_t var = NULL;
80   lmm_constraint_t cnst = NULL;
81
82   while ((var = extract_variable(sys))) {
83     XBT_WARN
84         ("Variable %p (%d) still in LMM system when freing it: this may be a bug",
85          var, var->id_int);
86     lmm_var_free(sys, var);
87   }
88
89   while ((cnst = extract_constraint(sys)))
90     lmm_cnst_free(sys, cnst);
91
92   xbt_mallocator_free(sys->variable_mallocator);
93   free(sys);
94 }
95
96 XBT_INLINE void lmm_variable_disable(lmm_system_t sys, lmm_variable_t var)
97 {
98   int i;
99   int n;
100
101   lmm_element_t elem = NULL;
102
103   XBT_IN("(sys=%p, var=%p)", sys, var);
104   sys->modified = 1;
105
106   n = 0;
107   for (i = 0; i < var->cnsts_number; i++) {
108     elem = &var->cnsts[i];
109     xbt_swag_remove(elem, &(elem->constraint->element_set));
110     xbt_swag_remove(elem, &(elem->constraint->active_element_set));
111     if (!xbt_swag_size(&(elem->constraint->element_set)))
112       make_constraint_inactive(sys, elem->constraint);
113     else {
114       if (n < i)
115         var->cnsts[n].constraint = elem->constraint;
116       n++;
117     }
118   }
119   if (n) {
120     var->cnsts_number = n;
121     lmm_update_modified_set(sys, var->cnsts[0].constraint);
122   }
123
124   var->cnsts_number = 0;
125   XBT_OUT();
126 }
127
128 static void lmm_var_free(lmm_system_t sys, lmm_variable_t var)
129 {
130
131   lmm_variable_disable(sys, var);
132   xbt_mallocator_release(sys->variable_mallocator, var);
133 }
134
135 static XBT_INLINE void lmm_cnst_free(lmm_system_t sys,
136                                      lmm_constraint_t cnst)
137 {
138 /*   xbt_assert(xbt_swag_size(&(cnst->element_set)), */
139 /*         "This list should be empty!"); */
140   make_constraint_inactive(sys, cnst);
141   free(cnst);
142 }
143
144 lmm_constraint_t lmm_constraint_new(lmm_system_t sys, void *id,
145                                     double bound_value)
146 {
147   lmm_constraint_t cnst = NULL;
148   s_lmm_element_t elem;
149
150   cnst = xbt_new0(s_lmm_constraint_t, 1);
151   cnst->id = id;
152   cnst->id_int = Global_const_debug_id++;
153   xbt_swag_init(&(cnst->element_set),
154                 xbt_swag_offset(elem, element_set_hookup));
155   xbt_swag_init(&(cnst->active_element_set),
156                 xbt_swag_offset(elem, active_element_set_hookup));
157
158   cnst->bound = bound_value;
159   cnst->usage = 0;
160   cnst->shared = 1;
161   insert_constraint(sys, cnst);
162
163   return cnst;
164 }
165
166 XBT_INLINE void lmm_constraint_shared(lmm_constraint_t cnst)
167 {
168   cnst->shared = 0;
169 }
170
171 XBT_INLINE int lmm_constraint_is_shared(lmm_constraint_t cnst)
172 {
173   return (cnst->shared);
174 }
175
176 XBT_INLINE void lmm_constraint_free(lmm_system_t sys,
177                                     lmm_constraint_t cnst)
178 {
179   remove_constraint(sys, cnst);
180   lmm_cnst_free(sys, cnst);
181 }
182
183 static void *lmm_variable_mallocator_new_f(void)
184 {
185   lmm_variable_t var = xbt_new(s_lmm_variable_t, 1);
186   var->cnsts = NULL; /* will be created by realloc */
187   return var;
188 }
189
190 static void lmm_variable_mallocator_free_f(void *var)
191 {
192   xbt_free(((lmm_variable_t) var)->cnsts);
193   xbt_free(var);
194 }
195
196 lmm_variable_t lmm_variable_new(lmm_system_t sys, void *id,
197                                 double weight,
198                                 double bound, int number_of_constraints)
199 {
200   lmm_variable_t var = NULL;
201   int i;
202
203   XBT_IN("(sys=%p, id=%p, weight=%f, bound=%f, num_cons =%d)",
204           sys, id, weight, bound, number_of_constraints);
205
206   var = xbt_mallocator_get(sys->variable_mallocator);
207   var->id = id;
208   var->id_int = Global_debug_id++;
209   var->cnsts = xbt_realloc(var->cnsts, number_of_constraints * sizeof(s_lmm_element_t));
210   for (i = 0; i < number_of_constraints; i++) {
211     var->cnsts[i].element_set_hookup.next = NULL;
212     var->cnsts[i].element_set_hookup.prev = NULL;
213     var->cnsts[i].active_element_set_hookup.next = NULL;
214     var->cnsts[i].active_element_set_hookup.prev = NULL;
215     var->cnsts[i].constraint = NULL;
216     var->cnsts[i].variable = NULL;
217     var->cnsts[i].value = 0.0;
218   }
219   var->cnsts_size = number_of_constraints;
220   var->cnsts_number = 0;
221   var->weight = weight;
222   var->bound = bound;
223   var->value = 0.0;
224   var->visited = sys->visited_counter - 1;
225   var->mu = 0.0;
226   var->new_mu = 0.0;
227   var->func_f = func_f_def;
228   var->func_fp = func_fp_def;
229   var->func_fpi = func_fpi_def;
230
231   var->variable_set_hookup.next = NULL;
232   var->variable_set_hookup.prev = NULL;
233   var->saturated_variable_set_hookup.next = NULL;
234   var->saturated_variable_set_hookup.prev = NULL;
235
236   if (weight)
237     xbt_swag_insert_at_head(var, &(sys->variable_set));
238   else
239     xbt_swag_insert_at_tail(var, &(sys->variable_set));
240
241   XBT_OUT(" returns %p", var);
242   return var;
243 }
244
245 void lmm_variable_free(lmm_system_t sys, lmm_variable_t var)
246 {
247   remove_variable(sys, var);
248   lmm_var_free(sys, var);
249 }
250
251 XBT_INLINE double lmm_variable_getvalue(lmm_variable_t var)
252 {
253   return (var->value);
254 }
255
256 XBT_INLINE double lmm_variable_getbound(lmm_variable_t var)
257 {
258   return (var->bound);
259 }
260
261 void lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
262                 lmm_variable_t var, double value)
263 {
264   lmm_element_t elem = NULL;
265
266   sys->modified = 1;
267
268   xbt_assert(var->cnsts_number < var->cnsts_size, "Too much constraints");
269
270   elem = &(var->cnsts[var->cnsts_number++]);
271
272   elem->value = value;
273   elem->constraint = cnst;
274   elem->variable = var;
275
276   if (var->weight)
277     xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
278   else
279     xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
280   if(!sys->selective_update_active) {
281     make_constraint_active(sys, cnst);
282   } else if(elem->value>0 || var->weight >0) {
283     make_constraint_active(sys, cnst);
284     lmm_update_modified_set(sys, cnst);
285     if (var->cnsts_number > 1)
286       lmm_update_modified_set(sys, var->cnsts[0].constraint);
287   }
288 }
289
290 void lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst,
291                     lmm_variable_t var, double value)
292 {
293   int i;
294   sys->modified = 1;
295
296   for (i = 0; i < var->cnsts_number; i++)
297     if (var->cnsts[i].constraint == cnst)
298       break;
299
300   if (i < var->cnsts_number) {
301     if (cnst->shared)
302       var->cnsts[i].value += value;
303     else
304       var->cnsts[i].value = MAX(var->cnsts[i].value, value);
305     lmm_update_modified_set(sys, cnst);
306   } else
307     lmm_expand(sys, cnst, var, value);
308 }
309
310 void lmm_elem_set_value(lmm_system_t sys, lmm_constraint_t cnst,
311                         lmm_variable_t var, double value)
312 {
313   int i;
314
315   for (i = 0; i < var->cnsts_number; i++)
316     if (var->cnsts[i].constraint == cnst)
317       break;
318
319   if (i < var->cnsts_number) {
320     var->cnsts[i].value = value;
321     sys->modified = 1;
322     lmm_update_modified_set(sys, cnst);
323   } else
324     DIE_IMPOSSIBLE;
325 }
326
327 XBT_INLINE lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t sys,
328                                                   lmm_variable_t var,
329                                                   int num)
330 {
331   if (num < var->cnsts_number)
332     return (var->cnsts[num].constraint);
333   else
334     return NULL;
335 }
336
337 XBT_INLINE double lmm_get_cnst_weight_from_var(lmm_system_t sys,
338                                                          lmm_variable_t var,
339                                                          int num)
340 {
341   if (num < var->cnsts_number)
342     return (var->cnsts[num].value);
343   else
344     return 0.0;
345 }
346
347 XBT_INLINE int lmm_get_number_of_cnst_from_var(lmm_system_t sys,
348                                                lmm_variable_t var)
349 {
350   return (var->cnsts_number);
351 }
352
353 lmm_variable_t lmm_get_var_from_cnst(lmm_system_t sys,
354                                      lmm_constraint_t cnst,
355                                      lmm_element_t * elem)
356 {
357   if (!(*elem))
358     *elem = xbt_swag_getFirst(&(cnst->element_set));
359   else
360     *elem = xbt_swag_getNext(*elem, cnst->element_set.offset);
361   if (*elem)
362     return (*elem)->variable;
363   else
364     return NULL;
365 }
366
367 XBT_INLINE void *lmm_constraint_id(lmm_constraint_t cnst)
368 {
369   return cnst->id;
370 }
371
372 XBT_INLINE void *lmm_variable_id(lmm_variable_t var)
373 {
374   return var->id;
375 }
376
377 static XBT_INLINE void saturated_constraint_set_update(double usage,
378                                                       int cnst_light_num,
379                                                       dyn_light_t saturated_constraint_set,
380                                                       double *min_usage)
381 {
382   xbt_assert(usage > 0,"Impossible");
383
384   if (*min_usage < 0 || *min_usage > usage) {
385     *min_usage = usage;
386     // XBT_HERE(" min_usage=%f (cnst->remaining / cnst->usage =%f)", *min_usage, usage);
387     saturated_constraint_set->data[0] = cnst_light_num;
388     saturated_constraint_set->pos = 1;
389   } else if (*min_usage == usage) {
390     if(saturated_constraint_set->pos == saturated_constraint_set->size) { // realloc the size
391       saturated_constraint_set->size *= 2;
392       saturated_constraint_set->data = xbt_realloc(saturated_constraint_set->data, (saturated_constraint_set->size) * sizeof(int));
393     }
394     saturated_constraint_set->data[saturated_constraint_set->pos] = cnst_light_num;
395     saturated_constraint_set->pos++;
396   }
397 }
398
399 static XBT_INLINE void saturated_variable_set_update(
400     s_lmm_constraint_light_t *cnst_light_tab,
401     dyn_light_t saturated_constraint_set,
402     lmm_system_t sys)
403 {
404   lmm_constraint_light_t cnst = NULL;
405   lmm_element_t elem = NULL;
406   xbt_swag_t elem_list = NULL;
407   int i;
408   for(i = 0; i< saturated_constraint_set->pos; i++){
409     cnst = &cnst_light_tab[saturated_constraint_set->data[i]];
410     elem_list = &(cnst->cnst->active_element_set);
411     xbt_swag_foreach(elem, elem_list) {
412       if (elem->variable->weight <= 0)
413         break;
414       if ((elem->value > 0))
415         xbt_swag_insert(elem->variable, &(sys->saturated_variable_set));
416     }
417   }
418 }
419
420 void lmm_print(lmm_system_t sys)
421 {
422   lmm_constraint_t cnst = NULL;
423   lmm_element_t elem = NULL;
424   lmm_variable_t var = NULL;
425   xbt_swag_t cnst_list = NULL;
426   xbt_swag_t var_list = NULL;
427   xbt_swag_t elem_list = NULL;
428   char print_buf[1024];
429   char *trace_buf = xbt_malloc0(sizeof(char));
430   double sum = 0.0;
431
432   /* Printing Objective */
433   var_list = &(sys->variable_set);
434   sprintf(print_buf, "MAX-MIN ( ");
435   trace_buf =
436       xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
437   strcat(trace_buf, print_buf);
438   xbt_swag_foreach(var, var_list) {
439     sprintf(print_buf, "'%d'(%f) ", var->id_int, var->weight);
440     trace_buf =
441         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
442     strcat(trace_buf, print_buf);
443   }
444   sprintf(print_buf, ")");
445   trace_buf =
446       xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
447   strcat(trace_buf, print_buf);
448   XBT_DEBUG("%20s", trace_buf);
449   trace_buf[0] = '\000';
450
451   XBT_DEBUG("Constraints");
452   /* Printing Constraints */
453   cnst_list = &(sys->active_constraint_set);
454   xbt_swag_foreach(cnst, cnst_list) {
455     sum = 0.0;
456     elem_list = &(cnst->element_set);
457     sprintf(print_buf, "\t");
458     trace_buf =
459         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
460     strcat(trace_buf, print_buf);
461     sprintf(print_buf, "%s(",(cnst->shared)?"":"max");
462     trace_buf =
463       xbt_realloc(trace_buf,
464       strlen(trace_buf) + strlen(print_buf) + 1);
465     strcat(trace_buf, print_buf);      
466     xbt_swag_foreach(elem, elem_list) {
467       sprintf(print_buf, "%f.'%d'(%f) %s ", elem->value,
468               elem->variable->id_int, elem->variable->value,(cnst->shared)?"+":",");
469       trace_buf =
470           xbt_realloc(trace_buf,
471                       strlen(trace_buf) + strlen(print_buf) + 1);
472       strcat(trace_buf, print_buf);
473       if(cnst->shared) 
474   sum += elem->value * elem->variable->value;
475       else 
476   sum = MAX(sum,elem->value * elem->variable->value);
477     }
478     sprintf(print_buf, "0) <= %f ('%d')", cnst->bound, cnst->id_int);
479     trace_buf =
480         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
481     strcat(trace_buf, print_buf);
482
483     if (!cnst->shared) {
484       sprintf(print_buf, " [MAX-Constraint]");
485       trace_buf =
486           xbt_realloc(trace_buf,
487                       strlen(trace_buf) + strlen(print_buf) + 1);
488       strcat(trace_buf, print_buf);
489     }
490     XBT_DEBUG("%s", trace_buf);
491     trace_buf[0] = '\000';
492     xbt_assert(!double_positive(sum - cnst->bound),
493                 "Incorrect value (%f is not smaller than %f): %g",
494                 sum, cnst->bound, sum - cnst->bound);
495   }
496
497   XBT_DEBUG("Variables");
498   /* Printing Result */
499   xbt_swag_foreach(var, var_list) {
500     if (var->bound > 0) {
501       XBT_DEBUG("'%d'(%f) : %f (<=%f)", var->id_int, var->weight, var->value,
502              var->bound);
503       xbt_assert(!double_positive(var->value - var->bound),
504                   "Incorrect value (%f is not smaller than %f",
505                   var->value, var->bound);
506     } else {
507       XBT_DEBUG("'%d'(%f) : %f", var->id_int, var->weight, var->value);
508     }
509   }
510
511   free(trace_buf);
512 }
513
514 void lmm_solve(lmm_system_t sys)
515 {
516   lmm_variable_t var = NULL;
517   lmm_constraint_t cnst = NULL;
518   lmm_constraint_t cnst_next = NULL;
519   lmm_element_t elem = NULL;
520   xbt_swag_t cnst_list = NULL;
521   xbt_swag_t var_list = NULL;
522   xbt_swag_t elem_list = NULL;
523   double min_usage = -1;
524   double min_bound = -1;
525
526   if (!(sys->modified))
527     return;
528
529   XBT_IN("(sys=%p)", sys);
530
531   /*
532    * Compute Usage and store the variables that reach the maximum.
533    */
534   cnst_list =
535       sys->
536       selective_update_active ? &(sys->modified_constraint_set) :
537       &(sys->active_constraint_set);
538
539   XBT_DEBUG("Active constraints : %d", xbt_swag_size(cnst_list));
540   /* Init: Only modified code portions */
541   xbt_swag_foreach(cnst, cnst_list) {
542     elem_list = &(cnst->element_set);
543     //XBT_DEBUG("Variable set : %d", xbt_swag_size(elem_list));
544     xbt_swag_foreach(elem, elem_list) {
545       var = elem->variable;
546       if (var->weight <= 0.0)
547         break;
548       var->value = 0.0;
549     }
550   }
551
552   s_lmm_constraint_light_t *cnst_light_tab = (s_lmm_constraint_light_t *)xbt_malloc0(xbt_swag_size(cnst_list)*sizeof(s_lmm_constraint_light_t));
553   int cnst_light_num = 0;
554   dyn_light_t saturated_constraint_set = xbt_new0(s_dyn_light_t,1);
555   saturated_constraint_set->size = 5;
556   saturated_constraint_set->data = xbt_new0(int, saturated_constraint_set->size);
557
558   xbt_swag_foreach_safe(cnst, cnst_next, cnst_list) {
559     /* INIT */
560     cnst->remaining = cnst->bound;
561     if (cnst->remaining == 0)
562       continue;
563     cnst->usage = 0;
564     elem_list = &(cnst->element_set);
565     xbt_swag_foreach(elem, elem_list) {
566       /* 0-weighted elements (ie, sleep actions) are at the end of the swag and we don't want to consider them */
567       if (elem->variable->weight <= 0)
568         break;
569       if ((elem->value > 0)) {
570         if (cnst->shared)
571           cnst->usage += elem->value / elem->variable->weight;
572         else if (cnst->usage < elem->value / elem->variable->weight)
573           cnst->usage = elem->value / elem->variable->weight;
574
575         make_elem_active(elem);
576         if (sys->keep_track)
577           xbt_swag_insert(elem->variable->id, sys->keep_track);
578       }
579     }
580     XBT_DEBUG("Constraint Usage '%d' : %f", cnst->id_int, cnst->usage);
581     /* Saturated constraints update */
582
583     if(cnst->usage > 0) {
584       cnst_light_tab[cnst_light_num].cnst = cnst;
585       cnst->cnst_light = &(cnst_light_tab[cnst_light_num]);
586       cnst_light_tab[cnst_light_num].remaining_over_usage = cnst->remaining / cnst->usage;
587       saturated_constraint_set_update(cnst_light_tab[cnst_light_num].remaining_over_usage,
588         cnst_light_num, saturated_constraint_set, &min_usage);
589       cnst_light_num++;
590     }
591   }
592
593   saturated_variable_set_update(  cnst_light_tab,
594                                   saturated_constraint_set,
595                                   sys);
596
597   /* Saturated variables update */
598
599   do {
600     /* Fix the variables that have to be */
601     var_list = &(sys->saturated_variable_set);
602
603     xbt_swag_foreach(var, var_list) {
604       if (var->weight <= 0.0)
605         DIE_IMPOSSIBLE;
606       /* First check if some of these variables have reach their upper
607          bound and update min_usage accordingly. */
608       XBT_DEBUG
609           ("var=%d, var->bound=%f, var->weight=%f, min_usage=%f, var->bound*var->weight=%f",
610            var->id_int, var->bound, var->weight, min_usage,
611            var->bound * var->weight);
612       if ((var->bound > 0) && (var->bound * var->weight < min_usage)) {
613         if (min_bound < 0)
614           min_bound = var->bound;
615         else
616           min_bound = MIN(min_bound, var->bound);
617         XBT_DEBUG("Updated min_bound=%f", min_bound);
618       }
619     }
620
621
622     while ((var = xbt_swag_getFirst(var_list))) {
623       int i;
624
625       if (min_bound < 0) {
626         var->value = min_usage / var->weight;
627       } else {
628         if (min_bound == var->bound)
629           var->value = var->bound;
630         else {
631           xbt_swag_remove(var, var_list);
632           continue;
633         }
634       }
635       XBT_DEBUG("Min usage: %f, Var(%d)->weight: %f, Var(%d)->value: %f ",
636              min_usage, var->id_int, var->weight, var->id_int, var->value);
637
638
639       /* Update usage */
640
641       for (i = 0; i < var->cnsts_number; i++) {
642         elem = &var->cnsts[i];
643         cnst = elem->constraint;
644         if (cnst->shared) {
645           double_update(&(cnst->remaining), elem->value * var->value);
646           double_update(&(cnst->usage), elem->value / var->weight);
647           if(cnst->usage<=0 || cnst->remaining<=0) {
648             if (cnst->cnst_light) {
649               int index = (cnst->cnst_light-cnst_light_tab);
650               XBT_DEBUG("index: %d \t cnst_light_num: %d \t || \t cnst: %p \t cnst->cnst_light: %p \t cnst_light_tab: %p ",
651                   index,cnst_light_num, cnst, cnst->cnst_light, cnst_light_tab);
652               cnst_light_tab[index]=cnst_light_tab[cnst_light_num-1];
653               cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
654               cnst_light_num--;
655               cnst->cnst_light = NULL;
656             }
657           } else {
658             cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
659           }
660           make_elem_inactive(elem);
661         } else {
662           cnst->usage = 0.0;
663           make_elem_inactive(elem);
664           elem_list = &(cnst->element_set);
665           xbt_swag_foreach(elem, elem_list) {
666             if (elem->variable->weight <= 0 || elem->variable->value > 0)
667               break;
668             if (elem->value > 0)
669               cnst->usage = MAX(cnst->usage, elem->value / elem->variable->weight);
670           }
671           if (cnst->usage<=0 || cnst->remaining<=0) {
672             if(cnst->cnst_light) {
673               int index = (cnst->cnst_light-cnst_light_tab);
674               XBT_DEBUG("index: %d \t cnst_light_num: %d \t || \t cnst: %p \t cnst->cnst_light: %p \t cnst_light_tab: %p ",
675                   index,cnst_light_num, cnst, cnst->cnst_light, cnst_light_tab);
676               cnst_light_tab[index]=cnst_light_tab[cnst_light_num-1];
677               cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
678               cnst_light_num--;
679               cnst->cnst_light = NULL;
680             }
681           } else {
682             cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
683           }
684         }
685       }
686       xbt_swag_remove(var, var_list);
687     }
688
689     /* Find out which variables reach the maximum */
690     min_usage = -1;
691     min_bound = -1;
692     saturated_constraint_set->pos = 0;
693     int pos;
694     for(pos=0; pos<cnst_light_num; pos++)
695       saturated_constraint_set_update(
696           cnst_light_tab[pos].remaining_over_usage,
697           pos,
698           saturated_constraint_set,
699           &min_usage);
700
701     saturated_variable_set_update(  cnst_light_tab,
702                                     saturated_constraint_set,
703                                     sys);
704
705   } while (cnst_light_num > 0);
706
707   sys->modified = 0;
708   if (sys->selective_update_active)
709     lmm_remove_all_modified_set(sys);
710
711   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
712     lmm_print(sys);
713   }
714
715   xbt_free(saturated_constraint_set->data);
716   xbt_free(saturated_constraint_set);
717   xbt_free(cnst_light_tab);
718   XBT_OUT();
719 }
720
721 /* Not a O(1) function */
722
723 void lmm_update(lmm_system_t sys, lmm_constraint_t cnst,
724                 lmm_variable_t var, double value)
725 {
726   int i;
727
728   for (i = 0; i < var->cnsts_number; i++)
729     if (var->cnsts[i].constraint == cnst) {
730       var->cnsts[i].value = value;
731       sys->modified = 1;
732       lmm_update_modified_set(sys, cnst);
733       return;
734     }
735 }
736
737 /** \brief Attribute the value bound to var->bound.
738  * 
739  *  \param sys the lmm_system_t
740  *  \param var the lmm_variable_t
741  *  \param bound the new bound to associate with var
742  * 
743  *  Makes var->bound equal to bound. Whenever this function is called 
744  *  a change is  signed in the system. To
745  *  avoid false system changing detection it is a good idea to test 
746  *  (bound != 0) before calling it.
747  *
748  */
749 void lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
750                                double bound)
751 {
752   sys->modified = 1;
753   var->bound = bound;
754
755   if (var->cnsts_number)
756     lmm_update_modified_set(sys, var->cnsts[0].constraint);
757 }
758
759
760 void lmm_update_variable_weight(lmm_system_t sys, lmm_variable_t var,
761                                 double weight)
762 {
763   int i;
764   lmm_element_t elem;
765
766   if (weight == var->weight)
767     return;
768   XBT_IN("(sys=%p, var=%p, weight=%f)", sys, var, weight);
769   sys->modified = 1;
770   var->weight = weight;
771   xbt_swag_remove(var, &(sys->variable_set));
772   if (weight)
773     xbt_swag_insert_at_head(var, &(sys->variable_set));
774   else
775     xbt_swag_insert_at_tail(var, &(sys->variable_set));
776
777   for (i = 0; i < var->cnsts_number; i++) {
778     elem = &var->cnsts[i];
779     xbt_swag_remove(elem, &(elem->constraint->element_set));
780     if (weight)
781       xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
782     else
783       xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
784
785     if (i == 0)
786       lmm_update_modified_set(sys, elem->constraint);
787   }
788   if (!weight)
789     var->value = 0.0;
790
791   XBT_OUT();
792 }
793
794 XBT_INLINE double lmm_get_variable_weight(lmm_variable_t var)
795 {
796   return var->weight;
797 }
798
799 XBT_INLINE void lmm_update_constraint_bound(lmm_system_t sys,
800                                             lmm_constraint_t cnst,
801                                             double bound)
802 {
803   sys->modified = 1;
804   lmm_update_modified_set(sys, cnst);
805   cnst->bound = bound;
806 }
807
808 XBT_INLINE int lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst)
809 {
810   return xbt_swag_belongs(cnst, &(sys->active_constraint_set));
811 }
812
813 XBT_INLINE lmm_constraint_t lmm_get_first_active_constraint(lmm_system_t
814                                                             sys)
815 {
816   return xbt_swag_getFirst(&(sys->active_constraint_set));
817 }
818
819 XBT_INLINE lmm_constraint_t lmm_get_next_active_constraint(lmm_system_t
820                                                            sys,
821                                                            lmm_constraint_t
822                                                            cnst)
823 {
824   return xbt_swag_getNext(cnst, (sys->active_constraint_set).offset);
825 }
826
827 #ifdef HAVE_LATENCY_BOUND_TRACKING
828 XBT_INLINE int lmm_is_variable_limited_by_latency(lmm_variable_t var)
829 {
830   return (double_equals(var->bound, var->value));
831 }
832 #endif
833
834
835 /** \brief Update the constraint set propagating recursively to
836  *  other constraints so the system should not be entirely computed.
837  *
838  *  \param sys the lmm_system_t
839  *  \param cnst the lmm_constraint_t affected by the change
840  *
841  *  A recursive algorithm to optimize the system recalculation selecting only
842  *  constraints that have changed. Each constraint change is propagated
843  *  to the list of constraints for each variable.
844  */
845 static void lmm_update_modified_set_rec(lmm_system_t sys,
846                                         lmm_constraint_t cnst)
847 {
848   lmm_element_t elem;
849
850   xbt_swag_foreach(elem, &cnst->element_set) {
851     lmm_variable_t var = elem->variable;
852     s_lmm_element_t *cnsts = var->cnsts;
853     int i;
854     for (i = 0; var->visited != sys->visited_counter
855              && i < var->cnsts_number ; i++) {
856       if (cnsts[i].constraint != cnst
857           && !xbt_swag_belongs(cnsts[i].constraint,
858                                &sys->modified_constraint_set)) {
859         xbt_swag_insert(cnsts[i].constraint, &sys->modified_constraint_set);
860         lmm_update_modified_set_rec(sys, cnsts[i].constraint);
861       }
862     }
863     var->visited = sys->visited_counter;
864   }
865 }
866
867 static void lmm_update_modified_set(lmm_system_t sys,
868                                     lmm_constraint_t cnst)
869 {
870   /* nothing to do if selective update isn't active */
871   if (sys->selective_update_active
872       && !xbt_swag_belongs(cnst, &sys->modified_constraint_set)) {
873     xbt_swag_insert(cnst, &sys->modified_constraint_set);
874     lmm_update_modified_set_rec(sys, cnst);
875   }
876 }
877
878 /** \brief Remove all constraints of the modified_constraint_set.
879  *
880  *  \param sys the lmm_system_t
881  */
882 static void lmm_remove_all_modified_set(lmm_system_t sys)
883 {
884   if (++sys->visited_counter == 1) {
885     /* the counter wrapped around, reset each variable->visited */
886     lmm_variable_t var;
887     xbt_swag_foreach(var, &sys->variable_set)
888       var->visited = 0;
889   }
890   xbt_swag_reset(&sys->modified_constraint_set);
891 }