Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a new "max-min" like sharing for parallel tasks.
[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
33   if (!(sys->modified))
34     return;
35
36   /* Init */
37   xbt_swag_init(&(cnst_to_update),
38                 xbt_swag_offset(s_cnst, saturated_constraint_set_hookup));
39
40   var_list = &(sys->variable_set);
41   DEBUG1("Variable set : %d", xbt_swag_size(var_list));
42   xbt_swag_foreach(var, var_list) {
43     var->value = 0.0;
44   }
45
46   cnst_list = &(sys->active_constraint_set);
47   DEBUG1("Active constraints : %d", xbt_swag_size(cnst_list));
48   xbt_swag_foreach(cnst, cnst_list) {
49     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
50   }
51   cnst_list = &(sys->saturated_constraint_set);
52   xbt_swag_foreach(cnst, cnst_list) {
53     cnst->remaining = cnst->bound;
54     cnst->usage = 0.0;
55   }
56
57   /* 
58    * Compute Usage and store the variables that reach the maximum.
59    */
60   while(1){
61     DEBUG1("Constraints to process: %d", xbt_swag_size(cnst_list));
62     xbt_swag_foreach_safe(cnst, cnst_next, cnst_list) {
63       int nb = 0;
64       elem_list = &(cnst->element_set);
65       cnst->usage = 0.0;
66       if(!cnst->shared) DIE_IMPOSSIBLE;
67       xbt_swag_foreach(elem, elem_list) {
68         if(elem->variable->weight <=0) break;
69         if ((elem->value > 0)) {
70           nb++;
71           if(cnst->usage>0)
72             cnst->usage = 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       cnst->usage = cnst->usage * nb;
80       /* Saturated constraints update */
81       if(min_usage<0 || min_usage > cnst->remaining / cnst->usage) {
82         min_usage = cnst->remaining / cnst->usage;
83         while ((useless_cnst = xbt_swag_extract(&(cnst_to_update)))) {
84           xbt_swag_insert_at_head(useless_cnst, cnst_list);
85         }
86         xbt_swag_remove(cnst, cnst_list);
87         xbt_swag_insert(cnst, &(cnst_to_update));
88       } else if (min_usage == cnst->remaining / cnst->usage) {
89         xbt_swag_remove(cnst, cnst_list);
90         xbt_swag_insert(cnst, &(cnst_to_update));
91       }
92     }
93     
94     if(!xbt_swag_size(&cnst_to_update)) break;
95     
96     while((cnst = xbt_swag_extract(&cnst_to_update))) {
97       int nb = 0;
98       xbt_swag_foreach(elem, elem_list) {
99         if(elem->variable->weight <=0) break;
100         if ((elem->value > 0)) nb++;
101       }
102       xbt_swag_foreach(elem, elem_list) {
103         var=elem->variable;
104         if(var->weight <=0) break;
105         if(var->value == 0.0) {
106           int i;
107           var->value = cnst->remaining / nb * var->weight /
108             elem->value;
109           
110           /* Update usage */
111           
112           for (i = 0; i < var->cnsts_number; i++) {
113             lmm_element_t elm = &var->cnsts[i];
114             cnst = elm->constraint;
115               double_update(&(cnst->remaining), elm->value * var->value);
116               double_update(&(cnst->usage), elm->value / var->weight);
117               //              make_elem_inactive(elm);
118             } 
119           }
120         }
121     }
122   } 
123
124   sys->modified = 0;
125   if(XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
126     lmm_print(sys);
127   }
128 }
129