Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Initialize fields in initialization list (sonar).
[simgrid.git] / src / surf / maxmin.cpp
index c28faae..f4a86af 100644 (file)
@@ -5,7 +5,7 @@
 
 /* \file callbacks.h */
 
-#include "maxmin_private.hpp"
+#include "surf/maxmin.hpp"
 #include "xbt/backtrace.hpp"
 #include "xbt/log.h"
 #include "xbt/mallocator.h"
@@ -25,12 +25,9 @@ double sg_maxmin_precision = 0.00001; /* Change this with --cfg=maxmin/precision
 double sg_surf_precision   = 0.00001; /* Change this with --cfg=surf/precision:VALUE */
 int sg_concurrency_limit   = -1;      /* Change this with --cfg=maxmin/concurrency-limit:VALUE */
 
-static int Global_debug_id = 1;
+int s_lmm_variable_t::Global_debug_id   = 1;
 int s_lmm_constraint_t::Global_debug_id = 1;
 
-static int lmm_can_enable_var(lmm_variable_t var);
-static int lmm_cnstrs_min_concurrency_slack(lmm_variable_t var);
-
 int s_lmm_element_t::get_concurrency() const
 {
   //Ignore element with weight less than one (e.g. cross-traffic)
@@ -85,7 +82,7 @@ void s_lmm_system_t::check_concurrency()
       lmm_element_t elem = (lmm_element_t)elemIt;
       // We should have staged variables only if concurrency is reached in some constraint
       xbt_assert(cnst->get_concurrency_limit() < 0 || elem->variable->staged_weight == 0 ||
-                     lmm_cnstrs_min_concurrency_slack(elem->variable) < elem->variable->concurrency_share,
+                     elem->variable->get_min_concurrency_slack() < elem->variable->concurrency_share,
                  "should not have staged variable!");
     }
 
@@ -122,7 +119,7 @@ void s_lmm_system_t::check_concurrency()
 void s_lmm_system_t::var_free(lmm_variable_t var)
 {
   XBT_IN("(sys=%p, var=%p)", this, var);
-  modified = 1;
+  modified = true;
 
   // TODOLATER Can do better than that by leaving only the variable in only one enabled_element_set, call
   // update_modified_set, and then remove it..
@@ -151,13 +148,12 @@ void s_lmm_system_t::var_free(lmm_variable_t var)
   XBT_OUT();
 }
 
-s_lmm_system_t::s_lmm_system_t(bool selective_update)
+s_lmm_system_t::s_lmm_system_t(bool selective_update) : selective_update_active(selective_update)
 {
   s_lmm_variable_t var;
   s_lmm_constraint_t cnst;
 
-  modified                = 0;
-  selective_update_active = selective_update;
+  modified                = false;
   visited_counter         = 1;
 
   XBT_DEBUG("Setting selective_update_active flag to %d", selective_update_active);
@@ -200,11 +196,10 @@ void s_lmm_system_t::cnst_free(lmm_constraint_t cnst)
   delete cnst;
 }
 
-s_lmm_constraint_t::s_lmm_constraint_t(void* id_value, double bound_value)
+s_lmm_constraint_t::s_lmm_constraint_t(void* id_value, double bound_value) : bound(bound_value), id(id_value)
 {
   s_lmm_element_t elem;
 
-  id     = id_value;
   id_int = Global_debug_id++;
   xbt_swag_init(&enabled_element_set, xbt_swag_offset(elem, enabled_element_set_hookup));
   xbt_swag_init(&disabled_element_set, xbt_swag_offset(elem, disabled_element_set_hookup));
@@ -212,7 +207,6 @@ s_lmm_constraint_t::s_lmm_constraint_t(void* id_value, double bound_value)
 
   remaining           = 0.0;
   usage               = 0.0;
-  bound               = bound_value;
   concurrency_limit   = sg_concurrency_limit;
   concurrency_current = 0;
   concurrency_maximum = 0;
@@ -245,27 +239,8 @@ lmm_variable_t s_lmm_system_t::variable_new(simgrid::surf::Action* id, double sh
 {
   XBT_IN("(sys=%p, id=%p, weight=%f, bound=%f, num_cons =%d)", this, id, sharing_weight, bound, number_of_constraints);
 
-  lmm_variable_t var = (lmm_variable_t)xbt_mallocator_get(variable_mallocator);
-  var->id = id;
-  var->id_int = Global_debug_id++;
-  var->cnsts.reserve(number_of_constraints);
-  var->sharing_weight    = sharing_weight;
-  var->staged_weight = 0.0;
-  var->bound = bound;
-  var->concurrency_share = 1;
-  var->value = 0.0;
-  var->visited           = visited_counter - 1;
-  var->mu = 0.0;
-  var->new_mu = 0.0;
-  var->func_f = func_f_def;
-  var->func_fp = func_fp_def;
-  var->func_fpi = func_fpi_def;
-
-  var->variable_set_hookup.next = nullptr;
-  var->variable_set_hookup.prev = nullptr;
-  var->saturated_variable_set_hookup.next = nullptr;
-  var->saturated_variable_set_hookup.prev = nullptr;
-
+  lmm_variable_t var = static_cast<lmm_variable_t>(xbt_mallocator_get(variable_mallocator));
+  var->initialize(id, sharing_weight, bound, number_of_constraints, visited_counter - 1);
   if (sharing_weight)
     xbt_swag_insert_at_head(var, &variable_set);
   else
@@ -281,24 +256,9 @@ void s_lmm_system_t::variable_free(lmm_variable_t var)
   var_free(var);
 }
 
-double lmm_variable_getvalue(lmm_variable_t var)
-{
-  return (var->value);
-}
-
-void lmm_variable_concurrency_share_set(lmm_variable_t var, short int concurrency_share)
-{
-  var->concurrency_share=concurrency_share;
-}
-
-double lmm_variable_getbound(lmm_variable_t var)
-{
-  return (var->bound);
-}
-
 void s_lmm_system_t::expand(lmm_constraint_t cnst, lmm_variable_t var, double consumption_weight)
 {
-  modified = 1;
+  modified = true;
 
   //Check if this variable already has an active element in this constraint
   //If it does, substract it from the required slack
@@ -351,7 +311,7 @@ void s_lmm_system_t::expand(lmm_constraint_t cnst, lmm_variable_t var, double co
 
 void s_lmm_system_t::expand_add(lmm_constraint_t cnst, lmm_variable_t var, double value)
 {
-  modified = 1;
+  modified = true;
 
   check_concurrency();
 
@@ -387,27 +347,6 @@ void s_lmm_system_t::expand_add(lmm_constraint_t cnst, lmm_variable_t var, doubl
   check_concurrency();
 }
 
-lmm_constraint_t lmm_get_cnst_from_var(lmm_system_t /*sys*/, lmm_variable_t var, unsigned num)
-{
-  if (num < var->cnsts.size())
-    return (var->cnsts[num].constraint);
-  else
-    return nullptr;
-}
-
-double lmm_get_cnst_weight_from_var(lmm_system_t /*sys*/, lmm_variable_t var, unsigned num)
-{
-  if (num < var->cnsts.size())
-    return (var->cnsts[num].consumption_weight);
-  else
-    return 0.0;
-}
-
-int lmm_get_number_of_cnst_from_var(lmm_system_t /*sys*/, lmm_variable_t var)
-{
-  return (var->cnsts.size());
-}
-
 lmm_variable_t s_lmm_constraint_t::get_variable(lmm_element_t* elem) const
 {
   if (*elem == nullptr) {
@@ -464,30 +403,25 @@ lmm_variable_t s_lmm_constraint_t::get_variable_safe(lmm_element_t* elem, lmm_el
     return nullptr;
 }
 
-void *lmm_variable_id(lmm_variable_t var)
-{
-  return var->id;
-}
-
-static inline void saturated_constraint_set_update(double usage, int cnst_light_num,
-                                                   dyn_light_t& saturated_constraint_set, double* min_usage)
+static inline void saturated_constraints_update(double usage, int cnst_light_num, dyn_light_t& saturated_constraints,
+                                                double* min_usage)
 {
   xbt_assert(usage > 0,"Impossible");
 
   if (*min_usage < 0 || *min_usage > usage) {
     *min_usage = usage;
     XBT_HERE(" min_usage=%f (cnst->remaining / cnst->usage =%f)", *min_usage, usage);
-    saturated_constraint_set.assign(1, cnst_light_num);
+    saturated_constraints.assign(1, cnst_light_num);
   } else if (*min_usage == usage) {
-    saturated_constraint_set.emplace_back(cnst_light_num);
+    saturated_constraints.emplace_back(cnst_light_num);
   }
 }
 
 static inline void saturated_variable_set_update(s_lmm_constraint_light_t* cnst_light_tab,
-                                                 const dyn_light_t& saturated_constraint_set, lmm_system_t sys)
+                                                 const dyn_light_t& saturated_constraints, lmm_system_t sys)
 {
   /* Add active variables (i.e. variables that need to be set) from the set of constraints to saturate (cnst_light_tab)*/
-  for (int const& saturated_cnst : saturated_constraint_set) {
+  for (int const& saturated_cnst : saturated_constraints) {
     lmm_constraint_light_t cnst = &cnst_light_tab[saturated_cnst];
     void* _elem;
     xbt_swag_t elem_list = &(cnst->cnst->active_element_set);
@@ -606,7 +540,7 @@ void s_lmm_system_t::solve()
 
   s_lmm_constraint_light_t* cnst_light_tab = new s_lmm_constraint_light_t[xbt_swag_size(cnst_list)]();
   int cnst_light_num = 0;
-  dyn_light_t saturated_constraint_set;
+  dyn_light_t saturated_constraints;
 
   xbt_swag_foreach_safe(_cnst, _cnst_next, cnst_list) {
     lmm_constraint_t cnst = (lmm_constraint_t)_cnst;
@@ -640,14 +574,14 @@ void s_lmm_system_t::solve()
       cnst_light_tab[cnst_light_num].cnst = cnst;
       cnst->cnst_light = &(cnst_light_tab[cnst_light_num]);
       cnst_light_tab[cnst_light_num].remaining_over_usage = cnst->remaining / cnst->usage;
-      saturated_constraint_set_update(cnst_light_tab[cnst_light_num].remaining_over_usage,
-        cnst_light_num, saturated_constraint_set, &min_usage);
+      saturated_constraints_update(cnst_light_tab[cnst_light_num].remaining_over_usage, cnst_light_num,
+                                   saturated_constraints, &min_usage);
       xbt_assert(cnst->active_element_set.count>0, "There is no sense adding a constraint that has no active element!");
       cnst_light_num++;
     }
   }
 
-  saturated_variable_set_update(cnst_light_tab, saturated_constraint_set, this);
+  saturated_variable_set_update(cnst_light_tab, saturated_constraints, this);
 
   /* Saturated variables update */
   do {
@@ -754,7 +688,7 @@ void s_lmm_system_t::solve()
     /* Find out which variables reach the maximum */
     min_usage = -1;
     min_bound = -1;
-    saturated_constraint_set.clear();
+    saturated_constraints.clear();
     int pos;
     for(pos=0; pos<cnst_light_num; pos++){
       xbt_assert(cnst_light_tab[pos].cnst->active_element_set.count>0, "Cannot saturate more a constraint that has"
@@ -762,15 +696,14 @@ void s_lmm_system_t::solve()
                  " 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);
-      saturated_constraint_set_update(cnst_light_tab[pos].remaining_over_usage, pos, saturated_constraint_set,
-                                      &min_usage);
+      saturated_constraints_update(cnst_light_tab[pos].remaining_over_usage, pos, saturated_constraints, &min_usage);
     }
 
-    saturated_variable_set_update(cnst_light_tab, saturated_constraint_set, this);
+    saturated_variable_set_update(cnst_light_tab, saturated_constraints, this);
 
   } while (cnst_light_num > 0);
 
-  modified = 0;
+  modified = false;
   if (selective_update_active)
     remove_all_modified_set();
 
@@ -800,18 +733,41 @@ void lmm_solve(lmm_system_t sys)
  */
 void s_lmm_system_t::update_variable_bound(lmm_variable_t var, double bound)
 {
-  modified   = 1;
+  modified   = true;
   var->bound = bound;
 
   if (not var->cnsts.empty())
     update_modified_set(var->cnsts[0].constraint);
 }
 
-/** \brief Measure the minimum concurrency slack across all constraints where the given var is involved */
-int lmm_cnstrs_min_concurrency_slack(lmm_variable_t var)
+void s_lmm_variable_t::initialize(simgrid::surf::Action* id_value, double sharing_weight_value, double bound_value,
+                                  int number_of_constraints, unsigned visited_value)
+{
+  id     = id_value;
+  id_int = s_lmm_variable_t::Global_debug_id++;
+  cnsts.reserve(number_of_constraints);
+  sharing_weight    = sharing_weight_value;
+  staged_weight     = 0.0;
+  bound             = bound_value;
+  concurrency_share = 1;
+  value             = 0.0;
+  visited           = visited_value;
+  mu                = 0.0;
+  new_mu            = 0.0;
+  func_f            = func_f_def;
+  func_fp           = func_fp_def;
+  func_fpi          = func_fpi_def;
+
+  variable_set_hookup.next           = nullptr;
+  variable_set_hookup.prev           = nullptr;
+  saturated_variable_set_hookup.next = nullptr;
+  saturated_variable_set_hookup.prev = nullptr;
+}
+
+int s_lmm_variable_t::get_min_concurrency_slack() const
 {
   int minslack = std::numeric_limits<int>::max();
-  for (s_lmm_element_t const& elem : var->cnsts) {
+  for (s_lmm_element_t const& elem : cnsts) {
     int slack = elem.constraint->get_concurrency_slack();
     if (slack < minslack) {
       // This is only an optimization, to avoid looking at more constraints when slack is already zero
@@ -823,21 +779,13 @@ int lmm_cnstrs_min_concurrency_slack(lmm_variable_t var)
   return minslack;
 }
 
-/* /Check if a variable can be enabled
- *
- * Make sure to set staged_weight before, if your intent is only to check concurrency
- */
-int lmm_can_enable_var(lmm_variable_t var){
-  return var->staged_weight>0 && lmm_cnstrs_min_concurrency_slack(var)>=var->concurrency_share;
-}
-
 //Small remark: In this implementation of lmm_enable_var and lmm_disable_var, we will meet multiple times with var when
 // running sys->update_modified_set.
 // A priori not a big performance issue, but we might do better by calling sys->update_modified_set within the for loops
 // (after doing the first for enabling==1, and before doing the last for disabling==1)
 void s_lmm_system_t::enable_var(lmm_variable_t var)
 {
-  xbt_assert(not XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug) || lmm_can_enable_var(var));
+  xbt_assert(not XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug) || var->can_enable());
 
   var->sharing_weight = var->staged_weight;
   var->staged_weight = 0;
@@ -907,7 +855,7 @@ void s_lmm_system_t::on_disabled_var(lmm_constraint_t cnstr)
 
     lmm_element_t nextelem = (lmm_element_t)xbt_swag_getNext(elem, cnstr->disabled_element_set.offset);
 
-    if (elem->variable->staged_weight > 0 && lmm_can_enable_var(elem->variable)) {
+    if (elem->variable->staged_weight > 0 && elem->variable->can_enable()) {
       //Found a staged variable
       //TODOLATER: Add random timing function to model reservation protocol fuzziness? Then how to make sure that
       //staged variables will eventually be called?
@@ -941,12 +889,12 @@ void s_lmm_system_t::update_variable_weight(lmm_variable_t var, double weight)
 
   XBT_IN("(sys=%p, var=%p, weight=%f)", this, var, weight);
 
-  modified = 1;
+  modified = true;
 
   //Are we enabling this variable?
   if (enabling_var){
     var->staged_weight = weight;
-    int minslack       = lmm_cnstrs_min_concurrency_slack(var);
+    int minslack       = var->get_min_concurrency_slack();
     if (minslack < var->concurrency_share) {
       XBT_DEBUG("Staging var (instead of enabling) because min concurrency slack %i, with weight %f and concurrency"
                 " share %i", minslack, weight, var->concurrency_share);
@@ -966,14 +914,9 @@ void s_lmm_system_t::update_variable_weight(lmm_variable_t var, double weight)
   XBT_OUT();
 }
 
-double lmm_get_variable_weight(lmm_variable_t var)
-{
-  return var->sharing_weight;
-}
-
 void s_lmm_system_t::update_constraint_bound(lmm_constraint_t cnst, double bound)
 {
-  modified = 1;
+  modified = true;
   update_modified_set(cnst);
   cnst->bound = bound;
 }
@@ -1044,7 +987,7 @@ void s_lmm_system_t::remove_all_modified_set()
  */
 double s_lmm_constraint_t::get_usage() const
 {
-  double usage         = 0.0;
+  double result              = 0.0;
   const_xbt_swag_t elem_list = &enabled_element_set;
   void* _elem;
 
@@ -1053,24 +996,24 @@ double s_lmm_constraint_t::get_usage() const
     lmm_element_t elem = (lmm_element_t)_elem;
     if (elem->consumption_weight > 0) {
       if (sharing_policy)
-        usage += elem->consumption_weight * elem->variable->value;
-      else if (usage < elem->consumption_weight * elem->variable->value)
-        usage = std::max(usage, elem->consumption_weight * elem->variable->value);
+        result += elem->consumption_weight * elem->variable->value;
+      else if (result < elem->consumption_weight * elem->variable->value)
+        result = std::max(result, elem->consumption_weight * elem->variable->value);
     }
   }
-  return usage;
+  return result;
 }
 
 int s_lmm_constraint_t::get_variable_amount() const
 {
-  int usage = 0;
+  int result                 = 0;
   const_xbt_swag_t elem_list = &enabled_element_set;
   void *_elem;
 
   xbt_swag_foreach(_elem, elem_list) {
     lmm_element_t elem = (lmm_element_t)_elem;
     if (elem->consumption_weight > 0)
-      usage++;
+      result++;
   }
return usage;
 return result;
 }