Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
function to get the weight of a constraint of a lmm variable
[simgrid.git] / src / surf / maxmin.c
index 34fb128..349093c 100644 (file)
@@ -25,7 +25,6 @@ static void lmm_update_modified_set(lmm_system_t sys,
 static void lmm_remove_all_modified_set(lmm_system_t sys);
 static int Global_debug_id = 1;
 static int Global_const_debug_id = 1;
-extern xbt_swag_t keep_track;
 
 lmm_system_t lmm_system_new(int selective_update)
 {
@@ -37,6 +36,7 @@ lmm_system_t lmm_system_new(int selective_update)
 
   l->modified = 0;
   l->selective_update_active = selective_update;
+  l->visited_counter = 1;
 
   XBT_DEBUG("Setting selective_update_active flag to %d\n",
          l->selective_update_active);
@@ -211,7 +211,7 @@ lmm_variable_t lmm_variable_new(lmm_system_t sys, void *id,
   var->weight = weight;
   var->bound = bound;
   var->value = 0.0;
-
+  var->visited = sys->visited_counter - 1;
   var->mu = 0.0;
   var->new_mu = 0.0;
   var->func_f = func_f_def;
@@ -320,6 +320,16 @@ XBT_INLINE lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t sys,
     return NULL;
 }
 
+XBT_INLINE double lmm_get_cnst_weight_from_var(lmm_system_t sys,
+                                                         lmm_variable_t var,
+                                                         int num)
+{
+  if (num < var->cnsts_number)
+    return (var->cnsts[num].value);
+  else
+    return 0.0;
+}
+
 XBT_INLINE int lmm_get_number_of_cnst_from_var(lmm_system_t sys,
                                                lmm_variable_t var)
 {
@@ -350,7 +360,7 @@ XBT_INLINE void *lmm_variable_id(lmm_variable_t var)
   return var->id;
 }
 
-static XBT_INLINE void saturated_constraint_set_update(lmm_system_t sys,
+static XBT_INLINE int saturated_constraint_set_update(lmm_system_t sys,
                                                        lmm_constraint_t
                                                        cnst,
                                                        double *min_usage)
@@ -360,11 +370,11 @@ static XBT_INLINE void saturated_constraint_set_update(lmm_system_t sys,
   XBT_IN("sys=%p, cnst=%p, min_usage=%f", sys, cnst, *min_usage);
   if (cnst->usage <= 0) {
     XBT_OUT();
-    return;
+    return 1;
   }
   if (cnst->remaining <= 0) {
     XBT_OUT();
-    return;
+    return 1;
   }
   if ((*min_usage < 0) || (*min_usage > cnst->remaining / cnst->usage)) {
     *min_usage = cnst->remaining / cnst->usage;
@@ -380,6 +390,7 @@ static XBT_INLINE void saturated_constraint_set_update(lmm_system_t sys,
     xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
   }
   XBT_OUT();
+  return 0;
 }
 
 static XBT_INLINE void saturated_variable_set_update(lmm_system_t sys)
@@ -500,6 +511,7 @@ void lmm_solve(lmm_system_t sys)
 {
   lmm_variable_t var = NULL;
   lmm_constraint_t cnst = NULL;
+  lmm_constraint_t cnst_next = NULL;
   lmm_element_t elem = NULL;
   xbt_swag_t cnst_list = NULL;
   xbt_swag_t var_list = NULL;
@@ -533,7 +545,7 @@ void lmm_solve(lmm_system_t sys)
     }
   }
 
-  xbt_swag_foreach(cnst, cnst_list) {
+  xbt_swag_foreach_safe(cnst, cnst_next, cnst_list) {
     /* INIT */
     cnst->remaining = cnst->bound;
     if (cnst->remaining == 0)
@@ -551,13 +563,16 @@ void lmm_solve(lmm_system_t sys)
           cnst->usage = elem->value / elem->variable->weight;
 
         make_elem_active(elem);
-        if(keep_track){
-            xbt_swag_insert((elem->variable)->id, keep_track);
-        }
+        if (sys->keep_track)
+          xbt_swag_insert(elem->variable->id, sys->keep_track);
       }
     }
     XBT_DEBUG("Constraint Usage '%d' : %f", cnst->id_int, cnst->usage);
     /* Saturated constraints update */
+    if(cnst->usage>0) {
+      xbt_swag_remove(cnst, cnst_list);
+      xbt_swag_insert_at_head(cnst, cnst_list);
+    }
     saturated_constraint_set_update(sys, cnst, &min_usage);
   }
   saturated_variable_set_update(sys);
@@ -612,6 +627,10 @@ void lmm_solve(lmm_system_t sys)
         if (cnst->shared) {
           double_update(&(cnst->remaining), elem->value * var->value);
           double_update(&(cnst->usage), elem->value / var->weight);
+          if(cnst->usage<=0 || cnst->remaining<=0) {
+            xbt_swag_remove(cnst, cnst_list);
+            xbt_swag_insert_at_tail(cnst, cnst_list);
+          }
           make_elem_inactive(elem);
         } else {                /* FIXME one day: We recompute usage.... :( */
           cnst->usage = 0.0;
@@ -638,8 +657,9 @@ void lmm_solve(lmm_system_t sys)
     /* Find out which variables reach the maximum */
     min_usage = -1;
     min_bound = -1;
+
     xbt_swag_foreach(cnst, cnst_list) {
-      saturated_constraint_set_update(sys, cnst, &min_usage);
+      if(saturated_constraint_set_update(sys, cnst, &min_usage)) break;
     }
     saturated_variable_set_update(sys);
 
@@ -718,13 +738,13 @@ void lmm_update_variable_weight(lmm_system_t sys, lmm_variable_t var,
       xbt_swag_insert_at_head(elem, &(elem->constraint->element_set));
     else
       xbt_swag_insert_at_tail(elem, &(elem->constraint->element_set));
+
+    if (i == 0)
+      lmm_update_modified_set(sys, elem->constraint);
   }
   if (!weight)
     var->value = 0.0;
 
-  if (var->cnsts_number)
-     lmm_update_modified_set(sys, var->cnsts[0].constraint);
-
   XBT_OUT();
 }
 
@@ -788,7 +808,8 @@ static void lmm_update_modified_set_rec(lmm_system_t sys,
     lmm_variable_t var = elem->variable;
     s_lmm_element_t *cnsts = var->cnsts;
     int i;
-    for (i = 0; i < var->cnsts_number; i++) {
+    for (i = 0; var->visited != sys->visited_counter
+             && i < var->cnsts_number ; i++) {
       if (cnsts[i].constraint != cnst
           && !xbt_swag_belongs(cnsts[i].constraint,
                                &sys->modified_constraint_set)) {
@@ -796,6 +817,7 @@ static void lmm_update_modified_set_rec(lmm_system_t sys,
         lmm_update_modified_set_rec(sys, cnsts[i].constraint);
       }
     }
+    var->visited = sys->visited_counter;
   }
 }
 
@@ -816,5 +838,11 @@ static void lmm_update_modified_set(lmm_system_t sys,
  */
 static void lmm_remove_all_modified_set(lmm_system_t sys)
 {
+  if (++sys->visited_counter == 1) {
+    /* the counter wrapped around, reset each variable->visited */
+    lmm_variable_t var;
+    xbt_swag_foreach(var, &sys->variable_set)
+      var->visited = 0;
+  }
   xbt_swag_reset(&sys->modified_constraint_set);
 }