Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d616c715e00e217099b956107fd49ddb4a6d5603
[simgrid.git] / src / surf / maxmin.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "xbt/mallocator.h"
12 #include "maxmin_private.h"
13 #include <stdlib.h>
14 #include <math.h>
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_maxmin, surf,
16                                 "Logging specific to SURF (maxmin)");
17
18 static void* lmm_variable_mallocator_new_f(void);
19 static void lmm_variable_mallocator_free_f(void *var);
20 static void lmm_variable_mallocator_reset_f(void *var);
21
22 lmm_system_t lmm_system_new(void)
23 {
24   lmm_system_t l = NULL;
25   s_lmm_variable_t var;
26   s_lmm_constraint_t cnst;
27
28   l = xbt_new0(s_lmm_system_t, 1);
29
30   l->modified = 0;
31   xbt_swag_init(&(l->variable_set),
32                 xbt_swag_offset(var, variable_set_hookup));
33   xbt_swag_init(&(l->constraint_set),
34                 xbt_swag_offset(cnst, constraint_set_hookup));
35
36   xbt_swag_init(&(l->active_constraint_set),
37                 xbt_swag_offset(cnst, active_constraint_set_hookup));
38
39   xbt_swag_init(&(l->saturated_variable_set),
40                 xbt_swag_offset(var, saturated_variable_set_hookup));
41   xbt_swag_init(&(l->saturated_constraint_set),
42                 xbt_swag_offset(cnst, saturated_constraint_set_hookup));
43
44   l->variable_mallocator = xbt_mallocator_new(64,
45                                               lmm_variable_mallocator_new_f,
46                                               lmm_variable_mallocator_free_f,
47                                               lmm_variable_mallocator_reset_f);
48
49   return l;
50 }
51
52 void lmm_system_free(lmm_system_t sys)
53 {
54   lmm_variable_t var = NULL;
55   lmm_constraint_t cnst = NULL;
56
57   while ((var = extract_variable(sys)))
58     lmm_var_free(sys, var);
59
60   while ((cnst = extract_constraint(sys)))
61     lmm_cnst_free(sys, cnst);
62
63   xbt_mallocator_free(sys->variable_mallocator);
64   free(sys);
65 }
66
67 void lmm_variable_disable(lmm_system_t sys, lmm_variable_t var)
68 {
69   int i;
70   lmm_element_t elem = NULL;
71
72   XBT_IN2("(sys=%p, var=%p)",sys,var);
73   sys->modified = 1;
74
75   for (i = 0; i < var->cnsts_number; i++) {
76     elem = &var->cnsts[i];
77     xbt_swag_remove(elem, &(elem->constraint->element_set));
78     xbt_swag_remove(elem, &(elem->constraint->active_element_set));
79     if (!xbt_swag_size(&(elem->constraint->element_set)))
80       make_constraint_inactive(sys, elem->constraint);
81   }
82   var->cnsts_number = 0;
83   XBT_OUT;
84 }
85
86 static void lmm_var_free(lmm_system_t sys, lmm_variable_t var)
87 {
88
89   lmm_variable_disable(sys, var);
90   free(var->cnsts);
91   xbt_mallocator_release(sys->variable_mallocator, var);
92 }
93
94 static void lmm_cnst_free(lmm_system_t sys, lmm_constraint_t cnst)
95 {
96 /*   xbt_assert0(xbt_swag_size(&(cnst->element_set)), */
97 /*            "This list should be empty!"); */
98   remove_active_constraint(sys, cnst);
99   free(cnst);
100 }
101
102 lmm_constraint_t lmm_constraint_new(lmm_system_t sys, void *id,
103                                     double bound_value)
104 {
105   lmm_constraint_t cnst = NULL;
106   s_lmm_element_t elem;
107
108   cnst = xbt_new0(s_lmm_constraint_t, 1);
109   cnst->id = id;
110   xbt_swag_init(&(cnst->element_set),
111                 xbt_swag_offset(elem, element_set_hookup));
112   xbt_swag_init(&(cnst->active_element_set),
113                 xbt_swag_offset(elem, active_element_set_hookup));
114
115   cnst->bound = bound_value;
116   cnst->usage = 0;
117   cnst->shared = 1;
118   insert_constraint(sys, cnst);
119
120   return cnst;
121 }
122
123 void lmm_constraint_shared(lmm_constraint_t cnst)
124 {
125   cnst->shared = 0;
126 }
127
128 void lmm_constraint_free(lmm_system_t sys, lmm_constraint_t cnst)
129 {
130   remove_constraint(sys, cnst);
131   lmm_cnst_free(sys, cnst);
132 }
133
134 static void* lmm_variable_mallocator_new_f(void) {
135   return xbt_new(s_lmm_variable_t, 1);
136 }
137
138 static void lmm_variable_mallocator_free_f(void *var) {
139   xbt_free(var);
140 }
141
142 static void lmm_variable_mallocator_reset_f(void *var) {
143   /* memset to zero like calloc */
144   memset(var, 0, sizeof(s_lmm_variable_t));
145 }
146
147 lmm_variable_t lmm_variable_new(lmm_system_t sys, void *id,
148                                 double weight,
149                                 double bound, int number_of_constraints)
150 {
151   lmm_variable_t var = NULL;
152   int i;
153
154   XBT_IN5("(sys=%p, id=%p, weight=%f, bound=%f, num_cons =%d)",
155           sys,id,weight,bound,number_of_constraints);
156
157   var = xbt_mallocator_get(sys->variable_mallocator);
158   var->id = id;
159   var->cnsts = xbt_new0(s_lmm_element_t, number_of_constraints);
160   for(i=0; i<number_of_constraints; i++) {
161     /* Should be useless because of the 
162        calloc but it seems to help valgrind... */
163     var->cnsts[i].element_set_hookup.next = NULL;
164     var->cnsts[i].element_set_hookup.prev = NULL;
165     var->cnsts[i].active_element_set_hookup.next = NULL;
166     var->cnsts[i].active_element_set_hookup.prev = NULL;
167     var->cnsts[i].constraint = NULL;
168     var->cnsts[i].variable = NULL;
169     var->cnsts[i].value = 0.0;
170   }
171   var->cnsts_size = number_of_constraints;
172   var->cnsts_number = 0; /* Should be useless because of the 
173                             calloc but it seems to help valgrind... */
174   var->weight = weight;
175   var->bound = bound;
176   var->value = 0.0;
177   var->df    = 0.0;
178   if(weight) xbt_swag_insert_at_head(var,&(sys->variable_set));
179   else xbt_swag_insert_at_tail(var,&(sys->variable_set));
180   XBT_OUT;
181   return var;
182 }
183
184 void lmm_variable_free(lmm_system_t sys, lmm_variable_t var)
185 {
186   remove_variable(sys, var);
187   lmm_var_free(sys, var);
188 }
189
190 double lmm_variable_getvalue(lmm_variable_t var)
191 {
192   return (var->value);
193 }
194
195 void lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
196                 lmm_variable_t var, double value)
197 {
198   lmm_element_t elem = NULL;
199
200   sys->modified = 1;
201
202   xbt_assert0(var->cnsts_number < var->cnsts_size,
203               "Too much constraints");
204
205   elem = &(var->cnsts[var->cnsts_number++]);
206
207   elem->value = value;
208   elem->constraint = cnst;
209   elem->variable = var;
210
211   if(var->weight) xbt_swag_insert_at_head(elem,&(elem->constraint->element_set));
212   else xbt_swag_insert_at_tail(elem,&(elem->constraint->element_set));
213
214   make_constraint_active(sys, cnst);
215 }
216
217 void lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst,
218                     lmm_variable_t var, double value)
219 {
220   int i ; 
221   sys->modified = 1;
222
223   for(i=0; i< var->cnsts_number ; i++)
224     if(var->cnsts[i].constraint == cnst) break;
225   
226   if(i<var->cnsts_number) var->cnsts[i].value +=value;
227   else lmm_expand(sys,cnst,var,value);
228 }
229
230 void lmm_elem_set_value(lmm_system_t sys, lmm_constraint_t cnst,
231                         lmm_variable_t var, double value)
232 {
233   int i ; 
234
235   for(i=0; i< var->cnsts_number ; i++)
236     if(var->cnsts[i].constraint == cnst) break;
237
238   if(i<var->cnsts_number) {
239     var->cnsts[i].value =value;
240     sys->modified = 1;
241   } else DIE_IMPOSSIBLE;
242 }
243
244 lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t sys,
245                                        lmm_variable_t var, int num)
246 {
247   if (num < var->cnsts_number)
248     return (var->cnsts[num].constraint);
249   else
250     return NULL;
251 }
252
253 int lmm_get_number_of_cnst_from_var(lmm_system_t sys, lmm_variable_t var)
254 {
255   return (var->cnsts_number);
256 }
257
258 lmm_variable_t lmm_get_var_from_cnst(lmm_system_t sys,
259                                      lmm_constraint_t cnst,
260                                      lmm_variable_t * var)
261 {
262   if (!(*var))
263     xbt_swag_getFirst(&(cnst->element_set));
264   else
265     *var = xbt_swag_getNext(*var, cnst->element_set.offset);
266   return *var;
267 }
268
269 void *lmm_constraint_id(lmm_constraint_t cnst)
270 {
271   return cnst->id;
272 }
273
274 void *lmm_variable_id(lmm_variable_t var)
275 {
276   return var->id;
277 }
278
279 static void saturated_constraint_set_update(lmm_system_t sys,
280                                             lmm_constraint_t cnst,
281                                             double *min_usage)
282 {
283   lmm_constraint_t useless_cnst = NULL;
284
285   XBT_IN3("sys=%p, cnst=%p, min_usage=%f",sys,cnst,*min_usage);
286   if (cnst->usage <= 0) {
287     XBT_OUT;
288     return;
289   }
290   if (cnst->remaining <= 0) {
291     XBT_OUT;
292     return;
293   }
294   if ((*min_usage < 0) || (*min_usage > cnst->remaining / cnst->usage)) {
295     *min_usage = cnst->remaining / cnst->usage;
296     LOG3(xbt_log_priority_trace, 
297          "min_usage=%f (cnst->remaining=%f, cnst->usage=%f)",*min_usage, 
298          cnst->remaining, cnst->usage);
299     while ((useless_cnst =
300             xbt_swag_getFirst(&(sys->saturated_constraint_set))))
301       xbt_swag_remove(useless_cnst, &(sys->saturated_constraint_set));
302
303     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
304   } else if (*min_usage == cnst->remaining / cnst->usage) {
305     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
306   }
307   XBT_OUT;
308 }
309
310 static void saturated_variable_set_update(lmm_system_t sys)
311 {
312   lmm_constraint_t cnst = NULL;
313   xbt_swag_t cnst_list = NULL;
314   lmm_element_t elem = NULL;
315   xbt_swag_t elem_list = NULL;
316
317   cnst_list = &(sys->saturated_constraint_set);
318   while ((cnst = xbt_swag_getFirst(cnst_list))) {
319     elem_list = &(cnst->active_element_set);
320     xbt_swag_foreach(elem, elem_list) {
321       if(elem->variable->weight<=0) break;
322       if ((elem->value > 0))
323         xbt_swag_insert(elem->variable, &(sys->saturated_variable_set));
324     }
325     xbt_swag_remove(cnst, cnst_list);
326   }
327 }
328
329 void lmm_print(lmm_system_t sys)
330 {
331   lmm_constraint_t cnst = NULL;
332   lmm_element_t elem = NULL;
333   lmm_variable_t var = NULL;
334   xbt_swag_t cnst_list = NULL;
335   xbt_swag_t var_list = NULL;
336   xbt_swag_t elem_list = NULL;
337   char print_buf[1024];
338   char *trace_buf=xbt_malloc0(sizeof(char));
339   double sum=0.0;
340
341   /* Printing Objective */
342   var_list = &(sys->variable_set);
343   sprintf(print_buf,"MAX-MIN ( ");
344   trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
345   strcat(trace_buf, print_buf);
346   xbt_swag_foreach(var, var_list) {
347     sprintf(print_buf,"'%p'(%f) ",var,var->weight);
348     trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
349     strcat(trace_buf, print_buf);
350   }
351   sprintf(print_buf,")");
352   trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
353   strcat(trace_buf, print_buf);
354   DEBUG1("%s",trace_buf);
355   trace_buf[0]='\000';
356
357   /* Printing Constraints */
358   cnst_list = &(sys->active_constraint_set);
359   xbt_swag_foreach(cnst, cnst_list) {
360     sum=0.0;
361     elem_list = &(cnst->element_set);
362     sprintf(print_buf,"\t");
363     trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
364     strcat(trace_buf, print_buf);
365     xbt_swag_foreach(elem, elem_list) {
366       sprintf(print_buf,"%f.'%p'(%f) + ",elem->value, 
367               elem->variable,elem->variable->value);
368       trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
369       strcat(trace_buf, print_buf);
370       sum += elem->value * elem->variable->value;
371     }
372     sprintf(print_buf,"0 <= %f ('%p')",cnst->bound,cnst);
373     trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
374     strcat(trace_buf, print_buf);
375
376     if(!cnst->shared) {
377       sprintf(print_buf," [MAX-Constraint]");
378       trace_buf = xbt_realloc(trace_buf,strlen(trace_buf)+strlen(print_buf)+1);
379       strcat(trace_buf, print_buf);
380     }
381     DEBUG1("%s",trace_buf);
382     trace_buf[0]='\000';
383     xbt_assert3((sum<=cnst->bound), "Incorrect value (%f is not smaller than %f): %g",
384                 sum,cnst->bound,sum-cnst->bound);
385   }
386
387   /* Printing Result */
388   xbt_swag_foreach(var, var_list) {
389     if(var->bound>0) {
390       DEBUG4("'%p'(%f) : %f (<=%f)",var,var->weight,var->value, var->bound);
391       xbt_assert0((var->value<=var->bound), "Incorrect value");
392     }
393     else 
394       DEBUG3("'%p'(%f) : %f",var,var->weight,var->value);
395   }
396
397   free(trace_buf);
398 }
399
400 void lmm_solve(lmm_system_t sys)
401 {
402   lmm_variable_t var = NULL;
403   lmm_constraint_t cnst = NULL;
404   lmm_element_t elem = NULL;
405   xbt_swag_t cnst_list = NULL;
406   xbt_swag_t var_list = NULL;
407   xbt_swag_t elem_list = NULL;
408   double min_usage = -1;
409
410   if (!(sys->modified))
411     return;
412
413   /* Init */
414   var_list = &(sys->variable_set);
415   DEBUG1("Variable set : %d", xbt_swag_size(var_list));
416   xbt_swag_foreach(var, var_list) {
417     var->value = 0.0;
418   }
419
420   /* 
421    * Compute Usage and store the variables that reach the maximum.
422    */
423   cnst_list = &(sys->active_constraint_set);
424   DEBUG1("Active constraints : %d", xbt_swag_size(cnst_list));
425   xbt_swag_foreach(cnst, cnst_list) {
426     /* INIT */
427     cnst->remaining = cnst->bound;
428     cnst->usage = 0;
429     elem_list = &(cnst->element_set);
430     cnst->usage = 0.0;
431     xbt_swag_foreach(elem, elem_list) {
432       if(elem->variable->weight <=0) break;
433       if ((elem->value > 0)) {
434         if(cnst->shared)
435           cnst->usage += elem->value / elem->variable->weight;
436         else 
437           if(cnst->usage<elem->value / elem->variable->weight)
438             cnst->usage = elem->value / elem->variable->weight;
439         DEBUG2("Constraint Usage %p : %f",cnst,cnst->usage);
440         make_elem_active(elem);
441       }
442     }
443     /* Saturated constraints update */
444     saturated_constraint_set_update(sys, cnst, &min_usage);
445   }
446   saturated_variable_set_update(sys);
447
448   /* Saturated variables update */
449
450   do {
451     /* Fix the variables that have to be */
452     var_list = &(sys->saturated_variable_set);
453
454     xbt_swag_foreach(var, var_list) {
455       /* First check if some of these variables have reach their upper
456          bound and update min_usage accordingly. */
457       DEBUG5("var=%p, var->bound=%f, var->weight=%f, min_usage=%f, var->bound*var->weight=%f",
458              var, var->bound, var->weight, min_usage,var->bound * var->weight);
459       if ((var->bound > 0) && (var->bound * var->weight < min_usage)) {
460         min_usage = var->bound * var->weight;
461         DEBUG1("Updated min_usage=%f",min_usage);
462       }
463     }
464
465
466     while ((var = xbt_swag_getFirst(var_list))) {
467       int i;
468
469       var->value = min_usage / var->weight;
470       DEBUG5("Min usage: %f, Var(%p)->weight: %f, Var(%p)->value: %f ",min_usage,var,var->weight,var,var->value);
471
472
473       /* Update usage */
474
475       for (i = 0; i < var->cnsts_number; i++) {
476         elem = &var->cnsts[i];
477         cnst = elem->constraint;
478         if(cnst->shared) {
479           double_update(&(cnst->remaining), elem->value * var->value);
480           double_update(&(cnst->usage), elem->value / var->weight);
481           make_elem_inactive(elem);
482         } else { /* FIXME one day: We recompute usage.... :( */
483           cnst->usage = 0.0;
484           make_elem_inactive(elem);
485           xbt_swag_foreach(elem, elem_list) {
486             if(elem->variable->weight <=0) break;
487             if(elem->variable->value > 0) break;
488             if ((elem->value > 0)) {
489               if(cnst->usage<elem->value / elem->variable->weight)
490                 cnst->usage = elem->value / elem->variable->weight;
491               DEBUG2("Constraint Usage %p : %f",cnst,cnst->usage);
492               make_elem_active(elem);
493             }
494           } 
495         }
496       }
497       xbt_swag_remove(var, var_list);
498     }
499
500     /* Find out which variables reach the maximum */
501     cnst_list = &(sys->active_constraint_set);
502     min_usage = -1;
503     xbt_swag_foreach(cnst, cnst_list) {
504       saturated_constraint_set_update(sys, cnst, &min_usage);
505     }
506     saturated_variable_set_update(sys);
507
508   } while (xbt_swag_size(&(sys->saturated_variable_set)));
509
510   sys->modified = 0;
511   if(XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
512     lmm_print(sys);
513   }
514 }
515
516 /* Not a O(1) function */
517
518 void lmm_update(lmm_system_t sys, lmm_constraint_t cnst,
519                 lmm_variable_t var, double value)
520 {
521   int i;
522
523   sys->modified = 1;
524   for (i = 0; i < var->cnsts_number; i++)
525     if (var->cnsts[i].constraint == cnst) {
526       var->cnsts[i].value = value;
527       return;
528     }
529 }
530
531 /** \brief Attribute the value bound to var->bound.
532  * 
533  *  \param sys the lmm_system_t
534  *  \param var the lmm_variable_t
535  *  \param bound the new bound to associate with var
536  * 
537  *  Makes var->bound equal to bound. Whenever this function is called 
538  *  a change is  signed in the system. To
539  *  avoid false system changing detection it is a good idea to test 
540  *  (bound != 0) before calling it.
541  *
542  */
543 void lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
544                                double bound)
545 {
546   sys->modified = 1;
547   var->bound = bound;
548 }
549
550 /** \brief Add the value delta to var->df (the sum of latencies).
551  * 
552  *  \param sys the lmm_system_t associated
553  *  \param var the lmm_variable_t which need to updated
554  *  \param delta the variation of the latency
555  * 
556  *  Add the value delta to var->df (the sum of latencys associated to the
557  *  flow). Whenever this function is called a change is  signed in the system. To
558  *  avoid false system changing detection it is a good idea to test 
559  *  (delta != 0) before calling it.
560  *
561  */
562 void lmm_update_variable_latency(lmm_system_t sys, lmm_variable_t var,
563                                double delta)
564 {
565   sys->modified = 1;
566   var->df += delta;
567 }
568
569 void lmm_update_variable_weight(lmm_system_t sys, lmm_variable_t var,
570                                 double weight)
571 {
572   int i ;
573   lmm_element_t elem;
574
575   XBT_IN3("(sys=%p, var=%p, weight=%f)",sys,var,weight);
576   sys->modified = 1;
577   var->weight = weight;
578   xbt_swag_remove(var,&(sys->variable_set));
579   if(weight) xbt_swag_insert_at_head(var,&(sys->variable_set));
580   else xbt_swag_insert_at_tail(var,&(sys->variable_set));
581
582   for (i = 0; i < var->cnsts_number; i++) {
583     elem = &var->cnsts[i];
584     xbt_swag_remove(elem, &(elem->constraint->element_set));
585     if(weight) xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
586     else xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
587   }
588   XBT_OUT;
589 }
590
591 double lmm_get_variable_weight(lmm_variable_t var)
592                                   
593 {
594   return var->weight;
595 }
596
597 void lmm_update_constraint_bound(lmm_system_t sys, lmm_constraint_t cnst,
598                                  double bound)
599 {
600   sys->modified = 1;
601   cnst->bound = bound;
602 }
603
604 int lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst)
605 {
606   return xbt_swag_belongs(cnst, &(sys->active_constraint_set));
607 }
608
609 lmm_constraint_t lmm_get_first_active_constraint(lmm_system_t sys)
610 {
611   return xbt_swag_getFirst(&(sys->active_constraint_set));
612 }
613
614 lmm_constraint_t lmm_get_next_active_constraint(lmm_system_t sys, lmm_constraint_t cnst)
615 {
616   return xbt_swag_getNext(cnst,(sys->active_constraint_set).offset);
617 }
618
619 /** \brief Attribute the value bound to var->bound.
620  * 
621  *  \param func_f    default function f associated with the chosen protocol flavor
622  *  \param func_fp   partial differential of f (f prime, f')
623  *  \param func_fpi  inverse of the partial differential of f (f prime inverse, (f')^{-1})
624  *  \param func_fpip partial differential of the inverse of the partial differential of f (f prime inverse prime, ((f')^{-1})')
625  * 
626  *  Set default functions to the ones passed as parameters.
627  *
628  */
629 void lmm_set_default_protocol_functions(double (* func_f)    (lmm_variable_t var, double x),
630                                         double (* func_fp)   (lmm_variable_t var, double x),
631                                         double (* func_fpi)  (lmm_variable_t var, double x),
632                                         double (* func_fpip) (lmm_variable_t var, double x))
633                                         
634 {
635   func_f_def    = func_f;
636   func_fp_def   = func_fp;
637   func_fpi_def  = func_fpi;
638   func_fpip_def = func_fpip;
639 }
640
641
642 /*
643  * NOTE for Reno: all functions consider the network
644  * coeficient (alpha) equal to 1.
645  */
646
647 /*
648  * For Reno f: $\alpha_f d_f \log\left(x_f\right)$
649  */
650 double func_reno_f(lmm_variable_t var, double x){
651   xbt_assert0(x,"Please report this bug.");
652   return var->df * log(x);
653 }
654
655 /*
656  * For Reno fp: $\frac{\alpha D_f}{x}$
657  */
658 double func_reno_fp(lmm_variable_t var, double x){
659   xbt_assert0(x,"Please report this bug.");
660   return var->df/x;
661 }
662
663 /*
664  * For Reno fpi: $\frac{\alpha D_f}{x}$
665  */
666 double func_reno_fpi(lmm_variable_t var, double x){
667   xbt_assert0(x,"Please report this bug.");
668   return var->df/x;
669 }
670
671 /*
672  * For Reno fpip: $-\frac{\alpha D_f}{x^2}$
673  */
674 double func_reno_fpip(lmm_variable_t var, double x){
675   xbt_assert0(x,"Please report this bug.");
676   return -( var->df/(x*x) ) ;
677 }
678
679
680 /*
681  * For Vegas f: $\frac{\sqrt{\frac{3}{2}}}{D_f} \arctan\left(\sqrt{\frac{3}{2}}x_f D_f\right)$
682  */
683 double func_vegas_f(lmm_variable_t var, double x){
684   xbt_assert0(x,"Please report this bug.");
685   // \sqrt{3/2} = 0.8164965808
686   return (0.8164965808 / var->df) * atan( (0.8164965808 / var->df)*x );
687 }
688
689 /*
690  * For Vegas fp: $\frac{3{D_f}^2}{3{D_f}^2x^2 + 2}$
691  */
692 double func_vegas_fp(lmm_variable_t var, double x){
693   xbt_assert0(x,"Please report this bug.");
694   return (3*var->df*var->df) / (3*var->df*var->df*x*x + 2);
695 }
696
697 /*
698  * For Vegas fpi: $\sqrt{\frac{1}{x} - \frac{2}{3{D_f}^2}}$
699  */
700 double func_vegas_fpi(lmm_variable_t var, double x){
701   double res_fpi; 
702   xbt_assert0( (x<0.0) ,"Please report this bug.");
703   xbt_assert0( (var->df<0.0), "Please report this bug.");
704   res_fpi = (1/x) - 2/(3*var->df*var->df);
705   return sqrt(res_fpi);
706 }
707
708 /*
709  * For Vegas fpip:  $-\frac{1}{2x^2\sqrt{\frac{1}{x} - \frac{2}{3{D_f}^2}}}$
710  */
711 double func_vegas_fpip(lmm_variable_t var, double x){
712   double res_fpip; 
713   xbt_assert0(x,"Please report this bug.");
714   xbt_assert0( (x<0.0), "Please report this bug.");
715   res_fpip = sqrt(1/x - 2/(3*var->df*var->df));
716   return -(1/(2*x*x*res_fpip));
717 }