Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / kernel / lmm / fair_bottleneck.cpp
1 /* Copyright (c) 2007-2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/kernel/lmm/maxmin.hpp"
7 #include "xbt/log.h"
8 #include "xbt/sysdep.h"
9 #include <algorithm>
10 #include <cfloat>
11 #include <cmath>
12 #include <cstdlib>
13 #include <xbt/utility.hpp>
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 simgrid::kernel::lmm::System* simgrid::kernel::lmm::make_new_fair_bottleneck_system(bool selective_update)
21 {
22   return new simgrid::kernel::lmm::FairBottleneck(selective_update);
23 }
24
25 void simgrid::kernel::lmm::FairBottleneck::bottleneck_solve()
26 {
27   if (not modified)
28     return;
29
30   XBT_DEBUG("Variable set : %zu", variable_set.size());
31   for (Variable& var : variable_set) {
32     var.value = 0.0;
33     XBT_DEBUG("Handling variable %p", &var);
34     if (var.sharing_weight > 0.0 && std::find_if(begin(var.cnsts), end(var.cnsts), [](Element const& x) {
35                                       return x.consumption_weight != 0.0;
36                                     }) != end(var.cnsts)) {
37       saturated_variable_set.push_back(var);
38     } else {
39       XBT_DEBUG("Err, finally, there is no need to take care of variable %p", &var);
40       if (var.sharing_weight > 0.0)
41         var.value = 1.0;
42     }
43   }
44
45   XBT_DEBUG("Active constraints : %zu", active_constraint_set.size());
46   for (Constraint& cnst : active_constraint_set) {
47     saturated_constraint_set.push_back(cnst);
48   }
49   for (Constraint& cnst : saturated_constraint_set) {
50     cnst.remaining = cnst.bound;
51     cnst.usage     = 0.0;
52   }
53
54   XBT_DEBUG("Fair bottleneck Initialized");
55
56   /*
57    * Compute Usage and store the variables that reach the maximum.
58    */
59   auto& var_list  = saturated_variable_set;
60   auto& cnst_list = saturated_constraint_set;
61   do {
62     if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
63       XBT_DEBUG("Fair bottleneck done");
64       print();
65     }
66     XBT_DEBUG("******* Constraints to process: %zu *******", cnst_list.size());
67     for (auto iter = std::begin(cnst_list); iter != std::end(cnst_list);) {
68       Constraint& cnst = *iter;
69       int nb = 0;
70       XBT_DEBUG("Processing cnst %p ", &cnst);
71       cnst.usage = 0.0;
72       for (Element& elem : cnst.enabled_element_set) {
73         xbt_assert(elem.variable->sharing_weight > 0);
74         if (elem.consumption_weight > 0 && elem.variable->saturated_variable_set_hook.is_linked())
75           nb++;
76       }
77       XBT_DEBUG("\tThere are %d variables", nb);
78       if (nb > 0 && not cnst.sharing_policy)
79         nb = 1;
80       if (nb == 0) {
81         cnst.remaining = 0.0;
82         cnst.usage     = 0.0;
83         iter           = cnst_list.erase(iter);
84       } else {
85         cnst.usage = cnst.remaining / nb;
86         XBT_DEBUG("\tConstraint Usage %p : %f with %d variables", &cnst, cnst.usage, nb);
87         iter++;
88       }
89     }
90
91     for (auto iter = std::begin(var_list); iter != std::end(var_list);) {
92       Variable& var  = *iter;
93       double min_inc = DBL_MAX;
94       for (Element const& elm : var.cnsts) {
95         if (elm.consumption_weight > 0)
96           min_inc = std::min(min_inc, elm.constraint->usage / elm.consumption_weight);
97       }
98       if (var.bound > 0)
99         min_inc = std::min(min_inc, var.bound - var.value);
100       var.mu    = min_inc;
101       XBT_DEBUG("Updating variable %p maximum increment: %g", &var, var.mu);
102       var.value += var.mu;
103       if (var.value == var.bound)
104         iter = var_list.erase(iter);
105       else
106         iter++;
107     }
108
109     for (auto iter = std::begin(cnst_list); iter != std::end(cnst_list);) {
110       Constraint& cnst = *iter;
111       XBT_DEBUG("Updating cnst %p ", &cnst);
112       if (cnst.sharing_policy) {
113         for (Element& elem : cnst.enabled_element_set) {
114           xbt_assert(elem.variable->sharing_weight > 0);
115           XBT_DEBUG("\tUpdate constraint %p (%g) with variable %p by %g", &cnst, cnst.remaining, elem.variable,
116                     elem.variable->mu);
117           double_update(&cnst.remaining, elem.consumption_weight * elem.variable->mu, sg_maxmin_precision);
118         }
119       } else {
120         for (Element& elem : cnst.enabled_element_set) {
121           xbt_assert(elem.variable->sharing_weight > 0);
122           XBT_DEBUG("\tNon-Shared variable. Update constraint usage of %p (%g) with variable %p by %g", &cnst,
123                     cnst.usage, elem.variable, elem.variable->mu);
124           cnst.usage = std::min(cnst.usage, elem.consumption_weight * elem.variable->mu);
125         }
126         XBT_DEBUG("\tUpdate constraint %p (%g) by %g", &cnst, cnst.remaining, cnst.usage);
127         double_update(&cnst.remaining, cnst.usage, sg_maxmin_precision);
128       }
129
130       XBT_DEBUG("\tRemaining for %p : %g", &cnst, cnst.remaining);
131       if (cnst.remaining <= 0.0) {
132         XBT_DEBUG("\tGet rid of constraint %p", &cnst);
133
134         iter = cnst_list.erase(iter);
135         for (Element& elem : cnst.enabled_element_set) {
136           if (elem.variable->sharing_weight <= 0)
137             break;
138           if (elem.consumption_weight > 0 && elem.variable->saturated_variable_set_hook.is_linked()) {
139             XBT_DEBUG("\t\tGet rid of variable %p", elem.variable);
140             simgrid::xbt::intrusive_erase(var_list, *elem.variable);
141           }
142         }
143       } else {
144         iter++;
145       }
146     }
147   } while (not var_list.empty());
148
149   cnst_list.clear();
150   modified = true;
151   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
152     XBT_DEBUG("Fair bottleneck done");
153     print();
154   }
155 }