Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c675ce0b265c801f027b41766d7a65f3dcfa788b
[simgrid.git] / src / surf / maxmin.cpp
1 /* Copyright (c) 2004-2015. 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 double sg_maxmin_precision = 0.00001;
24 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",
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->sharing_policy = 1; /* FIXME: don't hardcode the value */
161   insert_constraint(sys, cnst);
162
163   return cnst;
164 }
165
166 void lmm_constraint_shared(lmm_constraint_t cnst)
167 {
168   cnst->sharing_policy = 0;
169 }
170
171 /** Return true if the constraint is shared, and false if it's FATPIPE */
172 int lmm_constraint_sharing_policy(lmm_constraint_t cnst)
173 {
174   return (cnst->sharing_policy);
175 }
176
177 XBT_INLINE void lmm_constraint_free(lmm_system_t sys,
178                                     lmm_constraint_t cnst)
179 {
180   remove_constraint(sys, cnst);
181   lmm_cnst_free(sys, cnst);
182 }
183
184 static void *lmm_variable_mallocator_new_f(void)
185 {
186   lmm_variable_t var = xbt_new(s_lmm_variable_t, 1);
187   var->cnsts = NULL; /* will be created by realloc */
188   return var;
189 }
190
191 static void lmm_variable_mallocator_free_f(void *var)
192 {
193   xbt_free(((lmm_variable_t) var)->cnsts);
194   xbt_free(var);
195 }
196
197 lmm_variable_t lmm_variable_new(lmm_system_t sys, void *id,
198                                 double weight,
199                                 double bound, int number_of_constraints)
200 {
201   lmm_variable_t var = NULL;
202   int i;
203
204   XBT_IN("(sys=%p, id=%p, weight=%f, bound=%f, num_cons =%d)",
205           sys, id, weight, bound, number_of_constraints);
206
207   var = (lmm_variable_t) xbt_mallocator_get(sys->variable_mallocator);
208   var->id = id;
209   var->id_int = Global_debug_id++;
210   var->cnsts = (s_lmm_element_t *) xbt_realloc(var->cnsts, number_of_constraints * sizeof(s_lmm_element_t));
211   for (i = 0; i < number_of_constraints; i++) {
212     var->cnsts[i].element_set_hookup.next = NULL;
213     var->cnsts[i].element_set_hookup.prev = NULL;
214     var->cnsts[i].active_element_set_hookup.next = NULL;
215     var->cnsts[i].active_element_set_hookup.prev = NULL;
216     var->cnsts[i].constraint = NULL;
217     var->cnsts[i].variable = NULL;
218     var->cnsts[i].value = 0.0;
219   }
220   var->cnsts_size = number_of_constraints;
221   var->cnsts_number = 0;
222   var->weight = weight;
223   var->bound = bound;
224   var->value = 0.0;
225   var->visited = sys->visited_counter - 1;
226   var->mu = 0.0;
227   var->new_mu = 0.0;
228   var->func_f = func_f_def;
229   var->func_fp = func_fp_def;
230   var->func_fpi = func_fpi_def;
231
232   var->variable_set_hookup.next = NULL;
233   var->variable_set_hookup.prev = NULL;
234   var->saturated_variable_set_hookup.next = NULL;
235   var->saturated_variable_set_hookup.prev = NULL;
236
237   if (weight)
238     xbt_swag_insert_at_head(var, &(sys->variable_set));
239   else
240     xbt_swag_insert_at_tail(var, &(sys->variable_set));
241
242   XBT_OUT(" returns %p", var);
243   return var;
244 }
245
246 void lmm_variable_free(lmm_system_t sys, lmm_variable_t var)
247 {
248   remove_variable(sys, var);
249   lmm_var_free(sys, var);
250 }
251
252 double lmm_variable_getvalue(lmm_variable_t var)
253 {
254   return (var->value);
255 }
256
257 double lmm_variable_getbound(lmm_variable_t var)
258 {
259   return (var->bound);
260 }
261
262 /* Replace the content of elem_a with elem_b. The content of elem_b is cleared. */
263 static void renew_elem_entry(lmm_element_t elem_a, lmm_element_t elem_b)
264 {
265     elem_a->constraint = elem_b->constraint;
266     elem_a->variable   = elem_b->variable;
267     elem_a->value      = elem_b->value;
268
269     /* If elem_b is in the element_set swag, register the new element to the swag. */
270     if (xbt_swag_remove(elem_b, &(elem_b->constraint->element_set))) {
271       if (elem_a->variable->weight)
272         xbt_swag_insert_at_head(elem_a, &(elem_a->constraint->element_set));
273       else
274         xbt_swag_insert_at_tail(elem_a, &(elem_a->constraint->element_set));
275     }
276
277     if (xbt_swag_remove(elem_b, &(elem_b->constraint->active_element_set))) {
278       if (elem_a->variable->weight)
279         xbt_swag_insert_at_head(elem_a, &(elem_a->constraint->active_element_set));
280       else
281         xbt_swag_insert_at_tail(elem_a, &(elem_a->constraint->active_element_set));
282     }
283
284     elem_b->constraint = NULL;
285     elem_b->variable   = NULL;
286     elem_b->value      = 0;
287 }
288
289 void lmm_shrink(lmm_system_t sys, lmm_constraint_t cnst,
290                 lmm_variable_t var)
291 {
292   lmm_element_t elem = NULL;
293   int found = 0;
294
295   int i;
296   for (i = 0; i < var->cnsts_number; i++) {
297     elem = &(var->cnsts[i]);
298     if (elem->constraint == cnst) {
299       found = 1;
300       break;
301     }
302   }
303
304   if (!found) {
305     XBT_DEBUG("cnst %p is not found in var %p", cnst, var);
306     return;
307   }
308
309   sys->modified = 1;
310
311   XBT_DEBUG("remove elem(value %f, cnst %p, var %p) in var %p",
312       elem->value, elem->constraint, elem->variable, var);
313
314
315
316   /* We are going to change the constraint object and the variable object.
317    * Propagate this change to other objects. Calling here (not after
318    * modification) is correct? */
319   lmm_update_modified_set(sys, cnst);
320   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].
321
322
323
324   /* now var->cnsts[i] is not necessary any more */
325
326   xbt_swag_remove(elem, &(elem->constraint->element_set));
327   xbt_swag_remove(elem, &(elem->constraint->active_element_set));
328   elem->constraint = NULL;
329   elem->variable = NULL;
330   elem->value = 0;
331
332
333
334   /* We do not want to have an empty element entry before the last entry. So,
335    * plug up the hole with the last one. */
336   if (i < var->cnsts_number - 1)
337     renew_elem_entry(&var->cnsts[i], &var->cnsts[var->cnsts_number - 1]);
338
339   var->cnsts_number -= 1;
340
341
342   if (xbt_swag_size(&(cnst->element_set)) == 0)
343     make_constraint_inactive(sys, cnst);
344 }
345
346 void lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
347                 lmm_variable_t var, double value)
348 {
349   lmm_element_t elem = NULL;
350
351   sys->modified = 1;
352
353   xbt_assert(var->cnsts_number < var->cnsts_size, "Too much constraints");
354
355   elem = &(var->cnsts[var->cnsts_number++]);
356
357   elem->value = value;
358   elem->constraint = cnst;
359   elem->variable = var;
360
361   if (var->weight)
362     xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
363   else
364     xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
365   if(!sys->selective_update_active) {
366     make_constraint_active(sys, cnst);
367   } else if(elem->value>0 || var->weight >0) {
368     make_constraint_active(sys, cnst);
369     lmm_update_modified_set(sys, cnst);
370     if (var->cnsts_number > 1)
371       lmm_update_modified_set(sys, var->cnsts[0].constraint);
372   }
373 }
374
375 void lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst,
376                     lmm_variable_t var, double value)
377 {
378   int i;
379   sys->modified = 1;
380
381   for (i = 0; i < var->cnsts_number; i++)
382     if (var->cnsts[i].constraint == cnst)
383       break;
384
385   if (i < var->cnsts_number) {
386     if (cnst->sharing_policy)
387       var->cnsts[i].value += value;
388     else
389       var->cnsts[i].value = MAX(var->cnsts[i].value, value);
390     lmm_update_modified_set(sys, cnst);
391   } else
392     lmm_expand(sys, cnst, var, value);
393 }
394
395 lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t /*sys*/,
396                                                   lmm_variable_t var,
397                                                   int num)
398 {
399   if (num < var->cnsts_number)
400     return (var->cnsts[num].constraint);
401   else
402     return NULL;
403 }
404
405 double lmm_get_cnst_weight_from_var(lmm_system_t /*sys*/,
406                                                          lmm_variable_t var,
407                                                          int num)
408 {
409   if (num < var->cnsts_number)
410     return (var->cnsts[num].value);
411   else
412     return 0.0;
413 }
414
415 int lmm_get_number_of_cnst_from_var(lmm_system_t /*sys*/,
416                                                lmm_variable_t var)
417 {
418   return (var->cnsts_number);
419 }
420
421 lmm_variable_t lmm_get_var_from_cnst(lmm_system_t /*sys*/,
422                                      lmm_constraint_t cnst,
423                                      lmm_element_t * elem)
424 {
425   if (!(*elem))
426     *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->element_set));
427   else
428     *elem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->element_set.offset);
429   if (*elem)
430     return (*elem)->variable;
431   else
432     return NULL;
433 }
434
435 //if we modify the swag between calls, normal version may loop forever
436 //this safe version ensures that we browse the swag elements only once
437 lmm_variable_t lmm_get_var_from_cnst_safe(lmm_system_t /*sys*/,
438                                      lmm_constraint_t cnst,
439                                      lmm_element_t * elem,
440                                      lmm_element_t * nextelem,
441                                      int * numelem)
442 {
443   if (!(*elem)){
444     *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->element_set));
445     *numelem = xbt_swag_size(&(cnst->element_set))-1;
446   }else{
447     *elem = *nextelem;
448     if(*numelem>0){
449      (*numelem) --;
450     }else
451       return NULL;
452   }
453   if (*elem){
454     *nextelem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->element_set.offset);
455     return (*elem)->variable;
456   }else
457     return NULL;
458 }
459
460 void *lmm_constraint_id(lmm_constraint_t cnst)
461 {
462   return cnst->id;
463 }
464
465 void *lmm_variable_id(lmm_variable_t var)
466 {
467   return var->id;
468 }
469
470 static XBT_INLINE void saturated_constraint_set_update(double usage,
471                                                       int cnst_light_num,
472                                                       dyn_light_t saturated_constraint_set,
473                                                       double *min_usage)
474 {
475   xbt_assert(usage > 0,"Impossible");
476
477   if (*min_usage < 0 || *min_usage > usage) {
478     *min_usage = usage;
479     // XBT_HERE(" min_usage=%f (cnst->remaining / cnst->usage =%f)", *min_usage, usage);
480     saturated_constraint_set->data[0] = cnst_light_num;
481     saturated_constraint_set->pos = 1;
482   } else if (*min_usage == usage) {
483     if(saturated_constraint_set->pos == saturated_constraint_set->size) { // realloc the size
484       saturated_constraint_set->size *= 2;
485       saturated_constraint_set->data = (int*) xbt_realloc(saturated_constraint_set->data, (saturated_constraint_set->size) * sizeof(int));
486     }
487     saturated_constraint_set->data[saturated_constraint_set->pos] = cnst_light_num;
488     saturated_constraint_set->pos++;
489   }
490 }
491
492 static XBT_INLINE void saturated_variable_set_update(
493     s_lmm_constraint_light_t *cnst_light_tab,
494     dyn_light_t saturated_constraint_set,
495     lmm_system_t sys)
496 {
497   /* Add active variables (i.e. variables that need to be set) from the set of constraints to saturate (cnst_light_tab)*/ 
498   lmm_constraint_light_t cnst = NULL;
499   void *_elem;
500   lmm_element_t elem = NULL;
501   xbt_swag_t elem_list = NULL;
502   int i;
503   for(i = 0; i< saturated_constraint_set->pos; i++){
504     cnst = &cnst_light_tab[saturated_constraint_set->data[i]];
505     elem_list = &(cnst->cnst->active_element_set);
506     xbt_swag_foreach(_elem, elem_list) {
507       elem = (lmm_element_t)_elem;
508       if (elem->variable->weight <= 0)
509         break;
510       if ((elem->value > 0))
511         xbt_swag_insert(elem->variable, &(sys->saturated_variable_set));
512     }
513   }
514 }
515
516 void lmm_print(lmm_system_t sys)
517 {
518   void *_cnst, *_elem, *_var;
519   lmm_constraint_t cnst = NULL;
520   lmm_element_t elem = NULL;
521   lmm_variable_t var = NULL;
522   xbt_swag_t cnst_list = NULL;
523   xbt_swag_t var_list = NULL;
524   xbt_swag_t elem_list = NULL;
525   char print_buf[1024];
526   char *trace_buf = (char*) xbt_malloc0(sizeof(char));
527   double sum = 0.0;
528
529   /* Printing Objective */
530   var_list = &(sys->variable_set);
531   sprintf(print_buf, "MAX-MIN ( ");
532   trace_buf = (char*)
533       xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
534   strcat(trace_buf, print_buf);
535   xbt_swag_foreach(_var, var_list) {
536         var = (lmm_variable_t)_var;
537     sprintf(print_buf, "'%d'(%f) ", var->id_int, var->weight);
538     trace_buf = (char*)
539         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
540     strcat(trace_buf, print_buf);
541   }
542   sprintf(print_buf, ")");
543   trace_buf = (char*)
544       xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
545   strcat(trace_buf, print_buf);
546   XBT_DEBUG("%20s", trace_buf);
547   trace_buf[0] = '\000';
548
549   XBT_DEBUG("Constraints");
550   /* Printing Constraints */
551   cnst_list = &(sys->active_constraint_set);
552   xbt_swag_foreach(_cnst, cnst_list) {
553         cnst = (lmm_constraint_t)_cnst;
554     sum = 0.0;
555     elem_list = &(cnst->element_set);
556     sprintf(print_buf, "\t");
557     trace_buf = (char*)
558         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
559     strcat(trace_buf, print_buf);
560     sprintf(print_buf, "%s(",(cnst->sharing_policy)?"":"max");
561     trace_buf = (char*)
562       xbt_realloc(trace_buf,
563       strlen(trace_buf) + strlen(print_buf) + 1);
564     strcat(trace_buf, print_buf);      
565     xbt_swag_foreach(_elem, elem_list) {
566       elem = (lmm_element_t)_elem;
567       sprintf(print_buf, "%f.'%d'(%f) %s ", elem->value,
568               elem->variable->id_int, elem->variable->value,(cnst->sharing_policy)?"+":",");
569       trace_buf = (char*)
570           xbt_realloc(trace_buf,
571                       strlen(trace_buf) + strlen(print_buf) + 1);
572       strcat(trace_buf, print_buf);
573       if(cnst->sharing_policy)
574           sum += elem->value * elem->variable->value;
575       else 
576           sum = MAX(sum,elem->value * elem->variable->value);
577     }
578     sprintf(print_buf, "0) <= %f ('%d')", cnst->bound, cnst->id_int);
579     trace_buf = (char*)
580         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
581     strcat(trace_buf, print_buf);
582
583     if (!cnst->sharing_policy) {
584       sprintf(print_buf, " [MAX-Constraint]");
585       trace_buf = (char*)
586           xbt_realloc(trace_buf,
587                       strlen(trace_buf) + strlen(print_buf) + 1);
588       strcat(trace_buf, print_buf);
589     }
590     XBT_DEBUG("%s", trace_buf);
591     trace_buf[0] = '\000';
592     xbt_assert(!double_positive(sum - cnst->bound, cnst->bound*sg_maxmin_precision),
593                 "Incorrect value (%f is not smaller than %f): %g",
594                 sum, cnst->bound, sum - cnst->bound);
595   }
596
597   XBT_DEBUG("Variables");
598   /* Printing Result */
599   xbt_swag_foreach(_var, var_list) {
600         var = (lmm_variable_t)_var;
601     if (var->bound > 0) {
602       XBT_DEBUG("'%d'(%f) : %f (<=%f)", var->id_int, var->weight, var->value,
603              var->bound);
604       xbt_assert(!double_positive(var->value - var->bound, var->bound*sg_maxmin_precision),
605                   "Incorrect value (%f is not smaller than %f",
606                   var->value, var->bound);
607     } else {
608       XBT_DEBUG("'%d'(%f) : %f", var->id_int, var->weight, var->value);
609     }
610   }
611
612   free(trace_buf);
613 }
614
615 void lmm_solve(lmm_system_t sys)
616 {
617   void *_var, *_cnst, *_cnst_next, *_elem;
618   lmm_variable_t var = NULL;
619   lmm_constraint_t cnst = NULL;
620   lmm_element_t elem = NULL;
621   xbt_swag_t cnst_list = NULL;
622   xbt_swag_t var_list = NULL;
623   xbt_swag_t elem_list = NULL;
624   double min_usage = -1;
625   double min_bound = -1;
626
627   if (!(sys->modified))
628     return;
629
630   XBT_IN("(sys=%p)", sys);
631
632   /*
633    * 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.
634    */
635   cnst_list =
636       sys->
637       selective_update_active ? &(sys->modified_constraint_set) :
638       &(sys->active_constraint_set);
639
640   XBT_DEBUG("Active constraints : %d", xbt_swag_size(cnst_list));
641   /* Init: Only modified code portions: reset the value of active variables */
642   xbt_swag_foreach(_cnst, cnst_list) {
643         cnst = (lmm_constraint_t)_cnst;
644     elem_list = &(cnst->element_set);
645     //XBT_DEBUG("Variable set : %d", xbt_swag_size(elem_list));
646     xbt_swag_foreach(_elem, elem_list) {
647       var = ((lmm_element_t)_elem)->variable;
648       if (var->weight <= 0.0)
649         break;
650       var->value = 0.0;
651     }
652   }
653
654   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));
655   int cnst_light_num = 0;
656   dyn_light_t saturated_constraint_set = xbt_new0(s_dyn_light_t,1);
657   saturated_constraint_set->size = 5;
658   saturated_constraint_set->data = xbt_new0(int, saturated_constraint_set->size);
659
660   xbt_swag_foreach_safe(_cnst, _cnst_next, cnst_list) {
661         cnst = (lmm_constraint_t)_cnst;
662     /* INIT: Collect constraints that actually need to be saturated (i.e remaining  and usage are strictly positive) into cnst_light_tab. */
663     cnst->remaining = cnst->bound;
664     if (!double_positive(cnst->remaining, cnst->bound*sg_maxmin_precision))
665       continue;
666     cnst->usage = 0;
667     elem_list = &(cnst->element_set);
668     xbt_swag_foreach(_elem, elem_list) {
669       elem = (lmm_element_t)_elem;
670       /* 0-weighted elements (ie, sleep actions) are at the end of the swag and we don't want to consider them */
671       if (elem->variable->weight <= 0)
672         break;
673       if ((elem->value > 0)) {
674         if (cnst->sharing_policy)
675           cnst->usage += elem->value / elem->variable->weight;
676         else if (cnst->usage < elem->value / elem->variable->weight)
677           cnst->usage = elem->value / elem->variable->weight;
678
679         make_elem_active(elem);
680         simgrid::surf::Action *action = static_cast<simgrid::surf::Action*>(elem->variable->id);
681         if (sys->keep_track && !action->is_linked())
682           sys->keep_track->push_back(*action);
683       }
684     }
685     XBT_DEBUG("Constraint '%d' usage: %f remaining: %f ", cnst->id_int, cnst->usage, cnst->remaining);
686     /* Saturated constraints update */
687
688     if(cnst->usage > 0) {
689       cnst_light_tab[cnst_light_num].cnst = cnst;
690       cnst->cnst_light = &(cnst_light_tab[cnst_light_num]);
691       cnst_light_tab[cnst_light_num].remaining_over_usage = cnst->remaining / cnst->usage;
692       saturated_constraint_set_update(cnst_light_tab[cnst_light_num].remaining_over_usage,
693         cnst_light_num, saturated_constraint_set, &min_usage);
694       xbt_assert(cnst->active_element_set.count>0, "There is no sense adding a constraint that has no active element!" );
695       cnst_light_num++;
696     }
697   }
698
699   saturated_variable_set_update(  cnst_light_tab,
700                                   saturated_constraint_set,
701                                   sys);
702
703   /* Saturated variables update */
704
705   do {
706     /* Fix the variables that have to be */
707     var_list = &(sys->saturated_variable_set);
708
709     xbt_swag_foreach(_var, var_list) {
710       var = (lmm_variable_t)_var;
711       if (var->weight <= 0.0)
712         DIE_IMPOSSIBLE;
713       /* First check if some of these variables could reach their upper
714          bound and update min_bound accordingly. */
715       XBT_DEBUG
716           ("var=%d, var->bound=%f, var->weight=%f, min_usage=%f, var->bound*var->weight=%f",
717            var->id_int, var->bound, var->weight, min_usage,
718            var->bound * var->weight);
719       if ((var->bound > 0) && (var->bound * var->weight < min_usage)) {
720         if (min_bound < 0)
721           min_bound = var->bound*var->weight;
722         else
723           min_bound = MIN(min_bound, (var->bound*var->weight));
724         XBT_DEBUG("Updated min_bound=%f", min_bound);
725       }
726     }
727
728
729     while ((var = (lmm_variable_t)xbt_swag_getFirst(var_list))) {
730       int i;
731
732       if (min_bound < 0) {
733         //If no variable could reach its bound, deal iteratively the constraints usage ( at worst one constraint is saturated at each cycle) 
734         var->value = min_usage / var->weight;
735         XBT_DEBUG("Setting %p (%d) value to %f\n", var, var->id_int, var->value);
736       } else {
737         //If there exist a variable that can reach its bound, only update it (and other with the same bound) for now.
738             if (double_equals(min_bound, var->bound*var->weight, sg_maxmin_precision)){
739           var->value = var->bound;
740           XBT_DEBUG("Setting %p (%d) value to %f\n", var, var->id_int, var->value);
741         }
742         else {
743           // Variables which bound is different are not considered for this cycle, but they will be afterwards.  
744           XBT_DEBUG("Do not consider %p (%d) \n", var, var->id_int);
745           xbt_swag_remove(var, var_list);
746           continue;
747         }
748       }
749       XBT_DEBUG("Min usage: %f, Var(%d)->weight: %f, Var(%d)->value: %f ",
750              min_usage, var->id_int, var->weight, var->id_int, var->value);
751
752
753       /* Update the usage of contraints where this variable is involved */
754       for (i = 0; i < var->cnsts_number; i++) {
755         elem = &var->cnsts[i];
756         cnst = elem->constraint;
757         if (cnst->sharing_policy) {
758           //Remember: shared constraints require that sum(elem->value * var->value) < cnst->bound
759           double_update(&(cnst->remaining),  elem->value * var->value, cnst->bound*sg_maxmin_precision);
760           double_update(&(cnst->usage), elem->value / var->weight, sg_maxmin_precision);
761           //If the constraint is saturated, remove it from the set of active constraints (light_tab)
762           if(!double_positive(cnst->usage,sg_maxmin_precision) || !double_positive(cnst->remaining,cnst->bound*sg_maxmin_precision)) {
763             if (cnst->cnst_light) {
764               int index = (cnst->cnst_light-cnst_light_tab);
765               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  ",
766                         index,cnst_light_num, cnst, cnst->cnst_light, cnst_light_tab, cnst->usage, cnst->remaining, cnst->bound);
767               cnst_light_tab[index]=cnst_light_tab[cnst_light_num-1];
768               cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
769               cnst_light_num--;
770               cnst->cnst_light = NULL;
771             }
772           } else {
773             cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
774           }
775           make_elem_inactive(elem);
776         } else {
777           //Remember: non-shared constraints only require that max(elem->value * var->value) < cnst->bound
778           cnst->usage = 0.0;
779           make_elem_inactive(elem);
780           elem_list = &(cnst->element_set);
781           xbt_swag_foreach(_elem, elem_list) {
782                 elem = (lmm_element_t)_elem;
783                 if (elem->variable->weight <= 0) break; //Found an inactive variable -> no more active variables
784             if (elem->variable->value > 0) continue;
785             if (elem->value > 0)
786               cnst->usage = MAX(cnst->usage, elem->value / elem->variable->weight);
787           }
788           //If the constraint is saturated, remove it from the set of active constraints (light_tab)
789           if(!double_positive(cnst->usage,sg_maxmin_precision) || !double_positive(cnst->remaining,cnst->bound*sg_maxmin_precision)) {
790             if(cnst->cnst_light) {
791               int index = (cnst->cnst_light-cnst_light_tab);
792               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  ",
793                         index,cnst_light_num, cnst, cnst->cnst_light, cnst_light_tab, cnst->usage, cnst->remaining, cnst->bound);
794               cnst_light_tab[index]=cnst_light_tab[cnst_light_num-1];
795               cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
796               cnst_light_num--;
797               cnst->cnst_light = NULL;
798             }
799           } else {
800             cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
801             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." );
802           }
803         }
804       }
805       xbt_swag_remove(var, var_list);
806     }
807
808     /* Find out which variables reach the maximum */
809     min_usage = -1;
810     min_bound = -1;
811     saturated_constraint_set->pos = 0;
812     int pos;
813     for(pos=0; pos<cnst_light_num; pos++){
814       xbt_assert(cnst_light_tab[pos].cnst->active_element_set.count>0, "Cannot saturate more a constraint that has no active element! You may want to change the maxmin precision (--cfg=maxmin/precision:<new_value>) because of possible rounding effects.\n\tFor the record, the usage of this constraint is %g while the maxmin precision to which it is compared is %g.\n\tThe usage of the previous constraint is %g.", cnst_light_tab[pos].cnst->usage, sg_maxmin_precision, cnst_light_tab[pos-1].cnst->usage);
815       saturated_constraint_set_update(
816           cnst_light_tab[pos].remaining_over_usage,
817           pos,
818           saturated_constraint_set,
819           &min_usage);
820         }
821
822     saturated_variable_set_update(  cnst_light_tab,
823                                     saturated_constraint_set,
824                                     sys);
825
826   } while (cnst_light_num > 0);
827
828   sys->modified = 0;
829   if (sys->selective_update_active)
830     lmm_remove_all_modified_set(sys);
831
832   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
833     lmm_print(sys);
834   }
835
836   xbt_free(saturated_constraint_set->data);
837   xbt_free(saturated_constraint_set);
838   xbt_free(cnst_light_tab);
839   XBT_OUT();
840 }
841
842 /* Not a O(1) function */
843
844 void lmm_update(lmm_system_t sys, lmm_constraint_t cnst,
845                 lmm_variable_t var, double value)
846 {
847   int i;
848
849   for (i = 0; i < var->cnsts_number; i++)
850     if (var->cnsts[i].constraint == cnst) {
851       var->cnsts[i].value = value;
852       sys->modified = 1;
853       lmm_update_modified_set(sys, cnst);
854       return;
855     }
856 }
857
858 /** \brief Attribute the value bound to var->bound.
859  * 
860  *  \param sys the lmm_system_t
861  *  \param var the lmm_variable_t
862  *  \param bound the new bound to associate with var
863  * 
864  *  Makes var->bound equal to bound. Whenever this function is called 
865  *  a change is  signed in the system. To
866  *  avoid false system changing detection it is a good idea to test 
867  *  (bound != 0) before calling it.
868  *
869  */
870 void lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
871                                double bound)
872 {
873   sys->modified = 1;
874   var->bound = bound;
875
876   if (var->cnsts_number)
877     lmm_update_modified_set(sys, var->cnsts[0].constraint);
878 }
879
880
881 void lmm_update_variable_weight(lmm_system_t sys, lmm_variable_t var,
882                                 double weight)
883 {
884   int i;
885   lmm_element_t elem;
886
887   if (weight == var->weight)
888     return;
889   XBT_IN("(sys=%p, var=%p, weight=%f)", sys, var, weight);
890   sys->modified = 1;
891   var->weight = weight;
892   xbt_swag_remove(var, &(sys->variable_set));
893   if (weight)
894     xbt_swag_insert_at_head(var, &(sys->variable_set));
895   else
896     xbt_swag_insert_at_tail(var, &(sys->variable_set));
897
898   for (i = 0; i < var->cnsts_number; i++) {
899     elem = &var->cnsts[i];
900     xbt_swag_remove(elem, &(elem->constraint->element_set));
901     if (weight)
902       xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
903     else
904       xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
905
906     if (i == 0)
907       lmm_update_modified_set(sys, elem->constraint);
908   }
909   if (!weight)
910     var->value = 0.0;
911
912   XBT_OUT();
913 }
914
915 double lmm_get_variable_weight(lmm_variable_t var)
916 {
917   return var->weight;
918 }
919
920 void lmm_update_constraint_bound(lmm_system_t sys,
921                                             lmm_constraint_t cnst,
922                                             double bound)
923 {
924   sys->modified = 1;
925   lmm_update_modified_set(sys, cnst);
926   cnst->bound = bound;
927 }
928
929 int lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst)
930 {
931   return xbt_swag_belongs(cnst, &(sys->active_constraint_set));
932 }
933
934 XBT_INLINE lmm_constraint_t lmm_get_first_active_constraint(lmm_system_t
935                                                             sys)
936 {
937   return (lmm_constraint_t)xbt_swag_getFirst(&(sys->active_constraint_set));
938 }
939
940 XBT_INLINE lmm_constraint_t lmm_get_next_active_constraint(lmm_system_t
941                                                            sys,
942                                                            lmm_constraint_t
943                                                            cnst)
944 {
945   return (lmm_constraint_t)xbt_swag_getNext(cnst, (sys->active_constraint_set).offset);
946 }
947
948 #ifdef HAVE_LATENCY_BOUND_TRACKING
949 int lmm_is_variable_limited_by_latency(lmm_variable_t var)
950 {
951   return (double_equals(var->bound, var->value, var->bound*sg_maxmin_precision));
952 }
953 #endif
954
955
956 /** \brief Update the constraint set propagating recursively to
957  *  other constraints so the system should not be entirely computed.
958  *
959  *  \param sys the lmm_system_t
960  *  \param cnst the lmm_constraint_t affected by the change
961  *
962  *  A recursive algorithm to optimize the system recalculation selecting only
963  *  constraints that have changed. Each constraint change is propagated
964  *  to the list of constraints for each variable.
965  */
966 static void lmm_update_modified_set_rec(lmm_system_t sys,
967                                         lmm_constraint_t cnst)
968 {
969   void* _elem;
970
971   xbt_swag_foreach(_elem, &cnst->element_set) {
972     lmm_variable_t var = ((lmm_element_t)_elem)->variable;
973     s_lmm_element_t *cnsts = var->cnsts;
974     int i;
975     for (i = 0; var->visited != sys->visited_counter
976              && i < var->cnsts_number ; i++) {
977       if (cnsts[i].constraint != cnst
978           && !xbt_swag_belongs(cnsts[i].constraint,
979                                &sys->modified_constraint_set)) {
980         xbt_swag_insert(cnsts[i].constraint, &sys->modified_constraint_set);
981         lmm_update_modified_set_rec(sys, cnsts[i].constraint);
982       }
983     }
984     var->visited = sys->visited_counter;
985   }
986 }
987
988 static void lmm_update_modified_set(lmm_system_t sys,
989                                     lmm_constraint_t cnst)
990 {
991   /* nothing to do if selective update isn't active */
992   if (sys->selective_update_active
993       && !xbt_swag_belongs(cnst, &sys->modified_constraint_set)) {
994     xbt_swag_insert(cnst, &sys->modified_constraint_set);
995     lmm_update_modified_set_rec(sys, cnst);
996   }
997 }
998
999 /** \brief Remove all constraints of the modified_constraint_set.
1000  *
1001  *  \param sys the lmm_system_t
1002  */
1003 static void lmm_remove_all_modified_set(lmm_system_t sys)
1004 {
1005   if (++sys->visited_counter == 1) {
1006     /* the counter wrapped around, reset each variable->visited */
1007         void *_var;
1008     xbt_swag_foreach(_var, &sys->variable_set)
1009       ((lmm_variable_t)_var)->visited = 0;
1010   }
1011   xbt_swag_reset(&sys->modified_constraint_set);
1012 }
1013
1014 /**
1015  *  Returns total resource load
1016  *
1017  *  \param cnst the lmm_constraint_t associated to the resource
1018  *
1019  */
1020 double lmm_constraint_get_usage(lmm_constraint_t cnst) {
1021    double usage = 0.0;
1022    xbt_swag_t elem_list = &(cnst->element_set);
1023    void *_elem;
1024    lmm_element_t elem = NULL;
1025
1026    xbt_swag_foreach(_elem, elem_list) {
1027          elem = (lmm_element_t)_elem;
1028      /* 0-weighted elements (ie, sleep actions) are at the end of the swag and we don't want to consider them */
1029      if (elem->variable->weight <= 0)
1030        break;
1031      if ((elem->value > 0)) {
1032        if (cnst->sharing_policy)
1033          usage += elem->value * elem->variable->value;
1034        else if (usage < elem->value * elem->variable->value)
1035          usage = elem->value * elem->variable->value;
1036      }
1037    }
1038   return usage;
1039 }
1040
1041