Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rewrite function net_update_action_remaining_lazy like in cpu.
[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
231   XBT_OUT(" returns %p", var);
232   return var;
233 }
234
235 void lmm_variable_free(lmm_system_t sys, lmm_variable_t var)
236 {
237   remove_variable(sys, var);
238   lmm_var_free(sys, var);
239 }
240
241 XBT_INLINE double lmm_variable_getvalue(lmm_variable_t var)
242 {
243   return (var->value);
244 }
245
246 XBT_INLINE double lmm_variable_getbound(lmm_variable_t var)
247 {
248   return (var->bound);
249 }
250
251 void lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
252                 lmm_variable_t var, double value)
253 {
254   lmm_element_t elem = NULL;
255
256   sys->modified = 1;
257
258   xbt_assert(var->cnsts_number < var->cnsts_size, "Too much constraints");
259
260   elem = &(var->cnsts[var->cnsts_number++]);
261
262   elem->value = value;
263   elem->constraint = cnst;
264   elem->variable = var;
265
266   if (var->weight)
267     xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
268   else
269     xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
270   if(!sys->selective_update_active) {
271     make_constraint_active(sys, cnst);
272   } else if(elem->value>0 || var->weight >0) {
273     make_constraint_active(sys, cnst);
274     lmm_update_modified_set(sys, cnst);
275     if (var->cnsts_number > 1)
276       lmm_update_modified_set(sys, var->cnsts[0].constraint);
277   }
278 }
279
280 void lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst,
281                     lmm_variable_t var, double value)
282 {
283   int i;
284   sys->modified = 1;
285
286   for (i = 0; i < var->cnsts_number; i++)
287     if (var->cnsts[i].constraint == cnst)
288       break;
289
290   if (i < var->cnsts_number) {
291     if (cnst->shared)
292       var->cnsts[i].value += value;
293     else
294       var->cnsts[i].value = MAX(var->cnsts[i].value, value);
295     lmm_update_modified_set(sys, cnst);
296   } else
297     lmm_expand(sys, cnst, var, value);
298 }
299
300 void lmm_elem_set_value(lmm_system_t sys, lmm_constraint_t cnst,
301                         lmm_variable_t var, double value)
302 {
303   int i;
304
305   for (i = 0; i < var->cnsts_number; i++)
306     if (var->cnsts[i].constraint == cnst)
307       break;
308
309   if (i < var->cnsts_number) {
310     var->cnsts[i].value = value;
311     sys->modified = 1;
312     lmm_update_modified_set(sys, cnst);
313   } else
314     DIE_IMPOSSIBLE;
315 }
316
317 XBT_INLINE lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t sys,
318                                                   lmm_variable_t var,
319                                                   int num)
320 {
321   if (num < var->cnsts_number)
322     return (var->cnsts[num].constraint);
323   else
324     return NULL;
325 }
326
327 XBT_INLINE double lmm_get_cnst_weight_from_var(lmm_system_t sys,
328                                                          lmm_variable_t var,
329                                                          int num)
330 {
331   if (num < var->cnsts_number)
332     return (var->cnsts[num].value);
333   else
334     return 0.0;
335 }
336
337 XBT_INLINE int lmm_get_number_of_cnst_from_var(lmm_system_t sys,
338                                                lmm_variable_t var)
339 {
340   return (var->cnsts_number);
341 }
342
343 lmm_variable_t lmm_get_var_from_cnst(lmm_system_t sys,
344                                      lmm_constraint_t cnst,
345                                      lmm_element_t * elem)
346 {
347   if (!(*elem))
348     *elem = xbt_swag_getFirst(&(cnst->element_set));
349   else
350     *elem = xbt_swag_getNext(*elem, cnst->element_set.offset);
351   if (*elem)
352     return (*elem)->variable;
353   else
354     return NULL;
355 }
356
357 XBT_INLINE void *lmm_constraint_id(lmm_constraint_t cnst)
358 {
359   return cnst->id;
360 }
361
362 XBT_INLINE void *lmm_variable_id(lmm_variable_t var)
363 {
364   return var->id;
365 }
366
367 static XBT_INLINE int saturated_constraint_set_update(lmm_system_t sys,
368                                                       lmm_constraint_t
369                                                       cnst,
370                                                       double *min_usage)
371 {
372   volatile double usage;
373
374   XBT_IN("sys=%p, cnst=%p, min_usage=%f", sys, cnst, *min_usage);
375   if (cnst->usage <= 0) {
376     XBT_OUT();
377     return 1;
378   }
379   if (cnst->remaining <= 0) {
380     XBT_OUT();
381     return 1;
382   }
383   usage = cnst->remaining / cnst->usage;
384   if (*min_usage < 0 || *min_usage > usage) {
385     *min_usage = usage;
386     XBT_HERE(" min_usage=%f (cnst->remaining=%f, cnst->usage=%f)",
387              *min_usage, cnst->remaining, cnst->usage);
388     xbt_swag_reset(&sys->saturated_constraint_set);
389     xbt_swag_insert(cnst, &sys->saturated_constraint_set);
390   } else if (*min_usage == usage) {
391     xbt_swag_insert(cnst, &sys->saturated_constraint_set);
392   }
393   XBT_OUT();
394   return 0;
395 }
396
397 static XBT_INLINE void saturated_variable_set_update(lmm_system_t sys)
398 {
399   lmm_constraint_t cnst = NULL;
400   xbt_swag_t cnst_list = NULL;
401   lmm_element_t elem = NULL;
402   xbt_swag_t elem_list = NULL;
403
404   cnst_list = &(sys->saturated_constraint_set);
405   while ((cnst = xbt_swag_getFirst(cnst_list))) {
406     elem_list = &(cnst->active_element_set);
407     xbt_swag_foreach(elem, elem_list) {
408       if (elem->variable->weight <= 0)
409         break;
410       if ((elem->value > 0))
411         xbt_swag_insert(elem->variable, &(sys->saturated_variable_set));
412     }
413     xbt_swag_remove(cnst, cnst_list);
414   }
415 }
416
417 void lmm_print(lmm_system_t sys)
418 {
419   lmm_constraint_t cnst = NULL;
420   lmm_element_t elem = NULL;
421   lmm_variable_t var = NULL;
422   xbt_swag_t cnst_list = NULL;
423   xbt_swag_t var_list = NULL;
424   xbt_swag_t elem_list = NULL;
425   char print_buf[1024];
426   char *trace_buf = xbt_malloc0(sizeof(char));
427   double sum = 0.0;
428
429   /* Printing Objective */
430   var_list = &(sys->variable_set);
431   sprintf(print_buf, "MAX-MIN ( ");
432   trace_buf =
433       xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
434   strcat(trace_buf, print_buf);
435   xbt_swag_foreach(var, var_list) {
436     sprintf(print_buf, "'%d'(%f) ", var->id_int, var->weight);
437     trace_buf =
438         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
439     strcat(trace_buf, print_buf);
440   }
441   sprintf(print_buf, ")");
442   trace_buf =
443       xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
444   strcat(trace_buf, print_buf);
445   XBT_DEBUG("%20s", trace_buf);
446   trace_buf[0] = '\000';
447
448   XBT_DEBUG("Constraints");
449   /* Printing Constraints */
450   cnst_list = &(sys->active_constraint_set);
451   xbt_swag_foreach(cnst, cnst_list) {
452     sum = 0.0;
453     elem_list = &(cnst->element_set);
454     sprintf(print_buf, "\t");
455     trace_buf =
456         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
457     strcat(trace_buf, print_buf);
458     sprintf(print_buf, "%s(",(cnst->shared)?"":"max");
459     trace_buf =
460       xbt_realloc(trace_buf,
461                   strlen(trace_buf) + strlen(print_buf) + 1);
462     strcat(trace_buf, print_buf);      
463     xbt_swag_foreach(elem, elem_list) {
464       sprintf(print_buf, "%f.'%d'(%f) %s ", elem->value,
465               elem->variable->id_int, elem->variable->value,(cnst->shared)?"+":",");
466       trace_buf =
467           xbt_realloc(trace_buf,
468                       strlen(trace_buf) + strlen(print_buf) + 1);
469       strcat(trace_buf, print_buf);
470       if(cnst->shared) 
471         sum += elem->value * elem->variable->value;
472       else 
473         sum = MAX(sum,elem->value * elem->variable->value);
474     }
475     sprintf(print_buf, "0) <= %f ('%d')", cnst->bound, cnst->id_int);
476     trace_buf =
477         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
478     strcat(trace_buf, print_buf);
479
480     if (!cnst->shared) {
481       sprintf(print_buf, " [MAX-Constraint]");
482       trace_buf =
483           xbt_realloc(trace_buf,
484                       strlen(trace_buf) + strlen(print_buf) + 1);
485       strcat(trace_buf, print_buf);
486     }
487     XBT_DEBUG("%s", trace_buf);
488     trace_buf[0] = '\000';
489     xbt_assert(!double_positive(sum - cnst->bound),
490                 "Incorrect value (%f is not smaller than %f): %g",
491                 sum, cnst->bound, sum - cnst->bound);
492   }
493
494   XBT_DEBUG("Variables");
495   /* Printing Result */
496   xbt_swag_foreach(var, var_list) {
497     if (var->bound > 0) {
498       XBT_DEBUG("'%d'(%f) : %f (<=%f)", var->id_int, var->weight, var->value,
499              var->bound);
500       xbt_assert(!double_positive(var->value - var->bound),
501                   "Incorrect value (%f is not smaller than %f",
502                   var->value, var->bound);
503     } else {
504       XBT_DEBUG("'%d'(%f) : %f", var->id_int, var->weight, var->value);
505     }
506   }
507
508   free(trace_buf);
509 }
510
511 void lmm_solve(lmm_system_t sys)
512 {
513   lmm_variable_t var = NULL;
514   lmm_constraint_t cnst = NULL;
515   lmm_constraint_t cnst_next = NULL;
516   lmm_element_t elem = NULL;
517   xbt_swag_t cnst_list = NULL;
518   xbt_swag_t var_list = NULL;
519   xbt_swag_t elem_list = NULL;
520   double min_usage = -1;
521   double min_bound = -1;
522
523   if (!(sys->modified))
524     return;
525
526   XBT_IN("(sys=%p)", sys);
527
528   /*
529    * Compute Usage and store the variables that reach the maximum.
530    */
531   cnst_list =
532       sys->
533       selective_update_active ? &(sys->modified_constraint_set) :
534       &(sys->active_constraint_set);
535
536   XBT_DEBUG("Active constraints : %d", xbt_swag_size(cnst_list));
537   /* Init: Only modified code portions */
538   xbt_swag_foreach(cnst, cnst_list) {
539     elem_list = &(cnst->element_set);
540     //XBT_DEBUG("Variable set : %d", xbt_swag_size(elem_list));
541     xbt_swag_foreach(elem, elem_list) {
542       var = elem->variable;
543       if (var->weight <= 0.0)
544         break;
545       var->value = 0.0;
546     }
547   }
548
549   xbt_swag_foreach_safe(cnst, cnst_next, cnst_list) {
550     /* INIT */
551     cnst->remaining = cnst->bound;
552     if (cnst->remaining == 0)
553       continue;
554     cnst->usage = 0;
555     elem_list = &(cnst->element_set);
556     xbt_swag_foreach(elem, elem_list) {
557       /* 0-weighted elements (ie, sleep actions) are at the end of the swag and we don't want to consider them */
558       if (elem->variable->weight <= 0)
559         break;
560       if ((elem->value > 0)) {
561         if (cnst->shared)
562           cnst->usage += elem->value / elem->variable->weight;
563         else if (cnst->usage < elem->value / elem->variable->weight)
564           cnst->usage = elem->value / elem->variable->weight;
565
566         make_elem_active(elem);
567         if (sys->keep_track)
568           xbt_swag_insert(elem->variable->id, sys->keep_track);
569       }
570     }
571     XBT_DEBUG("Constraint Usage '%d' : %f", cnst->id_int, cnst->usage);
572     /* Saturated constraints update */
573     if(cnst->usage>0) {
574       xbt_swag_remove(cnst, cnst_list);
575       xbt_swag_insert_at_head(cnst, cnst_list);
576     }
577     saturated_constraint_set_update(sys, cnst, &min_usage);
578   }
579   saturated_variable_set_update(sys);
580
581   /* Saturated variables update */
582
583   do {
584     /* Fix the variables that have to be */
585     var_list = &(sys->saturated_variable_set);
586
587     xbt_swag_foreach(var, var_list) {
588       if (var->weight <= 0.0)
589         DIE_IMPOSSIBLE;
590       /* First check if some of these variables have reach their upper
591          bound and update min_usage accordingly. */
592       XBT_DEBUG
593           ("var=%d, var->bound=%f, var->weight=%f, min_usage=%f, var->bound*var->weight=%f",
594            var->id_int, var->bound, var->weight, min_usage,
595            var->bound * var->weight);
596       if ((var->bound > 0) && (var->bound * var->weight < min_usage)) {
597         if (min_bound < 0)
598           min_bound = var->bound;
599         else
600           min_bound = MIN(min_bound, var->bound);
601         XBT_DEBUG("Updated min_bound=%f", min_bound);
602       }
603     }
604
605
606     while ((var = xbt_swag_getFirst(var_list))) {
607       int i;
608
609       if (min_bound < 0) {
610         var->value = min_usage / var->weight;
611       } else {
612         if (min_bound == var->bound)
613           var->value = var->bound;
614         else {
615           xbt_swag_remove(var, var_list);
616           continue;
617         }
618       }
619       XBT_DEBUG("Min usage: %f, Var(%d)->weight: %f, Var(%d)->value: %f ",
620              min_usage, var->id_int, var->weight, var->id_int, var->value);
621
622
623       /* Update usage */
624
625       for (i = 0; i < var->cnsts_number; i++) {
626         elem = &var->cnsts[i];
627         cnst = elem->constraint;
628         if (cnst->shared) {
629           double_update(&(cnst->remaining), elem->value * var->value);
630           double_update(&(cnst->usage), elem->value / var->weight);
631           if(cnst->usage<=0 || cnst->remaining<=0) {
632             xbt_swag_remove(cnst, cnst_list);
633             xbt_swag_insert_at_tail(cnst, cnst_list);
634           }
635           make_elem_inactive(elem);
636         } else {                /* FIXME one day: We recompute usage.... :( */
637           cnst->usage = 0.0;
638           make_elem_inactive(elem);
639           elem_list = &(cnst->element_set);
640           xbt_swag_foreach(elem, elem_list) {
641             if (elem->variable->weight <= 0)
642               break;
643             if (elem->variable->value > 0)
644               break;
645             if ((elem->value > 0)) {
646               cnst->usage =
647                   MAX(cnst->usage, elem->value / elem->variable->weight);
648               XBT_DEBUG("Constraint Usage %d : %f", cnst->id_int,
649                      cnst->usage);
650               make_elem_active(elem);
651             }
652           }
653         }
654       }
655       xbt_swag_remove(var, var_list);
656     }
657
658     /* Find out which variables reach the maximum */
659     min_usage = -1;
660     min_bound = -1;
661
662     xbt_swag_foreach(cnst, cnst_list) {
663       if(saturated_constraint_set_update(sys, cnst, &min_usage)) break;
664     }
665     saturated_variable_set_update(sys);
666
667   } while (xbt_swag_size(&(sys->saturated_variable_set)));
668
669   sys->modified = 0;
670   if (sys->selective_update_active)
671     lmm_remove_all_modified_set(sys);
672
673   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
674     lmm_print(sys);
675   }
676   XBT_OUT();
677 }
678
679 /* Not a O(1) function */
680
681 void lmm_update(lmm_system_t sys, lmm_constraint_t cnst,
682                 lmm_variable_t var, double value)
683 {
684   int i;
685
686   for (i = 0; i < var->cnsts_number; i++)
687     if (var->cnsts[i].constraint == cnst) {
688       var->cnsts[i].value = value;
689       sys->modified = 1;
690       lmm_update_modified_set(sys, cnst);
691       return;
692     }
693 }
694
695 /** \brief Attribute the value bound to var->bound.
696  * 
697  *  \param sys the lmm_system_t
698  *  \param var the lmm_variable_t
699  *  \param bound the new bound to associate with var
700  * 
701  *  Makes var->bound equal to bound. Whenever this function is called 
702  *  a change is  signed in the system. To
703  *  avoid false system changing detection it is a good idea to test 
704  *  (bound != 0) before calling it.
705  *
706  */
707 void lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
708                                double bound)
709 {
710   sys->modified = 1;
711   var->bound = bound;
712
713   if (var->cnsts_number)
714     lmm_update_modified_set(sys, var->cnsts[0].constraint);
715 }
716
717
718 void lmm_update_variable_weight(lmm_system_t sys, lmm_variable_t var,
719                                 double weight)
720 {
721   int i;
722   lmm_element_t elem;
723
724   if (weight == var->weight)
725     return;
726   XBT_IN("(sys=%p, var=%p, weight=%f)", sys, var, weight);
727   sys->modified = 1;
728   var->weight = weight;
729   xbt_swag_remove(var, &(sys->variable_set));
730   if (weight)
731     xbt_swag_insert_at_head(var, &(sys->variable_set));
732   else
733     xbt_swag_insert_at_tail(var, &(sys->variable_set));
734
735   for (i = 0; i < var->cnsts_number; i++) {
736     elem = &var->cnsts[i];
737     xbt_swag_remove(elem, &(elem->constraint->element_set));
738     if (weight)
739       xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
740     else
741       xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
742
743     if (i == 0)
744       lmm_update_modified_set(sys, elem->constraint);
745   }
746   if (!weight)
747     var->value = 0.0;
748
749   XBT_OUT();
750 }
751
752 XBT_INLINE double lmm_get_variable_weight(lmm_variable_t var)
753 {
754   return var->weight;
755 }
756
757 XBT_INLINE void lmm_update_constraint_bound(lmm_system_t sys,
758                                             lmm_constraint_t cnst,
759                                             double bound)
760 {
761   sys->modified = 1;
762   lmm_update_modified_set(sys, cnst);
763   cnst->bound = bound;
764 }
765
766 XBT_INLINE int lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst)
767 {
768   return xbt_swag_belongs(cnst, &(sys->active_constraint_set));
769 }
770
771 XBT_INLINE lmm_constraint_t lmm_get_first_active_constraint(lmm_system_t
772                                                             sys)
773 {
774   return xbt_swag_getFirst(&(sys->active_constraint_set));
775 }
776
777 XBT_INLINE lmm_constraint_t lmm_get_next_active_constraint(lmm_system_t
778                                                            sys,
779                                                            lmm_constraint_t
780                                                            cnst)
781 {
782   return xbt_swag_getNext(cnst, (sys->active_constraint_set).offset);
783 }
784
785 #ifdef HAVE_LATENCY_BOUND_TRACKING
786 XBT_INLINE int lmm_is_variable_limited_by_latency(lmm_variable_t var)
787 {
788   return (double_equals(var->bound, var->value));
789 }
790 #endif
791
792
793 /** \brief Update the constraint set propagating recursively to
794  *  other constraints so the system should not be entirely computed.
795  *
796  *  \param sys the lmm_system_t
797  *  \param cnst the lmm_constraint_t affected by the change
798  *
799  *  A recursive algorithm to optimize the system recalculation selecting only
800  *  constraints that have changed. Each constraint change is propagated
801  *  to the list of constraints for each variable.
802  */
803 static void lmm_update_modified_set_rec(lmm_system_t sys,
804                                         lmm_constraint_t cnst)
805 {
806   lmm_element_t elem;
807
808   xbt_swag_foreach(elem, &cnst->element_set) {
809     lmm_variable_t var = elem->variable;
810     s_lmm_element_t *cnsts = var->cnsts;
811     int i;
812     for (i = 0; var->visited != sys->visited_counter
813              && i < var->cnsts_number ; i++) {
814       if (cnsts[i].constraint != cnst
815           && !xbt_swag_belongs(cnsts[i].constraint,
816                                &sys->modified_constraint_set)) {
817         xbt_swag_insert(cnsts[i].constraint, &sys->modified_constraint_set);
818         lmm_update_modified_set_rec(sys, cnsts[i].constraint);
819       }
820     }
821     var->visited = sys->visited_counter;
822   }
823 }
824
825 static void lmm_update_modified_set(lmm_system_t sys,
826                                     lmm_constraint_t cnst)
827 {
828   /* nothing to do if selective update isn't active */
829   if (sys->selective_update_active
830       && !xbt_swag_belongs(cnst, &sys->modified_constraint_set)) {
831     xbt_swag_insert(cnst, &sys->modified_constraint_set);
832     lmm_update_modified_set_rec(sys, cnst);
833   }
834 }
835
836 /** \brief Remove all constraints of the modified_constraint_set.
837  *
838  *  \param sys the lmm_system_t
839  */
840 static void lmm_remove_all_modified_set(lmm_system_t sys)
841 {
842   if (++sys->visited_counter == 1) {
843     /* the counter wrapped around, reset each variable->visited */
844     lmm_variable_t var;
845     xbt_swag_foreach(var, &sys->variable_set)
846       var->visited = 0;
847   }
848   xbt_swag_reset(&sys->modified_constraint_set);
849 }