Logo AND Algorithmique Numérique Distribuée

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