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 : #ifndef HCCLV2_INNER_NET_DEVICE_H
11 : #define HCCLV2_INNER_NET_DEVICE_H
12 :
13 : #include <mutex>
14 : #include <unordered_map>
15 : #include <functional>
16 : #include <tuple>
17 : #include <type_traits>
18 : #include "port.h"
19 : #include "ip_address.h"
20 : #include "orion_adapter_rts.h"
21 : #include "orion_adapter_hccp.h"
22 : #include "buffer_key.h"
23 : #include "tokenInfo_manager.h"
24 :
25 : namespace Hccl {
26 : struct NetDevInfo {
27 12 : bool operator==(const NetDevInfo &rhs) const
28 : {
29 12 : return rankId == rhs.rankId && type == rhs.type && devId == rhs.devId && addr == rhs.addr
30 24 : && protoType == rhs.protoType;
31 : }
32 :
33 : bool operator!=(const NetDevInfo &rhs) const
34 : {
35 : return !(rhs == *this);
36 : }
37 :
38 : bool operator<(const NetDevInfo &rhs) const
39 : {
40 : if (rankId < rhs.rankId) {
41 : return true;
42 : }
43 : if (rhs.rankId < rankId) {
44 : return false;
45 : }
46 : if (type < rhs.type) {
47 : return true;
48 : }
49 : if (rhs.type < type)
50 : return false;
51 : if (addr < rhs.addr) {
52 : return true;
53 : }
54 : if (rhs.addr < addr) {
55 : return false;
56 : }
57 : return devId < rhs.devId;
58 : }
59 :
60 : bool operator>(const NetDevInfo &rhs) const
61 : {
62 : return rhs < *this;
63 : }
64 :
65 : bool operator<=(const NetDevInfo &rhs) const
66 : {
67 : return !(rhs < *this);
68 : }
69 :
70 : bool operator>=(const NetDevInfo &rhs) const
71 : {
72 : return !(*this < rhs);
73 : }
74 :
75 : RankId rankId;
76 : PortDeploymentType type;
77 : LinkProtoType protoType;
78 : u32 devId;
79 : IpAddress addr;
80 : };
81 :
82 : class InnerNetDev {
83 : public:
84 : InnerNetDev(const NetDevInfo &info);
85 : ~InnerNetDev();
86 : // RdmaHandle 读写函数
87 5 : RdmaHandle getRdmaHandle() const
88 : {
89 5 : return rdmaHandle_;
90 : }
91 3 : void setRdmaHandle(const RdmaHandle &handle)
92 : {
93 3 : rdmaHandle_ = handle;
94 3 : }
95 :
96 : // HrtUbJfcMode 读写函数
97 2 : HrtUbJfcMode getUbMode() const
98 : {
99 2 : return ubMode_;
100 : }
101 1 : void setUbMode(HrtUbJfcMode mode)
102 : {
103 1 : ubMode_ = mode;
104 1 : }
105 :
106 : // JfcHandle 读写函数
107 : JfcHandle getUbJfcHandle(HrtUbJfcMode jfcMode);
108 : void setUbJfcHandle(const JfcHandle &handle)
109 : {
110 : ubJfcHandle_ = handle;
111 : }
112 :
113 : // dieId_ 读写函数
114 2 : uint32_t getDieId() const
115 : {
116 2 : return dieId_;
117 : }
118 1 : void setDieId(uint32_t id)
119 : {
120 1 : dieId_ = id;
121 1 : }
122 :
123 : // funcId_ 读写函数
124 2 : uint32_t getFuncId() const
125 : {
126 2 : return funcId_;
127 : }
128 1 : void setFuncId(uint32_t id)
129 : {
130 1 : funcId_ = id;
131 1 : }
132 :
133 : // TokenIdHandle 读写函数
134 2 : TokenIdHandle getTokenHandle() const
135 : {
136 2 : return tokenHandle_;
137 : }
138 1 : void setTokenHandle(const TokenIdHandle &handle)
139 : {
140 1 : tokenHandle_ = handle;
141 1 : }
142 :
143 : // tokenId_ 读写函数
144 2 : uint32_t getTokenId() const
145 : {
146 2 : return tokenId_;
147 : }
148 1 : void setTokenId(uint32_t id)
149 : {
150 1 : tokenId_ = id;
151 1 : }
152 :
153 7 : bool GetIsValid() const { return isValid_; }
154 :
155 : std::pair<TokenIdHandle, uint32_t> getTokenIdInfo(const BufferKey<uintptr_t, u64> &bufKey);
156 :
157 : void putTokenIdInfo(const BufferKey<uintptr_t, u64> &bufKey, TokenIdHandle tokenIdHandle);
158 :
159 : private:
160 : RdmaHandle rdmaHandle_{nullptr};
161 : HrtUbJfcMode ubMode_;
162 : JfcHandle ubJfcHandle_{0};
163 : uint32_t dieId_{0};
164 : uint32_t funcId_{0};
165 : TokenIdHandle tokenHandle_{0};
166 : uint32_t tokenId_{0};
167 : LinkProtoType localProto_;
168 : HrtNetworkMode netMode_{HrtNetworkMode::HDC};
169 :
170 : std::unique_ptr<TokenInfoManager> tokenInfoManager_{nullptr};
171 : bool isValid_ {true};
172 : };
173 :
174 : } // namespace Hccl
175 :
176 : namespace std {
177 : template<> struct hash<Hccl::PortDeploymentType> {
178 27 : size_t operator()(const Hccl::PortDeploymentType& val) const {
179 : // 直接指定底层类型为 int(如果确认枚举底层是 int)
180 27 : return hash<int>()(static_cast<int>(val));
181 : }
182 : };
183 :
184 : template<> struct hash<Hccl::LinkProtoType> {
185 27 : size_t operator()(const Hccl::LinkProtoType& val) const {
186 27 : return hash<int>()(static_cast<int>(val));
187 : }
188 : };
189 :
190 : const u64 HASH_NET_DEV = 0x9e3779b9;
191 : const u32 HASH_SEED_LEFT_BIT = 6;
192 : const u32 HASH_SEED_RIGHT_BIT = 2;
193 :
194 : template<> struct hash<Hccl::NetDevInfo> {
195 27 : size_t operator()(const Hccl::NetDevInfo& obj) const {
196 : // 组合各个成员的哈希值
197 27 : size_t hash1 = hash<Hccl::RankId>()(obj.rankId);
198 27 : size_t hash2 = hash<Hccl::PortDeploymentType>()(obj.type);
199 27 : size_t hash3 = hash<Hccl::LinkProtoType>()(obj.protoType);
200 27 : size_t hash4 = hash<u32>()(obj.devId);
201 27 : size_t hash5 = hash<Hccl::IpAddress>()(obj.addr); // 假设IpAddress有可用的hash
202 :
203 : // 改进的哈希组合方式,减少冲突
204 27 : size_t seed = 0;
205 27 : seed ^= hash1 + HASH_NET_DEV + (seed << HASH_SEED_LEFT_BIT) + (seed >> HASH_SEED_RIGHT_BIT);
206 27 : seed ^= hash2 + HASH_NET_DEV + (seed << HASH_SEED_LEFT_BIT) + (seed >> HASH_SEED_RIGHT_BIT);
207 27 : seed ^= hash3 + HASH_NET_DEV + (seed << HASH_SEED_LEFT_BIT) + (seed >> HASH_SEED_RIGHT_BIT);
208 27 : seed ^= hash4 + HASH_NET_DEV + (seed << HASH_SEED_LEFT_BIT) + (seed >> HASH_SEED_RIGHT_BIT);
209 27 : seed ^= hash5 + HASH_NET_DEV + (seed << HASH_SEED_LEFT_BIT) + (seed >> HASH_SEED_RIGHT_BIT);
210 27 : return seed;
211 : }
212 : };
213 : }
214 :
215 : #endif // HCCLV2_INNER_NET_DEVICE_H
|