Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3e94153e35b7c4ed66daf3ed50fb23ff89e732cf
[simgrid.git] / src / surf / maxmin.c
1 /* Authors: Arnaud Legrand                                                  */
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 #include "xbt/sysdep.h"
7 #include "xbt/error.h"
8 #include "maxmin_private.h"
9 #include <stdlib.h>
10
11 /* extern lmm_system_t Sys; */
12 /* extern lmm_constraint_t L1; */
13 /* extern lmm_constraint_t L2; */
14 /* extern lmm_constraint_t L3; */
15
16 /* extern lmm_variable_t R_1_2_3; */
17 /* extern lmm_variable_t R_1; */
18 /* extern lmm_variable_t R_2; */
19 /* extern lmm_variable_t R_3; */
20
21
22 lmm_system_t lmm_system_new(void)
23 {
24   lmm_system_t l = NULL;
25   s_lmm_variable_t var;
26   s_lmm_constraint_t cnst;
27
28   l = xbt_new0(s_lmm_system_t, 1);
29
30   l->modified = 0;
31   xbt_swag_init(&(l->variable_set),
32                 xbt_swag_offset(var, variable_set_hookup));
33   xbt_swag_init(&(l->constraint_set),
34                 xbt_swag_offset(cnst, constraint_set_hookup));
35
36   xbt_swag_init(&(l->active_constraint_set),
37                 xbt_swag_offset(cnst, active_constraint_set_hookup));
38
39   xbt_swag_init(&(l->saturated_variable_set),
40                 xbt_swag_offset(var, saturated_variable_set_hookup));
41   xbt_swag_init(&(l->saturated_constraint_set),
42                 xbt_swag_offset(cnst, saturated_constraint_set_hookup));
43
44   return l;
45 }
46
47 void lmm_system_free(lmm_system_t sys)
48 {
49   lmm_variable_t var = NULL;
50   lmm_constraint_t cnst = NULL;
51
52   while ((var = extract_variable(sys)))
53     lmm_var_free(sys, var);
54
55   while ((cnst = extract_constraint(sys)))
56     lmm_cnst_free(sys, cnst);
57
58   xbt_free(sys);
59 }
60
61 void lmm_variable_disable(lmm_system_t sys, lmm_variable_t var)
62 {
63   int i;
64   lmm_element_t elem = NULL;
65
66   sys->modified = 1;
67
68   for (i = 0; i < var->cnsts_number; i++) {
69     elem = &var->cnsts[i];
70     xbt_swag_remove(elem, &(elem->constraint->element_set));
71     if (xbt_swag_size(&(elem->constraint->element_set)))
72       make_constraint_inactive(sys, elem->constraint);
73   }
74   var->cnsts_number = 0;
75 }
76
77 static void lmm_var_free(lmm_system_t sys, lmm_variable_t var)
78 {
79
80   lmm_variable_disable(sys, var);
81
82   xbt_free(var->cnsts);
83   xbt_free(var);
84 }
85
86 static void lmm_cnst_free(lmm_system_t sys, lmm_constraint_t cnst)
87 {
88 /*   xbt_assert0(xbt_fifo_size(&(cnst->row)), */
89 /*               "This list should be empty!"); */
90   remove_active_constraint(sys, cnst);
91   xbt_free(cnst);
92 }
93
94 lmm_constraint_t lmm_constraint_new(lmm_system_t sys, void *id,
95                                     xbt_maxmin_float_t bound_value)
96 {
97   lmm_constraint_t cnst = NULL;
98   s_lmm_element_t elem;
99
100   cnst = xbt_new0(s_lmm_constraint_t, 1);
101   cnst->id = id;
102   xbt_swag_init(&(cnst->element_set),
103                 xbt_swag_offset(elem, element_set_hookup));
104   xbt_swag_init(&(cnst->active_element_set),
105                 xbt_swag_offset(elem, active_element_set_hookup));
106
107   cnst->bound = bound_value;
108   cnst->usage = 0;
109   insert_constraint(sys, cnst);
110
111   return cnst;
112 }
113
114 void lmm_constraint_free(lmm_system_t sys, lmm_constraint_t cnst)
115 {
116   remove_constraint(sys, cnst);
117   lmm_cnst_free(sys, cnst);
118 }
119
120 lmm_variable_t lmm_variable_new(lmm_system_t sys, void *id,
121                                 xbt_maxmin_float_t weight,
122                                 xbt_maxmin_float_t bound,
123                                 int number_of_constraints)
124 {
125   lmm_variable_t var = NULL;
126
127   var = xbt_new0(s_lmm_variable_t, 1);
128   var->id = id;
129   var->cnsts = xbt_new0(s_lmm_element_t, number_of_constraints);
130   var->cnsts_size = number_of_constraints;
131   /* var->cnsts_number = 0; *//* Useless because of the calloc  */
132   var->weight = weight;
133   var->bound = bound;
134   var->value = -1;
135   insert_variable(sys, var);
136
137   return var;
138 }
139
140 void lmm_variable_free(lmm_system_t sys, lmm_variable_t var)
141 {
142   remove_variable(sys, var);
143   lmm_var_free(sys, var);
144 }
145
146 xbt_maxmin_float_t lmm_variable_getvalue(lmm_variable_t var)
147 {
148   return (var->value);
149 }
150
151 void lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
152                 lmm_variable_t var, xbt_maxmin_float_t value)
153 {
154   lmm_element_t elem = NULL;
155
156   sys->modified = 1;
157
158   if (var->cnsts_number >= var->cnsts_size)
159     abort();
160
161   elem = &(var->cnsts[var->cnsts_number++]);
162
163   elem->value = value;
164   elem->constraint = cnst;
165   elem->variable = var;
166
167   insert_elem_in_constraint(elem);
168
169   make_constraint_active(sys, cnst);
170 }
171
172 lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t sys, lmm_variable_t var, int num)
173 {
174   if(num<var->cnsts_number) return(var->cnsts[num].constraint);
175   else return NULL;
176 }
177
178 int lmm_get_number_of_cnst_from_var(lmm_system_t sys, lmm_variable_t var)
179 {
180   return(var->cnsts_number);
181 }
182
183 void *lmm_constraint_id(lmm_constraint_t cnst)
184 {
185   return cnst->id;
186 }
187
188 void *lmm_variable_id(lmm_variable_t var)
189 {
190   return var->id;
191 }
192
193 static void saturated_constraints_update(lmm_system_t sys,
194                                          lmm_constraint_t cnst,
195                                          xbt_maxmin_float_t * min_usage)
196 {
197   lmm_constraint_t useless_cnst = NULL;
198
199   sys->modified = 1;
200
201   if (cnst->remaining <= 0)
202     return;
203   if ((*min_usage < 0) || (*min_usage > cnst->remaining / cnst->usage)) {
204     *min_usage = cnst->remaining / cnst->usage;
205
206     while ((useless_cnst = xbt_swag_getFirst(&(sys->saturated_constraint_set))))
207       xbt_swag_remove(useless_cnst, &(sys->saturated_constraint_set));
208
209     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
210   } else if (*min_usage == cnst->remaining / cnst->usage) {
211     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
212   }
213 }
214
215 static void saturated_variables_update(lmm_system_t sys)
216 {
217   lmm_constraint_t cnst = NULL;
218   xbt_swag_t cnst_list = NULL;
219   lmm_element_t elem = NULL;
220   xbt_swag_t elem_list = NULL;
221
222   sys->modified = 1;
223
224   cnst_list = &(sys->saturated_constraint_set);
225   while ((cnst = xbt_swag_getFirst(cnst_list))) {
226 /*   xbt_swag_foreach(cnst, cnst_list) { */
227     elem_list = &(cnst->active_element_set);
228     xbt_swag_foreach(elem, elem_list)
229         xbt_swag_insert(elem->variable, &(sys->saturated_variable_set));
230     xbt_swag_remove(cnst, cnst_list);
231   }
232
233 }
234
235 void lmm_solve(lmm_system_t sys)
236 {
237   lmm_variable_t var = NULL;
238   lmm_constraint_t cnst = NULL;
239   lmm_element_t elem = NULL;
240   xbt_swag_t cnst_list = NULL;
241   xbt_swag_t var_list = NULL;
242   xbt_swag_t elem_list = NULL;
243   xbt_maxmin_float_t min_usage = -1;
244
245   if(!(sys->modified)) return;
246
247   /* Init */
248   var_list = &(sys->variable_set);
249   xbt_swag_foreach(var, var_list) {
250     var->value = -1;
251   }
252
253
254   /* Compute Usage and store the variables that reach the maximum */
255
256   cnst_list = &(sys->active_constraint_set);
257   xbt_swag_foreach(cnst, cnst_list) {
258     /* INIT */
259     elem_list = &(cnst->element_set);
260     cnst->remaining = cnst->bound;
261     cnst->usage = 0;
262     xbt_swag_foreach(elem, elem_list) {
263       cnst->usage += elem->value / elem->variable->weight;
264       insert_active_elem_in_constraint(elem);
265     }
266
267     /* Saturated constraints update */
268     saturated_constraints_update(sys, cnst, &min_usage);
269   }
270
271   saturated_variables_update(sys);
272
273   /* Saturated variables update */
274
275   do {
276     /* Fix the variables that have to be */
277     var_list = &(sys->saturated_variable_set);
278
279
280     xbt_swag_foreach(var, var_list) {
281       /* First check if some of these variables have reach their upper
282          bound and update min_usage accordingly. */
283       if ((var->bound > 0) && (var->bound / var->weight < min_usage)) {
284         min_usage = var->bound / var->weight;
285       }
286     }
287
288
289     while ((var = xbt_swag_getFirst(var_list))) {
290       int i;
291
292       var->value = min_usage / var->weight;
293
294       /* Update usage */
295
296       for (i = 0; i < var->cnsts_number; i++) {
297         elem = &var->cnsts[i];
298         cnst = elem->constraint;
299         cnst->remaining -= elem->value * var->value;
300         cnst->usage -= elem->value / var->weight;
301         remove_active_elem_in_constraint(elem);
302       }
303       xbt_swag_remove(var, var_list);
304     }
305
306     /* Find out which variables reach the maximum */
307     cnst_list = &(sys->active_constraint_set);
308     min_usage = -1;
309     xbt_swag_foreach(cnst, cnst_list) {
310       saturated_constraints_update(sys, cnst, &min_usage);
311     }
312     saturated_variables_update(sys);
313
314   } while (xbt_swag_size(&(sys->saturated_variable_set)));
315
316   sys->modified = 0;
317 }
318
319 /* Not a O(1) function */
320
321 void lmm_update(lmm_system_t sys, lmm_constraint_t cnst,
322                 lmm_variable_t var, xbt_maxmin_float_t value)
323 {
324   int i;
325   lmm_element_t elem = NULL;
326
327   sys->modified = 1;
328   for (i = 0; i < var->cnsts_number; i++)
329     if (var->cnsts[i].constraint == cnst) {
330       elem->value = value;
331       return;
332     }
333 }
334
335 void lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
336                                xbt_maxmin_float_t bound)
337 {
338   sys->modified = 1;
339   var->bound = bound;
340 }
341
342 void lmm_update_variable_weight(lmm_system_t sys, lmm_variable_t var,
343                                 xbt_maxmin_float_t weight)
344 {
345   sys->modified = 1;
346   var->weight = weight;
347 }
348
349 void lmm_update_constraint_bound(lmm_system_t sys, lmm_constraint_t cnst,
350                                  xbt_maxmin_float_t bound)
351 {
352   sys->modified = 1;
353   cnst->bound = bound;
354 }
355
356 int lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst)
357 {
358   return xbt_swag_belongs(cnst,&(sys->active_constraint_set));
359 }
360
361
362 /* void lmm_print(lmm_system_t sys) */
363 /* { */
364 /*   lmm_variable_t var = NULL; */
365 /*   lmm_constraint_t cnst = NULL; */
366 /*   lmm_element_t elem = NULL; */
367 /*   xbt_swag_t cnst_list = NULL; */
368 /*   xbt_swag_t var_list = NULL; */
369 /*   xbt_swag_t elem_list = NULL; */
370
371 /*   var_list = &(sys->variable_set); */
372 /*   xbt_swag_foreach(var, var_list) { */
373 /*     printf("%s : {\n",var->id); */
374 /*     printf("\t %s : {\n",var->id); */
375 /*   } */
376 /* } */