Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
af1b2f9ae38afbb67668987ff75122faca8eb940
[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-4;
45   double dicotomi_min_error = 1e-8;
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
157         if(overall_error < fabs(var->value - tmp)){
158           overall_error = fabs(var->value - tmp);
159         }
160         
161         var->value = tmp;
162       }
163       DEBUG4("======> value of var %s (%p)  = %e, overall_error = %e", (char *)var->id, 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       WARN4("The link %s(%p) doesn't match the KKT property, expected less than %e and got %e", (char *)cnst->id, 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 ||  var->mu != 0){
194       WARN4("The flow %s(%p) doesn't match the KKT property, value expected (=0) got (lambda=%e) (sum_rho=%e)", (char *)var->id, 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       overall_error = fabs(min - max);
264
265       if( middle_diff < 0 ){
266         min = middle;
267       }else if( middle_diff > 0 ){
268         max = middle;
269       }else{
270         WARN0("Found an optimal solution with 0 error!");
271         overall_error = 0;
272         return middle;
273       }
274
275     }else if(min_diff == 0){
276       return min;
277     }else if(max_diff == 0){
278       return max;
279     }else if(min_diff > 0 && max_diff < 0){
280       WARN0("The impossible happened, partial_diff(min) > 0 && partial_diff(max) < 0");
281     }
282   }
283
284
285   DEBUG1("====> returning %e", (min+max)/2.0);
286   return ((min+max)/2.0);
287 }
288
289 /*
290  *
291  */
292 double partial_diff_mu(double mu, void *param_var){
293   double mu_partial=0.0;
294   double sigma_mu=0.0;
295   lmm_variable_t var = (lmm_variable_t)param_var;
296   int i;
297
298   //compute sigma_i
299   for(i=0; i<var->cnsts_number; i++)
300     sigma_mu += (var->cnsts[i].constraint)->lambda;
301   
302   //compute sigma_i + mu_i
303   sigma_mu += var->mu;
304   
305   //use auxiliar function passing (sigma_i + mu_i)
306   mu_partial = diff_aux(var, sigma_mu) ;
307  
308   //add the RTT limit
309   mu_partial += var->bound;
310
311   return mu_partial;
312 }
313
314 /*
315  *
316  */
317 double partial_diff_lambda(double lambda, void *param_cnst){
318
319   int i;
320   xbt_swag_t elem_list = NULL;
321   lmm_element_t elem = NULL;
322   lmm_variable_t var = NULL;
323   lmm_constraint_t cnst= (lmm_constraint_t) param_cnst;
324   double lambda_partial=0.0;
325   double sigma_mu=0.0;
326
327   elem_list = &(cnst->element_set);
328
329   DEBUG2("Computting diff of cnst (%p) %s", cnst, (char *)cnst->id);
330   
331   xbt_swag_foreach(elem, elem_list) {
332     var = elem->variable;
333     if(var->weight<=0) continue;
334     
335     //initilize de sumation variable
336     sigma_mu = 0.0;
337
338     //compute sigma_i of variable var
339     for(i=0; i<var->cnsts_number; i++){
340       sigma_mu += (var->cnsts[i].constraint)->lambda;
341     }
342         
343     //add mu_i if this flow has a RTT constraint associated
344     if(var->bound > 0) sigma_mu += var->mu;
345
346     //replace value of cnst->lambda by the value of parameter lambda
347     sigma_mu = (sigma_mu - cnst->lambda) + lambda;
348     
349     //use the auxiliar function passing (\sigma_i + \mu_i)
350     lambda_partial += diff_aux(var, sigma_mu);
351   }
352
353   lambda_partial += cnst->bound;
354
355   return lambda_partial;
356 }
357
358
359 double diff_aux(lmm_variable_t var, double x){
360   double tmp_fp, tmp_fpi, tmp_fpip, result;
361
362   xbt_assert0(var->func_fp, "Initialize the protocol functions first create variables before.");
363
364   tmp_fp = var->func_fp(var, x);
365   tmp_fpi = var->func_fpi(var, x);
366   tmp_fpip = var->func_fpip(var, x);
367
368   result = tmp_fpip*(var->func_fp(var, tmp_fpi));
369   
370   result = result - tmp_fpi;
371
372   result = result - (tmp_fpip * x);
373
374   return result;
375 }  
376  
377
378
379
380
381
382
383