1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
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".
13 #include "xbt/sysdep.h"
14 #include "maxmin_private.h"
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)");
26 #define SHOW_EXPR(expr) CDEBUG1(surf_lagrange,#expr " = %g",expr);
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);
33 * Local prototypes to implement the lagrangian optimization with optimal step, also called dichotomy.
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);
45 static int __check_feasible(xbt_swag_t cnst_list, xbt_swag_t var_list,
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;
55 xbt_swag_foreach(cnst, cnst_list) {
57 elem_list = &(cnst->element_set);
58 xbt_swag_foreach(elem, elem_list) {
65 if (double_positive(tmp - cnst->bound)) {
68 ("The link (%p) is over-used. Expected less than %f and got %f",
69 cnst, cnst->bound, tmp);
73 ("Checking feasability for constraint (%p): sat = %f, lambda = %f ",
74 cnst, tmp - cnst->bound, cnst->lambda);
77 xbt_swag_foreach(var, var_list) {
82 DEBUG3("Checking feasability for variable (%p): sat = %f mu = %f", var,
83 var->value - var->bound, var->mu);
85 if (double_positive(var->value - var->bound)) {
88 ("The variable (%p) is too large. Expected less than %f and got %f",
89 var, var->bound, var->value);
96 static double new_value(lmm_variable_t var)
101 for (i = 0; i < var->cnsts_number; i++) {
102 tmp += (var->cnsts[i].constraint)->lambda;
106 DEBUG3("\t Working on var (%p). cost = %e; Weight = %e", var, tmp,
108 //uses the partial differential inverse function
109 return var->func_fpi(var, tmp);
112 static double new_mu(lmm_variable_t var)
115 double sigma_i = 0.0;
118 for (j = 0; j < var->cnsts_number; j++) {
119 sigma_i += (var->cnsts[j].constraint)->lambda;
121 mu_i = var->func_fp(var, var->bound) - sigma_i;
127 static double dual_objective(xbt_swag_t var_list, xbt_swag_t cnst_list)
129 lmm_constraint_t cnst = NULL;
130 lmm_variable_t var = NULL;
134 xbt_swag_foreach(var, var_list) {
135 double sigma_i = 0.0;
141 for (j = 0; j < var->cnsts_number; j++)
142 sigma_i += (var->cnsts[j].constraint)->lambda;
147 DEBUG2("var %p : sigma_i = %1.20f", var, sigma_i);
149 obj += var->func_f(var, var->func_fpi(var, sigma_i)) -
150 sigma_i * var->func_fpi(var, sigma_i);
153 obj += var->mu * var->bound;
156 xbt_swag_foreach(cnst, cnst_list)
157 obj += cnst->lambda * cnst->bound;
162 void lagrange_solve(lmm_system_t sys)
165 * Lagrange Variables.
167 int max_iterations = 100;
168 double epsilon_min_error = MAXMIN_PRECISION;
169 double dichotomy_min_error = 1e-14;
170 double overall_modification = 1;
173 * Variables to manipulate the data structure proposed to model the maxmin
174 * fairness. See docummentation for more details.
176 xbt_swag_t cnst_list = NULL;
177 lmm_constraint_t cnst = NULL;
179 xbt_swag_t var_list = NULL;
180 lmm_variable_t var = NULL;
183 * Auxiliar variables.
190 DEBUG0("Iterative method configuration snapshot =====>");
191 DEBUG1("#### Maximum number of iterations : %d", max_iterations);
192 DEBUG1("#### Minimum error tolerated : %e", epsilon_min_error);
193 DEBUG1("#### Minimum error tolerated (dichotomy) : %e",
194 dichotomy_min_error);
196 if (XBT_LOG_ISENABLED(surf_lagrange, xbt_log_priority_debug)) {
200 if (!(sys->modified))
207 cnst_list = &(sys->active_constraint_set);
208 xbt_swag_foreach(cnst, cnst_list) {
210 cnst->new_lambda = 2.0;
211 DEBUG2("#### cnst(%p)->lambda : %e", cnst, cnst->lambda);
215 * Initialize the var list variable with only the active variables.
216 * Associate an index in the swag variables. Initialize mu.
218 var_list = &(sys->variable_set);
220 xbt_swag_foreach(var, var_list) {
225 if (var->bound < 0.0) {
226 DEBUG1("#### NOTE var(%d) is a boundless variable", i);
228 var->value = new_value(var);
232 var->value = new_value(var);
234 DEBUG2("#### var(%p) ->weight : %e", var, var->weight);
235 DEBUG2("#### var(%p) ->mu : %e", var, var->mu);
236 DEBUG2("#### var(%p) ->weight: %e", var, var->weight);
237 DEBUG2("#### var(%p) ->bound: %e", var, var->bound);
238 for (i = 0; i < var->cnsts_number; i++) {
239 if (var->cnsts[i].value == 0.0)
242 if (nb == var->cnsts_number)
248 * Compute dual objective.
250 obj = dual_objective(var_list, cnst_list);
253 * While doesn't reach a minimun error or a number maximum of iterations.
255 while (overall_modification > epsilon_min_error
256 && iteration < max_iterations) {
257 /* int dual_updated=0; */
260 DEBUG1("************** ITERATION %d **************", iteration);
261 DEBUG0("-------------- Gradient Descent ----------");
264 * Improve the value of mu_i
266 xbt_swag_foreach(var, var_list) {
269 if (var->bound >= 0) {
270 DEBUG1("Working on var (%p)", var);
271 var->new_mu = new_mu(var);
272 /* dual_updated += (fabs(var->new_mu-var->mu)>dichotomy_min_error); */
273 /* DEBUG2("dual_updated (%d) : %1.20f",dual_updated,fabs(var->new_mu-var->mu)); */
274 DEBUG3("Updating mu : var->mu (%p) : %1.20f -> %1.20f", var,
275 var->mu, var->new_mu);
276 var->mu = var->new_mu;
278 new_obj = dual_objective(var_list, cnst_list);
279 DEBUG3("Improvement for Objective (%g -> %g) : %g", obj, new_obj,
281 xbt_assert1(obj - new_obj >= -epsilon_min_error,
282 "Our gradient sucks! (%1.20f)", obj - new_obj);
288 * Improve the value of lambda_i
290 xbt_swag_foreach(cnst, cnst_list) {
291 DEBUG1("Working on cnst (%p)", cnst);
293 dichotomy(cnst->lambda, partial_diff_lambda, cnst,
294 dichotomy_min_error);
295 /* dual_updated += (fabs(cnst->new_lambda-cnst->lambda)>dichotomy_min_error); */
296 /* DEBUG2("dual_updated (%d) : %1.20f",dual_updated,fabs(cnst->new_lambda-cnst->lambda)); */
297 DEBUG3("Updating lambda : cnst->lambda (%p) : %1.20f -> %1.20f",
298 cnst, cnst->lambda, cnst->new_lambda);
299 cnst->lambda = cnst->new_lambda;
301 new_obj = dual_objective(var_list, cnst_list);
302 DEBUG3("Improvement for Objective (%g -> %g) : %g", obj, new_obj,
304 xbt_assert1(obj - new_obj >= -epsilon_min_error,
305 "Our gradient sucks! (%1.20f)", obj - new_obj);
310 * Now computes the values of each variable (\rho) based on
311 * the values of \lambda and \mu.
313 DEBUG0("-------------- Check convergence ----------");
314 overall_modification = 0;
315 xbt_swag_foreach(var, var_list) {
316 if (var->weight <= 0)
319 tmp = new_value(var);
321 overall_modification =
322 MAX(overall_modification, fabs(var->value - tmp));
325 DEBUG3("New value of var (%p) = %e, overall_modification = %e",
326 var, var->value, overall_modification);
330 DEBUG0("-------------- Check feasability ----------");
331 if (!__check_feasible(cnst_list, var_list, 0))
332 overall_modification = 1.0;
333 DEBUG2("Iteration %d: overall_modification : %f", iteration,
334 overall_modification);
335 /* if(!dual_updated) { */
336 /* WARN1("Could not improve the convergence at iteration %d. Drop it!",iteration); */
341 __check_feasible(cnst_list, var_list, 1);
343 if (overall_modification <= epsilon_min_error) {
344 DEBUG1("The method converges in %d iterations.", iteration);
346 if (iteration >= max_iterations) {
348 ("Method reach %d iterations, which is the maximum number of iterations allowed.",
351 /* INFO1("Method converged after %d iterations", iteration); */
353 if (XBT_LOG_ISENABLED(surf_lagrange, xbt_log_priority_debug)) {
359 * Returns a double value corresponding to the result of a dichotomy proccess with
360 * respect to a given variable/constraint (\mu in the case of a variable or \lambda in
361 * case of a constraint) and a initial value init.
363 * @param init initial value for \mu or \lambda
364 * @param diff a function that computes the differential of with respect a \mu or \lambda
365 * @param var_cnst a pointer to a variable or constraint
366 * @param min_erro a minimun error tolerated
368 * @return a double correponding to the result of the dichotomyal process
370 static double dichotomy(double init, double diff(double, void *),
371 void *var_cnst, double min_error)
374 double overall_error;
376 double min_diff, max_diff, middle_diff;
386 min_diff = max_diff = middle_diff = 0.0;
389 if ((diff_0 = diff(1e-16, var_cnst)) >= 0) {
390 CDEBUG1(surf_lagrange_dichotomy, "returning 0.0 (diff = %e)", diff_0);
395 min_diff = diff(min, var_cnst);
396 max_diff = diff(max, var_cnst);
398 while (overall_error > min_error) {
399 CDEBUG4(surf_lagrange_dichotomy,
400 "[min, max] = [%1.20f, %1.20f] || diffmin, diffmax = %1.20f, %1.20f",
401 min, max, min_diff, max_diff);
403 if (min_diff > 0 && max_diff > 0) {
405 CDEBUG0(surf_lagrange_dichotomy, "Decreasing min");
407 min_diff = diff(min, var_cnst);
409 CDEBUG0(surf_lagrange_dichotomy, "Decreasing max");
413 } else if (min_diff < 0 && max_diff < 0) {
415 CDEBUG0(surf_lagrange_dichotomy, "Increasing max");
417 max_diff = diff(max, var_cnst);
419 CDEBUG0(surf_lagrange_dichotomy, "Increasing min");
423 } else if (min_diff < 0 && max_diff > 0) {
424 middle = (max + min) / 2.0;
425 CDEBUG1(surf_lagrange_dichotomy, "Trying (max+min)/2 : %1.20f", middle);
427 if ((min == middle) || (max == middle)) {
428 CWARN4(surf_lagrange_dichotomy,
429 "Cannot improve the convergence! min=max=middle=%1.20f, diff = %1.20f."
430 " Reaching the 'double' limits. Maybe scaling your function would help ([%1.20f,%1.20f]).",
431 min, max - min, min_diff, max_diff);
434 middle_diff = diff(middle, var_cnst);
436 if (middle_diff < 0) {
437 CDEBUG0(surf_lagrange_dichotomy, "Increasing min");
439 overall_error = max_diff - middle_diff;
440 min_diff = middle_diff;
441 /* SHOW_EXPR(overall_error); */
442 } else if (middle_diff > 0) {
443 CDEBUG0(surf_lagrange_dichotomy, "Decreasing max");
445 overall_error = max_diff - middle_diff;
446 max_diff = middle_diff;
447 /* SHOW_EXPR(overall_error); */
450 /* SHOW_EXPR(overall_error); */
452 } else if (min_diff == 0) {
455 /* SHOW_EXPR(overall_error); */
456 } else if (max_diff == 0) {
459 /* SHOW_EXPR(overall_error); */
460 } else if (min_diff > 0 && max_diff < 0) {
461 CWARN0(surf_lagrange_dichotomy,
462 "The impossible happened, partial_diff(min) > 0 && partial_diff(max) < 0");
465 CWARN2(surf_lagrange_dichotomy,
466 "diffmin (%1.20f) or diffmax (%1.20f) are something I don't know, taking no action.",
472 CDEBUG1(surf_lagrange_dichotomy, "returning %e", (min + max) / 2.0);
474 return ((min + max) / 2.0);
477 static double partial_diff_lambda(double lambda, void *param_cnst)
481 xbt_swag_t elem_list = NULL;
482 lmm_element_t elem = NULL;
483 lmm_variable_t var = NULL;
484 lmm_constraint_t cnst = (lmm_constraint_t) param_cnst;
486 double sigma_i = 0.0;
489 elem_list = &(cnst->element_set);
491 CDEBUG1(surf_lagrange_dichotomy, "Computing diff of cnst (%p)", cnst);
493 xbt_swag_foreach(elem, elem_list) {
494 var = elem->variable;
495 if (var->weight <= 0)
498 CDEBUG1(surf_lagrange_dichotomy, "Computing sigma_i for var (%p)", var);
499 // Initialize the summation variable
503 for (j = 0; j < var->cnsts_number; j++) {
504 sigma_i += (var->cnsts[j].constraint)->lambda;
507 //add mu_i if this flow has a RTT constraint associated
511 //replace value of cnst->lambda by the value of parameter lambda
512 sigma_i = (sigma_i - cnst->lambda) + lambda;
514 diff += -var->func_fpi(var, sigma_i);
520 CDEBUG3(surf_lagrange_dichotomy,
521 "d D/d lambda for cnst (%p) at %1.20f = %1.20f", cnst, lambda,
527 /** \brief Attribute the value bound to var->bound.
529 * \param func_fpi inverse of the partial differential of f (f prime inverse, (f')^{-1})
531 * Set default functions to the ones passed as parameters. This is a polimorfism in C pure, enjoy the roots of programming.
534 void lmm_set_default_protocol_function(double (*func_f)
541 (lmm_variable_t var, double x),
542 double (*func_fp) (lmm_variable_t var,
544 double (*func_fpi) (lmm_variable_t var,
548 func_fp_def = func_fp;
549 func_fpi_def = func_fpi;
553 /**************** Vegas and Reno functions *************************/
555 * NOTE for Reno: all functions consider the network
556 * coeficient (alpha) equal to 1.
560 * For Vegas: $f(x) = \alpha D_f\ln(x)$
561 * Therefore: $fp(x) = \frac{\alpha D_f}{x}$
562 * Therefore: $fpi(x) = \frac{\alpha D_f}{x}$
564 #define VEGAS_SCALING 1000.0
566 double func_vegas_f(lmm_variable_t var, double x)
568 xbt_assert1(x > 0.0, "Don't call me with stupid values! (%1.20f)", x);
569 return VEGAS_SCALING * var->weight * log(x);
572 double func_vegas_fp(lmm_variable_t var, double x)
574 xbt_assert1(x > 0.0, "Don't call me with stupid values! (%1.20f)", x);
575 return VEGAS_SCALING * var->weight / x;
578 double func_vegas_fpi(lmm_variable_t var, double x)
580 xbt_assert1(x > 0.0, "Don't call me with stupid values! (%1.20f)", x);
581 return var->weight / (x / VEGAS_SCALING);
585 * For Reno: $f(x) = \frac{\sqrt{3/2}}{D_f} atan(\sqrt{3/2}D_f x)$
586 * Therefore: $fp(x) = \frac{3}{3 D_f^2 x^2+2}$
587 * Therefore: $fpi(x) = \sqrt{\frac{1}{{D_f}^2 x} - \frac{2}{3{D_f}^2}}$
589 #define RENO_SCALING 1.0
590 double func_reno_f(lmm_variable_t var, double x)
592 xbt_assert0(var->weight > 0.0, "Don't call me with stupid values!");
594 return RENO_SCALING * sqrt(3.0 / 2.0) / var->weight * atan(sqrt(3.0 / 2.0) *
598 double func_reno_fp(lmm_variable_t var, double x)
600 return RENO_SCALING * 3.0 / (3.0 * var->weight * var->weight * x * x + 2.0);
603 double func_reno_fpi(lmm_variable_t var, double x)
607 xbt_assert0(var->weight > 0.0, "Don't call me with stupid values!");
608 xbt_assert0(x > 0.0, "Don't call me with stupid values!");
611 1.0 / (var->weight * var->weight * (x / RENO_SCALING)) -
612 2.0 / (3.0 * var->weight * var->weight);
615 /* xbt_assert0(res_fpi>0.0,"Don't call me with stupid values!"); */
616 return sqrt(res_fpi);
620 /* Implementing new Reno-2
621 * For Reno-2: $f(x) = U_f(x_f) = \frac{{2}{D_f}}*ln(2+x*D_f)$
622 * Therefore: $fp(x) = 2/(Weight*x + 2)
623 * Therefore: $fpi(x) = (2*Weight)/x - 4
625 #define RENO2_SCALING 1.0
626 double func_reno2_f(lmm_variable_t var, double x)
628 xbt_assert0(var->weight > 0.0, "Don't call me with stupid values!");
629 return RENO2_SCALING * (1.0 / var->weight) * log((x * var->weight) /
630 (2.0 * x * var->weight +
634 double func_reno2_fp(lmm_variable_t var, double x)
636 return RENO2_SCALING * 3.0 / (var->weight * x *
637 (2.0 * var->weight * x + 3.0));
640 double func_reno2_fpi(lmm_variable_t var, double x)
645 xbt_assert0(x > 0.0, "Don't call me with stupid values!");
646 tmp = x * var->weight * var->weight;
647 res_fpi = tmp * (9.0 * x + 24.0);
652 res_fpi = RENO2_SCALING * (-3.0 * tmp + sqrt(res_fpi)) / (4.0 * tmp);