Logo AND Algorithmique Numérique Distribuée

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