Logo AND Algorithmique Numérique Distribuée

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