Logo AND Algorithmique Numérique Distribuée

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