Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branches 'master' and 'master' of github.com:simgrid/simgrid
[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 "maxmin_private.hpp"
8 #include "xbt/log.h"
9 #include "xbt/sysdep.h"
10 #include <float.h>
11 #include <math.h>
12 #include <stdlib.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;
22   void *_var_next;
23   void *_cnst;
24   void *_cnst_next;
25   void *_elem;
26   lmm_variable_t var = nullptr;
27   lmm_constraint_t cnst = nullptr;
28   s_lmm_constraint_t s_cnst;
29   lmm_element_t elem = nullptr;
30   xbt_swag_t cnst_list = nullptr;
31   xbt_swag_t var_list = nullptr;
32   xbt_swag_t elem_list = nullptr;
33
34   static s_xbt_swag_t cnst_to_update;
35
36   if (not sys->modified)
37     return;
38
39   /* Init */
40   xbt_swag_init(&(cnst_to_update), xbt_swag_offset(s_cnst, saturated_constraint_set_hookup));
41
42   var_list = &(sys->variable_set);
43   XBT_DEBUG("Variable set : %d", xbt_swag_size(var_list));
44   xbt_swag_foreach(_var, var_list) {
45     var = static_cast<lmm_variable_t>(_var);
46     int nb = 0;
47     var->value = 0.0;
48     XBT_DEBUG("Handling variable %p", var);
49     xbt_swag_insert(var, &(sys->saturated_variable_set));
50     for (int i = 0; i < var->cnsts_number; i++) {
51       if (var->cnsts[i].value == 0.0)
52         nb++;
53     }
54     if ((nb == var->cnsts_number) && (var->weight > 0.0)) {
55       XBT_DEBUG("Err, finally, there is no need to take care of variable %p", var);
56       xbt_swag_remove(var, &(sys->saturated_variable_set));
57       var->value = 1.0;
58     }
59     if (var->weight <= 0.0) {
60       XBT_DEBUG("Err, finally, there is no need to take care of variable %p", 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 = static_cast<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 = static_cast<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 *******", xbt_swag_size(cnst_list));
90     xbt_swag_foreach_safe(_cnst, _cnst_next, cnst_list) {
91       cnst = static_cast<lmm_constraint_t>(_cnst);
92       int nb = 0;
93       XBT_DEBUG("Processing cnst %p ", cnst);
94       elem_list = &(cnst->enabled_element_set);
95       cnst->usage = 0.0;
96       xbt_swag_foreach(_elem, elem_list) {
97         elem = static_cast<lmm_element_t>(_elem);
98         xbt_assert(elem->variable->weight > 0);
99         if ((elem->value > 0) && xbt_swag_belongs(elem->variable, var_list))
100           nb++;
101       }
102       XBT_DEBUG("\tThere are %d variables", nb);
103       if (nb > 0 && not cnst->sharing_policy)
104         nb = 1;
105       if (not 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       XBT_DEBUG("\tConstraint Usage %p : %f with %d variables", cnst, cnst->usage, nb);
113     }
114
115     xbt_swag_foreach_safe(_var, _var_next, var_list) {
116       var = static_cast<lmm_variable_t>(_var);
117       double min_inc = DBL_MAX;
118       for (int i = 0; i < var->cnsts_number; i++) {
119         lmm_element_t elm = &var->cnsts[i];
120         if (elm->value > 0)
121           min_inc = MIN(min_inc, elm->constraint->usage / elm->value);
122       }
123       if (var->bound > 0)
124         min_inc = MIN(min_inc, var->bound - var->value);
125       var->mu = min_inc;
126       XBT_DEBUG("Updating variable %p maximum increment: %g", var, var->mu);
127       var->value += var->mu;
128       if (var->value == var->bound) {
129         xbt_swag_remove(var, var_list);
130       }
131     }
132
133     xbt_swag_foreach_safe(_cnst, _cnst_next, cnst_list) {
134       cnst = static_cast<lmm_constraint_t>(_cnst);
135       XBT_DEBUG("Updating cnst %p ", cnst);
136       elem_list = &(cnst->enabled_element_set);
137       xbt_swag_foreach(_elem, elem_list) {
138         elem = static_cast<lmm_element_t>(_elem);
139         xbt_assert(elem->variable->weight > 0);
140         if (cnst->sharing_policy) {
141           XBT_DEBUG("\tUpdate constraint %p (%g) with variable %p by %g", cnst, cnst->remaining, elem->variable,
142                  elem->variable->mu);
143           double_update(&(cnst->remaining), elem->value * elem->variable->mu, sg_maxmin_precision);
144         } else {
145           XBT_DEBUG("\tNon-Shared variable. Update constraint usage of %p (%g) with variable %p by %g",
146               cnst, cnst->usage, elem->variable, elem->variable->mu);
147           cnst->usage = MIN(cnst->usage, elem->value * elem->variable->mu);
148         }
149       }
150       if (not cnst->sharing_policy) {
151         XBT_DEBUG("\tUpdate constraint %p (%g) by %g", cnst, cnst->remaining, cnst->usage);
152
153         double_update(&(cnst->remaining), cnst->usage, sg_maxmin_precision);
154       }
155
156       XBT_DEBUG("\tRemaining for %p : %g", cnst, cnst->remaining);
157       if (cnst->remaining <= 0.0) {
158         XBT_DEBUG("\tGet rid of constraint %p", cnst);
159
160         xbt_swag_remove(cnst, cnst_list);
161         xbt_swag_foreach(_elem, elem_list) {
162           elem = static_cast<lmm_element_t>(_elem);
163           if (elem->variable->weight <= 0)
164             break;
165           if (elem->value > 0) {
166             XBT_DEBUG("\t\tGet rid of variable %p", elem->variable);
167             xbt_swag_remove(elem->variable, var_list);
168           }
169         }
170       }
171     }
172   } while (xbt_swag_size(var_list));
173
174   xbt_swag_reset(cnst_list);
175   sys->modified = 0;
176   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
177     XBT_DEBUG("Fair bottleneck done");
178     lmm_print(sys);
179   }
180 }