Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Bug fix + cosmetics
[simgrid.git] / src / surf / fair_bottleneck.c
1 /*      $Id$ */
2
3 /* Copyright (c) 2007 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 "maxmin_private.h"
12 #include <stdlib.h>
13 #include <math.h>
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_maxmin);
16 #define SHOW_EXPR_G(expr) DEBUG1(#expr " = %g",expr);
17 #define SHOW_EXPR_D(expr) DEBUG1(#expr " = %d",expr);
18 #define SHOW_EXPR_P(expr) DEBUG1(#expr " = %p",expr);
19
20 void bottleneck_solve(lmm_system_t sys)
21 {
22   lmm_variable_t var = NULL;
23   lmm_constraint_t cnst = NULL;
24   lmm_constraint_t useless_cnst = NULL;
25   s_lmm_constraint_t s_cnst;
26   lmm_constraint_t cnst_next = NULL;
27   lmm_element_t elem = NULL;
28   xbt_swag_t cnst_list = NULL;
29   xbt_swag_t var_list = NULL;
30   xbt_swag_t elem_list = NULL;
31   double min_usage = -1;
32   int i;
33
34   static s_xbt_swag_t cnst_to_update;
35
36   if (!(sys->modified))
37     return;
38
39   /* Init */
40   xbt_swag_init(&(cnst_to_update),
41                 xbt_swag_offset(s_cnst, saturated_constraint_set_hookup));
42
43   var_list = &(sys->variable_set);
44   DEBUG1("Variable set : %d", xbt_swag_size(var_list));
45   xbt_swag_foreach(var, var_list) {
46     int nb=0;
47     var->value = 0.0;
48     for (i = 0; i < var->cnsts_number; i++) {
49       if(var->cnsts[i].value==0.0) nb++;
50     }
51     if((nb==var->cnsts_number) && (var->weight>0.0))
52       var->value = 1.0;
53   }
54
55   cnst_list = &(sys->active_constraint_set);
56   DEBUG1("Active constraints : %d", xbt_swag_size(cnst_list));
57   xbt_swag_foreach(cnst, cnst_list) {
58     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
59   }
60   cnst_list = &(sys->saturated_constraint_set);
61   xbt_swag_foreach(cnst, cnst_list) {
62     cnst->remaining = cnst->bound;
63     cnst->usage = 0.0;
64   }
65
66   DEBUG0("Fair bottleneck Initialized");
67
68   /* 
69    * Compute Usage and store the variables that reach the maximum.
70    */
71   while (1) {
72     DEBUG1("******* Constraints to process: %d *******", xbt_swag_size(cnst_list));
73     xbt_swag_foreach_safe(cnst, cnst_next, cnst_list) {
74       int nb = 0;
75       double max_elem = 0.0;
76       DEBUG1("Processing cnst %p ", cnst);
77       elem_list = &(cnst->element_set);
78       cnst->usage = 0.0;
79       xbt_swag_foreach(elem, elem_list) {
80         if (elem->variable->weight <= 0)
81           break;
82         if ((elem->value > 0)) {
83           nb++;
84           if (max_elem > 0)
85             max_elem =
86                 MAX(max_elem, elem->value / elem->variable->weight);
87           else
88             max_elem = elem->value / elem->variable->weight;
89         }
90         //      make_elem_active(elem);
91       }
92       DEBUG2("\tmax_elem : %g with %d variables",  max_elem,nb);
93       if(nb>0 && !cnst->shared)
94         nb = 1;
95       cnst->usage = max_elem * nb;
96       DEBUG3("\tConstraint Usage %p : %f with %d variables", cnst, cnst->usage,nb);
97       if(!nb) {
98         xbt_swag_remove(cnst, cnst_list);
99         continue;
100       }
101       /* Saturated constraints update */
102       if (min_usage < 0 || min_usage > cnst->remaining / cnst->usage) {
103         DEBUG3("Update min_usage (%g) with cnst %p -> %g",min_usage, cnst,
104                cnst->remaining / cnst->usage);
105
106         min_usage = cnst->remaining / cnst->usage;
107         while ((useless_cnst = xbt_swag_extract(&(cnst_to_update)))) {
108           xbt_swag_insert_at_head(useless_cnst, cnst_list);
109         }
110         xbt_swag_remove(cnst, cnst_list);
111         xbt_swag_insert(cnst, &(cnst_to_update));
112       } else if (min_usage == cnst->remaining / cnst->usage) {
113         DEBUG2("Keep   min_usage (%g) with cnst %p",min_usage, cnst);
114         xbt_swag_remove(cnst, cnst_list);
115         xbt_swag_insert(cnst, &(cnst_to_update));
116       }
117     }
118
119     if (!xbt_swag_size(&cnst_to_update))
120       break;
121
122     while ((cnst_next = xbt_swag_extract(&cnst_to_update))) {
123       int nb = 0;
124       elem_list = &(cnst_next->element_set);
125       xbt_swag_foreach(elem, elem_list) {
126         if (elem->variable->weight <= 0)
127           break;
128         if ((elem->value > 0))
129           nb++;
130       }
131       if(nb>0 && !(cnst_next->shared))
132         nb = 1;
133
134       DEBUG1("Updating for cnst %p",cnst_next);
135
136       xbt_swag_foreach(elem, elem_list) {
137         if (elem->value <= 0) continue;
138
139         var = elem->variable;
140
141         if (var->weight <= 0)
142           break;
143         if (var->value == 0.0) {
144           DEBUG2("\tUpdating var %p (%g)",var,var->value);
145           var->value = var->weight * cnst_next->remaining / (nb * elem->value);
146
147           /* Update usage */
148
149           for (i = 0; i < var->cnsts_number; i++) {
150             lmm_element_t elm = &var->cnsts[i];
151             cnst = elm->constraint;
152             DEBUG1("\t\tUpdating cnst %p",cnst);
153             double_update(&(cnst->remaining), elm->value * var->value);
154             double_update(&(cnst->usage), elm->value / var->weight);
155             //              make_elem_inactive(elm);
156           }
157         }
158       }
159     }
160   }
161
162   sys->modified = 0;
163   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
164     DEBUG0("Fair bottleneck done");
165     lmm_print(sys);
166   }
167 }