1 /* Copyright (c) 2004-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
10 #include "src/portable.h"
12 #include "xbt/asserts.h"
13 #include "surf/datatypes.h"
17 /** @addtogroup SURF_lmm
19 * A linear maxmin solver to resolve inequations systems.
21 * Most SimGrid model rely on a "fluid/steady-state" modeling that
22 * simulate the sharing of resources between actions at relatively
23 * coarse-grain. Such sharing is generally done by solving a set of
24 * linear inequations. Let's take an example and assume we have the
25 * variables \f$x_1\f$, \f$x_2\f$, \f$x_3\f$, and \f$x_4\f$ . Let's
26 * say that \f$x_1\f$ and \f$x_2\f$ correspond to activities running
27 * and the same CPU \f$A\f$ whose capacity is \f$C_A\f$. In such a
28 * case, we need to enforce:
30 * \f[ x_1 + x_2 \leq C_A \f]
32 * Likewise, if \f$x_3\f$ (resp. \f$x_4\f$) corresponds to a network
33 * flow \f$F_3\f$ (resp. \f$F_4\f$) that goes through a set of links
34 * \f$L_1\f$ and \f$L_2\f$ (resp. \f$L_2\f$ and \f$L_3\f$), then we
37 * \f[ x_3 \leq C_{L_1} \f]
38 * \f[ x_3 + x_4 \leq C_{L_2} \f]
39 * \f[ x_4 \leq C_{L_3} \f]
41 * One could set every variable to 0 to make sure the constraints are
42 * satisfied but this would obviously not be very realistic. A
43 * possible objective is to try to maximize the minimum of the
44 * \f$x_i\f$ . This ensures that all the \f$x_i\f$ are positive and "as
47 * This is called *max-min fairness* and is the most commonly used
48 * objective in SimGrid. Another possibility is to maximize
49 * \f$\sum_if(x_i)\f$, where \f$f\f$ is a strictly increasing concave
65 * A possible system could be:
66 * - three variables: `var1`, `var2`, `var3`
67 * - two constraints: `cons1`, `cons2`
68 * - four elements linking:
69 * - `elem1` linking `var1` and `cons1`
70 * - `elem2` linking `var2` and `cons1`
71 * - `elem3` linking `var2` and `cons2`
72 * - `elem4` linking `var3` and `cons2`
74 * And the corresponding inequations will be:
76 * var1.value <= var1.bound
77 * var2.value <= var2.bound
78 * var3.value <= var3.bound
79 * var1.weight * var1.value * elem1.value + var2.weight * var2.value * elem2.value <= cons1.bound
80 * var2.weight * var2.value * elem3.value + var3.weight * var3.value * elem4.value <= cons2.bound
82 * where `var1.value`, `var2.value` and `var3.value` are the unknown values.
84 * If a constraint is not shared, the sum is replaced by a max.
85 * For example, a third non-shared constraint `cons3` and the associated elements `elem5` and `elem6` could write as:
87 * max( var1.weight * var1.value * elem5.value , var3.weight * var3.value * elem6.value ) <= cons3.bound
89 * This is usefull for the sharing of resources for various models.
90 * For instance, for the network model, each link is associated
91 * to a constraint and each communication to a variable.
95 XBT_PUBLIC_DATA(double) sg_maxmin_precision;
96 XBT_PUBLIC_DATA(double) sg_surf_precision;
98 static XBT_INLINE void double_update(double *variable, double value, double precision)
100 //printf("Updating %g -= %g +- %g\n",*variable,value,precision);
101 //xbt_assert(value==0 || value>precision);
102 //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...
103 //xbt_assert(*variable< (2<<DBL_MANT_DIG)*precision && FLT_RADIX==2);
105 if (*variable < precision)
109 static XBT_INLINE int double_positive(double value, double precision)
111 return (value > precision);
114 static XBT_INLINE int double_equals(double value1, double value2, double precision)
116 return (fabs(value1 - value2) < precision);
121 /** @{ @ingroup SURF_lmm */
123 * @brief Create a new Linear MaxMim system
125 * @param selective_update [description]
127 XBT_PUBLIC(lmm_system_t) lmm_system_new(int selective_update);
130 * @brief Free an existing Linear MaxMin system
132 * @param sys The lmm system to free
134 XBT_PUBLIC(void) lmm_system_free(lmm_system_t sys);
137 * @brief Create a new Linear MaxMin constraint
139 * @param sys The system in which we add a constraint
140 * @param id Data associated to the constraint (e.g.: a network link)
141 * @param bound_value The bound value of the constraint
143 XBT_PUBLIC(lmm_constraint_t) lmm_constraint_new(lmm_system_t sys, void *id,
147 * @brief Share a constraint
148 * @details [long description]
150 * @param cnst The constraint to share
152 XBT_PUBLIC(void) lmm_constraint_shared(lmm_constraint_t cnst);
155 * @brief Check if a constraint is shared (shared by default)
157 * @param cnst The constraint to share
158 * @return 1 if shared, 0 otherwise
160 XBT_PUBLIC(int) lmm_constraint_sharing_policy(lmm_constraint_t cnst);
163 * @brief Free a constraint
165 * @param sys The system associated to the constraint
166 * @param cnst The constraint to free
168 XBT_PUBLIC(void) lmm_constraint_free(lmm_system_t sys, lmm_constraint_t cnst);
171 * @brief Get the usage of the constraint after the last lmm solve
173 * @param cnst A constraint
174 * @return The usage of the constraint
176 XBT_PUBLIC(double) lmm_constraint_get_usage(lmm_constraint_t cnst);
179 * @brief Create a new Linear MaxMin variable
181 * @param sys The system in which we add a constaint
182 * @param id Data associated to the variable (e.g.: a network communication)
183 * @param weight_value The weight of the variable (0.0 if not used)
184 * @param bound The maximum value of the variable (-1.0 if no maximum value)
185 * @param number_of_constraints The maximum number of constraint to associate to the variable
187 XBT_PUBLIC(lmm_variable_t) lmm_variable_new(lmm_system_t sys, void *id,
190 int number_of_constraints);
192 * @brief Free a variable
194 * @param sys The system associated to the variable
195 * @param var The variable to free
197 XBT_PUBLIC(void) lmm_variable_free(lmm_system_t sys, lmm_variable_t var);
200 * @brief Get the value of the variable after the last lmm solve
202 * @param var A variable
203 * @return The value of the variable
205 XBT_PUBLIC(double) lmm_variable_getvalue(lmm_variable_t var);
208 * @brief Get the maximum value of the variable (-1.0 if no maximum value)
210 * @param var A variable
211 * @return The bound of the variable
213 XBT_PUBLIC(double) lmm_variable_getbound(lmm_variable_t var);
216 * @brief Remove a variable from a constraint
218 * @param sys A system
219 * @param cnst A constraint
220 * @param var The variable to remove
222 XBT_PUBLIC(void) lmm_shrink(lmm_system_t sys, lmm_constraint_t cnst,
226 * @brief Associate a variable to a constraint with a coefficient
228 * @param sys A system
229 * @param cnst A constraint
230 * @param var A variable
231 * @param value The coefficient associated to the variable in the constraint
233 XBT_PUBLIC(void) lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
234 lmm_variable_t var, double value);
237 * @brief Add value to the coefficient between a constraint and a variable or
240 * @param sys A system
241 * @param cnst A constraint
242 * @param var A variable
243 * @param value The value to add to the coefficient associated to the variable in the constraint
245 XBT_PUBLIC(void) lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst,
246 lmm_variable_t var, double value);
249 * @brief Get the numth constraint associated to the variable
251 * @param sys The system associated to the variable (not used)
252 * @param var A variable
253 * @param num The rank of constraint we want to get
254 * @return The numth constraint
256 XBT_PUBLIC(lmm_constraint_t) lmm_get_cnst_from_var(lmm_system_t sys,
257 lmm_variable_t var, int num);
260 * @brief Get the weigth of the numth constraint associated to the variable
262 * @param sys The system associated to the variable (not used)
263 * @param var A variable
264 * @param num The rank of constraint we want to get
265 * @return The numth constraint
267 XBT_PUBLIC(double) lmm_get_cnst_weight_from_var(lmm_system_t sys, lmm_variable_t var,
271 * @brief Get the number of constraint associated to a variable
273 * @param sys The system associated to the variable (not used)
274 * @param var A variable
275 * @return The number of constraint associated to the variable
277 XBT_PUBLIC(int) lmm_get_number_of_cnst_from_var(lmm_system_t sys, lmm_variable_t var);
280 * @brief Get a var associated to a constraint
281 * @details Get the first variable of the next variable of elem if elem is not NULL
283 * @param sys The system associated to the variable (not used)
284 * @param cnst A constraint
285 * @param elem A element of constraint of the constraint or NULL
286 * @return A variable associated to a constraint
288 XBT_PUBLIC(lmm_variable_t) lmm_get_var_from_cnst(lmm_system_t sys,
289 lmm_constraint_t cnst,
290 lmm_element_t * elem);
293 * @brief Get a var associated to a constraint
294 * @details Get the first variable of the next variable of elem if elem is not NULL
296 * @param cnst A constraint
297 * @param elem A element of constraint of the constraint or NULL
298 * @param nextelem A element of constraint of the constraint or NULL, the one after elem
299 * @param numelem parameter representing the number of elements to go
301 * @return A variable associated to a constraint
303 XBT_PUBLIC(lmm_variable_t) lmm_get_var_from_cnst_safe(lmm_system_t /*sys*/,
304 lmm_constraint_t cnst,
305 lmm_element_t * elem,
306 lmm_element_t * nextelem,
310 * @brief Get the first active constraint of a system
312 * @param sys A system
313 * @return The first active constraint
315 XBT_PUBLIC(lmm_constraint_t) lmm_get_first_active_constraint(lmm_system_t sys);
318 * @brief Get the next active constraint of a constraint in a system
320 * @param sys A system
321 * @param cnst An active constraint of the system
323 * @return The next active constraint
325 XBT_PUBLIC(lmm_constraint_t) lmm_get_next_active_constraint(lmm_system_t sys,
326 lmm_constraint_t cnst);
328 #ifdef HAVE_LATENCY_BOUND_TRACKING
329 XBT_PUBLIC(int) lmm_is_variable_limited_by_latency(lmm_variable_t var);
333 * @brief Get the data associated to a constraint
335 * @param cnst A constraint
336 * @return The data associated to the constraint
338 XBT_PUBLIC(void *) lmm_constraint_id(lmm_constraint_t cnst);
341 * @brief Get the data associated to a variable
343 * @param var A variable
344 * @return The data associated to the variable
346 XBT_PUBLIC(void *) lmm_variable_id(lmm_variable_t var);
349 * @brief Update the value of element linking the constraint and the variable
351 * @param sys A system
352 * @param cnst A constraint
353 * @param var A variable
354 * @param value The new value
356 XBT_PUBLIC(void) lmm_update(lmm_system_t sys, lmm_constraint_t cnst,
357 lmm_variable_t var, double value);
360 * @brief Update the bound of a variable
362 * @param sys A system
363 * @param var A constraint
364 * @param bound The new bound
366 XBT_PUBLIC(void) lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
370 * @brief Update the weight of a variable
372 * @param sys A system
373 * @param var A variable
374 * @param weight The new weight of the variable
376 XBT_PUBLIC(void) lmm_update_variable_weight(lmm_system_t sys,
381 * @brief Get the weight of a variable
383 * @param var A variable
384 * @return The weight of the variable
386 XBT_PUBLIC(double) lmm_get_variable_weight(lmm_variable_t var);
389 * @brief Update a constraint bound
391 * @param sys A system
392 * @param cnst A constraint
393 * @param bound The new bound of the consrtaint
395 XBT_PUBLIC(void) lmm_update_constraint_bound(lmm_system_t sys,
396 lmm_constraint_t cnst,
400 * @brief [brief description]
402 * @param sys A system
403 * @param cnst A constraint
404 * @return [description]
406 XBT_PUBLIC(int) lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst);
409 * @brief Solve the lmm system
411 * @param sys The lmm system to solve
413 XBT_PUBLIC(void) lmm_solve(lmm_system_t sys);
415 XBT_PUBLIC(void) lagrange_solve(lmm_system_t sys);
416 XBT_PUBLIC(void) bottleneck_solve(lmm_system_t sys);
419 * Default functions associated to the chosen protocol. When
420 * using the lagrangian approach.
423 XBT_PUBLIC(void) lmm_set_default_protocol_function(double (*func_f)
433 XBT_PUBLIC(double func_reno_f) (lmm_variable_t var, double x);
434 XBT_PUBLIC(double func_reno_fp) (lmm_variable_t var, double x);
435 XBT_PUBLIC(double func_reno_fpi) (lmm_variable_t var, double x);
437 XBT_PUBLIC(double func_reno2_f) (lmm_variable_t var, double x);
438 XBT_PUBLIC(double func_reno2_fp) (lmm_variable_t var, double x);
439 XBT_PUBLIC(double func_reno2_fpi) (lmm_variable_t var, double x);
441 XBT_PUBLIC(double func_vegas_f) (lmm_variable_t var, double x);
442 XBT_PUBLIC(double func_vegas_fp) (lmm_variable_t var, double x);
443 XBT_PUBLIC(double func_vegas_fpi) (lmm_variable_t var, double x);
448 #endif /* _SURF_MAXMIN_H */