Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7cf24affb24b9acbbe9e817f0d1b626758532063
[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* _elem;
23   lmm_element_t elem   = nullptr;
24   xbt_swag_t elem_list = nullptr;
25
26   if (not sys->modified)
27     return;
28
29   XBT_DEBUG("Variable set : %zu", sys->variable_set.size());
30   for (s_lmm_variable_t& var : sys->variable_set) {
31     var.value = 0.0;
32     XBT_DEBUG("Handling variable %p", &var);
33     if (var.sharing_weight > 0.0 && std::find_if(begin(var.cnsts), end(var.cnsts), [](s_lmm_element_t const& x) {
34                                       return x.consumption_weight != 0.0;
35                                     }) != end(var.cnsts)) {
36       sys->saturated_variable_set.push_back(var);
37     } else {
38       XBT_DEBUG("Err, finally, there is no need to take care of variable %p", &var);
39       if (var.sharing_weight > 0.0)
40         var.value = 1.0;
41     }
42   }
43
44   XBT_DEBUG("Active constraints : %zu", sys->active_constraint_set.size());
45   for (s_lmm_constraint_t& cnst : sys->active_constraint_set) {
46     sys->saturated_constraint_set.push_back(cnst);
47   }
48   for (s_lmm_constraint_t& cnst : sys->saturated_constraint_set) {
49     cnst.remaining = cnst.bound;
50     cnst.usage     = 0.0;
51   }
52
53   XBT_DEBUG("Fair bottleneck Initialized");
54
55   /*
56    * Compute Usage and store the variables that reach the maximum.
57    */
58   auto& var_list  = sys->saturated_variable_set;
59   auto& cnst_list = sys->saturated_constraint_set;
60   do {
61     if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
62       XBT_DEBUG("Fair bottleneck done");
63       sys->print();
64     }
65     XBT_DEBUG("******* Constraints to process: %zu *******", cnst_list.size());
66     for (auto iter = std::begin(cnst_list); iter != std::end(cnst_list);) {
67       s_lmm_constraint_t& cnst = *iter;
68       int nb = 0;
69       XBT_DEBUG("Processing cnst %p ", &cnst);
70       elem_list  = &cnst.enabled_element_set;
71       cnst.usage = 0.0;
72       xbt_swag_foreach(_elem, elem_list)
73       {
74         elem = static_cast<lmm_element_t>(_elem);
75         xbt_assert(elem->variable->sharing_weight > 0);
76         if (elem->consumption_weight > 0 && elem->variable->saturated_variable_set_hook.is_linked())
77           nb++;
78       }
79       XBT_DEBUG("\tThere are %d variables", nb);
80       if (nb > 0 && not cnst.sharing_policy)
81         nb = 1;
82       if (nb == 0) {
83         cnst.remaining = 0.0;
84         cnst.usage     = 0.0;
85         iter           = cnst_list.erase(iter);
86       } else {
87         cnst.usage = cnst.remaining / nb;
88         XBT_DEBUG("\tConstraint Usage %p : %f with %d variables", &cnst, cnst.usage, nb);
89         iter++;
90       }
91     }
92
93     for (auto iter = std::begin(var_list); iter != std::end(var_list);) {
94       s_lmm_variable_t& var = *iter;
95       double min_inc = DBL_MAX;
96       for (s_lmm_element_t const& elm : var.cnsts) {
97         if (elm.consumption_weight > 0)
98           min_inc = std::min(min_inc, elm.constraint->usage / elm.consumption_weight);
99       }
100       if (var.bound > 0)
101         min_inc = std::min(min_inc, var.bound - var.value);
102       var.mu    = min_inc;
103       XBT_DEBUG("Updating variable %p maximum increment: %g", &var, var.mu);
104       var.value += var.mu;
105       if (var.value == var.bound)
106         iter = var_list.erase(iter);
107       else
108         iter++;
109     }
110
111     for (auto iter = std::begin(cnst_list); iter != std::end(cnst_list);) {
112       s_lmm_constraint_t& cnst = *iter;
113       XBT_DEBUG("Updating cnst %p ", &cnst);
114       elem_list = &cnst.enabled_element_set;
115       xbt_swag_foreach(_elem, elem_list)
116       {
117         elem = static_cast<lmm_element_t>(_elem);
118         xbt_assert(elem->variable->sharing_weight > 0);
119         if (cnst.sharing_policy) {
120           XBT_DEBUG("\tUpdate constraint %p (%g) with variable %p by %g", &cnst, cnst.remaining, elem->variable,
121                     elem->variable->mu);
122           double_update(&cnst.remaining, elem->consumption_weight * elem->variable->mu, sg_maxmin_precision);
123         } else {
124           XBT_DEBUG("\tNon-Shared variable. Update constraint usage of %p (%g) with variable %p by %g", &cnst,
125                     cnst.usage, elem->variable, elem->variable->mu);
126           cnst.usage = std::min(cnst.usage, elem->consumption_weight * elem->variable->mu);
127         }
128       }
129       if (not cnst.sharing_policy) {
130         XBT_DEBUG("\tUpdate constraint %p (%g) by %g", &cnst, cnst.remaining, cnst.usage);
131
132         double_update(&cnst.remaining, cnst.usage, sg_maxmin_precision);
133       }
134
135       XBT_DEBUG("\tRemaining for %p : %g", &cnst, cnst.remaining);
136       if (cnst.remaining <= 0.0) {
137         XBT_DEBUG("\tGet rid of constraint %p", &cnst);
138
139         iter = cnst_list.erase(iter);
140         xbt_swag_foreach(_elem, elem_list)
141         {
142           elem = static_cast<lmm_element_t>(_elem);
143           if (elem->variable->sharing_weight <= 0)
144             break;
145           if (elem->consumption_weight > 0 && elem->variable->saturated_variable_set_hook.is_linked()) {
146             XBT_DEBUG("\t\tGet rid of variable %p", elem->variable);
147             var_list.erase(var_list.iterator_to(*elem->variable));
148           }
149         }
150       } else {
151         iter++;
152       }
153     }
154   } while (not var_list.empty());
155
156   cnst_list.clear();
157   sys->modified = true;
158   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
159     XBT_DEBUG("Fair bottleneck done");
160     sys->print();
161   }
162 }