Logo AND Algorithmique Numérique Distribuée

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