Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add lmm_shrink() as the opposite of lmm_expand()
[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_shrink(lmm_system_t sys, lmm_constraint_t cnst,
262                 lmm_variable_t var)
263 {
264   lmm_element_t elem = NULL;
265   int found = 0;
266
267   int i;
268   for (i = 0; i < var->cnsts_number; i++) {
269     elem = &(var->cnsts[i]);
270     if (elem->constraint == cnst) {
271       found = 1;
272       break;
273     }
274   }
275
276   if (!found) {
277     XBT_WARN("cnst %p is not found in var %p", cnst, var);
278     return;
279   }
280
281   sys->modified = 1;
282
283   XBT_INFO("remove elem(value %lf, cnst %p, var %p) in var %p",
284       elem->value, elem->constraint, elem->variable, var);
285
286   xbt_swag_remove(elem, &(elem->constraint->element_set));
287   var->cnsts[i] = var->cnsts[var->cnsts_number - 1];
288   var->cnsts_number -= 1;
289
290
291   if (xbt_swag_size(&(cnst->element_set)) == 0)
292     make_constraint_inactive(sys, cnst);
293   else {
294     /* This shrink operation effects the given constraint as well as all the constraints of the variable? */
295     lmm_update_modified_set(sys, cnst);
296     lmm_update_modified_set(sys, var->cnsts[0].constraint);
297   }
298
299
300 #if 0
301   if (!sys->selective_update_active) {
302     make_constraint_active(sys, cnst);
303
304   } else if (elem->value > 0 || var->weight > 0) {
305     make_constraint_active(sys, cnst);
306     lmm_update_modified_set(sys, cnst);
307
308     /* FIXME: Why is only the cnsts[0] used here? Don't we do this other cnts[i]? */
309     /* if a variable object has changes, we might want to propagate changes to all constraints of it? */
310     if (var->cnsts_number > 1)
311       lmm_update_modified_set(sys, var->cnsts[0].constraint);
312   }
313 #endif
314
315 }
316
317 void lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
318                 lmm_variable_t var, double value)
319 {
320   lmm_element_t elem = NULL;
321
322   sys->modified = 1;
323
324   xbt_assert(var->cnsts_number < var->cnsts_size, "Too much constraints");
325
326   elem = &(var->cnsts[var->cnsts_number++]);
327
328   elem->value = value;
329   elem->constraint = cnst;
330   elem->variable = var;
331
332   if (var->weight)
333     xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
334   else
335     xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
336   if(!sys->selective_update_active) {
337     make_constraint_active(sys, cnst);
338   } else if(elem->value>0 || var->weight >0) {
339     make_constraint_active(sys, cnst);
340     lmm_update_modified_set(sys, cnst);
341     if (var->cnsts_number > 1)
342       lmm_update_modified_set(sys, var->cnsts[0].constraint);
343   }
344 }
345
346 void lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst,
347                     lmm_variable_t var, double value)
348 {
349   int i;
350   sys->modified = 1;
351
352   for (i = 0; i < var->cnsts_number; i++)
353     if (var->cnsts[i].constraint == cnst)
354       break;
355
356   if (i < var->cnsts_number) {
357     if (cnst->shared)
358       var->cnsts[i].value += value;
359     else
360       var->cnsts[i].value = MAX(var->cnsts[i].value, value);
361     lmm_update_modified_set(sys, cnst);
362   } else
363     lmm_expand(sys, cnst, var, value);
364 }
365
366 void lmm_elem_set_value(lmm_system_t sys, lmm_constraint_t cnst,
367                         lmm_variable_t var, double value)
368 {
369   int i;
370
371   for (i = 0; i < var->cnsts_number; i++)
372     if (var->cnsts[i].constraint == cnst)
373       break;
374
375   if (i < var->cnsts_number) {
376     var->cnsts[i].value = value;
377     sys->modified = 1;
378     lmm_update_modified_set(sys, cnst);
379   } else
380     DIE_IMPOSSIBLE;
381 }
382
383 XBT_INLINE lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t sys,
384                                                   lmm_variable_t var,
385                                                   int num)
386 {
387   if (num < var->cnsts_number)
388     return (var->cnsts[num].constraint);
389   else
390     return NULL;
391 }
392
393 XBT_INLINE double lmm_get_cnst_weight_from_var(lmm_system_t sys,
394                                                          lmm_variable_t var,
395                                                          int num)
396 {
397   if (num < var->cnsts_number)
398     return (var->cnsts[num].value);
399   else
400     return 0.0;
401 }
402
403 XBT_INLINE int lmm_get_number_of_cnst_from_var(lmm_system_t sys,
404                                                lmm_variable_t var)
405 {
406   return (var->cnsts_number);
407 }
408
409 lmm_variable_t lmm_get_var_from_cnst(lmm_system_t sys,
410                                      lmm_constraint_t cnst,
411                                      lmm_element_t * elem)
412 {
413   if (!(*elem))
414     *elem = xbt_swag_getFirst(&(cnst->element_set));
415   else
416     *elem = xbt_swag_getNext(*elem, cnst->element_set.offset);
417   if (*elem)
418     return (*elem)->variable;
419   else
420     return NULL;
421 }
422
423 XBT_INLINE void *lmm_constraint_id(lmm_constraint_t cnst)
424 {
425   return cnst->id;
426 }
427
428 XBT_INLINE void *lmm_variable_id(lmm_variable_t var)
429 {
430   return var->id;
431 }
432
433 static XBT_INLINE void saturated_constraint_set_update(double usage,
434                                                       int cnst_light_num,
435                                                       dyn_light_t saturated_constraint_set,
436                                                       double *min_usage)
437 {
438   xbt_assert(usage > 0,"Impossible");
439
440   if (*min_usage < 0 || *min_usage > usage) {
441     *min_usage = usage;
442     // XBT_HERE(" min_usage=%f (cnst->remaining / cnst->usage =%f)", *min_usage, usage);
443     saturated_constraint_set->data[0] = cnst_light_num;
444     saturated_constraint_set->pos = 1;
445   } else if (*min_usage == usage) {
446     if(saturated_constraint_set->pos == saturated_constraint_set->size) { // realloc the size
447       saturated_constraint_set->size *= 2;
448       saturated_constraint_set->data = xbt_realloc(saturated_constraint_set->data, (saturated_constraint_set->size) * sizeof(int));
449     }
450     saturated_constraint_set->data[saturated_constraint_set->pos] = cnst_light_num;
451     saturated_constraint_set->pos++;
452   }
453 }
454
455 static XBT_INLINE void saturated_variable_set_update(
456     s_lmm_constraint_light_t *cnst_light_tab,
457     dyn_light_t saturated_constraint_set,
458     lmm_system_t sys)
459 {
460   lmm_constraint_light_t cnst = NULL;
461   lmm_element_t elem = NULL;
462   xbt_swag_t elem_list = NULL;
463   int i;
464   for(i = 0; i< saturated_constraint_set->pos; i++){
465     cnst = &cnst_light_tab[saturated_constraint_set->data[i]];
466     elem_list = &(cnst->cnst->active_element_set);
467     xbt_swag_foreach(elem, elem_list) {
468       if (elem->variable->weight <= 0)
469         break;
470       if ((elem->value > 0))
471         xbt_swag_insert(elem->variable, &(sys->saturated_variable_set));
472     }
473   }
474 }
475
476 void lmm_print(lmm_system_t sys)
477 {
478   lmm_constraint_t cnst = NULL;
479   lmm_element_t elem = NULL;
480   lmm_variable_t var = NULL;
481   xbt_swag_t cnst_list = NULL;
482   xbt_swag_t var_list = NULL;
483   xbt_swag_t elem_list = NULL;
484   char print_buf[1024];
485   char *trace_buf = xbt_malloc0(sizeof(char));
486   double sum = 0.0;
487
488   /* Printing Objective */
489   var_list = &(sys->variable_set);
490   sprintf(print_buf, "MAX-MIN ( ");
491   trace_buf =
492       xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
493   strcat(trace_buf, print_buf);
494   xbt_swag_foreach(var, var_list) {
495     sprintf(print_buf, "'%d'(%f) ", var->id_int, var->weight);
496     trace_buf =
497         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
498     strcat(trace_buf, print_buf);
499   }
500   sprintf(print_buf, ")");
501   trace_buf =
502       xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
503   strcat(trace_buf, print_buf);
504   XBT_DEBUG("%20s", trace_buf);
505   trace_buf[0] = '\000';
506
507   XBT_DEBUG("Constraints");
508   /* Printing Constraints */
509   cnst_list = &(sys->active_constraint_set);
510   xbt_swag_foreach(cnst, cnst_list) {
511     sum = 0.0;
512     elem_list = &(cnst->element_set);
513     sprintf(print_buf, "\t");
514     trace_buf =
515         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
516     strcat(trace_buf, print_buf);
517     sprintf(print_buf, "%s(",(cnst->shared)?"":"max");
518     trace_buf =
519       xbt_realloc(trace_buf,
520       strlen(trace_buf) + strlen(print_buf) + 1);
521     strcat(trace_buf, print_buf);      
522     xbt_swag_foreach(elem, elem_list) {
523       sprintf(print_buf, "%f.'%d'(%f) %s ", elem->value,
524               elem->variable->id_int, elem->variable->value,(cnst->shared)?"+":",");
525       trace_buf =
526           xbt_realloc(trace_buf,
527                       strlen(trace_buf) + strlen(print_buf) + 1);
528       strcat(trace_buf, print_buf);
529       if(cnst->shared) 
530   sum += elem->value * elem->variable->value;
531       else 
532   sum = MAX(sum,elem->value * elem->variable->value);
533     }
534     sprintf(print_buf, "0) <= %f ('%d')", cnst->bound, cnst->id_int);
535     trace_buf =
536         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
537     strcat(trace_buf, print_buf);
538
539     if (!cnst->shared) {
540       sprintf(print_buf, " [MAX-Constraint]");
541       trace_buf =
542           xbt_realloc(trace_buf,
543                       strlen(trace_buf) + strlen(print_buf) + 1);
544       strcat(trace_buf, print_buf);
545     }
546     XBT_DEBUG("%s", trace_buf);
547     trace_buf[0] = '\000';
548     xbt_assert(!double_positive(sum - cnst->bound),
549                 "Incorrect value (%f is not smaller than %f): %g",
550                 sum, cnst->bound, sum - cnst->bound);
551   }
552
553   XBT_DEBUG("Variables");
554   /* Printing Result */
555   xbt_swag_foreach(var, var_list) {
556     if (var->bound > 0) {
557       XBT_DEBUG("'%d'(%f) : %f (<=%f)", var->id_int, var->weight, var->value,
558              var->bound);
559       xbt_assert(!double_positive(var->value - var->bound),
560                   "Incorrect value (%f is not smaller than %f",
561                   var->value, var->bound);
562     } else {
563       XBT_DEBUG("'%d'(%f) : %f", var->id_int, var->weight, var->value);
564     }
565   }
566
567   free(trace_buf);
568 }
569
570 void lmm_solve(lmm_system_t sys)
571 {
572   lmm_variable_t var = NULL;
573   lmm_constraint_t cnst = NULL;
574   lmm_constraint_t cnst_next = NULL;
575   lmm_element_t elem = NULL;
576   xbt_swag_t cnst_list = NULL;
577   xbt_swag_t var_list = NULL;
578   xbt_swag_t elem_list = NULL;
579   double min_usage = -1;
580   double min_bound = -1;
581
582   if (!(sys->modified))
583     return;
584
585   XBT_IN("(sys=%p)", sys);
586
587   /*
588    * Compute Usage and store the variables that reach the maximum.
589    */
590   cnst_list =
591       sys->
592       selective_update_active ? &(sys->modified_constraint_set) :
593       &(sys->active_constraint_set);
594
595   XBT_DEBUG("Active constraints : %d", xbt_swag_size(cnst_list));
596   /* Init: Only modified code portions */
597   xbt_swag_foreach(cnst, cnst_list) {
598     elem_list = &(cnst->element_set);
599     //XBT_DEBUG("Variable set : %d", xbt_swag_size(elem_list));
600     xbt_swag_foreach(elem, elem_list) {
601       var = elem->variable;
602       if (var->weight <= 0.0)
603         break;
604       var->value = 0.0;
605     }
606   }
607
608   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));
609   int cnst_light_num = 0;
610   dyn_light_t saturated_constraint_set = xbt_new0(s_dyn_light_t,1);
611   saturated_constraint_set->size = 5;
612   saturated_constraint_set->data = xbt_new0(int, saturated_constraint_set->size);
613
614   xbt_swag_foreach_safe(cnst, cnst_next, cnst_list) {
615     /* INIT */
616     cnst->remaining = cnst->bound;
617     if (cnst->remaining == 0)
618       continue;
619     cnst->usage = 0;
620     elem_list = &(cnst->element_set);
621     xbt_swag_foreach(elem, elem_list) {
622       /* 0-weighted elements (ie, sleep actions) are at the end of the swag and we don't want to consider them */
623       if (elem->variable->weight <= 0)
624         break;
625       if ((elem->value > 0)) {
626         if (cnst->shared)
627           cnst->usage += elem->value / elem->variable->weight;
628         else if (cnst->usage < elem->value / elem->variable->weight)
629           cnst->usage = elem->value / elem->variable->weight;
630
631         make_elem_active(elem);
632         if (sys->keep_track)
633           xbt_swag_insert(elem->variable->id, sys->keep_track);
634       }
635     }
636     XBT_DEBUG("Constraint Usage '%d' : %f", cnst->id_int, cnst->usage);
637     /* Saturated constraints update */
638
639     if(cnst->usage > 0) {
640       cnst_light_tab[cnst_light_num].cnst = cnst;
641       cnst->cnst_light = &(cnst_light_tab[cnst_light_num]);
642       cnst_light_tab[cnst_light_num].remaining_over_usage = cnst->remaining / cnst->usage;
643       saturated_constraint_set_update(cnst_light_tab[cnst_light_num].remaining_over_usage,
644         cnst_light_num, saturated_constraint_set, &min_usage);
645       cnst_light_num++;
646     }
647   }
648
649   saturated_variable_set_update(  cnst_light_tab,
650                                   saturated_constraint_set,
651                                   sys);
652
653   /* Saturated variables update */
654
655   do {
656     /* Fix the variables that have to be */
657     var_list = &(sys->saturated_variable_set);
658
659     xbt_swag_foreach(var, var_list) {
660       if (var->weight <= 0.0)
661         DIE_IMPOSSIBLE;
662       /* First check if some of these variables have reach their upper
663          bound and update min_usage accordingly. */
664       XBT_DEBUG
665           ("var=%d, var->bound=%f, var->weight=%f, min_usage=%f, var->bound*var->weight=%f",
666            var->id_int, var->bound, var->weight, min_usage,
667            var->bound * var->weight);
668       if ((var->bound > 0) && (var->bound * var->weight < min_usage)) {
669         if (min_bound < 0)
670           min_bound = var->bound;
671         else
672           min_bound = MIN(min_bound, var->bound);
673         XBT_DEBUG("Updated min_bound=%f", min_bound);
674       }
675     }
676
677
678     while ((var = xbt_swag_getFirst(var_list))) {
679       int i;
680
681       if (min_bound < 0) {
682         var->value = min_usage / var->weight;
683       } else {
684         if (min_bound == var->bound)
685           var->value = var->bound;
686         else {
687           xbt_swag_remove(var, var_list);
688           continue;
689         }
690       }
691       XBT_DEBUG("Min usage: %f, Var(%d)->weight: %f, Var(%d)->value: %f ",
692              min_usage, var->id_int, var->weight, var->id_int, var->value);
693
694
695       /* Update usage */
696
697       for (i = 0; i < var->cnsts_number; i++) {
698         elem = &var->cnsts[i];
699         cnst = elem->constraint;
700         if (cnst->shared) {
701           double_update(&(cnst->remaining), elem->value * var->value);
702           double_update(&(cnst->usage), elem->value / var->weight);
703           if(cnst->usage<=0 || cnst->remaining<=0) {
704             if (cnst->cnst_light) {
705               int index = (cnst->cnst_light-cnst_light_tab);
706               XBT_DEBUG("index: %d \t cnst_light_num: %d \t || \t cnst: %p \t cnst->cnst_light: %p \t cnst_light_tab: %p ",
707                   index,cnst_light_num, cnst, cnst->cnst_light, cnst_light_tab);
708               cnst_light_tab[index]=cnst_light_tab[cnst_light_num-1];
709               cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
710               cnst_light_num--;
711               cnst->cnst_light = NULL;
712             }
713           } else {
714             cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
715           }
716           make_elem_inactive(elem);
717         } else {
718           cnst->usage = 0.0;
719           make_elem_inactive(elem);
720           elem_list = &(cnst->element_set);
721           xbt_swag_foreach(elem, elem_list) {
722             if (elem->variable->weight <= 0 || elem->variable->value > 0)
723               break;
724             if (elem->value > 0)
725               cnst->usage = MAX(cnst->usage, elem->value / elem->variable->weight);
726           }
727           if (cnst->usage<=0 || cnst->remaining<=0) {
728             if(cnst->cnst_light) {
729               int index = (cnst->cnst_light-cnst_light_tab);
730               XBT_DEBUG("index: %d \t cnst_light_num: %d \t || \t cnst: %p \t cnst->cnst_light: %p \t cnst_light_tab: %p ",
731                   index,cnst_light_num, cnst, cnst->cnst_light, cnst_light_tab);
732               cnst_light_tab[index]=cnst_light_tab[cnst_light_num-1];
733               cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
734               cnst_light_num--;
735               cnst->cnst_light = NULL;
736             }
737           } else {
738             cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
739           }
740         }
741       }
742       xbt_swag_remove(var, var_list);
743     }
744
745     /* Find out which variables reach the maximum */
746     min_usage = -1;
747     min_bound = -1;
748     saturated_constraint_set->pos = 0;
749     int pos;
750     for(pos=0; pos<cnst_light_num; pos++)
751       saturated_constraint_set_update(
752           cnst_light_tab[pos].remaining_over_usage,
753           pos,
754           saturated_constraint_set,
755           &min_usage);
756
757     saturated_variable_set_update(  cnst_light_tab,
758                                     saturated_constraint_set,
759                                     sys);
760
761   } while (cnst_light_num > 0);
762
763   sys->modified = 0;
764   if (sys->selective_update_active)
765     lmm_remove_all_modified_set(sys);
766
767   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
768     lmm_print(sys);
769   }
770
771   xbt_free(saturated_constraint_set->data);
772   xbt_free(saturated_constraint_set);
773   xbt_free(cnst_light_tab);
774   XBT_OUT();
775 }
776
777 /* Not a O(1) function */
778
779 void lmm_update(lmm_system_t sys, lmm_constraint_t cnst,
780                 lmm_variable_t var, double value)
781 {
782   int i;
783
784   for (i = 0; i < var->cnsts_number; i++)
785     if (var->cnsts[i].constraint == cnst) {
786       var->cnsts[i].value = value;
787       sys->modified = 1;
788       lmm_update_modified_set(sys, cnst);
789       return;
790     }
791 }
792
793 /** \brief Attribute the value bound to var->bound.
794  * 
795  *  \param sys the lmm_system_t
796  *  \param var the lmm_variable_t
797  *  \param bound the new bound to associate with var
798  * 
799  *  Makes var->bound equal to bound. Whenever this function is called 
800  *  a change is  signed in the system. To
801  *  avoid false system changing detection it is a good idea to test 
802  *  (bound != 0) before calling it.
803  *
804  */
805 void lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
806                                double bound)
807 {
808   sys->modified = 1;
809   var->bound = bound;
810
811   if (var->cnsts_number)
812     lmm_update_modified_set(sys, var->cnsts[0].constraint);
813 }
814
815
816 void lmm_update_variable_weight(lmm_system_t sys, lmm_variable_t var,
817                                 double weight)
818 {
819   int i;
820   lmm_element_t elem;
821
822   if (weight == var->weight)
823     return;
824   XBT_IN("(sys=%p, var=%p, weight=%f)", sys, var, weight);
825   sys->modified = 1;
826   var->weight = weight;
827   xbt_swag_remove(var, &(sys->variable_set));
828   if (weight)
829     xbt_swag_insert_at_head(var, &(sys->variable_set));
830   else
831     xbt_swag_insert_at_tail(var, &(sys->variable_set));
832
833   for (i = 0; i < var->cnsts_number; i++) {
834     elem = &var->cnsts[i];
835     xbt_swag_remove(elem, &(elem->constraint->element_set));
836     if (weight)
837       xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
838     else
839       xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
840
841     if (i == 0)
842       lmm_update_modified_set(sys, elem->constraint);
843   }
844   if (!weight)
845     var->value = 0.0;
846
847   XBT_OUT();
848 }
849
850 XBT_INLINE double lmm_get_variable_weight(lmm_variable_t var)
851 {
852   return var->weight;
853 }
854
855 XBT_INLINE void lmm_update_constraint_bound(lmm_system_t sys,
856                                             lmm_constraint_t cnst,
857                                             double bound)
858 {
859   sys->modified = 1;
860   lmm_update_modified_set(sys, cnst);
861   cnst->bound = bound;
862 }
863
864 XBT_INLINE int lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst)
865 {
866   return xbt_swag_belongs(cnst, &(sys->active_constraint_set));
867 }
868
869 XBT_INLINE lmm_constraint_t lmm_get_first_active_constraint(lmm_system_t
870                                                             sys)
871 {
872   return xbt_swag_getFirst(&(sys->active_constraint_set));
873 }
874
875 XBT_INLINE lmm_constraint_t lmm_get_next_active_constraint(lmm_system_t
876                                                            sys,
877                                                            lmm_constraint_t
878                                                            cnst)
879 {
880   return xbt_swag_getNext(cnst, (sys->active_constraint_set).offset);
881 }
882
883 #ifdef HAVE_LATENCY_BOUND_TRACKING
884 XBT_INLINE int lmm_is_variable_limited_by_latency(lmm_variable_t var)
885 {
886   return (double_equals(var->bound, var->value));
887 }
888 #endif
889
890
891 /** \brief Update the constraint set propagating recursively to
892  *  other constraints so the system should not be entirely computed.
893  *
894  *  \param sys the lmm_system_t
895  *  \param cnst the lmm_constraint_t affected by the change
896  *
897  *  A recursive algorithm to optimize the system recalculation selecting only
898  *  constraints that have changed. Each constraint change is propagated
899  *  to the list of constraints for each variable.
900  */
901 static void lmm_update_modified_set_rec(lmm_system_t sys,
902                                         lmm_constraint_t cnst)
903 {
904   lmm_element_t elem;
905
906   xbt_swag_foreach(elem, &cnst->element_set) {
907     lmm_variable_t var = elem->variable;
908     s_lmm_element_t *cnsts = var->cnsts;
909     int i;
910     for (i = 0; var->visited != sys->visited_counter
911              && i < var->cnsts_number ; i++) {
912       if (cnsts[i].constraint != cnst
913           && !xbt_swag_belongs(cnsts[i].constraint,
914                                &sys->modified_constraint_set)) {
915         xbt_swag_insert(cnsts[i].constraint, &sys->modified_constraint_set);
916         lmm_update_modified_set_rec(sys, cnsts[i].constraint);
917       }
918     }
919     var->visited = sys->visited_counter;
920   }
921 }
922
923 static void lmm_update_modified_set(lmm_system_t sys,
924                                     lmm_constraint_t cnst)
925 {
926   /* nothing to do if selective update isn't active */
927   if (sys->selective_update_active
928       && !xbt_swag_belongs(cnst, &sys->modified_constraint_set)) {
929     xbt_swag_insert(cnst, &sys->modified_constraint_set);
930     lmm_update_modified_set_rec(sys, cnst);
931   }
932 }
933
934 /** \brief Remove all constraints of the modified_constraint_set.
935  *
936  *  \param sys the lmm_system_t
937  */
938 static void lmm_remove_all_modified_set(lmm_system_t sys)
939 {
940   if (++sys->visited_counter == 1) {
941     /* the counter wrapped around, reset each variable->visited */
942     lmm_variable_t var;
943     xbt_swag_foreach(var, &sys->variable_set)
944       var->visited = 0;
945   }
946   xbt_swag_reset(&sys->modified_constraint_set);
947 }