Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
normalize some type names
[simgrid.git] / src / surf / maxmin.c
1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. 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   fprintf(stderr, "%s", trace_buf);
427   //XBT_DEBUG("%20s", trace_buf); FIXME
428   trace_buf[0] = '\000';
429
430   XBT_DEBUG("Constraints");
431   /* Printing Constraints */
432   cnst_list = &(sys->active_constraint_set);
433   xbt_swag_foreach(cnst, cnst_list) {
434     sum = 0.0;
435     elem_list = &(cnst->element_set);
436     sprintf(print_buf, "\t");
437     trace_buf =
438         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
439     strcat(trace_buf, print_buf);
440     sprintf(print_buf, "%s(",(cnst->shared)?"":"max");
441     trace_buf =
442       xbt_realloc(trace_buf,
443                   strlen(trace_buf) + strlen(print_buf) + 1);
444     strcat(trace_buf, print_buf);      
445     xbt_swag_foreach(elem, elem_list) {
446       sprintf(print_buf, "%f.'%d'(%f) %s ", elem->value,
447               elem->variable->id_int, elem->variable->value,(cnst->shared)?"+":",");
448       trace_buf =
449           xbt_realloc(trace_buf,
450                       strlen(trace_buf) + strlen(print_buf) + 1);
451       strcat(trace_buf, print_buf);
452       if(cnst->shared) 
453         sum += elem->value * elem->variable->value;
454       else 
455         sum = MAX(sum,elem->value * elem->variable->value);
456     }
457     sprintf(print_buf, "0) <= %f ('%d')", cnst->bound, cnst->id_int);
458     trace_buf =
459         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
460     strcat(trace_buf, print_buf);
461
462     if (!cnst->shared) {
463       sprintf(print_buf, " [MAX-Constraint]");
464       trace_buf =
465           xbt_realloc(trace_buf,
466                       strlen(trace_buf) + strlen(print_buf) + 1);
467       strcat(trace_buf, print_buf);
468     }
469     //   XBT_DEBUG("%s", trace_buf);
470     fprintf(stderr, "%s\n", trace_buf);
471     trace_buf[0] = '\000';
472     xbt_assert(!double_positive(sum - cnst->bound),
473                 "Incorrect value (%f is not smaller than %f): %g",
474                 sum, cnst->bound, sum - cnst->bound);
475   }
476
477   XBT_DEBUG("Variables");
478   /* Printing Result */
479   xbt_swag_foreach(var, var_list) {
480     if (var->bound > 0) {
481       XBT_DEBUG("'%d'(%f) : %f (<=%f)", var->id_int, var->weight, var->value,
482              var->bound);
483       xbt_assert(!double_positive(var->value - var->bound),
484                   "Incorrect value (%f is not smaller than %f",
485                   var->value, var->bound);
486     } else {
487       XBT_DEBUG("'%d'(%f) : %f", var->id_int, var->weight, var->value);
488     }
489   }
490
491   free(trace_buf);
492 }
493
494 void lmm_solve(lmm_system_t sys)
495 {
496   lmm_variable_t var = NULL;
497   lmm_constraint_t cnst = NULL;
498   lmm_element_t elem = NULL;
499   xbt_swag_t cnst_list = NULL;
500   xbt_swag_t var_list = NULL;
501   xbt_swag_t elem_list = NULL;
502   double min_usage = -1;
503   double min_bound = -1;
504
505   if (!(sys->modified))
506     return;
507
508   XBT_IN("(sys=%p)", sys);
509
510   /*
511    * Compute Usage and store the variables that reach the maximum.
512    */
513   cnst_list =
514       sys->
515       selective_update_active ? &(sys->modified_constraint_set) :
516       &(sys->active_constraint_set);
517
518   XBT_DEBUG("Active constraints : %d", xbt_swag_size(cnst_list));
519   /* Init: Only modified code portions */
520   xbt_swag_foreach(cnst, cnst_list) {
521     elem_list = &(cnst->element_set);
522     //XBT_DEBUG("Variable set : %d", xbt_swag_size(elem_list));
523     xbt_swag_foreach(elem, elem_list) {
524       var = elem->variable;
525       if (var->weight <= 0.0)
526         break;
527       var->value = 0.0;
528     }
529   }
530
531   xbt_swag_foreach(cnst, cnst_list) {
532     /* INIT */
533     cnst->remaining = cnst->bound;
534     if (cnst->remaining == 0)
535       continue;
536     cnst->usage = 0;
537     elem_list = &(cnst->element_set);
538     xbt_swag_foreach(elem, elem_list) {
539       /* 0-weighted elements (ie, sleep actions) are at the end of the swag and we don't want to consider them */
540       if (elem->variable->weight <= 0)
541         break;
542       if ((elem->value > 0)) {
543         if (cnst->shared)
544           cnst->usage += elem->value / elem->variable->weight;
545         else if (cnst->usage < elem->value / elem->variable->weight)
546           cnst->usage = elem->value / elem->variable->weight;
547
548         make_elem_active(elem);
549         if(keep_track){
550             xbt_swag_insert((elem->variable)->id, keep_track);
551         }
552       }
553     }
554     XBT_DEBUG("Constraint Usage '%d' : %f", cnst->id_int, cnst->usage);
555     /* Saturated constraints update */
556     saturated_constraint_set_update(sys, cnst, &min_usage);
557   }
558   saturated_variable_set_update(sys);
559
560   /* Saturated variables update */
561
562   do {
563     /* Fix the variables that have to be */
564     var_list = &(sys->saturated_variable_set);
565
566     xbt_swag_foreach(var, var_list) {
567       if (var->weight <= 0.0)
568         DIE_IMPOSSIBLE;
569       /* First check if some of these variables have reach their upper
570          bound and update min_usage accordingly. */
571       XBT_DEBUG
572           ("var=%d, var->bound=%f, var->weight=%f, min_usage=%f, var->bound*var->weight=%f",
573            var->id_int, var->bound, var->weight, min_usage,
574            var->bound * var->weight);
575       if ((var->bound > 0) && (var->bound * var->weight < min_usage)) {
576         if (min_bound < 0)
577           min_bound = var->bound;
578         else
579           min_bound = MIN(min_bound, var->bound);
580         XBT_DEBUG("Updated min_bound=%f", min_bound);
581       }
582     }
583
584
585     while ((var = xbt_swag_getFirst(var_list))) {
586       int i;
587
588       if (min_bound < 0) {
589         var->value = min_usage / var->weight;
590       } else {
591         if (min_bound == var->bound)
592           var->value = var->bound;
593         else {
594           xbt_swag_remove(var, var_list);
595           continue;
596         }
597       }
598       XBT_DEBUG("Min usage: %f, Var(%d)->weight: %f, Var(%d)->value: %f ",
599              min_usage, var->id_int, var->weight, var->id_int, var->value);
600
601
602       /* Update usage */
603
604       for (i = 0; i < var->cnsts_number; i++) {
605         elem = &var->cnsts[i];
606         cnst = elem->constraint;
607         if (cnst->shared) {
608           double_update(&(cnst->remaining), elem->value * var->value);
609           double_update(&(cnst->usage), elem->value / var->weight);
610           make_elem_inactive(elem);
611         } else {                /* FIXME one day: We recompute usage.... :( */
612           cnst->usage = 0.0;
613           make_elem_inactive(elem);
614           elem_list = &(cnst->element_set);
615           xbt_swag_foreach(elem, elem_list) {
616             if (elem->variable->weight <= 0)
617               break;
618             if (elem->variable->value > 0)
619               break;
620             if ((elem->value > 0)) {
621               cnst->usage =
622                   MAX(cnst->usage, elem->value / elem->variable->weight);
623               XBT_DEBUG("Constraint Usage %d : %f", cnst->id_int,
624                      cnst->usage);
625               make_elem_active(elem);
626             }
627           }
628         }
629       }
630       xbt_swag_remove(var, var_list);
631     }
632
633     /* Find out which variables reach the maximum */
634     cnst_list =
635         sys->selective_update_active ? &(sys->modified_constraint_set) :
636         &(sys->active_constraint_set);
637     min_usage = -1;
638     min_bound = -1;
639     xbt_swag_foreach(cnst, cnst_list) {
640       saturated_constraint_set_update(sys, cnst, &min_usage);
641     }
642     saturated_variable_set_update(sys);
643
644   } while (xbt_swag_size(&(sys->saturated_variable_set)));
645
646   sys->modified = 0;
647   if (sys->selective_update_active)
648     lmm_remove_all_modified_set(sys);
649
650   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
651     lmm_print(sys);
652   }
653   XBT_OUT();
654 }
655
656 /* Not a O(1) function */
657
658 void lmm_update(lmm_system_t sys, lmm_constraint_t cnst,
659                 lmm_variable_t var, double value)
660 {
661   int i;
662
663   for (i = 0; i < var->cnsts_number; i++)
664     if (var->cnsts[i].constraint == cnst) {
665       var->cnsts[i].value = value;
666       sys->modified = 1;
667       lmm_update_modified_set(sys, cnst);
668       return;
669     }
670 }
671
672 /** \brief Attribute the value bound to var->bound.
673  * 
674  *  \param sys the lmm_system_t
675  *  \param var the lmm_variable_t
676  *  \param bound the new bound to associate with var
677  * 
678  *  Makes var->bound equal to bound. Whenever this function is called 
679  *  a change is  signed in the system. To
680  *  avoid false system changing detection it is a good idea to test 
681  *  (bound != 0) before calling it.
682  *
683  */
684 void lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
685                                double bound)
686 {
687   int i;
688
689   sys->modified = 1;
690   var->bound = bound;
691
692   for (i = 0; i < var->cnsts_number; i++)
693     lmm_update_modified_set(sys, var->cnsts[i].constraint);
694
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     lmm_update_modified_set(sys, elem->constraint);
724   }
725   if (!weight)
726     var->value = 0.0;
727
728   XBT_OUT();
729 }
730
731 XBT_INLINE double lmm_get_variable_weight(lmm_variable_t var)
732 {
733   return var->weight;
734 }
735
736 XBT_INLINE void lmm_update_constraint_bound(lmm_system_t sys,
737                                             lmm_constraint_t cnst,
738                                             double bound)
739 {
740   sys->modified = 1;
741   lmm_update_modified_set(sys, cnst);
742   cnst->bound = bound;
743 }
744
745 XBT_INLINE int lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst)
746 {
747   return xbt_swag_belongs(cnst, &(sys->active_constraint_set));
748 }
749
750 XBT_INLINE lmm_constraint_t lmm_get_first_active_constraint(lmm_system_t
751                                                             sys)
752 {
753   return xbt_swag_getFirst(&(sys->active_constraint_set));
754 }
755
756 XBT_INLINE lmm_constraint_t lmm_get_next_active_constraint(lmm_system_t
757                                                            sys,
758                                                            lmm_constraint_t
759                                                            cnst)
760 {
761   return xbt_swag_getNext(cnst, (sys->active_constraint_set).offset);
762 }
763
764 #ifdef HAVE_LATENCY_BOUND_TRACKING
765 XBT_INLINE int lmm_is_variable_limited_by_latency(lmm_variable_t var)
766 {
767   return (double_equals(var->bound, var->value));
768 }
769 #endif
770
771
772 /** \brief Update the constraint set propagating recursively to
773  *  other constraints so the system should not be entirely computed.
774  *
775  *  \param sys the lmm_system_t
776  *  \param cnst the lmm_constraint_t affected by the change
777  *
778  *  A recursive algorithm to optimize the system recalculation selecting only
779  *  constraints that have changed. Each constraint change is propagated
780  *  to the list of constraints for each variable.
781  */
782 static void lmm_update_modified_set(lmm_system_t sys,
783                                     lmm_constraint_t cnst)
784 {
785   lmm_element_t elem = NULL;
786   lmm_variable_t var = NULL;
787   xbt_swag_t elem_list = NULL;
788   int i;
789
790   /* return if selective update isn't active */
791   if (!sys->selective_update_active)
792     return;
793
794   //XBT_DEBUG("Updating modified constraint set with constraint %d", cnst->id_int);
795
796   if (xbt_swag_belongs(cnst, &(sys->modified_constraint_set)))
797     return;
798
799   //XBT_DEBUG("Inserting into modified constraint set %d", cnst->id_int);
800
801   /* add to modified set */
802   xbt_swag_insert(cnst, &(sys->modified_constraint_set));
803
804   elem_list = &(cnst->element_set);
805   xbt_swag_foreach(elem, elem_list) {
806     var = elem->variable;
807     for (i = 0; i < var->cnsts_number; i++)
808       if (cnst != var->cnsts[i].constraint) {
809         //XBT_DEBUG("Updating modified %d calling for %d", cnst->id_int, var->cnsts[i].constraint->id_int);
810         lmm_update_modified_set(sys, var->cnsts[i].constraint);
811       }
812   }
813 }
814
815 /** \brief Remove all constraints of the modified_constraint_set.
816  *
817  *  \param sys the lmm_system_t
818  */
819 static void lmm_remove_all_modified_set(lmm_system_t sys)
820 {
821   lmm_element_t elem = NULL;
822   lmm_element_t elem_next = NULL;
823   xbt_swag_t elem_list = NULL;
824
825   elem_list = &(sys->modified_constraint_set);
826   xbt_swag_foreach_safe(elem, elem_next, elem_list) {
827     xbt_swag_remove(elem, elem_list);
828   }
829 }