Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sanitize the OOP of kernel::profile
[simgrid.git] / src / kernel / lmm / fair_bottleneck.cpp
index ed92dfa..4426ce8 100644 (file)
@@ -1,80 +1,52 @@
-/* Copyright (c) 2007-2011, 2013-2017. The SimGrid Team.
- * All rights reserved.                                                     */
+/* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
-#include "surf/maxmin.hpp"
-#include "xbt/log.h"
+#include "src/kernel/lmm/maxmin.hpp"
+#include "src/surf/surf_interface.hpp"
 #include "xbt/sysdep.h"
+
 #include <algorithm>
 #include <cfloat>
 #include <cmath>
 #include <cstdlib>
+#include <xbt/utility.hpp>
 
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_maxmin);
-#define SHOW_EXPR_G(expr) XBT_DEBUG(#expr " = %g", expr);
-#define SHOW_EXPR_D(expr) XBT_DEBUG(#expr " = %d", expr);
-#define SHOW_EXPR_P(expr) XBT_DEBUG(#expr " = %p", expr);
 
-void simgrid::surf::bottleneck_solve(lmm_system_t sys)
+simgrid::kernel::lmm::System* simgrid::kernel::lmm::make_new_fair_bottleneck_system(bool selective_update)
 {
-  void* _var;
-  void* _var_next;
-  void* _cnst;
-  void* _cnst_next;
-  void* _elem;
-  lmm_variable_t var    = nullptr;
-  lmm_constraint_t cnst = nullptr;
-  s_lmm_constraint_t s_cnst;
-  lmm_element_t elem   = nullptr;
-  xbt_swag_t cnst_list = nullptr;
-  xbt_swag_t var_list  = nullptr;
-  xbt_swag_t elem_list = nullptr;
-
-  static s_xbt_swag_t cnst_to_update;
+  return new simgrid::kernel::lmm::FairBottleneck(selective_update);
+}
 
-  if (not sys->modified)
+void simgrid::kernel::lmm::FairBottleneck::bottleneck_solve()
+{
+  if (not modified_)
     return;
 
-  /* Init */
-  xbt_swag_init(&(cnst_to_update), xbt_swag_offset(s_cnst, saturated_constraint_set_hookup));
-
-  var_list = &(sys->variable_set);
-  XBT_DEBUG("Variable set : %d", xbt_swag_size(var_list));
-  xbt_swag_foreach(_var, var_list)
-  {
-    var        = static_cast<lmm_variable_t>(_var);
-    var->value = 0.0;
-    XBT_DEBUG("Handling variable %p", var);
-    xbt_swag_insert(var, &(sys->saturated_variable_set));
-    auto weighted = std::find_if(begin(var->cnsts), end(var->cnsts),
-                                 [](s_lmm_element_t const& x) { return x.consumption_weight != 0.0; });
-    if (weighted == end(var->cnsts) && var->sharing_weight > 0.0) {
-      XBT_DEBUG("Err, finally, there is no need to take care of variable %p", var);
-      xbt_swag_remove(var, &(sys->saturated_variable_set));
-      var->value = 1.0;
-    }
-    if (var->sharing_weight <= 0.0) {
-      XBT_DEBUG("Err, finally, there is no need to take care of variable %p", var);
-      xbt_swag_remove(var, &(sys->saturated_variable_set));
+  XBT_DEBUG("Variable set : %zu", variable_set.size());
+  for (Variable& var : variable_set) {
+    var.value = 0.0;
+    XBT_DEBUG("Handling variable %p", &var);
+    if (var.sharing_weight > 0.0 && std::find_if(begin(var.cnsts), end(var.cnsts), [](Element const& x) {
+                                      return x.consumption_weight != 0.0;
+                                    }) != end(var.cnsts)) {
+      saturated_variable_set.push_back(var);
+    } else {
+      XBT_DEBUG("Err, finally, there is no need to take care of variable %p", &var);
+      if (var.sharing_weight > 0.0)
+        var.value = 1.0;
     }
   }
-  var_list = &(sys->saturated_variable_set);
 
-  cnst_list = &(sys->active_constraint_set);
-  XBT_DEBUG("Active constraints : %d", xbt_swag_size(cnst_list));
-  xbt_swag_foreach(_cnst, cnst_list)
-  {
-    cnst = static_cast<lmm_constraint_t>(_cnst);
-    xbt_swag_insert(cnst, &(sys->saturated_constraint_set));
+  XBT_DEBUG("Active constraints : %zu", active_constraint_set.size());
+  for (Constraint& cnst : active_constraint_set) {
+    saturated_constraint_set.push_back(cnst);
   }
-  cnst_list = &(sys->saturated_constraint_set);
-  xbt_swag_foreach(_cnst, cnst_list)
-  {
-    cnst            = static_cast<lmm_constraint_t>(_cnst);
-    cnst->remaining = cnst->bound;
-    cnst->usage     = 0.0;
+  for (Constraint& cnst : saturated_constraint_set) {
+    cnst.remaining = cnst.bound;
+    cnst.usage     = 0.0;
   }
 
   XBT_DEBUG("Fair bottleneck Initialized");
@@ -82,105 +54,100 @@ void simgrid::surf::bottleneck_solve(lmm_system_t sys)
   /*
    * Compute Usage and store the variables that reach the maximum.
    */
+  auto& var_list  = saturated_variable_set;
+  auto& cnst_list = saturated_constraint_set;
   do {
     if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
       XBT_DEBUG("Fair bottleneck done");
-      sys->print();
+      print();
     }
-    XBT_DEBUG("******* Constraints to process: %d *******", xbt_swag_size(cnst_list));
-    xbt_swag_foreach_safe(_cnst, _cnst_next, cnst_list)
-    {
-      cnst   = static_cast<lmm_constraint_t>(_cnst);
+    XBT_DEBUG("******* Constraints to process: %zu *******", cnst_list.size());
+    for (auto iter = std::begin(cnst_list); iter != std::end(cnst_list);) {
+      Constraint& cnst = *iter;
       int nb = 0;
-      XBT_DEBUG("Processing cnst %p ", cnst);
-      elem_list   = &(cnst->enabled_element_set);
-      cnst->usage = 0.0;
-      xbt_swag_foreach(_elem, elem_list)
-      {
-        elem = static_cast<lmm_element_t>(_elem);
-        xbt_assert(elem->variable->sharing_weight > 0);
-        if ((elem->consumption_weight > 0) && xbt_swag_belongs(elem->variable, var_list))
+      XBT_DEBUG("Processing cnst %p ", &cnst);
+      cnst.usage = 0.0;
+      for (Element& elem : cnst.enabled_element_set) {
+        xbt_assert(elem.variable->sharing_weight > 0);
+        if (elem.consumption_weight > 0 && elem.variable->saturated_variable_set_hook.is_linked())
           nb++;
       }
       XBT_DEBUG("\tThere are %d variables", nb);
-      if (nb > 0 && not cnst->sharing_policy)
+      if (nb > 0 && cnst.sharing_policy == s4u::Link::SharingPolicy::FATPIPE)
         nb = 1;
-      if (not nb) {
-        cnst->remaining = 0.0;
-        cnst->usage     = cnst->remaining;
-        xbt_swag_remove(cnst, cnst_list);
-        continue;
+      if (nb == 0) {
+        cnst.remaining = 0.0;
+        cnst.usage     = 0.0;
+        iter           = cnst_list.erase(iter);
+      } else {
+        cnst.usage = cnst.remaining / nb;
+        XBT_DEBUG("\tConstraint Usage %p : %f with %d variables", &cnst, cnst.usage, nb);
+        iter++;
       }
-      cnst->usage = cnst->remaining / nb;
-      XBT_DEBUG("\tConstraint Usage %p : %f with %d variables", cnst, cnst->usage, nb);
     }
 
-    xbt_swag_foreach_safe(_var, _var_next, var_list)
-    {
-      var            = static_cast<lmm_variable_t>(_var);
+    for (auto iter = std::begin(var_list); iter != std::end(var_list);) {
+      Variable& var  = *iter;
       double min_inc = DBL_MAX;
-      for (s_lmm_element_t const& elm : var->cnsts) {
+      for (Element const& elm : var.cnsts) {
         if (elm.consumption_weight > 0)
           min_inc = std::min(min_inc, elm.constraint->usage / elm.consumption_weight);
       }
-      if (var->bound > 0)
-        min_inc = std::min(min_inc, var->bound - var->value);
-      var->mu   = min_inc;
-      XBT_DEBUG("Updating variable %p maximum increment: %g", var, var->mu);
-      var->value += var->mu;
-      if (var->value == var->bound) {
-        xbt_swag_remove(var, var_list);
-      }
+      if (var.bound > 0)
+        min_inc = std::min(min_inc, var.bound - var.value);
+      var.mu    = min_inc;
+      XBT_DEBUG("Updating variable %p maximum increment: %g", &var, var.mu);
+      var.value += var.mu;
+      if (var.value == var.bound)
+        iter = var_list.erase(iter);
+      else
+        iter++;
     }
 
-    xbt_swag_foreach_safe(_cnst, _cnst_next, cnst_list)
-    {
-      cnst = static_cast<lmm_constraint_t>(_cnst);
-      XBT_DEBUG("Updating cnst %p ", cnst);
-      elem_list = &(cnst->enabled_element_set);
-      xbt_swag_foreach(_elem, elem_list)
-      {
-        elem = static_cast<lmm_element_t>(_elem);
-        xbt_assert(elem->variable->sharing_weight > 0);
-        if (cnst->sharing_policy) {
-          XBT_DEBUG("\tUpdate constraint %p (%g) with variable %p by %g", cnst, cnst->remaining, elem->variable,
-                    elem->variable->mu);
-          double_update(&(cnst->remaining), elem->consumption_weight * elem->variable->mu, sg_maxmin_precision);
-        } else {
-          XBT_DEBUG("\tNon-Shared variable. Update constraint usage of %p (%g) with variable %p by %g", cnst,
-                    cnst->usage, elem->variable, elem->variable->mu);
-          cnst->usage = std::min(cnst->usage, elem->consumption_weight * elem->variable->mu);
+    for (auto iter = std::begin(cnst_list); iter != std::end(cnst_list);) {
+      Constraint& cnst = *iter;
+      XBT_DEBUG("Updating cnst %p ", &cnst);
+      if (cnst.sharing_policy != s4u::Link::SharingPolicy::FATPIPE) {
+        for (Element& elem : cnst.enabled_element_set) {
+          xbt_assert(elem.variable->sharing_weight > 0);
+          XBT_DEBUG("\tUpdate constraint %p (%g) with variable %p by %g", &cnst, cnst.remaining, elem.variable,
+                    elem.variable->mu);
+          double_update(&cnst.remaining, elem.consumption_weight * elem.variable->mu, sg_maxmin_precision);
         }
-      }
-      if (not cnst->sharing_policy) {
-        XBT_DEBUG("\tUpdate constraint %p (%g) by %g", cnst, cnst->remaining, cnst->usage);
-
-        double_update(&(cnst->remaining), cnst->usage, sg_maxmin_precision);
+      } else {
+        for (Element& elem : cnst.enabled_element_set) {
+          xbt_assert(elem.variable->sharing_weight > 0);
+          XBT_DEBUG("\tNon-Shared variable. Update constraint usage of %p (%g) with variable %p by %g", &cnst,
+                    cnst.usage, elem.variable, elem.variable->mu);
+          cnst.usage = std::min(cnst.usage, elem.consumption_weight * elem.variable->mu);
+        }
+        XBT_DEBUG("\tUpdate constraint %p (%g) by %g", &cnst, cnst.remaining, cnst.usage);
+        double_update(&cnst.remaining, cnst.usage, sg_maxmin_precision);
       }
 
-      XBT_DEBUG("\tRemaining for %p : %g", cnst, cnst->remaining);
-      if (cnst->remaining <= 0.0) {
-        XBT_DEBUG("\tGet rid of constraint %p", cnst);
+      XBT_DEBUG("\tRemaining for %p : %g", &cnst, cnst.remaining);
+      if (cnst.remaining <= 0.0) {
+        XBT_DEBUG("\tGet rid of constraint %p", &cnst);
 
-        xbt_swag_remove(cnst, cnst_list);
-        xbt_swag_foreach(_elem, elem_list)
-        {
-          elem = static_cast<lmm_element_t>(_elem);
-          if (elem->variable->sharing_weight <= 0)
+        iter = cnst_list.erase(iter);
+        for (Element& elem : cnst.enabled_element_set) {
+          if (elem.variable->sharing_weight <= 0)
             break;
-          if (elem->consumption_weight > 0) {
-            XBT_DEBUG("\t\tGet rid of variable %p", elem->variable);
-            xbt_swag_remove(elem->variable, var_list);
+          if (elem.consumption_weight > 0 && elem.variable->saturated_variable_set_hook.is_linked()) {
+            XBT_DEBUG("\t\tGet rid of variable %p", elem.variable);
+            simgrid::xbt::intrusive_erase(var_list, *elem.variable);
           }
         }
+      } else {
+        iter++;
       }
     }
-  } while (xbt_swag_size(var_list));
+  } while (not var_list.empty());
 
-  xbt_swag_reset(cnst_list);
-  sys->modified = true;
+  cnst_list.clear();
+  modified_ = true;
   if (XBT_LOG_ISENABLED(surf_maxmin, xbt_log_priority_debug)) {
     XBT_DEBUG("Fair bottleneck done");
-    sys->print();
+    print();
   }
 }