Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Handle FAT_PIPES.
[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) break;
67         if ((elem->value > 0)) {
68           nb++;
69           if(cnst->usage>0)
70             cnst->usage = MIN(cnst->usage, elem->value / elem->variable->weight);
71           else 
72             cnst->usage = elem->value / elem->variable->weight;
73         } 
74         DEBUG2("Constraint Usage %p : %f",cnst,cnst->usage);
75         //      make_elem_active(elem);
76       }
77       if(!cnst->shared) nb=1;
78       cnst->usage = cnst->usage * nb;
79       /* Saturated constraints update */
80       if(min_usage<0 || min_usage > cnst->remaining / cnst->usage) {
81         min_usage = cnst->remaining / cnst->usage;
82         while ((useless_cnst = xbt_swag_extract(&(cnst_to_update)))) {
83           xbt_swag_insert_at_head(useless_cnst, cnst_list);
84         }
85         xbt_swag_remove(cnst, cnst_list);
86         xbt_swag_insert(cnst, &(cnst_to_update));
87       } else if (min_usage == cnst->remaining / cnst->usage) {
88         xbt_swag_remove(cnst, cnst_list);
89         xbt_swag_insert(cnst, &(cnst_to_update));
90       }
91     }
92     
93     if(!xbt_swag_size(&cnst_to_update)) break;
94     
95     while((cnst = xbt_swag_extract(&cnst_to_update))) {
96       int nb = 0;
97       xbt_swag_foreach(elem, elem_list) {
98         if(elem->variable->weight <=0) break;
99         if ((elem->value > 0)) nb++;
100       }
101       if(!cnst->shared) nb=1;
102
103       xbt_swag_foreach(elem, elem_list) {
104         var=elem->variable;
105         if(var->weight <=0) break;
106         if(var->value == 0.0) {
107           int i;
108           var->value = cnst->remaining / nb * var->weight /
109             elem->value;
110           
111           /* Update usage */
112           
113           for (i = 0; i < var->cnsts_number; i++) {
114             lmm_element_t elm = &var->cnsts[i];
115             cnst = elm->constraint;
116               double_update(&(cnst->remaining), elm->value * var->value);
117               double_update(&(cnst->usage), elm->value / var->weight);
118               //              make_elem_inactive(elm);
119             } 
120           }
121         }
122     }
123   } 
124
125   sys->modified = 0;
126   if(XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
127     lmm_print(sys);
128   }
129 }
130