Logo AND Algorithmique Numérique Distribuée

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