Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Constification in maxmin.
[simgrid.git] / src / kernel / lmm / fair_bottleneck.cpp
1 /* Copyright (c) 2007-2011, 2013-2017. 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 "src/kernel/lmm/maxmin.hpp"
8 #include "xbt/log.h"
9 #include "xbt/sysdep.h"
10 #include <algorithm>
11 #include <cfloat>
12 #include <cmath>
13 #include <cstdlib>
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_maxmin);
16 #define SHOW_EXPR_G(expr) XBT_DEBUG(#expr " = %g", expr);
17 #define SHOW_EXPR_D(expr) XBT_DEBUG(#expr " = %d", expr);
18 #define SHOW_EXPR_P(expr) XBT_DEBUG(#expr " = %p", expr);
19
20 void simgrid::kernel::lmm::bottleneck_solve(lmm_system_t sys)
21 {
22   void* _var;
23   void* _var_next;
24   void* _cnst;
25   void* _cnst_next;
26   void* _elem;
27   lmm_variable_t var    = nullptr;
28   lmm_constraint_t cnst = nullptr;
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   if (not sys->modified)
35     return;
36
37   var_list = &(sys->variable_set);
38   XBT_DEBUG("Variable set : %d", xbt_swag_size(var_list));
39   xbt_swag_foreach(_var, var_list)
40   {
41     var        = static_cast<lmm_variable_t>(_var);
42     var->value = 0.0;
43     XBT_DEBUG("Handling variable %p", var);
44     if (var->sharing_weight > 0.0 && std::find_if(begin(var->cnsts), end(var->cnsts), [](s_lmm_element_t const& x) {
45                                        return x.consumption_weight != 0.0;
46                                      }) != end(var->cnsts)) {
47       xbt_swag_insert(var, &(sys->saturated_variable_set));
48     } else {
49       XBT_DEBUG("Err, finally, there is no need to take care of variable %p", var);
50       if (var->sharing_weight > 0.0)
51         var->value = 1.0;
52     }
53   }
54   var_list = &(sys->saturated_variable_set);
55
56   cnst_list = &(sys->active_constraint_set);
57   XBT_DEBUG("Active constraints : %d", xbt_swag_size(cnst_list));
58   xbt_swag_foreach(_cnst, cnst_list)
59   {
60     cnst = static_cast<lmm_constraint_t>(_cnst);
61     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
62   }
63   cnst_list = &(sys->saturated_constraint_set);
64   xbt_swag_foreach(_cnst, cnst_list)
65   {
66     cnst            = static_cast<lmm_constraint_t>(_cnst);
67     cnst->remaining = cnst->bound;
68     cnst->usage     = 0.0;
69   }
70
71   XBT_DEBUG("Fair bottleneck Initialized");
72
73   /*
74    * Compute Usage and store the variables that reach the maximum.
75    */
76   do {
77     if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
78       XBT_DEBUG("Fair bottleneck done");
79       sys->print();
80     }
81     XBT_DEBUG("******* Constraints to process: %d *******", xbt_swag_size(cnst_list));
82     xbt_swag_foreach_safe(_cnst, _cnst_next, cnst_list)
83     {
84       cnst   = static_cast<lmm_constraint_t>(_cnst);
85       int nb = 0;
86       XBT_DEBUG("Processing cnst %p ", cnst);
87       elem_list   = &(cnst->enabled_element_set);
88       cnst->usage = 0.0;
89       xbt_swag_foreach(_elem, elem_list)
90       {
91         elem = static_cast<lmm_element_t>(_elem);
92         xbt_assert(elem->variable->sharing_weight > 0);
93         if ((elem->consumption_weight > 0) && xbt_swag_belongs(elem->variable, var_list))
94           nb++;
95       }
96       XBT_DEBUG("\tThere are %d variables", nb);
97       if (nb > 0 && not cnst->sharing_policy)
98         nb = 1;
99       if (not nb) {
100         cnst->remaining = 0.0;
101         cnst->usage     = cnst->remaining;
102         xbt_swag_remove(cnst, cnst_list);
103         continue;
104       }
105       cnst->usage = cnst->remaining / nb;
106       XBT_DEBUG("\tConstraint Usage %p : %f with %d variables", cnst, cnst->usage, nb);
107     }
108
109     xbt_swag_foreach_safe(_var, _var_next, var_list)
110     {
111       var            = static_cast<lmm_variable_t>(_var);
112       double min_inc = DBL_MAX;
113       for (s_lmm_element_t const& elm : var->cnsts) {
114         if (elm.consumption_weight > 0)
115           min_inc = std::min(min_inc, elm.constraint->usage / elm.consumption_weight);
116       }
117       if (var->bound > 0)
118         min_inc = std::min(min_inc, var->bound - var->value);
119       var->mu   = min_inc;
120       XBT_DEBUG("Updating variable %p maximum increment: %g", var, var->mu);
121       var->value += var->mu;
122       if (var->value == var->bound) {
123         xbt_swag_remove(var, var_list);
124       }
125     }
126
127     xbt_swag_foreach_safe(_cnst, _cnst_next, cnst_list)
128     {
129       cnst = static_cast<lmm_constraint_t>(_cnst);
130       XBT_DEBUG("Updating cnst %p ", cnst);
131       elem_list = &(cnst->enabled_element_set);
132       xbt_swag_foreach(_elem, elem_list)
133       {
134         elem = static_cast<lmm_element_t>(_elem);
135         xbt_assert(elem->variable->sharing_weight > 0);
136         if (cnst->sharing_policy) {
137           XBT_DEBUG("\tUpdate constraint %p (%g) with variable %p by %g", cnst, cnst->remaining, elem->variable,
138                     elem->variable->mu);
139           double_update(&(cnst->remaining), elem->consumption_weight * elem->variable->mu, sg_maxmin_precision);
140         } else {
141           XBT_DEBUG("\tNon-Shared variable. Update constraint usage of %p (%g) with variable %p by %g", cnst,
142                     cnst->usage, elem->variable, elem->variable->mu);
143           cnst->usage = std::min(cnst->usage, elem->consumption_weight * elem->variable->mu);
144         }
145       }
146       if (not cnst->sharing_policy) {
147         XBT_DEBUG("\tUpdate constraint %p (%g) by %g", cnst, cnst->remaining, cnst->usage);
148
149         double_update(&(cnst->remaining), cnst->usage, sg_maxmin_precision);
150       }
151
152       XBT_DEBUG("\tRemaining for %p : %g", cnst, cnst->remaining);
153       if (cnst->remaining <= 0.0) {
154         XBT_DEBUG("\tGet rid of constraint %p", cnst);
155
156         xbt_swag_remove(cnst, cnst_list);
157         xbt_swag_foreach(_elem, elem_list)
158         {
159           elem = static_cast<lmm_element_t>(_elem);
160           if (elem->variable->sharing_weight <= 0)
161             break;
162           if (elem->consumption_weight > 0) {
163             XBT_DEBUG("\t\tGet rid of variable %p", elem->variable);
164             xbt_swag_remove(elem->variable, var_list);
165           }
166         }
167       }
168     }
169   } while (xbt_swag_size(var_list));
170
171   xbt_swag_reset(cnst_list);
172   sys->modified = true;
173   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
174     XBT_DEBUG("Fair bottleneck done");
175     sys->print();
176   }
177 }