Logo AND Algorithmique Numérique Distribuée

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