Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4595fbb5dc3d0f8626adce9c2153ec6e11c75c32
[simgrid.git] / src / surf / maxmin.cpp
1 /* Copyright (c) 2004-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /* \file callbacks.h */
7
8 #include "maxmin_private.hpp"
9 #include "xbt/backtrace.hpp"
10 #include "xbt/log.h"
11 #include "xbt/mallocator.h"
12 #include "xbt/sysdep.h"
13 #include <algorithm>
14 #include <cmath>
15 #include <cstdlib>
16 #include <cxxabi.h>
17 #include <limits>
18 #include <vector>
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_maxmin, surf, "Logging specific to SURF (maxmin)");
21
22 typedef std::vector<int> dyn_light_t;
23
24 double sg_maxmin_precision = 0.00001; /* Change this with --cfg=maxmin/precision:VALUE */
25 double sg_surf_precision   = 0.00001; /* Change this with --cfg=surf/precision:VALUE */
26 int sg_concurrency_limit   = -1;      /* Change this with --cfg=maxmin/concurrency-limit:VALUE */
27
28 static void *lmm_variable_mallocator_new_f();
29 static void lmm_variable_mallocator_free_f(void *var);
30 #define lmm_variable_mallocator_reset_f ((void_f_pvoid_t)nullptr)
31 static void lmm_update_modified_set(lmm_system_t sys, lmm_constraint_t cnst);
32 static void lmm_remove_all_modified_set(lmm_system_t sys);
33 static int Global_debug_id = 1;
34 static int Global_const_debug_id = 1;
35
36 static inline void lmm_cnst_free(lmm_system_t sys, lmm_constraint_t cnst);
37
38 static void lmm_on_disabled_var(lmm_system_t sys, lmm_constraint_t cnstr);
39 static void lmm_enable_var(lmm_system_t sys, lmm_variable_t var);
40 static int lmm_can_enable_var(lmm_variable_t var);
41 static void lmm_disable_var(lmm_system_t sys, lmm_variable_t var);
42 static int lmm_concurrency_slack(lmm_constraint_t cnstr);
43 static int lmm_cnstrs_min_concurrency_slack(lmm_variable_t var);
44
45 static inline int lmm_element_concurrency(lmm_element_t elem)
46 {
47   //Ignore element with weight less than one (e.g. cross-traffic)
48   return (elem->consumption_weight >= 1) ? 1 : 0;
49   //There are other alternatives, but they will change the behaviour of the model..
50   //So do not use it unless you want to make a new model.
51   //If you do, remember to change the variables concurrency share to reflect it.
52   //Potential examples are:
53   //return (elem->weight>0)?1:0;//Include element as soon  as weight is non-zero
54   //return (int)ceil(elem->weight);//Include element as the rounded-up integer value of the element weight
55 }
56
57 static inline void lmm_decrease_concurrency(lmm_element_t elem)
58 {
59   xbt_assert(elem->constraint->concurrency_current>=lmm_element_concurrency(elem));
60   elem->constraint->concurrency_current-=lmm_element_concurrency(elem);
61 }
62
63 static inline void lmm_increase_concurrency(lmm_element_t elem)
64 {
65   elem->constraint->concurrency_current+= lmm_element_concurrency(elem);
66
67   lmm_constraint_t cnstr=elem->constraint;
68
69   if(cnstr->concurrency_current > cnstr->concurrency_maximum)
70     cnstr->concurrency_maximum= cnstr->concurrency_current;
71
72   xbt_assert(cnstr->concurrency_limit<0 || cnstr->concurrency_current<=cnstr->concurrency_limit,
73              "Concurrency limit overflow!");
74 }
75
76 static void lmm_check_concurrency(lmm_system_t sys)
77 {
78   // These checks are very expensive, so do them only if we want to debug SURF LMM
79   if (not XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug))
80     return;
81
82   void* cnstIt;
83   xbt_swag_foreach(cnstIt, &(sys->constraint_set))
84   {
85     lmm_constraint_t cnst = (lmm_constraint_t)cnstIt;
86     int concurrency       = 0;
87     void* elemIt;
88     xbt_swag_foreach(elemIt, &(cnst->enabled_element_set))
89     {
90       lmm_element_t elem = (lmm_element_t)elemIt;
91       xbt_assert(elem->variable->sharing_weight > 0);
92       concurrency += lmm_element_concurrency(elem);
93     }
94
95     xbt_swag_foreach(elemIt, &(cnst->disabled_element_set))
96     {
97       lmm_element_t elem = (lmm_element_t)elemIt;
98       // We should have staged variables only if concurrency is reached in some constraint
99       xbt_assert(cnst->concurrency_limit < 0 || elem->variable->staged_weight == 0 ||
100                      lmm_cnstrs_min_concurrency_slack(elem->variable) < elem->variable->concurrency_share,
101                  "should not have staged variable!");
102     }
103
104     xbt_assert(cnst->concurrency_limit < 0 || cnst->concurrency_limit >= concurrency, "concurrency check failed!");
105     xbt_assert(cnst->concurrency_current == concurrency, "concurrency_current is out-of-date!");
106   }
107
108   // Check that for each variable, all corresponding elements are in the same state (i.e. same element sets)
109   void* varIt;
110   xbt_swag_foreach(varIt, &(sys->variable_set))
111   {
112     lmm_variable_t var = (lmm_variable_t)varIt;
113
114     if (var->cnsts.empty())
115       continue;
116
117     lmm_element_t elem     = &var->cnsts[0];
118     int belong_to_enabled  = xbt_swag_belongs(elem, &(elem->constraint->enabled_element_set));
119     int belong_to_disabled = xbt_swag_belongs(elem, &(elem->constraint->disabled_element_set));
120     int belong_to_active   = xbt_swag_belongs(elem, &(elem->constraint->active_element_set));
121
122     for (s_lmm_element_t const& elem : var->cnsts) {
123       xbt_assert(belong_to_enabled == xbt_swag_belongs(&elem, &(elem.constraint->enabled_element_set)),
124                  "Variable inconsistency (1): enabled_element_set");
125       xbt_assert(belong_to_disabled == xbt_swag_belongs(&elem, &(elem.constraint->disabled_element_set)),
126                  "Variable inconsistency (2): disabled_element_set");
127       xbt_assert(belong_to_active == xbt_swag_belongs(&elem, &(elem.constraint->active_element_set)),
128                  "Variable inconsistency (3): active_element_set");
129     }
130   }
131 }
132
133 static void lmm_var_free(lmm_system_t sys, lmm_variable_t var)
134 {
135   XBT_IN("(sys=%p, var=%p)", sys, var);
136   sys->modified = 1;
137
138   // TODOLATER Can do better than that by leaving only the variable in only one enabled_element_set, call
139   // lmm_update_modified_set, and then remove it..
140   if (not var->cnsts.empty())
141     lmm_update_modified_set(sys, var->cnsts[0].constraint);
142
143   for (s_lmm_element_t& elem : var->cnsts) {
144     if (var->sharing_weight > 0)
145       lmm_decrease_concurrency(&elem);
146     xbt_swag_remove(&elem, &(elem.constraint->enabled_element_set));
147     xbt_swag_remove(&elem, &(elem.constraint->disabled_element_set));
148     xbt_swag_remove(&elem, &(elem.constraint->active_element_set));
149     int nelements = xbt_swag_size(&(elem.constraint->enabled_element_set)) +
150                     xbt_swag_size(&(elem.constraint->disabled_element_set));
151     if (nelements == 0)
152       make_constraint_inactive(sys, elem.constraint);
153     else
154       lmm_on_disabled_var(sys, elem.constraint);
155   }
156
157   // Check if we can enable new variables going through the constraints where var was.
158   // Do it after removing all elements, so the first disabled variables get priority over those with smaller requirement
159   for (s_lmm_element_t& elem : var->cnsts) {
160     if (xbt_swag_size(&(elem.constraint->disabled_element_set)))
161       lmm_on_disabled_var(sys, elem.constraint);
162   }
163
164   var->cnsts.clear();
165
166   lmm_check_concurrency(sys);
167
168   xbt_mallocator_release(sys->variable_mallocator, var);
169   XBT_OUT();
170 }
171
172 lmm_system_t lmm_system_new(bool selective_update)
173 {
174   s_lmm_variable_t var;
175   s_lmm_constraint_t cnst;
176
177   lmm_system_t l = new s_lmm_system_t();
178
179   l->modified = 0;
180   l->selective_update_active = selective_update;
181   l->visited_counter = 1;
182
183   XBT_DEBUG("Setting selective_update_active flag to %d", l->selective_update_active);
184
185   xbt_swag_init(&(l->variable_set), xbt_swag_offset(var, variable_set_hookup));
186   xbt_swag_init(&(l->constraint_set), xbt_swag_offset(cnst, constraint_set_hookup));
187
188   xbt_swag_init(&(l->active_constraint_set), xbt_swag_offset(cnst, active_constraint_set_hookup));
189
190   xbt_swag_init(&(l->modified_constraint_set), xbt_swag_offset(cnst, modified_constraint_set_hookup));
191   xbt_swag_init(&(l->saturated_variable_set), xbt_swag_offset(var, saturated_variable_set_hookup));
192   xbt_swag_init(&(l->saturated_constraint_set), xbt_swag_offset(cnst, saturated_constraint_set_hookup));
193
194   l->variable_mallocator = xbt_mallocator_new(65536,
195                                               lmm_variable_mallocator_new_f,
196                                               lmm_variable_mallocator_free_f,
197                                               lmm_variable_mallocator_reset_f);
198
199   l->solve_fun = &lmm_solve;
200
201   return l;
202 }
203
204 void lmm_system_free(lmm_system_t sys)
205 {
206   lmm_variable_t var = nullptr;
207   lmm_constraint_t cnst = nullptr;
208
209   if (sys == nullptr)
210     return;
211
212   while ((var = (lmm_variable_t) extract_variable(sys))) {
213     auto demangled = simgrid::xbt::demangle(typeid(*var->id).name());
214     XBT_WARN("Probable bug: a %s variable (#%d) not removed before the LMM system destruction.", demangled.get(),
215              var->id_int);
216     lmm_var_free(sys, var);
217   }
218   while ((cnst = (lmm_constraint_t) extract_constraint(sys)))
219     lmm_cnst_free(sys, cnst);
220
221   xbt_mallocator_free(sys->variable_mallocator);
222   delete sys;
223 }
224
225 static inline void lmm_cnst_free(lmm_system_t sys, lmm_constraint_t cnst)
226 {
227   make_constraint_inactive(sys, cnst);
228   delete cnst;
229 }
230
231 lmm_constraint_t lmm_constraint_new(lmm_system_t sys, void *id, double bound_value)
232 {
233   lmm_constraint_t cnst = nullptr;
234   s_lmm_element_t elem;
235
236   cnst         = new s_lmm_constraint_t();
237   cnst->id     = id;
238   cnst->id_int = Global_const_debug_id++;
239   xbt_swag_init(&(cnst->enabled_element_set), xbt_swag_offset(elem, enabled_element_set_hookup));
240   xbt_swag_init(&(cnst->disabled_element_set), xbt_swag_offset(elem, disabled_element_set_hookup));
241   xbt_swag_init(&(cnst->active_element_set), xbt_swag_offset(elem, active_element_set_hookup));
242
243   cnst->bound = bound_value;
244   cnst->concurrency_maximum=0;
245   cnst->concurrency_current=0;
246   cnst->concurrency_limit  = sg_concurrency_limit;
247   cnst->usage = 0;
248   cnst->sharing_policy = 1; /* FIXME: don't hardcode the value */
249   insert_constraint(sys, cnst);
250
251   return cnst;
252 }
253
254 int lmm_constraint_concurrency_limit_get(lmm_constraint_t cnst)
255 {
256  return cnst->concurrency_limit;
257 }
258
259 void lmm_constraint_concurrency_limit_set(lmm_constraint_t cnst, int concurrency_limit)
260 {
261   xbt_assert(concurrency_limit<0 || cnst->concurrency_maximum<=concurrency_limit,
262              "New concurrency limit should be larger than observed concurrency maximum. Maybe you want to call"
263              " lmm_constraint_concurrency_maximum_reset() to reset the maximum?");
264   cnst->concurrency_limit = concurrency_limit;
265 }
266
267 void lmm_constraint_concurrency_maximum_reset(lmm_constraint_t cnst)
268 {
269   cnst->concurrency_maximum = 0;
270 }
271
272 int lmm_constraint_concurrency_maximum_get(lmm_constraint_t cnst)
273 {
274  xbt_assert(cnst->concurrency_limit<0 || cnst->concurrency_maximum<=cnst->concurrency_limit,
275             "Very bad: maximum observed concurrency is higher than limit. This is a bug of SURF, please report it.");
276   return cnst->concurrency_maximum;
277 }
278
279 void lmm_constraint_shared(lmm_constraint_t cnst)
280 {
281   cnst->sharing_policy = 0;
282 }
283
284 /** Return true if the constraint is shared, and false if it's FATPIPE */
285 int lmm_constraint_sharing_policy(lmm_constraint_t cnst)
286 {
287   return (cnst->sharing_policy);
288 }
289
290 static void *lmm_variable_mallocator_new_f()
291 {
292   return new s_lmm_variable_t;
293 }
294
295 static void lmm_variable_mallocator_free_f(void *var)
296 {
297   delete static_cast<lmm_variable_t>(var);
298 }
299
300 lmm_variable_t lmm_variable_new(lmm_system_t sys, simgrid::surf::Action* id, double sharing_weight, double bound,
301                                 int number_of_constraints)
302 {
303   XBT_IN("(sys=%p, id=%p, weight=%f, bound=%f, num_cons =%d)", sys, id, sharing_weight, bound, number_of_constraints);
304
305   lmm_variable_t var = (lmm_variable_t)xbt_mallocator_get(sys->variable_mallocator);
306   var->id = id;
307   var->id_int = Global_debug_id++;
308   var->cnsts.reserve(number_of_constraints);
309   var->sharing_weight    = sharing_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 = nullptr;
322   var->variable_set_hookup.prev = nullptr;
323   var->saturated_variable_set_hookup.next = nullptr;
324   var->saturated_variable_set_hookup.prev = nullptr;
325
326   if (sharing_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 void lmm_variable_concurrency_share_set(lmm_variable_t var, short int concurrency_share)
347 {
348   var->concurrency_share=concurrency_share;
349 }
350
351 double lmm_variable_getbound(lmm_variable_t var)
352 {
353   return (var->bound);
354 }
355
356 void lmm_expand(lmm_system_t sys, lmm_constraint_t cnst, lmm_variable_t var, double consumption_weight)
357 {
358   sys->modified = 1;
359
360   //Check if this variable already has an active element in this constraint
361   //If it does, substract it from the required slack
362   int current_share = 0;
363   if(var->concurrency_share>1){
364     for (s_lmm_element_t& elem : var->cnsts) {
365       if (elem.constraint == cnst && xbt_swag_belongs(&elem, &(elem.constraint->enabled_element_set)))
366         current_share += lmm_element_concurrency(&elem);
367     }
368   }
369
370   //Check if we need to disable the variable
371   if (var->sharing_weight > 0 && var->concurrency_share - current_share > lmm_concurrency_slack(cnst)) {
372     double weight = var->sharing_weight;
373     lmm_disable_var(sys,var);
374     for (s_lmm_element_t const& elem : var->cnsts)
375       lmm_on_disabled_var(sys, elem.constraint);
376     consumption_weight = 0;
377     var->staged_weight=weight;
378     xbt_assert(not var->sharing_weight);
379   }
380
381   xbt_assert(var->cnsts.size() < var->cnsts.capacity(), "Too much constraints");
382
383   var->cnsts.resize(var->cnsts.size() + 1);
384   s_lmm_element_t& elem = var->cnsts.back();
385
386   elem.consumption_weight = consumption_weight;
387   elem.constraint         = cnst;
388   elem.variable           = var;
389
390   if (var->sharing_weight) {
391     xbt_swag_insert_at_head(&elem, &(elem.constraint->enabled_element_set));
392     lmm_increase_concurrency(&elem);
393   } else
394     xbt_swag_insert_at_tail(&elem, &(elem.constraint->disabled_element_set));
395
396   if (not sys->selective_update_active) {
397     make_constraint_active(sys, cnst);
398   } else if (elem.consumption_weight > 0 || var->sharing_weight > 0) {
399     make_constraint_active(sys, cnst);
400     lmm_update_modified_set(sys, cnst);
401     //TODOLATER: Why do we need this second call?
402     if (var->cnsts.size() > 1)
403       lmm_update_modified_set(sys, var->cnsts[0].constraint);
404   }
405
406   lmm_check_concurrency(sys);
407 }
408
409 void lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst, lmm_variable_t var, double value)
410 {
411   sys->modified = 1;
412
413   lmm_check_concurrency(sys);
414
415   //BEWARE: In case you have multiple elements in one constraint, this will always add value to the first element.
416   auto elem_it = std::find_if(begin(var->cnsts), end(var->cnsts),
417                               [&cnst](s_lmm_element_t const& x) { return x.constraint == cnst; });
418   if (elem_it != end(var->cnsts)) {
419     s_lmm_element_t& elem = *elem_it;
420     if (var->sharing_weight)
421       lmm_decrease_concurrency(&elem);
422
423     if (cnst->sharing_policy)
424       elem.consumption_weight += value;
425     else
426       elem.consumption_weight = std::max(elem.consumption_weight, value);
427
428     //We need to check that increasing value of the element does not cross the concurrency limit
429     if (var->sharing_weight) {
430       if (lmm_concurrency_slack(cnst) < lmm_element_concurrency(&elem)) {
431         double weight = var->sharing_weight;
432         lmm_disable_var(sys,var);
433         for (s_lmm_element_t const& elem2 : var->cnsts)
434           lmm_on_disabled_var(sys, elem2.constraint);
435         var->staged_weight=weight;
436         xbt_assert(not var->sharing_weight);
437       }
438       lmm_increase_concurrency(&elem);
439     }
440     lmm_update_modified_set(sys, cnst);
441   } else
442     lmm_expand(sys, cnst, var, value);
443
444   lmm_check_concurrency(sys);
445 }
446
447 lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t /*sys*/, lmm_variable_t var, unsigned num)
448 {
449   if (num < var->cnsts.size())
450     return (var->cnsts[num].constraint);
451   else
452     return nullptr;
453 }
454
455 double lmm_get_cnst_weight_from_var(lmm_system_t /*sys*/, lmm_variable_t var, unsigned num)
456 {
457   if (num < var->cnsts.size())
458     return (var->cnsts[num].consumption_weight);
459   else
460     return 0.0;
461 }
462
463 int lmm_get_number_of_cnst_from_var(lmm_system_t /*sys*/, lmm_variable_t var)
464 {
465   return (var->cnsts.size());
466 }
467
468 lmm_variable_t lmm_get_var_from_cnst(lmm_system_t /*sys*/, lmm_constraint_t cnst, lmm_element_t * elem)
469 {
470   if (*elem == nullptr) {
471     // That is the first call, pick the first element among enabled_element_set (or disabled_element_set if
472     // enabled_element_set is empty)
473     *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->enabled_element_set));
474     if (*elem == nullptr)
475       *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->disabled_element_set));
476   } else {
477     //elem is not null, so we carry on
478     if(xbt_swag_belongs(*elem,&(cnst->enabled_element_set))){
479       //Look at enabled_element_set, and jump to disabled_element_set when finished
480       *elem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->enabled_element_set.offset);
481       if (*elem == nullptr)
482         *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->disabled_element_set));
483     } else {
484       *elem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->disabled_element_set.offset);
485     }
486   }
487   if (*elem)
488     return (*elem)->variable;
489   else
490     return nullptr;
491 }
492
493 //if we modify the swag between calls, normal version may loop forever
494 //this safe version ensures that we browse the swag elements only once
495 lmm_variable_t lmm_get_var_from_cnst_safe(lmm_system_t /*sys*/, lmm_constraint_t cnst, lmm_element_t * elem,
496                                           lmm_element_t * nextelem, int * numelem)
497 {
498   if (*elem == nullptr) {
499     *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->enabled_element_set));
500     *numelem = xbt_swag_size(&(cnst->enabled_element_set))+xbt_swag_size(&(cnst->disabled_element_set))-1;
501     if (*elem == nullptr)
502       *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->disabled_element_set));
503   }else{
504     *elem = *nextelem;
505     if(*numelem>0){
506      (*numelem) --;
507     }else
508       return nullptr;
509   }
510   if (*elem){
511     //elem is not null, so we carry on
512     if(xbt_swag_belongs(*elem,&(cnst->enabled_element_set))){
513       //Look at enabled_element_set, and jump to disabled_element_set when finished
514       *nextelem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->enabled_element_set.offset);
515       if (*nextelem == nullptr)
516         *nextelem = (lmm_element_t) xbt_swag_getFirst(&(cnst->disabled_element_set));
517     } else {
518       *nextelem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->disabled_element_set.offset);
519     }
520     return (*elem)->variable;
521   }else
522     return nullptr;
523 }
524
525 void *lmm_constraint_id(lmm_constraint_t cnst)
526 {
527   return cnst->id;
528 }
529
530 void *lmm_variable_id(lmm_variable_t var)
531 {
532   return var->id;
533 }
534
535 static inline void saturated_constraint_set_update(double usage, int cnst_light_num,
536                                                    dyn_light_t& saturated_constraint_set, double* min_usage)
537 {
538   xbt_assert(usage > 0,"Impossible");
539
540   if (*min_usage < 0 || *min_usage > usage) {
541     *min_usage = usage;
542     XBT_HERE(" min_usage=%f (cnst->remaining / cnst->usage =%f)", *min_usage, usage);
543     saturated_constraint_set.assign(1, cnst_light_num);
544   } else if (*min_usage == usage) {
545     saturated_constraint_set.emplace_back(cnst_light_num);
546   }
547 }
548
549 static inline void saturated_variable_set_update(s_lmm_constraint_light_t* cnst_light_tab,
550                                                  const dyn_light_t& saturated_constraint_set, lmm_system_t sys)
551 {
552   /* Add active variables (i.e. variables that need to be set) from the set of constraints to saturate (cnst_light_tab)*/
553   for (int const& saturated_cnst : saturated_constraint_set) {
554     lmm_constraint_light_t cnst = &cnst_light_tab[saturated_cnst];
555     void* _elem;
556     xbt_swag_t elem_list = &(cnst->cnst->active_element_set);
557     xbt_swag_foreach(_elem, elem_list) {
558       lmm_element_t elem = (lmm_element_t)_elem;
559       //Visiting active_element_set, so, by construction, should never get a zero weight, correct?
560       xbt_assert(elem->variable->sharing_weight > 0);
561       if (elem->consumption_weight > 0)
562         xbt_swag_insert(elem->variable, &(sys->saturated_variable_set));
563     }
564   }
565 }
566
567 void lmm_print(lmm_system_t sys)
568 {
569   std::string buf       = std::string("MAX-MIN ( ");
570   void* _var;
571
572   /* Printing Objective */
573   xbt_swag_t var_list = &(sys->variable_set);
574   xbt_swag_foreach(_var, var_list) {
575     lmm_variable_t var = (lmm_variable_t)_var;
576     buf = buf + "'" + std::to_string(var->id_int) + "'(" + std::to_string(var->sharing_weight) + ") ";
577   }
578   buf += ")";
579   XBT_DEBUG("%20s", buf.c_str());
580   buf.clear();
581
582   XBT_DEBUG("Constraints");
583   /* Printing Constraints */
584   void* _cnst;
585   xbt_swag_t cnst_list = &(sys->active_constraint_set);
586   xbt_swag_foreach(_cnst, cnst_list) {
587     lmm_constraint_t cnst = (lmm_constraint_t)_cnst;
588     double sum = 0.0;
589     //Show  the enabled variables
590     void* _elem;
591     xbt_swag_t elem_list = &(cnst->enabled_element_set);
592     buf += "\t";
593     buf += ((cnst->sharing_policy) ? "(" : "max(");
594     xbt_swag_foreach(_elem, elem_list) {
595       lmm_element_t elem = (lmm_element_t)_elem;
596       buf  = buf + std::to_string(elem->consumption_weight) + ".'" + std::to_string(elem->variable->id_int) + "'(" +
597             std::to_string(elem->variable->value) + ")" + ((cnst->sharing_policy) ? " + " : " , ");
598       if(cnst->sharing_policy)
599         sum += elem->consumption_weight * elem->variable->value;
600       else
601         sum = std::max(sum, elem->consumption_weight * elem->variable->value);
602     }
603     //TODO: Adding disabled elements only for test compatibility, but do we really want them to be printed?
604     elem_list = &(cnst->disabled_element_set);
605     xbt_swag_foreach(_elem, elem_list) {
606       lmm_element_t elem = (lmm_element_t)_elem;
607       buf  = buf + std::to_string(elem->consumption_weight) + ".'" + std::to_string(elem->variable->id_int) + "'(" +
608             std::to_string(elem->variable->value) + ")" + ((cnst->sharing_policy) ? " + " : " , ");
609       if(cnst->sharing_policy)
610         sum += elem->consumption_weight * elem->variable->value;
611       else
612         sum = std::max(sum, elem->consumption_weight * elem->variable->value);
613     }
614
615     buf = buf + "0) <= " + std::to_string(cnst->bound) + " ('" + std::to_string(cnst->id_int) + "')";
616
617     if (not cnst->sharing_policy) {
618       buf += " [MAX-Constraint]";
619     }
620     XBT_DEBUG("%s", buf.c_str());
621     buf.clear();
622     xbt_assert(not double_positive(sum - cnst->bound, cnst->bound * sg_maxmin_precision),
623                "Incorrect value (%f is not smaller than %f): %g", sum, cnst->bound, sum - cnst->bound);
624   }
625
626   XBT_DEBUG("Variables");
627   /* Printing Result */
628   xbt_swag_foreach(_var, var_list) {
629     lmm_variable_t var = (lmm_variable_t)_var;
630     if (var->bound > 0) {
631       XBT_DEBUG("'%d'(%f) : %f (<=%f)", var->id_int, var->sharing_weight, var->value, var->bound);
632       xbt_assert(not double_positive(var->value - var->bound, var->bound * sg_maxmin_precision),
633                  "Incorrect value (%f is not smaller than %f", var->value, var->bound);
634     } else {
635       XBT_DEBUG("'%d'(%f) : %f", var->id_int, var->sharing_weight, var->value);
636     }
637   }
638 }
639
640 void lmm_solve(lmm_system_t sys)
641 {
642   void* _cnst;
643   void* _cnst_next;
644   void* _elem;
645   double min_usage = -1;
646   double min_bound = -1;
647
648   if (not sys->modified)
649     return;
650
651   XBT_IN("(sys=%p)", sys);
652
653   /* Compute Usage and store the variables that reach the maximum. If selective_update_active is true, only constraints
654    * that changed are considered. Otherwise all constraints with active actions are considered.
655    */
656   xbt_swag_t cnst_list = sys->selective_update_active ? &(sys->modified_constraint_set) : &(sys->active_constraint_set);
657
658   XBT_DEBUG("Active constraints : %d", xbt_swag_size(cnst_list));
659   /* Init: Only modified code portions: reset the value of active variables */
660   xbt_swag_foreach(_cnst, cnst_list) {
661     lmm_constraint_t cnst = (lmm_constraint_t)_cnst;
662     xbt_swag_t elem_list  = &(cnst->enabled_element_set);
663     xbt_swag_foreach(_elem, elem_list) {
664       lmm_variable_t var = ((lmm_element_t)_elem)->variable;
665       xbt_assert(var->sharing_weight > 0.0);
666       var->value = 0.0;
667     }
668   }
669
670   s_lmm_constraint_light_t* cnst_light_tab = new s_lmm_constraint_light_t[xbt_swag_size(cnst_list)]();
671   int cnst_light_num = 0;
672   dyn_light_t saturated_constraint_set;
673
674   xbt_swag_foreach_safe(_cnst, _cnst_next, cnst_list) {
675     lmm_constraint_t cnst = (lmm_constraint_t)_cnst;
676     /* INIT: Collect constraints that actually need to be saturated (i.e remaining  and usage are strictly positive)
677      * into cnst_light_tab. */
678     cnst->remaining = cnst->bound;
679     if (not double_positive(cnst->remaining, cnst->bound * sg_maxmin_precision))
680       continue;
681     cnst->usage = 0;
682     xbt_swag_t elem_list = &(cnst->enabled_element_set);
683     xbt_swag_foreach(_elem, elem_list) {
684       lmm_element_t elem = (lmm_element_t)_elem;
685       xbt_assert(elem->variable->sharing_weight > 0);
686       if (elem->consumption_weight > 0) {
687         if (cnst->sharing_policy)
688           cnst->usage += elem->consumption_weight / elem->variable->sharing_weight;
689         else if (cnst->usage < elem->consumption_weight / elem->variable->sharing_weight)
690           cnst->usage = elem->consumption_weight / elem->variable->sharing_weight;
691
692         make_elem_active(elem);
693         simgrid::surf::Action *action = static_cast<simgrid::surf::Action*>(elem->variable->id);
694         if (sys->keep_track && not action->is_linked())
695           sys->keep_track->push_back(*action);
696       }
697     }
698     XBT_DEBUG("Constraint '%d' usage: %f remaining: %f concurrency: %i<=%i<=%i", cnst->id_int, cnst->usage,
699               cnst->remaining,cnst->concurrency_current,cnst->concurrency_maximum,cnst->concurrency_limit);
700     /* Saturated constraints update */
701
702     if(cnst->usage > 0) {
703       cnst_light_tab[cnst_light_num].cnst = cnst;
704       cnst->cnst_light = &(cnst_light_tab[cnst_light_num]);
705       cnst_light_tab[cnst_light_num].remaining_over_usage = cnst->remaining / cnst->usage;
706       saturated_constraint_set_update(cnst_light_tab[cnst_light_num].remaining_over_usage,
707         cnst_light_num, saturated_constraint_set, &min_usage);
708       xbt_assert(cnst->active_element_set.count>0, "There is no sense adding a constraint that has no active element!");
709       cnst_light_num++;
710     }
711   }
712
713   saturated_variable_set_update(  cnst_light_tab, saturated_constraint_set, sys);
714
715   /* Saturated variables update */
716   do {
717     /* Fix the variables that have to be */
718     xbt_swag_t var_list = &(sys->saturated_variable_set);
719     void* _var;
720     lmm_variable_t var = nullptr;
721     xbt_swag_foreach(_var, var_list) {
722       var = (lmm_variable_t)_var;
723       if (var->sharing_weight <= 0.0)
724         DIE_IMPOSSIBLE;
725       /* First check if some of these variables could reach their upper bound and update min_bound accordingly. */
726       XBT_DEBUG("var=%d, var->bound=%f, var->weight=%f, min_usage=%f, var->bound*var->weight=%f", var->id_int,
727                 var->bound, var->sharing_weight, min_usage, var->bound * var->sharing_weight);
728       if ((var->bound > 0) && (var->bound * var->sharing_weight < min_usage)) {
729         if (min_bound < 0)
730           min_bound = var->bound * var->sharing_weight;
731         else
732           min_bound = std::min(min_bound, (var->bound * var->sharing_weight));
733         XBT_DEBUG("Updated min_bound=%f", min_bound);
734       }
735     }
736
737     while ((var = (lmm_variable_t)xbt_swag_getFirst(var_list))) {
738       if (min_bound < 0) {
739         //If no variable could reach its bound, deal iteratively the constraints usage ( at worst one constraint is
740         // saturated at each cycle)
741         var->value = min_usage / var->sharing_weight;
742         XBT_DEBUG("Setting var (%d) value to %f\n", var->id_int, var->value);
743       } else {
744          //If there exist a variable that can reach its bound, only update it (and other with the same bound) for now.
745          if (double_equals(min_bound, var->bound * var->sharing_weight, sg_maxmin_precision)) {
746            var->value = var->bound;
747            XBT_DEBUG("Setting %p (%d) value to %f\n", var, var->id_int, var->value);
748          } else {
749            // Variables which bound is different are not considered for this cycle, but they will be afterwards.
750            XBT_DEBUG("Do not consider %p (%d) \n", var, var->id_int);
751            xbt_swag_remove(var, var_list);
752            continue;
753          }
754       }
755       XBT_DEBUG("Min usage: %f, Var(%d)->weight: %f, Var(%d)->value: %f ", min_usage, var->id_int, var->sharing_weight,
756                 var->id_int, var->value);
757
758       /* Update the usage of contraints where this variable is involved */
759       for (s_lmm_element_t& elem : var->cnsts) {
760         lmm_constraint_t cnst = elem.constraint;
761         if (cnst->sharing_policy) {
762           // Remember: shared constraints require that sum(elem.value * var->value) < cnst->bound
763           double_update(&(cnst->remaining), elem.consumption_weight * var->value, cnst->bound * sg_maxmin_precision);
764           double_update(&(cnst->usage), elem.consumption_weight / var->sharing_weight, sg_maxmin_precision);
765           // If the constraint is saturated, remove it from the set of active constraints (light_tab)
766           if (not double_positive(cnst->usage, sg_maxmin_precision) ||
767               not double_positive(cnst->remaining, cnst->bound * sg_maxmin_precision)) {
768             if (cnst->cnst_light) {
769               int index = (cnst->cnst_light-cnst_light_tab);
770               XBT_DEBUG("index: %d \t cnst_light_num: %d \t || usage: %f remaining: %f bound: %f  ",
771                          index,cnst_light_num, cnst->usage, cnst->remaining, cnst->bound);
772               cnst_light_tab[index]=cnst_light_tab[cnst_light_num-1];
773               cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
774               cnst_light_num--;
775               cnst->cnst_light = nullptr;
776             }
777           } else {
778             cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
779           }
780           make_elem_inactive(&elem);
781         } else {
782           // Remember: non-shared constraints only require that max(elem.value * var->value) < cnst->bound
783           cnst->usage = 0.0;
784           make_elem_inactive(&elem);
785           xbt_swag_t elem_list = &(cnst->enabled_element_set);
786           xbt_swag_foreach(_elem, elem_list) {
787             lmm_element_t elem2 = static_cast<lmm_element_t>(_elem);
788             xbt_assert(elem2->variable->sharing_weight > 0);
789             if (elem2->variable->value > 0)
790               continue;
791             if (elem2->consumption_weight > 0)
792               cnst->usage = std::max(cnst->usage, elem2->consumption_weight / elem2->variable->sharing_weight);
793           }
794           //If the constraint is saturated, remove it from the set of active constraints (light_tab)
795           if (not double_positive(cnst->usage, sg_maxmin_precision) ||
796               not double_positive(cnst->remaining, cnst->bound * sg_maxmin_precision)) {
797             if(cnst->cnst_light) {
798               int index = (cnst->cnst_light-cnst_light_tab);
799               XBT_DEBUG("index: %d \t cnst_light_num: %d \t || \t cnst: %p \t cnst->cnst_light: %p "
800                         "\t cnst_light_tab: %p usage: %f remaining: %f bound: %f  ", index,cnst_light_num, cnst,
801                         cnst->cnst_light, cnst_light_tab, cnst->usage, cnst->remaining, cnst->bound);
802               cnst_light_tab[index]=cnst_light_tab[cnst_light_num-1];
803               cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
804               cnst_light_num--;
805               cnst->cnst_light = nullptr;
806             }
807           } else {
808             cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
809             xbt_assert(cnst->active_element_set.count>0, "Should not keep a maximum constraint that has no active"
810                        " element! You want to check the maxmin precision and possible rounding effects." );
811           }
812         }
813       }
814       xbt_swag_remove(var, var_list);
815     }
816
817     /* Find out which variables reach the maximum */
818     min_usage = -1;
819     min_bound = -1;
820     saturated_constraint_set.clear();
821     int pos;
822     for(pos=0; pos<cnst_light_num; pos++){
823       xbt_assert(cnst_light_tab[pos].cnst->active_element_set.count>0, "Cannot saturate more a constraint that has"
824                  " no active element! You may want to change the maxmin precision (--cfg=maxmin/precision:<new_value>)"
825                  " because of possible rounding effects.\n\tFor the record, the usage of this constraint is %g while "
826                  "the maxmin precision to which it is compared is %g.\n\tThe usage of the previous constraint is %g.",
827                  cnst_light_tab[pos].cnst->usage, sg_maxmin_precision, cnst_light_tab[pos-1].cnst->usage);
828       saturated_constraint_set_update(cnst_light_tab[pos].remaining_over_usage, pos, saturated_constraint_set,
829                                       &min_usage);
830     }
831
832     saturated_variable_set_update(cnst_light_tab, saturated_constraint_set, sys);
833
834   } while (cnst_light_num > 0);
835
836   sys->modified = 0;
837   if (sys->selective_update_active)
838     lmm_remove_all_modified_set(sys);
839
840   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
841     lmm_print(sys);
842   }
843
844   lmm_check_concurrency(sys);
845
846   delete[] cnst_light_tab;
847   XBT_OUT();
848 }
849
850 /** \brief Attribute the value bound to var->bound.
851  *
852  *  \param sys the lmm_system_t
853  *  \param var the lmm_variable_t
854  *  \param bound the new bound to associate with var
855  *
856  *  Makes var->bound equal to bound. Whenever this function is called a change is  signed in the system. To
857  *  avoid false system changing detection it is a good idea to test (bound != 0) before calling it.
858  */
859 void lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var, double bound)
860 {
861   sys->modified = 1;
862   var->bound = bound;
863
864   if (not var->cnsts.empty())
865     lmm_update_modified_set(sys, var->cnsts[0].constraint);
866 }
867
868 int lmm_concurrency_slack(lmm_constraint_t cnstr)
869 {
870   if (cnstr->concurrency_limit < 0)
871     return std::numeric_limits<int>::max();
872   return  cnstr->concurrency_limit - cnstr->concurrency_current;
873 }
874
875 /** \brief Measure the minimum concurrency slack across all constraints where the given var is involved */
876 int lmm_cnstrs_min_concurrency_slack(lmm_variable_t var)
877 {
878   int minslack = std::numeric_limits<int>::max();
879   for (s_lmm_element_t const& elem : var->cnsts) {
880     int slack = lmm_concurrency_slack(elem.constraint);
881     if (slack < minslack) {
882       // This is only an optimization, to avoid looking at more constraints when slack is already zero
883       if (slack == 0)
884         return 0;
885       minslack = slack;
886     }
887   }
888   return minslack;
889 }
890
891 /* /Check if a variable can be enabled
892  *
893  * Make sure to set staged_weight before, if your intent is only to check concurrency
894  */
895 int lmm_can_enable_var(lmm_variable_t var){
896   return var->staged_weight>0 && lmm_cnstrs_min_concurrency_slack(var)>=var->concurrency_share;
897 }
898
899 //Small remark: In this implementation of lmm_enable_var and lmm_disable_var, we will meet multiple times with var when
900 // running lmm_update_modified_set.
901 //A priori not a big performance issue, but we might do better by calling lmm_update_modified_set within the for loops
902 // (after doing the first for enabling==1, and before doing the last for disabling==1)
903 void lmm_enable_var(lmm_system_t sys, lmm_variable_t var){
904   xbt_assert(lmm_can_enable_var(var));
905
906   var->sharing_weight = var->staged_weight;
907   var->staged_weight = 0;
908
909   // Enabling the variable, move to var to list head. Subtlety is: here, we need to call lmm_update_modified_set AFTER
910   // moving at least one element of var.
911
912   xbt_swag_remove(var, &(sys->variable_set));
913   xbt_swag_insert_at_head(var, &(sys->variable_set));
914   for (s_lmm_element_t& elem : var->cnsts) {
915     xbt_swag_remove(&elem, &(elem.constraint->disabled_element_set));
916     xbt_swag_insert_at_head(&elem, &(elem.constraint->enabled_element_set));
917     lmm_increase_concurrency(&elem);
918   }
919   if (not var->cnsts.empty())
920     lmm_update_modified_set(sys, var->cnsts[0].constraint);
921
922   //When used within lmm_on_disabled_var, we would get an assertion fail, because transiently there can be variables
923   // that are staged and could be activated.
924   //Anyway, caller functions all call lmm_check_concurrency() in the end.
925 }
926
927 void lmm_disable_var(lmm_system_t sys, lmm_variable_t var){
928   xbt_assert(not var->staged_weight, "Staged weight should have been cleared");
929   // Disabling the variable, move to var to list tail. Subtlety is: here, we need to call lmm_update_modified_set BEFORE
930   // moving the last element of var.
931   xbt_swag_remove(var, &(sys->variable_set));
932   xbt_swag_insert_at_tail(var, &(sys->variable_set));
933   if (not var->cnsts.empty())
934     lmm_update_modified_set(sys, var->cnsts[0].constraint);
935   for (s_lmm_element_t& elem : var->cnsts) {
936     xbt_swag_remove(&elem, &(elem.constraint->enabled_element_set));
937     xbt_swag_insert_at_tail(&elem, &(elem.constraint->disabled_element_set));
938
939     xbt_swag_remove(&elem, &(elem.constraint->active_element_set));
940
941     lmm_decrease_concurrency(&elem);
942   }
943
944   var->sharing_weight = 0.0;
945   var->staged_weight=0.0;
946   var->value = 0.0;
947   lmm_check_concurrency(sys);
948 }
949
950 /* /brief Find variables that can be enabled and enable them.
951  *
952  * Assuming that the variable has already been removed from non-zero weights
953  * Can we find a staged variable to add?
954  * If yes, check that none of the constraints that this variable is involved in is at the limit of its concurrency
955  * And then add it to enabled variables
956  */
957 void lmm_on_disabled_var(lmm_system_t sys, lmm_constraint_t cnstr){
958
959   if(cnstr->concurrency_limit<0)
960     return;
961
962   int numelem = xbt_swag_size(&(cnstr->disabled_element_set));
963   if (not numelem)
964     return;
965
966   lmm_element_t elem = (lmm_element_t)xbt_swag_getFirst(&(cnstr->disabled_element_set));
967
968   //Cannot use xbt_swag_foreach, because lmm_enable_var will modify disabled_element_set.. within the loop
969   while (numelem-- && elem) {
970
971     lmm_element_t nextelem = (lmm_element_t)xbt_swag_getNext(elem, cnstr->disabled_element_set.offset);
972
973     if (elem->variable->staged_weight > 0 && lmm_can_enable_var(elem->variable)) {
974       //Found a staged variable
975       //TODOLATER: Add random timing function to model reservation protocol fuzziness? Then how to make sure that
976       //staged variables will eventually be called?
977       lmm_enable_var(sys, elem->variable);
978     }
979
980     xbt_assert(cnstr->concurrency_current<=cnstr->concurrency_limit,"Concurrency overflow!");
981     if(cnstr->concurrency_current==cnstr->concurrency_limit)
982       break;
983
984     elem = nextelem;
985   }
986
987   //We could get an assertion fail, because transiently there can be variables that are staged and could be activated.
988   //And we need to go through all constraints of the disabled var before getting back a coherent state.
989   //Anyway, caller functions all call lmm_check_concurrency() in the end.
990 }
991
992 /* \brief update the weight of a variable, and enable/disable it.
993  * @return Returns whether a change was made
994  */
995 void lmm_update_variable_weight(lmm_system_t sys, lmm_variable_t var, double weight)
996 {
997   xbt_assert(weight>=0,"Variable weight should not be negative!");
998
999   if (weight == var->sharing_weight)
1000     return;
1001
1002   int enabling_var  = (weight > 0 && var->sharing_weight <= 0);
1003   int disabling_var = (weight <= 0 && var->sharing_weight > 0);
1004
1005   XBT_IN("(sys=%p, var=%p, weight=%f)", sys, var, weight);
1006
1007   sys->modified = 1;
1008
1009   //Are we enabling this variable?
1010   if (enabling_var){
1011     var->staged_weight = weight;
1012     int minslack       = lmm_cnstrs_min_concurrency_slack(var);
1013     if (minslack < var->concurrency_share) {
1014       XBT_DEBUG("Staging var (instead of enabling) because min concurrency slack %i, with weight %f and concurrency"
1015                 " share %i", minslack, weight, var->concurrency_share);
1016       return;
1017     }
1018     XBT_DEBUG("Enabling var with min concurrency slack %i", minslack);
1019     lmm_enable_var(sys,var);
1020   } else if (disabling_var){
1021     //Are we disabling this variable?
1022     lmm_disable_var(sys,var);
1023   } else {
1024     var->sharing_weight = weight;
1025   }
1026
1027   lmm_check_concurrency(sys);
1028
1029   XBT_OUT();
1030 }
1031
1032 double lmm_get_variable_weight(lmm_variable_t var)
1033 {
1034   return var->sharing_weight;
1035 }
1036
1037 void lmm_update_constraint_bound(lmm_system_t sys, lmm_constraint_t cnst, double bound)
1038 {
1039   sys->modified = 1;
1040   lmm_update_modified_set(sys, cnst);
1041   cnst->bound = bound;
1042 }
1043
1044 int lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst)
1045 {
1046   return xbt_swag_belongs(cnst, &(sys->active_constraint_set));
1047 }
1048
1049 /** \brief Update the constraint set propagating recursively to other constraints so the system should not be entirely
1050  *  computed.
1051  *
1052  *  \param sys the lmm_system_t
1053  *  \param cnst the lmm_constraint_t affected by the change
1054  *
1055  *  A recursive algorithm to optimize the system recalculation selecting only constraints that have changed. Each
1056  *  constraint change is propagated to the list of constraints for each variable.
1057  */
1058 static void lmm_update_modified_set_rec(lmm_system_t sys, lmm_constraint_t cnst)
1059 {
1060   void* _elem;
1061
1062   xbt_swag_foreach(_elem, &cnst->enabled_element_set) {
1063     lmm_variable_t var = ((lmm_element_t)_elem)->variable;
1064     for (s_lmm_element_t const& elem : var->cnsts) {
1065       if (var->visited == sys->visited_counter)
1066         break;
1067       if (elem.constraint != cnst && not xbt_swag_belongs(elem.constraint, &sys->modified_constraint_set)) {
1068         xbt_swag_insert(elem.constraint, &sys->modified_constraint_set);
1069         lmm_update_modified_set_rec(sys, elem.constraint);
1070       }
1071     }
1072     //var will be ignored in later visits as long as sys->visited_counter does not move
1073     var->visited = sys->visited_counter;
1074   }
1075 }
1076
1077 static void lmm_update_modified_set(lmm_system_t sys, lmm_constraint_t cnst)
1078 {
1079   /* nothing to do if selective update isn't active */
1080   if (sys->selective_update_active && not xbt_swag_belongs(cnst, &sys->modified_constraint_set)) {
1081     xbt_swag_insert(cnst, &sys->modified_constraint_set);
1082     lmm_update_modified_set_rec(sys, cnst);
1083   }
1084 }
1085
1086 /** \brief Remove all constraints of the modified_constraint_set.
1087  *
1088  *  \param sys the lmm_system_t
1089  */
1090 static void lmm_remove_all_modified_set(lmm_system_t sys)
1091 {
1092   // We cleverly un-flag all variables just by incrementing sys->visited_counter
1093   // In effect, the var->visited value will no more be equal to sys->visited counter
1094   // To be clean, when visited counter has wrapped around, we force these var->visited values so that variables that
1095   // were in the modified a long long time ago are not wrongly skipped here, which would lead to very nasty bugs
1096   // (i.e. not readibily reproducible, and requiring a lot of run time before happening).
1097   if (++sys->visited_counter == 1) {
1098     /* the counter wrapped around, reset each variable->visited */
1099   void *_var;
1100     xbt_swag_foreach(_var, &sys->variable_set)
1101       ((lmm_variable_t)_var)->visited = 0;
1102   }
1103   xbt_swag_reset(&sys->modified_constraint_set);
1104 }
1105
1106 /**
1107  * Returns resource load (in flop per second, or byte per second, or similar)
1108  *
1109  * If the resource is shared (the default case), the load is sum of resource usage made by every variables located on
1110  * this resource.
1111  *
1112  * If the resource is not shared (ie in FATPIPE mode), then the load is the max (not the sum) of all resource usages
1113  * located on this resource.
1114  *
1115  * \param cnst the lmm_constraint_t associated to the resource
1116  */
1117 double lmm_constraint_get_usage(lmm_constraint_t cnst) {
1118   double usage         = 0.0;
1119   xbt_swag_t elem_list = &(cnst->enabled_element_set);
1120   void* _elem;
1121
1122   xbt_swag_foreach(_elem, elem_list)
1123   {
1124     lmm_element_t elem = (lmm_element_t)_elem;
1125     if (elem->consumption_weight > 0) {
1126       if (cnst->sharing_policy)
1127         usage += elem->consumption_weight * elem->variable->value;
1128       else if (usage < elem->consumption_weight * elem->variable->value)
1129         usage = std::max(usage, elem->consumption_weight * elem->variable->value);
1130     }
1131   }
1132   return usage;
1133 }
1134
1135 int lmm_constraint_get_variable_amount(lmm_constraint_t cnst) {
1136   int usage = 0;
1137   xbt_swag_t elem_list = &(cnst->enabled_element_set);
1138   void *_elem;
1139
1140   xbt_swag_foreach(_elem, elem_list) {
1141     lmm_element_t elem = (lmm_element_t)_elem;
1142     if (elem->consumption_weight > 0)
1143       usage++;
1144   }
1145  return usage;
1146 }