Logo AND Algorithmique Numérique Distribuée

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