Logo AND Algorithmique Numérique Distribuée

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