Logo AND Algorithmique Numérique Distribuée

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