Logo AND Algorithmique Numérique Distribuée

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