Logo AND Algorithmique Numérique Distribuée

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