Logo AND Algorithmique Numérique Distribuée

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