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));
78 xbt_swag_foreach_safe(cnst, cnst_next, cnst_list) {
80 double max_elem = 0.0;
81 DEBUG1("Processing cnst %p ", cnst);
82 elem_list = &(cnst->element_set);
84 xbt_swag_foreach(elem, elem_list) {
85 if (elem->variable->weight <= 0)
87 if ((elem->value > 0)) {
91 MAX(max_elem, elem->value / elem->variable->weight);
93 max_elem = elem->value / elem->variable->weight;
95 // make_elem_active(elem);
97 DEBUG2("\tmax_elem : %g with %d variables", max_elem,nb);
98 if(nb>0 && !cnst->shared)
100 cnst->usage = max_elem * nb;
101 DEBUG3("\tConstraint Usage %p : %f with %d variables", cnst, cnst->usage,nb);
103 xbt_swag_remove(cnst, cnst_list);
107 /* Saturated constraints update */
108 if (min_usage < 0 || min_usage > cnst->remaining / cnst->usage) {
109 DEBUG3("Update min_usage (%g) with cnst %p -> %g",min_usage, cnst,
110 cnst->remaining / cnst->usage);
112 min_usage = cnst->remaining / cnst->usage;
113 while ((useless_cnst = xbt_swag_extract(&(cnst_to_update)))) {
114 xbt_swag_insert_at_head(useless_cnst, cnst_list);
116 xbt_swag_remove(cnst, cnst_list);
117 xbt_swag_insert(cnst, &(cnst_to_update));
118 } else if (min_usage == cnst->remaining / cnst->usage) {
119 DEBUG2("Keep min_usage (%g) with cnst %p",min_usage, cnst);
120 xbt_swag_remove(cnst, cnst_list);
121 xbt_swag_insert(cnst, &(cnst_to_update));
123 DEBUG1("\tmin_usage: %f. No update",min_usage);
127 if (!xbt_swag_size(&cnst_to_update))
130 while ((cnst_next = xbt_swag_extract(&cnst_to_update))) {
132 double remaining = cnst_next->remaining;
133 elem_list = &(cnst_next->element_set);
134 xbt_swag_foreach(elem, elem_list) {
135 if (elem->variable->weight <= 0)
137 if ((elem->value > 0))
140 if(nb>0 && !(cnst_next->shared))
143 DEBUG1("Updating for cnst %p",cnst_next);
145 xbt_swag_foreach(elem, elem_list) {
146 if (elem->value <= 0) continue;
148 var = elem->variable;
150 if (var->weight <= 0)
152 if (var->value == 0.0) {
153 DEBUG2("\tUpdating var %p (%g)",var,var->value);
154 var->value = var->weight * remaining / (nb * elem->value);
158 for (i = 0; i < var->cnsts_number; i++) {
159 lmm_element_t elm = &var->cnsts[i];
160 cnst = elm->constraint;
161 DEBUG1("\t\tUpdating cnst %p",cnst);
162 double_update(&(cnst->remaining), elm->value * var->value);
163 double_update(&(cnst->usage), elm->value / var->weight);
164 // make_elem_inactive(elm);
172 if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
173 DEBUG0("Fair bottleneck done");