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 HCCL_CCU_RANK_GROUP_H
12 : #define HCCL_CCU_RANK_GROUP_H
13 :
14 : #include <vector>
15 : #include <utility>
16 : #include <functional>
17 : #include "types.h"
18 : #include "virtual_topo.h"
19 :
20 : namespace Hccl {
21 :
22 : class RankGroup {
23 : public:
24 71 : RankGroup() = default;
25 3 : explicit RankGroup(const std::vector<RankId> &ranks) : ranks(ranks)
26 : {
27 3 : }
28 :
29 88 : ~RankGroup() = default;
30 :
31 : // 添加RankId到ranks中
32 10 : void AddRank(RankId rankId)
33 : {
34 10 : ranks.emplace_back(rankId);
35 10 : }
36 :
37 : // 获取ranks
38 0 : std::vector<RankId> GetRanks() const
39 : {
40 0 : return ranks;
41 : }
42 :
43 : private:
44 : std::vector<RankId> ranks;
45 : };
46 :
47 : struct LinkInfo {
48 : RankId rankId;
49 : u32 dieId;
50 : IpAddress localAddr;
51 : IpAddress remoteAddr;
52 :
53 6 : LinkInfo(RankId rId, u32 dId, IpAddress lAddr, IpAddress rAddr)
54 6 : : rankId(rId), dieId(dId), localAddr(lAddr), remoteAddr(rAddr){};
55 :
56 16 : LinkInfo(const LinkData &linkdata)
57 16 : : rankId(linkdata.GetRemoteRankId()), dieId(linkdata.GetLocalDieId()), localAddr(linkdata.GetLocalAddr()),
58 16 : remoteAddr(linkdata.GetRemoteAddr()){};
59 :
60 5 : explicit LinkInfo():rankId(0),dieId(0),localAddr(IpAddress("0.0.0.1")),remoteAddr(IpAddress("0.0.0.1")){};
61 : };
62 :
63 : class LinkGroup {
64 : public:
65 6 : LinkGroup() = default;
66 14 : explicit LinkGroup(const std::vector<LinkInfo> &links) : links(links)
67 : {
68 14 : }
69 :
70 69 : ~LinkGroup() = default;
71 :
72 : // 添加linkData到links中
73 10 : void AddLink(LinkInfo linkInfo)
74 : {
75 10 : links.emplace_back(linkInfo);
76 10 : }
77 :
78 : // 获取links
79 76 : std::vector<LinkInfo> GetLinks() const
80 : {
81 76 : return links;
82 : }
83 :
84 : private:
85 : std::vector<LinkInfo> links;
86 : };
87 :
88 : } // namespace Hccl
89 :
90 : // 在全局作用域定义哈希函数
91 : namespace std {
92 : // 定义一个常量用于哈希计算中的乘法操作
93 : constexpr size_t K_HASH_MULTIPLIER = 31;
94 :
95 : template <> class hash<Hccl::RankGroup> {
96 : public:
97 : size_t operator()(const Hccl::RankGroup &rg) const
98 : {
99 : size_t hashValue = 0;
100 : for (const auto &id : rg.GetRanks()) {
101 : hashValue = hashValue * K_HASH_MULTIPLIER + hash<Hccl::RankId>()(id);
102 : }
103 : return hashValue;
104 : }
105 : };
106 :
107 : template <> class equal_to<Hccl::RankGroup> {
108 : public:
109 : bool operator()(const Hccl::RankGroup &rg1, const Hccl::RankGroup &rg2) const
110 : {
111 : if (rg1.GetRanks().size() != rg2.GetRanks().size()) {
112 : return false;
113 : } else {
114 : for (u32 i = 0; i < rg1.GetRanks().size(); i++) {
115 : if (rg1.GetRanks()[i] != rg2.GetRanks()[i]) {
116 : return false;
117 : }
118 : }
119 : }
120 : return true;
121 : }
122 : };
123 :
124 : template <> class hash<Hccl::LinkInfo> {
125 : public:
126 14 : size_t operator()(const Hccl::LinkInfo &LinkInfo) const
127 : {
128 14 : auto rankIdHash = hash<Hccl::RankId>{}(LinkInfo.rankId);
129 14 : auto dieIdHash = hash<u32>{}(LinkInfo.dieId);
130 14 : auto localEidHash = hash<Hccl::IpAddress>{}(LinkInfo.localAddr);
131 14 : auto remoteEidHash = hash<Hccl::IpAddress>{}(LinkInfo.remoteAddr);
132 :
133 14 : return Hccl::HashCombine({rankIdHash, dieIdHash, localEidHash, remoteEidHash});
134 : }
135 : };
136 :
137 : template <> class hash<Hccl::LinkGroup> {
138 : public:
139 14 : size_t operator()(const Hccl::LinkGroup &rg) const
140 : {
141 14 : size_t hashValue = 0;
142 28 : for (const auto &id : rg.GetLinks()) {
143 14 : hashValue = hashValue * K_HASH_MULTIPLIER + hash<Hccl::LinkInfo>()(id);
144 14 : }
145 14 : return hashValue;
146 : }
147 : };
148 :
149 : template <> class equal_to<Hccl::LinkGroup> {
150 : public:
151 6 : bool operator()(const Hccl::LinkGroup &rg1, const Hccl::LinkGroup &rg2) const
152 : {
153 6 : if (rg1.GetLinks().size() != rg2.GetLinks().size()) {
154 0 : return false;
155 : } else {
156 12 : for (u32 i = 0; i < rg1.GetLinks().size(); i++) {
157 12 : if ((rg1.GetLinks()[i].rankId != rg2.GetLinks()[i].rankId)
158 12 : || rg1.GetLinks()[i].dieId != rg2.GetLinks()[i].dieId) {
159 0 : return false;
160 : }
161 : }
162 : }
163 6 : return true;
164 : }
165 : };
166 : } // namespace std
167 :
168 : #endif // HCCL_CCU_RANK_GROUP_H
|