Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix another race in log initializations.
[simgrid.git] / src / surf / maxmin.c
1 /* Copyright (c) 2004-2011. 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
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/mallocator.h"
11 #include "maxmin_private.h"
12 #include <stdlib.h>
13 #include <stdio.h>              /* sprintf */
14 #include <math.h>
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_maxmin, surf,
16                                 "Logging specific to SURF (maxmin)");
17
18 double sg_maxmin_precision = 0.00001;
19
20 static void *lmm_variable_mallocator_new_f(void);
21 static void lmm_variable_mallocator_free_f(void *var);
22 #define lmm_variable_mallocator_reset_f ((void_f_pvoid_t)NULL)
23 static void lmm_update_modified_set(lmm_system_t sys,
24                                     lmm_constraint_t cnst);
25 static void lmm_remove_all_modified_set(lmm_system_t sys);
26 static int Global_debug_id = 1;
27 static int Global_const_debug_id = 1;
28
29 lmm_system_t lmm_system_new(int selective_update)
30 {
31   lmm_system_t l = NULL;
32   s_lmm_variable_t var;
33   s_lmm_constraint_t cnst;
34
35   l = xbt_new0(s_lmm_system_t, 1);
36
37   l->modified = 0;
38   l->selective_update_active = selective_update;
39   l->visited_counter = 1;
40
41   XBT_DEBUG("Setting selective_update_active flag to %d\n",
42          l->selective_update_active);
43
44   xbt_swag_init(&(l->variable_set),
45                 xbt_swag_offset(var, variable_set_hookup));
46   xbt_swag_init(&(l->constraint_set),
47                 xbt_swag_offset(cnst, constraint_set_hookup));
48
49   xbt_swag_init(&(l->active_constraint_set),
50                 xbt_swag_offset(cnst, active_constraint_set_hookup));
51
52   xbt_swag_init(&(l->modified_constraint_set),
53                 xbt_swag_offset(cnst, modified_constraint_set_hookup));
54   xbt_swag_init(&(l->saturated_variable_set),
55                 xbt_swag_offset(var, saturated_variable_set_hookup));
56   xbt_swag_init(&(l->saturated_constraint_set),
57                 xbt_swag_offset(cnst, saturated_constraint_set_hookup));
58
59   l->variable_mallocator = xbt_mallocator_new(65536,
60                                               lmm_variable_mallocator_new_f,
61                                               lmm_variable_mallocator_free_f,
62                                               lmm_variable_mallocator_reset_f);
63
64   return l;
65 }
66
67 void lmm_system_free(lmm_system_t sys)
68 {
69   lmm_variable_t var = NULL;
70   lmm_constraint_t cnst = NULL;
71
72   while ((var = extract_variable(sys))) {
73     XBT_WARN
74         ("Variable %p (%d) still in LMM system when freing it: this may be a bug",
75          var, var->id_int);
76     lmm_var_free(sys, var);
77   }
78
79   while ((cnst = extract_constraint(sys)))
80     lmm_cnst_free(sys, cnst);
81
82   xbt_mallocator_free(sys->variable_mallocator);
83   free(sys);
84 }
85
86 XBT_INLINE void lmm_variable_disable(lmm_system_t sys, lmm_variable_t var)
87 {
88   int i;
89   int n;
90
91   lmm_element_t elem = NULL;
92
93   XBT_IN("(sys=%p, var=%p)", sys, var);
94   sys->modified = 1;
95
96   n = 0;
97   for (i = 0; i < var->cnsts_number; i++) {
98     elem = &var->cnsts[i];
99     xbt_swag_remove(elem, &(elem->constraint->element_set));
100     xbt_swag_remove(elem, &(elem->constraint->active_element_set));
101     if (!xbt_swag_size(&(elem->constraint->element_set)))
102       make_constraint_inactive(sys, elem->constraint);
103     else {
104       if (n < i)
105         var->cnsts[n].constraint = elem->constraint;
106       n++;
107     }
108   }
109   if (n) {
110     var->cnsts_number = n;
111     lmm_update_modified_set(sys, var->cnsts[0].constraint);
112   }
113
114   var->cnsts_number = 0;
115   XBT_OUT();
116 }
117
118 static void lmm_var_free(lmm_system_t sys, lmm_variable_t var)
119 {
120
121   lmm_variable_disable(sys, var);
122   xbt_mallocator_release(sys->variable_mallocator, var);
123 }
124
125 static XBT_INLINE void lmm_cnst_free(lmm_system_t sys,
126                                      lmm_constraint_t cnst)
127 {
128 /*   xbt_assert(xbt_swag_size(&(cnst->element_set)), */
129 /*            "This list should be empty!"); */
130   make_constraint_inactive(sys, cnst);
131   free(cnst);
132 }
133
134 lmm_constraint_t lmm_constraint_new(lmm_system_t sys, void *id,
135                                     double bound_value)
136 {
137   lmm_constraint_t cnst = NULL;
138   s_lmm_element_t elem;
139
140   cnst = xbt_new0(s_lmm_constraint_t, 1);
141   cnst->id = id;
142   cnst->id_int = Global_const_debug_id++;
143   xbt_swag_init(&(cnst->element_set),
144                 xbt_swag_offset(elem, element_set_hookup));
145   xbt_swag_init(&(cnst->active_element_set),
146                 xbt_swag_offset(elem, active_element_set_hookup));
147
148   cnst->bound = bound_value;
149   cnst->usage = 0;
150   cnst->shared = 1;
151   insert_constraint(sys, cnst);
152
153   return cnst;
154 }
155
156 XBT_INLINE void lmm_constraint_shared(lmm_constraint_t cnst)
157 {
158   cnst->shared = 0;
159 }
160
161 XBT_INLINE int lmm_constraint_is_shared(lmm_constraint_t cnst)
162 {
163   return (cnst->shared);
164 }
165
166 XBT_INLINE void lmm_constraint_free(lmm_system_t sys,
167                                     lmm_constraint_t cnst)
168 {
169   remove_constraint(sys, cnst);
170   lmm_cnst_free(sys, cnst);
171 }
172
173 static void *lmm_variable_mallocator_new_f(void)
174 {
175   lmm_variable_t var = xbt_new(s_lmm_variable_t, 1);
176   var->cnsts = NULL; /* will be created by realloc */
177   return var;
178 }
179
180 static void lmm_variable_mallocator_free_f(void *var)
181 {
182   xbt_free(((lmm_variable_t) var)->cnsts);
183   xbt_free(var);
184 }
185
186 lmm_variable_t lmm_variable_new(lmm_system_t sys, void *id,
187                                 double weight,
188                                 double bound, int number_of_constraints)
189 {
190   lmm_variable_t var = NULL;
191   int i;
192
193   XBT_IN("(sys=%p, id=%p, weight=%f, bound=%f, num_cons =%d)",
194           sys, id, weight, bound, number_of_constraints);
195
196   var = xbt_mallocator_get(sys->variable_mallocator);
197   var->id = id;
198   var->id_int = Global_debug_id++;
199   var->cnsts = xbt_realloc(var->cnsts, number_of_constraints * sizeof(s_lmm_element_t));
200   for (i = 0; i < number_of_constraints; i++) {
201     var->cnsts[i].element_set_hookup.next = NULL;
202     var->cnsts[i].element_set_hookup.prev = NULL;
203     var->cnsts[i].active_element_set_hookup.next = NULL;
204     var->cnsts[i].active_element_set_hookup.prev = NULL;
205     var->cnsts[i].constraint = NULL;
206     var->cnsts[i].variable = NULL;
207     var->cnsts[i].value = 0.0;
208   }
209   var->cnsts_size = number_of_constraints;
210   var->cnsts_number = 0;
211   var->weight = weight;
212   var->bound = bound;
213   var->value = 0.0;
214   var->visited = sys->visited_counter - 1;
215   var->mu = 0.0;
216   var->new_mu = 0.0;
217   var->func_f = func_f_def;
218   var->func_fp = func_fp_def;
219   var->func_fpi = func_fpi_def;
220
221   var->variable_set_hookup.next = NULL;
222   var->variable_set_hookup.prev = NULL;
223   var->saturated_variable_set_hookup.next = NULL;
224   var->saturated_variable_set_hookup.prev = NULL;
225
226   if (weight)
227     xbt_swag_insert_at_head(var, &(sys->variable_set));
228   else
229     xbt_swag_insert_at_tail(var, &(sys->variable_set));
230   XBT_OUT();
231   return var;
232 }
233
234 void lmm_variable_free(lmm_system_t sys, lmm_variable_t var)
235 {
236   remove_variable(sys, var);
237   lmm_var_free(sys, var);
238 }
239
240 XBT_INLINE double lmm_variable_getvalue(lmm_variable_t var)
241 {
242   return (var->value);
243 }
244
245 XBT_INLINE double lmm_variable_getbound(lmm_variable_t var)
246 {
247   return (var->bound);
248 }
249
250 void lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
251                 lmm_variable_t var, double value)
252 {
253   lmm_element_t elem = NULL;
254
255   sys->modified = 1;
256
257   xbt_assert(var->cnsts_number < var->cnsts_size, "Too much constraints");
258
259   elem = &(var->cnsts[var->cnsts_number++]);
260
261   elem->value = value;
262   elem->constraint = cnst;
263   elem->variable = var;
264
265   if (var->weight)
266     xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
267   else
268     xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
269
270   make_constraint_active(sys, cnst);
271   lmm_update_modified_set(sys, cnst);
272   if (var->cnsts_number > 1)
273     lmm_update_modified_set(sys, var->cnsts[0].constraint);
274 }
275
276 void lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst,
277                     lmm_variable_t var, double value)
278 {
279   int i;
280   sys->modified = 1;
281
282   for (i = 0; i < var->cnsts_number; i++)
283     if (var->cnsts[i].constraint == cnst)
284       break;
285
286   if (i < var->cnsts_number) {
287     if (cnst->shared)
288       var->cnsts[i].value += value;
289     else
290       var->cnsts[i].value = MAX(var->cnsts[i].value, value);
291     lmm_update_modified_set(sys, cnst);
292   } else
293     lmm_expand(sys, cnst, var, value);
294 }
295
296 void lmm_elem_set_value(lmm_system_t sys, lmm_constraint_t cnst,
297                         lmm_variable_t var, double value)
298 {
299   int i;
300
301   for (i = 0; i < var->cnsts_number; i++)
302     if (var->cnsts[i].constraint == cnst)
303       break;
304
305   if (i < var->cnsts_number) {
306     var->cnsts[i].value = value;
307     sys->modified = 1;
308     lmm_update_modified_set(sys, cnst);
309   } else
310     DIE_IMPOSSIBLE;
311 }
312
313 XBT_INLINE lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t sys,
314                                                   lmm_variable_t var,
315                                                   int num)
316 {
317   if (num < var->cnsts_number)
318     return (var->cnsts[num].constraint);
319   else
320     return NULL;
321 }
322
323 XBT_INLINE int lmm_get_number_of_cnst_from_var(lmm_system_t sys,
324                                                lmm_variable_t var)
325 {
326   return (var->cnsts_number);
327 }
328
329 lmm_variable_t lmm_get_var_from_cnst(lmm_system_t sys,
330                                      lmm_constraint_t cnst,
331                                      lmm_element_t * elem)
332 {
333   if (!(*elem))
334     *elem = xbt_swag_getFirst(&(cnst->element_set));
335   else
336     *elem = xbt_swag_getNext(*elem, cnst->element_set.offset);
337   if (*elem)
338     return (*elem)->variable;
339   else
340     return NULL;
341 }
342
343 XBT_INLINE void *lmm_constraint_id(lmm_constraint_t cnst)
344 {
345   return cnst->id;
346 }
347
348 XBT_INLINE void *lmm_variable_id(lmm_variable_t var)
349 {
350   return var->id;
351 }
352
353 static XBT_INLINE void saturated_constraint_set_update(lmm_system_t sys,
354                                                        lmm_constraint_t
355                                                        cnst,
356                                                        double *min_usage)
357 {
358   lmm_constraint_t useless_cnst = NULL;
359
360   XBT_IN("sys=%p, cnst=%p, min_usage=%f", sys, cnst, *min_usage);
361   if (cnst->usage <= 0) {
362     XBT_OUT();
363     return;
364   }
365   if (cnst->remaining <= 0) {
366     XBT_OUT();
367     return;
368   }
369   if ((*min_usage < 0) || (*min_usage > cnst->remaining / cnst->usage)) {
370     *min_usage = cnst->remaining / cnst->usage;
371     XBT_LOG(xbt_log_priority_trace,
372          "min_usage=%f (cnst->remaining=%f, cnst->usage=%f)", *min_usage,
373          cnst->remaining, cnst->usage);
374     while ((useless_cnst =
375             xbt_swag_getFirst(&(sys->saturated_constraint_set))))
376       xbt_swag_remove(useless_cnst, &(sys->saturated_constraint_set));
377
378     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
379   } else if (*min_usage == cnst->remaining / cnst->usage) {
380     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
381   }
382   XBT_OUT();
383 }
384
385 static XBT_INLINE void saturated_variable_set_update(lmm_system_t sys)
386 {
387   lmm_constraint_t cnst = NULL;
388   xbt_swag_t cnst_list = NULL;
389   lmm_element_t elem = NULL;
390   xbt_swag_t elem_list = NULL;
391
392   cnst_list = &(sys->saturated_constraint_set);
393   while ((cnst = xbt_swag_getFirst(cnst_list))) {
394     elem_list = &(cnst->active_element_set);
395     xbt_swag_foreach(elem, elem_list) {
396       if (elem->variable->weight <= 0)
397         break;
398       if ((elem->value > 0))
399         xbt_swag_insert(elem->variable, &(sys->saturated_variable_set));
400     }
401     xbt_swag_remove(cnst, cnst_list);
402   }
403 }
404
405 void lmm_print(lmm_system_t sys)
406 {
407   lmm_constraint_t cnst = NULL;
408   lmm_element_t elem = NULL;
409   lmm_variable_t var = NULL;
410   xbt_swag_t cnst_list = NULL;
411   xbt_swag_t var_list = NULL;
412   xbt_swag_t elem_list = NULL;
413   char print_buf[1024];
414   char *trace_buf = xbt_malloc0(sizeof(char));
415   double sum = 0.0;
416
417   /* Printing Objective */
418   var_list = &(sys->variable_set);
419   sprintf(print_buf, "MAX-MIN ( ");
420   trace_buf =
421       xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
422   strcat(trace_buf, print_buf);
423   xbt_swag_foreach(var, var_list) {
424     sprintf(print_buf, "'%d'(%f) ", var->id_int, var->weight);
425     trace_buf =
426         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
427     strcat(trace_buf, print_buf);
428   }
429   sprintf(print_buf, ")");
430   trace_buf =
431       xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
432   strcat(trace_buf, print_buf);
433   XBT_DEBUG("%20s", trace_buf);
434   trace_buf[0] = '\000';
435
436   XBT_DEBUG("Constraints");
437   /* Printing Constraints */
438   cnst_list = &(sys->active_constraint_set);
439   xbt_swag_foreach(cnst, cnst_list) {
440     sum = 0.0;
441     elem_list = &(cnst->element_set);
442     sprintf(print_buf, "\t");
443     trace_buf =
444         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
445     strcat(trace_buf, print_buf);
446     sprintf(print_buf, "%s(",(cnst->shared)?"":"max");
447     trace_buf =
448       xbt_realloc(trace_buf,
449                   strlen(trace_buf) + strlen(print_buf) + 1);
450     strcat(trace_buf, print_buf);      
451     xbt_swag_foreach(elem, elem_list) {
452       sprintf(print_buf, "%f.'%d'(%f) %s ", elem->value,
453               elem->variable->id_int, elem->variable->value,(cnst->shared)?"+":",");
454       trace_buf =
455           xbt_realloc(trace_buf,
456                       strlen(trace_buf) + strlen(print_buf) + 1);
457       strcat(trace_buf, print_buf);
458       if(cnst->shared) 
459         sum += elem->value * elem->variable->value;
460       else 
461         sum = MAX(sum,elem->value * elem->variable->value);
462     }
463     sprintf(print_buf, "0) <= %f ('%d')", cnst->bound, cnst->id_int);
464     trace_buf =
465         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
466     strcat(trace_buf, print_buf);
467
468     if (!cnst->shared) {
469       sprintf(print_buf, " [MAX-Constraint]");
470       trace_buf =
471           xbt_realloc(trace_buf,
472                       strlen(trace_buf) + strlen(print_buf) + 1);
473       strcat(trace_buf, print_buf);
474     }
475     XBT_DEBUG("%s", trace_buf);
476     trace_buf[0] = '\000';
477     xbt_assert(!double_positive(sum - cnst->bound),
478                 "Incorrect value (%f is not smaller than %f): %g",
479                 sum, cnst->bound, sum - cnst->bound);
480   }
481
482   XBT_DEBUG("Variables");
483   /* Printing Result */
484   xbt_swag_foreach(var, var_list) {
485     if (var->bound > 0) {
486       XBT_DEBUG("'%d'(%f) : %f (<=%f)", var->id_int, var->weight, var->value,
487              var->bound);
488       xbt_assert(!double_positive(var->value - var->bound),
489                   "Incorrect value (%f is not smaller than %f",
490                   var->value, var->bound);
491     } else {
492       XBT_DEBUG("'%d'(%f) : %f", var->id_int, var->weight, var->value);
493     }
494   }
495
496   free(trace_buf);
497 }
498
499 void lmm_solve(lmm_system_t sys)
500 {
501   lmm_variable_t var = NULL;
502   lmm_constraint_t cnst = NULL;
503   lmm_element_t elem = NULL;
504   xbt_swag_t cnst_list = NULL;
505   xbt_swag_t var_list = NULL;
506   xbt_swag_t elem_list = NULL;
507   double min_usage = -1;
508   double min_bound = -1;
509
510   if (!(sys->modified))
511     return;
512
513   XBT_IN("(sys=%p)", sys);
514
515   /*
516    * Compute Usage and store the variables that reach the maximum.
517    */
518   cnst_list =
519       sys->
520       selective_update_active ? &(sys->modified_constraint_set) :
521       &(sys->active_constraint_set);
522
523   XBT_DEBUG("Active constraints : %d", xbt_swag_size(cnst_list));
524   /* Init: Only modified code portions */
525   xbt_swag_foreach(cnst, cnst_list) {
526     elem_list = &(cnst->element_set);
527     //XBT_DEBUG("Variable set : %d", xbt_swag_size(elem_list));
528     xbt_swag_foreach(elem, elem_list) {
529       var = elem->variable;
530       if (var->weight <= 0.0)
531         break;
532       var->value = 0.0;
533     }
534   }
535
536   xbt_swag_foreach(cnst, cnst_list) {
537     /* INIT */
538     cnst->remaining = cnst->bound;
539     if (cnst->remaining == 0)
540       continue;
541     cnst->usage = 0;
542     elem_list = &(cnst->element_set);
543     xbt_swag_foreach(elem, elem_list) {
544       /* 0-weighted elements (ie, sleep actions) are at the end of the swag and we don't want to consider them */
545       if (elem->variable->weight <= 0)
546         break;
547       if ((elem->value > 0)) {
548         if (cnst->shared)
549           cnst->usage += elem->value / elem->variable->weight;
550         else if (cnst->usage < elem->value / elem->variable->weight)
551           cnst->usage = elem->value / elem->variable->weight;
552
553         make_elem_active(elem);
554         if (sys->keep_track)
555           xbt_swag_insert(elem->variable->id, sys->keep_track);
556       }
557     }
558     XBT_DEBUG("Constraint Usage '%d' : %f", cnst->id_int, cnst->usage);
559     /* Saturated constraints update */
560     saturated_constraint_set_update(sys, cnst, &min_usage);
561   }
562   saturated_variable_set_update(sys);
563
564   /* Saturated variables update */
565
566   do {
567     /* Fix the variables that have to be */
568     var_list = &(sys->saturated_variable_set);
569
570     xbt_swag_foreach(var, var_list) {
571       if (var->weight <= 0.0)
572         DIE_IMPOSSIBLE;
573       /* First check if some of these variables have reach their upper
574          bound and update min_usage accordingly. */
575       XBT_DEBUG
576           ("var=%d, var->bound=%f, var->weight=%f, min_usage=%f, var->bound*var->weight=%f",
577            var->id_int, var->bound, var->weight, min_usage,
578            var->bound * var->weight);
579       if ((var->bound > 0) && (var->bound * var->weight < min_usage)) {
580         if (min_bound < 0)
581           min_bound = var->bound;
582         else
583           min_bound = MIN(min_bound, var->bound);
584         XBT_DEBUG("Updated min_bound=%f", min_bound);
585       }
586     }
587
588
589     while ((var = xbt_swag_getFirst(var_list))) {
590       int i;
591
592       if (min_bound < 0) {
593         var->value = min_usage / var->weight;
594       } else {
595         if (min_bound == var->bound)
596           var->value = var->bound;
597         else {
598           xbt_swag_remove(var, var_list);
599           continue;
600         }
601       }
602       XBT_DEBUG("Min usage: %f, Var(%d)->weight: %f, Var(%d)->value: %f ",
603              min_usage, var->id_int, var->weight, var->id_int, var->value);
604
605
606       /* Update usage */
607
608       for (i = 0; i < var->cnsts_number; i++) {
609         elem = &var->cnsts[i];
610         cnst = elem->constraint;
611         if (cnst->shared) {
612           double_update(&(cnst->remaining), elem->value * var->value);
613           double_update(&(cnst->usage), elem->value / var->weight);
614           make_elem_inactive(elem);
615         } else {                /* FIXME one day: We recompute usage.... :( */
616           cnst->usage = 0.0;
617           make_elem_inactive(elem);
618           elem_list = &(cnst->element_set);
619           xbt_swag_foreach(elem, elem_list) {
620             if (elem->variable->weight <= 0)
621               break;
622             if (elem->variable->value > 0)
623               break;
624             if ((elem->value > 0)) {
625               cnst->usage =
626                   MAX(cnst->usage, elem->value / elem->variable->weight);
627               XBT_DEBUG("Constraint Usage %d : %f", cnst->id_int,
628                      cnst->usage);
629               make_elem_active(elem);
630             }
631           }
632         }
633       }
634       xbt_swag_remove(var, var_list);
635     }
636
637     /* Find out which variables reach the maximum */
638     min_usage = -1;
639     min_bound = -1;
640     xbt_swag_foreach(cnst, cnst_list) {
641       saturated_constraint_set_update(sys, cnst, &min_usage);
642     }
643     saturated_variable_set_update(sys);
644
645   } while (xbt_swag_size(&(sys->saturated_variable_set)));
646
647   sys->modified = 0;
648   if (sys->selective_update_active)
649     lmm_remove_all_modified_set(sys);
650
651   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
652     lmm_print(sys);
653   }
654   XBT_OUT();
655 }
656
657 /* Not a O(1) function */
658
659 void lmm_update(lmm_system_t sys, lmm_constraint_t cnst,
660                 lmm_variable_t var, double value)
661 {
662   int i;
663
664   for (i = 0; i < var->cnsts_number; i++)
665     if (var->cnsts[i].constraint == cnst) {
666       var->cnsts[i].value = value;
667       sys->modified = 1;
668       lmm_update_modified_set(sys, cnst);
669       return;
670     }
671 }
672
673 /** \brief Attribute the value bound to var->bound.
674  * 
675  *  \param sys the lmm_system_t
676  *  \param var the lmm_variable_t
677  *  \param bound the new bound to associate with var
678  * 
679  *  Makes var->bound equal to bound. Whenever this function is called 
680  *  a change is  signed in the system. To
681  *  avoid false system changing detection it is a good idea to test 
682  *  (bound != 0) before calling it.
683  *
684  */
685 void lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
686                                double bound)
687 {
688   sys->modified = 1;
689   var->bound = bound;
690
691   if (var->cnsts_number)
692     lmm_update_modified_set(sys, var->cnsts[0].constraint);
693 }
694
695
696 void lmm_update_variable_weight(lmm_system_t sys, lmm_variable_t var,
697                                 double weight)
698 {
699   int i;
700   lmm_element_t elem;
701
702   if (weight == var->weight)
703     return;
704   XBT_IN("(sys=%p, var=%p, weight=%f)", sys, var, weight);
705   sys->modified = 1;
706   var->weight = weight;
707   xbt_swag_remove(var, &(sys->variable_set));
708   if (weight)
709     xbt_swag_insert_at_head(var, &(sys->variable_set));
710   else
711     xbt_swag_insert_at_tail(var, &(sys->variable_set));
712
713   for (i = 0; i < var->cnsts_number; i++) {
714     elem = &var->cnsts[i];
715     xbt_swag_remove(elem, &(elem->constraint->element_set));
716     if (weight)
717       xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
718     else
719       xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
720
721     if (i == 0)
722       lmm_update_modified_set(sys, elem->constraint);
723   }
724   if (!weight)
725     var->value = 0.0;
726
727   XBT_OUT();
728 }
729
730 XBT_INLINE double lmm_get_variable_weight(lmm_variable_t var)
731 {
732   return var->weight;
733 }
734
735 XBT_INLINE void lmm_update_constraint_bound(lmm_system_t sys,
736                                             lmm_constraint_t cnst,
737                                             double bound)
738 {
739   sys->modified = 1;
740   lmm_update_modified_set(sys, cnst);
741   cnst->bound = bound;
742 }
743
744 XBT_INLINE int lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst)
745 {
746   return xbt_swag_belongs(cnst, &(sys->active_constraint_set));
747 }
748
749 XBT_INLINE lmm_constraint_t lmm_get_first_active_constraint(lmm_system_t
750                                                             sys)
751 {
752   return xbt_swag_getFirst(&(sys->active_constraint_set));
753 }
754
755 XBT_INLINE lmm_constraint_t lmm_get_next_active_constraint(lmm_system_t
756                                                            sys,
757                                                            lmm_constraint_t
758                                                            cnst)
759 {
760   return xbt_swag_getNext(cnst, (sys->active_constraint_set).offset);
761 }
762
763 #ifdef HAVE_LATENCY_BOUND_TRACKING
764 XBT_INLINE int lmm_is_variable_limited_by_latency(lmm_variable_t var)
765 {
766   return (double_equals(var->bound, var->value));
767 }
768 #endif
769
770
771 /** \brief Update the constraint set propagating recursively to
772  *  other constraints so the system should not be entirely computed.
773  *
774  *  \param sys the lmm_system_t
775  *  \param cnst the lmm_constraint_t affected by the change
776  *
777  *  A recursive algorithm to optimize the system recalculation selecting only
778  *  constraints that have changed. Each constraint change is propagated
779  *  to the list of constraints for each variable.
780  */
781 static void lmm_update_modified_set_rec(lmm_system_t sys,
782                                         lmm_constraint_t cnst)
783 {
784   lmm_element_t elem;
785
786   xbt_swag_foreach(elem, &cnst->element_set) {
787     lmm_variable_t var = elem->variable;
788     s_lmm_element_t *cnsts = var->cnsts;
789     int i;
790     for (i = 0; var->visited != sys->visited_counter
791              && i < var->cnsts_number ; i++) {
792       if (cnsts[i].constraint != cnst
793           && !xbt_swag_belongs(cnsts[i].constraint,
794                                &sys->modified_constraint_set)) {
795         xbt_swag_insert(cnsts[i].constraint, &sys->modified_constraint_set);
796         lmm_update_modified_set_rec(sys, cnsts[i].constraint);
797       }
798     }
799     var->visited = sys->visited_counter;
800   }
801 }
802
803 static void lmm_update_modified_set(lmm_system_t sys,
804                                     lmm_constraint_t cnst)
805 {
806   /* nothing to do if selective update isn't active */
807   if (sys->selective_update_active
808       && !xbt_swag_belongs(cnst, &sys->modified_constraint_set)) {
809     xbt_swag_insert(cnst, &sys->modified_constraint_set);
810     lmm_update_modified_set_rec(sys, cnst);
811   }
812 }
813
814 /** \brief Remove all constraints of the modified_constraint_set.
815  *
816  *  \param sys the lmm_system_t
817  */
818 static void lmm_remove_all_modified_set(lmm_system_t sys)
819 {
820   if (++sys->visited_counter == 1) {
821     /* the counter wrapped around, reset each variable->visited */
822     lmm_variable_t var;
823     xbt_swag_foreach(var, &sys->variable_set)
824       var->visited = 0;
825   }
826   xbt_swag_reset(&sys->modified_constraint_set);
827 }