Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
reindent.
[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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_lagrange, surf,
21                                 "Logging specific to SURF (lagrange)");
22 XBT_LOG_NEW_SUBCATEGORY(surf_lagrange_dichotomy, surf_lagrange,
23                         "Logging specific to SURF (lagrange dichotomy)");
24
25 #define SHOW_EXPR(expr) CDEBUG1(surf_lagrange,#expr " = %g",expr);
26
27 double (*func_f_def) (lmm_variable_t, double);
28 double (*func_fp_def) (lmm_variable_t, double);
29 double (*func_fpi_def) (lmm_variable_t, double);
30
31 /*
32  * Local prototypes to implement the lagrangian optimization with optimal step, also called dichotomy.
33  */
34 //solves the proportional fairness using a lagrange optimizition with dichotomy step
35 void lagrange_solve(lmm_system_t sys);
36 //computes the value of the dichotomy using a initial values, init, with a specific variable or constraint
37 static double dichotomy(double init, double diff(double, void *),
38                         void *var_cnst, double min_error);
39 //computes the value of the differential of variable param_var applied to mu  
40 static double partial_diff_mu(double mu, void *param_var);
41 //computes the value of the differential of constraint param_cnst applied to lambda  
42 static double partial_diff_lambda(double lambda, void *param_cnst);
43
44 static int __check_feasible(xbt_swag_t cnst_list, xbt_swag_t var_list,
45                             int warn)
46 {
47   xbt_swag_t elem_list = NULL;
48   lmm_element_t elem = NULL;
49   lmm_constraint_t cnst = NULL;
50   lmm_variable_t var = NULL;
51
52   double tmp;
53
54   xbt_swag_foreach(cnst, cnst_list) {
55     tmp = 0;
56     elem_list = &(cnst->element_set);
57     xbt_swag_foreach(elem, elem_list) {
58       var = elem->variable;
59       if (var->weight <= 0)
60         continue;
61       tmp += var->value;
62     }
63
64     if (double_positive(tmp - cnst->bound)) {
65       if (warn)
66         WARN3
67             ("The link (%p) is over-used. Expected less than %f and got %f",
68              cnst, cnst->bound, tmp);
69       return 0;
70     }
71     DEBUG3
72         ("Checking feasability for constraint (%p): sat = %f, lambda = %f ",
73          cnst, tmp - cnst->bound, cnst->lambda);
74   }
75
76   xbt_swag_foreach(var, var_list) {
77     if (!var->weight)
78       break;
79     if (var->bound < 0)
80       continue;
81     DEBUG3("Checking feasability for variable (%p): sat = %f mu = %f", var,
82            var->value - var->bound, var->mu);
83
84     if (double_positive(var->value - var->bound)) {
85       if (warn)
86         WARN3
87             ("The variable (%p) is too large. Expected less than %f and got %f",
88              var, var->bound, var->value);
89       return 0;
90     }
91   }
92   return 1;
93 }
94
95 static double new_value(lmm_variable_t var)
96 {
97   double tmp = 0;
98   int i;
99
100   for (i = 0; i < var->cnsts_number; i++) {
101     tmp += (var->cnsts[i].constraint)->lambda;
102   }
103   if (var->bound > 0)
104     tmp += var->mu;
105   DEBUG3("\t Working on var (%p). cost = %e; Df = %e", var, tmp, var->df);
106   //uses the partial differential inverse function
107   return var->func_fpi(var, tmp);
108 }
109
110 static double new_mu(lmm_variable_t var)
111 {
112   double mu_i = 0.0;
113   double sigma_i = 0.0;
114   int j;
115
116   for (j = 0; j < var->cnsts_number; j++) {
117     sigma_i += (var->cnsts[j].constraint)->lambda;
118   }
119   mu_i = var->func_fp(var, var->bound) - sigma_i;
120   if (mu_i < 0.0)
121     return 0.0;
122   return mu_i;
123 }
124
125 static double dual_objective(xbt_swag_t var_list, xbt_swag_t cnst_list)
126 {
127   lmm_constraint_t cnst = NULL;
128   lmm_variable_t var = NULL;
129
130   double obj = 0.0;
131
132   xbt_swag_foreach(var, var_list) {
133     double sigma_i = 0.0;
134     int j;
135
136     if (!var->weight)
137       break;
138
139     for (j = 0; j < var->cnsts_number; j++)
140       sigma_i += (var->cnsts[j].constraint)->lambda;
141
142     if (var->bound > 0)
143       sigma_i += var->mu;
144
145     DEBUG2("var %p : sigma_i = %1.20f", var, sigma_i);
146
147     obj += var->func_f(var, var->func_fpi(var, sigma_i)) -
148         sigma_i * var->func_fpi(var, sigma_i);
149
150     if (var->bound > 0)
151       obj += var->mu * var->bound;
152   }
153
154   xbt_swag_foreach(cnst, cnst_list)
155       obj += cnst->lambda * cnst->bound;
156
157   return obj;
158 }
159
160 void lagrange_solve(lmm_system_t sys)
161 {
162   /*
163    * Lagrange Variables.
164    */
165   int max_iterations = 100;
166   double epsilon_min_error = MAXMIN_PRECISION;
167   double dichotomy_min_error = 1e-14;
168   double overall_modification = 1;
169
170   /*
171    * Variables to manipulate the data structure proposed to model the maxmin
172    * fairness. See docummentation for more details.
173    */
174   xbt_swag_t cnst_list = NULL;
175   lmm_constraint_t cnst = NULL;
176
177   xbt_swag_t var_list = NULL;
178   lmm_variable_t var = NULL;
179
180   /*
181    * Auxiliar variables.
182    */
183   int iteration = 0;
184   double tmp = 0;
185   int i;
186   double obj, new_obj;
187
188   DEBUG0("Iterative method configuration snapshot =====>");
189   DEBUG1("#### Maximum number of iterations       : %d", max_iterations);
190   DEBUG1("#### Minimum error tolerated            : %e",
191          epsilon_min_error);
192   DEBUG1("#### Minimum error tolerated (dichotomy) : %e",
193          dichotomy_min_error);
194
195   if (XBT_LOG_ISENABLED(surf_lagrange, xbt_log_priority_debug)) {
196     lmm_print(sys);
197   }
198
199   if (!(sys->modified))
200     return;
201
202
203   /* 
204    * Initialize lambda.
205    */
206   cnst_list = &(sys->active_constraint_set);
207   xbt_swag_foreach(cnst, cnst_list) {
208     cnst->lambda = 1.0;
209     cnst->new_lambda = 2.0;
210     DEBUG2("#### cnst(%p)->lambda :  %e", cnst, cnst->lambda);
211   }
212
213   /* 
214    * Initialize the var list variable with only the active variables. 
215    * Associate an index in the swag variables. Initialize mu.
216    */
217   var_list = &(sys->variable_set);
218   i = 0;
219   xbt_swag_foreach(var, var_list) {
220     if (!var->weight)
221       var->value = 0.0;
222     else {
223       if (var->bound < 0.0) {
224         DEBUG1("#### NOTE var(%d) is a boundless variable", i);
225         var->mu = -1.0;
226         var->value = new_value(var);
227       } else {
228         var->mu = 1.0;
229         var->new_mu = 2.0;
230         var->value = new_value(var);
231       }
232       DEBUG3("#### var(%d) %p ->df :  %e", i, var, var->df);
233       DEBUG3("#### var(%d) %p ->mu :  %e", i, var, var->mu);
234       DEBUG3("#### var(%d) %p ->weight: %e", i, var, var->weight);
235       DEBUG3("#### var(%d) %p ->bound: %e", i, var, var->bound);
236       i++;
237     }
238   }
239
240   /* 
241    * Compute dual objective.
242    */
243   obj = dual_objective(var_list, cnst_list);
244
245   /*
246    * While doesn't reach a minimun error or a number maximum of iterations.
247    */
248   while (overall_modification > epsilon_min_error
249          && iteration < max_iterations) {
250 /*     int dual_updated=0; */
251
252     iteration++;
253     DEBUG1("************** ITERATION %d **************", iteration);
254     DEBUG0("-------------- Gradient Descent ----------");
255
256     /*                       
257      * Improve the value of mu_i
258      */
259     xbt_swag_foreach(var, var_list) {
260       if (!var->weight)
261         break;
262       if (var->bound >= 0) {
263         DEBUG1("Working on var (%p)", var);
264         var->new_mu = new_mu(var);
265 /*      dual_updated += (fabs(var->new_mu-var->mu)>dichotomy_min_error); */
266 /*      DEBUG2("dual_updated (%d) : %1.20f",dual_updated,fabs(var->new_mu-var->mu)); */
267         DEBUG3("Updating mu : var->mu (%p) : %1.20f -> %1.20f", var,
268                var->mu, var->new_mu);
269         var->mu = var->new_mu;
270
271         new_obj = dual_objective(var_list, cnst_list);
272         DEBUG3("Improvement for Objective (%g -> %g) : %g", obj, new_obj,
273                obj - new_obj);
274         xbt_assert1(obj - new_obj >= -epsilon_min_error,
275                     "Our gradient sucks! (%1.20f)", obj - new_obj);
276         obj = new_obj;
277       }
278     }
279
280     /*
281      * Improve the value of lambda_i
282      */
283     xbt_swag_foreach(cnst, cnst_list) {
284       DEBUG1("Working on cnst (%p)", cnst);
285       cnst->new_lambda =
286           dichotomy(cnst->lambda, partial_diff_lambda, cnst,
287                     dichotomy_min_error);
288 /*       dual_updated += (fabs(cnst->new_lambda-cnst->lambda)>dichotomy_min_error); */
289 /*       DEBUG2("dual_updated (%d) : %1.20f",dual_updated,fabs(cnst->new_lambda-cnst->lambda)); */
290       DEBUG3("Updating lambda : cnst->lambda (%p) : %1.20f -> %1.20f",
291              cnst, cnst->lambda, cnst->new_lambda);
292       cnst->lambda = cnst->new_lambda;
293
294       new_obj = dual_objective(var_list, cnst_list);
295       DEBUG3("Improvement for Objective (%g -> %g) : %g", obj, new_obj,
296              obj - new_obj);
297       xbt_assert1(obj - new_obj >= -epsilon_min_error,
298                   "Our gradient sucks! (%1.20f)", obj - new_obj);
299       obj = new_obj;
300     }
301
302     /*
303      * Now computes the values of each variable (\rho) based on
304      * the values of \lambda and \mu.
305      */
306     DEBUG0("-------------- Check convergence ----------");
307     overall_modification = 0;
308     xbt_swag_foreach(var, var_list) {
309       if (var->weight <= 0)
310         var->value = 0.0;
311       else {
312         tmp = new_value(var);
313
314         overall_modification =
315             MAX(overall_modification, fabs(var->value - tmp));
316
317         var->value = tmp;
318         DEBUG3("New value of var (%p)  = %e, overall_modification = %e",
319                var, var->value, overall_modification);
320       }
321     }
322
323     DEBUG0("-------------- Check feasability ----------");
324     if (!__check_feasible(cnst_list, var_list, 0))
325       overall_modification = 1.0;
326     DEBUG2("Iteration %d: overall_modification : %f", iteration,
327            overall_modification);
328 /*     if(!dual_updated) { */
329 /*       WARN1("Could not improve the convergence at iteration %d. Drop it!",iteration); */
330 /*       break; */
331 /*     } */
332   }
333
334   __check_feasible(cnst_list, var_list, 1);
335
336   if (overall_modification <= epsilon_min_error) {
337     DEBUG1("The method converges in %d iterations.", iteration);
338   }
339   if (iteration >= max_iterations) {
340     DEBUG1
341         ("Method reach %d iterations, which is the maximum number of iterations allowed.",
342          iteration);
343   }
344 /*   INFO1("Method converged after %d iterations", iteration); */
345
346   if (XBT_LOG_ISENABLED(surf_lagrange, xbt_log_priority_debug)) {
347     lmm_print(sys);
348   }
349 }
350
351 /*
352  * Returns a double value corresponding to the result of a dichotomy proccess with
353  * respect to a given variable/constraint (\mu in the case of a variable or \lambda in
354  * case of a constraint) and a initial value init. 
355  *
356  * @param init initial value for \mu or \lambda
357  * @param diff a function that computes the differential of with respect a \mu or \lambda
358  * @param var_cnst a pointer to a variable or constraint 
359  * @param min_erro a minimun error tolerated
360  *
361  * @return a double correponding to the result of the dichotomyal process
362  */
363 static double dichotomy(double init, double diff(double, void *),
364                         void *var_cnst, double min_error)
365 {
366   double min, max;
367   double overall_error;
368   double middle;
369   double min_diff, max_diff, middle_diff;
370   double diff_0 = 0.0;
371   min = max = init;
372
373   XBT_IN;
374
375   if (init == 0.0) {
376     min = max = 0.5;
377   }
378
379   min_diff = max_diff = middle_diff = 0.0;
380   overall_error = 1;
381
382   if ((diff_0 = diff(1e-16, var_cnst)) >= 0) {
383     CDEBUG1(surf_lagrange_dichotomy, "returning 0.0 (diff = %e)", diff_0);
384     XBT_OUT;
385     return 0.0;
386   }
387
388   min_diff = diff(min, var_cnst);
389   max_diff = diff(max, var_cnst);
390
391   while (overall_error > min_error) {
392     CDEBUG4(surf_lagrange_dichotomy,
393             "[min, max] = [%1.20f, %1.20f] || diffmin, diffmax = %1.20f, %1.20f",
394             min, max, min_diff, max_diff);
395
396     if (min_diff > 0 && max_diff > 0) {
397       if (min == max) {
398         CDEBUG0(surf_lagrange_dichotomy, "Decreasing min");
399         min = min / 2.0;
400         min_diff = diff(min, var_cnst);
401       } else {
402         CDEBUG0(surf_lagrange_dichotomy, "Decreasing max");
403         max = min;
404         max_diff = min_diff;
405       }
406     } else if (min_diff < 0 && max_diff < 0) {
407       if (min == max) {
408         CDEBUG0(surf_lagrange_dichotomy, "Increasing max");
409         max = max * 2.0;
410         max_diff = diff(max, var_cnst);
411       } else {
412         CDEBUG0(surf_lagrange_dichotomy, "Increasing min");
413         min = max;
414         min_diff = max_diff;
415       }
416     } else if (min_diff < 0 && max_diff > 0) {
417       middle = (max + min) / 2.0;
418       CDEBUG1(surf_lagrange_dichotomy, "Trying (max+min)/2 : %1.20f",
419               middle);
420
421       if ((min == middle) || (max == middle)) {
422         CWARN4(surf_lagrange_dichotomy,
423                "Cannot improve the convergence! min=max=middle=%1.20f, diff = %1.20f."
424                " Reaching the 'double' limits. Maybe scaling your function would help ([%1.20f,%1.20f]).",
425                min, max - min, min_diff, max_diff);
426         break;
427       }
428       middle_diff = diff(middle, var_cnst);
429
430       if (middle_diff < 0) {
431         CDEBUG0(surf_lagrange_dichotomy, "Increasing min");
432         min = middle;
433         overall_error = max_diff - middle_diff;
434         min_diff = middle_diff;
435 /*      SHOW_EXPR(overall_error); */
436       } else if (middle_diff > 0) {
437         CDEBUG0(surf_lagrange_dichotomy, "Decreasing max");
438         max = middle;
439         overall_error = max_diff - middle_diff;
440         max_diff = middle_diff;
441 /*      SHOW_EXPR(overall_error); */
442       } else {
443         overall_error = 0;
444 /*      SHOW_EXPR(overall_error); */
445       }
446     } else if (min_diff == 0) {
447       max = min;
448       overall_error = 0;
449 /*       SHOW_EXPR(overall_error); */
450     } else if (max_diff == 0) {
451       min = max;
452       overall_error = 0;
453 /*       SHOW_EXPR(overall_error); */
454     } else if (min_diff > 0 && max_diff < 0) {
455       CWARN0(surf_lagrange_dichotomy,
456              "The impossible happened, partial_diff(min) > 0 && partial_diff(max) < 0");
457       abort();
458     } else {
459       CWARN2(surf_lagrange_dichotomy,
460              "diffmin (%1.20f) or diffmax (%1.20f) are something I don't know, taking no action.",
461              min_diff, max_diff);
462       abort();
463     }
464   }
465
466   CDEBUG1(surf_lagrange_dichotomy, "returning %e", (min + max) / 2.0);
467   XBT_OUT;
468   return ((min + max) / 2.0);
469 }
470
471 static double partial_diff_lambda(double lambda, void *param_cnst)
472 {
473
474   int j;
475   xbt_swag_t elem_list = NULL;
476   lmm_element_t elem = NULL;
477   lmm_variable_t var = NULL;
478   lmm_constraint_t cnst = (lmm_constraint_t) param_cnst;
479   double diff = 0.0;
480   double sigma_i = 0.0;
481
482   XBT_IN;
483   elem_list = &(cnst->element_set);
484
485   CDEBUG1(surf_lagrange_dichotomy, "Computing diff of cnst (%p)", cnst);
486
487   xbt_swag_foreach(elem, elem_list) {
488     var = elem->variable;
489     if (var->weight <= 0)
490       continue;
491
492     CDEBUG1(surf_lagrange_dichotomy, "Computing sigma_i for var (%p)",
493             var);
494     // Initialize the summation variable
495     sigma_i = 0.0;
496
497     // Compute sigma_i 
498     for (j = 0; j < var->cnsts_number; j++) {
499       sigma_i += (var->cnsts[j].constraint)->lambda;
500     }
501
502     //add mu_i if this flow has a RTT constraint associated
503     if (var->bound > 0)
504       sigma_i += var->mu;
505
506     //replace value of cnst->lambda by the value of parameter lambda
507     sigma_i = (sigma_i - cnst->lambda) + lambda;
508
509     diff += -var->func_fpi(var, sigma_i);
510   }
511
512
513   diff += cnst->bound;
514
515   CDEBUG3(surf_lagrange_dichotomy,
516           "d D/d lambda for cnst (%p) at %1.20f = %1.20f", cnst, lambda,
517           diff);
518   XBT_OUT;
519   return diff;
520 }
521
522 /** \brief Attribute the value bound to var->bound.
523  * 
524  *  \param func_fpi  inverse of the partial differential of f (f prime inverse, (f')^{-1})
525  * 
526  *  Set default functions to the ones passed as parameters. This is a polimorfism in C pure, enjoy the roots of programming.
527  *
528  */
529 void
530 lmm_set_default_protocol_function(double (*func_f)
531                                   (lmm_variable_t var, double x),
532                                   double (*func_fp) (lmm_variable_t var,
533                                                      double x),
534                                   double (*func_fpi) (lmm_variable_t var,
535                                                       double x))
536 {
537   func_f_def = func_f;
538   func_fp_def = func_fp;
539   func_fpi_def = func_fpi;
540 }
541
542
543 /**************** Vegas and Reno functions *************************/
544 /*
545  * NOTE for Reno: all functions consider the network
546  * coeficient (alpha) equal to 1.
547  */
548
549 /*
550  * For Vegas: $f(x) = \alpha D_f\ln(x)$
551  * Therefore: $fp(x) = \frac{\alpha D_f}{x}$
552  * Therefore: $fpi(x) = \frac{\alpha D_f}{x}$
553  */
554 #define VEGAS_SCALING 1000.0
555
556 double func_vegas_f(lmm_variable_t var, double x)
557 {
558   xbt_assert1(x > 0.0, "Don't call me with stupid values! (%1.20f)", x);
559   return VEGAS_SCALING * var->df * log(x);
560 }
561
562 double func_vegas_fp(lmm_variable_t var, double x)
563 {
564   xbt_assert1(x > 0.0, "Don't call me with stupid values! (%1.20f)", x);
565   return VEGAS_SCALING * var->df / x;
566 }
567
568 double func_vegas_fpi(lmm_variable_t var, double x)
569 {
570   xbt_assert1(x > 0.0, "Don't call me with stupid values! (%1.20f)", x);
571   return var->df / (x / VEGAS_SCALING);
572 }
573
574 /*
575  * For Reno:  $f(x) = \frac{\sqrt{3/2}}{D_f} atan(\sqrt{3/2}D_f x)$
576  * Therefore: $fp(x)  = \frac{3}{3 D_f^2 x^2+2}$
577  * Therefore: $fpi(x)  = \sqrt{\frac{1}{{D_f}^2 x} - \frac{2}{3{D_f}^2}}$
578  */
579 #define RENO_SCALING 1.0
580 double func_reno_f(lmm_variable_t var, double x)
581 {
582   xbt_assert0(var->df > 0.0, "Don't call me with stupid values!");
583
584   return RENO_SCALING * sqrt(3.0 / 2.0) / var->df * atan(sqrt(3.0 / 2.0) *
585                                                          var->df * x);
586 }
587
588 double func_reno_fp(lmm_variable_t var, double x)
589 {
590   return RENO_SCALING * 3.0 / (3.0 * var->df * var->df * x * x + 2.0);
591 }
592
593 double func_reno_fpi(lmm_variable_t var, double x)
594 {
595   double res_fpi;
596
597   xbt_assert0(var->df > 0.0, "Don't call me with stupid values!");
598   xbt_assert0(x > 0.0, "Don't call me with stupid values!");
599
600   res_fpi =
601       1.0 / (var->df * var->df * (x / RENO_SCALING)) -
602       2.0 / (3.0 * var->df * var->df);
603   if (res_fpi <= 0.0)
604     return 0.0;
605 /*   xbt_assert0(res_fpi>0.0,"Don't call me with stupid values!"); */
606   return sqrt(res_fpi);
607 }