Logo AND Algorithmique Numérique Distribuée

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