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 3432 : MAKE_ENUM(LinkDirection, BOTH, SEND_ONLY, RECV_ONLY)
31 16749 : MAKE_ENUM(LinkProtocol, UB_CTP, UB_TP, ROCE, HCCS, TCP, UB_MEM, PCIE, UBOE, UB_RTP)
32 : MAKE_ENUM(LinkPortType, PEER, BACKUP_PLANE, NET_PLANE)
33 11337 : MAKE_ENUM(LinkType, PEER2PEER, PEER2NET, PEER2BACKUP)
34 4088 : MAKE_ENUM(AddrPosition, HOST, DEVICE)
35 1085 : MAKE_ENUM(AddrType, EID, IPV4, IPV6)
36 3871 : MAKE_ENUM(NetType, CLOS, MESH_1D, MESH_2D, A3_SERVER, A2_AX_SERVER, TOPO_FILE_DESC)
37 4169 : 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 : // 类型不同,直接不等
44 2 : if (lhs.type != rhs.type) {
45 0 : return false;
46 : }
47 :
48 : // 类型相同,根据类型比较具体数据
49 2 : switch (lhs.type) {
50 2 : case COMM_ADDR_TYPE_IP_V4:
51 2 : return lhs.addr.s_addr == rhs.addr.s_addr;
52 :
53 0 : case COMM_ADDR_TYPE_IP_V6:
54 : // 比较 IPv6 地址的 16 字节
55 0 : return memcmp(lhs.addr6.s6_addr, rhs.addr6.s6_addr, sizeof(lhs.addr6.s6_addr)) == 0;
56 :
57 0 : case COMM_ADDR_TYPE_ID:
58 0 : return lhs.id == rhs.id;
59 :
60 0 : case COMM_ADDR_TYPE_EID:
61 : // 假设 COMM_ADDR_EID_LEN 是一个常量,比如 16
62 0 : return memcmp(lhs.eid, rhs.eid, COMM_ADDR_EID_LEN) == 0;
63 :
64 0 : case COMM_ADDR_TYPE_RESERVED:
65 : default:
66 0 : return true;
67 : }
68 : }
69 :
70 : namespace std {
71 : template <>
72 : struct hash<Hccl::LinkProtocol> {
73 : size_t operator()(const Hccl::LinkProtocol& k) const noexcept { return static_cast<std::size_t>(k); }
74 : };
75 : template <>
76 : struct hash<Hccl::NetType> {
77 25 : size_t operator()(const Hccl::NetType& k) const noexcept { return static_cast<std::size_t>(k); }
78 : };
79 : template <>
80 : struct hash<Hccl::TopoType> {
81 16 : size_t operator()(const Hccl::TopoType& k) const noexcept { return static_cast<std::size_t>(k); }
82 : };
83 :
84 : template <>
85 : struct hash<CommProtocol> {
86 535 : size_t operator()(const CommProtocol& protocol) const { return static_cast<size_t>(protocol); }
87 : };
88 :
89 : template <>
90 : struct hash<CommAddr> {
91 61 : size_t operator()(const CommAddr& commAddr) const noexcept
92 : {
93 61 : size_t h = 0;
94 61 : h = h ^ static_cast<size_t>(commAddr.type);
95 61 : switch (commAddr.type) {
96 0 : case COMM_ADDR_TYPE_EID: {
97 0 : for (u32 i = 0; i < COMM_ADDR_EID_LEN && i < sizeof(commAddr.eid); ++i) {
98 0 : h = h ^ (static_cast<size_t>(commAddr.eid[i]) << ((i % sizeof(size_t)) * 8));
99 : }
100 0 : break;
101 : }
102 61 : case COMM_ADDR_TYPE_IP_V4: {
103 : // IPv4地址哈希
104 61 : h = h ^ static_cast<size_t>(commAddr.addr.s_addr);
105 61 : break;
106 : }
107 0 : case COMM_ADDR_TYPE_IP_V6: {
108 0 : for (u32 i = 0; i < sizeof(commAddr.addr6); ++i) {
109 0 : h = h ^ static_cast<size_t>(commAddr.addr6.s6_addr[i]) << (i % 8);
110 : }
111 0 : break;
112 : }
113 0 : case COMM_ADDR_TYPE_ID: {
114 : // ID类型哈希
115 0 : h = h ^ static_cast<size_t>(commAddr.id);
116 0 : break;
117 : }
118 0 : default: {
119 0 : break;
120 : }
121 : }
122 61 : return h;
123 : }
124 : };
125 :
126 : template <>
127 : struct hash<std::pair<CommAddr, CommProtocol>> {
128 61 : size_t operator()(const std::pair<CommAddr, CommProtocol>& key) const
129 : {
130 61 : size_t h1 = std::hash<CommAddr>()(key.first);
131 61 : size_t h2 = std::hash<CommProtocol>()(key.second);
132 61 : return h1 ^ (h2 << 1);
133 : }
134 : };
135 : }; // namespace std
136 :
137 : #endif // TOPO_COMMON_TYPES_H
|