Logo AND Algorithmique Numérique Distribuée

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