Logo AND Algorithmique Numérique Distribuée

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