Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename simgrid::kernel::lmm::s_lmm_variable_t -> Variable.
[simgrid.git] / src / kernel / lmm / lagrange.cpp
1 /* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved.          */
2
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 /*
7  * Modeling the proportional fairness using the Lagrangian Optimization Approach. For a detailed description see:
8  * "ssh://username@scm.gforge.inria.fr/svn/memo/people/pvelho/lagrange/ppf.ps".
9  */
10 #include "src/kernel/lmm/maxmin.hpp"
11 #include "xbt/log.h"
12 #include "xbt/sysdep.h"
13
14 #include <algorithm>
15 #include <cstdlib>
16 #ifndef MATH
17 #include <cmath>
18 #endif
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_lagrange, surf, "Logging specific to SURF (lagrange)");
21 XBT_LOG_NEW_SUBCATEGORY(surf_lagrange_dichotomy, surf_lagrange, "Logging specific to SURF (lagrange dichotomy)");
22
23 #define SHOW_EXPR(expr) XBT_CDEBUG(surf_lagrange, #expr " = %g", expr);
24 #define VEGAS_SCALING 1000.0
25 #define RENO_SCALING 1.0
26 #define RENO2_SCALING 1.0
27
28 namespace simgrid {
29 namespace kernel {
30 namespace lmm {
31
32 double (*func_f_def)(const Variable&, double);
33 double (*func_fp_def)(const Variable&, double);
34 double (*func_fpi_def)(const Variable&, double);
35
36 /*
37  * Local prototypes to implement the Lagrangian optimization with optimal step, also called dichotomy.
38  */
39 // solves the proportional fairness using a Lagrangian optimization with dichotomy step
40 void lagrange_solve(lmm_system_t sys);
41 // computes the value of the dichotomy using a initial values, init, with a specific variable or constraint
42 static double dichotomy(double init, double diff(double, const s_lmm_constraint_t&), const s_lmm_constraint_t& cnst,
43                         double min_error);
44 // computes the value of the differential of constraint cnst applied to lambda
45 static double partial_diff_lambda(double lambda, const s_lmm_constraint_t& cnst);
46
47 template <class CnstList, class VarList>
48 static int __check_feasible(const CnstList& cnst_list, const VarList& var_list, int warn)
49 {
50   for (s_lmm_constraint_t const& cnst : cnst_list) {
51     double tmp = 0;
52     for (s_lmm_element_t const& elem : cnst.enabled_element_set) {
53       lmm_variable_t var = elem.variable;
54       xbt_assert(var->sharing_weight > 0);
55       tmp += var->value;
56     }
57
58     if (double_positive(tmp - cnst.bound, sg_maxmin_precision)) {
59       if (warn)
60         XBT_WARN("The link (%p) is over-used. Expected less than %f and got %f", &cnst, cnst.bound, tmp);
61       return 0;
62     }
63     XBT_DEBUG("Checking feasability for constraint (%p): sat = %f, lambda = %f ", &cnst, tmp - cnst.bound, cnst.lambda);
64   }
65
66   for (Variable const& var : var_list) {
67     if (not var.sharing_weight)
68       break;
69     if (var.bound < 0)
70       continue;
71     XBT_DEBUG("Checking feasability for variable (%p): sat = %f mu = %f", &var, var.value - var.bound, var.mu);
72
73     if (double_positive(var.value - var.bound, sg_maxmin_precision)) {
74       if (warn)
75         XBT_WARN("The variable (%p) is too large. Expected less than %f and got %f", &var, var.bound, var.value);
76       return 0;
77     }
78   }
79   return 1;
80 }
81
82 static double new_value(const Variable& var)
83 {
84   double tmp = 0;
85
86   for (s_lmm_element_t const& elem : var.cnsts) {
87     tmp += elem.constraint->lambda;
88   }
89   if (var.bound > 0)
90     tmp += var.mu;
91   XBT_DEBUG("\t Working on var (%p). cost = %e; Weight = %e", &var, tmp, var.sharing_weight);
92   // uses the partial differential inverse function
93   return var.func_fpi(var, tmp);
94 }
95
96 static double new_mu(const Variable& var)
97 {
98   double mu_i    = 0.0;
99   double sigma_i = 0.0;
100
101   for (s_lmm_element_t const& elem : var.cnsts) {
102     sigma_i += elem.constraint->lambda;
103   }
104   mu_i = var.func_fp(var, var.bound) - sigma_i;
105   if (mu_i < 0.0)
106     return 0.0;
107   return mu_i;
108 }
109
110 template <class VarList, class CnstList>
111 static double dual_objective(const VarList& var_list, const CnstList& cnst_list)
112 {
113   double obj = 0.0;
114
115   for (Variable const& var : var_list) {
116     double sigma_i = 0.0;
117
118     if (not var.sharing_weight)
119       break;
120
121     for (s_lmm_element_t const& elem : var.cnsts)
122       sigma_i += elem.constraint->lambda;
123
124     if (var.bound > 0)
125       sigma_i += var.mu;
126
127     XBT_DEBUG("var %p : sigma_i = %1.20f", &var, sigma_i);
128
129     obj += var.func_f(var, var.func_fpi(var, sigma_i)) - sigma_i * var.func_fpi(var, sigma_i);
130
131     if (var.bound > 0)
132       obj += var.mu * var.bound;
133   }
134
135   for (s_lmm_constraint_t const& cnst : cnst_list)
136     obj += cnst.lambda * cnst.bound;
137
138   return obj;
139 }
140
141 void lagrange_solve(lmm_system_t sys)
142 {
143   /* Lagrange Variables. */
144   int max_iterations       = 100;
145   double epsilon_min_error = 0.00001; /* this is the precision on the objective function so it's none of the
146                                          configurable values and this value is the legacy one */
147   double dichotomy_min_error  = 1e-14;
148   double overall_modification = 1;
149
150   XBT_DEBUG("Iterative method configuration snapshot =====>");
151   XBT_DEBUG("#### Maximum number of iterations        : %d", max_iterations);
152   XBT_DEBUG("#### Minimum error tolerated             : %e", epsilon_min_error);
153   XBT_DEBUG("#### Minimum error tolerated (dichotomy) : %e", dichotomy_min_error);
154
155   if (XBT_LOG_ISENABLED(surf_lagrange, xbt_log_priority_debug)) {
156     sys->print();
157   }
158
159   if (not sys->modified)
160     return;
161
162   /* Initialize lambda. */
163   auto& cnst_list = sys->active_constraint_set;
164   for (s_lmm_constraint_t& cnst : cnst_list) {
165     cnst.lambda     = 1.0;
166     cnst.new_lambda = 2.0;
167     XBT_DEBUG("#### cnst(%p)->lambda :  %e", &cnst, cnst.lambda);
168   }
169
170   /*
171    * Initialize the var_list variable with only the active variables. Initialize mu.
172    */
173   auto& var_list = sys->variable_set;
174   for (Variable& var : var_list) {
175     if (not var.sharing_weight)
176       var.value = 0.0;
177     else {
178       if (var.bound < 0.0) {
179         XBT_DEBUG("#### NOTE var(%p) is a boundless variable", &var);
180         var.mu = -1.0;
181       } else {
182         var.mu     = 1.0;
183         var.new_mu = 2.0;
184       }
185       var.value = new_value(var);
186       XBT_DEBUG("#### var(%p) ->weight :  %e", &var, var.sharing_weight);
187       XBT_DEBUG("#### var(%p) ->mu :  %e", &var, var.mu);
188       XBT_DEBUG("#### var(%p) ->weight: %e", &var, var.sharing_weight);
189       XBT_DEBUG("#### var(%p) ->bound: %e", &var, var.bound);
190       auto weighted = std::find_if(begin(var.cnsts), end(var.cnsts),
191                                    [](s_lmm_element_t const& x) { return x.consumption_weight != 0.0; });
192       if (weighted == end(var.cnsts))
193         var.value = 1.0;
194     }
195   }
196
197   /*  Compute dual objective. */
198   double obj = dual_objective(var_list, cnst_list);
199
200   /* While doesn't reach a minimum error or a number maximum of iterations. */
201   int iteration = 0;
202   while (overall_modification > epsilon_min_error && iteration < max_iterations) {
203     iteration++;
204     XBT_DEBUG("************** ITERATION %d **************", iteration);
205     XBT_DEBUG("-------------- Gradient Descent ----------");
206
207     /* Improve the value of mu_i */
208     for (Variable& var : var_list) {
209       if (var.sharing_weight && var.bound >= 0) {
210         XBT_DEBUG("Working on var (%p)", &var);
211         var.new_mu = new_mu(var);
212         XBT_DEBUG("Updating mu : var->mu (%p) : %1.20f -> %1.20f", &var, var.mu, var.new_mu);
213         var.mu = var.new_mu;
214
215         double new_obj = dual_objective(var_list, cnst_list);
216         XBT_DEBUG("Improvement for Objective (%g -> %g) : %g", obj, new_obj, obj - new_obj);
217         xbt_assert(obj - new_obj >= -epsilon_min_error, "Our gradient sucks! (%1.20f)", obj - new_obj);
218         obj = new_obj;
219       }
220     }
221
222     /* Improve the value of lambda_i */
223     for (s_lmm_constraint_t& cnst : cnst_list) {
224       XBT_DEBUG("Working on cnst (%p)", &cnst);
225       cnst.new_lambda = dichotomy(cnst.lambda, partial_diff_lambda, cnst, dichotomy_min_error);
226       XBT_DEBUG("Updating lambda : cnst->lambda (%p) : %1.20f -> %1.20f", &cnst, cnst.lambda, cnst.new_lambda);
227       cnst.lambda = cnst.new_lambda;
228
229       double new_obj = dual_objective(var_list, cnst_list);
230       XBT_DEBUG("Improvement for Objective (%g -> %g) : %g", obj, new_obj, obj - new_obj);
231       xbt_assert(obj - new_obj >= -epsilon_min_error, "Our gradient sucks! (%1.20f)", obj - new_obj);
232       obj = new_obj;
233     }
234
235     /* Now computes the values of each variable (\rho) based on the values of \lambda and \mu. */
236     XBT_DEBUG("-------------- Check convergence ----------");
237     overall_modification = 0;
238     for (Variable& var : var_list) {
239       if (var.sharing_weight <= 0)
240         var.value = 0.0;
241       else {
242         double tmp = new_value(var);
243
244         overall_modification = std::max(overall_modification, fabs(var.value - tmp));
245
246         var.value = tmp;
247         XBT_DEBUG("New value of var (%p)  = %e, overall_modification = %e", &var, var.value, overall_modification);
248       }
249     }
250
251     XBT_DEBUG("-------------- Check feasability ----------");
252     if (not __check_feasible(cnst_list, var_list, 0))
253       overall_modification = 1.0;
254     XBT_DEBUG("Iteration %d: overall_modification : %f", iteration, overall_modification);
255   }
256
257   __check_feasible(cnst_list, var_list, 1);
258
259   if (overall_modification <= epsilon_min_error) {
260     XBT_DEBUG("The method converges in %d iterations.", iteration);
261   }
262   if (iteration >= max_iterations) {
263     XBT_DEBUG("Method reach %d iterations, which is the maximum number of iterations allowed.", iteration);
264   }
265
266   if (XBT_LOG_ISENABLED(surf_lagrange, xbt_log_priority_debug)) {
267     sys->print();
268   }
269 }
270
271 /*
272  * Returns a double value corresponding to the result of a dichotomy process with respect to a given
273  * variable/constraint (\mu in the case of a variable or \lambda in case of a constraint) and a initial value init.
274  *
275  * @param init initial value for \mu or \lambda
276  * @param diff a function that computes the differential of with respect a \mu or \lambda
277  * @param var_cnst a pointer to a variable or constraint
278  * @param min_erro a minimum error tolerated
279  *
280  * @return a double corresponding to the result of the dichotomy process
281  */
282 static double dichotomy(double init, double diff(double, const s_lmm_constraint_t&), const s_lmm_constraint_t& cnst,
283                         double min_error)
284 {
285   double min = init;
286   double max = init;
287   double overall_error;
288   double middle;
289   double middle_diff;
290   double diff_0 = 0.0;
291
292   XBT_IN();
293
294   if (fabs(init) < 1e-20) {
295     min = 0.5;
296     max = 0.5;
297   }
298
299   overall_error = 1;
300
301   diff_0 = diff(1e-16, cnst);
302   if (diff_0 >= 0) {
303     XBT_CDEBUG(surf_lagrange_dichotomy, "returning 0.0 (diff = %e)", diff_0);
304     XBT_OUT();
305     return 0.0;
306   }
307
308   double min_diff = diff(min, cnst);
309   double max_diff = diff(max, cnst);
310
311   while (overall_error > min_error) {
312     XBT_CDEBUG(surf_lagrange_dichotomy, "[min, max] = [%1.20f, %1.20f] || diffmin, diffmax = %1.20f, %1.20f", min, max,
313                min_diff, max_diff);
314
315     if (min_diff > 0 && max_diff > 0) {
316       if (min == max) {
317         XBT_CDEBUG(surf_lagrange_dichotomy, "Decreasing min");
318         min      = min / 2.0;
319         min_diff = diff(min, cnst);
320       } else {
321         XBT_CDEBUG(surf_lagrange_dichotomy, "Decreasing max");
322         max      = min;
323         max_diff = min_diff;
324       }
325     } else if (min_diff < 0 && max_diff < 0) {
326       if (min == max) {
327         XBT_CDEBUG(surf_lagrange_dichotomy, "Increasing max");
328         max      = max * 2.0;
329         max_diff = diff(max, cnst);
330       } else {
331         XBT_CDEBUG(surf_lagrange_dichotomy, "Increasing min");
332         min      = max;
333         min_diff = max_diff;
334       }
335     } else if (min_diff < 0 && max_diff > 0) {
336       middle = (max + min) / 2.0;
337       XBT_CDEBUG(surf_lagrange_dichotomy, "Trying (max+min)/2 : %1.20f", middle);
338
339       if ((fabs(min - middle) < 1e-20) || (fabs(max - middle) < 1e-20)) {
340         XBT_CWARN(surf_lagrange_dichotomy,
341                   "Cannot improve the convergence! min=max=middle=%1.20f, diff = %1.20f."
342                   " Reaching the 'double' limits. Maybe scaling your function would help ([%1.20f,%1.20f]).",
343                   min, max - min, min_diff, max_diff);
344         break;
345       }
346       middle_diff = diff(middle, cnst);
347
348       if (middle_diff < 0) {
349         XBT_CDEBUG(surf_lagrange_dichotomy, "Increasing min");
350         min           = middle;
351         overall_error = max_diff - middle_diff;
352         min_diff      = middle_diff;
353       } else if (middle_diff > 0) {
354         XBT_CDEBUG(surf_lagrange_dichotomy, "Decreasing max");
355         max           = middle;
356         overall_error = max_diff - middle_diff;
357         max_diff      = middle_diff;
358       } else {
359         overall_error = 0;
360       }
361     } else if (fabs(min_diff) < 1e-20) {
362       max           = min;
363       overall_error = 0;
364     } else if (fabs(max_diff) < 1e-20) {
365       min           = max;
366       overall_error = 0;
367     } else if (min_diff > 0 && max_diff < 0) {
368       XBT_CWARN(surf_lagrange_dichotomy, "The impossible happened, partial_diff(min) > 0 && partial_diff(max) < 0");
369       xbt_abort();
370     } else {
371       XBT_CWARN(surf_lagrange_dichotomy,
372                 "diffmin (%1.20f) or diffmax (%1.20f) are something I don't know, taking no action.", min_diff,
373                 max_diff);
374       xbt_abort();
375     }
376   }
377
378   XBT_CDEBUG(surf_lagrange_dichotomy, "returning %e", (min + max) / 2.0);
379   XBT_OUT();
380   return ((min + max) / 2.0);
381 }
382
383 static double partial_diff_lambda(double lambda, const s_lmm_constraint_t& cnst)
384 {
385   double diff           = 0.0;
386
387   XBT_IN();
388
389   XBT_CDEBUG(surf_lagrange_dichotomy, "Computing diff of cnst (%p)", &cnst);
390
391   for (s_lmm_element_t const& elem : cnst.enabled_element_set) {
392     Variable& var = *elem.variable;
393     xbt_assert(var.sharing_weight > 0);
394     XBT_CDEBUG(surf_lagrange_dichotomy, "Computing sigma_i for var (%p)", &var);
395     // Initialize the summation variable
396     double sigma_i = 0.0;
397
398     // Compute sigma_i
399     for (s_lmm_element_t const& elem2 : var.cnsts)
400       sigma_i += elem2.constraint->lambda;
401
402     // add mu_i if this flow has a RTT constraint associated
403     if (var.bound > 0)
404       sigma_i += var.mu;
405
406     // replace value of cnst.lambda by the value of parameter lambda
407     sigma_i = (sigma_i - cnst.lambda) + lambda;
408
409     diff += -var.func_fpi(var, sigma_i);
410   }
411
412   diff += cnst.bound;
413
414   XBT_CDEBUG(surf_lagrange_dichotomy, "d D/d lambda for cnst (%p) at %1.20f = %1.20f", &cnst, lambda, diff);
415   XBT_OUT();
416   return diff;
417 }
418
419 /** \brief Attribute the value bound to var->bound.
420  *
421  *  \param func_fpi  inverse of the partial differential of f (f prime inverse, (f')^{-1})
422  *
423  *  Set default functions to the ones passed as parameters. This is a polymorphism in C pure, enjoy the roots of
424  *  programming.
425  *
426  */
427 void lmm_set_default_protocol_function(double (*func_f)(const Variable& var, double x),
428                                        double (*func_fp)(const Variable& var, double x),
429                                        double (*func_fpi)(const Variable& var, double x))
430 {
431   func_f_def   = func_f;
432   func_fp_def  = func_fp;
433   func_fpi_def = func_fpi;
434 }
435
436 /**************** Vegas and Reno functions *************************/
437 /* NOTE for Reno: all functions consider the network coefficient (alpha) equal to 1. */
438
439 /*
440  * For Vegas: $f(x) = \alpha D_f\ln(x)$
441  * Therefore: $fp(x) = \frac{\alpha D_f}{x}$
442  * Therefore: $fpi(x) = \frac{\alpha D_f}{x}$
443  */
444 double func_vegas_f(const Variable& var, double x)
445 {
446   xbt_assert(x > 0.0, "Don't call me with stupid values! (%1.20f)", x);
447   return VEGAS_SCALING * var.sharing_weight * log(x);
448 }
449
450 double func_vegas_fp(const Variable& var, double x)
451 {
452   xbt_assert(x > 0.0, "Don't call me with stupid values! (%1.20f)", x);
453   return VEGAS_SCALING * var.sharing_weight / x;
454 }
455
456 double func_vegas_fpi(const Variable& var, double x)
457 {
458   xbt_assert(x > 0.0, "Don't call me with stupid values! (%1.20f)", x);
459   return var.sharing_weight / (x / VEGAS_SCALING);
460 }
461
462 /*
463  * For Reno:  $f(x) = \frac{\sqrt{3/2}}{D_f} atan(\sqrt{3/2}D_f x)$
464  * Therefore: $fp(x)  = \frac{3}{3 D_f^2 x^2+2}$
465  * Therefore: $fpi(x)  = \sqrt{\frac{1}{{D_f}^2 x} - \frac{2}{3{D_f}^2}}$
466  */
467 double func_reno_f(const Variable& var, double x)
468 {
469   xbt_assert(var.sharing_weight > 0.0, "Don't call me with stupid values!");
470
471   return RENO_SCALING * sqrt(3.0 / 2.0) / var.sharing_weight * atan(sqrt(3.0 / 2.0) * var.sharing_weight * x);
472 }
473
474 double func_reno_fp(const Variable& var, double x)
475 {
476   return RENO_SCALING * 3.0 / (3.0 * var.sharing_weight * var.sharing_weight * x * x + 2.0);
477 }
478
479 double func_reno_fpi(const Variable& var, double x)
480 {
481   double res_fpi;
482
483   xbt_assert(var.sharing_weight > 0.0, "Don't call me with stupid values!");
484   xbt_assert(x > 0.0, "Don't call me with stupid values!");
485
486   res_fpi = 1.0 / (var.sharing_weight * var.sharing_weight * (x / RENO_SCALING)) -
487             2.0 / (3.0 * var.sharing_weight * var.sharing_weight);
488   if (res_fpi <= 0.0)
489     return 0.0;
490   return sqrt(res_fpi);
491 }
492
493 /* Implementing new Reno-2
494  * For Reno-2:  $f(x)   = U_f(x_f) = \frac{{2}{D_f}}*ln(2+x*D_f)$
495  * Therefore:   $fp(x)  = 2/(Weight*x + 2)
496  * Therefore:   $fpi(x) = (2*Weight)/x - 4
497  */
498 double func_reno2_f(const Variable& var, double x)
499 {
500   xbt_assert(var.sharing_weight > 0.0, "Don't call me with stupid values!");
501   return RENO2_SCALING * (1.0 / var.sharing_weight) *
502          log((x * var.sharing_weight) / (2.0 * x * var.sharing_weight + 3.0));
503 }
504
505 double func_reno2_fp(const Variable& var, double x)
506 {
507   return RENO2_SCALING * 3.0 / (var.sharing_weight * x * (2.0 * var.sharing_weight * x + 3.0));
508 }
509
510 double func_reno2_fpi(const Variable& var, double x)
511 {
512   xbt_assert(x > 0.0, "Don't call me with stupid values!");
513   double tmp     = x * var.sharing_weight * var.sharing_weight;
514   double res_fpi = tmp * (9.0 * x + 24.0);
515
516   if (res_fpi <= 0.0)
517     return 0.0;
518
519   res_fpi = RENO2_SCALING * (-3.0 * tmp + sqrt(res_fpi)) / (4.0 * tmp);
520   return res_fpi;
521 }
522 }
523 }
524 }