Logo AND Algorithmique Numérique Distribuée

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