Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
smells and bugs
[simgrid.git] / src / surf / fair_bottleneck.cpp
1 /* Copyright (c) 2007-2011, 2013-2014. 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 #include "xbt/sysdep.h"
8 #include "xbt/log.h"
9 #include "maxmin_private.hpp"
10 #include <stdlib.h>
11 #include <math.h>
12
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_maxmin);
14 #define SHOW_EXPR_G(expr) XBT_DEBUG(#expr " = %g",expr);
15 #define SHOW_EXPR_D(expr) XBT_DEBUG(#expr " = %d",expr);
16 #define SHOW_EXPR_P(expr) XBT_DEBUG(#expr " = %p",expr);
17
18 void bottleneck_solve(lmm_system_t sys)
19 {
20   void *_var;
21   void *_var_next;
22   void *_cnst;
23   void *_cnst_next;
24   void *_elem;
25   lmm_variable_t var = nullptr;
26   lmm_constraint_t cnst = nullptr;
27   s_lmm_constraint_t s_cnst;
28   lmm_element_t elem = nullptr;
29   xbt_swag_t cnst_list = nullptr;
30   xbt_swag_t var_list = nullptr;
31   xbt_swag_t elem_list = nullptr;
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), xbt_swag_offset(s_cnst, saturated_constraint_set_hookup));
40
41   var_list = &(sys->variable_set);
42   XBT_DEBUG("Variable set : %d", xbt_swag_size(var_list));
43   xbt_swag_foreach(_var, var_list) {
44     var = static_cast<lmm_variable_t>(_var);
45     int nb = 0;
46     var->value = 0.0;
47     XBT_DEBUG("Handling variable %p", var);
48     xbt_swag_insert(var, &(sys->saturated_variable_set));
49     for (int 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       XBT_DEBUG("Err, finally, there is no need to take care of variable %p", 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", var);
60       xbt_swag_remove(var, &(sys->saturated_variable_set));
61     }
62   }
63   var_list = &(sys->saturated_variable_set);
64
65   cnst_list = &(sys->active_constraint_set);
66   XBT_DEBUG("Active constraints : %d", xbt_swag_size(cnst_list));
67   xbt_swag_foreach(_cnst, cnst_list) {
68     cnst = static_cast<lmm_constraint_t>(_cnst);
69     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
70   }
71   cnst_list = &(sys->saturated_constraint_set);
72   xbt_swag_foreach(_cnst, cnst_list) {
73     cnst = static_cast<lmm_constraint_t>(_cnst);
74     cnst->remaining = cnst->bound;
75     cnst->usage = 0.0;
76   }
77
78   XBT_DEBUG("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       XBT_DEBUG("Fair bottleneck done");
86       lmm_print(sys);
87     }
88     XBT_DEBUG("******* Constraints to process: %d *******", xbt_swag_size(cnst_list));
89     xbt_swag_foreach_safe(_cnst, _cnst_next, cnst_list) {
90       cnst = static_cast<lmm_constraint_t>(_cnst);
91       int nb = 0;
92       XBT_DEBUG("Processing cnst %p ", cnst);
93       elem_list = &(cnst->enabled_element_set);
94       cnst->usage = 0.0;
95       xbt_swag_foreach(_elem, elem_list) {
96         elem = static_cast<lmm_element_t>(_elem);
97         xbt_assert(elem->variable->weight > 0);
98         if ((elem->value > 0) && xbt_swag_belongs(elem->variable, var_list))
99           nb++;
100       }
101       XBT_DEBUG("\tThere are %d variables", nb);
102       if (nb > 0 && !cnst->sharing_policy)
103         nb = 1;
104       if (!nb) {
105         cnst->remaining = 0.0;
106         cnst->usage = cnst->remaining;
107         xbt_swag_remove(cnst, cnst_list);
108         continue;
109       }
110       cnst->usage = cnst->remaining / nb;
111       XBT_DEBUG("\tConstraint Usage %p : %f with %d variables", cnst, cnst->usage, nb);
112     }
113
114     xbt_swag_foreach_safe(_var, _var_next, var_list) {
115       var = static_cast<lmm_variable_t>(_var);
116       double min_inc =  var->cnsts[0].constraint->usage / var->cnsts[0].value;
117       for (int i = 1; i < var->cnsts_number; i++) {
118         lmm_element_t elm = &var->cnsts[i];
119         min_inc = MIN(min_inc, elm->constraint->usage / elm->value);
120       }
121       if (var->bound > 0)
122         min_inc = MIN(min_inc, var->bound - var->value);
123       var->mu = min_inc;
124       XBT_DEBUG("Updating variable %p maximum increment: %g", var, var->mu);
125       var->value += var->mu;
126       if (var->value == var->bound) {
127         xbt_swag_remove(var, var_list);
128       }
129     }
130
131     xbt_swag_foreach_safe(_cnst, _cnst_next, cnst_list) {
132       cnst = static_cast<lmm_constraint_t>(_cnst);
133       XBT_DEBUG("Updating cnst %p ", cnst);
134       elem_list = &(cnst->enabled_element_set);
135       xbt_swag_foreach(_elem, elem_list) {
136         elem = static_cast<lmm_element_t>(_elem);
137         xbt_assert(elem->variable->weight > 0);
138         if (cnst->sharing_policy) {
139           XBT_DEBUG("\tUpdate constraint %p (%g) with variable %p by %g", cnst, cnst->remaining, elem->variable,
140                  elem->variable->mu);
141           double_update(&(cnst->remaining), elem->value * elem->variable->mu, sg_maxmin_precision);
142         } else {
143           XBT_DEBUG("\tNon-Shared variable. Update constraint usage of %p (%g) with variable %p by %g",
144               cnst, cnst->usage, elem->variable, elem->variable->mu);
145           cnst->usage = MIN(cnst->usage, elem->value * elem->variable->mu);
146         }
147       }
148       if (!cnst->sharing_policy) {
149         XBT_DEBUG("\tUpdate constraint %p (%g) by %g", cnst, cnst->remaining, cnst->usage);
150
151         double_update(&(cnst->remaining), cnst->usage, sg_maxmin_precision);
152       }
153
154       XBT_DEBUG("\tRemaining for %p : %g", cnst, cnst->remaining);
155       if (cnst->remaining <= 0.0) {
156         XBT_DEBUG("\tGet rid of constraint %p", cnst);
157
158         xbt_swag_remove(cnst, cnst_list);
159         xbt_swag_foreach(_elem, elem_list) {
160           elem = static_cast<lmm_element_t>(_elem);
161           if (elem->variable->weight <= 0)
162             break;
163           if (elem->value > 0) {
164             XBT_DEBUG("\t\tGet rid of variable %p", elem->variable);
165             xbt_swag_remove(elem->variable, var_list);
166           }
167         }
168       }
169     }
170   } while (xbt_swag_size(var_list));
171
172   xbt_swag_reset(cnst_list);
173   sys->modified = 0;
174   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
175     XBT_DEBUG("Fair bottleneck done");
176     lmm_print(sys);
177   }
178 }