Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2239f4a6a2823fb4bd64b6295f2f005df1c874ed
[simgrid.git] / src / include / surf / maxmin.h
1 /* Copyright (c) 2004-2015. 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 #ifndef _SURF_MAXMIN_H
8 #define _SURF_MAXMIN_H
9
10 #include "portable.h"
11 #include "xbt/misc.h"
12 #include "xbt/asserts.h"
13 #include "surf/datatypes.h"
14 #include <math.h>
15 //#include <float.h>
16
17
18 /** @addtogroup SURF_lmm 
19  * @details 
20  * A linear maxmin solver to resolves inequations systems.
21  * 
22  * Most SimGrid model rely on a "fluid/steady-state" modeling that
23  * samount to share resources between actions at relatively
24  * coarse-grain.  Such sharing is generally done by solving a set of
25  * linear inequations. Let's take an example and assume we have the
26  * variables \f$x_1\f$, \f$x_2\f$, \f$x_3\f$, and \f$x_4\f$ . Let's
27  * say that \f$x_1\f$ and \f$x_2\f$ correspond to activities running
28  * and the same CPU \f$A\f$ whose capacity is \f$C_A\f$ . In such a
29  * case, we need to enforce:
30  *
31  *   \f[ x_1 + x_2 \leq C_A \f]
32  *
33  * Likewise, if \f$x_3\f$ (resp. \f$x_4\f$) corresponds to a network
34  * flow \f$F_3\f$ (resp. \f$F_4\f$) that goes through a set of links
35  * \f$L_1\f$ and \f$L_2\f$ (resp. \f$L_2\f$ and \f$L_3\f$), then we
36  * need to enforce:
37  *
38  *   \f[ x_3  \leq C_{L_1} \f]
39  *   \f[ x_3 + x_4 \leq C_{L_2} \f]
40  *   \f[ x_4 \leq C_{L_3} \f]
41  * 
42  * One could set every variable to 0 to make sure the constraints are
43  * satisfied but this would obviously not be very realistic. A
44  * possible objective is to try to maximize the minimum of the
45  * \f$x_i\f$ . This ensures that all the \f$x_i\f$ are positive and "as
46  * large as possible". 
47  *
48  * This is called *max-min fairness* and is the most commonly used
49  * objective in SimGrid. Another possibility is to maximize
50  * \f$\sum_if(x_i)\f$, where \f$f\f$ is a strictly increasing concave
51  * function.
52  *
53  * Constraint: 
54  *  - bound (set)
55  *  - shared (set)
56  *  - usage (computed)
57  * Variable:
58  *  - weight (set)
59  *  - bound (set)
60  *  - value (computed)
61  * Element:
62  *  - value (set)
63  * 
64  * A possible system could be:
65  * - three variables: `var1`, `var2`, `var3`
66  * - two constraints: `cons1`, `cons2`
67  * - four elements linking:
68  *  - `elem1` linking `var1` and `cons1`
69  *  - `elem2` linking `var2` and `cons1`
70  *  - `elem3` linking `var2` and `cons2`
71  *  - `elem4` linking `var3` and `cons2`
72  * 
73  * And the corresponding inequations will be:
74  * 
75  *     var1.value <= var1.bound
76  *     var2.value <= var2.bound
77  *     var3.value <= var3.bound
78  *     var1.weight * var1.value * elem1.value + var2.weight * var2.value * elem2.value <= cons1.bound
79  *     var2.weight * var2.value * elem3.value + var3.weight * var3.value * elem4.value <= cons2.bound
80  * 
81  * where `var1.value`, `var2.value` and `var3.value` are the unknown values
82  * 
83  * if a constraint is not shared the sum is replace by a max
84  * 
85  * Its usefull for the sharing of resources for various models.
86  * For instance for the network model the link are associated 
87  * to consrtaint and the communications to variables.
88  */
89
90 extern double sg_maxmin_precision;
91 extern double sg_surf_precision;
92  
93 static XBT_INLINE void double_update(double *variable, double value, double precision)
94 {
95   //printf("Updating %g -= %g +- %g\n",*variable,value,precision);
96   //xbt_assert(value==0  || value>precision);
97   //Check that precision is higher than the machine-dependent size of the mantissa. If not, brutal rounding  may happen, and the precision mechanism is not active... 
98   //xbt_assert(*variable< (2<<DBL_MANT_DIG)*precision && FLT_RADIX==2);
99   *variable -= value;
100   if (*variable < precision)
101     *variable = 0.0;
102 }
103
104 static XBT_INLINE int double_positive(double value, double precision)
105 {
106   return (value > precision);
107 }
108
109 static XBT_INLINE int double_equals(double value1, double value2, double precision)
110 {
111   return (fabs(value1 - value2) < precision);
112 }
113
114 SG_BEGIN_DECL()
115
116 /** @{ @ingroup SURF_lmm */
117 /**
118  * @brief Create a new Linear MaxMim system
119  * 
120  * @param selective_update [description]
121  */
122 XBT_PUBLIC(lmm_system_t) lmm_system_new(int selective_update);
123
124 /**
125  * @brief Free an existing Linear MaxMin system
126  * 
127  * @param sys The lmm system to free
128  */
129 XBT_PUBLIC(void) lmm_system_free(lmm_system_t sys);
130
131 /**
132  * @brief Create a new Linear MaxMin constraint
133  * 
134  * @param sys The system in which we add a constraint
135  * @param id Data associated to the constraint (e.g.: a network link)
136  * @param bound_value The bound value of the constraint 
137  */
138 XBT_PUBLIC(lmm_constraint_t) lmm_constraint_new(lmm_system_t sys, void *id,
139                                                 double bound_value);
140
141 /**
142  * @brief Share a constraint
143  * @details [long description]
144  * 
145  * @param cnst The constraint to share
146  */
147 XBT_PUBLIC(void) lmm_constraint_shared(lmm_constraint_t cnst);
148
149 /**
150  * @brief Check if a constraint is shared (shared by default)
151  * 
152  * @param cnst The constraint to share
153  * @return 1 if shared, 0 otherwise
154  */
155 XBT_PUBLIC(int) lmm_constraint_is_shared(lmm_constraint_t cnst);
156
157 /**
158  * @brief Free a constraint
159  * 
160  * @param sys The system associated to the constraint
161  * @param cnst The constraint to free
162  */
163 XBT_PUBLIC(void) lmm_constraint_free(lmm_system_t sys, lmm_constraint_t cnst);
164
165 /**
166  * @brief Get the usage of the constraint after the last lmm solve
167  * 
168  * @param cnst A constraint
169  * @return The usage of the constraint
170  */
171 XBT_PUBLIC(double) lmm_constraint_get_usage(lmm_constraint_t cnst);
172
173 /**
174  * @brief Create a new Linear MaxMin variable
175  * 
176  * @param sys The system in which we add a constaint
177  * @param id Data associated to the variable (e.g.: a network communication)
178  * @param weight_value The weight of the variable (0.0 if not used)
179  * @param bound The maximum value of the variable (-1.0 if no maximum value)
180  * @param number_of_constraints The maximum number of constraint to associate to the variable
181  */
182 XBT_PUBLIC(lmm_variable_t) lmm_variable_new(lmm_system_t sys, void *id,
183                                             double weight_value,
184                                             double bound,
185                                             int number_of_constraints);
186 /**
187  * @brief Free a variable
188  * 
189  * @param sys The system associated to the variable
190  * @param var The variable to free
191  */
192 XBT_PUBLIC(void) lmm_variable_free(lmm_system_t sys, lmm_variable_t var);
193
194 /**
195  * @brief Get the value of the variable after the last lmm solve
196  * 
197  * @param var A variable
198  * @return The value of the variable
199  */
200 XBT_PUBLIC(double) lmm_variable_getvalue(lmm_variable_t var);
201
202 /**
203  * @brief Get the maximum value of the variable (-1.0 if no maximum value)
204  * 
205  * @param var A variable
206  * @return The bound of the variable
207  */
208 XBT_PUBLIC(double) lmm_variable_getbound(lmm_variable_t var);
209
210 /**
211  * @brief Remove a variable from a constraint
212  * 
213  * @param sys A system
214  * @param cnst A constraint
215  * @param var The variable to remove
216  */
217 XBT_PUBLIC(void) lmm_shrink(lmm_system_t sys, lmm_constraint_t cnst,
218                             lmm_variable_t var);
219
220 /**
221  * @brief Associate a variable to a constraint with a coefficient
222  * 
223  * @param sys A system
224  * @param cnst A constraint
225  * @param var A variable
226  * @param value The coefficient associated to the variable in the constraint
227  */
228 XBT_PUBLIC(void) lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
229                             lmm_variable_t var, double value);
230
231 /**
232  * @brief Add value to the coefficient between a constraint and a variable or 
233  *        create one
234  * 
235  * @param sys A system
236  * @param cnst A constraint
237  * @param var A variable
238  * @param value The value to add to the coefficient associated to the variable in the constraint
239  */
240 XBT_PUBLIC(void) lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst,
241                     lmm_variable_t var, double value);
242
243 /**
244  * @brief Get the numth constraint associated to the variable
245  * 
246  * @param sys The system associated to the variable (not used)
247  * @param var A variable
248  * @param num The rank of constraint we want to get
249  * @return The numth constraint
250  */
251 XBT_PUBLIC(lmm_constraint_t) lmm_get_cnst_from_var(lmm_system_t sys,
252                                        lmm_variable_t var, int num);
253
254 /**
255  * @brief Get the weigth of the numth constraint associated to the variable
256  * 
257  * @param sys The system associated to the variable (not used)
258  * @param var A variable
259  * @param num The rank of constraint we want to get
260  * @return The numth constraint
261  */
262 XBT_PUBLIC(double) lmm_get_cnst_weight_from_var(lmm_system_t sys, lmm_variable_t var,
263                                     int num);
264
265 /**
266  * @brief Get the number of constraint associated to a variable
267  * 
268  * @param sys The system associated to the variable (not used)
269  * @param var A variable
270  * @return The number of constraint associated to the variable
271  */
272 XBT_PUBLIC(int) lmm_get_number_of_cnst_from_var(lmm_system_t sys, lmm_variable_t var);
273
274 /**
275  * @brief Get a var associated to a constraint 
276  * @details Get the first variable of the next variable of elem if elem is not NULL
277  * 
278  * @param sys The system associated to the variable (not used)
279  * @param cnst A constraint
280  * @param elem A element of constraint of the constraint or NULL
281  * @return A variable associated to a constraint
282  */
283 XBT_PUBLIC(lmm_variable_t) lmm_get_var_from_cnst(lmm_system_t sys,
284                                      lmm_constraint_t cnst,
285                                      lmm_element_t * elem);
286
287 /**
288  * @brief Get a var associated to a constraint
289  * @details Get the first variable of the next variable of elem if elem is not NULL
290  *
291  * @param cnst A constraint
292  * @param elem A element of constraint of the constraint or NULL
293  * @param nextelem A element of constraint of the constraint or NULL, the one after elem
294  * @param numelem parameter representing the number of elements to go
295  *
296  * @return A variable associated to a constraint
297  */
298 XBT_PUBLIC(lmm_variable_t) lmm_get_var_from_cnst_safe(lmm_system_t /*sys*/,
299                                      lmm_constraint_t cnst,
300                                      lmm_element_t * elem,
301                                      lmm_element_t * nextelem,
302                                      int * numelem);
303
304 /**
305  * @brief Get the first active constraint of a system
306  * 
307  * @param sys A system
308  * @return The first active constraint
309  */
310 XBT_PUBLIC(lmm_constraint_t) lmm_get_first_active_constraint(lmm_system_t sys);
311
312 /**
313  * @brief Get the next active constraint of a constraint in a system
314  * 
315  * @param sys A system
316  * @param cnst An active constraint of the system
317  * 
318  * @return The next active constraint
319  */
320 XBT_PUBLIC(lmm_constraint_t) lmm_get_next_active_constraint(lmm_system_t sys,
321                                                 lmm_constraint_t cnst);
322
323 #ifdef HAVE_LATENCY_BOUND_TRACKING
324 XBT_PUBLIC(int) lmm_is_variable_limited_by_latency(lmm_variable_t var);
325 #endif
326
327 /**
328  * @brief Get the data associated to a constraint
329  * 
330  * @param cnst A constraint
331  * @return The data associated to the constraint
332  */
333 XBT_PUBLIC(void *) lmm_constraint_id(lmm_constraint_t cnst);
334
335 /**
336  * @brief Get the data associated to a variable
337  * 
338  * @param var A variable
339  * @return The data associated to the variable
340  */
341 XBT_PUBLIC(void *) lmm_variable_id(lmm_variable_t var);
342
343 /**
344  * @brief Update the value of element linking the constraint and the variable
345  * 
346  * @param sys A system
347  * @param cnst A constraint
348  * @param var A variable
349  * @param value The new value
350  */
351 XBT_PUBLIC(void) lmm_update(lmm_system_t sys, lmm_constraint_t cnst,
352                 lmm_variable_t var, double value);
353
354 /**
355  * @brief Update the bound of a variable
356  * 
357  * @param sys A system
358  * @param var A constraint
359  * @param bound The new bound
360  */
361 XBT_PUBLIC(void) lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
362                                double bound);
363
364 /**
365  * @brief Update the weight of a variable
366  * 
367  * @param sys A system
368  * @param var A variable
369  * @param weight The new weight of the variable
370  */
371 XBT_PUBLIC(void) lmm_update_variable_weight(lmm_system_t sys,
372                                             lmm_variable_t var,
373                                             double weight);
374
375 /**
376  * @brief Get the weight of a variable
377  * 
378  * @param var A variable
379  * @return The weight of the variable
380  */
381 XBT_PUBLIC(double) lmm_get_variable_weight(lmm_variable_t var);
382
383 /**
384  * @brief Update a constraint bound
385  * 
386  * @param sys A system
387  * @param cnst A constraint
388  * @param bound The new bound of the consrtaint
389  */
390 XBT_PUBLIC(void) lmm_update_constraint_bound(lmm_system_t sys,
391                                              lmm_constraint_t cnst,
392                                              double bound);
393
394 /**
395  * @brief [brief description]
396  * 
397  * @param sys A system
398  * @param cnst A constraint
399  * @return [description]
400  */
401 XBT_PUBLIC(int) lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst);
402
403 /**
404  * @brief Solve the lmm system
405  * 
406  * @param sys The lmm system to solve
407  */
408 XBT_PUBLIC(void) lmm_solve(lmm_system_t sys);
409
410 XBT_PUBLIC(void) lagrange_solve(lmm_system_t sys);
411 XBT_PUBLIC(void) bottleneck_solve(lmm_system_t sys);
412
413 /**
414  * Default functions associated to the chosen protocol. When
415  * using the lagrangian approach.
416  */
417
418 XBT_PUBLIC(void) lmm_set_default_protocol_function(double (*func_f)
419                                                     (lmm_variable_t var,
420                                                      double x),
421                                                    double (*func_fp)
422                                                     (lmm_variable_t var,
423                                                      double x),
424                                                    double (*func_fpi)
425                                                     (lmm_variable_t var,
426                                                      double x));
427
428 XBT_PUBLIC(double func_reno_f) (lmm_variable_t var, double x);
429 XBT_PUBLIC(double func_reno_fp) (lmm_variable_t var, double x);
430 XBT_PUBLIC(double func_reno_fpi) (lmm_variable_t var, double x);
431
432 XBT_PUBLIC(double func_reno2_f) (lmm_variable_t var, double x);
433 XBT_PUBLIC(double func_reno2_fp) (lmm_variable_t var, double x);
434 XBT_PUBLIC(double func_reno2_fpi) (lmm_variable_t var, double x);
435
436 XBT_PUBLIC(double func_vegas_f) (lmm_variable_t var, double x);
437 XBT_PUBLIC(double func_vegas_fp) (lmm_variable_t var, double x);
438 XBT_PUBLIC(double func_vegas_fpi) (lmm_variable_t var, double x);
439
440 /** @} */
441 SG_END_DECL()
442
443 #endif                          /* _SURF_MAXMIN_H */