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