Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a safe version of lmm_get_var_from_cnst to avoid looping forever when iterating...
authorAugustin Degomme <augustin.degomme@imag.fr>
Tue, 14 Oct 2014 12:24:22 +0000 (14:24 +0200)
committerAugustin Degomme <augustin.degomme@imag.fr>
Thu, 16 Oct 2014 13:04:04 +0000 (15:04 +0200)
src/include/surf/maxmin.h
src/surf/maxmin.cpp
src/surf/network_cm02.cpp

index cf32554..2a5dc0a 100644 (file)
@@ -284,6 +284,24 @@ XBT_PUBLIC(lmm_variable_t) lmm_get_var_from_cnst(lmm_system_t sys,
                                      lmm_constraint_t cnst,
                                      lmm_element_t * elem);
 
                                      lmm_constraint_t cnst,
                                      lmm_element_t * elem);
 
+/**
+ * @brief Get a var associated to a constraint
+ * @details Get the first variable of the next variable of elem if elem is not NULL
+ *
+ * @param sys The system associated to the variable (not used)
+ * @param cnst A constraint
+ * @param elem A element of constraint of the constraint or NULL
+ * @param nextelem A element of constraint of the constraint or NULL, the one after elem
+ * @param numelem parameter representing the number of elements to go
+ *
+ * @return A variable associated to a constraint
+ */
+XBT_PUBLIC(lmm_variable_t) lmm_get_var_from_cnst_safe(lmm_system_t /*sys*/,
+                                     lmm_constraint_t cnst,
+                                     lmm_element_t * elem,
+                                     lmm_element_t * nextelem,
+                                     int * numelem);
+
 /**
  * @brief Get the first active constraint of a system
  * 
 /**
  * @brief Get the first active constraint of a system
  * 
index cd74d2a..ba1f601 100644 (file)
@@ -431,6 +431,31 @@ lmm_variable_t lmm_get_var_from_cnst(lmm_system_t /*sys*/,
     return NULL;
 }
 
     return NULL;
 }
 
+//if we modify the swag between calls, normal version may loop forever
+//this safe version ensures that we browse the swag elements only once
+lmm_variable_t lmm_get_var_from_cnst_safe(lmm_system_t /*sys*/,
+                                     lmm_constraint_t cnst,
+                                     lmm_element_t * elem,
+                                     lmm_element_t * nextelem,
+                                     int * numelem)
+{
+  if (!(*elem)){
+    *elem = (lmm_element_t) xbt_swag_getFirst(&(cnst->element_set));
+    *numelem = xbt_swag_size(&(cnst->element_set))-1;
+  }else{
+    *elem = *nextelem;
+    if(*numelem>0){
+     (*numelem) --;
+    }else
+      return NULL;
+  }
+  if (*elem){
+    *nextelem = (lmm_element_t) xbt_swag_getNext(*elem, cnst->element_set.offset);
+    return (*elem)->variable;
+  }else
+    return NULL;
+}
+
 void *lmm_constraint_id(lmm_constraint_t cnst)
 {
   return cnst->id;
 void *lmm_constraint_id(lmm_constraint_t cnst)
 {
   return cnst->id;
index 637a243..fe4d32d 100644 (file)
@@ -614,6 +614,9 @@ void NetworkCm02Link::updateBandwidth(double value, double date){
                  (p_power.peak * p_power.scale);
   lmm_variable_t var = NULL;
   lmm_element_t elem = NULL;
                  (p_power.peak * p_power.scale);
   lmm_variable_t var = NULL;
   lmm_element_t elem = NULL;
+  lmm_element_t nextelem = NULL;
+  int numelem = 0;
+
   NetworkCm02ActionPtr action = NULL;
 
   p_power.peak = value;
   NetworkCm02ActionPtr action = NULL;
 
   p_power.peak = value;
@@ -625,7 +628,7 @@ void NetworkCm02Link::updateBandwidth(double value, double date){
   TRACE_surf_link_set_bandwidth(date, getName(), sg_bandwidth_factor * p_power.peak * p_power.scale);
 #endif
   if (sg_weight_S_parameter > 0) {
   TRACE_surf_link_set_bandwidth(date, getName(), sg_bandwidth_factor * p_power.peak * p_power.scale);
 #endif
   if (sg_weight_S_parameter > 0) {
-    while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
+    while ((var = lmm_get_var_from_cnst_safe(getModel()->getMaxminSystem(), getConstraint(), &elem, &nextelem, &numelem))) {
       action = (NetworkCm02ActionPtr) lmm_variable_id(var);
       action->m_weight += delta;
       if (!action->isSuspended())
       action = (NetworkCm02ActionPtr) lmm_variable_id(var);
       action->m_weight += delta;
       if (!action->isSuspended())
@@ -638,10 +641,12 @@ void NetworkCm02Link::updateLatency(double value, double date){
   double delta = value - m_latCurrent;
   lmm_variable_t var = NULL;
   lmm_element_t elem = NULL;
   double delta = value - m_latCurrent;
   lmm_variable_t var = NULL;
   lmm_element_t elem = NULL;
+  lmm_element_t nextelem = NULL;
+  int numelem = 0;
   NetworkCm02ActionPtr action = NULL;
 
   m_latCurrent = value;
   NetworkCm02ActionPtr action = NULL;
 
   m_latCurrent = value;
-  while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), getConstraint(), &elem))) {
+  while ((var = lmm_get_var_from_cnst_safe(getModel()->getMaxminSystem(), getConstraint(), &elem, &nextelem, &numelem))) {
     action = (NetworkCm02ActionPtr) lmm_variable_id(var);
     action->m_latCurrent += delta;
     action->m_weight += delta;
     action = (NetworkCm02ActionPtr) lmm_variable_id(var);
     action->m_latCurrent += delta;
     action->m_weight += delta;