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