3 /* Copyright (c) 2007 Arnaud Legrand. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "xbt/sysdep.h"
11 #include "maxmin_private.h"
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_maxmin);
16 #define SHOW_EXPR_G(expr) DEBUG1(#expr " = %g",expr);
17 #define SHOW_EXPR_D(expr) DEBUG1(#expr " = %d",expr);
18 #define SHOW_EXPR_P(expr) DEBUG1(#expr " = %p",expr);
20 void bottleneck_solve(lmm_system_t sys)
22 lmm_variable_t var = NULL;
23 lmm_constraint_t cnst = NULL;
24 lmm_constraint_t useless_cnst = NULL;
25 s_lmm_constraint_t s_cnst;
26 lmm_constraint_t cnst_next = NULL;
27 lmm_element_t elem = NULL;
28 xbt_swag_t cnst_list = NULL;
29 xbt_swag_t var_list = NULL;
30 xbt_swag_t elem_list = NULL;
31 double min_usage = -1;
34 static s_xbt_swag_t cnst_to_update;
40 xbt_swag_init(&(cnst_to_update),
41 xbt_swag_offset(s_cnst, saturated_constraint_set_hookup));
43 var_list = &(sys->variable_set);
44 DEBUG1("Variable set : %d", xbt_swag_size(var_list));
45 xbt_swag_foreach(var, var_list) {
48 for (i = 0; i < var->cnsts_number; i++) {
49 if(var->cnsts[i].value==0.0) nb++;
51 if((nb==var->cnsts_number) && (var->weight>0.0))
55 cnst_list = &(sys->active_constraint_set);
56 DEBUG1("Active constraints : %d", xbt_swag_size(cnst_list));
57 xbt_swag_foreach(cnst, cnst_list) {
58 xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
60 cnst_list = &(sys->saturated_constraint_set);
61 xbt_swag_foreach(cnst, cnst_list) {
62 cnst->remaining = cnst->bound;
66 DEBUG0("Fair bottleneck Initialized");
69 * Compute Usage and store the variables that reach the maximum.
72 /* if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) { */
73 /* DEBUG0("Fair bottleneck done"); */
76 DEBUG1("******* Constraints to process: %d *******", xbt_swag_size(cnst_list));
77 xbt_swag_foreach_safe(cnst, cnst_next, cnst_list) {
79 double max_elem = 0.0;
80 DEBUG1("Processing cnst %p ", cnst);
81 elem_list = &(cnst->element_set);
83 xbt_swag_foreach(elem, elem_list) {
84 if (elem->variable->weight <= 0)
86 if ((elem->value > 0)) {
90 MAX(max_elem, elem->value / elem->variable->weight);
92 max_elem = elem->value / elem->variable->weight;
94 // make_elem_active(elem);
96 DEBUG2("\tmax_elem : %g with %d variables", max_elem,nb);
97 if(nb>0 && !cnst->shared)
99 cnst->usage = max_elem * nb;
100 DEBUG3("\tConstraint Usage %p : %f with %d variables", cnst, cnst->usage,nb);
102 xbt_swag_remove(cnst, cnst_list);
105 /* Saturated constraints update */
106 if (min_usage < 0 || min_usage > cnst->remaining / cnst->usage) {
107 DEBUG3("Update min_usage (%g) with cnst %p -> %g",min_usage, cnst,
108 cnst->remaining / cnst->usage);
110 min_usage = cnst->remaining / cnst->usage;
111 while ((useless_cnst = xbt_swag_extract(&(cnst_to_update)))) {
112 xbt_swag_insert_at_head(useless_cnst, cnst_list);
114 xbt_swag_remove(cnst, cnst_list);
115 xbt_swag_insert(cnst, &(cnst_to_update));
116 } else if (min_usage == cnst->remaining / cnst->usage) {
117 DEBUG2("Keep min_usage (%g) with cnst %p",min_usage, cnst);
118 xbt_swag_remove(cnst, cnst_list);
119 xbt_swag_insert(cnst, &(cnst_to_update));
123 if (!xbt_swag_size(&cnst_to_update))
126 while ((cnst_next = xbt_swag_extract(&cnst_to_update))) {
128 double remaining = cnst_next->remaining;
129 elem_list = &(cnst_next->element_set);
130 xbt_swag_foreach(elem, elem_list) {
131 if (elem->variable->weight <= 0)
133 if ((elem->value > 0))
136 if(nb>0 && !(cnst_next->shared))
139 DEBUG1("Updating for cnst %p",cnst_next);
141 xbt_swag_foreach(elem, elem_list) {
142 if (elem->value <= 0) continue;
144 var = elem->variable;
146 if (var->weight <= 0)
148 if (var->value == 0.0) {
149 DEBUG2("\tUpdating var %p (%g)",var,var->value);
150 var->value = var->weight * remaining / (nb * elem->value);
154 for (i = 0; i < var->cnsts_number; i++) {
155 lmm_element_t elm = &var->cnsts[i];
156 cnst = elm->constraint;
157 DEBUG1("\t\tUpdating cnst %p",cnst);
158 double_update(&(cnst->remaining), elm->value * var->value);
159 double_update(&(cnst->usage), elm->value / var->weight);
160 // make_elem_inactive(elm);
168 if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
169 DEBUG0("Fair bottleneck done");