1 /* Copyright (c) 2007-2011, 2013-2017. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "src/kernel/lmm/maxmin.hpp"
9 #include "xbt/sysdep.h"
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);
20 void simgrid::kernel::lmm::bottleneck_solve(lmm_system_t sys)
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;
34 if (not sys->modified)
37 var_list = &(sys->variable_set);
38 XBT_DEBUG("Variable set : %d", xbt_swag_size(var_list));
39 xbt_swag_foreach(_var, var_list)
41 var = static_cast<lmm_variable_t>(_var);
43 XBT_DEBUG("Handling variable %p", var);
44 xbt_swag_insert(var, &(sys->saturated_variable_set));
45 auto weighted = std::find_if(begin(var->cnsts), end(var->cnsts),
46 [](s_lmm_element_t const& x) { return x.consumption_weight != 0.0; });
47 if (weighted == end(var->cnsts) && var->sharing_weight > 0.0) {
48 XBT_DEBUG("Err, finally, there is no need to take care of variable %p", var);
49 xbt_swag_remove(var, &(sys->saturated_variable_set));
52 if (var->sharing_weight <= 0.0) {
53 XBT_DEBUG("Err, finally, there is no need to take care of variable %p", var);
54 xbt_swag_remove(var, &(sys->saturated_variable_set));
57 var_list = &(sys->saturated_variable_set);
59 cnst_list = &(sys->active_constraint_set);
60 XBT_DEBUG("Active constraints : %d", xbt_swag_size(cnst_list));
61 xbt_swag_foreach(_cnst, cnst_list)
63 cnst = static_cast<lmm_constraint_t>(_cnst);
64 xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
66 cnst_list = &(sys->saturated_constraint_set);
67 xbt_swag_foreach(_cnst, cnst_list)
69 cnst = static_cast<lmm_constraint_t>(_cnst);
70 cnst->remaining = cnst->bound;
74 XBT_DEBUG("Fair bottleneck Initialized");
77 * Compute Usage and store the variables that reach the maximum.
80 if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
81 XBT_DEBUG("Fair bottleneck done");
84 XBT_DEBUG("******* Constraints to process: %d *******", xbt_swag_size(cnst_list));
85 xbt_swag_foreach_safe(_cnst, _cnst_next, cnst_list)
87 cnst = static_cast<lmm_constraint_t>(_cnst);
89 XBT_DEBUG("Processing cnst %p ", cnst);
90 elem_list = &(cnst->enabled_element_set);
92 xbt_swag_foreach(_elem, elem_list)
94 elem = static_cast<lmm_element_t>(_elem);
95 xbt_assert(elem->variable->sharing_weight > 0);
96 if ((elem->consumption_weight > 0) && xbt_swag_belongs(elem->variable, var_list))
99 XBT_DEBUG("\tThere are %d variables", nb);
100 if (nb > 0 && not cnst->sharing_policy)
103 cnst->remaining = 0.0;
104 cnst->usage = cnst->remaining;
105 xbt_swag_remove(cnst, cnst_list);
108 cnst->usage = cnst->remaining / nb;
109 XBT_DEBUG("\tConstraint Usage %p : %f with %d variables", cnst, cnst->usage, nb);
112 xbt_swag_foreach_safe(_var, _var_next, var_list)
114 var = static_cast<lmm_variable_t>(_var);
115 double min_inc = DBL_MAX;
116 for (s_lmm_element_t const& elm : var->cnsts) {
117 if (elm.consumption_weight > 0)
118 min_inc = std::min(min_inc, elm.constraint->usage / elm.consumption_weight);
121 min_inc = std::min(min_inc, var->bound - var->value);
123 XBT_DEBUG("Updating variable %p maximum increment: %g", var, var->mu);
124 var->value += var->mu;
125 if (var->value == var->bound) {
126 xbt_swag_remove(var, var_list);
130 xbt_swag_foreach_safe(_cnst, _cnst_next, cnst_list)
132 cnst = static_cast<lmm_constraint_t>(_cnst);
133 XBT_DEBUG("Updating cnst %p ", cnst);
134 elem_list = &(cnst->enabled_element_set);
135 xbt_swag_foreach(_elem, elem_list)
137 elem = static_cast<lmm_element_t>(_elem);
138 xbt_assert(elem->variable->sharing_weight > 0);
139 if (cnst->sharing_policy) {
140 XBT_DEBUG("\tUpdate constraint %p (%g) with variable %p by %g", cnst, cnst->remaining, elem->variable,
142 double_update(&(cnst->remaining), elem->consumption_weight * elem->variable->mu, sg_maxmin_precision);
144 XBT_DEBUG("\tNon-Shared variable. Update constraint usage of %p (%g) with variable %p by %g", cnst,
145 cnst->usage, elem->variable, elem->variable->mu);
146 cnst->usage = std::min(cnst->usage, elem->consumption_weight * elem->variable->mu);
149 if (not cnst->sharing_policy) {
150 XBT_DEBUG("\tUpdate constraint %p (%g) by %g", cnst, cnst->remaining, cnst->usage);
152 double_update(&(cnst->remaining), cnst->usage, sg_maxmin_precision);
155 XBT_DEBUG("\tRemaining for %p : %g", cnst, cnst->remaining);
156 if (cnst->remaining <= 0.0) {
157 XBT_DEBUG("\tGet rid of constraint %p", cnst);
159 xbt_swag_remove(cnst, cnst_list);
160 xbt_swag_foreach(_elem, elem_list)
162 elem = static_cast<lmm_element_t>(_elem);
163 if (elem->variable->sharing_weight <= 0)
165 if (elem->consumption_weight > 0) {
166 XBT_DEBUG("\t\tGet rid of variable %p", elem->variable);
167 xbt_swag_remove(elem->variable, var_list);
172 } while (xbt_swag_size(var_list));
174 xbt_swag_reset(cnst_list);
175 sys->modified = true;
176 if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
177 XBT_DEBUG("Fair bottleneck done");