Logo AND Algorithmique Numérique Distribuée

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