Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove some unbreakable spaces breaking the pdf generation
[simgrid.git] / src / include / surf / maxmin.h
1 /* Copyright (c) 2004-2014. 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 "portable.h"
11 #include "xbt/misc.h"
12 #include "surf/datatypes.h"
13 #include <math.h>
14
15
16 /** @addtogroup SURF_lmm 
17  * @details 
18  * A linear maxmin solver to resolves inequations systems.
19  * 
20  * Most SimGrid model rely on a "fluid/steady-state" modeling that
21  * samount to share resources between actions at relatively
22  * coarse-grain.  Such sharing is generally done by solving a set of
23  * linear inequations. Let's take an example and assume we have the
24  * variables \f$x_1\f$, \f$x_2\f$, \f$x_3\f$, and \f$x_4\f$ . Let's
25  * say that \f$x_1\f$ and \f$x_2\f$ correspond to activities running
26  * and the same CPU \f$A\f$ whose capacity is \f$C_A\f$ . In such a
27  * case, we need to enforce:
28  *
29  *   \f[ x_1 + x_2 \leq C_A \f]
30  *
31  * Likewise, if \f$x_3\f$ (resp. \f$x_4\f$) corresponds to a network
32  * flow \f$F_3\f$ (resp. \f$F_4\f$) that goes through a set of links
33  * \f$L_1\f$ and \f$L_2\f$ (resp. \f$L_2\f$ and \f$L_3\f$), then we
34  * 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
41  * satisfied but this would obviously not be very realistic. A
42  * possible objective is to try to maximize the minimum of the
43  * \f$x_i\f$ . This ensures that all the \f$x_i\f$ are positive and "as
44  * large as possible". 
45  *
46  * This is called *max-min fairness* and is the most commonly used
47  * objective in SimGrid. Another possibility is to maximize
48  * \f$\sum_if(x_i)\f$, where \f$f\f$ is a strictly increasing concave
49  * function.
50  *
51  * Constraint: 
52  *  - bound (set)
53  *  - shared (set)
54  *  - usage (computed)
55  * Variable:
56  *  - weight (set)
57  *  - bound (set)
58  *  - value (computed)
59  * Element:
60  *  - value (set)
61  * 
62  * A possible system could be:
63  * - three variables: `var1`, `var2`, `var3`
64  * - two constraints: `cons1`, `cons2`
65  * - four elements linking:
66  *  - `elem1` linking `var1` and `cons1`
67  *  - `elem2` linking `var2` and `cons1`
68  *  - `elem3` linking `var2` and `cons2`
69  *  - `elem4` linking `var3` and `cons2`
70  * 
71  * And the corresponding inequations will be:
72  * 
73  *     var1.value <= var1.bound
74  *     var2.value <= var2.bound
75  *     var3.value <= var3.bound
76  *     var1.weight * var1.value * elem1.value + var2.weight * var2.value * elem2.value <= cons1.bound
77  *     var2.weight * var2.value * elem3.value + var3.weight * var3.value * elem4.value <= cons2.bound
78  * 
79  * where `var1.value`, `var2.value` and `var3.value` are the unknown values
80  * 
81  * if a constraint is not shared the sum is replace by a max
82  * 
83  * Its usefull for the sharing of resources for various models.
84  * For instance for the network model the link are associated 
85  * to consrtaint and the communications to variables.
86  */
87
88 extern double sg_maxmin_precision;
89 extern double sg_surf_precision;
90
91 static XBT_INLINE void double_update(double *variable, double value, double precision)
92 {
93   *variable -= value;
94   if (*variable < precision)
95     *variable = 0.0;
96 }
97
98 static XBT_INLINE int double_positive(double value, double precision)
99 {
100   return (value > precision);
101 }
102
103 static XBT_INLINE int double_equals(double value1, double value2, double precision)
104 {
105   return (fabs(value1 - value2) < precision);
106 }
107
108 SG_BEGIN_DECL()
109
110 /** @{ @ingroup SURF_lmm */
111 /**
112  * @brief Create a new Linear MaxMim system
113  * 
114  * @param selective_update [description]
115  */
116 XBT_PUBLIC(lmm_system_t) lmm_system_new(int selective_update);
117
118 /**
119  * @brief Free an existing Linear MaxMin system
120  * 
121  * @param sys The lmm system to free
122  */
123 XBT_PUBLIC(void) lmm_system_free(lmm_system_t sys);
124
125 /**
126  * @brief Create a new Linear MaxMin constraint
127  * 
128  * @param sys The system in which we add a constraint
129  * @param id Data associated to the constraint (e.g.: a network link)
130  * @param bound_value The bound value of the constraint 
131  */
132 XBT_PUBLIC(lmm_constraint_t) lmm_constraint_new(lmm_system_t sys, void *id,
133                                                 double bound_value);
134
135 /**
136  * @brief Share a constraint
137  * @details [long description]
138  * 
139  * @param cnst The constraint to share
140  */
141 XBT_PUBLIC(void) lmm_constraint_shared(lmm_constraint_t cnst);
142
143 /**
144  * @brief Check if a constraint is shared (shared by default)
145  * 
146  * @param cnst The constraint to share
147  * @return 1 if shared, 0 otherwise
148  */
149 XBT_PUBLIC(int) lmm_constraint_is_shared(lmm_constraint_t cnst);
150
151 /**
152  * @brief Free a constraint
153  * 
154  * @param sys The system associated to the constraint
155  * @param cnst The constraint to free
156  */
157 XBT_PUBLIC(void) lmm_constraint_free(lmm_system_t sys, lmm_constraint_t cnst);
158
159 /**
160  * @brief Get the usage of the constraint after the last lmm solve
161  * 
162  * @param cnst A constraint
163  * @return The usage of the constraint
164  */
165 XBT_PUBLIC(double) lmm_constraint_get_usage(lmm_constraint_t cnst);
166
167 /**
168  * @brief Create a new Linear MaxMin variable
169  * 
170  * @param sys The system in which we add a constaint
171  * @param id Data associated to the variable (e.g.: a network communication)
172  * @param weight_value The weight of the variable (0.0 if not used)
173  * @param bound The maximum value of the variable (-1.0 if no maximum value)
174  * @param number_of_constraints The maximum number of constraint to associate to the variable
175  */
176 XBT_PUBLIC(lmm_variable_t) lmm_variable_new(lmm_system_t sys, void *id,
177                                             double weight_value,
178                                             double bound,
179                                             int number_of_constraints);
180 /**
181  * @brief Free a variable
182  * 
183  * @param sys The system associated to the variable
184  * @param var The variable to free
185  */
186 XBT_PUBLIC(void) lmm_variable_free(lmm_system_t sys, lmm_variable_t var);
187
188 /**
189  * @brief Get the value of the variable after the last lmm solve
190  * 
191  * @param var A variable
192  * @return The value of the variable
193  */
194 XBT_PUBLIC(double) lmm_variable_getvalue(lmm_variable_t var);
195
196 /**
197  * @brief Get the maximum value of the variable (-1.0 if no maximum value)
198  * 
199  * @param var A variable
200  * @return The bound of the variable
201  */
202 XBT_PUBLIC(double) lmm_variable_getbound(lmm_variable_t var);
203
204 /**
205  * @brief Remove a variable from a constraint
206  * 
207  * @param sys A system
208  * @param cnst A constraint
209  * @param var The variable to remove
210  */
211 XBT_PUBLIC(void) lmm_shrink(lmm_system_t sys, lmm_constraint_t cnst,
212                             lmm_variable_t var);
213
214 /**
215  * @brief Associate a variable to a constraint with a coefficient
216  * 
217  * @param sys A system
218  * @param cnst A constraint
219  * @param var A variable
220  * @param value The coefficient associated to the variable in the constraint
221  */
222 XBT_PUBLIC(void) lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
223                             lmm_variable_t var, double value);
224
225 /**
226  * @brief Add value to the coefficient between a constraint and a variable or 
227  *        create one
228  * 
229  * @param sys A system
230  * @param cnst A constraint
231  * @param var A variable
232  * @param value The value to add to the coefficient associated to the variable in the constraint
233  */
234 XBT_PUBLIC(void) lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst,
235                     lmm_variable_t var, double value);
236
237 /**
238  * @brief Get the numth constraint associated to the variable
239  * 
240  * @param sys The system associated to the variable (not used)
241  * @param var A variable
242  * @param num The rank of constraint we want to get
243  * @return The numth constraint
244  */
245 XBT_PUBLIC(lmm_constraint_t) lmm_get_cnst_from_var(lmm_system_t sys,
246                                        lmm_variable_t var, int num);
247
248 /**
249  * @brief Get the weigth of the numth constraint associated to the variable
250  * 
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
255  */
256 XBT_PUBLIC(double) lmm_get_cnst_weight_from_var(lmm_system_t sys, lmm_variable_t var,
257                                     int num);
258
259 /**
260  * @brief Get the number of constraint associated to a variable
261  * 
262  * @param sys The system associated to the variable (not used)
263  * @param var A variable
264  * @return The number of constraint associated to the variable
265  */
266 XBT_PUBLIC(int) lmm_get_number_of_cnst_from_var(lmm_system_t sys, lmm_variable_t var);
267
268 /**
269  * @brief Get a var associated to a constraint 
270  * @details Get the first variable of the next variable of elem if elem is not NULL
271  * 
272  * @param sys The system associated to the variable (not used)
273  * @param cnst A constraint
274  * @param elem A element of constraint of the constraint or NULL
275  * @return A variable associated to a constraint
276  */
277 XBT_PUBLIC(lmm_variable_t) lmm_get_var_from_cnst(lmm_system_t sys,
278                                      lmm_constraint_t cnst,
279                                      lmm_element_t * elem);
280
281 /**
282  * @brief Get the first active constraint of a system
283  * 
284  * @param sys A system
285  * @return The first active constraint
286  */
287 XBT_PUBLIC(lmm_constraint_t) lmm_get_first_active_constraint(lmm_system_t sys);
288
289 /**
290  * @brief Get the next active constraint of a constraint in a system
291  * 
292  * @param sys A system
293  * @param cnst An active constraint of the system
294  * 
295  * @return The next active constraint
296  */
297 XBT_PUBLIC(lmm_constraint_t) lmm_get_next_active_constraint(lmm_system_t sys,
298                                                 lmm_constraint_t cnst);
299
300 #ifdef HAVE_LATENCY_BOUND_TRACKING
301 XBT_PUBLIC(int) lmm_is_variable_limited_by_latency(lmm_variable_t var);
302 #endif
303
304 /**
305  * @brief Get the data associated to a constraint
306  * 
307  * @param cnst A constraint
308  * @return The data associated to the constraint
309  */
310 XBT_PUBLIC(void *) lmm_constraint_id(lmm_constraint_t cnst);
311
312 /**
313  * @brief Get the data associated to a variable
314  * 
315  * @param var A variable
316  * @return The data associated to the variable
317  */
318 XBT_PUBLIC(void *) lmm_variable_id(lmm_variable_t var);
319
320 /**
321  * @brief Update the value of element linking the constraint and the variable
322  * 
323  * @param sys A system
324  * @param cnst A constraint
325  * @param var A variable
326  * @param value The new value
327  */
328 XBT_PUBLIC(void) lmm_update(lmm_system_t sys, lmm_constraint_t cnst,
329                 lmm_variable_t var, double value);
330
331 /**
332  * @brief Update the bound of a variable
333  * 
334  * @param sys A system
335  * @param var A constraint
336  * @param bound The new bound
337  */
338 XBT_PUBLIC(void) lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
339                                double bound);
340
341 /**
342  * @brief Update the weight of a variable
343  * 
344  * @param sys A system
345  * @param var A variable
346  * @param weight The new weight of the variable
347  */
348 XBT_PUBLIC(void) lmm_update_variable_weight(lmm_system_t sys,
349                                             lmm_variable_t var,
350                                             double weight);
351
352 /**
353  * @brief Get the weight of a variable
354  * 
355  * @param var A variable
356  * @return The weight of the variable
357  */
358 XBT_PUBLIC(double) lmm_get_variable_weight(lmm_variable_t var);
359
360 /**
361  * @brief Update a constraint bound
362  * 
363  * @param sys A system
364  * @param cnst A constraint
365  * @param bound The new bound of the consrtaint
366  */
367 XBT_PUBLIC(void) lmm_update_constraint_bound(lmm_system_t sys,
368                                              lmm_constraint_t cnst,
369                                              double bound);
370
371 /**
372  * @brief [brief description]
373  * 
374  * @param sys A system
375  * @param cnst A constraint
376  * @return [description]
377  */
378 XBT_PUBLIC(int) lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst);
379
380 /**
381  * @brief Solve the lmm system
382  * 
383  * @param sys The lmm system to solve
384  */
385 XBT_PUBLIC(void) lmm_solve(lmm_system_t sys);
386
387 XBT_PUBLIC(void) lagrange_solve(lmm_system_t sys);
388 XBT_PUBLIC(void) bottleneck_solve(lmm_system_t sys);
389
390 /**
391  * Default functions associated to the chosen protocol. When
392  * using the lagrangian approach.
393  */
394
395 XBT_PUBLIC(void) lmm_set_default_protocol_function(double (*func_f)
396                                                     (lmm_variable_t var,
397                                                      double x),
398                                                    double (*func_fp)
399                                                     (lmm_variable_t var,
400                                                      double x),
401                                                    double (*func_fpi)
402                                                     (lmm_variable_t var,
403                                                      double x));
404
405 XBT_PUBLIC(double func_reno_f) (lmm_variable_t var, double x);
406 XBT_PUBLIC(double func_reno_fp) (lmm_variable_t var, double x);
407 XBT_PUBLIC(double func_reno_fpi) (lmm_variable_t var, double x);
408
409 XBT_PUBLIC(double func_reno2_f) (lmm_variable_t var, double x);
410 XBT_PUBLIC(double func_reno2_fp) (lmm_variable_t var, double x);
411 XBT_PUBLIC(double func_reno2_fpi) (lmm_variable_t var, double x);
412
413 XBT_PUBLIC(double func_vegas_f) (lmm_variable_t var, double x);
414 XBT_PUBLIC(double func_vegas_fp) (lmm_variable_t var, double x);
415 XBT_PUBLIC(double func_vegas_fpi) (lmm_variable_t var, double x);
416
417 /** @} */
418 SG_END_DECL()
419
420 #endif                          /* _SURF_MAXMIN_H */