Logo AND Algorithmique Numérique Distribuée

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