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