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