Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef HCCLV2_TOPO_MATCH_BASE
12 : #define HCCLV2_TOPO_MATCH_BASE
13 :
14 : #include <unordered_set>
15 : #include <algorithm>
16 :
17 : #include "virtual_topo.h"
18 : #include "rank_gph.h"
19 : #include "log.h"
20 : #include "dev_type.h"
21 : #include "const_val.h"
22 :
23 : namespace Hccl {
24 : constexpr int RANK_SIZE_TWO = 2;
25 : constexpr int RANK_SIZE_THREE = 3;
26 : constexpr int RANK_SIZE_FOUR = 4;
27 : constexpr int RANK_SIZE_EIGHT = 8;
28 :
29 : constexpr int COMM_LEVEL_SIZE_0 = 0;
30 : constexpr int COMM_LEVEL_SIZE_1 = 1;
31 : constexpr int COMM_LEVEL_SIZE_2 = 2;
32 : constexpr int COMM_LEVEL_SIZE_3 = 3;
33 :
34 : const std::vector<std::vector<u32>> SERVER_910A_4_RING_SEQUENCE
35 : = {{0, 1, 2, 6, 5, 4, 7, 3}, {0, 3, 7, 4, 5, 6, 2, 1}, {0, 2, 3, 1, 5, 7, 6, 4}, {0, 4, 6, 7, 5, 1, 3, 2}};
36 :
37 : struct Hccl910AServerValid4PRanksVectorHashFuc {
38 336 : std::size_t operator()(const std::vector<s32> key) const
39 : {
40 336 : size_t ret = 0;
41 1680 : for (auto it : key) {
42 1344 : ret ^= static_cast<u32>(it);
43 : }
44 336 : return ret;
45 : }
46 : };
47 :
48 : const std::unordered_set<std::vector<s32>, Hccl910AServerValid4PRanksVectorHashFuc> SERVER_910A_VALID_4P_RANKS
49 : = {{0, 1, 4, 5}, {0, 2, 4, 6}, {0, 3, 4, 7}, {1, 2, 5, 6}, {1, 3, 5, 7}, {2, 3, 6, 7}, {0, 1, 2, 3}, {4, 5, 6, 7}};
50 :
51 : const std::vector<u32> SERVER_910A_4P_SEQUENCE = {0, 1, 3, 2};
52 :
53 : class TopoMatchBase {
54 : public:
55 : explicit TopoMatchBase(const RankId vRank, const u32 rankSize, const RankGraph* rankGraph, const DevType devType);
56 : virtual ~TopoMatchBase();
57 :
58 : virtual std::string Describe() const = 0;
59 :
60 : virtual HcclResult MatchTopo(
61 : std::vector<std::vector<RankId>>& vTopo, std::vector<RankId>& virtRanks, std::map<RankId, u32>& virtRankMap);
62 :
63 : virtual HcclResult MatchTopo(
64 : std::vector<std::vector<std::vector<RankId>>>& vTopo, std::vector<std::vector<RankId>>& virtRanks,
65 : std::vector<std::map<RankId, u32>>& virtRankMap);
66 :
67 : virtual HcclResult SetTargetRanks(std::set<u32>& targetRanks);
68 :
69 : std::set<u32> batchSendRecvtargetRanks_; // for batchsendrecv create links
70 :
71 : protected:
72 : bool IsAllRanksFullMeshConnected(std::set<RankId> rankSet) const;
73 : u32 GetPathNum(RankId srcRankId, RankId dstRankId) const;
74 : HcclResult GenVirtRankMapping(std::vector<RankId>& virtRanks, std::map<RankId, u32>& virtRankMap) const;
75 : HcclResult GenVirtRankMappingMultiLevel(
76 : std::vector<std::vector<RankId>>& virtRanks, std::vector<std::map<RankId, u32>>& virtRankMap) const;
77 :
78 : HcclResult CalcRankOnSamePlaneOfR0(
79 : std::vector<std::vector<RankId>>& rankOnSameBoardVector, std::vector<std::vector<RankId>>& rankOnSameSlotVector,
80 : std::vector<u32>& numRanksPerBoard) const;
81 :
82 : u32 GcdTwo(u32 a, u32 b) const;
83 : u32 GcdMultiple(const std::vector<u32>& numbers) const;
84 :
85 : HcclResult GenerateLevel1(
86 : const std::set<RankId>& rankSetLevel1, u32 gcdInstSize, RankId rankId,
87 : std::vector<std::vector<std::vector<RankId>>>& vTopo, std::vector<std::vector<RankId>>& virtRanks) const;
88 :
89 : template <typename T>
90 : using Matrix = std::vector<std::vector<T>>;
91 : template <typename T>
92 : using Tensor = std::vector<std::vector<std::vector<T>>>;
93 :
94 : template <typename T>
95 0 : std::string PrintSet(const std::set<T>& values) const
96 : {
97 0 : std::ostringstream oss;
98 0 : for (const auto& value : values) {
99 0 : oss << value << " ";
100 : }
101 0 : return oss.str();
102 0 : }
103 :
104 : template <typename T>
105 0 : std::string PrintVector(const std::vector<T>& values) const
106 : {
107 0 : std::ostringstream oss;
108 0 : for (const auto& value : values) {
109 0 : oss << value << " ";
110 : }
111 0 : return oss.str();
112 0 : }
113 :
114 : template <typename T>
115 0 : std::string PrintMatrix(const Matrix<T>& matrix) const
116 : {
117 0 : std::ostringstream oss;
118 0 : for (const auto& row : matrix) {
119 0 : oss << "{ ";
120 0 : for (const auto& val : row) {
121 0 : oss << val << " ";
122 : }
123 0 : oss << "}";
124 : }
125 0 : return oss.str();
126 0 : }
127 :
128 : template <typename T>
129 0 : std::string PrintTensor(const Tensor<T>& tensor) const
130 : {
131 0 : std::ostringstream oss;
132 0 : for (const auto& matrix : tensor) {
133 0 : oss << "[ " << PrintMatrix(matrix) << " ]";
134 : }
135 0 : return oss.str();
136 0 : }
137 :
138 : RankId myRank_ = INVALID_RANKID;
139 : u32 rankSize_ = 0;
140 : const RankGraph* rankGraph_ = nullptr;
141 : DevType devType_ = DevType::DEV_TYPE_NOSOC;
142 : };
143 :
144 : } // namespace Hccl
145 :
146 : #endif // !HCCLV2_TOPO_MATCH_BASE
|