Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Corrected some bugs, added the simix support to the other workstation
[simgrid.git] / src / surf / lagrange.c
1 /*      $Id$     */
2 /* Copyright (c) 2007 Arnaud Legrand, Pedro Velho. All rights reserved.     */
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5 /*
6  * Modelling the proportional fairness using the Lagrange Optimization 
7  * Approach. For a detailed description see:
8  * "ssh://username@scm.gforge.inria.fr/svn/memo/people/pvelho/lagrange/ppf.ps".
9  */
10 #include "xbt/log.h"
11 #include "xbt/sysdep.h"
12 #include "xbt/mallocator.h"
13 #include "maxmin_private.h"
14
15 #include <stdlib.h>
16 #ifndef MATH
17 #include <math.h>
18 #endif
19
20
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_lagrange, surf, "Logging specific to SURF (lagrange)");
22
23 /*
24  * Local prototypes to implement the lagrangian optimization with optimal step, also called dicotomi.
25  */
26 //solves the proportional fairness using a lagrange optimizition with dicotomi step
27 void   lagrange_solve       (lmm_system_t sys);
28 //computes the value of the dicotomi using a initial values, init, with a specific variable or constraint
29 double dicotomi(double init, double diff(double, void*), void *var_cnst, double min_error);
30 //computes the value of the differential of variable param_var applied to mu  
31 double partial_diff_mu      (double mu, void * param_var);
32 //computes the value of the differential of constraint param_cnst applied to lambda  
33 double partial_diff_lambda  (double lambda, void * param_cnst);
34 //auxiliar function to compute the partial_diff
35 double diff_aux(lmm_variable_t var, double x);
36
37
38 void lagrange_solve(lmm_system_t sys)
39 {
40   /*
41    * Lagrange Variables.
42    */
43   int max_iterations= 10000;
44   double epsilon_min_error  = 1e-6;
45   double dicotomi_min_error = 1e-6;
46   double overall_error = 1;
47
48   /*
49    * Variables to manipulate the data structure proposed to model the maxmin
50    * fairness. See docummentation for more details.
51    */
52   xbt_swag_t elem_list  = NULL;
53   lmm_element_t elem    = NULL;
54
55   xbt_swag_t cnst_list  = NULL;
56   lmm_constraint_t cnst = NULL;
57   
58   xbt_swag_t var_list   = NULL;
59   lmm_variable_t var    = NULL;
60
61   /*
62    * Auxiliar variables.
63    */
64   int iteration=0;
65   double tmp=0;
66   int i;
67    
68
69   DEBUG0("Iterative method configuration snapshot =====>");
70   DEBUG1("#### Maximum number of iterations       : %d", max_iterations);
71   DEBUG1("#### Minimum error tolerated            : %e", epsilon_min_error);  
72   DEBUG1("#### Minimum error tolerated (dicotomi) : %e", dicotomi_min_error);
73
74   if ( !(sys->modified))
75     return;
76
77   /* 
78    * Initialize the var list variable with only the active variables. 
79    * Associate an index in the swag variables. Initialize mu.
80    */
81   var_list = &(sys->variable_set);
82   i=0;
83   xbt_swag_foreach(var, var_list) {    
84     if((var->bound > 0.0) || (var->weight <= 0.0)){
85       DEBUG1("#### NOTE var(%d) is a boundless variable", i);
86       var->mu = -1.0;
87     } else{ 
88       var->mu =   1.0;
89       var->new_mu = 2.0;
90     }
91     DEBUG2("#### var(%d)->mu :  %e", i, var->mu);
92     DEBUG2("#### var(%d)->weight: %e", i, var->weight);
93     i++;
94   }
95
96   /* 
97    * Initialize lambda.
98    */
99   cnst_list=&(sys->active_constraint_set); 
100   xbt_swag_foreach(cnst, cnst_list){
101     cnst->lambda = 1.0;
102     cnst->new_lambda = 2.0;
103     DEBUG2("#### cnst(%p)->lambda :  %e", cnst, cnst->lambda);
104   }
105   
106   /*
107    * While doesn't reach a minimun error or a number maximum of iterations.
108    */
109   while(overall_error > epsilon_min_error && iteration < max_iterations){
110    
111     iteration++;
112     DEBUG1("************** ITERATION %d **************", iteration);    
113
114     /*                       
115      * Compute the value of mu_i
116      */
117     //forall mu_i in mu_1, mu_2, ..., mu_n
118     xbt_swag_foreach(var, var_list) {
119       if((var->bound >= 0) && (var->weight > 0) ){
120         var->new_mu = dicotomi(var->mu, partial_diff_mu, var, dicotomi_min_error);
121         if(var->new_mu < 0) var->new_mu = 0;
122         var->mu = var->new_mu;
123       }
124     }
125
126     /*
127      * Compute the value of lambda_i
128      */
129     //forall lambda_i in lambda_1, lambda_2, ..., lambda_n
130     xbt_swag_foreach(cnst, cnst_list) {
131       cnst->new_lambda = dicotomi(cnst->lambda, partial_diff_lambda, cnst, dicotomi_min_error);
132       DEBUG2("====> cnst->lambda (%p) = %e", cnst, cnst->new_lambda);      
133       cnst->lambda = cnst->new_lambda;
134     }
135
136     /*
137      * Now computes the values of each variable (\rho) based on
138      * the values of \lambda and \mu.
139      */
140     overall_error=0;   
141     xbt_swag_foreach(var, var_list) {
142       if(var->weight <=0) 
143         var->value = 0.0;
144       else {
145         //compute sigma_i + mu_i
146         tmp = 0;
147         for(i=0; i<var->cnsts_number; i++){
148           tmp += (var->cnsts[i].constraint)->lambda;
149           if(var->bound > 0) 
150             tmp+=var->mu;
151         }
152
153         //uses the partial differential inverse function
154         tmp = var->func_fpi(var, tmp);
155
156         //computes de overall_error using normalized value
157         if(overall_error <  (fabs(var->value - tmp)/tmp) ){
158           overall_error = (fabs(var->value - tmp)/tmp);
159         }
160         
161         var->value = tmp;
162       }
163       DEBUG3("======> value of var (%p)  = %e, overall_error = %e", var, var->value, overall_error);       
164     }
165   }
166
167
168   //verify the KKT property for each link
169   xbt_swag_foreach(cnst, cnst_list){
170     tmp = 0;
171     elem_list = &(cnst->element_set);
172     xbt_swag_foreach(elem, elem_list) {
173       var = elem->variable;
174       if(var->weight<=0) continue;
175       tmp += var->value;
176     }
177   
178     tmp = tmp - cnst->bound;
179
180     if(tmp > epsilon_min_error){
181       WARN3("The link (%p) doesn't match the KKT property, expected less than %e and got %e", cnst, epsilon_min_error, tmp);
182     }
183   
184   }
185   
186   //verify the KKT property of each flow
187   xbt_swag_foreach(var, var_list){
188     if(var->bound <= 0 || var->weight <= 0) continue;
189     tmp = 0;
190     tmp = (var->value - var->bound);
191
192     
193     if(tmp != 0.0 ||  var->mu != 0.0){
194       WARN3("The flow (%p) doesn't match the KKT property, value expected (=0) got (lambda=%e) (sum_rho=%e)", var, var->mu, tmp);
195     }
196
197   }
198
199   if(overall_error <= epsilon_min_error){
200     DEBUG1("The method converge in %d iterations.", iteration);
201   }else{
202     WARN1("Method reach %d iterations, which is the maxmimun number of iterations allowed.", iteration);
203   }
204 }
205
206 /*
207  * Returns a double value corresponding to the result of a dicotomi proccess with
208  * respect to a given variable/constraint (\mu in the case of a variable or \lambda in
209  * case of a constraint) and a initial value init. 
210  *
211  * @param init initial value for \mu or \lambda
212  * @param diff a function that computes the differential of with respect a \mu or \lambda
213  * @param var_cnst a pointer to a variable or constraint 
214  * @param min_erro a minimun error tolerated
215  *
216  * @return a double correponding to the result of the dicotomial process
217  */
218 double dicotomi(double init, double diff(double, void*), void *var_cnst, double min_error){
219   double min, max;
220   double overall_error;
221   double middle;
222   double min_diff, max_diff, middle_diff;
223   
224   min = max = init;
225
226   if(init == 0){
227     min = max = 1;
228   }
229
230   min_diff = max_diff = middle_diff = 0.0;
231   overall_error = 1;
232
233   if(diff(0.0, var_cnst) > 0){
234     DEBUG1("====> returning 0.0 (diff = %e)", diff(0.0, var_cnst));
235     return 0.0;
236   }
237
238   DEBUG0("====> not detected positive diff in 0");
239
240   while(overall_error > min_error){
241
242     min_diff = diff(min, var_cnst);
243     max_diff = diff(max, var_cnst);
244
245     DEBUG2("DICOTOMI ===> min = %e , max = %e", min, max);
246     DEBUG2("DICOTOMI ===> diffmin = %e , diffmax = %e", min_diff, max_diff);
247
248     if( min_diff > 0 && max_diff > 0 ){
249       if(min == max){
250         min = min / 2.0;
251       }else{
252         max = min;
253       }
254     }else if( min_diff < 0 && max_diff < 0 ){
255       if(min == max){
256         max = max * 2.0;
257       }else{
258         min = max;
259       }
260     }else if( min_diff < 0 && max_diff > 0 ){
261       middle = (max + min)/2.0;
262       middle_diff = diff(middle, var_cnst);
263
264       if(max != 0.0 && min != 0.0){
265         overall_error = fabs(min - max)/max;
266       }
267
268       if( middle_diff < 0 ){
269         min = middle;
270       }else if( middle_diff > 0 ){
271         max = middle;
272       }else{
273         WARN0("Found an optimal solution with 0 error!");
274         overall_error = 0;
275         return middle;
276       }
277
278     }else if(min_diff == 0){
279       return min;
280     }else if(max_diff == 0){
281       return max;
282     }else if(min_diff > 0 && max_diff < 0){
283       WARN0("The impossible happened, partial_diff(min) > 0 && partial_diff(max) < 0");
284     }
285   }
286
287
288   DEBUG1("====> returning %e", (min+max)/2.0);
289   return ((min+max)/2.0);
290 }
291
292 /*
293  *
294  */
295 double partial_diff_mu(double mu, void *param_var){
296   double mu_partial=0.0;
297   double sigma_mu=0.0;
298   lmm_variable_t var = (lmm_variable_t)param_var;
299   int i;
300
301   //compute sigma_i
302   for(i=0; i<var->cnsts_number; i++)
303     sigma_mu += (var->cnsts[i].constraint)->lambda;
304   
305   //compute sigma_i + mu_i
306   sigma_mu += mu;
307   
308   //use auxiliar function passing (sigma_i + mu_i)
309   mu_partial = diff_aux(var, sigma_mu) ;
310  
311   //add the RTT limit
312   mu_partial += var->bound;
313
314   return mu_partial;
315 }
316
317 /*
318  *
319  */
320 double partial_diff_lambda(double lambda, void *param_cnst){
321
322   int i;
323   xbt_swag_t elem_list = NULL;
324   lmm_element_t elem = NULL;
325   lmm_variable_t var = NULL;
326   lmm_constraint_t cnst= (lmm_constraint_t) param_cnst;
327   double lambda_partial=0.0;
328   double sigma_mu=0.0;
329
330   elem_list = &(cnst->element_set);
331
332   DEBUG1("Computting diff of cnst (%p)", cnst);
333   
334   xbt_swag_foreach(elem, elem_list) {
335     var = elem->variable;
336     if(var->weight<=0) continue;
337     
338     //initilize de sumation variable
339     sigma_mu = 0.0;
340
341     //compute sigma_i of variable var
342     for(i=0; i<var->cnsts_number; i++){
343       sigma_mu += (var->cnsts[i].constraint)->lambda;
344     }
345         
346     //add mu_i if this flow has a RTT constraint associated
347     if(var->bound > 0) sigma_mu += var->mu;
348
349     //replace value of cnst->lambda by the value of parameter lambda
350     sigma_mu = (sigma_mu - cnst->lambda) + lambda;
351     
352     //use the auxiliar function passing (\sigma_i + \mu_i)
353     lambda_partial += diff_aux(var, sigma_mu);
354   }
355
356   lambda_partial += cnst->bound;
357
358   return lambda_partial;
359 }
360
361
362 double diff_aux(lmm_variable_t var, double x){
363   double tmp_fp, tmp_fpi, tmp_fpip, result;
364
365   xbt_assert0(var->func_fp, "Initialize the protocol functions first create variables before.");
366
367   tmp_fp = var->func_fp(var, x);
368   tmp_fpi = var->func_fpi(var, x);
369   tmp_fpip = var->func_fpip(var, x);
370
371   result = tmp_fpip*(var->func_fp(var, tmp_fpi));
372   
373   result = result - tmp_fpi;
374
375   result = result - (tmp_fpip * x);
376
377   return result;
378 }  
379  
380
381
382
383
384
385
386