Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8e8d049715bfb0a76de90c769eb38beedeb39eae
[simgrid.git] / src / surf / maxmin.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "xbt/mallocator.h"
12 #include "maxmin_private.h"
13 #include <stdlib.h>
14 #include <stdio.h> /* sprintf */
15 #include <math.h>
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_maxmin, surf,
17                                 "Logging specific to SURF (maxmin)");
18
19 static void *lmm_variable_mallocator_new_f(void);
20 static void lmm_variable_mallocator_free_f(void *var);
21 static void lmm_variable_mallocator_reset_f(void *var);
22
23 lmm_system_t lmm_system_new(void)
24 {
25   lmm_system_t l = NULL;
26   s_lmm_variable_t var;
27   s_lmm_constraint_t cnst;
28
29   l = xbt_new0(s_lmm_system_t, 1);
30
31   l->modified = 0;
32   xbt_swag_init(&(l->variable_set),
33                 xbt_swag_offset(var, variable_set_hookup));
34   xbt_swag_init(&(l->constraint_set),
35                 xbt_swag_offset(cnst, constraint_set_hookup));
36
37   xbt_swag_init(&(l->active_constraint_set),
38                 xbt_swag_offset(cnst, active_constraint_set_hookup));
39
40   xbt_swag_init(&(l->saturated_variable_set),
41                 xbt_swag_offset(var, saturated_variable_set_hookup));
42   xbt_swag_init(&(l->saturated_constraint_set),
43                 xbt_swag_offset(cnst, saturated_constraint_set_hookup));
44
45   l->variable_mallocator = xbt_mallocator_new(64,
46                                               lmm_variable_mallocator_new_f,
47                                               lmm_variable_mallocator_free_f,
48                                               lmm_variable_mallocator_reset_f);
49
50   return l;
51 }
52
53 void lmm_system_free(lmm_system_t sys)
54 {
55   lmm_variable_t var = NULL;
56   lmm_constraint_t cnst = NULL;
57
58   while ((var = extract_variable(sys)))
59     lmm_var_free(sys, var);
60
61   while ((cnst = extract_constraint(sys)))
62     lmm_cnst_free(sys, cnst);
63
64   xbt_mallocator_free(sys->variable_mallocator);
65   free(sys);
66 }
67
68 void lmm_variable_disable(lmm_system_t sys, lmm_variable_t var)
69 {
70   int i;
71   lmm_element_t elem = NULL;
72
73   XBT_IN2("(sys=%p, var=%p)", sys, var);
74   sys->modified = 1;
75
76   for (i = 0; i < var->cnsts_number; i++) {
77     elem = &var->cnsts[i];
78     xbt_swag_remove(elem, &(elem->constraint->element_set));
79     xbt_swag_remove(elem, &(elem->constraint->active_element_set));
80     if (!xbt_swag_size(&(elem->constraint->element_set)))
81       make_constraint_inactive(sys, elem->constraint);
82   }
83   var->cnsts_number = 0;
84   XBT_OUT;
85 }
86
87 static void lmm_var_free(lmm_system_t sys, lmm_variable_t var)
88 {
89
90   lmm_variable_disable(sys, var);
91   free(var->cnsts);
92   xbt_mallocator_release(sys->variable_mallocator, var);
93 }
94
95 static void lmm_cnst_free(lmm_system_t sys, lmm_constraint_t cnst)
96 {
97 /*   xbt_assert0(xbt_swag_size(&(cnst->element_set)), */
98 /*            "This list should be empty!"); */
99   remove_active_constraint(sys, cnst);
100   free(cnst);
101 }
102
103 lmm_constraint_t lmm_constraint_new(lmm_system_t sys, void *id,
104                                     double bound_value)
105 {
106   lmm_constraint_t cnst = NULL;
107   s_lmm_element_t elem;
108
109   cnst = xbt_new0(s_lmm_constraint_t, 1);
110   cnst->id = id;
111   xbt_swag_init(&(cnst->element_set),
112                 xbt_swag_offset(elem, element_set_hookup));
113   xbt_swag_init(&(cnst->active_element_set),
114                 xbt_swag_offset(elem, active_element_set_hookup));
115
116   cnst->bound = bound_value;
117   cnst->usage = 0;
118   cnst->shared = 1;
119   insert_constraint(sys, cnst);
120
121   return cnst;
122 }
123
124 void lmm_constraint_shared(lmm_constraint_t cnst)
125 {
126   cnst->shared = 0;
127 }
128
129 void lmm_constraint_free(lmm_system_t sys, lmm_constraint_t cnst)
130 {
131   remove_constraint(sys, cnst);
132   lmm_cnst_free(sys, cnst);
133 }
134
135 static void *lmm_variable_mallocator_new_f(void)
136 {
137   return xbt_new(s_lmm_variable_t, 1);
138 }
139
140 static void lmm_variable_mallocator_free_f(void *var)
141 {
142   xbt_free(var);
143 }
144
145 static void lmm_variable_mallocator_reset_f(void *var)
146 {
147   /* memset to zero like calloc */
148   memset(var, 0, sizeof(s_lmm_variable_t));
149 }
150
151 lmm_variable_t lmm_variable_new(lmm_system_t sys, void *id,
152                                 double weight,
153                                 double bound, int number_of_constraints)
154 {
155   lmm_variable_t var = NULL;
156   int i;
157
158   XBT_IN5("(sys=%p, id=%p, weight=%f, bound=%f, num_cons =%d)",
159           sys, id, weight, bound, number_of_constraints);
160
161   var = xbt_mallocator_get(sys->variable_mallocator);
162   var->id = id;
163   var->cnsts = xbt_new0(s_lmm_element_t, number_of_constraints);
164   for (i = 0; i < number_of_constraints; i++) {
165     /* Should be useless because of the 
166        calloc but it seems to help valgrind... */
167     var->cnsts[i].element_set_hookup.next = NULL;
168     var->cnsts[i].element_set_hookup.prev = NULL;
169     var->cnsts[i].active_element_set_hookup.next = NULL;
170     var->cnsts[i].active_element_set_hookup.prev = NULL;
171     var->cnsts[i].constraint = NULL;
172     var->cnsts[i].variable = NULL;
173     var->cnsts[i].value = 0.0;
174   }
175   var->cnsts_size = number_of_constraints;
176   var->cnsts_number = 0;        /* Should be useless because of the 
177                                    calloc but it seems to help valgrind... */
178   var->weight = weight;
179   var->bound = bound;
180   var->value = 0.0;
181   var->df = 0.0;
182
183   var->func_f = func_f_def;
184   var->func_fp = func_fp_def;
185   var->func_fpi = func_fpi_def;
186
187   if (weight)
188     xbt_swag_insert_at_head(var, &(sys->variable_set));
189   else
190     xbt_swag_insert_at_tail(var, &(sys->variable_set));
191   XBT_OUT;
192   return var;
193 }
194
195 void lmm_variable_free(lmm_system_t sys, lmm_variable_t var)
196 {
197   remove_variable(sys, var);
198   lmm_var_free(sys, var);
199 }
200
201 double lmm_variable_getvalue(lmm_variable_t var)
202 {
203   return (var->value);
204 }
205
206 double lmm_variable_getbound(lmm_variable_t var)
207 {
208   return (var->bound);
209 }
210
211 void lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
212                 lmm_variable_t var, double value)
213 {
214   lmm_element_t elem = NULL;
215
216   sys->modified = 1;
217
218   xbt_assert0(var->cnsts_number < var->cnsts_size, "Too much constraints");
219
220   elem = &(var->cnsts[var->cnsts_number++]);
221
222   elem->value = value;
223   elem->constraint = cnst;
224   elem->variable = var;
225
226   if (var->weight)
227     xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
228   else
229     xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
230
231   make_constraint_active(sys, cnst);
232 }
233
234 void lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst,
235                     lmm_variable_t var, double value)
236 {
237   int i;
238   sys->modified = 1;
239
240   for (i = 0; i < var->cnsts_number; i++)
241     if (var->cnsts[i].constraint == cnst)
242       break;
243
244   if (i < var->cnsts_number)
245     var->cnsts[i].value += value;
246   else
247     lmm_expand(sys, cnst, var, value);
248 }
249
250 void lmm_elem_set_value(lmm_system_t sys, lmm_constraint_t cnst,
251                         lmm_variable_t var, double value)
252 {
253   int i;
254
255   for (i = 0; i < var->cnsts_number; i++)
256     if (var->cnsts[i].constraint == cnst)
257       break;
258
259   if (i < var->cnsts_number) {
260     var->cnsts[i].value = value;
261     sys->modified = 1;
262   } else
263     DIE_IMPOSSIBLE;
264 }
265
266 lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t sys,
267                                        lmm_variable_t var, int num)
268 {
269   if (num < var->cnsts_number)
270     return (var->cnsts[num].constraint);
271   else
272     return NULL;
273 }
274
275 int lmm_get_number_of_cnst_from_var(lmm_system_t sys, lmm_variable_t var)
276 {
277   return (var->cnsts_number);
278 }
279
280 lmm_variable_t lmm_get_var_from_cnst(lmm_system_t sys,
281                                      lmm_constraint_t cnst,
282                                      lmm_element_t * elem)
283 {
284   if (!(*elem))
285     *elem = xbt_swag_getFirst(&(cnst->element_set));
286   else
287     *elem = xbt_swag_getNext(*elem, cnst->element_set.offset);
288   if(*elem)
289     return (*elem)->variable;
290   else 
291     return NULL;
292 }
293
294 void *lmm_constraint_id(lmm_constraint_t cnst)
295 {
296   return cnst->id;
297 }
298
299 void *lmm_variable_id(lmm_variable_t var)
300 {
301   return var->id;
302 }
303
304 static void saturated_constraint_set_update(lmm_system_t sys,
305                                             lmm_constraint_t cnst,
306                                             double *min_usage)
307 {
308   lmm_constraint_t useless_cnst = NULL;
309
310   XBT_IN3("sys=%p, cnst=%p, min_usage=%f", sys, cnst, *min_usage);
311   if (cnst->usage <= 0) {
312     XBT_OUT;
313     return;
314   }
315   if (cnst->remaining <= 0) {
316     XBT_OUT;
317     return;
318   }
319   if ((*min_usage < 0) || (*min_usage > cnst->remaining / cnst->usage)) {
320     *min_usage = cnst->remaining / cnst->usage;
321     LOG3(xbt_log_priority_trace,
322          "min_usage=%f (cnst->remaining=%f, cnst->usage=%f)", *min_usage,
323          cnst->remaining, cnst->usage);
324     while ((useless_cnst =
325             xbt_swag_getFirst(&(sys->saturated_constraint_set))))
326       xbt_swag_remove(useless_cnst, &(sys->saturated_constraint_set));
327
328     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
329   } else if (*min_usage == cnst->remaining / cnst->usage) {
330     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
331   }
332   XBT_OUT;
333 }
334
335 static void saturated_variable_set_update(lmm_system_t sys)
336 {
337   lmm_constraint_t cnst = NULL;
338   xbt_swag_t cnst_list = NULL;
339   lmm_element_t elem = NULL;
340   xbt_swag_t elem_list = NULL;
341
342   cnst_list = &(sys->saturated_constraint_set);
343   while ((cnst = xbt_swag_getFirst(cnst_list))) {
344     elem_list = &(cnst->active_element_set);
345     xbt_swag_foreach(elem, elem_list) {
346       if (elem->variable->weight <= 0)
347         break;
348       if ((elem->value > 0))
349         xbt_swag_insert(elem->variable, &(sys->saturated_variable_set));
350     }
351     xbt_swag_remove(cnst, cnst_list);
352   }
353 }
354
355 void lmm_print(lmm_system_t sys)
356 {
357   lmm_constraint_t cnst = NULL;
358   lmm_element_t elem = NULL;
359   lmm_variable_t var = NULL;
360   xbt_swag_t cnst_list = NULL;
361   xbt_swag_t var_list = NULL;
362   xbt_swag_t elem_list = NULL;
363   char print_buf[1024];
364   char *trace_buf = xbt_malloc0(sizeof(char));
365   double sum = 0.0;
366
367   /* Printing Objective */
368   var_list = &(sys->variable_set);
369   sprintf(print_buf, "MAX-MIN ( ");
370   trace_buf =
371       xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
372   strcat(trace_buf, print_buf);
373   xbt_swag_foreach(var, var_list) {
374     sprintf(print_buf, "'%p'(%f) ", var, var->weight);
375     trace_buf =
376         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
377     strcat(trace_buf, print_buf);
378   }
379   sprintf(print_buf, ")");
380   trace_buf =
381       xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
382   strcat(trace_buf, print_buf);
383   DEBUG1("%s", trace_buf);
384   trace_buf[0] = '\000';
385
386   DEBUG0("Constraints");
387   /* Printing Constraints */
388   cnst_list = &(sys->active_constraint_set);
389   xbt_swag_foreach(cnst, cnst_list) {
390     sum = 0.0;
391     elem_list = &(cnst->element_set);
392     sprintf(print_buf, "\t");
393     trace_buf =
394         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
395     strcat(trace_buf, print_buf);
396     xbt_swag_foreach(elem, elem_list) {
397       sprintf(print_buf, "%f.'%p'(%f) + ", elem->value,
398               elem->variable, elem->variable->value);
399       trace_buf =
400           xbt_realloc(trace_buf,
401                       strlen(trace_buf) + strlen(print_buf) + 1);
402       strcat(trace_buf, print_buf);
403       sum += elem->value * elem->variable->value;
404     }
405     sprintf(print_buf, "0 <= %f ('%p')", cnst->bound, cnst);
406     trace_buf =
407         xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
408     strcat(trace_buf, print_buf);
409
410     if (!cnst->shared) {
411       sprintf(print_buf, " [MAX-Constraint]");
412       trace_buf =
413           xbt_realloc(trace_buf,
414                       strlen(trace_buf) + strlen(print_buf) + 1);
415       strcat(trace_buf, print_buf);
416     }
417     DEBUG1("%s", trace_buf);
418     trace_buf[0] = '\000';
419     if (double_positive(sum - cnst->bound))
420       WARN3("Incorrect value (%f is not smaller than %f): %g",
421             sum, cnst->bound, sum - cnst->bound);
422   }
423
424   DEBUG0("Variables");
425   /* Printing Result */
426   xbt_swag_foreach(var, var_list) {
427     if (var->bound > 0) {
428       DEBUG4("'%p'(%f) : %f (<=%f)", var, var->weight, var->value,
429              var->bound);
430       if (double_positive(var->value - var->bound))
431         WARN2("Incorrect value (%f is not smaller than %f",
432               var->value, var->bound);
433     } else
434       DEBUG3("'%p'(%f) : %f", var, var->weight, var->value);
435   }
436
437   free(trace_buf);
438 }
439
440 void lmm_solve(lmm_system_t sys)
441 {
442   lmm_variable_t var = NULL;
443   lmm_constraint_t cnst = NULL;
444   lmm_element_t elem = NULL;
445   xbt_swag_t cnst_list = NULL;
446   xbt_swag_t var_list = NULL;
447   xbt_swag_t elem_list = NULL;
448   double min_usage = -1;
449
450   if (!(sys->modified))
451     return;
452
453   /* Init */
454   var_list = &(sys->variable_set);
455   DEBUG1("Variable set : %d", xbt_swag_size(var_list));
456   xbt_swag_foreach(var, var_list) {
457     int nb=0;
458     int i;
459     if(var->weight<=0.0) break;
460     var->value = 0.0;
461     for (i = 0; i < var->cnsts_number; i++) {
462       if(var->cnsts[i].value==0.0) nb++;
463     }
464     if((nb==var->cnsts_number) && (var->weight>0.0))
465       var->value = 1.0;
466   }
467
468   /* 
469    * Compute Usage and store the variables that reach the maximum.
470    */
471   cnst_list = &(sys->active_constraint_set);
472   DEBUG1("Active constraints : %d", xbt_swag_size(cnst_list));
473   xbt_swag_foreach(cnst, cnst_list) {
474     /* INIT */
475     cnst->remaining = cnst->bound;
476     cnst->usage = 0;
477     elem_list = &(cnst->element_set);
478     cnst->usage = 0.0;
479     xbt_swag_foreach(elem, elem_list) {
480       if (elem->variable->weight <= 0)
481         break;
482       if ((elem->value > 0)) {
483         if (cnst->shared)
484           cnst->usage += elem->value / elem->variable->weight;
485         else if (cnst->usage < elem->value / elem->variable->weight)
486           cnst->usage = elem->value / elem->variable->weight;
487         DEBUG2("Constraint Usage %p : %f", cnst, cnst->usage);
488         make_elem_active(elem);
489       }
490     }
491     /* Saturated constraints update */
492     saturated_constraint_set_update(sys, cnst, &min_usage);
493   }
494   saturated_variable_set_update(sys);
495
496   /* Saturated variables update */
497
498   do {
499     /* Fix the variables that have to be */
500     var_list = &(sys->saturated_variable_set);
501
502     xbt_swag_foreach(var, var_list) {
503       if(var->weight<=0.0) DIE_IMPOSSIBLE;
504       /* First check if some of these variables have reach their upper
505          bound and update min_usage accordingly. */
506       DEBUG5
507           ("var=%p, var->bound=%f, var->weight=%f, min_usage=%f, var->bound*var->weight=%f",
508            var, var->bound, var->weight, min_usage,
509            var->bound * var->weight);
510       if ((var->bound > 0) && (var->bound * var->weight < min_usage)) {
511         min_usage = var->bound * var->weight;
512         DEBUG1("Updated min_usage=%f", min_usage);
513       }
514     }
515
516
517     while ((var = xbt_swag_getFirst(var_list))) {
518       int i;
519
520       var->value = min_usage / var->weight;
521       DEBUG5("Min usage: %f, Var(%p)->weight: %f, Var(%p)->value: %f ",
522              min_usage, var, var->weight, var, var->value);
523
524
525       /* Update usage */
526
527       for (i = 0; i < var->cnsts_number; i++) {
528         elem = &var->cnsts[i];
529         cnst = elem->constraint;
530         if (cnst->shared) {
531           double_update(&(cnst->remaining), elem->value * var->value);
532           double_update(&(cnst->usage), elem->value / var->weight);
533           make_elem_inactive(elem);
534         } else {                /* FIXME one day: We recompute usage.... :( */
535           cnst->usage = 0.0;
536           make_elem_inactive(elem);
537           xbt_swag_foreach(elem, elem_list) {
538             if (elem->variable->weight <= 0)
539               break;
540             if (elem->variable->value > 0)
541               break;
542             if ((elem->value > 0)) {
543               if (cnst->usage < elem->value / elem->variable->weight)
544                 cnst->usage = elem->value / elem->variable->weight;
545               DEBUG2("Constraint Usage %p : %f", cnst, cnst->usage);
546               make_elem_active(elem);
547             }
548           }
549         }
550       }
551       xbt_swag_remove(var, var_list);
552     }
553
554     /* Find out which variables reach the maximum */
555     cnst_list = &(sys->active_constraint_set);
556     min_usage = -1;
557     xbt_swag_foreach(cnst, cnst_list) {
558       saturated_constraint_set_update(sys, cnst, &min_usage);
559     }
560     saturated_variable_set_update(sys);
561
562   } while (xbt_swag_size(&(sys->saturated_variable_set)));
563
564   sys->modified = 0;
565   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
566     lmm_print(sys);
567   }
568 }
569
570 /* Not a O(1) function */
571
572 void lmm_update(lmm_system_t sys, lmm_constraint_t cnst,
573                 lmm_variable_t var, double value)
574 {
575   int i;
576
577   sys->modified = 1;
578   for (i = 0; i < var->cnsts_number; i++)
579     if (var->cnsts[i].constraint == cnst) {
580       var->cnsts[i].value = value;
581       return;
582     }
583 }
584
585 /** \brief Attribute the value bound to var->bound.
586  * 
587  *  \param sys the lmm_system_t
588  *  \param var the lmm_variable_t
589  *  \param bound the new bound to associate with var
590  * 
591  *  Makes var->bound equal to bound. Whenever this function is called 
592  *  a change is  signed in the system. To
593  *  avoid false system changing detection it is a good idea to test 
594  *  (bound != 0) before calling it.
595  *
596  */
597 void lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
598                                double bound)
599 {
600   sys->modified = 1;
601   var->bound = bound;
602 }
603
604 /** \brief Add the value delta to var->df (the sum of latencies).
605  * 
606  *  \param sys the lmm_system_t associated
607  *  \param var the lmm_variable_t which need to updated
608  *  \param delta the variation of the latency
609  * 
610  *  Add the value delta to var->df (the sum of latencys associated to the
611  *  flow). Whenever this function is called a change is  signed in the system. To
612  *  avoid false system changing detection it is a good idea to test 
613  *  (delta != 0) before calling it.
614  *
615  */
616 void lmm_update_variable_latency(lmm_system_t sys, lmm_variable_t var,
617                                  double delta)
618 {
619   sys->modified = 1;
620   var->df += delta;
621 }
622
623 void lmm_update_variable_weight(lmm_system_t sys, lmm_variable_t var,
624                                 double weight)
625 {
626   int i;
627   lmm_element_t elem;
628
629   if(weight==var->weight) return;
630   XBT_IN3("(sys=%p, var=%p, weight=%f)", sys, var, weight);
631   sys->modified = 1;
632   var->weight = weight;
633   xbt_swag_remove(var, &(sys->variable_set));
634   if (weight)
635     xbt_swag_insert_at_head(var, &(sys->variable_set));
636   else
637     xbt_swag_insert_at_tail(var, &(sys->variable_set));
638
639   for (i = 0; i < var->cnsts_number; i++) {
640     elem = &var->cnsts[i];
641     xbt_swag_remove(elem, &(elem->constraint->element_set));
642     if (weight)
643       xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
644     else
645       xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
646   }
647   if(!weight)
648     var->value = 0.0;
649
650   XBT_OUT;
651 }
652
653 double lmm_get_variable_weight(lmm_variable_t var)
654 {
655   return var->weight;
656 }
657
658 void lmm_update_constraint_bound(lmm_system_t sys, lmm_constraint_t cnst,
659                                  double bound)
660 {
661   sys->modified = 1;
662   cnst->bound = bound;
663 }
664
665 int lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst)
666 {
667   return xbt_swag_belongs(cnst, &(sys->active_constraint_set));
668 }
669
670 lmm_constraint_t lmm_get_first_active_constraint(lmm_system_t sys)
671 {
672   return xbt_swag_getFirst(&(sys->active_constraint_set));
673 }
674
675 lmm_constraint_t lmm_get_next_active_constraint(lmm_system_t sys,
676                                                 lmm_constraint_t cnst)
677 {
678   return xbt_swag_getNext(cnst, (sys->active_constraint_set).offset);
679 }