Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'bmf' into 'master'
[simgrid.git] / src / kernel / lmm / bmf.hpp
1 /* Copyright (c) 2004-2022. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SURF_BMF_HPP
7 #define SURF_BMF_HPP
8
9 #include "src/kernel/lmm/maxmin.hpp"
10 #include <boost/container_hash/hash.hpp>
11 #include <eigen3/Eigen/Dense>
12 #include <unordered_set>
13
14 namespace simgrid {
15 namespace kernel {
16 namespace lmm {
17
18 /** @brief Generate all combinations of valid allocation */
19 class XBT_PUBLIC AllocationGenerator {
20 public:
21   explicit AllocationGenerator(Eigen::MatrixXd A);
22
23   /**
24    * @brief Get next valid allocation
25    *
26    * @param next_alloc Allocation (OUTPUT)
27    * @return true if there's an allocation not tested yet, false otherwise
28    */
29   bool next(std::vector<int>& next_alloc);
30
31 private:
32   Eigen::MatrixXd A_;
33   std::vector<int> alloc_;
34   bool first_ = true;
35 };
36
37 /**
38  * @beginrst
39  *
40  * Despite the simplicity of BMF fairness definition, it's quite hard to
41  * find a BMF allocation in the general case.
42  *
43  * This solver implements one possible algorithm to find a BMF, as proposed
44  * at: https://hal.archives-ouvertes.fr/hal-01552739.
45  *
46  * The idea of this algorithm is that each player/flow "selects" a resource to
47  * saturate. Then, we calculate the rate each flow would have with this allocation.
48  * If the allocation is a valid BMF and no one needs to move, it's over. Otherwise,
49  * each player selects a new resource to saturate based on the minimim rate possible
50  * between all resources.
51  *
52  * The steps:
53  * 1) Given an initial allocation B_i
54  * 2) Build a matrix A'_ji and C'_ji which assures that the player receives the most
55  * share at selected resources
56  * 3) Solve: A'_ji * rho_i = C'_j
57  * 4) Calculate the minimum fair rate for each resource j: f_j. The f_j represents
58  * the maximum each flow can receive at the resource j.
59  * 5) Builds a new vector B'_i = arg min(f_j/A_ji).
60  * 6) Stop if B == B' (nobody needs to move), go to step 2 otherwise
61  *
62  * Despite the overall good performance of this algorithm, which converges in a few
63  * iterations, we don't have any assurance about its convergence. In the worst case,
64  * it may be needed to test all possible combination of allocations (which is exponential).
65  *
66  * @endrst
67  */
68 class XBT_PUBLIC BmfSolver {
69 public:
70   /**
71    * @brief Instantiate the BMF solver
72    *
73    * @param A A_ji: consumption of player i on resource j
74    * @param maxA maxA_ji: consumption of larger player i on resource j
75    * @param C Resource capacity
76    * @param shared Is resource shared between player or each player receives the full capacity (FATPIPE links)
77    * @param phi Bound for each player
78    */
79   BmfSolver(Eigen::MatrixXd A, Eigen::MatrixXd maxA, Eigen::VectorXd C, std::vector<bool> shared, Eigen::VectorXd phi);
80   /** @brief Solve equation system to find a fair-sharing of resources */
81   Eigen::VectorXd solve();
82
83 private:
84   using allocation_map_t = std::unordered_map<int, std::unordered_set<int>>;
85   /**
86    * @brief Get actual resource capacity considering bounded players
87    *
88    * Calculates the resource capacity considering that some players on it may be bounded by user,
89    * i.e. an explicit limit in speed was configured
90    *
91    * @param resource Internal index of resource in C_ vector
92    * @param bounded_players List of players that are externally bounded
93    * @return Actual resource capacity
94    */
95   double get_resource_capacity(int resource, const std::vector<int>& bounded_players) const;
96   /**
97    * @brief Auxiliary method to get list of bounded player from allocation
98    *
99    * @param alloc Current allocation
100    * @return list of bounded players
101    */
102   std::vector<int> get_bounded_players(const allocation_map_t& alloc) const;
103
104   /**
105    * @brief Given an allocation calculates the speed/rho for each player
106    *
107    * Do the magic!!
108    * Builds 2 auxiliares matrices A' and C' and solves the system: rho_i = inv(A'_ji) * C'_j
109    *
110    * All resources in A' and C' are saturated, i.e., sum(A'_j * rho_i) = C'_j.
111    *
112    * The matrix A' is built as follows:
113    * - For each resource j in alloc: copy row A_j to A'
114    * - If 2 players (i, k) share a same resource, assure fairness by adding a row in A' such as:
115    *   -  A_ji*rho_i - Ajk*rho_j = 0
116    *
117    * @param alloc for each resource, players that chose to saturate it
118    * @return Vector rho with "players' speed"
119    */
120   Eigen::VectorXd equilibrium(const allocation_map_t& alloc) const;
121
122   /**
123    * @brief Given a fair_sharing vector, gets the allocation
124    *
125    * The allocation for player i is given by: min(bound, f_j/A_ji).
126    * The minimum between all fair-sharing and the external bound (if any)
127    *
128    * The algorithm dictates a random initial allocation. For simplicity, we opt to use the same
129    * logic with the fair_sharing vector.
130    *
131    * @param fair_sharing Fair sharing vector
132    * @param initial Is this the initial allocation?
133    * @return allocation vector
134    */
135   bool get_alloc(const Eigen::VectorXd& fair_sharing, const allocation_map_t& last_alloc, allocation_map_t& alloc,
136                  bool initial);
137
138   bool disturb_allocation(allocation_map_t& alloc, std::vector<int>& alloc_by_player);
139   /**
140    * @brief Calculates the fair sharing for each resource
141    *
142    * Basically 3 options:
143    * 1) resource in allocation: A_ji*rho_i since all players who selected this resource have the same share
144    * 2) resource not selected by saturated (fully used): divide it by the number of players C_/n_players
145    * 3) resource not selected and not-saturated: no limitation
146    *
147    * @param alloc Allocation map (resource-> players)
148    * @param rho Speed for each player i
149    * @param fair_sharing Output vector, fair sharing for each resource j
150    */
151   void set_fair_sharing(const allocation_map_t& alloc, const Eigen::VectorXd& rho, Eigen::VectorXd& fair_sharing) const;
152
153   /**
154    * @brief Check if allocation is BMF
155    *
156    * To be a bmf allocation it must:
157    * - respect the capacity of all resources
158    * - saturate at least 1 resource
159    * - every player receives maximum share in at least 1 saturated resource
160    * @param rho Allocation
161    * @return true if BMF false otherwise
162    */
163   bool is_bmf(const Eigen::VectorXd& rho) const;
164   std::vector<int> alloc_map_to_vector(const allocation_map_t& alloc) const;
165
166   /**
167    * @brief Set of debug functions to print the different objects
168    */
169   template <typename T> std::string debug_eigen(const T& obj) const;
170   template <typename C> std::string debug_vector(const C& container) const;
171   std::string debug_alloc(const allocation_map_t& alloc) const;
172
173   Eigen::MatrixXd A_;    //!< A_ji: resource usage matrix, each row j represents a resource and col i a flow/player
174   Eigen::MatrixXd maxA_; //!< maxA_ji,  similar as A_, but containing the maximum consumption of player i (if player a
175                          //!< single flow it's equal to A_)
176   Eigen::VectorXd C_;    //!< C_j Capacity of each resource
177   std::vector<bool> C_shared_; //!< shared_j Resource j is shared or not
178   Eigen::VectorXd phi_;        //!< phi_i bound for each player
179
180   std::unordered_set<std::vector<int>, boost::hash<std::vector<int>>> allocations_;
181   AllocationGenerator gen_;
182   std::vector<int> allocations_age_;
183   static constexpr int NO_RESOURCE = -1;                    //!< flag to indicate player has selected no resource
184   int max_iteration_               = sg_bmf_max_iterations; //!< number maximum of iterations of BMF algorithm
185 };
186
187 /**
188  * @beginrst
189  *
190  * A BMF (bottleneck max fairness) solver to resolve inequation systems.
191  *
192  * Usually, SimGrid relies on a *max-min fairness* solver to share the resources.
193  * Max-min is great when sharing homogenous resources, however it cannot be used with heterogeneous resources.
194  *
195  * BMF is a natural alternative to max-min, providing a fair-sharing of heterogeneous resources (CPU, network, disk).
196  * It is specially relevant for the implementation of parallel tasks whose sharing involves different
197  * kinds of resources.
198  *
199  * BMF assures that every flow receives the maximum share possible in at least 1 bottleneck (fully used) resource.
200  *
201  * The BMF is characterized by:
202  * - A_ji: a matrix of requirement for flows/player. For each resource j, and flow i, A_ji represents the utilization
203  * of resource j for 1 unit of the flow i.
204  * - rho_i: the rate allocated for flow i (same among all resources)
205  * - C_j: the capacity of each resource (can be bytes/s, flops/s, etc)
206  *
207  * Therefore, these conditions need to satisfied to an allocation be considered a BMF:
208  * 1) All constraints are respected (flows cannot use more than the resource has available)
209  *   - for all resource j and player i: A_ji * rho_i <= C_j
210  * 2) At least 1 resource is fully used (bottleneck).
211  *   - for some resource j: A_ji * rho_i = C_j
212  * 3) Each flow (player) receives the maximum share in at least 1 bottleneck.
213  *   - for all player i: exist a resource j: A_ji * rho_i >= A_jk * rho_k for all other player k
214  *
215  * Despite the prove of existence of a BMF allocation in the general case, it may not
216  * be unique, which leads to possible different rate for the applications.
217  *
218  * More details about BMF can be found at: https://hal.inria.fr/hal-01243985/document
219  *
220  * @endrst
221  */
222 /**
223  * @brief Bottleneck max-fair system
224  */
225 class XBT_PUBLIC BmfSystem : public System {
226 public:
227   using System::System;
228   /** @brief Implements the solve method to calculate a BMF allocation */
229   void solve() final;
230
231 private:
232   using allocation_map_t = std::unordered_map<int, std::unordered_set<int>>;
233   /**
234    * @brief Solve equation system to find a fair-sharing of resources
235    *
236    * @param cnst_list Constraint list (modified for selective update or active)
237    */
238   template <class CnstList> void bmf_solve(const CnstList& cnst_list);
239   /**
240    * @brief Iterates over system and build the consumption matrix A_ji and maxA_ji
241    *
242    * Each row j represents a resource and each col i a player/flow
243    *
244    * Considers only active variables to build the matrix.
245    *
246    * @param number_cnsts Number of constraints in the system
247    * @param A Consumption matrix (OUTPUT)
248    * @param maxA Max subflow consumption matrix (OUTPUT)
249    * @param phi Bounds for variables
250    */
251   void get_flows_data(int number_cnsts, Eigen::MatrixXd& A, Eigen::MatrixXd& maxA, Eigen::VectorXd& phi);
252   /**
253    * @brief Builds the vector C_ with resource's capacity
254    *
255    * @param cnst_list Constraint list (modified for selective update or active)
256    * @param C Resource capacity vector
257    * @param shared Resource is shared or not (fatpipe links)
258    */
259   template <class CnstList>
260   void get_constraint_data(const CnstList& cnst_list, Eigen::VectorXd& C, std::vector<bool>& shared);
261
262   std::unordered_map<int, Variable*> idx2Var_; //!< Map player index (and position in matrices) to system's variable
263   std::unordered_map<const Constraint*, int> cnst2idx_; //!< Conversely map constraint to index
264   bool warned_nonlinear_ = false;
265 };
266
267 } // namespace lmm
268 } // namespace kernel
269 } // namespace simgrid
270
271 #endif