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 NET_INSTANCE_H
12 : #define NET_INSTANCE_H
13 :
14 : #include <set>
15 : #include <vector>
16 : #include <string>
17 : #include <memory>
18 : #include <unordered_map>
19 : #include <map>
20 : #include <utility>
21 :
22 : #include "graph.h"
23 : #include "ip_address.h"
24 : #include "iterator.h"
25 : #include "types.h"
26 : #include "topo_common_types.h"
27 :
28 : namespace Hccl {
29 : constexpr u32 DEFAULT_LISTENING_PORT = 60001;
30 : class NetInstance {
31 : public:
32 : class ConnInterface {
33 : public:
34 : // 使用地址信息、位置信息、链路类型、链路协议构造接口
35 1085 : explicit ConnInterface(const IpAddress inputAddr, const std::set<string> inputPorts, const AddrPosition inputPos, const LinkType inputLinkType,
36 387 : const std::set<LinkProtocol> inputLinkProtocol, TopoType inputTopoType = TopoType::CLOS, u32 intputTopoInstId = 0)
37 1085 : :addr(inputAddr), ports(inputPorts), pos(inputPos), linkType(inputLinkType), linkProtocols(inputLinkProtocol), topoType(inputTopoType), topoInstId(intputTopoInstId){}
38 : IpAddress GetAddr() const;
39 : AddrPosition GetPos() const;
40 : std::set<string> GetPorts() const;
41 : LinkType GetLinkType() const;
42 : std::set<LinkProtocol> GetLinkProtocols() const;
43 : void SetLocalDieId(u32 dieId);
44 : u32 GetLocalDieId() const;
45 : TopoType GetTopoType() const;
46 : u32 GetTopoInstId() const;
47 : std::string Describe() const;
48 : bool operator==(const ConnInterface &rhs) const;
49 : bool operator!=(const ConnInterface &rhs) const;
50 :
51 : private:
52 : IpAddress addr{};
53 : std::set<string> ports{};
54 : AddrPosition pos{};
55 : LinkType linkType{};
56 : std::set<LinkProtocol> linkProtocols{};
57 : u32 localDieId_{};
58 : TopoType topoType{TopoType::CLOS};
59 : u32 topoInstId{0};
60 : };
61 :
62 : class Node {
63 : public:
64 1403 : MAKE_ENUM(NodeType, PEER, FABRIC)
65 764 : explicit Node(NodeType nodeType) : type_(nodeType)
66 : {
67 764 : }
68 1014 : virtual ~Node() = default;
69 :
70 : void AddConnInterface(u32 layer, const shared_ptr<NetInstance::ConnInterface> &interface);
71 : void AddConnInterfaces(u32 layer, const std::vector<std::shared_ptr<NetInstance::ConnInterface>> &interfaces);
72 : NodeType GetType() const;
73 : std::vector<std::shared_ptr<NetInstance::ConnInterface>> GetIfacesByLayer(u32 layer) const;
74 : std::vector<std::shared_ptr<NetInstance::ConnInterface>> GetIfaces() const;
75 : void SetEndpointToIface(const CommAddr& commAddr, CommProtocol protocol, const std::shared_ptr<NetInstance::ConnInterface>& iface);
76 : const std::unordered_map<std::pair<CommAddr, CommProtocol>, std::shared_ptr<NetInstance::ConnInterface>> GetEndpointToIfaceMap() const;
77 : NodeId GetNodeId() const;
78 : string GetNodeIdStr() const;
79 : const std::unordered_map<u32, std::vector<std::shared_ptr<NetInstance::ConnInterface>>> GetInterfacesMap() const;
80 : virtual std::string Describe() const = 0;
81 :
82 : protected:
83 : NodeId nodeId_{0};
84 :
85 : private:
86 : std::unordered_map<u32, std::vector<std::shared_ptr<NetInstance::ConnInterface>>> interfacesMap_;
87 : std::unordered_map<std::pair<CommAddr, CommProtocol>, std::shared_ptr<NetInstance::ConnInterface>> endpointToIfaceMap_;
88 : NodeType type_;
89 : };
90 :
91 : class Peer : public Node {
92 : public:
93 : using NetInstancePtr = const NetInstance *;
94 687 : Peer(RankId rankId, LocalId localId, LocalId replacedLocalId, DeviceId deviceId, u32 devicePort = DEFAULT_LISTENING_PORT,
95 : u32 hostPort = DEFAULT_LISTENING_PORT)
96 1374 : : Node(NodeType::PEER), rankId_(rankId), localId_(localId), replacedLocalId_(replacedLocalId),
97 687 : deviceId_(deviceId), devicePort_(devicePort), hostPort_(hostPort)
98 : {
99 687 : nodeId_ = GenerateNodeId(rankId);
100 687 : }
101 : static NodeId GenerateNodeId(RankId rankId);
102 : void AddNetInstance(const std::shared_ptr<NetInstance> &NetInstance);
103 : LocalId GetLocalId() const;
104 : LocalId GetReplacedLocalId() const;
105 : RankId GetRankId() const;
106 : DeviceId GetDeviceId() const;
107 : u32 GetDevicePort() const;
108 : u32 GetHostPort() const;
109 : std::set<u32> GetLevels() const;
110 : NetInstancePtr GetNetInstance(u32 level) const;
111 : std::map<std::string, std::vector<IpAddress>> GetPortAddrMapLayer0() const;
112 : void SetPortPortAddrMapLayer0(std::map<std::string, std::vector<IpAddress>> portAddrMap);
113 : std::string Describe() const override;
114 : private:
115 : RankId rankId_;
116 : LocalId localId_;
117 : LocalId replacedLocalId_;
118 : DeviceId deviceId_;
119 : u32 devicePort_;
120 : u32 hostPort_;
121 : std::set<u32> netLayers_;
122 : std::map<std::string, std::vector<IpAddress>> portAddrMapLayer0_{}; // layer0 层端口与IpAddress的映射。
123 : std::vector<NetInstancePtr> netInsts_; // 下标为level,约束:level从0递增
124 : };
125 :
126 : class Fabric : public Node {
127 : public:
128 25 : explicit Fabric(FabricId fabricId, PlaneId planeId) : Node(NodeType::FABRIC), fabricId_(fabricId), planeId_(planeId)
129 : {
130 25 : nodeId_ = GenerateNodeId(fabricId);
131 25 : }
132 :
133 52 : explicit Fabric(FabricId fabricId)
134 104 : : Node(NodeType::FABRIC), fabricId_(fabricId), planeId_("")
135 : {
136 52 : nodeId_ = GenerateNodeId(fabricId);
137 52 : }
138 :
139 : PlaneId GetPlaneId() const;
140 : std::string Describe() const override;
141 :
142 : private:
143 : FabricId fabricId_;
144 : PlaneId planeId_;
145 : NodeId GenerateNodeId(FabricId fabricId) const;
146 : };
147 :
148 : class Link {
149 : public:
150 777 : Link(std::shared_ptr<NetInstance::Node> source, std::shared_ptr<NetInstance::Node> target,
151 : std::shared_ptr<NetInstance::ConnInterface> sourceIface, std::shared_ptr<NetInstance::ConnInterface> targetIface, LinkType type,
152 271 : std::set<LinkProtocol> linkProtocols, LinkDirection direction = LinkDirection::BOTH, u32 hop = 1)
153 777 : : source_(source), target_(target), sourceIface_(sourceIface), targetIface_(targetIface), type_(type),
154 777 : linkProtocols_(linkProtocols), direction_(direction), hop_(hop)
155 : {
156 777 : }
157 340 : Link() = default;
158 :
159 : u32 GetHop() const;
160 : LinkType GetType() const;
161 : std::set<LinkProtocol> GetLinkProtocols() const;
162 : LinkDirection GetLinkDirection() const;
163 : std::shared_ptr<NetInstance::ConnInterface> GetSourceIface() const;
164 : std::shared_ptr<NetInstance::ConnInterface> GetTargetIface() const;
165 : std::shared_ptr<NetInstance::Node> GetSourceNode() const;
166 : std::shared_ptr<NetInstance::Node> GetTargetNode() const;
167 : std::string Describe() const;
168 : bool IsEmpty() const;
169 :
170 : bool operator==(const Link &rhs) const;
171 : bool operator!=(const Link &rhs) const;
172 :
173 : private:
174 : std::shared_ptr<NetInstance::Node> source_{nullptr};
175 : std::shared_ptr<NetInstance::Node> target_{nullptr};
176 : std::shared_ptr<NetInstance::ConnInterface> sourceIface_{nullptr};
177 : std::shared_ptr<NetInstance::ConnInterface> targetIface_{nullptr}; // 如果target为Fabric节点,则为空
178 : LinkType type_{};
179 : set<LinkProtocol> linkProtocols_{};
180 : LinkDirection direction_{LinkDirection::BOTH};
181 : u32 hop_{1};
182 : };
183 :
184 : struct Path {
185 : std::vector<Link> links;
186 : LinkDirection direction{LinkDirection::BOTH};
187 : };
188 :
189 : struct TopoInstance {
190 : u32 topoInstId{0};
191 : TopoType topoType;
192 : std::set<RankId> ranks;
193 78 : TopoInstance() = default;
194 :
195 121 : TopoInstance(u32 instId) : topoInstId(instId)
196 121 : {}
197 : };
198 :
199 : // FabType: Fabric Group的拓扑类型,目前仅支持INNER与CLOS类型
200 : // INNER: 同Inner Group内Rank间互联
201 : // CLOS: 不同Rank经Fabric互联
202 : MAKE_ENUM(FabType, INNER, CLOS);
203 : std::unordered_map<u32,std::shared_ptr<TopoInstance>> topoInsts_;
204 :
205 : NetInstance(const u32 netLayer, const std::string &netInstId, const NetType netType);
206 243 : virtual ~NetInstance() = default;
207 :
208 : u32 GetNetLayer() const;
209 : std::string GetNetInstId() const;
210 : NetType GetNetType() const;
211 : std::set<RankId> GetRankIds() const;
212 : u32 GetRankSize() const;
213 : bool HasNode(const NodeId nodeId) const;
214 : const std::unordered_map<RankId, std::shared_ptr<Peer>>& GetPeers() const;
215 : const std::vector<std::shared_ptr<Fabric>>& GetFabrics() const;
216 : Graph<Node, Link>& GetGraph();
217 : void AddRankId(const RankId rankId);
218 : void AddNode(const std::shared_ptr<Node> &node);
219 : void AddLink(const std::shared_ptr<Link> &link);
220 : void DeleteLink(const NodeId srcNodeId, const NodeId dstNodeId);
221 :
222 : void UpdateTopoInst(u32 topoInstId, TopoType topoType, RankId rankId);
223 : void GetTopoInstsByLayer(std::vector<u32>& topoInsts, u32& topoInstNum) const;
224 : HcclResult GetTopoType(const u32 topoInstId, TopoType& topoType) const;
225 : HcclResult GetRanksByTopoInst(const u32 topoInstId, std::vector<u32>& ranks, u32& rankNum) const;
226 : virtual std::vector<Path> GetPaths(const RankId srcRankId, const RankId dstRankId) const = 0;
227 : std::string Describe() const;
228 :
229 : protected:
230 : u32 netLayer{0};
231 : std::string netInstId{""};
232 : NetType netType{NetType::CLOS};
233 : std::set<RankId> rankIds;
234 : std::unordered_map<RankId, std::shared_ptr<Peer>> peers;
235 : std::unordered_map<LocalId, RankId> localIdsMap;
236 : std::vector<std::shared_ptr<Fabric>> fabrics;
237 : std::unordered_map<PlaneId, NodeId> planeId2Node; // 除了创建时,其他是否需要使用
238 : Graph<Node, Link> vGraph;
239 :
240 : void AddPeer(const std::shared_ptr<Peer> &peer);
241 : void AddFabric(const std::shared_ptr<Fabric> &fabric);
242 : };
243 :
244 : class InnerNetInstance : public NetInstance {
245 : public:
246 189 : InnerNetInstance(const u32 netLayer, const std::string &netInstId) : NetInstance(netLayer, netInstId, NetType::TOPO_FILE_DESC){};
247 :
248 190 : ~InnerNetInstance() override = default;
249 :
250 : std::vector<Path> GetPaths(const RankId srcRankId, const RankId dstRankId) const override;
251 : };
252 :
253 : class ClosNetInstance : public NetInstance {
254 : public:
255 54 : ClosNetInstance(const u32 netLayer, const std::string &netInstId) : NetInstance(netLayer, netInstId, NetType::CLOS){};
256 :
257 56 : ~ClosNetInstance() override = default;
258 :
259 : std::vector<Path> GetPaths(const RankId srcRankId, const RankId dstRankId) const override;
260 : };
261 :
262 : } // namespace Hccl
263 :
264 : #endif // NET_INSTANCE_H
|