Logo AND Algorithmique Numérique Distribuée

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