Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Take into account the weird case where remain would already be 0 (could happen if...
[simgrid.git] / src / surf / fair_bottleneck.c
1 /*      $Id$ */
2
3 /* Copyright (c) 2007 Arnaud Legrand. All rights reserved.               */
4
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. */
7
8
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "maxmin_private.h"
12 #include <stdlib.h>
13 #include <math.h>
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_maxmin);
16
17 void bottleneck_solve(lmm_system_t sys)
18 {
19   lmm_variable_t var = NULL;
20   lmm_constraint_t cnst = NULL;
21   lmm_constraint_t useless_cnst = NULL;
22   s_lmm_constraint_t s_cnst;
23   lmm_constraint_t cnst_next = NULL;
24   lmm_element_t elem = NULL;
25   xbt_swag_t cnst_list = NULL;
26   xbt_swag_t var_list = NULL;
27   xbt_swag_t elem_list = NULL;
28   double min_usage = -1;
29
30   static s_xbt_swag_t cnst_to_update;
31
32   if (!(sys->modified))
33     return;
34
35   /* Init */
36   xbt_swag_init(&(cnst_to_update),
37                 xbt_swag_offset(s_cnst, saturated_constraint_set_hookup));
38
39   var_list = &(sys->variable_set);
40   DEBUG1("Variable set : %d", xbt_swag_size(var_list));
41   xbt_swag_foreach(var, var_list) {
42     var->value = 0.0;
43   }
44
45   cnst_list = &(sys->active_constraint_set);
46   DEBUG1("Active constraints : %d", xbt_swag_size(cnst_list));
47   xbt_swag_foreach(cnst, cnst_list) {
48     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
49   }
50   cnst_list = &(sys->saturated_constraint_set);
51   xbt_swag_foreach(cnst, cnst_list) {
52     cnst->remaining = cnst->bound;
53     cnst->usage = 0.0;
54   }
55
56   /* 
57    * Compute Usage and store the variables that reach the maximum.
58    */
59   while (1) {
60     DEBUG1("Constraints to process: %d", xbt_swag_size(cnst_list));
61     xbt_swag_foreach_safe(cnst, cnst_next, cnst_list) {
62       int nb = 0;
63       elem_list = &(cnst->element_set);
64       cnst->usage = 0.0;
65       xbt_swag_foreach(elem, elem_list) {
66         if (elem->variable->weight <= 0)
67           break;
68         if ((elem->value > 0)) {
69           nb++;
70           if (cnst->usage > 0)
71             cnst->usage =
72                 MIN(cnst->usage, elem->value / elem->variable->weight);
73           else
74             cnst->usage = elem->value / elem->variable->weight;
75         }
76         DEBUG2("Constraint Usage %p : %f", cnst, cnst->usage);
77         //      make_elem_active(elem);
78       }
79       if (!cnst->shared)
80         nb = 1;
81       cnst->usage = cnst->usage * nb;
82       /* Saturated constraints update */
83       if (min_usage < 0 || min_usage > cnst->remaining / cnst->usage) {
84         min_usage = cnst->remaining / cnst->usage;
85         while ((useless_cnst = xbt_swag_extract(&(cnst_to_update)))) {
86           xbt_swag_insert_at_head(useless_cnst, cnst_list);
87         }
88         xbt_swag_remove(cnst, cnst_list);
89         xbt_swag_insert(cnst, &(cnst_to_update));
90       } else if (min_usage == cnst->remaining / cnst->usage) {
91         xbt_swag_remove(cnst, cnst_list);
92         xbt_swag_insert(cnst, &(cnst_to_update));
93       }
94     }
95
96     if (!xbt_swag_size(&cnst_to_update))
97       break;
98
99     while ((cnst = xbt_swag_extract(&cnst_to_update))) {
100       int nb = 0;
101       xbt_swag_foreach(elem, elem_list) {
102         if (elem->variable->weight <= 0)
103           break;
104         if ((elem->value > 0))
105           nb++;
106       }
107       if (!cnst->shared)
108         nb = 1;
109
110       xbt_swag_foreach(elem, elem_list) {
111         var = elem->variable;
112         if (var->weight <= 0)
113           break;
114         if (var->value == 0.0) {
115           int i;
116           var->value = cnst->remaining / nb * var->weight / elem->value;
117
118           /* Update usage */
119
120           for (i = 0; i < var->cnsts_number; i++) {
121             lmm_element_t elm = &var->cnsts[i];
122             cnst = elm->constraint;
123             double_update(&(cnst->remaining), elm->value * var->value);
124             double_update(&(cnst->usage), elm->value / var->weight);
125             //              make_elem_inactive(elm);
126           }
127         }
128       }
129     }
130   }
131
132   sys->modified = 0;
133   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
134     lmm_print(sys);
135   }
136 }