1 /* Copyright (c) 2004-2014. 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 "surf/datatypes.h"
16 /** @addtogroup SURF_lmm
18 * A linear maxmin solver to resolves inequations systems.
20 * A system is composed of variables, constraints and elements linking them.
32 * A possible system could be:
33 * - three variables: `var1`, `var2`, `var3`
34 * - two constraints: `cons1`, `cons2`
35 * - four elements linking:
36 * - `elem1` linking `var1` and `cons1`
37 * - `elem2` linking `var2` and `cons1`
38 * - `elem3` linking `var2` and `cons2`
39 * - `elem4` linking `var3` and `cons2`
41 * And the corresponding inequations will be:
43 * var1.value <= var1.bound
44 * var2.value <= var2.bound
45 * var3.value <= var3.bound
46 * var1.weight * var1.value * elem1.value + var2.weight * var2.value * elem2.value <= cons1.bound
47 * var2.weight * var2.value * elem3.value + var3.weight * var3.value * elem4.value <= cons2.bound
49 * where `var1.value`, `var2.value` and `var3.value` are the unknown values
51 * if a constraint is not shared the sum is replace by a max
53 * Its usefull for the sharing of resources for various models.
54 * For instance for the network model the link are associated
55 * to consrtaint and the communications to variables.
58 extern double sg_maxmin_precision;
59 #define MAXMIN_PRECISION sg_maxmin_precision
60 static XBT_INLINE void double_update(double *variable, double value)
63 if (*variable < MAXMIN_PRECISION)
67 static XBT_INLINE int double_positive(double value)
69 return (value > MAXMIN_PRECISION);
72 static XBT_INLINE int double_equals(double value1, double value2)
74 return (fabs(value1 - value2) < MAXMIN_PRECISION);
79 /** @{ @ingroup SURF_lmm */
81 * @brief Create a new Linear MaxMim system
83 * @param selective_update [description]
85 XBT_PUBLIC(lmm_system_t) lmm_system_new(int selective_update);
88 * @brief Free an existing Linear MaxMin system
90 * @param sys The lmm system to free
92 XBT_PUBLIC(void) lmm_system_free(lmm_system_t sys);
95 * @brief Create a new Linear MaxMin constraint
97 * @param sys The system in which we add a constraint
98 * @param id Data associated to the constraint (e.g.: a network link)
99 * @param bound_value The bound value of the constraint
101 XBT_PUBLIC(lmm_constraint_t) lmm_constraint_new(lmm_system_t sys, void *id,
105 * @brief Share a constraint
106 * @details [long description]
108 * @param cnst The constraint to share
110 void lmm_constraint_shared(lmm_constraint_t cnst);
113 * @brief Check if a constraint is shared (shared by default)
115 * @param cnst The constraint to share
116 * @return 1 if shared, 0 otherwise
118 int lmm_constraint_is_shared(lmm_constraint_t cnst);
121 * @brief Free a constraint
123 * @param sys The system associated to the constraint
124 * @param cnst The constraint to free
126 void lmm_constraint_free(lmm_system_t sys, lmm_constraint_t cnst);
129 * @brief Get the usage of the constraint after the last lmm solve
131 * @param cnst A constraint
132 * @return The usage of the constraint
134 double lmm_constraint_get_usage(lmm_constraint_t cnst);
137 * @brief Create a new Linear MaxMin variable
139 * @param sys The system in which we add a constaint
140 * @param id Data associated to the variable (e.g.: a network communication)
141 * @param weight_value The weight of the variable (0.0 if not used)
142 * @param bound The maximum value of the variable (-1.0 if no maximum value)
143 * @param number_of_constraints The maximum number of constraint to associate to the variable
145 XBT_PUBLIC(lmm_variable_t) lmm_variable_new(lmm_system_t sys, void *id,
148 int number_of_constraints);
150 * @brief Free a variable
152 * @param sys The system associated to the variable
153 * @param var The variable to free
155 XBT_PUBLIC(void) lmm_variable_free(lmm_system_t sys, lmm_variable_t var);
158 * @brief Get the value of the variable after the last lmm solve
160 * @param var A variable
161 * @return The value of the variable
163 XBT_PUBLIC(double) lmm_variable_getvalue(lmm_variable_t var);
166 * @brief Get the maximum value of the variable (-1.0 if no maximum value)
168 * @param var A variable
169 * @return The bound of the variable
171 XBT_PUBLIC(double) lmm_variable_getbound(lmm_variable_t var);
174 * @brief Remove a variable from a constraint
176 * @param sys A system
177 * @param cnst A constraint
178 * @param var The variable to remove
180 XBT_PUBLIC(void) lmm_shrink(lmm_system_t sys, lmm_constraint_t cnst,
184 * @brief Associate a variable to a constraint with a coefficient
186 * @param sys A system
187 * @param cnst A constraint
188 * @param var A variable
189 * @param value The coefficient associated to the variable in the constraint
191 XBT_PUBLIC(void) lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
192 lmm_variable_t var, double value);
195 * @brief Add value to the coefficient between a constraint and a variable or
198 * @param sys A system
199 * @param cnst A constraint
200 * @param var A variable
201 * @param value The value to add to the coefficient associated to the variable in the constraint
203 void lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst,
204 lmm_variable_t var, double value);
207 * @brief Get the numth constraint associated to the variable
209 * @param sys The system associated to the variable (not used)
210 * @param var A variable
211 * @param num The rank of constraint we want to get
212 * @return The numth constraint
214 lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t sys,
215 lmm_variable_t var, int num);
218 * @brief Get the weigth of the numth constraint associated to the variable
220 * @param sys The system associated to the variable (not used)
221 * @param var A variable
222 * @param num The rank of constraint we want to get
223 * @return The numth constraint
225 double lmm_get_cnst_weight_from_var(lmm_system_t sys, lmm_variable_t var,
229 * @brief Get the number of constraint associated to a variable
231 * @param sys The system associated to the variable (not used)
232 * @param var A variable
233 * @return The number of constraint associated to the variable
235 int lmm_get_number_of_cnst_from_var(lmm_system_t sys, lmm_variable_t var);
238 * @brief Get a var associated to a constraint
239 * @details Get the first variable of the next variable of elem if elem is not NULL
241 * @param sys The system associated to the variable (not used)
242 * @param cnst A constraint
243 * @param elem A element of constraint of the constraint or NULL
244 * @return A variable associated to a constraint
246 lmm_variable_t lmm_get_var_from_cnst(lmm_system_t sys,
247 lmm_constraint_t cnst,
248 lmm_element_t * elem);
251 * @brief Get the first active constraint of a system
253 * @param sys A system
254 * @return The first active constraint
256 lmm_constraint_t lmm_get_first_active_constraint(lmm_system_t sys);
259 * @brief Get the next active constraint of a constraint in a system
261 * @param sys A system
262 * @param cnst An active constraint of the system
264 * @return The next active constraint
266 lmm_constraint_t lmm_get_next_active_constraint(lmm_system_t sys,
267 lmm_constraint_t cnst);
269 #ifdef HAVE_LATENCY_BOUND_TRACKING
270 XBT_PUBLIC(int) lmm_is_variable_limited_by_latency(lmm_variable_t var);
274 * @brief Get the data associated to a constraint
276 * @param cnst A constraint
277 * @return The data associated to the constraint
279 void *lmm_constraint_id(lmm_constraint_t cnst);
282 * @brief Get the data associated to a variable
284 * @param var A variable
285 * @return The data associated to the variable
287 void *lmm_variable_id(lmm_variable_t var);
290 * @brief Update the value of element linking the constraint and the variable
292 * @param sys A system
293 * @param cnst A constraint
294 * @param var A variable
295 * @param value The new value
297 void lmm_update(lmm_system_t sys, lmm_constraint_t cnst,
298 lmm_variable_t var, double value);
301 * @brief Update the bound of a variable
303 * @param sys A system
304 * @param var A constraint
305 * @param bound The new bound
307 void lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
311 * @brief Update the weight of a variable
313 * @param sys A system
314 * @param var A variable
315 * @param weight The new weight of the variable
317 XBT_PUBLIC(void) lmm_update_variable_weight(lmm_system_t sys,
322 * @brief Get the weight of a variable
324 * @param var A variable
325 * @return The weight of the variable
327 double lmm_get_variable_weight(lmm_variable_t var);
330 * @brief Update a constraint bound
332 * @param sys A system
333 * @param cnst A constraint
334 * @param bound The new bound of the consrtaint
336 XBT_PUBLIC(void) lmm_update_constraint_bound(lmm_system_t sys,
337 lmm_constraint_t cnst,
341 * @brief [brief description]
343 * @param sys A system
344 * @param cnst A constraint
345 * @return [description]
347 int lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst);
350 * @brief Solve the lmm system
352 * @param sys The lmm system to solve
354 XBT_PUBLIC(void) lmm_solve(lmm_system_t sys);
356 XBT_PUBLIC(void) lagrange_solve(lmm_system_t sys);
357 XBT_PUBLIC(void) bottleneck_solve(lmm_system_t sys);
360 * Default functions associated to the chosen protocol. When
361 * using the lagrangian approach.
364 XBT_PUBLIC(void) lmm_set_default_protocol_function(double (*func_f)
374 XBT_PUBLIC(double func_reno_f) (lmm_variable_t var, double x);
375 XBT_PUBLIC(double func_reno_fp) (lmm_variable_t var, double x);
376 XBT_PUBLIC(double func_reno_fpi) (lmm_variable_t var, double x);
378 XBT_PUBLIC(double func_reno2_f) (lmm_variable_t var, double x);
379 XBT_PUBLIC(double func_reno2_fp) (lmm_variable_t var, double x);
380 XBT_PUBLIC(double func_reno2_fpi) (lmm_variable_t var, double x);
382 XBT_PUBLIC(double func_vegas_f) (lmm_variable_t var, double x);
383 XBT_PUBLIC(double func_vegas_fp) (lmm_variable_t var, double x);
384 XBT_PUBLIC(double func_vegas_fpi) (lmm_variable_t var, double x);
389 #endif /* _SURF_MAXMIN_H */