Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of github.com:simgrid/simgrid into dev_10
[simgrid.git] / src / kernel / lmm / fair_bottleneck.cpp
1 /* Copyright (c) 2007-2020. 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 "src/surf/surf_interface.hpp"
8 #include "xbt/sysdep.h"
9
10 #include <algorithm>
11 #include <cfloat>
12 #include <cmath>
13 #include <cstdlib>
14 #include <xbt/utility.hpp>
15
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_maxmin);
17
18 void simgrid::kernel::lmm::FairBottleneck::bottleneck_solve()
19 {
20   if (not modified_)
21     return;
22
23   XBT_DEBUG("Variable set : %zu", variable_set.size());
24   for (Variable& var : variable_set) {
25     var.value_ = 0.0;
26     XBT_DEBUG("Handling variable %p", &var);
27     if (var.sharing_penalty_ > 0.0 && std::find_if(begin(var.cnsts_), end(var.cnsts_), [](Element const& x) {
28                                         return x.consumption_weight != 0.0;
29                                       }) != end(var.cnsts_)) {
30       saturated_variable_set.push_back(var);
31     } else {
32       XBT_DEBUG("Err, finally, there is no need to take care of variable %p", &var);
33       if (var.sharing_penalty_ > 0.0)
34         var.value_ = 1.0;
35     }
36   }
37
38   XBT_DEBUG("Active constraints : %zu", active_constraint_set.size());
39   for (Constraint& cnst : active_constraint_set) {
40     saturated_constraint_set.push_back(cnst);
41   }
42   for (Constraint& cnst : saturated_constraint_set) {
43     cnst.remaining_ = cnst.bound_;
44     cnst.usage_     = 0.0;
45   }
46
47   XBT_DEBUG("Fair bottleneck Initialized");
48
49   /*
50    * Compute Usage and store the variables that reach the maximum.
51    */
52   auto& var_list  = saturated_variable_set;
53   auto& cnst_list = saturated_constraint_set;
54   do {
55     if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
56       XBT_DEBUG("Fair bottleneck done");
57       print();
58     }
59     XBT_DEBUG("******* Constraints to process: %zu *******", cnst_list.size());
60     for (auto iter = std::begin(cnst_list); iter != std::end(cnst_list);) {
61       Constraint& cnst = *iter;
62       int nb = 0;
63       XBT_DEBUG("Processing cnst %p ", &cnst);
64       cnst.usage_ = 0.0;
65       for (const Element& elem : cnst.enabled_element_set_) {
66         xbt_assert(elem.variable->sharing_penalty_ > 0);
67         if (elem.consumption_weight > 0 && elem.variable->saturated_variable_set_hook_.is_linked())
68           nb++;
69       }
70       XBT_DEBUG("\tThere are %d variables", nb);
71       if (nb > 0 && cnst.sharing_policy_ == s4u::Link::SharingPolicy::FATPIPE)
72         nb = 1;
73       if (nb == 0) {
74         cnst.remaining_ = 0.0;
75         cnst.usage_     = 0.0;
76         iter           = cnst_list.erase(iter);
77       } else {
78         cnst.usage_ = cnst.remaining_ / nb;
79         XBT_DEBUG("\tConstraint Usage %p : %f with %d variables", &cnst, cnst.usage_, nb);
80         iter++;
81       }
82     }
83
84     for (auto iter = std::begin(var_list); iter != std::end(var_list);) {
85       Variable& var  = *iter;
86       double min_inc = DBL_MAX;
87       for (Element const& elm : var.cnsts_) {
88         if (elm.consumption_weight > 0)
89           min_inc = std::min(min_inc, elm.constraint->usage_ / elm.consumption_weight);
90       }
91       if (var.bound_ > 0)
92         min_inc = std::min(min_inc, var.bound_ - var.value_);
93       var.mu_ = min_inc;
94       XBT_DEBUG("Updating variable %p maximum increment: %g", &var, var.mu_);
95       var.value_ += var.mu_;
96       if (var.value_ == var.bound_)
97         iter = var_list.erase(iter);
98       else
99         iter++;
100     }
101
102     for (auto iter = std::begin(cnst_list); iter != std::end(cnst_list);) {
103       Constraint& cnst = *iter;
104       XBT_DEBUG("Updating cnst %p ", &cnst);
105       if (cnst.sharing_policy_ != s4u::Link::SharingPolicy::FATPIPE) {
106         for (const Element& elem : cnst.enabled_element_set_) {
107           xbt_assert(elem.variable->sharing_penalty_ > 0);
108           XBT_DEBUG("\tUpdate constraint %p (%g) with variable %p by %g", &cnst, cnst.remaining_, elem.variable,
109                     elem.variable->mu_);
110           double_update(&cnst.remaining_, elem.consumption_weight * elem.variable->mu_, sg_maxmin_precision);
111         }
112       } else {
113         for (const Element& elem : cnst.enabled_element_set_) {
114           xbt_assert(elem.variable->sharing_penalty_ > 0);
115           XBT_DEBUG("\tNon-Shared variable. Update constraint usage of %p (%g) with variable %p by %g", &cnst,
116                     cnst.usage_, elem.variable, elem.variable->mu_);
117           cnst.usage_ = std::min(cnst.usage_, elem.consumption_weight * elem.variable->mu_);
118         }
119         XBT_DEBUG("\tUpdate constraint %p (%g) by %g", &cnst, cnst.remaining_, cnst.usage_);
120         double_update(&cnst.remaining_, cnst.usage_, sg_maxmin_precision);
121       }
122
123       XBT_DEBUG("\tRemaining for %p : %g", &cnst, cnst.remaining_);
124       if (cnst.remaining_ <= 0.0) {
125         XBT_DEBUG("\tGet rid of constraint %p", &cnst);
126
127         iter = cnst_list.erase(iter);
128         for (const Element& elem : cnst.enabled_element_set_) {
129           if (elem.variable->sharing_penalty_ <= 0)
130             break;
131           if (elem.consumption_weight > 0 && elem.variable->saturated_variable_set_hook_.is_linked()) {
132             XBT_DEBUG("\t\tGet rid of variable %p", elem.variable);
133             simgrid::xbt::intrusive_erase(var_list, *elem.variable);
134           }
135         }
136       } else {
137         iter++;
138       }
139     }
140   } while (not var_list.empty());
141
142   cnst_list.clear();
143   modified_ = true;
144   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
145     XBT_DEBUG("Fair bottleneck done");
146     print();
147   }
148 }