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 TOPO_COMMON_TYPES_H
12 : #define TOPO_COMMON_TYPES_H
13 :
14 : #include <cstdint>
15 : #include <cstring>
16 : #include <netinet/in.h>
17 :
18 : #include "hccl_res.h"
19 : #include "enum_factory.h"
20 : #include "hccl/base.h"
21 : #include "hccl_rank_graph.h"
22 : namespace Hccl {
23 : using NetPlaneId = u32;
24 : using LocalId = u32;
25 : using NodeId = u64;
26 : using DeviceId = u32;
27 : using PlaneId = std::string;
28 : using FabricId = u32;
29 :
30 3037 : MAKE_ENUM(LinkDirection, BOTH, SEND_ONLY, RECV_ONLY)
31 234209 : MAKE_ENUM(LinkProtocol, UB_CTP, UB_TP, ROCE, HCCS, TCP, UB_MEM, PCIE, UBOE, UBG)
32 : MAKE_ENUM(LinkPortType, PEER, BACKUP_PLANE, NET_PLANE)
33 172192 : MAKE_ENUM(LinkType, PEER2PEER, PEER2NET, PEER2BACKUP)
34 8126 : MAKE_ENUM(AddrPosition, HOST, DEVICE)
35 998 : MAKE_ENUM(AddrType, EID, IPV4, IPV6)
36 3813 : MAKE_ENUM(NetType, CLOS, MESH_1D, MESH_2D, A3_SERVER, A2_AX_SERVER, TOPO_FILE_DESC)
37 111515 : MAKE_ENUM(TopoType, CLOS, MESH_1D, MESH_2D, A3_SERVER, A2_AX_SERVER, TOPO_TYPE_RESERVED)
38 : constexpr LocalId BACKUP_LOCAL_ID = 64;
39 : } // namespace Hccl
40 :
41 2 : inline bool operator==(const CommAddr& lhs, const CommAddr& rhs) {
42 : // 类型不同,直接不等
43 2 : if (lhs.type != rhs.type) {
44 0 : return false;
45 : }
46 :
47 : // 类型相同,根据类型比较具体数据
48 2 : switch (lhs.type) {
49 2 : case COMM_ADDR_TYPE_IP_V4:
50 2 : return lhs.addr.s_addr == rhs.addr.s_addr;
51 :
52 0 : case COMM_ADDR_TYPE_IP_V6:
53 : // 比较 IPv6 地址的 16 字节
54 0 : return memcmp(lhs.addr6.s6_addr, rhs.addr6.s6_addr, sizeof(lhs.addr6.s6_addr)) == 0;
55 :
56 0 : case COMM_ADDR_TYPE_ID:
57 0 : return lhs.id == rhs.id;
58 :
59 0 : case COMM_ADDR_TYPE_EID:
60 : // 假设 COMM_ADDR_EID_LEN 是一个常量,比如 16
61 0 : return memcmp(lhs.eid, rhs.eid, COMM_ADDR_EID_LEN) == 0;
62 :
63 0 : case COMM_ADDR_TYPE_RESERVED:
64 : default:
65 0 : return true;
66 : }
67 : }
68 :
69 : namespace std {
70 : template <>
71 : struct hash<Hccl::LinkProtocol> {
72 : size_t operator()(const Hccl::LinkProtocol& k) const noexcept
73 : {
74 : return static_cast<std::size_t>(k);
75 : }
76 : };
77 : template <>
78 : struct hash<Hccl::NetType> {
79 25 : size_t operator()(const Hccl::NetType& k) const noexcept
80 : {
81 25 : return static_cast<std::size_t>(k);
82 : }
83 : };
84 : template <>
85 : struct hash<Hccl::TopoType> {
86 16 : size_t operator()(const Hccl::TopoType& k) const noexcept
87 : {
88 16 : return static_cast<std::size_t>(k);
89 : }
90 : };
91 :
92 : template <>
93 : struct hash<CommProtocol> {
94 517 : size_t operator()(const CommProtocol& protocol) const
95 : {
96 517 : return static_cast<size_t>(protocol);
97 : }
98 : };
99 :
100 : template <>
101 : struct hash<CommAddr> {
102 87 : size_t operator()(const CommAddr& commAddr) const noexcept
103 : {
104 87 : size_t h = 0;
105 87 : h = h ^ static_cast<size_t>(commAddr.type);
106 87 : switch (commAddr.type) {
107 0 : case COMM_ADDR_TYPE_EID: {
108 0 : for (u32 i = 0; i < COMM_ADDR_EID_LEN && i < sizeof(commAddr.eid); ++i) {
109 0 : h = h ^ (static_cast<size_t>(commAddr.eid[i]) << ((i % sizeof(size_t)) * 8));
110 : }
111 0 : break;
112 : }
113 87 : case COMM_ADDR_TYPE_IP_V4: {
114 : // IPv4地址哈希
115 87 : h = h ^ static_cast<size_t>(commAddr.addr.s_addr);
116 87 : break;
117 : }
118 0 : case COMM_ADDR_TYPE_IP_V6: {
119 0 : for (u32 i = 0; i < sizeof(commAddr.addr6); ++i) {
120 0 : h = h ^ static_cast<size_t>(commAddr.addr6.s6_addr[i]) << (i % 8);
121 : }
122 0 : break;
123 : }
124 0 : case COMM_ADDR_TYPE_ID: {
125 : // ID类型哈希
126 0 : h = h ^ static_cast<size_t>(commAddr.id);
127 0 : break;
128 : }
129 0 : default: {
130 0 : break;
131 : }
132 : }
133 87 : return h;
134 : }
135 : };
136 :
137 : template <>
138 : struct hash<std::pair<CommAddr, CommProtocol>> {
139 87 : size_t operator()(const std::pair<CommAddr, CommProtocol>& key) const
140 : {
141 87 : size_t h1 = std::hash<CommAddr>()(key.first);
142 87 : size_t h2 = std::hash<CommProtocol>()(key.second);
143 87 : return h1 ^ (h2 << 1);
144 : }
145 : };
146 : }; // namespace std
147 :
148 : #endif // TOPO_COMMON_TYPES_H
|