Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix Memleaks
[simgrid.git] / src / surf / maxmin.c
index a441d1e..5eee02a 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2004-2011. The SimGrid Team.
+/* Copyright (c) 2004-2013. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -21,7 +21,7 @@ typedef struct s_dyn_light {
   int size;
 } s_dyn_light_t, *dyn_light_t;
 
-double sg_maxmin_precision = 0.00001;
+XBT_EXPORT_NO_IMPORT(double) sg_maxmin_precision = 0.00001;
 
 static void *lmm_variable_mallocator_new_f(void);
 static void lmm_variable_mallocator_free_f(void *var);
@@ -32,6 +32,10 @@ static void lmm_remove_all_modified_set(lmm_system_t sys);
 static int Global_debug_id = 1;
 static int Global_const_debug_id = 1;
 
+static void lmm_var_free(lmm_system_t sys, lmm_variable_t var);
+static XBT_INLINE void lmm_cnst_free(lmm_system_t sys,
+                                     lmm_constraint_t cnst);
+
 lmm_system_t lmm_system_new(int selective_update)
 {
   lmm_system_t l = NULL;
@@ -89,7 +93,7 @@ void lmm_system_free(lmm_system_t sys)
   free(sys);
 }
 
-XBT_INLINE void lmm_variable_disable(lmm_system_t sys, lmm_variable_t var)
+static XBT_INLINE void lmm_variable_disable(lmm_system_t sys, lmm_variable_t var)
 {
   int i;
   int n;
@@ -254,6 +258,90 @@ XBT_INLINE double lmm_variable_getbound(lmm_variable_t var)
   return (var->bound);
 }
 
+/* Replace the content of elem_a with elem_b. The content of elem_b is cleared. */
+static void renew_elem_entry(lmm_element_t elem_a, lmm_element_t elem_b)
+{
+    elem_a->constraint = elem_b->constraint;
+    elem_a->variable   = elem_b->variable;
+    elem_a->value      = elem_b->value;
+
+    /* If elem_b is in the element_set swag, register the new element to the swag. */
+    if (xbt_swag_remove(elem_b, &(elem_b->constraint->element_set))) {
+      if (elem_a->variable->weight)
+        xbt_swag_insert_at_head(elem_a, &(elem_a->constraint->element_set));
+      else
+        xbt_swag_insert_at_tail(elem_a, &(elem_a->constraint->element_set));
+    }
+
+    if (xbt_swag_remove(elem_b, &(elem_b->constraint->active_element_set))) {
+      if (elem_a->variable->weight)
+        xbt_swag_insert_at_head(elem_a, &(elem_a->constraint->active_element_set));
+      else
+        xbt_swag_insert_at_tail(elem_a, &(elem_a->constraint->active_element_set));
+    }
+
+    elem_b->constraint = NULL;
+    elem_b->variable   = NULL;
+    elem_b->value      = 0;
+}
+
+void lmm_shrink(lmm_system_t sys, lmm_constraint_t cnst,
+                lmm_variable_t var)
+{
+  lmm_element_t elem = NULL;
+  int found = 0;
+
+  int i;
+  for (i = 0; i < var->cnsts_number; i++) {
+    elem = &(var->cnsts[i]);
+    if (elem->constraint == cnst) {
+      found = 1;
+      break;
+    }
+  }
+
+  if (!found) {
+    XBT_DEBUG("cnst %p is not found in var %p", cnst, var);
+    return;
+  }
+
+  sys->modified = 1;
+
+  XBT_DEBUG("remove elem(value %lf, cnst %p, var %p) in var %p",
+      elem->value, elem->constraint, elem->variable, var);
+
+
+
+  /* We are going to change the constraint object and the variable object.
+   * Propagate this change to other objects. Calling here (not after
+   * modification) is correct? */
+  lmm_update_modified_set(sys, cnst);
+  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].
+
+
+
+  /* now var->cnsts[i] is not necessary any more */
+
+  xbt_swag_remove(elem, &(elem->constraint->element_set));
+  xbt_swag_remove(elem, &(elem->constraint->active_element_set));
+  elem->constraint = NULL;
+  elem->variable = NULL;
+  elem->value = 0;
+
+
+
+  /* We do not want to have an empty element entry before the last entry. So,
+   * plug up the hole with the last one. */
+  if (i < var->cnsts_number - 1)
+    renew_elem_entry(&var->cnsts[i], &var->cnsts[var->cnsts_number - 1]);
+
+  var->cnsts_number -= 1;
+
+
+  if (xbt_swag_size(&(cnst->element_set)) == 0)
+    make_constraint_inactive(sys, cnst);
+}
+
 void lmm_expand(lmm_system_t sys, lmm_constraint_t cnst,
                 lmm_variable_t var, double value)
 {
@@ -385,7 +473,7 @@ static XBT_INLINE void saturated_constraint_set_update(double usage,
   } else if (*min_usage == usage) {
     if(saturated_constraint_set->pos == saturated_constraint_set->size) { // realloc the size
       saturated_constraint_set->size *= 2;
-      saturated_constraint_set->data = realloc(saturated_constraint_set->data, (saturated_constraint_set->size) * sizeof(int));
+      saturated_constraint_set->data = xbt_realloc(saturated_constraint_set->data, (saturated_constraint_set->size) * sizeof(int));
     }
     saturated_constraint_set->data[saturated_constraint_set->pos] = cnst_light_num;
     saturated_constraint_set->pos++;
@@ -659,7 +747,6 @@ void lmm_solve(lmm_system_t sys)
           make_elem_inactive(elem);
           elem_list = &(cnst->element_set);
           xbt_swag_foreach(elem, elem_list) {
-              make_elem_active(elem);
             if (elem->variable->weight <= 0 || elem->variable->value > 0)
               break;
             if (elem->value > 0)
@@ -886,3 +973,30 @@ static void lmm_remove_all_modified_set(lmm_system_t sys)
   }
   xbt_swag_reset(&sys->modified_constraint_set);
 }
+
+/**
+ *  Returns total resource load
+ *
+ *  \param cnst the lmm_constraint_t associated to the resource
+ *
+ */
+double lmm_constraint_get_usage(lmm_constraint_t cnst) {
+   double usage = 0.0;
+   xbt_swag_t elem_list = &(cnst->element_set);
+   lmm_element_t elem = NULL;
+
+   xbt_swag_foreach(elem, elem_list) {
+     /* 0-weighted elements (ie, sleep actions) are at the end of the swag and we don't want to consider them */
+     if (elem->variable->weight <= 0)
+       break;
+     if ((elem->value > 0)) {
+       if (cnst->shared)
+         usage += elem->value * elem->variable->value;
+       else if (usage < elem->value * elem->variable->value)
+         usage = elem->value * elem->variable->value;
+     }
+   }
+  return usage;
+}
+
+