Logo AND Algorithmique Numérique Distribuée

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