Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move surf::As to s4u::As
[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 /* \file callbacks.h */
8
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "xbt/mallocator.h"
12 #include "maxmin_private.hpp"
13 #include <stdlib.h>
14 #include <stdio.h>              /* sprintf */
15 #include <math.h>
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_maxmin, surf,
17                                 "Logging specific to SURF (maxmin)");
18
19 typedef struct s_dyn_light {
20   int *data;
21   int pos;
22   int size;
23 } s_dyn_light_t, *dyn_light_t;
24
25 double sg_maxmin_precision = 0.00001;
26 double sg_surf_precision   = 0.00001;
27
28 static void *lmm_variable_mallocator_new_f(void);
29 static void lmm_variable_mallocator_free_f(void *var);
30 #define lmm_variable_mallocator_reset_f ((void_f_pvoid_t)NULL)
31 static void lmm_update_modified_set(lmm_system_t sys,
32                                     lmm_constraint_t cnst);
33 static void lmm_remove_all_modified_set(lmm_system_t sys);
34 static int Global_debug_id = 1;
35 static int Global_const_debug_id = 1;
36
37 static void lmm_var_free(lmm_system_t sys, lmm_variable_t var);
38 static XBT_INLINE void lmm_cnst_free(lmm_system_t sys,
39                                      lmm_constraint_t cnst);
40
41 static void lmm_on_disabled_var(lmm_system_t sys, lmm_constraint_t cnstr);
42 static void lmm_enable_var(lmm_system_t sys, lmm_variable_t var);
43 static int lmm_can_enable_var(lmm_variable_t var);
44 static void lmm_disable_var(lmm_system_t sys, lmm_variable_t var);
45 static int lmm_concurrency_slack(lmm_constraint_t cnstr);
46 static int lmm_cnstrs_min_concurrency_slack(lmm_variable_t var);
47
48 static void lmm_check_concurrency(lmm_system_t sys);
49
50 inline int lmm_element_concurrency(lmm_element_t elem) {
51   //Ignore element with weight less than one (e.g. cross-traffic)
52   return (elem->value>=1)?1:0;
53   //There are other alternatives, but they will change the behaviour of the model..
54   //So do not use it unless you want to make a new model.
55   //If you do, remember to change the variables concurrency share to reflect it.
56   //Potential examples are:
57   //return (elem->weight>0)?1:0;//Include element as soon  as weight is non-zero
58   //return (int)ceil(elem->weight);//Include element as the rounded-up integer value of the element weight
59 }
60
61 inline void lmm_decrease_concurrency(lmm_element_t elem) {
62   xbt_assert(elem->constraint->concurrency_current>=lmm_element_concurrency(elem));
63   elem->constraint->concurrency_current-=lmm_element_concurrency(elem);
64 }
65
66 inline void lmm_increase_concurrency(lmm_element_t elem) {
67
68   elem->constraint->concurrency_current+= lmm_element_concurrency(elem);
69
70   lmm_constraint_t cnstr=elem->constraint;
71   
72   if(cnstr->concurrency_current > cnstr->concurrency_maximum)
73     cnstr->concurrency_maximum= cnstr->concurrency_current;
74
75   xbt_assert(cnstr->concurrency_limit<0 || cnstr->concurrency_current<=cnstr->concurrency_limit,"Concurrency limit overflow!");
76 }
77
78 lmm_system_t lmm_system_new(int selective_update)
79 {
80   lmm_system_t l = NULL;
81   s_lmm_variable_t var;
82   s_lmm_constraint_t cnst;
83
84   l = xbt_new0(s_lmm_system_t, 1);
85
86   l->modified = 0;
87   l->selective_update_active = selective_update;
88   l->visited_counter = 1;
89
90   XBT_DEBUG("Setting selective_update_active flag to %d",
91          l->selective_update_active);
92
93   xbt_swag_init(&(l->variable_set),
94                 xbt_swag_offset(var, variable_set_hookup));
95   xbt_swag_init(&(l->constraint_set),
96                 xbt_swag_offset(cnst, constraint_set_hookup));
97
98   xbt_swag_init(&(l->active_constraint_set),
99                 xbt_swag_offset(cnst, active_constraint_set_hookup));
100
101   xbt_swag_init(&(l->modified_constraint_set),
102                 xbt_swag_offset(cnst, modified_constraint_set_hookup));
103   xbt_swag_init(&(l->saturated_variable_set),
104                 xbt_swag_offset(var, saturated_variable_set_hookup));
105   xbt_swag_init(&(l->saturated_constraint_set),
106                 xbt_swag_offset(cnst, saturated_constraint_set_hookup));
107
108   l->variable_mallocator = xbt_mallocator_new(65536,
109                                               lmm_variable_mallocator_new_f,
110                                               lmm_variable_mallocator_free_f,
111                                               lmm_variable_mallocator_reset_f);
112
113   return l;
114 }
115
116 void lmm_system_free(lmm_system_t sys)
117 {
118   lmm_variable_t var = NULL;
119   lmm_constraint_t cnst = NULL;
120
121   
122   while ((var = (lmm_variable_t) extract_variable(sys))) {
123     XBT_WARN("Variable %d still in system when freing it: this may be a bug", var->id_int);
124     lmm_var_free(sys, var);
125   }
126
127   while ((cnst = (lmm_constraint_t) extract_constraint(sys)))
128     lmm_cnst_free(sys, cnst);
129
130   xbt_mallocator_free(sys->variable_mallocator);
131   free(sys);
132 }
133
134 static XBT_INLINE void lmm_variable_remove(lmm_system_t sys, lmm_variable_t var)
135 {
136   int i;
137   int nelements;
138   
139   lmm_element_t elem = NULL;
140   
141   XBT_IN("(sys=%p, var=%p)", sys, var);
142   sys->modified = 1;
143
144   //TODOLATER Can do better than that by leaving only the variable in only one enabled_element_set, call lmm_update_modified_set, and then remove it..
145   if(var->cnsts_number)
146       lmm_update_modified_set(sys, var->cnsts[0].constraint);
147
148   for (i = 0; i < var->cnsts_number; i++) {
149     elem = &var->cnsts[i];
150     if(var->weight>0)
151     lmm_decrease_concurrency(elem);
152     xbt_swag_remove(elem, &(elem->constraint->enabled_element_set));
153     xbt_swag_remove(elem, &(elem->constraint->disabled_element_set));
154     xbt_swag_remove(elem, &(elem->constraint->active_element_set));
155     nelements=xbt_swag_size(&(elem->constraint->enabled_element_set)) +
156               xbt_swag_size(&(elem->constraint->disabled_element_set));
157     if (!nelements)
158       make_constraint_inactive(sys, elem->constraint);
159     else
160       lmm_on_disabled_var(sys,elem->constraint);
161   }
162   
163   //Check if we can enable new variables going through the constraints where var was.
164   //Do it after removing all elements, so he first disabled variables get priority over those with smaller
165   //requirement
166   for (i = 0; i < var->cnsts_number; i++) {
167     elem = &var->cnsts[i];
168     if(xbt_swag_size(&(elem->constraint->disabled_element_set)))
169       lmm_on_disabled_var(sys,elem->constraint);  
170   }
171   
172   var->cnsts_number = 0;
173
174   lmm_check_concurrency(sys);
175     
176   XBT_OUT();
177 }
178
179 static void lmm_var_free(lmm_system_t sys, lmm_variable_t var)
180 {
181
182   lmm_variable_remove(sys, var);
183   xbt_mallocator_release(sys->variable_mallocator, var);
184 }
185
186 static XBT_INLINE void lmm_cnst_free(lmm_system_t sys,
187                                      lmm_constraint_t cnst)
188 {
189   make_constraint_inactive(sys, cnst);
190   free(cnst);
191 }
192
193 lmm_constraint_t lmm_constraint_new(lmm_system_t sys, void *id,
194                                     double bound_value)
195 {
196   lmm_constraint_t cnst = NULL;
197   s_lmm_element_t elem;
198
199   cnst = xbt_new0(s_lmm_constraint_t, 1);
200   cnst->id = id;
201   cnst->id_int = Global_const_debug_id++;
202   xbt_swag_init(&(cnst->enabled_element_set),
203                 xbt_swag_offset(elem, enabled_element_set_hookup));
204   xbt_swag_init(&(cnst->disabled_element_set),
205                 xbt_swag_offset(elem, disabled_element_set_hookup));
206   xbt_swag_init(&(cnst->active_element_set),
207                 xbt_swag_offset(elem, active_element_set_hookup));
208
209   cnst->bound = bound_value;
210   cnst->concurrency_maximum=0;
211   cnst->concurrency_current=0;
212   //TODO MARTIN Maybe a configuration item for the default cap concurrency? 
213   cnst->concurrency_limit=100;
214   cnst->usage = 0;
215   cnst->sharing_policy = 1; /* FIXME: don't hardcode the value */
216   insert_constraint(sys, cnst);
217
218   return cnst;
219 }
220
221 int lmm_constraint_concurrency_limit_get(lmm_constraint_t cnst)
222 {
223  return cnst->concurrency_limit;
224 }
225
226 void lmm_constraint_concurrency_limit_set(lmm_constraint_t cnst, int concurrency_limit)
227 {
228   xbt_assert(concurrency_limit<0 || cnst->concurrency_maximum<=concurrency_limit,"New concurrency limit should be larger than observed concurrency maximum. Maybe you want to call  lmm_constraint_concurrency_maximum_reset() to reset the maximum?");
229   cnst->concurrency_limit = concurrency_limit;
230 }
231
232 void lmm_constraint_concurrency_maximum_reset(lmm_constraint_t cnst)
233 {
234   cnst->concurrency_maximum = 0;
235 }
236
237 int lmm_constraint_concurrency_maximum_get(lmm_constraint_t cnst)
238 {
239  xbt_assert(cnst->concurrency_limit<0 || cnst->concurrency_maximum<=cnst->concurrency_limit,"Very bad: maximum observed concurrency is higher than limit. This is a bug of SURF, please report it.");
240   return cnst->concurrency_maximum;
241 }
242
243 void lmm_constraint_shared(lmm_constraint_t cnst)
244 {
245   cnst->sharing_policy = 0;
246 }
247
248 /** Return true if the constraint is shared, and false if it's FATPIPE */
249 int lmm_constraint_sharing_policy(lmm_constraint_t cnst)
250 {
251   return (cnst->sharing_policy);
252 }
253
254 /* @brief Remove a constraint 
255  * Currently this is dead code, but it is exposed in maxmin.h
256  * Apparently, this call was designed assuming that constraint would no more have elements in it. 
257  * If this is not the case, assertion will fail, and you need to add calls e.g. to lmm_shrink before effectively removing it.
258  */
259 XBT_INLINE void lmm_constraint_free(lmm_system_t sys,
260                                     lmm_constraint_t cnst)
261 {
262   xbt_assert(!xbt_swag_size(&(cnst->active_element_set)),"Removing constraint but it still has active elements");
263   xbt_assert(!xbt_swag_size(&(cnst->enabled_element_set)),"Removing constraint but it still has enabled elements");
264   xbt_assert(!xbt_swag_size(&(cnst->disabled_element_set)),"Removing constraint but it still has disabled elements");
265   remove_constraint(sys, cnst);
266   lmm_cnst_free(sys, cnst);
267 }
268
269 static void *lmm_variable_mallocator_new_f(void)
270 {
271   lmm_variable_t var = xbt_new(s_lmm_variable_t, 1);
272   var->cnsts = NULL; /* will be created by realloc */
273   return var;
274 }
275
276 static void lmm_variable_mallocator_free_f(void *var)
277 {
278   xbt_free(((lmm_variable_t) var)->cnsts);
279   xbt_free(var);
280 }
281
282 lmm_variable_t lmm_variable_new(lmm_system_t sys, void *id,
283                                 double weight,
284                                 double bound, int number_of_constraints)
285 {
286   lmm_variable_t var = NULL;
287   int i;
288
289   XBT_IN("(sys=%p, id=%p, weight=%f, bound=%f, num_cons =%d)",
290           sys, id, weight, bound, number_of_constraints);
291
292   var = (lmm_variable_t) xbt_mallocator_get(sys->variable_mallocator);
293   var->id = id;
294   var->id_int = Global_debug_id++;
295   var->cnsts = (s_lmm_element_t *) xbt_realloc(var->cnsts, number_of_constraints * sizeof(s_lmm_element_t));
296   for (i = 0; i < number_of_constraints; i++) {
297     var->cnsts[i].enabled_element_set_hookup.next = NULL;
298     var->cnsts[i].enabled_element_set_hookup.prev = NULL;
299     var->cnsts[i].disabled_element_set_hookup.next = NULL;
300     var->cnsts[i].disabled_element_set_hookup.prev = NULL;
301     var->cnsts[i].active_element_set_hookup.next = NULL;
302     var->cnsts[i].active_element_set_hookup.prev = NULL;
303     var->cnsts[i].constraint = NULL;
304     var->cnsts[i].variable = NULL;
305     var->cnsts[i].value = 0.0;
306   }
307   var->cnsts_size = number_of_constraints;
308   var->cnsts_number = 0;
309   var->weight = weight;
310   var->staged_weight = 0.0;
311   var->bound = bound;
312   var->concurrency_share = 1;
313   var->value = 0.0;
314   var->visited = sys->visited_counter - 1;
315   var->mu = 0.0;
316   var->new_mu = 0.0;
317   var->func_f = func_f_def;
318   var->func_fp = func_fp_def;
319   var->func_fpi = func_fpi_def;
320
321   var->variable_set_hookup.next = NULL;
322   var->variable_set_hookup.prev = NULL;
323   var->saturated_variable_set_hookup.next = NULL;
324   var->saturated_variable_set_hookup.prev = NULL;
325
326   if (weight)
327     xbt_swag_insert_at_head(var, &(sys->variable_set));
328   else
329     xbt_swag_insert_at_tail(var, &(sys->variable_set));
330
331   XBT_OUT(" returns %p", var);
332   return var;
333 }
334
335 void lmm_variable_free(lmm_system_t sys, lmm_variable_t var)
336 {
337   remove_variable(sys, var);
338   lmm_var_free(sys, var);
339 }
340
341 double lmm_variable_getvalue(lmm_variable_t var)
342 {
343   return (var->value);
344 }
345
346
347 void lmm_variable_concurrency_share_set(lmm_variable_t var, short int concurrency_share)
348 {
349   var->concurrency_share=concurrency_share;
350 }
351
352 double lmm_variable_getbound(lmm_variable_t var)
353 {
354   return (var->bound);
355 }
356
357 void lmm_shrink(lmm_system_t sys, lmm_constraint_t cnst,
358                 lmm_variable_t var)
359 {
360   lmm_element_t elem = NULL;
361   int found = 0;
362
363   int i;
364   for (i = 0; i < var->cnsts_number; i++) {
365     elem = &(var->cnsts[i]);
366     if (elem->constraint == cnst) {
367       found = 1;
368       break;
369     }
370   }
371
372   if (!found) {
373     XBT_DEBUG("cnst %p is not found in var %p", cnst, var);
374     return;
375   }
376
377   sys->modified = 1;
378
379   XBT_DEBUG("remove elem(value %f, cnst %p, var %p) in var %p",
380       elem->value, elem->constraint, elem->variable, var);
381
382   /* We are going to change the constraint object and the variable object.
383    * Propagate this change to other objects. Calling here before removing variable from not active elements (inactive elements are not visited)
384    */
385   lmm_update_modified_set(sys, cnst);
386   //Useful in case var was already removed from the constraint
387   lmm_update_modified_set(sys, var->cnsts[0].constraint); // will look up enabled_element_set of this constraint, and then each var in the enabled_element_set, and each var->cnsts[i].
388
389   if(xbt_swag_remove(elem, &(elem->constraint->enabled_element_set)))
390     lmm_decrease_concurrency(elem);
391
392   xbt_swag_remove(elem, &(elem->constraint->active_element_set));
393   elem->constraint = NULL;
394   elem->variable = NULL;
395   elem->value = 0;
396
397   var->cnsts_number -= 1;
398
399   //No variable in this constraint -> make it inactive
400   if (xbt_swag_size(&(cnst->enabled_element_set))+xbt_swag_size(&(cnst->disabled_element_set)) == 0)
401     make_constraint_inactive(sys, cnst);
402   else {
403     //Check maxconcurrency to see if we can enable new variables
404     lmm_on_disabled_var(sys,elem->constraint);       
405   }
406
407   lmm_check_concurrency(sys);
408 }
409
410 void lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
411                 lmm_variable_t var, double value)
412 {
413   lmm_element_t elem = NULL;
414   double weight;
415   int i,current_share;
416   
417   
418   sys->modified = 1;
419
420   //Check if this variable already has an active element in this constraint
421   //If it does, substract it from the required slack
422   current_share=0;
423   if(var->concurrency_share>1){
424     for( i=0; i<var->cnsts_number;i++){
425       if(var->cnsts[i].constraint==cnst && xbt_swag_belongs(&var->cnsts[i],&(var->cnsts[i].constraint->enabled_element_set)))
426         current_share+=lmm_element_concurrency(&(var->cnsts[i]));
427     }
428   }
429
430   //Check if we need to disable the variable 
431   if(var->weight>0 && var->concurrency_share-current_share>lmm_concurrency_slack(cnst))
432    {
433     weight=var->weight;
434     lmm_disable_var(sys,var);
435     for (i = 0; i < var->cnsts_number; i++)
436       lmm_on_disabled_var(sys,var->cnsts[i].constraint);
437     value=0;
438     var->staged_weight=weight;
439     xbt_assert(!var->weight);
440   }
441
442   xbt_assert(var->cnsts_number < var->cnsts_size, "Too much constraints");
443
444   elem = &(var->cnsts[var->cnsts_number++]);
445
446   elem->value = value;
447   elem->constraint = cnst;
448   elem->variable = var;
449
450   
451   if (var->weight){
452     xbt_swag_insert_at_head(elem, &(elem->constraint->enabled_element_set));
453     lmm_increase_concurrency(elem);
454   }
455   else
456     xbt_swag_insert_at_tail(elem, &(elem->constraint->disabled_element_set));
457
458   if(!sys->selective_update_active) {
459     make_constraint_active(sys, cnst);
460   } else if(elem->value>0 || var->weight >0) {
461     make_constraint_active(sys, cnst);
462     lmm_update_modified_set(sys, cnst);
463     //TODOLATER: Why do we need this second call?
464     if (var->cnsts_number > 1)
465       lmm_update_modified_set(sys, var->cnsts[0].constraint);
466   }
467
468   lmm_check_concurrency(sys);
469 }
470
471 void lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst,
472                     lmm_variable_t var, double value)
473 {
474   int i,j;
475   double weight;
476   sys->modified = 1;
477   
478   lmm_check_concurrency(sys);
479
480   //BEWARE: In case you have multiple elements in one constraint, this will always
481   //add value to the first element.
482   for (i = 0; i < var->cnsts_number; i++)
483     if (var->cnsts[i].constraint == cnst)
484       break;
485
486   if (i < var->cnsts_number) {
487     if (var->weight)
488       lmm_decrease_concurrency(&var->cnsts[i]);
489
490     if (cnst->sharing_policy)
491       var->cnsts[i].value += value;
492     else
493       var->cnsts[i].value = MAX(var->cnsts[i].value, value);
494
495     //We need to check that increasing value of the element does not cross the concurrency limit
496     if (var->weight){
497       if(lmm_concurrency_slack(cnst)<lmm_element_concurrency(&var->cnsts[i])){
498         weight=var->weight;
499         lmm_disable_var(sys,var);
500         for (j = 0; j < var->cnsts_number; j++)
501           lmm_on_disabled_var(sys,var->cnsts[j].constraint);
502         var->staged_weight=weight;
503         xbt_assert(!var->weight);
504       }
505       lmm_increase_concurrency(&var->cnsts[i]);
506     }
507     
508     lmm_update_modified_set(sys, cnst);
509   } else
510     lmm_expand(sys, cnst, var, value);
511
512   lmm_check_concurrency(sys);
513 }
514
515 lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t /*sys*/,
516                                                   lmm_variable_t var,
517                                                   int num)
518 {
519   if (num < var->cnsts_number)
520     return (var->cnsts[num].constraint);
521   else
522     return NULL;
523 }
524
525 double lmm_get_cnst_weight_from_var(lmm_system_t /*sys*/,
526                                                          lmm_variable_t var,
527                                                          int num)
528 {
529   if (num < var->cnsts_number)
530     return (var->cnsts[num].value);
531   else
532     return 0.0;
533 }
534
535 int lmm_get_number_of_cnst_from_var(lmm_system_t /*sys*/,
536                                                lmm_variable_t var)
537 {
538   return (var->cnsts_number);
539 }
540
541 lmm_variable_t lmm_get_var_from_cnst(lmm_system_t /*sys*/,
542                                      lmm_constraint_t cnst,
543                                      lmm_element_t * elem)
544 {
545   
546   if (!(*elem)) {
547     //That is the first call, pick the first element among enabled_element_set (or disabled_element_set if enabled_element_set is empty)
548     *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->enabled_element_set));
549     if (!(*elem))
550       *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->disabled_element_set));
551   }  else {
552     //elem is not null, so we carry on
553     if(xbt_swag_belongs(*elem,&(cnst->enabled_element_set))){
554       //Look at enabled_element_set, and jump to disabled_element_set when finished
555       *elem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->enabled_element_set.offset);
556       if (!(*elem))
557   *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->disabled_element_set));
558     } else {
559       *elem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->disabled_element_set.offset);      
560     }
561   }
562   if (*elem)
563     return (*elem)->variable;
564   else
565     return NULL;
566 }
567
568 //if we modify the swag between calls, normal version may loop forever
569 //this safe version ensures that we browse the swag elements only once
570 lmm_variable_t lmm_get_var_from_cnst_safe(lmm_system_t /*sys*/,
571                                      lmm_constraint_t cnst,
572                                      lmm_element_t * elem,
573                                      lmm_element_t * nextelem,
574                                      int * numelem)
575 {
576   if (!(*elem)){
577     *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->enabled_element_set));
578     *numelem = xbt_swag_size(&(cnst->enabled_element_set))+xbt_swag_size(&(cnst->disabled_element_set))-1;
579     if (!(*elem))
580       *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->disabled_element_set));
581   }else{
582     *elem = *nextelem;
583     if(*numelem>0){
584      (*numelem) --;
585     }else
586       return NULL;
587   }
588   if (*elem){
589     //elem is not null, so we carry on
590     if(xbt_swag_belongs(*elem,&(cnst->enabled_element_set))){
591       //Look at enabled_element_set, and jump to disabled_element_set when finished
592       *nextelem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->enabled_element_set.offset);
593       if (!(*nextelem))
594   *nextelem = (lmm_element_t) xbt_swag_getFirst(&(cnst->disabled_element_set));
595     } else {
596       *nextelem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->disabled_element_set.offset);      
597     }
598     return (*elem)->variable;
599   }else
600     return NULL;
601 }
602
603 void *lmm_constraint_id(lmm_constraint_t cnst)
604 {
605   return cnst->id;
606 }
607
608 void *lmm_variable_id(lmm_variable_t var)
609 {
610   return var->id;
611 }
612
613 static XBT_INLINE void saturated_constraint_set_update(double usage,
614                                                       int cnst_light_num,
615                                                       dyn_light_t saturated_constraint_set,
616                                                       double *min_usage)
617 {
618   xbt_assert(usage > 0,"Impossible");
619
620   if (*min_usage < 0 || *min_usage > usage) {
621     *min_usage = usage;
622     XBT_HERE(" min_usage=%f (cnst->remaining / cnst->usage =%f)", *min_usage, usage);
623     saturated_constraint_set->data[0] = cnst_light_num;
624     saturated_constraint_set->pos = 1;
625   } else if (*min_usage == usage) {
626     if(saturated_constraint_set->pos == saturated_constraint_set->size) { // realloc the size
627       saturated_constraint_set->size *= 2;
628       saturated_constraint_set->data = (int*) xbt_realloc(saturated_constraint_set->data, (saturated_constraint_set->size) * sizeof(int));
629     }
630     saturated_constraint_set->data[saturated_constraint_set->pos] = cnst_light_num;
631     saturated_constraint_set->pos++;
632   }
633 }
634
635 static XBT_INLINE void saturated_variable_set_update(
636     s_lmm_constraint_light_t *cnst_light_tab,
637     dyn_light_t saturated_constraint_set,
638     lmm_system_t sys)
639 {
640   /* Add active variables (i.e. variables that need to be set) from the set of constraints to saturate (cnst_light_tab)*/ 
641   lmm_constraint_light_t cnst = NULL;
642   void *_elem;
643   lmm_element_t elem = NULL;
644   xbt_swag_t elem_list = NULL;
645   int i;
646   for(i = 0; i< saturated_constraint_set->pos; i++){
647     cnst = &cnst_light_tab[saturated_constraint_set->data[i]];
648     elem_list = &(cnst->cnst->active_element_set);
649     xbt_swag_foreach(_elem, elem_list) {
650       elem = (lmm_element_t)_elem;
651       //Visiting active_element_set, so, by construction, should never get a zero weight, correct?
652       xbt_assert(elem->variable->weight > 0);
653       if ((elem->value > 0))
654         xbt_swag_insert(elem->variable, &(sys->saturated_variable_set));
655     }
656   }
657 }
658
659 void lmm_print(lmm_system_t sys)
660 {
661   void *_cnst, *_elem, *_var;
662   lmm_constraint_t cnst = NULL;
663   lmm_element_t elem = NULL;
664   lmm_variable_t var = NULL;
665   xbt_swag_t cnst_list = NULL;
666   xbt_swag_t var_list = NULL;
667   xbt_swag_t elem_list = NULL;
668   char print_buf[1024];
669   char *trace_buf = (char*) xbt_malloc0(sizeof(char));
670   double sum = 0.0;
671
672   /* Printing Objective */
673   var_list = &(sys->variable_set);
674   sprintf(print_buf, "MAX-MIN ( ");
675   trace_buf = (char*)
676       xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
677   strcat(trace_buf, print_buf);
678   xbt_swag_foreach(_var, var_list) {
679   var = (lmm_variable_t)_var;
680     sprintf(print_buf, "'%d'(%f) ", var->id_int, var->weight);
681     trace_buf = (char*)
682         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
683     strcat(trace_buf, print_buf);
684   }
685   sprintf(print_buf, ")");
686   trace_buf = (char*)
687       xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
688   strcat(trace_buf, print_buf);
689   XBT_DEBUG("%20s", trace_buf);
690   trace_buf[0] = '\000';
691
692   XBT_DEBUG("Constraints");
693   /* Printing Constraints */
694   cnst_list = &(sys->active_constraint_set);
695   xbt_swag_foreach(_cnst, cnst_list) {
696   cnst = (lmm_constraint_t)_cnst;
697     sum = 0.0;
698     //Show  the enabled variables
699     elem_list = &(cnst->enabled_element_set);
700     sprintf(print_buf, "\t");
701     trace_buf = (char*)
702         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
703     strcat(trace_buf, print_buf);
704     sprintf(print_buf, "%s(",(cnst->sharing_policy)?"":"max");
705     trace_buf = (char*)
706       xbt_realloc(trace_buf,
707       strlen(trace_buf) + strlen(print_buf) + 1);
708     strcat(trace_buf, print_buf);      
709     xbt_swag_foreach(_elem, elem_list) {
710       elem = (lmm_element_t)_elem;
711       sprintf(print_buf, "%f.'%d'(%f) %s ", elem->value,
712               elem->variable->id_int, elem->variable->value,(cnst->sharing_policy)?"+":",");
713       trace_buf = (char*)
714           xbt_realloc(trace_buf,
715                       strlen(trace_buf) + strlen(print_buf) + 1);
716       strcat(trace_buf, print_buf);
717       if(cnst->sharing_policy)
718         sum += elem->value * elem->variable->value;
719       else 
720         sum = MAX(sum,elem->value * elem->variable->value);
721     }
722     //TODO: Adding disabled elements only for test compatibility, but do we really want them to be printed?
723     elem_list = &(cnst->disabled_element_set);
724     xbt_swag_foreach(_elem, elem_list) {
725       elem = (lmm_element_t)_elem;
726       sprintf(print_buf, "%f.'%d'(%f) %s ", elem->value,
727               elem->variable->id_int, elem->variable->value,(cnst->sharing_policy)?"+":",");
728       trace_buf = (char*)
729           xbt_realloc(trace_buf,
730                       strlen(trace_buf) + strlen(print_buf) + 1);
731       strcat(trace_buf, print_buf);
732       if(cnst->sharing_policy)
733         sum += elem->value * elem->variable->value;
734       else 
735         sum = MAX(sum,elem->value * elem->variable->value);
736     }
737     
738     sprintf(print_buf, "0) <= %f ('%d')", cnst->bound, cnst->id_int);
739     trace_buf = (char*)
740         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
741     strcat(trace_buf, print_buf);
742
743     if (!cnst->sharing_policy) {
744       sprintf(print_buf, " [MAX-Constraint]");
745       trace_buf = (char*)
746           xbt_realloc(trace_buf,
747                       strlen(trace_buf) + strlen(print_buf) + 1);
748       strcat(trace_buf, print_buf);
749     }
750     XBT_DEBUG("%s", trace_buf);
751     trace_buf[0] = '\000';
752        xbt_assert(!double_positive(sum - cnst->bound, cnst->bound*sg_maxmin_precision),
753                "Incorrect value (%f is not smaller than %f): %g",
754                  sum, cnst->bound, sum - cnst->bound);
755        //if(double_positive(sum - cnst->bound, cnst->bound*sg_maxmin_precision))
756        //XBT_ERROR("Incorrect value (%f is not smaller than %f): %g",sum, cnst->bound, sum - cnst->bound);
757       
758   }
759
760   XBT_DEBUG("Variables");
761   /* Printing Result */
762   xbt_swag_foreach(_var, var_list) {
763   var = (lmm_variable_t)_var;
764     if (var->bound > 0) {
765       XBT_DEBUG("'%d'(%f) : %f (<=%f)", var->id_int, var->weight, var->value,
766              var->bound);
767       xbt_assert(!double_positive(var->value - var->bound, var->bound*sg_maxmin_precision),
768                   "Incorrect value (%f is not smaller than %f",
769                   var->value, var->bound);
770     } else {
771       XBT_DEBUG("'%d'(%f) : %f", var->id_int, var->weight, var->value);
772     }
773   }
774
775   free(trace_buf);
776 }
777
778 void lmm_solve(lmm_system_t sys)
779 {
780   void *_var, *_cnst, *_cnst_next, *_elem;
781   lmm_variable_t var = NULL;
782   lmm_constraint_t cnst = NULL;
783   lmm_element_t elem = NULL;
784   xbt_swag_t cnst_list = NULL;
785   xbt_swag_t var_list = NULL;
786   xbt_swag_t elem_list = NULL;
787   double min_usage = -1;
788   double min_bound = -1;
789
790   if (!(sys->modified))
791     return;
792
793   XBT_IN("(sys=%p)", sys);
794
795   /*
796    * 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.
797    */
798   cnst_list =
799       sys->
800       selective_update_active ? &(sys->modified_constraint_set) :
801       &(sys->active_constraint_set);
802
803   XBT_DEBUG("Active constraints : %d", xbt_swag_size(cnst_list));
804   /* Init: Only modified code portions: reset the value of active variables */
805   xbt_swag_foreach(_cnst, cnst_list) {
806   cnst = (lmm_constraint_t)_cnst;
807     elem_list = &(cnst->enabled_element_set);
808     //XBT_DEBUG("Variable set : %d", xbt_swag_size(elem_list));
809     xbt_swag_foreach(_elem, elem_list) {
810       var = ((lmm_element_t)_elem)->variable;
811       xbt_assert(var->weight > 0.0);
812       var->value = 0.0;
813     }
814   }
815
816   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));
817   int cnst_light_num = 0;
818   dyn_light_t saturated_constraint_set = xbt_new0(s_dyn_light_t,1);
819   saturated_constraint_set->size = 5;
820   saturated_constraint_set->data = xbt_new0(int, saturated_constraint_set->size);
821
822   xbt_swag_foreach_safe(_cnst, _cnst_next, cnst_list) {
823   cnst = (lmm_constraint_t)_cnst;
824     /* INIT: Collect constraints that actually need to be saturated (i.e remaining  and usage are strictly positive) into cnst_light_tab. */
825     cnst->remaining = cnst->bound;
826     if (!double_positive(cnst->remaining, cnst->bound*sg_maxmin_precision))
827       continue;
828     cnst->usage = 0;
829     elem_list = &(cnst->enabled_element_set);
830     xbt_swag_foreach(_elem, elem_list) {
831       elem = (lmm_element_t)_elem;
832       xbt_assert(elem->variable->weight > 0);
833       if ((elem->value > 0)) {
834         if (cnst->sharing_policy)
835           cnst->usage += elem->value / elem->variable->weight;
836         else if (cnst->usage < elem->value / elem->variable->weight)
837           cnst->usage = elem->value / elem->variable->weight;
838
839         make_elem_active(elem);
840         simgrid::surf::Action *action = static_cast<simgrid::surf::Action*>(elem->variable->id);
841         if (sys->keep_track && !action->is_linked())
842           sys->keep_track->push_back(*action);
843       }
844     }
845     XBT_DEBUG("Constraint '%d' usage: %f remaining: %f concurrency: %i<=%i<=%i", cnst->id_int, cnst->usage, cnst->remaining,cnst->concurrency_current,cnst->concurrency_maximum,cnst->concurrency_limit);
846     /* Saturated constraints update */
847
848     if(cnst->usage > 0) {
849       cnst_light_tab[cnst_light_num].cnst = cnst;
850       cnst->cnst_light = &(cnst_light_tab[cnst_light_num]);
851       cnst_light_tab[cnst_light_num].remaining_over_usage = cnst->remaining / cnst->usage;
852       saturated_constraint_set_update(cnst_light_tab[cnst_light_num].remaining_over_usage,
853         cnst_light_num, saturated_constraint_set, &min_usage);
854       xbt_assert(cnst->active_element_set.count>0, "There is no sense adding a constraint that has no active element!" );
855       cnst_light_num++;
856     }
857   }
858
859   saturated_variable_set_update(  cnst_light_tab,
860                                   saturated_constraint_set,
861                                   sys);
862
863   /* Saturated variables update */
864
865   do {
866     /* Fix the variables that have to be */
867     var_list = &(sys->saturated_variable_set);
868
869     xbt_swag_foreach(_var, var_list) {
870       var = (lmm_variable_t)_var;
871       if (var->weight <= 0.0)
872         DIE_IMPOSSIBLE;
873       /* First check if some of these variables could reach their upper
874          bound and update min_bound accordingly. */
875       XBT_DEBUG
876           ("var=%d, var->bound=%f, var->weight=%f, min_usage=%f, var->bound*var->weight=%f",
877            var->id_int, var->bound, var->weight, min_usage,
878            var->bound * var->weight);
879       if ((var->bound > 0) && (var->bound * var->weight < min_usage)) {
880         if (min_bound < 0)
881           min_bound = var->bound*var->weight;
882         else
883           min_bound = MIN(min_bound, (var->bound*var->weight));
884         XBT_DEBUG("Updated min_bound=%f", min_bound);
885       }
886     }
887
888
889     while ((var = (lmm_variable_t)xbt_swag_getFirst(var_list))) {
890       int i;
891
892       if (min_bound < 0) {
893   //If no variable could reach its bound, deal iteratively the constraints usage ( at worst one constraint is saturated at each cycle) 
894         var->value = min_usage / var->weight;
895         // XBT_DEBUG("Setting %p (%d) value to %f\n", var, var->id_int, var->value);
896         XBT_DEBUG("Setting var (%d) value to %f\n", var->id_int, var->value);
897       } else {
898   //If there exist a variable that can reach its bound, only update it (and other with the same bound) for now.
899       if (double_equals(min_bound, var->bound*var->weight, sg_maxmin_precision)){
900           var->value = var->bound;
901           XBT_DEBUG("Setting %p (%d) value to %f\n", var, var->id_int, var->value);
902         }
903         else {
904     // Variables which bound is different are not considered for this cycle, but they will be afterwards.  
905           XBT_DEBUG("Do not consider %p (%d) \n", var, var->id_int);
906           xbt_swag_remove(var, var_list);
907           continue;
908         }
909       }
910       XBT_DEBUG("Min usage: %f, Var(%d)->weight: %f, Var(%d)->value: %f ",
911              min_usage, var->id_int, var->weight, var->id_int, var->value);
912
913
914       /* Update the usage of contraints where this variable is involved */
915       for (i = 0; i < var->cnsts_number; i++) {
916         elem = &var->cnsts[i];
917         cnst = elem->constraint;
918         if (cnst->sharing_policy) {
919     //Remember: shared constraints require that sum(elem->value * var->value) < cnst->bound
920           double_update(&(cnst->remaining),  elem->value * var->value, cnst->bound*sg_maxmin_precision);
921           double_update(&(cnst->usage), elem->value / var->weight, sg_maxmin_precision);
922     //If the constraint is saturated, remove it from the set of active constraints (light_tab)
923           if(!double_positive(cnst->usage,sg_maxmin_precision) || !double_positive(cnst->remaining,cnst->bound*sg_maxmin_precision)) {
924             if (cnst->cnst_light) {
925               int index = (cnst->cnst_light-cnst_light_tab);
926               XBT_DEBUG("index: %d \t cnst_light_num: %d \t || usage: %f remaining: %f bound: %f  ",
927               index,cnst_light_num, cnst->usage, cnst->remaining, cnst->bound);
928               //              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  ",
929               //index,cnst_light_num, cnst, cnst->cnst_light, cnst_light_tab, cnst->usage, cnst->remaining, cnst->bound);
930               cnst_light_tab[index]=cnst_light_tab[cnst_light_num-1];
931               cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
932               cnst_light_num--;
933               cnst->cnst_light = NULL;
934             }
935           } else {
936             cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
937           }
938           make_elem_inactive(elem);
939         } else {
940     //Remember: non-shared constraints only require that max(elem->value * var->value) < cnst->bound
941           cnst->usage = 0.0;
942           make_elem_inactive(elem);
943           elem_list = &(cnst->enabled_element_set);
944           xbt_swag_foreach(_elem, elem_list) {
945       elem = (lmm_element_t)_elem;
946       xbt_assert(elem->variable->weight > 0);
947       if (elem->variable->value > 0) continue;
948       if (elem->value > 0)
949         cnst->usage = MAX(cnst->usage, elem->value / elem->variable->weight);
950           }
951     //If the constraint is saturated, remove it from the set of active constraints (light_tab)
952           if(!double_positive(cnst->usage,sg_maxmin_precision) || !double_positive(cnst->remaining,cnst->bound*sg_maxmin_precision)) {
953             if(cnst->cnst_light) {
954               int index = (cnst->cnst_light-cnst_light_tab);
955               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  ",
956       index,cnst_light_num, cnst, cnst->cnst_light, cnst_light_tab, cnst->usage, cnst->remaining, cnst->bound);
957               cnst_light_tab[index]=cnst_light_tab[cnst_light_num-1];
958               cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
959               cnst_light_num--;
960               cnst->cnst_light = NULL;
961             }
962           } else {
963             cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
964             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." );
965           }
966         }
967       }
968       xbt_swag_remove(var, var_list);
969     }
970
971     /* Find out which variables reach the maximum */
972     min_usage = -1;
973     min_bound = -1;
974     saturated_constraint_set->pos = 0;
975     int pos;
976     for(pos=0; pos<cnst_light_num; pos++){
977       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);
978       saturated_constraint_set_update(
979           cnst_light_tab[pos].remaining_over_usage,
980           pos,
981           saturated_constraint_set,
982           &min_usage);
983   }
984
985     saturated_variable_set_update(  cnst_light_tab,
986                                     saturated_constraint_set,
987                                     sys);
988
989   } while (cnst_light_num > 0);
990
991   sys->modified = 0;
992   if (sys->selective_update_active)
993     lmm_remove_all_modified_set(sys);
994
995   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
996     lmm_print(sys);
997   }
998
999   lmm_check_concurrency(sys);
1000   
1001   xbt_free(saturated_constraint_set->data);
1002   xbt_free(saturated_constraint_set);
1003   xbt_free(cnst_light_tab);
1004   XBT_OUT();
1005 }
1006
1007
1008 /** \brief Attribute the value bound to var->bound.
1009  * 
1010  *  \param sys the lmm_system_t
1011  *  \param var the lmm_variable_t
1012  *  \param bound the new bound to associate with var
1013  * 
1014  *  Makes var->bound equal to bound. Whenever this function is called 
1015  *  a change is  signed in the system. To
1016  *  avoid false system changing detection it is a good idea to test 
1017  *  (bound != 0) before calling it.
1018  *
1019 */
1020 void lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
1021     double bound)
1022 {
1023   sys->modified = 1;
1024   var->bound = bound;
1025
1026   if (var->cnsts_number)
1027     lmm_update_modified_set(sys, var->cnsts[0].constraint);
1028 }
1029
1030
1031
1032 int lmm_concurrency_slack(lmm_constraint_t cnstr){
1033
1034   //FIXME MARTIN: Replace by infinite value std::numeric_limits<int>::(max)(), or something better within Simgrid?
1035   if(cnstr->concurrency_limit<0)
1036     return 666;
1037
1038   return  cnstr->concurrency_limit - cnstr->concurrency_current;  
1039 }
1040
1041 /** \brief Measure the minimum concurrency slack across all constraints where var is involved
1042  *
1043  * \param The variable to check for
1044  *
1045  */
1046 int lmm_cnstrs_min_concurrency_slack(lmm_variable_t var){
1047   int i;
1048   //FIXME MARTIN: Replace by infinite value std::numeric_limits<int>::(max)(), or something better within Simgrid?
1049   int slack,minslack=666;
1050   for (i = 0; i < var->cnsts_number; i++) {
1051     slack=lmm_concurrency_slack(var->cnsts[i].constraint);
1052     
1053     //This is only an optimization, to avoid looking at more constraints when slack is already zero
1054     //Disable it when debugging to let lmm_concurrency_slack catch nasty things
1055     if(!slack   && !XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug))
1056       return 0;
1057
1058     if(minslack>slack)
1059       minslack=slack;
1060   }
1061
1062   return minslack;
1063 }
1064
1065 /* /Check if a variable can be enabled
1066  *
1067  * Make sure to set staged_weight before, if your intent is only to check concurrency 
1068  */
1069 int lmm_can_enable_var(lmm_variable_t var){
1070   return var->staged_weight>0 && lmm_cnstrs_min_concurrency_slack(var)>=var->concurrency_share;
1071 }
1072
1073
1074 //Small remark: In this implementation of lmm_enable_var and lmm_disable_var, we will meet multiple times with var when running lmm_update_modified_set.
1075 //A priori not a big performance issue, but we might do better by calling lmm_update_modified_set within the for loops (after doing the first for enabling==1, and before doing the last for disabling==1)
1076
1077 void lmm_enable_var(lmm_system_t sys, lmm_variable_t var){
1078
1079   int i;
1080   lmm_element_t elem;
1081   
1082   xbt_assert(lmm_can_enable_var(var));
1083
1084   var->weight = var->staged_weight;
1085   var->staged_weight = 0;
1086
1087   //Enabling the variable, move to var to list head. Subtility is: here, we need to call lmm_update_modified_set AFTER moving at least one element of var.
1088
1089   xbt_swag_remove(var, &(sys->variable_set));
1090   xbt_swag_insert_at_head(var, &(sys->variable_set));
1091   for (i = 0; i < var->cnsts_number; i++) {
1092     elem = &var->cnsts[i];
1093     xbt_swag_remove(elem, &(elem->constraint->disabled_element_set));
1094     xbt_swag_insert_at_head(elem, &(elem->constraint->enabled_element_set));
1095     lmm_increase_concurrency(elem);
1096   }
1097   if (var->cnsts_number)
1098     lmm_update_modified_set(sys, var->cnsts[0].constraint);
1099
1100   //When used within lmm_on_disabled_var, we would get an assertion fail, because transiently there can be variables that are staged and could be activated.
1101   //Anyway, caller functions all call lmm_check_concurrency() in the end. 
1102   //  lmm_check_concurrency(sys);
1103 }
1104
1105 void lmm_disable_var(lmm_system_t sys, lmm_variable_t var){
1106   int i;
1107   lmm_element_t elem;
1108
1109   xbt_assert(!var->staged_weight,"Staged weight should have been cleared");
1110   //Disabling the variable, move to var to list tail. Subtility is: here, we need to call lmm_update_modified_set BEFORE moving the last element of var.
1111   xbt_swag_remove(var, &(sys->variable_set));
1112   xbt_swag_insert_at_tail(var, &(sys->variable_set));
1113   if (var->cnsts_number)
1114     lmm_update_modified_set(sys, var->cnsts[0].constraint);
1115   for (i = 0; i < var->cnsts_number; i++) {
1116     elem = &var->cnsts[i];
1117     xbt_swag_remove(elem, &(elem->constraint->enabled_element_set));
1118     xbt_swag_insert_at_tail(elem, &(elem->constraint->disabled_element_set));
1119
1120     xbt_swag_remove(elem, &(elem->constraint->active_element_set));
1121
1122     lmm_decrease_concurrency(elem);
1123   }
1124
1125   var->weight=0.0;
1126   var->staged_weight=0.0;
1127   var->value = 0.0;
1128   lmm_check_concurrency(sys);
1129 }
1130  
1131 /* /brief Find variables that can be enabled and enable them.
1132  * 
1133  * Assuming that the variable has already been removed from non-zero weights
1134  * Can we find a staged variable to add?
1135  * If yes, check that none of the constraints that this variable is involved in is at the limit of its concurrency
1136  * And then add it to enabled variables
1137  */
1138 void lmm_on_disabled_var(lmm_system_t sys, lmm_constraint_t cnstr){
1139
1140   lmm_element_t elem;
1141   lmm_element_t nextelem;
1142   int numelem;
1143   
1144   if(cnstr->concurrency_limit<0)
1145     return;
1146
1147   numelem=xbt_swag_size(&(cnstr->disabled_element_set));
1148   if(!numelem)
1149     return;
1150
1151   elem= (lmm_element_t) xbt_swag_getFirst(&(cnstr->disabled_element_set));
1152
1153   //Cannot use xbt_swag_foreach, because lmm_enable_var will modify disabled_element_set.. within the loop
1154   while(numelem-- && elem ){
1155
1156     nextelem = (lmm_element_t) xbt_swag_getNext(elem, cnstr->disabled_element_set.offset);      
1157
1158     if (elem->variable->staged_weight>0 )
1159       {
1160         //Found a staged variable
1161         //TODOLATER: Add random timing function to model reservation protocol fuzziness? Then how to make sure that staged variables will eventually be called?
1162         if(lmm_can_enable_var(elem->variable)){
1163           lmm_enable_var(sys,elem->variable);
1164         }        
1165       }
1166
1167     xbt_assert(cnstr->concurrency_current<=cnstr->concurrency_limit,"Concurrency overflow!");
1168     if(cnstr->concurrency_current==cnstr->concurrency_limit)
1169       break;
1170
1171     elem = nextelem;
1172   }
1173
1174   //We could get an assertion fail, because transiently there can be variables that are staged and could be activated.
1175   //And we need to go through all constraints of the disabled var before getting back a coherent state.
1176   //Anyway, caller functions all call lmm_check_concurrency() in the end. 
1177   //  lmm_check_concurrency(sys);
1178
1179 }
1180
1181 /* \brief update the weight of a variable, and enable/disable it.
1182  * @return Returns whether a change was made
1183  */
1184 void lmm_update_variable_weight(lmm_system_t sys, lmm_variable_t var,
1185              double weight)
1186 {
1187   int minslack;
1188   
1189   xbt_assert(weight>=0,"Variable weight should not be negative!");
1190   
1191   if (weight == var->weight)
1192     return;
1193
1194   int enabling_var=  (weight>0 && var->weight<=0);
1195   int disabling_var= (weight<=0 && var->weight>0);
1196  
1197   XBT_IN("(sys=%p, var=%p, weight=%f)", sys, var, weight);
1198
1199   sys->modified = 1;
1200   
1201   //Are we enabling this variable?
1202   if (enabling_var){
1203     var->staged_weight = weight;
1204     minslack=lmm_cnstrs_min_concurrency_slack(var);
1205     if(minslack<var->concurrency_share){      
1206       XBT_DEBUG("Staging var (instead of enabling) because min concurrency slack %i, with weight %f and concurrency share %i", minslack, weight, var->concurrency_share);
1207       return;
1208     }
1209     XBT_DEBUG("Enabling var with min concurrency slack %i", minslack);
1210     lmm_enable_var(sys,var);   
1211   } else if (disabling_var){
1212     //Are we disabling this variable?
1213     lmm_disable_var(sys,var);       
1214   } else {
1215     var->weight=weight;
1216   }
1217
1218   lmm_check_concurrency(sys);
1219   
1220   XBT_OUT();
1221   return;
1222 }
1223
1224 double lmm_get_variable_weight(lmm_variable_t var)
1225 {
1226   return var->weight;
1227 }
1228
1229 void lmm_update_constraint_bound(lmm_system_t sys,
1230                                             lmm_constraint_t cnst,
1231                                             double bound)
1232 {
1233   sys->modified = 1;
1234   lmm_update_modified_set(sys, cnst);
1235   cnst->bound = bound;
1236 }
1237
1238 int lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst)
1239 {
1240   return xbt_swag_belongs(cnst, &(sys->active_constraint_set));
1241 }
1242
1243 XBT_INLINE lmm_constraint_t lmm_get_first_active_constraint(lmm_system_t
1244                                                             sys)
1245 {
1246   return (lmm_constraint_t)xbt_swag_getFirst(&(sys->active_constraint_set));
1247 }
1248
1249 XBT_INLINE lmm_constraint_t lmm_get_next_active_constraint(lmm_system_t
1250                                                            sys,
1251                                                            lmm_constraint_t
1252                                                            cnst)
1253 {
1254   return (lmm_constraint_t)xbt_swag_getNext(cnst, (sys->active_constraint_set).offset);
1255 }
1256
1257 #ifdef HAVE_LATENCY_BOUND_TRACKING
1258 XBT_PUBLIC(int) lmm_is_variable_limited_by_latency(lmm_variable_t var)
1259 {
1260   return (double_equals(var->bound, var->value, var->bound*sg_maxmin_precision));
1261 }
1262 #endif
1263
1264
1265 /** \brief Update the constraint set propagating recursively to
1266  *  other constraints so the system should not be entirely computed.
1267  *
1268  *  \param sys the lmm_system_t
1269  *  \param cnst the lmm_constraint_t affected by the change
1270  *
1271  *  A recursive algorithm to optimize the system recalculation selecting only
1272  *  constraints that have changed. Each constraint change is propagated
1273  *  to the list of constraints for each variable.
1274  */
1275 static void lmm_update_modified_set_rec(lmm_system_t sys,
1276                                         lmm_constraint_t cnst)
1277 {
1278   void* _elem;
1279
1280   //TODOLATER: Why lmm_modified_set has been changed in git version 2392B5157...? Looks equivalent logically and less obvious..
1281   
1282   xbt_swag_foreach(_elem, &cnst->enabled_element_set) {
1283     lmm_variable_t var = ((lmm_element_t)_elem)->variable;
1284     s_lmm_element_t *cnsts = var->cnsts;
1285     int i;
1286     for (i = 0; var->visited != sys->visited_counter
1287              && i < var->cnsts_number ; i++) {
1288       if (cnsts[i].constraint != cnst
1289           && !xbt_swag_belongs(cnsts[i].constraint,
1290                                &sys->modified_constraint_set)) {
1291         xbt_swag_insert(cnsts[i].constraint, &sys->modified_constraint_set);
1292         lmm_update_modified_set_rec(sys, cnsts[i].constraint);
1293       }
1294     }
1295     //var will be ignored in later visits as long as sys->visited_counter does not move 
1296     var->visited = sys->visited_counter;
1297   }
1298 }
1299
1300 static void lmm_update_modified_set(lmm_system_t sys,
1301                                     lmm_constraint_t cnst)
1302 {
1303   /* nothing to do if selective update isn't active */
1304   if (sys->selective_update_active
1305       && !xbt_swag_belongs(cnst, &sys->modified_constraint_set)) {
1306     xbt_swag_insert(cnst, &sys->modified_constraint_set);
1307     lmm_update_modified_set_rec(sys, cnst);
1308   }
1309 }
1310
1311 /** \brief Remove all constraints of the modified_constraint_set.
1312  *
1313  *  \param sys the lmm_system_t
1314  *
1315  */
1316 static void lmm_remove_all_modified_set(lmm_system_t sys)
1317 {
1318
1319   //We cleverly un-flag all variables just by incrementing sys->visited_counter
1320   //In effect, the var->visited value will no more be equal to sys->visited counter
1321   //To be clean, when visited counter has wrapped around, we force these var->visited values so that variables that were in the modified a long (long long) time ago are not wrongly skipped here, which would lead to very nasty bugs (i.e. not readibily reproducible, and requiring a lot of run time before happening).  
1322   if (++sys->visited_counter == 1) {
1323     /* the counter wrapped around, reset each variable->visited */
1324   void *_var;
1325     xbt_swag_foreach(_var, &sys->variable_set)
1326       ((lmm_variable_t)_var)->visited = 0;
1327   } 
1328   xbt_swag_reset(&sys->modified_constraint_set);
1329 }
1330
1331 /**
1332  *  Returns total resource load
1333  *
1334  * \param cnst the lmm_constraint_t associated to the resource
1335  *
1336  *
1337  * This is dead code, but we may use it later for debug/trace.
1338  */
1339 double lmm_constraint_get_usage(lmm_constraint_t cnst) {
1340    double usage = 0.0;
1341    xbt_swag_t elem_list = &(cnst->enabled_element_set);
1342    void *_elem;
1343    lmm_element_t elem = NULL;
1344
1345    xbt_swag_foreach(_elem, elem_list) {
1346    elem = (lmm_element_t)_elem;
1347      if ((elem->value > 0)) {
1348        if (cnst->sharing_policy)
1349          usage += elem->value * elem->variable->value;
1350        else if (usage < elem->value * elem->variable->value)
1351          usage = elem->value * elem->variable->value;
1352      }
1353    }
1354   return usage;
1355 }
1356
1357 void lmm_check_concurrency(lmm_system_t sys){
1358   void* _cnst;
1359   void* _elem;
1360   void* _var;
1361   lmm_element_t elem;
1362   lmm_constraint_t cnst;
1363   lmm_variable_t var;
1364   int concurrency;
1365   int i,belong_to_enabled,belong_to_disabled,belong_to_active;
1366   
1367   //These checks are very expensive, so do them only if we want to debug SURF LMM
1368   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
1369   
1370     xbt_swag_foreach(_cnst, &(sys->constraint_set)) {
1371       cnst = (lmm_constraint_t) _cnst;
1372       concurrency=0;
1373       xbt_swag_foreach(_elem, &(cnst->enabled_element_set)) {
1374         elem = (lmm_element_t)_elem;
1375         xbt_assert(elem->variable->weight > 0);
1376         concurrency+=lmm_element_concurrency(elem);
1377       }
1378       
1379       xbt_swag_foreach(_elem, &(cnst->disabled_element_set)) {
1380   elem = (lmm_element_t)_elem;
1381   //We should have staged variables only if concurrency is reached in some constraint
1382   xbt_assert(cnst->concurrency_limit<0 || elem->variable->staged_weight==0 || lmm_cnstrs_min_concurrency_slack(elem->variable) < elem->variable->concurrency_share,"should not have staged variable!");
1383       }
1384       
1385       xbt_assert(cnst->concurrency_limit<0 || cnst->concurrency_limit >= concurrency,"concurrency check failed!");
1386       xbt_assert(cnst->concurrency_current == concurrency, "concurrency_current is out-of-date!");
1387     }
1388
1389
1390     //Check that for each variable, all corresponding elements are in the same state (i.e. same element sets)
1391     xbt_swag_foreach(_var, &(sys->variable_set)) {
1392       var= (lmm_variable_t) _var;
1393
1394       if(!var->cnsts_number)
1395         continue;
1396
1397       elem = &var->cnsts[0];
1398       belong_to_enabled=xbt_swag_belongs(elem,&(elem->constraint->enabled_element_set));
1399       belong_to_disabled=xbt_swag_belongs(elem,&(elem->constraint->disabled_element_set));
1400       belong_to_active=xbt_swag_belongs(elem,&(elem->constraint->active_element_set));
1401
1402       for (i = 1; i < var->cnsts_number; i++) {
1403         elem = &var->cnsts[i];
1404         xbt_assert(belong_to_enabled==xbt_swag_belongs(elem,&(elem->constraint->enabled_element_set)),
1405                    "Variable inconsistency (1): enabled_element_set");
1406         xbt_assert(belong_to_disabled==xbt_swag_belongs(elem,&(elem->constraint->disabled_element_set)),
1407            "Variable inconsistency (2): disabled_element_set");
1408         xbt_assert(belong_to_active==xbt_swag_belongs(elem,&(elem->constraint->active_element_set)),
1409                    "Variable inconsistency (3): active_element_set");
1410       }
1411       
1412     }
1413   }
1414 }