Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
30fb1fb6bd638d15fcdb4b53542ad6a8d8d17819
[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 (or inactive) variable", i);
86       var->mu = -1.0;
87     } else{ 
88       var->mu =   1.0;
89       var->new_mu = 2.0;
90     }
91     DEBUG3("#### var(%d) %p ->mu :  %e", i, var, var->mu);
92     DEBUG3("#### var(%d) %p ->weight: %e", i, var, var->weight);
93     DEBUG3("#### var(%d) %p ->bound: %e", i, var, var->bound);
94     i++;
95   }
96
97   /* 
98    * Initialize lambda.
99    */
100   cnst_list=&(sys->active_constraint_set); 
101   xbt_swag_foreach(cnst, cnst_list){
102     cnst->lambda = 1.0;
103     cnst->new_lambda = 2.0;
104     DEBUG2("#### cnst(%p)->lambda :  %e", cnst, cnst->lambda);
105   }
106   
107   /*
108    * While doesn't reach a minimun error or a number maximum of iterations.
109    */
110   while(overall_error > epsilon_min_error && iteration < max_iterations){
111    
112     iteration++;
113     DEBUG1("************** ITERATION %d **************", iteration);    
114
115     /*                       
116      * Compute the value of mu_i
117      */
118     //forall mu_i in mu_1, mu_2, ..., mu_n
119     xbt_swag_foreach(var, var_list) {
120       if((var->bound >= 0) && (var->weight > 0) ){
121         var->new_mu = dicotomi(var->mu, partial_diff_mu, var, dicotomi_min_error);
122         if(var->new_mu < 0) var->new_mu = 0;
123         DEBUG2("====> var->mu (%p) = %e", var, var->new_mu);      
124         var->mu = var->new_mu;
125       } 
126     }
127
128     /*
129      * Compute the value of lambda_i
130      */
131     //forall lambda_i in lambda_1, lambda_2, ..., lambda_n
132     xbt_swag_foreach(cnst, cnst_list) {
133       cnst->new_lambda = dicotomi(cnst->lambda, partial_diff_lambda, cnst, dicotomi_min_error);
134       DEBUG2("====> cnst->lambda (%p) = %e", cnst, cnst->new_lambda);      
135       cnst->lambda = cnst->new_lambda;
136     }
137
138     /*
139      * Now computes the values of each variable (\rho) based on
140      * the values of \lambda and \mu.
141      */
142     overall_error=0;   
143     xbt_swag_foreach(var, var_list) {
144       if(var->weight <=0) 
145         var->value = 0.0;
146       else {
147         //compute sigma_i + mu_i
148         tmp = 0;
149         for(i=0; i<var->cnsts_number; i++){
150           tmp += (var->cnsts[i].constraint)->lambda;
151         }
152         if(var->bound > 0) 
153           tmp+=var->mu;
154         DEBUG3("\t Working on var (%p). cost = %e; Df = %e", var, tmp, var->df);
155
156         //uses the partial differential inverse function
157         tmp = var->func_fpi(var, tmp);
158
159         //computes de overall_error using normalized value
160         if(overall_error <  (fabs(var->value - tmp)/tmp) ){
161           overall_error = (fabs(var->value - tmp)/tmp);
162         }
163         
164         var->value = tmp;
165       }
166       DEBUG3("======> value of var (%p)  = %e, overall_error = %e", var, var->value, overall_error);       
167     }
168   }
169
170
171   //verify the KKT property for each link
172   xbt_swag_foreach(cnst, cnst_list){
173     tmp = 0;
174     elem_list = &(cnst->element_set);
175     xbt_swag_foreach(elem, elem_list) {
176       var = elem->variable;
177       if(var->weight<=0) continue;
178       tmp += var->value;
179     }
180   
181     if(tmp - cnst->bound > epsilon_min_error) {
182       WARN3("The link (%p) is over-used. Expected less than %e and got %e", cnst, cnst->bound, tmp);
183     }
184     if(!((fabs(tmp - cnst->bound)<epsilon_min_error && cnst->lambda>=epsilon_min_error) ||
185          (fabs(tmp - cnst->bound)>=epsilon_min_error && cnst->lambda<epsilon_min_error))) {
186       WARN1("The KKT condition is not verified for cnst %p...", cnst);
187       overall_error=1.0;
188     }
189   }
190   
191   //verify the KKT property of each flow
192   xbt_swag_foreach(var, var_list){
193     if(var->bound < 0 || var->weight <= 0) continue;
194
195     INFO2("Checking KKT: sat = %e mu = %e",var->value - var->bound,var->mu);
196     if(!((fabs(var->value - var->bound)<epsilon_min_error && var->mu>=epsilon_min_error) ||
197          (fabs(var->value - var->bound)>=epsilon_min_error && var->mu<epsilon_min_error))) {
198       WARN1("The KKT condition is not verified for var %p...",var);
199       overall_error=1.0;
200     }
201
202 /*     tmp = 0; */
203 /*     tmp = (var->value - var->bound); */
204 /*     if(tmp != 0.0 ||  var->mu != 0.0){ */
205 /*       WARN3("The flow (%p) doesn't match the KKT property, value expected (=0) got (lambda=%e) (sum_rho=%e)", var, var->mu, tmp); */
206 /*     } */
207   }
208
209   if(overall_error <= epsilon_min_error){
210     DEBUG1("The method converges in %d iterations.", iteration);
211   }else{
212     WARN1("Method reach %d iterations, which is the maxmimun number of iterations allowed.", iteration);
213   }
214 }
215
216 /*
217  * Returns a double value corresponding to the result of a dicotomi proccess with
218  * respect to a given variable/constraint (\mu in the case of a variable or \lambda in
219  * case of a constraint) and a initial value init. 
220  *
221  * @param init initial value for \mu or \lambda
222  * @param diff a function that computes the differential of with respect a \mu or \lambda
223  * @param var_cnst a pointer to a variable or constraint 
224  * @param min_erro a minimun error tolerated
225  *
226  * @return a double correponding to the result of the dicotomial process
227  */
228 double dicotomi(double init, double diff(double, void*), void *var_cnst, double min_error){
229   double min, max;
230   double overall_error;
231   double middle;
232   double min_diff, max_diff, middle_diff;
233   
234   min = max = init;
235
236   if(init == 0){
237     min = max = 1;
238   }
239
240   min_diff = max_diff = middle_diff = 0.0;
241   overall_error = 1;
242
243   if(diff(0.0, var_cnst) > 0){
244     DEBUG1("====> returning 0.0 (diff = %e)", diff(0.0, var_cnst));
245     return 0.0;
246   }
247
248   DEBUG0("====> not detected positive diff in 0");
249
250   while(overall_error > min_error){
251
252     min_diff = diff(min, var_cnst);
253     max_diff = diff(max, var_cnst);
254
255     DEBUG2("DICOTOMI ===> min = %e , max = %e", min, max);
256     DEBUG2("DICOTOMI ===> diffmin = %e , diffmax = %e", min_diff, max_diff);
257
258     if( min_diff > 0 && max_diff > 0 ){
259       if(min == max){
260         min = min / 2.0;
261       }else{
262         max = min;
263       }
264     }else if( min_diff < 0 && max_diff < 0 ){
265       if(min == max){
266         max = max * 2.0;
267       }else{
268         min = max;
269       }
270     }else if( min_diff < 0 && max_diff > 0 ){
271       middle = (max + min)/2.0;
272       middle_diff = diff(middle, var_cnst);
273
274       if(max != 0.0 && min != 0.0){
275         overall_error = fabs(min - max)/max;
276       }
277
278       if( middle_diff < 0 ){
279         min = middle;
280       }else if( middle_diff > 0 ){
281         max = middle;
282       }else{
283         WARN0("Found an optimal solution with 0 error!");
284         overall_error = 0;
285         return middle;
286       }
287
288     }else if(min_diff == 0){
289       return min;
290     }else if(max_diff == 0){
291       return max;
292     }else if(min_diff > 0 && max_diff < 0){
293       WARN0("The impossible happened, partial_diff(min) > 0 && partial_diff(max) < 0");
294     }
295   }
296
297
298   DEBUG1("====> returning %e", (min+max)/2.0);
299   return ((min+max)/2.0);
300 }
301
302 /*
303  *
304  */
305 double partial_diff_mu(double mu, void *param_var){
306   double mu_partial=0.0;
307   double sigma_mu=0.0;
308   lmm_variable_t var = (lmm_variable_t)param_var;
309   int i;
310
311   //compute sigma_i
312   for(i=0; i<var->cnsts_number; i++)
313     sigma_mu += (var->cnsts[i].constraint)->lambda;
314   
315   //compute sigma_i + mu_i
316   sigma_mu += mu;
317   
318   //use auxiliar function passing (sigma_i + mu_i)
319   mu_partial = diff_aux(var, sigma_mu) ;
320  
321   //add the RTT limit
322   mu_partial += var->bound;
323
324   return mu_partial;
325 }
326
327 /*
328  *
329  */
330 double partial_diff_lambda(double lambda, void *param_cnst){
331
332   int i;
333   xbt_swag_t elem_list = NULL;
334   lmm_element_t elem = NULL;
335   lmm_variable_t var = NULL;
336   lmm_constraint_t cnst= (lmm_constraint_t) param_cnst;
337   double lambda_partial=0.0;
338   double sigma_i=0.0;
339
340   elem_list = &(cnst->element_set);
341
342   DEBUG1("Computting diff of cnst (%p)", cnst);
343   
344   xbt_swag_foreach(elem, elem_list) {
345     var = elem->variable;
346     if(var->weight<=0) continue;
347     
348     //initilize de sumation variable
349     sigma_i = 0.0;
350
351     //compute sigma_i of variable var
352     for(i=0; i<var->cnsts_number; i++){
353       sigma_i += (var->cnsts[i].constraint)->lambda;
354     }
355         
356     //add mu_i if this flow has a RTT constraint associated
357     if(var->bound > 0) sigma_i += var->mu;
358
359     //replace value of cnst->lambda by the value of parameter lambda
360     sigma_i = (sigma_i - cnst->lambda) + lambda;
361     
362     //use the auxiliar function passing (\sigma_i + \mu_i)
363     lambda_partial += diff_aux(var, sigma_i);
364   }
365
366   lambda_partial += cnst->bound;
367
368   return lambda_partial;
369 }
370
371
372 double diff_aux(lmm_variable_t var, double x){
373   double tmp_fp, tmp_fpi, tmp_fpip, result;
374
375   xbt_assert0(var->func_fp, "Initialize the protocol functions first create variables before.");
376
377   tmp_fp = var->func_fp(var, x);
378   tmp_fpi = var->func_fpi(var, x);
379   tmp_fpip = var->func_fpip(var, x);
380
381   result = tmp_fpip*(var->func_fp(var, tmp_fpi));
382   
383   result = result - tmp_fpi;
384
385   result = result - (tmp_fpip * x);
386
387   return result;
388 }  
389  
390
391
392
393
394
395
396