Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move a definition around so that other network models don't need to see CM02 internals...
[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; Df = %e", var, tmp, var->df);
105   //uses the partial differential inverse function
106   return var->func_fpi(var, tmp);
107 }
108
109 static double new_mu(lmm_variable_t var)
110 {
111   double mu_i = 0.0;
112   double sigma_i = 0.0;
113   int j;
114
115   for (j = 0; j < var->cnsts_number; j++) {
116     sigma_i += (var->cnsts[j].constraint)->lambda;
117   }
118   mu_i = var->func_fp(var, var->bound) - sigma_i;
119   if (mu_i < 0.0)
120     return 0.0;
121   return mu_i;
122 }
123
124 static double dual_objective(xbt_swag_t var_list, xbt_swag_t cnst_list)
125 {
126   lmm_constraint_t cnst = NULL;
127   lmm_variable_t var = NULL;
128
129   double obj = 0.0;
130
131   xbt_swag_foreach(var, var_list) {
132     double sigma_i = 0.0;
133     int j;
134
135     if (!var->weight)
136       break;
137
138     for (j = 0; j < var->cnsts_number; j++)
139       sigma_i += (var->cnsts[j].constraint)->lambda;
140
141     if (var->bound > 0)
142       sigma_i += var->mu;
143
144     DEBUG2("var %p : sigma_i = %1.20f", var, sigma_i);
145
146     obj += var->func_f(var, var->func_fpi(var, sigma_i)) -
147         sigma_i * var->func_fpi(var, sigma_i);
148
149     if (var->bound > 0)
150       obj += var->mu * var->bound;
151   }
152
153   xbt_swag_foreach(cnst, cnst_list)
154       obj += cnst->lambda * cnst->bound;
155
156   return obj;
157 }
158
159 void lagrange_solve(lmm_system_t sys)
160 {
161   /*
162    * Lagrange Variables.
163    */
164   int max_iterations = 100;
165   double epsilon_min_error = MAXMIN_PRECISION;
166   double dichotomy_min_error = 1e-14;
167   double overall_modification = 1;
168
169   /*
170    * Variables to manipulate the data structure proposed to model the maxmin
171    * fairness. See docummentation for more details.
172    */
173   xbt_swag_t cnst_list = NULL;
174   lmm_constraint_t cnst = NULL;
175
176   xbt_swag_t var_list = NULL;
177   lmm_variable_t var = NULL;
178
179   /*
180    * Auxiliar variables.
181    */
182   int iteration = 0;
183   double tmp = 0;
184   int i;
185   double obj, new_obj;
186
187   DEBUG0("Iterative method configuration snapshot =====>");
188   DEBUG1("#### Maximum number of iterations       : %d", max_iterations);
189   DEBUG1("#### Minimum error tolerated            : %e",
190          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) ->df :  %e", var, var->df);
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) nb++;
238       }
239       if(nb==var->cnsts_number) var->value = 1.0;
240     }
241   }
242
243   /* 
244    * Compute dual objective.
245    */
246   obj = dual_objective(var_list, cnst_list);
247
248   /*
249    * While doesn't reach a minimun error or a number maximum of iterations.
250    */
251   while (overall_modification > epsilon_min_error
252          && iteration < max_iterations) {
253 /*     int dual_updated=0; */
254
255     iteration++;
256     DEBUG1("************** ITERATION %d **************", iteration);
257     DEBUG0("-------------- Gradient Descent ----------");
258
259     /*                       
260      * Improve the value of mu_i
261      */
262     xbt_swag_foreach(var, var_list) {
263       if (!var->weight)
264         break;
265       if (var->bound >= 0) {
266         DEBUG1("Working on var (%p)", var);
267         var->new_mu = new_mu(var);
268 /*      dual_updated += (fabs(var->new_mu-var->mu)>dichotomy_min_error); */
269 /*      DEBUG2("dual_updated (%d) : %1.20f",dual_updated,fabs(var->new_mu-var->mu)); */
270         DEBUG3("Updating mu : var->mu (%p) : %1.20f -> %1.20f", var,
271                var->mu, var->new_mu);
272         var->mu = var->new_mu;
273
274         new_obj = dual_objective(var_list, cnst_list);
275         DEBUG3("Improvement for Objective (%g -> %g) : %g", obj, new_obj,
276                obj - new_obj);
277         xbt_assert1(obj - new_obj >= -epsilon_min_error,
278                     "Our gradient sucks! (%1.20f)", obj - new_obj);
279         obj = new_obj;
280       }
281     }
282
283     /*
284      * Improve the value of lambda_i
285      */
286     xbt_swag_foreach(cnst, cnst_list) {
287       DEBUG1("Working on cnst (%p)", cnst);
288       cnst->new_lambda =
289           dichotomy(cnst->lambda, partial_diff_lambda, cnst,
290                     dichotomy_min_error);
291 /*       dual_updated += (fabs(cnst->new_lambda-cnst->lambda)>dichotomy_min_error); */
292 /*       DEBUG2("dual_updated (%d) : %1.20f",dual_updated,fabs(cnst->new_lambda-cnst->lambda)); */
293       DEBUG3("Updating lambda : cnst->lambda (%p) : %1.20f -> %1.20f",
294              cnst, cnst->lambda, cnst->new_lambda);
295       cnst->lambda = cnst->new_lambda;
296
297       new_obj = dual_objective(var_list, cnst_list);
298       DEBUG3("Improvement for Objective (%g -> %g) : %g", obj, new_obj,
299              obj - new_obj);
300       xbt_assert1(obj - new_obj >= -epsilon_min_error,
301                   "Our gradient sucks! (%1.20f)", obj - new_obj);
302       obj = new_obj;
303     }
304
305     /*
306      * Now computes the values of each variable (\rho) based on
307      * the values of \lambda and \mu.
308      */
309     DEBUG0("-------------- Check convergence ----------");
310     overall_modification = 0;
311     xbt_swag_foreach(var, var_list) {
312       if (var->weight <= 0)
313         var->value = 0.0;
314       else {
315         tmp = new_value(var);
316
317         overall_modification =
318             MAX(overall_modification, fabs(var->value - tmp));
319
320         var->value = tmp;
321         DEBUG3("New value of var (%p)  = %e, overall_modification = %e",
322                var, var->value, overall_modification);
323       }
324     }
325
326     DEBUG0("-------------- Check feasability ----------");
327     if (!__check_feasible(cnst_list, var_list, 0))
328       overall_modification = 1.0;
329     DEBUG2("Iteration %d: overall_modification : %f", iteration,
330            overall_modification);
331 /*     if(!dual_updated) { */
332 /*       WARN1("Could not improve the convergence at iteration %d. Drop it!",iteration); */
333 /*       break; */
334 /*     } */
335   }
336
337   __check_feasible(cnst_list, var_list, 1);
338
339   if (overall_modification <= epsilon_min_error) {
340     DEBUG1("The method converges in %d iterations.", iteration);
341   }
342   if (iteration >= max_iterations) {
343     DEBUG1
344         ("Method reach %d iterations, which is the maximum number of iterations allowed.",
345          iteration);
346   }
347 /*   INFO1("Method converged after %d iterations", iteration); */
348
349   if (XBT_LOG_ISENABLED(surf_lagrange, xbt_log_priority_debug)) {
350     lmm_print(sys);
351   }
352 }
353
354 /*
355  * Returns a double value corresponding to the result of a dichotomy proccess with
356  * respect to a given variable/constraint (\mu in the case of a variable or \lambda in
357  * case of a constraint) and a initial value init. 
358  *
359  * @param init initial value for \mu or \lambda
360  * @param diff a function that computes the differential of with respect a \mu or \lambda
361  * @param var_cnst a pointer to a variable or constraint 
362  * @param min_erro a minimun error tolerated
363  *
364  * @return a double correponding to the result of the dichotomyal process
365  */
366 static double dichotomy(double init, double diff(double, void *),
367                         void *var_cnst, double min_error)
368 {
369   double min, max;
370   double overall_error;
371   double middle;
372   double min_diff, max_diff, middle_diff;
373   double diff_0 = 0.0;
374   min = max = init;
375
376   XBT_IN;
377
378   if (init == 0.0) {
379     min = max = 0.5;
380   }
381
382   min_diff = max_diff = middle_diff = 0.0;
383   overall_error = 1;
384
385   if ((diff_0 = diff(1e-16, var_cnst)) >= 0) {
386     CDEBUG1(surf_lagrange_dichotomy, "returning 0.0 (diff = %e)", diff_0);
387     XBT_OUT;
388     return 0.0;
389   }
390
391   min_diff = diff(min, var_cnst);
392   max_diff = diff(max, var_cnst);
393
394   while (overall_error > min_error) {
395     CDEBUG4(surf_lagrange_dichotomy,
396             "[min, max] = [%1.20f, %1.20f] || diffmin, diffmax = %1.20f, %1.20f",
397             min, max, min_diff, max_diff);
398
399     if (min_diff > 0 && max_diff > 0) {
400       if (min == max) {
401         CDEBUG0(surf_lagrange_dichotomy, "Decreasing min");
402         min = min / 2.0;
403         min_diff = diff(min, var_cnst);
404       } else {
405         CDEBUG0(surf_lagrange_dichotomy, "Decreasing max");
406         max = min;
407         max_diff = min_diff;
408       }
409     } else if (min_diff < 0 && max_diff < 0) {
410       if (min == max) {
411         CDEBUG0(surf_lagrange_dichotomy, "Increasing max");
412         max = max * 2.0;
413         max_diff = diff(max, var_cnst);
414       } else {
415         CDEBUG0(surf_lagrange_dichotomy, "Increasing min");
416         min = max;
417         min_diff = max_diff;
418       }
419     } else if (min_diff < 0 && max_diff > 0) {
420       middle = (max + min) / 2.0;
421       CDEBUG1(surf_lagrange_dichotomy, "Trying (max+min)/2 : %1.20f",
422               middle);
423
424       if ((min == middle) || (max == middle)) {
425         CWARN4(surf_lagrange_dichotomy,
426                "Cannot improve the convergence! min=max=middle=%1.20f, diff = %1.20f."
427                " Reaching the 'double' limits. Maybe scaling your function would help ([%1.20f,%1.20f]).",
428                min, max - min, min_diff, max_diff);
429         break;
430       }
431       middle_diff = diff(middle, var_cnst);
432
433       if (middle_diff < 0) {
434         CDEBUG0(surf_lagrange_dichotomy, "Increasing min");
435         min = middle;
436         overall_error = max_diff - middle_diff;
437         min_diff = middle_diff;
438 /*      SHOW_EXPR(overall_error); */
439       } else if (middle_diff > 0) {
440         CDEBUG0(surf_lagrange_dichotomy, "Decreasing max");
441         max = middle;
442         overall_error = max_diff - middle_diff;
443         max_diff = middle_diff;
444 /*      SHOW_EXPR(overall_error); */
445       } else {
446         overall_error = 0;
447 /*      SHOW_EXPR(overall_error); */
448       }
449     } else if (min_diff == 0) {
450       max = min;
451       overall_error = 0;
452 /*       SHOW_EXPR(overall_error); */
453     } else if (max_diff == 0) {
454       min = max;
455       overall_error = 0;
456 /*       SHOW_EXPR(overall_error); */
457     } else if (min_diff > 0 && max_diff < 0) {
458       CWARN0(surf_lagrange_dichotomy,
459              "The impossible happened, partial_diff(min) > 0 && partial_diff(max) < 0");
460       abort();
461     } else {
462       CWARN2(surf_lagrange_dichotomy,
463              "diffmin (%1.20f) or diffmax (%1.20f) are something I don't know, taking no action.",
464              min_diff, max_diff);
465       abort();
466     }
467   }
468
469   CDEBUG1(surf_lagrange_dichotomy, "returning %e", (min + max) / 2.0);
470   XBT_OUT;
471   return ((min + max) / 2.0);
472 }
473
474 static double partial_diff_lambda(double lambda, void *param_cnst)
475 {
476
477   int j;
478   xbt_swag_t elem_list = NULL;
479   lmm_element_t elem = NULL;
480   lmm_variable_t var = NULL;
481   lmm_constraint_t cnst = (lmm_constraint_t) param_cnst;
482   double diff = 0.0;
483   double sigma_i = 0.0;
484
485   XBT_IN;
486   elem_list = &(cnst->element_set);
487
488   CDEBUG1(surf_lagrange_dichotomy, "Computing diff of cnst (%p)", cnst);
489
490   xbt_swag_foreach(elem, elem_list) {
491     var = elem->variable;
492     if (var->weight <= 0)
493       continue;
494
495     CDEBUG1(surf_lagrange_dichotomy, "Computing sigma_i for var (%p)",
496             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
533 lmm_set_default_protocol_function(double (*func_f)
534                                   (lmm_variable_t var, double x),
535                                   double (*func_fp) (lmm_variable_t var,
536                                                      double x),
537                                   double (*func_fpi) (lmm_variable_t var,
538                                                       double x))
539 {
540   func_f_def = func_f;
541   func_fp_def = func_fp;
542   func_fpi_def = func_fpi;
543 }
544
545
546 /**************** Vegas and Reno functions *************************/
547 /*
548  * NOTE for Reno: all functions consider the network
549  * coeficient (alpha) equal to 1.
550  */
551
552 /*
553  * For Vegas: $f(x) = \alpha D_f\ln(x)$
554  * Therefore: $fp(x) = \frac{\alpha D_f}{x}$
555  * Therefore: $fpi(x) = \frac{\alpha D_f}{x}$
556  */
557 #define VEGAS_SCALING 1000.0
558
559 double func_vegas_f(lmm_variable_t var, double x)
560 {
561   xbt_assert1(x > 0.0, "Don't call me with stupid values! (%1.20f)", x);
562   return VEGAS_SCALING * var->df * log(x);
563 }
564
565 double func_vegas_fp(lmm_variable_t var, double x)
566 {
567   xbt_assert1(x > 0.0, "Don't call me with stupid values! (%1.20f)", x);
568   return VEGAS_SCALING * var->df / x;
569 }
570
571 double func_vegas_fpi(lmm_variable_t var, double x)
572 {
573   xbt_assert1(x > 0.0, "Don't call me with stupid values! (%1.20f)", x);
574   return var->df / (x / VEGAS_SCALING);
575 }
576
577 /*
578  * For Reno:  $f(x) = \frac{\sqrt{3/2}}{D_f} atan(\sqrt{3/2}D_f x)$
579  * Therefore: $fp(x)  = \frac{3}{3 D_f^2 x^2+2}$
580  * Therefore: $fpi(x)  = \sqrt{\frac{1}{{D_f}^2 x} - \frac{2}{3{D_f}^2}}$
581  */
582 #define RENO_SCALING 1.0
583 double func_reno_f(lmm_variable_t var, double x)
584 {
585   xbt_assert0(var->df > 0.0, "Don't call me with stupid values!");
586
587   return RENO_SCALING * sqrt(3.0 / 2.0) / var->df * atan(sqrt(3.0 / 2.0) *
588                                                          var->df * x);
589 }
590
591 double func_reno_fp(lmm_variable_t var, double x)
592 {
593   return RENO_SCALING * 3.0 / (3.0 * var->df * var->df * x * x + 2.0);
594 }
595
596 double func_reno_fpi(lmm_variable_t var, double x)
597 {
598   double res_fpi;
599
600   xbt_assert0(var->df > 0.0, "Don't call me with stupid values!");
601   xbt_assert0(x > 0.0, "Don't call me with stupid values!");
602
603   res_fpi =
604       1.0 / (var->df * var->df * (x / RENO_SCALING)) -
605       2.0 / (3.0 * var->df * var->df);
606   if (res_fpi <= 0.0)
607     return 0.0;
608 /*   xbt_assert0(res_fpi>0.0,"Don't call me with stupid values!"); */
609   return sqrt(res_fpi);
610 }