Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
876fa33d9caace71001dc53317578c7ea9a1effc
[simgrid.git] / src / surf / maxmin.cpp
1 /* Copyright (c) 2004-2013. 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.hpp"
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 = (lmm_variable_t) 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 = (lmm_constraint_t) extract_constraint(sys)))
90     lmm_cnst_free(sys, cnst);
91
92   xbt_mallocator_free(sys->variable_mallocator);
93   free(sys);
94 }
95
96 static 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 void lmm_constraint_shared(lmm_constraint_t cnst)
167 {
168   cnst->shared = 0;
169 }
170
171 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 = (lmm_variable_t) xbt_mallocator_get(sys->variable_mallocator);
207   var->id = id;
208   var->id_int = Global_debug_id++;
209   var->cnsts = (s_lmm_element_t *) 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 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_DEBUG("cnst %p is not found in var %p", cnst, var);
305     return;
306   }
307
308   sys->modified = 1;
309
310   XBT_DEBUG("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 void lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
346                 lmm_variable_t var, double value)
347 {
348   lmm_element_t elem = NULL;
349
350   sys->modified = 1;
351
352   xbt_assert(var->cnsts_number < var->cnsts_size, "Too much constraints");
353
354   elem = &(var->cnsts[var->cnsts_number++]);
355
356   elem->value = value;
357   elem->constraint = cnst;
358   elem->variable = var;
359
360   if (var->weight)
361     xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
362   else
363     xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
364   if(!sys->selective_update_active) {
365     make_constraint_active(sys, cnst);
366   } else if(elem->value>0 || var->weight >0) {
367     make_constraint_active(sys, cnst);
368     lmm_update_modified_set(sys, cnst);
369     if (var->cnsts_number > 1)
370       lmm_update_modified_set(sys, var->cnsts[0].constraint);
371   }
372 }
373
374 void lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst,
375                     lmm_variable_t var, double value)
376 {
377   int i;
378   sys->modified = 1;
379
380   for (i = 0; i < var->cnsts_number; i++)
381     if (var->cnsts[i].constraint == cnst)
382       break;
383
384   if (i < var->cnsts_number) {
385     if (cnst->shared)
386       var->cnsts[i].value += value;
387     else
388       var->cnsts[i].value = MAX(var->cnsts[i].value, value);
389     lmm_update_modified_set(sys, cnst);
390   } else
391     lmm_expand(sys, cnst, var, value);
392 }
393
394 void lmm_elem_set_value(lmm_system_t sys, lmm_constraint_t cnst,
395                         lmm_variable_t var, double value)
396 {
397   int i;
398
399   for (i = 0; i < var->cnsts_number; i++)
400     if (var->cnsts[i].constraint == cnst)
401       break;
402
403   if (i < var->cnsts_number) {
404     var->cnsts[i].value = value;
405     sys->modified = 1;
406     lmm_update_modified_set(sys, cnst);
407   } else
408     DIE_IMPOSSIBLE;
409 }
410
411 lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t /*sys*/,
412                                                   lmm_variable_t var,
413                                                   int num)
414 {
415   if (num < var->cnsts_number)
416     return (var->cnsts[num].constraint);
417   else
418     return NULL;
419 }
420
421 double lmm_get_cnst_weight_from_var(lmm_system_t /*sys*/,
422                                                          lmm_variable_t var,
423                                                          int num)
424 {
425   if (num < var->cnsts_number)
426     return (var->cnsts[num].value);
427   else
428     return 0.0;
429 }
430
431 int lmm_get_number_of_cnst_from_var(lmm_system_t /*sys*/,
432                                                lmm_variable_t var)
433 {
434   return (var->cnsts_number);
435 }
436
437 lmm_variable_t lmm_get_var_from_cnst(lmm_system_t /*sys*/,
438                                      lmm_constraint_t cnst,
439                                      lmm_element_t * elem)
440 {
441   if (!(*elem))
442     *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->element_set));
443   else
444     *elem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->element_set.offset);
445   if (*elem)
446     return (*elem)->variable;
447   else
448     return NULL;
449 }
450
451 void *lmm_constraint_id(lmm_constraint_t cnst)
452 {
453   return cnst->id;
454 }
455
456 void *lmm_variable_id(lmm_variable_t var)
457 {
458   return var->id;
459 }
460
461 static XBT_INLINE void saturated_constraint_set_update(double usage,
462                                                       int cnst_light_num,
463                                                       dyn_light_t saturated_constraint_set,
464                                                       double *min_usage)
465 {
466   xbt_assert(usage > 0,"Impossible");
467
468   if (*min_usage < 0 || *min_usage > usage) {
469     *min_usage = usage;
470     // XBT_HERE(" min_usage=%f (cnst->remaining / cnst->usage =%f)", *min_usage, usage);
471     saturated_constraint_set->data[0] = cnst_light_num;
472     saturated_constraint_set->pos = 1;
473   } else if (*min_usage == usage) {
474     if(saturated_constraint_set->pos == saturated_constraint_set->size) { // realloc the size
475       saturated_constraint_set->size *= 2;
476       saturated_constraint_set->data = (int*) xbt_realloc(saturated_constraint_set->data, (saturated_constraint_set->size) * sizeof(int));
477     }
478     saturated_constraint_set->data[saturated_constraint_set->pos] = cnst_light_num;
479     saturated_constraint_set->pos++;
480   }
481 }
482
483 static XBT_INLINE void saturated_variable_set_update(
484     s_lmm_constraint_light_t *cnst_light_tab,
485     dyn_light_t saturated_constraint_set,
486     lmm_system_t sys)
487 {
488   lmm_constraint_light_t cnst = NULL;
489   void *_elem;
490   lmm_element_t elem = NULL;
491   xbt_swag_t elem_list = NULL;
492   int i;
493   for(i = 0; i< saturated_constraint_set->pos; i++){
494     cnst = &cnst_light_tab[saturated_constraint_set->data[i]];
495     elem_list = &(cnst->cnst->active_element_set);
496     xbt_swag_foreach(_elem, elem_list) {
497       elem = (lmm_element_t)_elem;
498       if (elem->variable->weight <= 0)
499         break;
500       if ((elem->value > 0))
501         xbt_swag_insert(elem->variable, &(sys->saturated_variable_set));
502     }
503   }
504 }
505
506 void lmm_print(lmm_system_t sys)
507 {
508   void *_cnst, *_elem, *_var;
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 = (char*) 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 = (char*)
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         var = (lmm_variable_t)_var;
527     sprintf(print_buf, "'%d'(%f) ", var->id_int, var->weight);
528     trace_buf = (char*)
529         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
530     strcat(trace_buf, print_buf);
531   }
532   sprintf(print_buf, ")");
533   trace_buf = (char*)
534       xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
535   strcat(trace_buf, print_buf);
536   XBT_DEBUG("%20s", trace_buf);
537   trace_buf[0] = '\000';
538
539   XBT_DEBUG("Constraints");
540   /* Printing Constraints */
541   cnst_list = &(sys->active_constraint_set);
542   xbt_swag_foreach(_cnst, cnst_list) {
543         cnst = (lmm_constraint_t)_cnst;
544     sum = 0.0;
545     elem_list = &(cnst->element_set);
546     sprintf(print_buf, "\t");
547     trace_buf = (char*)
548         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
549     strcat(trace_buf, print_buf);
550     sprintf(print_buf, "%s(",(cnst->shared)?"":"max");
551     trace_buf = (char*)
552       xbt_realloc(trace_buf,
553       strlen(trace_buf) + strlen(print_buf) + 1);
554     strcat(trace_buf, print_buf);      
555     xbt_swag_foreach(_elem, elem_list) {
556       elem = (lmm_element_t)_elem;
557       sprintf(print_buf, "%f.'%d'(%f) %s ", elem->value,
558               elem->variable->id_int, elem->variable->value,(cnst->shared)?"+":",");
559       trace_buf = (char*)
560           xbt_realloc(trace_buf,
561                       strlen(trace_buf) + strlen(print_buf) + 1);
562       strcat(trace_buf, print_buf);
563       if(cnst->shared) 
564   sum += elem->value * elem->variable->value;
565       else 
566   sum = MAX(sum,elem->value * elem->variable->value);
567     }
568     sprintf(print_buf, "0) <= %f ('%d')", cnst->bound, cnst->id_int);
569     trace_buf = (char*)
570         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
571     strcat(trace_buf, print_buf);
572
573     if (!cnst->shared) {
574       sprintf(print_buf, " [MAX-Constraint]");
575       trace_buf = (char*)
576           xbt_realloc(trace_buf,
577                       strlen(trace_buf) + strlen(print_buf) + 1);
578       strcat(trace_buf, print_buf);
579     }
580     XBT_DEBUG("%s", trace_buf);
581     trace_buf[0] = '\000';
582     xbt_assert(!double_positive(sum - cnst->bound),
583                 "Incorrect value (%f is not smaller than %f): %g",
584                 sum, cnst->bound, sum - cnst->bound);
585   }
586
587   XBT_DEBUG("Variables");
588   /* Printing Result */
589   xbt_swag_foreach(_var, var_list) {
590         var = (lmm_variable_t)_var;
591     if (var->bound > 0) {
592       XBT_DEBUG("'%d'(%f) : %f (<=%f)", var->id_int, var->weight, var->value,
593              var->bound);
594       xbt_assert(!double_positive(var->value - var->bound),
595                   "Incorrect value (%f is not smaller than %f",
596                   var->value, var->bound);
597     } else {
598       XBT_DEBUG("'%d'(%f) : %f", var->id_int, var->weight, var->value);
599     }
600   }
601
602   free(trace_buf);
603 }
604
605 void lmm_solve(lmm_system_t sys)
606 {
607   void *_var, *_cnst, *_cnst_next, *_elem;
608   lmm_variable_t var = NULL;
609   lmm_constraint_t cnst = NULL;
610   lmm_element_t elem = NULL;
611   xbt_swag_t cnst_list = NULL;
612   xbt_swag_t var_list = NULL;
613   xbt_swag_t elem_list = NULL;
614   double min_usage = -1;
615   double min_bound = -1;
616
617   if (!(sys->modified))
618     return;
619
620   XBT_IN("(sys=%p)", sys);
621
622   /*
623    * Compute Usage and store the variables that reach the maximum.
624    */
625   cnst_list =
626       sys->
627       selective_update_active ? &(sys->modified_constraint_set) :
628       &(sys->active_constraint_set);
629
630   XBT_DEBUG("Active constraints : %d", xbt_swag_size(cnst_list));
631   /* Init: Only modified code portions */
632   xbt_swag_foreach(_cnst, cnst_list) {
633         cnst = (lmm_constraint_t)_cnst;
634     elem_list = &(cnst->element_set);
635     //XBT_DEBUG("Variable set : %d", xbt_swag_size(elem_list));
636     xbt_swag_foreach(_elem, elem_list) {
637       var = ((lmm_element_t)_elem)->variable;
638       if (var->weight <= 0.0)
639         break;
640       var->value = 0.0;
641     }
642   }
643
644   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));
645   int cnst_light_num = 0;
646   dyn_light_t saturated_constraint_set = xbt_new0(s_dyn_light_t,1);
647   saturated_constraint_set->size = 5;
648   saturated_constraint_set->data = xbt_new0(int, saturated_constraint_set->size);
649
650   xbt_swag_foreach_safe(_cnst, _cnst_next, cnst_list) {
651         cnst = (lmm_constraint_t)_cnst;
652     /* INIT */
653     cnst->remaining = cnst->bound;
654     if (cnst->remaining == 0)
655       continue;
656     cnst->usage = 0;
657     elem_list = &(cnst->element_set);
658     xbt_swag_foreach(_elem, elem_list) {
659       elem = (lmm_element_t)_elem;
660       /* 0-weighted elements (ie, sleep actions) are at the end of the swag and we don't want to consider them */
661       if (elem->variable->weight <= 0)
662         break;
663       if ((elem->value > 0)) {
664         if (cnst->shared)
665           cnst->usage += elem->value / elem->variable->weight;
666         else if (cnst->usage < elem->value / elem->variable->weight)
667           cnst->usage = elem->value / elem->variable->weight;
668
669         make_elem_active(elem);
670         ActionLmmPtr action = static_cast<ActionLmmPtr>(elem->variable->id);
671         if (sys->keep_track && !action->is_linked())
672           sys->keep_track->push_back(*action);
673       }
674     }
675     XBT_DEBUG("Constraint Usage '%d' : %f", cnst->id_int, cnst->usage);
676     /* Saturated constraints update */
677
678     if(cnst->usage > 0) {
679       cnst_light_tab[cnst_light_num].cnst = cnst;
680       cnst->cnst_light = &(cnst_light_tab[cnst_light_num]);
681       cnst_light_tab[cnst_light_num].remaining_over_usage = cnst->remaining / cnst->usage;
682       saturated_constraint_set_update(cnst_light_tab[cnst_light_num].remaining_over_usage,
683         cnst_light_num, saturated_constraint_set, &min_usage);
684       cnst_light_num++;
685     }
686   }
687
688   saturated_variable_set_update(  cnst_light_tab,
689                                   saturated_constraint_set,
690                                   sys);
691
692   /* Saturated variables update */
693
694   do {
695     /* Fix the variables that have to be */
696     var_list = &(sys->saturated_variable_set);
697
698     xbt_swag_foreach(_var, var_list) {
699       var = (lmm_variable_t)_var;
700       if (var->weight <= 0.0)
701         DIE_IMPOSSIBLE;
702       /* First check if some of these variables have reach their upper
703          bound and update min_usage accordingly. */
704       XBT_DEBUG
705           ("var=%d, var->bound=%f, var->weight=%f, min_usage=%f, var->bound*var->weight=%f",
706            var->id_int, var->bound, var->weight, min_usage,
707            var->bound * var->weight);
708       if ((var->bound > 0) && (var->bound * var->weight < min_usage)) {
709         if (min_bound < 0)
710           min_bound = var->bound;
711         else
712           min_bound = MIN(min_bound, var->bound);
713         XBT_DEBUG("Updated min_bound=%f", min_bound);
714       }
715     }
716
717
718     while ((var = (lmm_variable_t)xbt_swag_getFirst(var_list))) {
719       int i;
720
721       if (min_bound < 0) {
722         var->value = min_usage / var->weight;
723       } else {
724         if (min_bound == var->bound)
725           var->value = var->bound;
726         else {
727           xbt_swag_remove(var, var_list);
728           continue;
729         }
730       }
731       XBT_DEBUG("Min usage: %f, Var(%d)->weight: %f, Var(%d)->value: %f ",
732              min_usage, var->id_int, var->weight, var->id_int, var->value);
733
734
735       /* Update usage */
736
737       for (i = 0; i < var->cnsts_number; i++) {
738         elem = &var->cnsts[i];
739         cnst = elem->constraint;
740         if (cnst->shared) {
741           double_update(&(cnst->remaining), elem->value * var->value);
742           double_update(&(cnst->usage), elem->value / var->weight);
743           if(cnst->usage<=0 || cnst->remaining<=0) {
744             if (cnst->cnst_light) {
745               int index = (cnst->cnst_light-cnst_light_tab);
746               XBT_DEBUG("index: %d \t cnst_light_num: %d \t || \t cnst: %p \t cnst->cnst_light: %p \t cnst_light_tab: %p ",
747                   index,cnst_light_num, cnst, cnst->cnst_light, cnst_light_tab);
748               cnst_light_tab[index]=cnst_light_tab[cnst_light_num-1];
749               cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
750               cnst_light_num--;
751               cnst->cnst_light = NULL;
752             }
753           } else {
754             cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
755           }
756           make_elem_inactive(elem);
757         } else {
758           cnst->usage = 0.0;
759           make_elem_inactive(elem);
760           elem_list = &(cnst->element_set);
761           xbt_swag_foreach(_elem, elem_list) {
762                 elem = (lmm_element_t)_elem;
763             if (elem->variable->weight <= 0 || elem->variable->value > 0)
764               break;
765             if (elem->value > 0)
766               cnst->usage = MAX(cnst->usage, elem->value / elem->variable->weight);
767           }
768           if (cnst->usage<=0 || cnst->remaining<=0) {
769             if(cnst->cnst_light) {
770               int index = (cnst->cnst_light-cnst_light_tab);
771               XBT_DEBUG("index: %d \t cnst_light_num: %d \t || \t cnst: %p \t cnst->cnst_light: %p \t cnst_light_tab: %p ",
772                   index,cnst_light_num, cnst, cnst->cnst_light, cnst_light_tab);
773               cnst_light_tab[index]=cnst_light_tab[cnst_light_num-1];
774               cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
775               cnst_light_num--;
776               cnst->cnst_light = NULL;
777             }
778           } else {
779             cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
780           }
781         }
782       }
783       xbt_swag_remove(var, var_list);
784     }
785
786     /* Find out which variables reach the maximum */
787     min_usage = -1;
788     min_bound = -1;
789     saturated_constraint_set->pos = 0;
790     int pos;
791     for(pos=0; pos<cnst_light_num; pos++)
792       saturated_constraint_set_update(
793           cnst_light_tab[pos].remaining_over_usage,
794           pos,
795           saturated_constraint_set,
796           &min_usage);
797
798     saturated_variable_set_update(  cnst_light_tab,
799                                     saturated_constraint_set,
800                                     sys);
801
802   } while (cnst_light_num > 0);
803
804   sys->modified = 0;
805   if (sys->selective_update_active)
806     lmm_remove_all_modified_set(sys);
807
808   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
809     lmm_print(sys);
810   }
811
812   xbt_free(saturated_constraint_set->data);
813   xbt_free(saturated_constraint_set);
814   xbt_free(cnst_light_tab);
815   XBT_OUT();
816 }
817
818 /* Not a O(1) function */
819
820 void lmm_update(lmm_system_t sys, lmm_constraint_t cnst,
821                 lmm_variable_t var, double value)
822 {
823   int i;
824
825   for (i = 0; i < var->cnsts_number; i++)
826     if (var->cnsts[i].constraint == cnst) {
827       var->cnsts[i].value = value;
828       sys->modified = 1;
829       lmm_update_modified_set(sys, cnst);
830       return;
831     }
832 }
833
834 /** \brief Attribute the value bound to var->bound.
835  * 
836  *  \param sys the lmm_system_t
837  *  \param var the lmm_variable_t
838  *  \param bound the new bound to associate with var
839  * 
840  *  Makes var->bound equal to bound. Whenever this function is called 
841  *  a change is  signed in the system. To
842  *  avoid false system changing detection it is a good idea to test 
843  *  (bound != 0) before calling it.
844  *
845  */
846 void lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
847                                double bound)
848 {
849   sys->modified = 1;
850   var->bound = bound;
851
852   if (var->cnsts_number)
853     lmm_update_modified_set(sys, var->cnsts[0].constraint);
854 }
855
856
857 void lmm_update_variable_weight(lmm_system_t sys, lmm_variable_t var,
858                                 double weight)
859 {
860   int i;
861   lmm_element_t elem;
862
863   if (weight == var->weight)
864     return;
865   XBT_IN("(sys=%p, var=%p, weight=%f)", sys, var, weight);
866   sys->modified = 1;
867   var->weight = weight;
868   xbt_swag_remove(var, &(sys->variable_set));
869   if (weight)
870     xbt_swag_insert_at_head(var, &(sys->variable_set));
871   else
872     xbt_swag_insert_at_tail(var, &(sys->variable_set));
873
874   for (i = 0; i < var->cnsts_number; i++) {
875     elem = &var->cnsts[i];
876     xbt_swag_remove(elem, &(elem->constraint->element_set));
877     if (weight)
878       xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
879     else
880       xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
881
882     if (i == 0)
883       lmm_update_modified_set(sys, elem->constraint);
884   }
885   if (!weight)
886     var->value = 0.0;
887
888   XBT_OUT();
889 }
890
891 double lmm_get_variable_weight(lmm_variable_t var)
892 {
893   return var->weight;
894 }
895
896 void lmm_update_constraint_bound(lmm_system_t sys,
897                                             lmm_constraint_t cnst,
898                                             double bound)
899 {
900   sys->modified = 1;
901   lmm_update_modified_set(sys, cnst);
902   cnst->bound = bound;
903 }
904
905 int lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst)
906 {
907   return xbt_swag_belongs(cnst, &(sys->active_constraint_set));
908 }
909
910 XBT_INLINE lmm_constraint_t lmm_get_first_active_constraint(lmm_system_t
911                                                             sys)
912 {
913   return (lmm_constraint_t)xbt_swag_getFirst(&(sys->active_constraint_set));
914 }
915
916 XBT_INLINE lmm_constraint_t lmm_get_next_active_constraint(lmm_system_t
917                                                            sys,
918                                                            lmm_constraint_t
919                                                            cnst)
920 {
921   return (lmm_constraint_t)xbt_swag_getNext(cnst, (sys->active_constraint_set).offset);
922 }
923
924 #ifdef HAVE_LATENCY_BOUND_TRACKING
925 XBT_INLINE int lmm_is_variable_limited_by_latency(lmm_variable_t var)
926 {
927   return (double_equals(var->bound, var->value));
928 }
929 #endif
930
931
932 /** \brief Update the constraint set propagating recursively to
933  *  other constraints so the system should not be entirely computed.
934  *
935  *  \param sys the lmm_system_t
936  *  \param cnst the lmm_constraint_t affected by the change
937  *
938  *  A recursive algorithm to optimize the system recalculation selecting only
939  *  constraints that have changed. Each constraint change is propagated
940  *  to the list of constraints for each variable.
941  */
942 static void lmm_update_modified_set_rec(lmm_system_t sys,
943                                         lmm_constraint_t cnst)
944 {
945   void* _elem;
946
947   xbt_swag_foreach(_elem, &cnst->element_set) {
948     lmm_variable_t var = ((lmm_element_t)_elem)->variable;
949     s_lmm_element_t *cnsts = var->cnsts;
950     int i;
951     for (i = 0; var->visited != sys->visited_counter
952              && i < var->cnsts_number ; i++) {
953       if (cnsts[i].constraint != cnst
954           && !xbt_swag_belongs(cnsts[i].constraint,
955                                &sys->modified_constraint_set)) {
956         xbt_swag_insert(cnsts[i].constraint, &sys->modified_constraint_set);
957         lmm_update_modified_set_rec(sys, cnsts[i].constraint);
958       }
959     }
960     var->visited = sys->visited_counter;
961   }
962 }
963
964 static void lmm_update_modified_set(lmm_system_t sys,
965                                     lmm_constraint_t cnst)
966 {
967   /* nothing to do if selective update isn't active */
968   if (sys->selective_update_active
969       && !xbt_swag_belongs(cnst, &sys->modified_constraint_set)) {
970     xbt_swag_insert(cnst, &sys->modified_constraint_set);
971     lmm_update_modified_set_rec(sys, cnst);
972   }
973 }
974
975 /** \brief Remove all constraints of the modified_constraint_set.
976  *
977  *  \param sys the lmm_system_t
978  */
979 static void lmm_remove_all_modified_set(lmm_system_t sys)
980 {
981   if (++sys->visited_counter == 1) {
982     /* the counter wrapped around, reset each variable->visited */
983         void *_var;
984     xbt_swag_foreach(_var, &sys->variable_set)
985       ((lmm_variable_t)_var)->visited = 0;
986   }
987   xbt_swag_reset(&sys->modified_constraint_set);
988 }
989
990 /**
991  *  Returns total resource load
992  *
993  *  \param cnst the lmm_constraint_t associated to the resource
994  *
995  */
996 double lmm_constraint_get_usage(lmm_constraint_t cnst) {
997    double usage = 0.0;
998    xbt_swag_t elem_list = &(cnst->element_set);
999    void *_elem;
1000    lmm_element_t elem = NULL;
1001
1002    xbt_swag_foreach(_elem, elem_list) {
1003          elem = (lmm_element_t)_elem;
1004      /* 0-weighted elements (ie, sleep actions) are at the end of the swag and we don't want to consider them */
1005      if (elem->variable->weight <= 0)
1006        break;
1007      if ((elem->value > 0)) {
1008        if (cnst->shared)
1009          usage += elem->value * elem->variable->value;
1010        else if (usage < elem->value * elem->variable->value)
1011          usage = elem->value * elem->variable->value;
1012      }
1013    }
1014   return usage;
1015 }
1016
1017