Logo AND Algorithmique Numérique Distribuée

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