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. */
12 #include "xbt/asserts.h"
13 #include "surf/datatypes.h"
17 /** @addtogroup SURF_lmm
19 * A linear maxmin solver to resolves inequations systems.
21 * Most SimGrid model rely on a "fluid/steady-state" modeling that
22 * samount to share 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
63 * A possible system could be:
64 * - three variables: `var1`, `var2`, `var3`
65 * - two constraints: `cons1`, `cons2`
66 * - four elements linking:
67 * - `elem1` linking `var1` and `cons1`
68 * - `elem2` linking `var2` and `cons1`
69 * - `elem3` linking `var2` and `cons2`
70 * - `elem4` linking `var3` and `cons2`
72 * And the corresponding inequations will be:
74 * var1.value <= var1.bound
75 * var2.value <= var2.bound
76 * var3.value <= var3.bound
77 * var1.weight * var1.value * elem1.value + var2.weight * var2.value * elem2.value <= cons1.bound
78 * var2.weight * var2.value * elem3.value + var3.weight * var3.value * elem4.value <= cons2.bound
80 * where `var1.value`, `var2.value` and `var3.value` are the unknown values
82 * if a constraint is not shared the sum is replace by a max
84 * Its usefull for the sharing of resources for various models.
85 * For instance for the network model the link are associated
86 * to consrtaint and the communications to variables.
89 XBT_PUBLIC_DATA(double) sg_maxmin_precision;
90 XBT_PUBLIC_DATA(double) sg_surf_precision;
92 static XBT_INLINE void double_update(double *variable, double value, double precision)
94 //printf("Updating %g -= %g +- %g\n",*variable,value,precision);
95 //xbt_assert(value==0 || value>precision);
96 //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...
97 //xbt_assert(*variable< (2<<DBL_MANT_DIG)*precision && FLT_RADIX==2);
99 if (*variable < precision)
103 static XBT_INLINE int double_positive(double value, double precision)
105 return (value > precision);
108 static XBT_INLINE int double_equals(double value1, double value2, double precision)
110 return (fabs(value1 - value2) < precision);
115 /** @{ @ingroup SURF_lmm */
117 * @brief Create a new Linear MaxMim system
119 * @param selective_update [description]
121 XBT_PUBLIC(lmm_system_t) lmm_system_new(int selective_update);
124 * @brief Free an existing Linear MaxMin system
126 * @param sys The lmm system to free
128 XBT_PUBLIC(void) lmm_system_free(lmm_system_t sys);
131 * @brief Create a new Linear MaxMin constraint
133 * @param sys The system in which we add a constraint
134 * @param id Data associated to the constraint (e.g.: a network link)
135 * @param bound_value The bound value of the constraint
137 XBT_PUBLIC(lmm_constraint_t) lmm_constraint_new(lmm_system_t sys, void *id,
141 * @brief Share a constraint
142 * @details [long description]
144 * @param cnst The constraint to share
146 XBT_PUBLIC(void) lmm_constraint_shared(lmm_constraint_t cnst);
149 * @brief Check if a constraint is shared (shared by default)
151 * @param cnst The constraint to share
152 * @return 1 if shared, 0 otherwise
154 XBT_PUBLIC(int) lmm_constraint_is_shared(lmm_constraint_t cnst);
157 * @brief Free a constraint
159 * @param sys The system associated to the constraint
160 * @param cnst The constraint to free
162 XBT_PUBLIC(void) lmm_constraint_free(lmm_system_t sys, lmm_constraint_t cnst);
165 * @brief Get the usage of the constraint after the last lmm solve
167 * @param cnst A constraint
168 * @return The usage of the constraint
170 XBT_PUBLIC(double) lmm_constraint_get_usage(lmm_constraint_t cnst);
173 * @brief Create a new Linear MaxMin variable
175 * @param sys The system in which we add a constaint
176 * @param id Data associated to the variable (e.g.: a network communication)
177 * @param weight_value The weight of the variable (0.0 if not used)
178 * @param bound The maximum value of the variable (-1.0 if no maximum value)
179 * @param number_of_constraints The maximum number of constraint to associate to the variable
181 XBT_PUBLIC(lmm_variable_t) lmm_variable_new(lmm_system_t sys, void *id,
184 int number_of_constraints);
186 * @brief Free a variable
188 * @param sys The system associated to the variable
189 * @param var The variable to free
191 XBT_PUBLIC(void) lmm_variable_free(lmm_system_t sys, lmm_variable_t var);
194 * @brief Get the value of the variable after the last lmm solve
196 * @param var A variable
197 * @return The value of the variable
199 XBT_PUBLIC(double) lmm_variable_getvalue(lmm_variable_t var);
202 * @brief Get the maximum value of the variable (-1.0 if no maximum value)
204 * @param var A variable
205 * @return The bound of the variable
207 XBT_PUBLIC(double) lmm_variable_getbound(lmm_variable_t var);
210 * @brief Remove a variable from a constraint
212 * @param sys A system
213 * @param cnst A constraint
214 * @param var The variable to remove
216 XBT_PUBLIC(void) lmm_shrink(lmm_system_t sys, lmm_constraint_t cnst,
220 * @brief Associate a variable to a constraint with a coefficient
222 * @param sys A system
223 * @param cnst A constraint
224 * @param var A variable
225 * @param value The coefficient associated to the variable in the constraint
227 XBT_PUBLIC(void) lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
228 lmm_variable_t var, double value);
231 * @brief Add value to the coefficient between a constraint and a variable or
234 * @param sys A system
235 * @param cnst A constraint
236 * @param var A variable
237 * @param value The value to add to the coefficient associated to the variable in the constraint
239 XBT_PUBLIC(void) lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst,
240 lmm_variable_t var, double value);
243 * @brief Get the numth constraint associated to the variable
245 * @param sys The system associated to the variable (not used)
246 * @param var A variable
247 * @param num The rank of constraint we want to get
248 * @return The numth constraint
250 XBT_PUBLIC(lmm_constraint_t) lmm_get_cnst_from_var(lmm_system_t sys,
251 lmm_variable_t var, int num);
254 * @brief Get the weigth of the numth constraint associated to the variable
256 * @param sys The system associated to the variable (not used)
257 * @param var A variable
258 * @param num The rank of constraint we want to get
259 * @return The numth constraint
261 XBT_PUBLIC(double) lmm_get_cnst_weight_from_var(lmm_system_t sys, lmm_variable_t var,
265 * @brief Get the number of constraint associated to a variable
267 * @param sys The system associated to the variable (not used)
268 * @param var A variable
269 * @return The number of constraint associated to the variable
271 XBT_PUBLIC(int) lmm_get_number_of_cnst_from_var(lmm_system_t sys, lmm_variable_t var);
274 * @brief Get a var associated to a constraint
275 * @details Get the first variable of the next variable of elem if elem is not NULL
277 * @param sys The system associated to the variable (not used)
278 * @param cnst A constraint
279 * @param elem A element of constraint of the constraint or NULL
280 * @return A variable associated to a constraint
282 XBT_PUBLIC(lmm_variable_t) lmm_get_var_from_cnst(lmm_system_t sys,
283 lmm_constraint_t cnst,
284 lmm_element_t * elem);
287 * @brief Get a var associated to a constraint
288 * @details Get the first variable of the next variable of elem if elem is not NULL
290 * @param cnst A constraint
291 * @param elem A element of constraint of the constraint or NULL
292 * @param nextelem A element of constraint of the constraint or NULL, the one after elem
293 * @param numelem parameter representing the number of elements to go
295 * @return A variable associated to a constraint
297 XBT_PUBLIC(lmm_variable_t) lmm_get_var_from_cnst_safe(lmm_system_t /*sys*/,
298 lmm_constraint_t cnst,
299 lmm_element_t * elem,
300 lmm_element_t * nextelem,
304 * @brief Get the first active constraint of a system
306 * @param sys A system
307 * @return The first active constraint
309 XBT_PUBLIC(lmm_constraint_t) lmm_get_first_active_constraint(lmm_system_t sys);
312 * @brief Get the next active constraint of a constraint in a system
314 * @param sys A system
315 * @param cnst An active constraint of the system
317 * @return The next active constraint
319 XBT_PUBLIC(lmm_constraint_t) lmm_get_next_active_constraint(lmm_system_t sys,
320 lmm_constraint_t cnst);
322 #ifdef HAVE_LATENCY_BOUND_TRACKING
323 XBT_PUBLIC(int) lmm_is_variable_limited_by_latency(lmm_variable_t var);
327 * @brief Get the data associated to a constraint
329 * @param cnst A constraint
330 * @return The data associated to the constraint
332 XBT_PUBLIC(void *) lmm_constraint_id(lmm_constraint_t cnst);
335 * @brief Get the data associated to a variable
337 * @param var A variable
338 * @return The data associated to the variable
340 XBT_PUBLIC(void *) lmm_variable_id(lmm_variable_t var);
343 * @brief Update the value of element linking the constraint and the variable
345 * @param sys A system
346 * @param cnst A constraint
347 * @param var A variable
348 * @param value The new value
350 XBT_PUBLIC(void) lmm_update(lmm_system_t sys, lmm_constraint_t cnst,
351 lmm_variable_t var, double value);
354 * @brief Update the bound of a variable
356 * @param sys A system
357 * @param var A constraint
358 * @param bound The new bound
360 XBT_PUBLIC(void) lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
364 * @brief Update the weight of a variable
366 * @param sys A system
367 * @param var A variable
368 * @param weight The new weight of the variable
370 XBT_PUBLIC(void) lmm_update_variable_weight(lmm_system_t sys,
375 * @brief Get the weight of a variable
377 * @param var A variable
378 * @return The weight of the variable
380 XBT_PUBLIC(double) lmm_get_variable_weight(lmm_variable_t var);
383 * @brief Update a constraint bound
385 * @param sys A system
386 * @param cnst A constraint
387 * @param bound The new bound of the consrtaint
389 XBT_PUBLIC(void) lmm_update_constraint_bound(lmm_system_t sys,
390 lmm_constraint_t cnst,
394 * @brief [brief description]
396 * @param sys A system
397 * @param cnst A constraint
398 * @return [description]
400 XBT_PUBLIC(int) lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst);
403 * @brief Solve the lmm system
405 * @param sys The lmm system to solve
407 XBT_PUBLIC(void) lmm_solve(lmm_system_t sys);
409 XBT_PUBLIC(void) lagrange_solve(lmm_system_t sys);
410 XBT_PUBLIC(void) bottleneck_solve(lmm_system_t sys);
413 * Default functions associated to the chosen protocol. When
414 * using the lagrangian approach.
417 XBT_PUBLIC(void) lmm_set_default_protocol_function(double (*func_f)
427 XBT_PUBLIC(double func_reno_f) (lmm_variable_t var, double x);
428 XBT_PUBLIC(double func_reno_fp) (lmm_variable_t var, double x);
429 XBT_PUBLIC(double func_reno_fpi) (lmm_variable_t var, double x);
431 XBT_PUBLIC(double func_reno2_f) (lmm_variable_t var, double x);
432 XBT_PUBLIC(double func_reno2_fp) (lmm_variable_t var, double x);
433 XBT_PUBLIC(double func_reno2_fpi) (lmm_variable_t var, double x);
435 XBT_PUBLIC(double func_vegas_f) (lmm_variable_t var, double x);
436 XBT_PUBLIC(double func_vegas_fp) (lmm_variable_t var, double x);
437 XBT_PUBLIC(double func_vegas_fpi) (lmm_variable_t var, double x);
442 #endif /* _SURF_MAXMIN_H */