Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[simgrid.git] / src / kernel / lmm / maxmin.hpp
index e688ead..d7de61e 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2004-2019. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2004-2020. 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. */
@@ -8,6 +8,7 @@
 
 #include "simgrid/kernel/resource/Action.hpp"
 #include "simgrid/s4u/Link.hpp"
+#include "src/surf/surf_interface.hpp"
 #include "xbt/asserts.h"
 #include "xbt/mallocator.h"
 
@@ -174,12 +175,12 @@ public:
  *     the corresponding action will enable it (at least this is the idea).
  * \li Enabled elements which variable's weight is non-zero. They are utilized in some LMM functions.
  * \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.
+ *     LMM_solve iterates over active elements during resolution, dynamically making them active or inactive.
  */
 class XBT_PUBLIC Constraint {
 public:
   Constraint() = delete;
-  Constraint(void* id_value, double bound_value);
+  Constraint(resource::Resource* id_value, double bound_value);
 
   /** @brief Unshare a constraint. */
   void unshare() { sharing_policy_ = s4u::Link::SharingPolicy::FATPIPE; }
@@ -244,7 +245,7 @@ public:
    * @brief Get the data associated to a constraint
    * @return The data associated to the constraint
    */
-  void* get_id() const { return id_; }
+  resource::Resource* get_id() const { return id_; }
 
   /* hookup to system */
   boost::intrusive::list_member_hook<> constraint_set_hook_;
@@ -260,25 +261,25 @@ public:
   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 remaining_ = 0.0;
+  double usage_     = 0.0;
   double bound_;
   // TODO MARTIN Check maximum value across resources at the end of simulation and give a warning is more than e.g. 500
-  int concurrency_current_; /* The current concurrency */
-  int concurrency_maximum_; /* The maximum number of (enabled and disabled) variables associated to the constraint at
-                             * any given time (essentially for tracing)*/
+  int concurrency_current_ = 0; /* The current concurrency */
+  int concurrency_maximum_ = 0; /* The maximum number of (enabled and disabled) variables associated to the constraint
+                                 * at any given time (essentially for tracing)*/
 
-  s4u::Link::SharingPolicy sharing_policy_;
+  s4u::Link::SharingPolicy sharing_policy_ = s4u::Link::SharingPolicy::SHARED;
   int rank_; // Only used in debug messages to identify the constraint
-  double lambda_;
-  double new_lambda_;
-  ConstraintLight* cnst_light_;
+  double lambda_               = 0.0;
+  double new_lambda_           = 0.0;
+  ConstraintLight* cnst_light_ = nullptr;
 
 private:
-  static int next_rank_;  // To give a separate rank_ to each contraint
-  int concurrency_limit_; /* The maximum number of variables that may be enabled at any time (stage variables if
-                           * necessary) */
-  void* id_;
+  static int next_rank_;  // To give a separate rank_ to each constraint
+  int concurrency_limit_ = sg_concurrency_limit; /* The maximum number of variables that may be enabled at any time
+                                                  * (stage variables if necessary) */
+  resource::Resource* id_;
 };
 
 /**
@@ -289,8 +290,8 @@ private:
  */
 class XBT_PUBLIC Variable {
 public:
-  void initialize(resource::Action* id_value, double sharing_weight_value, double bound_value,
-                  int number_of_constraints, unsigned visited_value);
+  void initialize(resource::Action* id_value, double sharing_penalty, double bound_value, int number_of_constraints,
+                  unsigned visited_value);
 
   /** @brief Get the value of the variable after the last lmm solve */
   double get_value() const { return value_; }
@@ -327,16 +328,16 @@ public:
   /** @brief Get the data associated to a variable */
   resource::Action* get_id() const { return id_; }
 
-  /** @brief Get the weight of a variable */
-  double get_weight() const { return sharing_weight_; }
+  /** @brief Get the penalty of a variable */
+  double get_penalty() const { return sharing_penalty_; }
 
   /** @brief Measure the minimum concurrency slack across all constraints where the given var is involved */
   int get_min_concurrency_slack() const;
 
   /** @brief Check if a variable can be enabled
-   * Make sure to set staged_weight before, if your intent is only to check concurrency
+   * Make sure to set staged_penalty before, if your intent is only to check concurrency
    */
-  int can_enable() const { return staged_weight_ > 0 && get_min_concurrency_slack() >= concurrency_share_; }
+  bool can_enable() const { return staged_penalty_ > 0 && get_min_concurrency_slack() >= concurrency_share_; }
 
   /* hookup to system */
   boost::intrusive::list_member_hook<> variable_set_hook_;
@@ -344,13 +345,13 @@ public:
 
   std::vector<Element> cnsts_;
 
-  // sharing_weight: variable's impact on the resource during the sharing
+  // sharing_penalty: variable's impact on the resource during the sharing
   //   if == 0, the variable is not considered by LMM
   //   on CPU, actions with N threads have a sharing of N
-  //   on network, the actions with higher latency have a lesser sharing_weight
-  double sharing_weight_;
+  //   on network, the actions with higher latency have a lesser sharing_penalty
+  double sharing_penalty_;
 
-  double staged_weight_; /* If non-zero, variable is staged for addition as soon as maxconcurrency constraints will be
+  double staged_penalty_; /* If non-zero, variable is staged for addition as soon as maxconcurrency constraints will be
                             met */
   double bound_;
   double value_;
@@ -371,7 +372,7 @@ inline void Element::make_active()
 inline void Element::make_inactive()
 {
   if (active_element_set_hook.is_linked())
-    simgrid::xbt::intrusive_erase(constraint->active_element_set_, *this);
+    xbt::intrusive_erase(constraint->active_element_set_, *this);
 }
 
 /**
@@ -392,16 +393,17 @@ public:
    * @param id Data associated to the constraint (e.g.: a network link)
    * @param bound_value The bound value of the constraint
    */
-  Constraint* constraint_new(void* id, double bound_value);
+  Constraint* constraint_new(resource::Resource* id, double bound_value);
 
   /**
    * @brief Create a new Linear MaxMin variable
    * @param id Data associated to the variable (e.g.: a network communication)
-   * @param weight_value The weight of the variable (0.0 if not used)
+   * @param sharing_penalty The weight of the variable (0.0 if not used)
    * @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
+   * @param number_of_constraints The maximum number of constraints to associate to the variable
    */
-  Variable* variable_new(resource::Action* id, double weight_value, double bound, size_t number_of_constraints);
+  Variable* variable_new(resource::Action* id, double sharing_penalty, double bound = -1.0,
+                         size_t number_of_constraints = 1);
 
   /**
    * @brief Free a variable
@@ -409,6 +411,9 @@ public:
    */
   void variable_free(Variable * var);
 
+  /** @brief Free all variables */
+  void variable_free_all();
+
   /**
    * @brief Associate a variable to a constraint with a coefficient
    * @param cnst A constraint
@@ -428,17 +433,13 @@ public:
   /** @brief Update the bound of a variable */
   void update_variable_bound(Variable * var, double bound);
 
-  /**
-   * @brief Update the weight of a variable
-   * @param var A variable
-   * @param weight The new weight of the variable
-   */
-  void update_variable_weight(Variable * var, double weight);
+  /** @brief Update the sharing penalty of a variable */
+  void update_variable_penalty(Variable* var, double penalty);
 
   /** @brief Update a constraint bound */
   void update_constraint_bound(Constraint * cnst, double bound);
 
-  int constraint_used(Constraint* cnst) { return cnst->active_constraint_set_hook_.is_linked(); }
+  int constraint_used(const Constraint* cnst) const { return cnst->active_constraint_set_hook_.is_linked(); }
 
   /** @brief Print the lmm system */
   void print() const;
@@ -475,9 +476,9 @@ private:
   void remove_variable(Variable * var)
   {
     if (var->variable_set_hook_.is_linked())
-      simgrid::xbt::intrusive_erase(variable_set, *var);
+      xbt::intrusive_erase(variable_set, *var);
     if (var->saturated_variable_set_hook_.is_linked())
-      simgrid::xbt::intrusive_erase(saturated_variable_set, *var);
+      xbt::intrusive_erase(saturated_variable_set, *var);
   }
   void make_constraint_active(Constraint * cnst)
   {
@@ -487,9 +488,9 @@ private:
   void make_constraint_inactive(Constraint * cnst)
   {
     if (cnst->active_constraint_set_hook_.is_linked())
-      simgrid::xbt::intrusive_erase(active_constraint_set, *cnst);
+      xbt::intrusive_erase(active_constraint_set, *cnst);
     if (cnst->modified_constraint_set_hook_.is_linked())
-      simgrid::xbt::intrusive_erase(modified_constraint_set, *cnst);
+      xbt::intrusive_erase(modified_constraint_set, *cnst);
   }
 
   void enable_var(Variable * var);
@@ -504,8 +505,8 @@ private:
    */
   void update(Constraint * cnst, Variable * var, double value);
 
-  void update_modified_set(Constraint * cnst);
-  void update_modified_set_rec(Constraint * cnst);
+  void update_modified_set(Constraint* cnst);
+  void update_modified_set_rec(const Constraint* cnst);
 
   /** @brief Remove all constraints of the modified_constraint_set. */
   void remove_all_modified_set();
@@ -531,6 +532,12 @@ public:
   resource::Action::ModifiedSet* modified_set_ = nullptr;
 
 private:
+  typedef std::vector<int> dyn_light_t;
+  
+  //Data used in lmm::solve
+  std::vector<ConstraintLight> cnst_light_vec;
+  dyn_light_t saturated_constraints;
+
   bool selective_update_active; /* flag to update partially the system only selecting changed portions */
   unsigned visited_counter_ = 1; /* used by System::update_modified_set() and System::remove_all_modified_set() to
                                   * cleverly (un-)flag the constraints (more details in these functions) */
@@ -557,8 +564,8 @@ XBT_PUBLIC System* make_new_maxmin_system(bool selective_update);
 XBT_PUBLIC System* make_new_fair_bottleneck_system(bool selective_update);
 
 /** @} */
-}
-}
-}
+} // namespace lmm
+} // namespace kernel
+} // namespace simgrid
 
 #endif