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. */
7 #include "xbt/sysdep.h"
9 #include "xbt/mallocator.h"
10 #include "maxmin_private.hpp"
12 #include <stdio.h> /* sprintf */
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_maxmin, surf,
15 "Logging specific to SURF (maxmin)");
17 typedef struct s_dyn_light {
21 } s_dyn_light_t, *dyn_light_t;
23 XBT_EXPORT_NO_IMPORT(double) sg_maxmin_precision = 0.00001;
24 XBT_EXPORT_NO_IMPORT(double) sg_surf_precision = 0.00001;
26 static void *lmm_variable_mallocator_new_f(void);
27 static void lmm_variable_mallocator_free_f(void *var);
28 #define lmm_variable_mallocator_reset_f ((void_f_pvoid_t)NULL)
29 static void lmm_update_modified_set(lmm_system_t sys,
30 lmm_constraint_t cnst);
31 static void lmm_remove_all_modified_set(lmm_system_t sys);
32 static int Global_debug_id = 1;
33 static int Global_const_debug_id = 1;
35 static void lmm_var_free(lmm_system_t sys, lmm_variable_t var);
36 static XBT_INLINE void lmm_cnst_free(lmm_system_t sys,
37 lmm_constraint_t cnst);
39 lmm_system_t lmm_system_new(int selective_update)
41 lmm_system_t l = NULL;
43 s_lmm_constraint_t cnst;
45 l = xbt_new0(s_lmm_system_t, 1);
48 l->selective_update_active = selective_update;
49 l->visited_counter = 1;
51 XBT_DEBUG("Setting selective_update_active flag to %d\n",
52 l->selective_update_active);
54 xbt_swag_init(&(l->variable_set),
55 xbt_swag_offset(var, variable_set_hookup));
56 xbt_swag_init(&(l->constraint_set),
57 xbt_swag_offset(cnst, constraint_set_hookup));
59 xbt_swag_init(&(l->active_constraint_set),
60 xbt_swag_offset(cnst, active_constraint_set_hookup));
62 xbt_swag_init(&(l->modified_constraint_set),
63 xbt_swag_offset(cnst, modified_constraint_set_hookup));
64 xbt_swag_init(&(l->saturated_variable_set),
65 xbt_swag_offset(var, saturated_variable_set_hookup));
66 xbt_swag_init(&(l->saturated_constraint_set),
67 xbt_swag_offset(cnst, saturated_constraint_set_hookup));
69 l->variable_mallocator = xbt_mallocator_new(65536,
70 lmm_variable_mallocator_new_f,
71 lmm_variable_mallocator_free_f,
72 lmm_variable_mallocator_reset_f);
77 void lmm_system_free(lmm_system_t sys)
79 lmm_variable_t var = NULL;
80 lmm_constraint_t cnst = NULL;
82 while ((var = (lmm_variable_t) extract_variable(sys))) {
84 ("Variable %p (%d) still in LMM system when freing it: this may be a bug",
86 lmm_var_free(sys, var);
89 while ((cnst = (lmm_constraint_t) extract_constraint(sys)))
90 lmm_cnst_free(sys, cnst);
92 xbt_mallocator_free(sys->variable_mallocator);
96 static XBT_INLINE void lmm_variable_disable(lmm_system_t sys, lmm_variable_t var)
101 lmm_element_t elem = NULL;
103 XBT_IN("(sys=%p, var=%p)", sys, var);
107 for (i = 0; i < var->cnsts_number; i++) {
108 elem = &var->cnsts[i];
109 xbt_swag_remove(elem, &(elem->constraint->element_set));
110 xbt_swag_remove(elem, &(elem->constraint->active_element_set));
111 if (!xbt_swag_size(&(elem->constraint->element_set)))
112 make_constraint_inactive(sys, elem->constraint);
115 var->cnsts[n].constraint = elem->constraint;
120 var->cnsts_number = n;
121 lmm_update_modified_set(sys, var->cnsts[0].constraint);
124 var->cnsts_number = 0;
128 static void lmm_var_free(lmm_system_t sys, lmm_variable_t var)
131 lmm_variable_disable(sys, var);
132 xbt_mallocator_release(sys->variable_mallocator, var);
135 static XBT_INLINE void lmm_cnst_free(lmm_system_t sys,
136 lmm_constraint_t cnst)
138 /* xbt_assert(xbt_swag_size(&(cnst->element_set)), */
139 /* "This list should be empty!"); */
140 make_constraint_inactive(sys, cnst);
144 lmm_constraint_t lmm_constraint_new(lmm_system_t sys, void *id,
147 lmm_constraint_t cnst = NULL;
148 s_lmm_element_t elem;
150 cnst = xbt_new0(s_lmm_constraint_t, 1);
152 cnst->id_int = Global_const_debug_id++;
153 xbt_swag_init(&(cnst->element_set),
154 xbt_swag_offset(elem, element_set_hookup));
155 xbt_swag_init(&(cnst->active_element_set),
156 xbt_swag_offset(elem, active_element_set_hookup));
158 cnst->bound = bound_value;
161 insert_constraint(sys, cnst);
166 void lmm_constraint_shared(lmm_constraint_t cnst)
171 int lmm_constraint_is_shared(lmm_constraint_t cnst)
173 return (cnst->shared);
176 XBT_INLINE void lmm_constraint_free(lmm_system_t sys,
177 lmm_constraint_t cnst)
179 remove_constraint(sys, cnst);
180 lmm_cnst_free(sys, cnst);
183 static void *lmm_variable_mallocator_new_f(void)
185 lmm_variable_t var = xbt_new(s_lmm_variable_t, 1);
186 var->cnsts = NULL; /* will be created by realloc */
190 static void lmm_variable_mallocator_free_f(void *var)
192 xbt_free(((lmm_variable_t) var)->cnsts);
196 lmm_variable_t lmm_variable_new(lmm_system_t sys, void *id,
198 double bound, int number_of_constraints)
200 lmm_variable_t var = NULL;
203 XBT_IN("(sys=%p, id=%p, weight=%f, bound=%f, num_cons =%d)",
204 sys, id, weight, bound, number_of_constraints);
206 var = (lmm_variable_t) xbt_mallocator_get(sys->variable_mallocator);
208 var->id_int = Global_debug_id++;
209 var->cnsts = (s_lmm_element_t *) xbt_realloc(var->cnsts, number_of_constraints * sizeof(s_lmm_element_t));
210 for (i = 0; i < number_of_constraints; i++) {
211 var->cnsts[i].element_set_hookup.next = NULL;
212 var->cnsts[i].element_set_hookup.prev = NULL;
213 var->cnsts[i].active_element_set_hookup.next = NULL;
214 var->cnsts[i].active_element_set_hookup.prev = NULL;
215 var->cnsts[i].constraint = NULL;
216 var->cnsts[i].variable = NULL;
217 var->cnsts[i].value = 0.0;
219 var->cnsts_size = number_of_constraints;
220 var->cnsts_number = 0;
221 var->weight = weight;
224 var->visited = sys->visited_counter - 1;
227 var->func_f = func_f_def;
228 var->func_fp = func_fp_def;
229 var->func_fpi = func_fpi_def;
231 var->variable_set_hookup.next = NULL;
232 var->variable_set_hookup.prev = NULL;
233 var->saturated_variable_set_hookup.next = NULL;
234 var->saturated_variable_set_hookup.prev = NULL;
237 xbt_swag_insert_at_head(var, &(sys->variable_set));
239 xbt_swag_insert_at_tail(var, &(sys->variable_set));
241 XBT_OUT(" returns %p", var);
245 void lmm_variable_free(lmm_system_t sys, lmm_variable_t var)
247 remove_variable(sys, var);
248 lmm_var_free(sys, var);
251 double lmm_variable_getvalue(lmm_variable_t var)
256 double lmm_variable_getbound(lmm_variable_t var)
261 /* Replace the content of elem_a with elem_b. The content of elem_b is cleared. */
262 static void renew_elem_entry(lmm_element_t elem_a, lmm_element_t elem_b)
264 elem_a->constraint = elem_b->constraint;
265 elem_a->variable = elem_b->variable;
266 elem_a->value = elem_b->value;
268 /* If elem_b is in the element_set swag, register the new element to the swag. */
269 if (xbt_swag_remove(elem_b, &(elem_b->constraint->element_set))) {
270 if (elem_a->variable->weight)
271 xbt_swag_insert_at_head(elem_a, &(elem_a->constraint->element_set));
273 xbt_swag_insert_at_tail(elem_a, &(elem_a->constraint->element_set));
276 if (xbt_swag_remove(elem_b, &(elem_b->constraint->active_element_set))) {
277 if (elem_a->variable->weight)
278 xbt_swag_insert_at_head(elem_a, &(elem_a->constraint->active_element_set));
280 xbt_swag_insert_at_tail(elem_a, &(elem_a->constraint->active_element_set));
283 elem_b->constraint = NULL;
284 elem_b->variable = NULL;
288 void lmm_shrink(lmm_system_t sys, lmm_constraint_t cnst,
291 lmm_element_t elem = NULL;
295 for (i = 0; i < var->cnsts_number; i++) {
296 elem = &(var->cnsts[i]);
297 if (elem->constraint == cnst) {
304 XBT_DEBUG("cnst %p is not found in var %p", cnst, var);
310 XBT_DEBUG("remove elem(value %f, cnst %p, var %p) in var %p",
311 elem->value, elem->constraint, elem->variable, var);
315 /* We are going to change the constraint object and the variable object.
316 * Propagate this change to other objects. Calling here (not after
317 * modification) is correct? */
318 lmm_update_modified_set(sys, cnst);
319 lmm_update_modified_set(sys, var->cnsts[0].constraint); // will look up element_set of this constraint, and then each var in the element_set, and each var->cnsts[i].
323 /* now var->cnsts[i] is not necessary any more */
325 xbt_swag_remove(elem, &(elem->constraint->element_set));
326 xbt_swag_remove(elem, &(elem->constraint->active_element_set));
327 elem->constraint = NULL;
328 elem->variable = NULL;
333 /* We do not want to have an empty element entry before the last entry. So,
334 * plug up the hole with the last one. */
335 if (i < var->cnsts_number - 1)
336 renew_elem_entry(&var->cnsts[i], &var->cnsts[var->cnsts_number - 1]);
338 var->cnsts_number -= 1;
341 if (xbt_swag_size(&(cnst->element_set)) == 0)
342 make_constraint_inactive(sys, cnst);
345 void lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
346 lmm_variable_t var, double value)
348 lmm_element_t elem = NULL;
352 xbt_assert(var->cnsts_number < var->cnsts_size, "Too much constraints");
354 elem = &(var->cnsts[var->cnsts_number++]);
357 elem->constraint = cnst;
358 elem->variable = var;
361 xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
363 xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
364 if(!sys->selective_update_active) {
365 make_constraint_active(sys, cnst);
366 } else if(elem->value>0 || var->weight >0) {
367 make_constraint_active(sys, cnst);
368 lmm_update_modified_set(sys, cnst);
369 if (var->cnsts_number > 1)
370 lmm_update_modified_set(sys, var->cnsts[0].constraint);
374 void lmm_expand_add(lmm_system_t sys, lmm_constraint_t cnst,
375 lmm_variable_t var, double value)
380 for (i = 0; i < var->cnsts_number; i++)
381 if (var->cnsts[i].constraint == cnst)
384 if (i < var->cnsts_number) {
386 var->cnsts[i].value += value;
388 var->cnsts[i].value = MAX(var->cnsts[i].value, value);
389 lmm_update_modified_set(sys, cnst);
391 lmm_expand(sys, cnst, var, value);
394 lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t /*sys*/,
398 if (num < var->cnsts_number)
399 return (var->cnsts[num].constraint);
404 double lmm_get_cnst_weight_from_var(lmm_system_t /*sys*/,
408 if (num < var->cnsts_number)
409 return (var->cnsts[num].value);
414 int lmm_get_number_of_cnst_from_var(lmm_system_t /*sys*/,
417 return (var->cnsts_number);
420 lmm_variable_t lmm_get_var_from_cnst(lmm_system_t /*sys*/,
421 lmm_constraint_t cnst,
422 lmm_element_t * elem)
425 *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->element_set));
427 *elem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->element_set.offset);
429 return (*elem)->variable;
434 //if we modify the swag between calls, normal version may loop forever
435 //this safe version ensures that we browse the swag elements only once
436 lmm_variable_t lmm_get_var_from_cnst_safe(lmm_system_t /*sys*/,
437 lmm_constraint_t cnst,
438 lmm_element_t * elem,
439 lmm_element_t * nextelem,
443 *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->element_set));
444 *numelem = xbt_swag_size(&(cnst->element_set))-1;
453 *nextelem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->element_set.offset);
454 return (*elem)->variable;
459 void *lmm_constraint_id(lmm_constraint_t cnst)
464 void *lmm_variable_id(lmm_variable_t var)
469 static XBT_INLINE void saturated_constraint_set_update(double usage,
471 dyn_light_t saturated_constraint_set,
474 xbt_assert(usage > 0,"Impossible");
476 if (*min_usage < 0 || *min_usage > usage) {
478 // XBT_HERE(" min_usage=%f (cnst->remaining / cnst->usage =%f)", *min_usage, usage);
479 saturated_constraint_set->data[0] = cnst_light_num;
480 saturated_constraint_set->pos = 1;
481 } else if (*min_usage == usage) {
482 if(saturated_constraint_set->pos == saturated_constraint_set->size) { // realloc the size
483 saturated_constraint_set->size *= 2;
484 saturated_constraint_set->data = (int*) xbt_realloc(saturated_constraint_set->data, (saturated_constraint_set->size) * sizeof(int));
486 saturated_constraint_set->data[saturated_constraint_set->pos] = cnst_light_num;
487 saturated_constraint_set->pos++;
491 static XBT_INLINE void saturated_variable_set_update(
492 s_lmm_constraint_light_t *cnst_light_tab,
493 dyn_light_t saturated_constraint_set,
496 /* Add active variables (i.e. variables that need to be set) from the set of constraints to saturate (cnst_light_tab)*/
497 lmm_constraint_light_t cnst = NULL;
499 lmm_element_t elem = NULL;
500 xbt_swag_t elem_list = NULL;
502 for(i = 0; i< saturated_constraint_set->pos; i++){
503 cnst = &cnst_light_tab[saturated_constraint_set->data[i]];
504 elem_list = &(cnst->cnst->active_element_set);
505 xbt_swag_foreach(_elem, elem_list) {
506 elem = (lmm_element_t)_elem;
507 if (elem->variable->weight <= 0)
509 if ((elem->value > 0))
510 xbt_swag_insert(elem->variable, &(sys->saturated_variable_set));
515 void lmm_print(lmm_system_t sys)
517 void *_cnst, *_elem, *_var;
518 lmm_constraint_t cnst = NULL;
519 lmm_element_t elem = NULL;
520 lmm_variable_t var = NULL;
521 xbt_swag_t cnst_list = NULL;
522 xbt_swag_t var_list = NULL;
523 xbt_swag_t elem_list = NULL;
524 char print_buf[1024];
525 char *trace_buf = (char*) xbt_malloc0(sizeof(char));
528 /* Printing Objective */
529 var_list = &(sys->variable_set);
530 sprintf(print_buf, "MAX-MIN ( ");
532 xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
533 strcat(trace_buf, print_buf);
534 xbt_swag_foreach(_var, var_list) {
535 var = (lmm_variable_t)_var;
536 sprintf(print_buf, "'%d'(%f) ", var->id_int, var->weight);
538 xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
539 strcat(trace_buf, print_buf);
541 sprintf(print_buf, ")");
543 xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
544 strcat(trace_buf, print_buf);
545 XBT_DEBUG("%20s", trace_buf);
546 trace_buf[0] = '\000';
548 XBT_DEBUG("Constraints");
549 /* Printing Constraints */
550 cnst_list = &(sys->active_constraint_set);
551 xbt_swag_foreach(_cnst, cnst_list) {
552 cnst = (lmm_constraint_t)_cnst;
554 elem_list = &(cnst->element_set);
555 sprintf(print_buf, "\t");
557 xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
558 strcat(trace_buf, print_buf);
559 sprintf(print_buf, "%s(",(cnst->shared)?"":"max");
561 xbt_realloc(trace_buf,
562 strlen(trace_buf) + strlen(print_buf) + 1);
563 strcat(trace_buf, print_buf);
564 xbt_swag_foreach(_elem, elem_list) {
565 elem = (lmm_element_t)_elem;
566 sprintf(print_buf, "%f.'%d'(%f) %s ", elem->value,
567 elem->variable->id_int, elem->variable->value,(cnst->shared)?"+":",");
569 xbt_realloc(trace_buf,
570 strlen(trace_buf) + strlen(print_buf) + 1);
571 strcat(trace_buf, print_buf);
573 sum += elem->value * elem->variable->value;
575 sum = MAX(sum,elem->value * elem->variable->value);
577 sprintf(print_buf, "0) <= %f ('%d')", cnst->bound, cnst->id_int);
579 xbt_realloc(trace_buf, strlen(trace_buf) + strlen(print_buf) + 1);
580 strcat(trace_buf, print_buf);
583 sprintf(print_buf, " [MAX-Constraint]");
585 xbt_realloc(trace_buf,
586 strlen(trace_buf) + strlen(print_buf) + 1);
587 strcat(trace_buf, print_buf);
589 XBT_DEBUG("%s", trace_buf);
590 trace_buf[0] = '\000';
591 xbt_assert(!double_positive(sum - cnst->bound, cnst->bound*sg_maxmin_precision),
592 "Incorrect value (%f is not smaller than %f): %g",
593 sum, cnst->bound, sum - cnst->bound);
596 XBT_DEBUG("Variables");
597 /* Printing Result */
598 xbt_swag_foreach(_var, var_list) {
599 var = (lmm_variable_t)_var;
600 if (var->bound > 0) {
601 XBT_DEBUG("'%d'(%f) : %f (<=%f)", var->id_int, var->weight, var->value,
603 xbt_assert(!double_positive(var->value - var->bound, var->bound*sg_maxmin_precision),
604 "Incorrect value (%f is not smaller than %f",
605 var->value, var->bound);
607 XBT_DEBUG("'%d'(%f) : %f", var->id_int, var->weight, var->value);
614 void lmm_solve(lmm_system_t sys)
616 void *_var, *_cnst, *_cnst_next, *_elem;
617 lmm_variable_t var = NULL;
618 lmm_constraint_t cnst = NULL;
619 lmm_element_t elem = NULL;
620 xbt_swag_t cnst_list = NULL;
621 xbt_swag_t var_list = NULL;
622 xbt_swag_t elem_list = NULL;
623 double min_usage = -1;
624 double min_bound = -1;
626 if (!(sys->modified))
629 XBT_IN("(sys=%p)", sys);
632 * Compute Usage and store the variables that reach the maximum. If selective_update_active is true, only constraints that changed are considered. Otherwise all constraints with active actions are considered.
636 selective_update_active ? &(sys->modified_constraint_set) :
637 &(sys->active_constraint_set);
639 XBT_DEBUG("Active constraints : %d", xbt_swag_size(cnst_list));
640 /* Init: Only modified code portions: reset the value of active variables */
641 xbt_swag_foreach(_cnst, cnst_list) {
642 cnst = (lmm_constraint_t)_cnst;
643 elem_list = &(cnst->element_set);
644 //XBT_DEBUG("Variable set : %d", xbt_swag_size(elem_list));
645 xbt_swag_foreach(_elem, elem_list) {
646 var = ((lmm_element_t)_elem)->variable;
647 if (var->weight <= 0.0)
653 s_lmm_constraint_light_t *cnst_light_tab = (s_lmm_constraint_light_t *)xbt_malloc0(xbt_swag_size(cnst_list)*sizeof(s_lmm_constraint_light_t));
654 int cnst_light_num = 0;
655 dyn_light_t saturated_constraint_set = xbt_new0(s_dyn_light_t,1);
656 saturated_constraint_set->size = 5;
657 saturated_constraint_set->data = xbt_new0(int, saturated_constraint_set->size);
659 xbt_swag_foreach_safe(_cnst, _cnst_next, cnst_list) {
660 cnst = (lmm_constraint_t)_cnst;
661 /* INIT: Collect constraints that actually need to be saturated (i.e remaining and usage are strictly positive) into cnst_light_tab. */
662 cnst->remaining = cnst->bound;
663 if (!double_positive(cnst->remaining, cnst->bound*sg_maxmin_precision))
666 elem_list = &(cnst->element_set);
667 xbt_swag_foreach(_elem, elem_list) {
668 elem = (lmm_element_t)_elem;
669 /* 0-weighted elements (ie, sleep actions) are at the end of the swag and we don't want to consider them */
670 if (elem->variable->weight <= 0)
672 if ((elem->value > 0)) {
674 cnst->usage += elem->value / elem->variable->weight;
675 else if (cnst->usage < elem->value / elem->variable->weight)
676 cnst->usage = elem->value / elem->variable->weight;
678 make_elem_active(elem);
679 ActionPtr action = static_cast<ActionPtr>(elem->variable->id);
680 if (sys->keep_track && !action->is_linked())
681 sys->keep_track->push_back(*action);
684 XBT_DEBUG("Constraint '%d' usage: %f remaining: %f ", cnst->id_int, cnst->usage, cnst->remaining);
685 /* Saturated constraints update */
687 if(cnst->usage > 0) {
688 cnst_light_tab[cnst_light_num].cnst = cnst;
689 cnst->cnst_light = &(cnst_light_tab[cnst_light_num]);
690 cnst_light_tab[cnst_light_num].remaining_over_usage = cnst->remaining / cnst->usage;
691 saturated_constraint_set_update(cnst_light_tab[cnst_light_num].remaining_over_usage,
692 cnst_light_num, saturated_constraint_set, &min_usage);
693 xbt_assert(cnst->active_element_set.count>0, "There is no sense adding a constraint that has no active element!" );
698 saturated_variable_set_update( cnst_light_tab,
699 saturated_constraint_set,
702 /* Saturated variables update */
705 /* Fix the variables that have to be */
706 var_list = &(sys->saturated_variable_set);
708 xbt_swag_foreach(_var, var_list) {
709 var = (lmm_variable_t)_var;
710 if (var->weight <= 0.0)
712 /* First check if some of these variables could reach their upper
713 bound and update min_bound accordingly. */
715 ("var=%d, var->bound=%f, var->weight=%f, min_usage=%f, var->bound*var->weight=%f",
716 var->id_int, var->bound, var->weight, min_usage,
717 var->bound * var->weight);
718 if ((var->bound > 0) && (var->bound * var->weight < min_usage)) {
720 min_bound = var->bound*var->weight;
722 min_bound = MIN(min_bound, (var->bound*var->weight));
723 XBT_DEBUG("Updated min_bound=%f", min_bound);
728 while ((var = (lmm_variable_t)xbt_swag_getFirst(var_list))) {
732 //If no variable could reach its bound, deal iteratively the constraints usage ( at worst one constraint is saturated at each cycle)
733 var->value = min_usage / var->weight;
734 XBT_DEBUG("Setting %p (%d) value to %f\n", var, var->id_int, var->value);
736 //If there exist a variable that can reach its bound, only update it (and other with the same bound) for now.
737 if (double_equals(min_bound, var->bound*var->weight, sg_maxmin_precision)){
738 var->value = var->bound;
739 XBT_DEBUG("Setting %p (%d) value to %f\n", var, var->id_int, var->value);
742 // Variables which bound is different are not considered for this cycle, but they will be afterwards.
743 XBT_DEBUG("Do not consider %p (%d) \n", var, var->id_int);
744 xbt_swag_remove(var, var_list);
748 XBT_DEBUG("Min usage: %f, Var(%d)->weight: %f, Var(%d)->value: %f ",
749 min_usage, var->id_int, var->weight, var->id_int, var->value);
752 /* Update the usage of contraints where this variable is involved */
753 for (i = 0; i < var->cnsts_number; i++) {
754 elem = &var->cnsts[i];
755 cnst = elem->constraint;
757 //Remember: shared constraints require that sum(elem->value * var->value) < cnst->bound
758 double_update(&(cnst->remaining), elem->value * var->value, cnst->bound*sg_maxmin_precision);
759 double_update(&(cnst->usage), elem->value / var->weight, sg_maxmin_precision);
760 //If the constraint is saturated, remove it from the set of active constraints (light_tab)
761 if(!double_positive(cnst->usage,sg_maxmin_precision) || !double_positive(cnst->remaining,cnst->bound*sg_maxmin_precision)) {
762 if (cnst->cnst_light) {
763 int index = (cnst->cnst_light-cnst_light_tab);
764 XBT_DEBUG("index: %d \t cnst_light_num: %d \t || \t cnst: %p \t cnst->cnst_light: %p \t cnst_light_tab: %p usage: %f remaining: %f bound: %f ",
765 index,cnst_light_num, cnst, cnst->cnst_light, cnst_light_tab, cnst->usage, cnst->remaining, cnst->bound);
766 cnst_light_tab[index]=cnst_light_tab[cnst_light_num-1];
767 cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
769 cnst->cnst_light = NULL;
772 cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
774 make_elem_inactive(elem);
776 //Remember: non-shared constraints only require that max(elem->value * var->value) < cnst->bound
778 make_elem_inactive(elem);
779 elem_list = &(cnst->element_set);
780 xbt_swag_foreach(_elem, elem_list) {
781 elem = (lmm_element_t)_elem;
782 if (elem->variable->weight <= 0) break; //Found an inactive variable -> no more active variables
783 if (elem->variable->value > 0) continue;
785 cnst->usage = MAX(cnst->usage, elem->value / elem->variable->weight);
787 //If the constraint is saturated, remove it from the set of active constraints (light_tab)
788 if(!double_positive(cnst->usage,sg_maxmin_precision) || !double_positive(cnst->remaining,cnst->bound*sg_maxmin_precision)) {
789 if(cnst->cnst_light) {
790 int index = (cnst->cnst_light-cnst_light_tab);
791 XBT_DEBUG("index: %d \t cnst_light_num: %d \t || \t cnst: %p \t cnst->cnst_light: %p \t cnst_light_tab: %p usage: %f remaining: %f bound: %f ",
792 index,cnst_light_num, cnst, cnst->cnst_light, cnst_light_tab, cnst->usage, cnst->remaining, cnst->bound);
793 cnst_light_tab[index]=cnst_light_tab[cnst_light_num-1];
794 cnst_light_tab[index].cnst->cnst_light = &cnst_light_tab[index];
796 cnst->cnst_light = NULL;
799 cnst->cnst_light->remaining_over_usage = cnst->remaining / cnst->usage;
800 xbt_assert(cnst->active_element_set.count>0, "Should not keep a maximum constraint that has no active element! You want to check the maxmin precision and possible rounding effects." );
804 xbt_swag_remove(var, var_list);
807 /* Find out which variables reach the maximum */
810 saturated_constraint_set->pos = 0;
812 for(pos=0; pos<cnst_light_num; pos++){
813 xbt_assert(cnst_light_tab[pos].cnst->active_element_set.count>0, "Cannot saturate more a constraint that has no active element! You may want to change the maxmin precision (--cfg=maxmin/precision:<new_value>) because of possible rounding effects.\n\tFor the record, the usage of this constraint is %g while the maxmin precision to which it is compared is %g.\n\tThe usage of the previous constraint is %g.", cnst_light_tab[pos].cnst->usage, sg_maxmin_precision, cnst_light_tab[pos-1].cnst->usage);
814 saturated_constraint_set_update(
815 cnst_light_tab[pos].remaining_over_usage,
817 saturated_constraint_set,
821 saturated_variable_set_update( cnst_light_tab,
822 saturated_constraint_set,
825 } while (cnst_light_num > 0);
828 if (sys->selective_update_active)
829 lmm_remove_all_modified_set(sys);
831 if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
835 xbt_free(saturated_constraint_set->data);
836 xbt_free(saturated_constraint_set);
837 xbt_free(cnst_light_tab);
841 /* Not a O(1) function */
843 void lmm_update(lmm_system_t sys, lmm_constraint_t cnst,
844 lmm_variable_t var, double value)
848 for (i = 0; i < var->cnsts_number; i++)
849 if (var->cnsts[i].constraint == cnst) {
850 var->cnsts[i].value = value;
852 lmm_update_modified_set(sys, cnst);
857 /** \brief Attribute the value bound to var->bound.
859 * \param sys the lmm_system_t
860 * \param var the lmm_variable_t
861 * \param bound the new bound to associate with var
863 * Makes var->bound equal to bound. Whenever this function is called
864 * a change is signed in the system. To
865 * avoid false system changing detection it is a good idea to test
866 * (bound != 0) before calling it.
869 void lmm_update_variable_bound(lmm_system_t sys, lmm_variable_t var,
875 if (var->cnsts_number)
876 lmm_update_modified_set(sys, var->cnsts[0].constraint);
880 void lmm_update_variable_weight(lmm_system_t sys, lmm_variable_t var,
886 if (weight == var->weight)
888 XBT_IN("(sys=%p, var=%p, weight=%f)", sys, var, weight);
890 var->weight = weight;
891 xbt_swag_remove(var, &(sys->variable_set));
893 xbt_swag_insert_at_head(var, &(sys->variable_set));
895 xbt_swag_insert_at_tail(var, &(sys->variable_set));
897 for (i = 0; i < var->cnsts_number; i++) {
898 elem = &var->cnsts[i];
899 xbt_swag_remove(elem, &(elem->constraint->element_set));
901 xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
903 xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
906 lmm_update_modified_set(sys, elem->constraint);
914 double lmm_get_variable_weight(lmm_variable_t var)
919 void lmm_update_constraint_bound(lmm_system_t sys,
920 lmm_constraint_t cnst,
924 lmm_update_modified_set(sys, cnst);
928 int lmm_constraint_used(lmm_system_t sys, lmm_constraint_t cnst)
930 return xbt_swag_belongs(cnst, &(sys->active_constraint_set));
933 XBT_INLINE lmm_constraint_t lmm_get_first_active_constraint(lmm_system_t
936 return (lmm_constraint_t)xbt_swag_getFirst(&(sys->active_constraint_set));
939 XBT_INLINE lmm_constraint_t lmm_get_next_active_constraint(lmm_system_t
944 return (lmm_constraint_t)xbt_swag_getNext(cnst, (sys->active_constraint_set).offset);
947 #ifdef HAVE_LATENCY_BOUND_TRACKING
948 XBT_INLINE int lmm_is_variable_limited_by_latency(lmm_variable_t var)
950 return (double_equals(var->bound, var->value, var->bound*sg_maxmin_precision));
955 /** \brief Update the constraint set propagating recursively to
956 * other constraints so the system should not be entirely computed.
958 * \param sys the lmm_system_t
959 * \param cnst the lmm_constraint_t affected by the change
961 * A recursive algorithm to optimize the system recalculation selecting only
962 * constraints that have changed. Each constraint change is propagated
963 * to the list of constraints for each variable.
965 static void lmm_update_modified_set_rec(lmm_system_t sys,
966 lmm_constraint_t cnst)
970 xbt_swag_foreach(_elem, &cnst->element_set) {
971 lmm_variable_t var = ((lmm_element_t)_elem)->variable;
972 s_lmm_element_t *cnsts = var->cnsts;
974 for (i = 0; var->visited != sys->visited_counter
975 && i < var->cnsts_number ; i++) {
976 if (cnsts[i].constraint != cnst
977 && !xbt_swag_belongs(cnsts[i].constraint,
978 &sys->modified_constraint_set)) {
979 xbt_swag_insert(cnsts[i].constraint, &sys->modified_constraint_set);
980 lmm_update_modified_set_rec(sys, cnsts[i].constraint);
983 var->visited = sys->visited_counter;
987 static void lmm_update_modified_set(lmm_system_t sys,
988 lmm_constraint_t cnst)
990 /* nothing to do if selective update isn't active */
991 if (sys->selective_update_active
992 && !xbt_swag_belongs(cnst, &sys->modified_constraint_set)) {
993 xbt_swag_insert(cnst, &sys->modified_constraint_set);
994 lmm_update_modified_set_rec(sys, cnst);
998 /** \brief Remove all constraints of the modified_constraint_set.
1000 * \param sys the lmm_system_t
1002 static void lmm_remove_all_modified_set(lmm_system_t sys)
1004 if (++sys->visited_counter == 1) {
1005 /* the counter wrapped around, reset each variable->visited */
1007 xbt_swag_foreach(_var, &sys->variable_set)
1008 ((lmm_variable_t)_var)->visited = 0;
1010 xbt_swag_reset(&sys->modified_constraint_set);
1014 * Returns total resource load
1016 * \param cnst the lmm_constraint_t associated to the resource
1019 double lmm_constraint_get_usage(lmm_constraint_t cnst) {
1021 xbt_swag_t elem_list = &(cnst->element_set);
1023 lmm_element_t elem = NULL;
1025 xbt_swag_foreach(_elem, elem_list) {
1026 elem = (lmm_element_t)_elem;
1027 /* 0-weighted elements (ie, sleep actions) are at the end of the swag and we don't want to consider them */
1028 if (elem->variable->weight <= 0)
1030 if ((elem->value > 0)) {
1032 usage += elem->value * elem->variable->value;
1033 else if (usage < elem->value * elem->variable->value)
1034 usage = elem->value * elem->variable->value;