Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
forbid an incomplete and unused constructor
[simgrid.git] / src / kernel / lmm / maxmin.hpp
index a691217..e5efd9d 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2004-2017. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2004-2018. 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. */
@@ -12,7 +12,8 @@
 #include "xbt/asserts.h"
 #include "xbt/mallocator.h"
 #include "xbt/misc.h"
-#include "xbt/swag.h"
+#include "xbt/utility.hpp"
+#include <boost/intrusive/list.hpp>
 #include <cmath>
 #include <limits>
 #include <vector>
@@ -84,7 +85,7 @@ namespace lmm {
  *
  *     max( var1.weight * var1.value * elem5.value  ,  var3.weight * var3.value * elem6.value ) <= cons3.bound
  *
- * This is usefull for the sharing of resources for various models.
+ * This is useful for the sharing of resources for various models.
  * For instance, for the network model, each link is associated to a constraint and each communication to a variable.
  *
  * Implementation details
@@ -144,21 +145,21 @@ XBT_PUBLIC(void) bottleneck_solve(lmm_system_t sys);
 /** Default functions associated to the chosen protocol. When using the lagrangian approach. */
 
 XBT_PUBLIC(void)
-lmm_set_default_protocol_function(double (*func_f)(lmm_variable_t var, double x),
-                                  double (*func_fp)(lmm_variable_t var, double x),
-                                  double (*func_fpi)(lmm_variable_t var, double x));
+set_default_protocol_function(double (*func_f)(const Variable& var, double x),
+                              double (*func_fp)(const Variable& var, double x),
+                              double (*func_fpi)(const Variable& var, double x));
 
-XBT_PUBLIC(double) func_reno_f(lmm_variable_t var, double x);
-XBT_PUBLIC(double) func_reno_fp(lmm_variable_t var, double x);
-XBT_PUBLIC(double) func_reno_fpi(lmm_variable_t var, double x);
+XBT_PUBLIC(double) func_reno_f(const Variable& var, double x);
+XBT_PUBLIC(double) func_reno_fp(const Variable& var, double x);
+XBT_PUBLIC(double) func_reno_fpi(const Variable& var, double x);
 
-XBT_PUBLIC(double) func_reno2_f(lmm_variable_t var, double x);
-XBT_PUBLIC(double) func_reno2_fp(lmm_variable_t var, double x);
-XBT_PUBLIC(double) func_reno2_fpi(lmm_variable_t var, double x);
+XBT_PUBLIC(double) func_reno2_f(const Variable& var, double x);
+XBT_PUBLIC(double) func_reno2_fp(const Variable& var, double x);
+XBT_PUBLIC(double) func_reno2_fpi(const Variable& var, double x);
 
-XBT_PUBLIC(double) func_vegas_f(lmm_variable_t var, double x);
-XBT_PUBLIC(double) func_vegas_fp(lmm_variable_t var, double x);
-XBT_PUBLIC(double) func_vegas_fpi(lmm_variable_t var, double x);
+XBT_PUBLIC(double) func_vegas_f(const Variable& var, double x);
+XBT_PUBLIC(double) func_vegas_fp(const Variable& var, double x);
+XBT_PUBLIC(double) func_vegas_fpi(const Variable& var, double x);
 
 /**
  * @brief LMM element
@@ -167,7 +168,7 @@ XBT_PUBLIC(double) func_vegas_fpi(lmm_variable_t var, double x);
  * Then, it is used to list all variables involved in constraint through constraint's xxx_element_set lists, or
  * vice-versa list all constraints for a given variable.
  */
-XBT_PUBLIC_CLASS s_lmm_element_t
+XBT_PUBLIC_CLASS Element
 {
 public:
   int get_concurrency() const;
@@ -178,9 +179,9 @@ public:
   void make_inactive();
 
   /* hookup to constraint */
-  s_xbt_swag_hookup_t enabled_element_set_hookup;
-  s_xbt_swag_hookup_t disabled_element_set_hookup;
-  s_xbt_swag_hookup_t active_element_set_hookup;
+  boost::intrusive::list_member_hook<> enabled_element_set_hook;
+  boost::intrusive::list_member_hook<> disabled_element_set_hook;
+  boost::intrusive::list_member_hook<> active_element_set_hook;
 
   lmm_constraint_t constraint;
   lmm_variable_t variable;
@@ -191,7 +192,7 @@ public:
   double consumption_weight;
 };
 
-struct s_lmm_constraint_light_t {
+struct ConstraintLight {
   double remaining_over_usage;
   lmm_constraint_t cnst;
 };
@@ -205,11 +206,11 @@ struct s_lmm_constraint_light_t {
  * \li Active elements which variable's weight is non-zero (i.e. it is enabled) AND its element value is non-zero.
  *     LMM_solve iterates over active elements during resolution, dynamically making them active or unactive.
  */
-XBT_PUBLIC_CLASS s_lmm_constraint_t
+XBT_PUBLIC_CLASS Constraint
 {
 public:
-  s_lmm_constraint_t() = default;
-  s_lmm_constraint_t(void* id_value, double bound_value);
+  Constraint() = delete;
+  Constraint(void* id_value, double bound_value);
 
   /** @brief Unshare a constraint. */
   void unshare() { sharing_policy = 0; }
@@ -229,7 +230,7 @@ public:
 
   /**
    * @brief Sets the concurrency limit for this constraint
-   * @param concurrency_limit The concurrency limit to use for this constraint
+   * @param limit The concurrency limit to use for this constraint
    */
   void set_concurrency_limit(int limit)
   {
@@ -292,13 +293,19 @@ public:
   void* get_id() const { return id; }
 
   /* hookup to system */
-  s_xbt_swag_hookup_t constraint_set_hookup           = {nullptr, nullptr};
-  s_xbt_swag_hookup_t active_constraint_set_hookup    = {nullptr, nullptr};
-  s_xbt_swag_hookup_t modified_constraint_set_hookup  = {nullptr, nullptr};
-  s_xbt_swag_hookup_t saturated_constraint_set_hookup = {nullptr, nullptr};
-  s_xbt_swag_t enabled_element_set;  /* a list of lmm_element_t */
-  s_xbt_swag_t disabled_element_set; /* a list of lmm_element_t */
-  s_xbt_swag_t active_element_set;   /* a list of lmm_element_t */
+  boost::intrusive::list_member_hook<> constraint_set_hook;
+  boost::intrusive::list_member_hook<> active_constraint_set_hook;
+  boost::intrusive::list_member_hook<> modified_constraint_set_hook;
+  boost::intrusive::list_member_hook<> saturated_constraint_set_hook;
+  boost::intrusive::list<Element, boost::intrusive::member_hook<Element, boost::intrusive::list_member_hook<>,
+                                                                &Element::enabled_element_set_hook>>
+      enabled_element_set;
+  boost::intrusive::list<Element, boost::intrusive::member_hook<Element, boost::intrusive::list_member_hook<>,
+                                                                &Element::disabled_element_set_hook>>
+      disabled_element_set;
+  boost::intrusive::list<Element, boost::intrusive::member_hook<Element, boost::intrusive::list_member_hook<>,
+                                                                &Element::active_element_set_hook>>
+      active_element_set;
   double remaining;
   double usage;
   double bound;
@@ -307,7 +314,7 @@ public:
   int concurrency_maximum; /* The maximum number of (enabled and disabled) variables associated to the constraint at any
                             * given time (essentially for tracing)*/
 
-  int sharing_policy; /* see @e_surf_link_sharing_policy_t (0: FATPIPE, 1: SHARED, 2: FULLDUPLEX) */
+  int sharing_policy; /* see @e_surf_link_sharing_policy_t (0: FATPIPE, 1: SHARED, 2: SPLITDUPLEX) */
   int id_int;
   double lambda;
   double new_lambda;
@@ -326,10 +333,10 @@ private:
  * When something prevents us from enabling a variable, we "stage" the weight that we would have like to set, so that as
  * soon as possible we enable the variable with desired weight
  */
-XBT_PUBLIC_CLASS s_lmm_variable_t
+XBT_PUBLIC_CLASS Variable
 {
 public:
-  void initialize(simgrid::surf::Action * id_value, double sharing_weight_value, double bound_value,
+  void initialize(simgrid::surf::Action* id_value, double sharing_weight_value, double bound_value,
                   int number_of_constraints, unsigned visited_value);
 
   /**
@@ -346,7 +353,7 @@ public:
 
   /**
    * @brief Set the concurrent share of the variable
-   * @param concurrency_share The new concurrency share
+   * @param value The new concurrency share
    */
   void set_concurrency_share(short int value) { concurrency_share = value; }
 
@@ -391,10 +398,10 @@ public:
   int can_enable() const { return staged_weight > 0 && get_min_concurrency_slack() >= concurrency_share; }
 
   /* hookup to system */
-  s_xbt_swag_hookup_t variable_set_hookup           = {nullptr, nullptr};
-  s_xbt_swag_hookup_t saturated_variable_set_hookup = {nullptr, nullptr};
+  boost::intrusive::list_member_hook<> variable_set_hook;
+  boost::intrusive::list_member_hook<> saturated_variable_set_hook;
 
-  std::vector<s_lmm_element_t> cnsts;
+  std::vector<Element> cnsts;
 
   // sharing_weight: variable's impact on the resource during the sharing
   //   if == 0, the variable is not considered by LMM
@@ -409,41 +416,42 @@ public:
   short int concurrency_share; /* The maximum number of elements that variable will add to a constraint */
   simgrid::surf::Action* id;
   int id_int;
-  unsigned visited; /* used by lmm_update_modified_set */
+  unsigned visited; /* used by System::update_modified_set() */
   /* \begin{For Lagrange only} */
   double mu;
   double new_mu;
-  double (*func_f)(s_lmm_variable_t * var, double x);   /* (f)    */
-  double (*func_fp)(s_lmm_variable_t * var, double x);  /* (f')    */
-  double (*func_fpi)(s_lmm_variable_t * var, double x); /* (f')^{-1}    */
+  double (*func_f)(const Variable& var, double x);   /* (f)    */
+  double (*func_fp)(const Variable& var, double x);  /* (f')    */
+  double (*func_fpi)(const Variable& var, double x); /* (f')^{-1}    */
   /* \end{For Lagrange only} */
 
 private:
   static int Global_debug_id;
 };
 
-inline void s_lmm_element_t::make_active()
+inline void Element::make_active()
 {
-  xbt_swag_insert_at_head(this, &constraint->active_element_set);
+  constraint->active_element_set.push_front(*this);
 }
-inline void s_lmm_element_t::make_inactive()
+inline void Element::make_inactive()
 {
-  xbt_swag_remove(this, &constraint->active_element_set);
+  if (active_element_set_hook.is_linked())
+    simgrid::xbt::intrusive_erase(constraint->active_element_set, *this);
 }
 
 /**
  * @brief LMM system
  */
-XBT_PUBLIC_CLASS s_lmm_system_t
+XBT_PUBLIC_CLASS System
 {
 public:
   /**
    * @brief Create a new Linear MaxMim system
    * @param selective_update whether we should do lazy updates
    */
-  explicit s_lmm_system_t(bool selective_update);
+  explicit System(bool selective_update);
   /** @brief Free an existing Linear MaxMin system */
-  ~s_lmm_system_t();
+  ~System();
 
   /**
    * @brief Create a new Linear MaxMin constraint
@@ -459,7 +467,7 @@ public:
    * @param bound The maximum value of the variable (-1.0 if no maximum value)
    * @param number_of_constraints The maximum number of constraint to associate to the variable
    */
-  lmm_variable_t variable_new(simgrid::surf::Action * id, double weight_value, double bound, int number_of_constraints);
+  lmm_variable_t variable_new(simgrid::surf::Action* id, double weight_value, double bound, int number_of_constraints);
 
   /**
    * @brief Free a variable
@@ -509,7 +517,7 @@ public:
    * @param cnst A constraint
    * @return [description]
    */
-  int constraint_used(lmm_constraint_t cnst) { return xbt_swag_belongs(cnst, &active_constraint_set); }
+  int constraint_used(lmm_constraint_t cnst) { return cnst->active_constraint_set_hook.is_linked(); }
 
   /** @brief Print the lmm system */
   void print() const;
@@ -523,19 +531,41 @@ private:
 
   void var_free(lmm_variable_t var);
   void cnst_free(lmm_constraint_t cnst);
-  lmm_variable_t extract_variable() { return static_cast<lmm_variable_t>(xbt_swag_extract(&variable_set)); }
-  lmm_constraint_t extract_constraint() { return static_cast<lmm_constraint_t>(xbt_swag_extract(&constraint_set)); }
-  void insert_constraint(lmm_constraint_t cnst) { xbt_swag_insert(cnst, &constraint_set); }
+  lmm_variable_t extract_variable()
+  {
+    if (variable_set.empty())
+      return nullptr;
+    lmm_variable_t res = &variable_set.front();
+    variable_set.pop_front();
+    return res;
+  }
+  lmm_constraint_t extract_constraint()
+  {
+    if (constraint_set.empty())
+      return nullptr;
+    lmm_constraint_t res = &constraint_set.front();
+    constraint_set.pop_front();
+    return res;
+  }
+  void insert_constraint(lmm_constraint_t cnst) { constraint_set.push_back(*cnst); }
   void remove_variable(lmm_variable_t var)
   {
-    xbt_swag_remove(var, &variable_set);
-    xbt_swag_remove(var, &saturated_variable_set);
+    if (var->variable_set_hook.is_linked())
+      simgrid::xbt::intrusive_erase(variable_set, *var);
+    if (var->saturated_variable_set_hook.is_linked())
+      simgrid::xbt::intrusive_erase(saturated_variable_set, *var);
+  }
+  void make_constraint_active(lmm_constraint_t cnst)
+  {
+    if (not cnst->active_constraint_set_hook.is_linked())
+      active_constraint_set.push_back(*cnst);
   }
-  void make_constraint_active(lmm_constraint_t cnst) { xbt_swag_insert(cnst, &active_constraint_set); }
   void make_constraint_inactive(lmm_constraint_t cnst)
   {
-    xbt_swag_remove(cnst, &active_constraint_set);
-    xbt_swag_remove(cnst, &modified_constraint_set);
+    if (cnst->active_constraint_set_hook.is_linked())
+      simgrid::xbt::intrusive_erase(active_constraint_set, *cnst);
+    if (cnst->modified_constraint_set_hook.is_linked())
+      simgrid::xbt::intrusive_erase(modified_constraint_set, *cnst);
   }
 
   void enable_var(lmm_variable_t var);
@@ -557,12 +587,21 @@ private:
   void remove_all_modified_set();
   void check_concurrency() const;
 
+  template <class CnstList> void solve(CnstList& cnst_list);
 public:
   bool modified;
-  s_xbt_swag_t variable_set;             /* a list of lmm_variable_t */
-  s_xbt_swag_t active_constraint_set;    /* a list of lmm_constraint_t */
-  s_xbt_swag_t saturated_variable_set;   /* a list of lmm_variable_t */
-  s_xbt_swag_t saturated_constraint_set; /* a list of lmm_constraint_t */
+  boost::intrusive::list<Variable, boost::intrusive::member_hook<Variable, boost::intrusive::list_member_hook<>,
+                                                                 &Variable::variable_set_hook>>
+      variable_set;
+  boost::intrusive::list<Constraint, boost::intrusive::member_hook<Constraint, boost::intrusive::list_member_hook<>,
+                                                                   &Constraint::active_constraint_set_hook>>
+      active_constraint_set;
+  boost::intrusive::list<Variable, boost::intrusive::member_hook<Variable, boost::intrusive::list_member_hook<>,
+                                                                 &Variable::saturated_variable_set_hook>>
+      saturated_variable_set;
+  boost::intrusive::list<Constraint, boost::intrusive::member_hook<Constraint, boost::intrusive::list_member_hook<>,
+                                                                   &Constraint::saturated_constraint_set_hook>>
+      saturated_constraint_set;
 
   simgrid::surf::ActionLmmListPtr keep_track;
 
@@ -570,16 +609,20 @@ public:
 
 private:
   bool selective_update_active; /* flag to update partially the system only selecting changed portions */
-  unsigned visited_counter;     /* used by lmm_update_modified_set and lmm_remove_modified_set to cleverly (un-)flag the
-                                 * constraints (more details in these functions) */
-  s_xbt_swag_t constraint_set;  /* a list of lmm_constraint_t */
-  s_xbt_swag_t modified_constraint_set; /* a list of modified lmm_constraint_t */
+  unsigned visited_counter;     /* used by System::update_modified_set() and System::remove_all_modified_set() to
+                                 * cleverly (un-)flag the constraints (more details in these functions) */
+  boost::intrusive::list<Constraint, boost::intrusive::member_hook<Constraint, boost::intrusive::list_member_hook<>,
+                                                                   &Constraint::constraint_set_hook>>
+      constraint_set;
+  boost::intrusive::list<Constraint, boost::intrusive::member_hook<Constraint, boost::intrusive::list_member_hook<>,
+                                                                   &Constraint::modified_constraint_set_hook>>
+      modified_constraint_set;
   xbt_mallocator_t variable_mallocator;
 };
 
-extern XBT_PRIVATE double (*func_f_def)(lmm_variable_t, double);
-extern XBT_PRIVATE double (*func_fp_def)(lmm_variable_t, double);
-extern XBT_PRIVATE double (*func_fpi_def)(lmm_variable_t, double);
+extern XBT_PRIVATE double (*func_f_def)(const Variable&, double);
+extern XBT_PRIVATE double (*func_fp_def)(const Variable&, double);
+extern XBT_PRIVATE double (*func_fpi_def)(const Variable&, double);
 
 /** @} */
 }