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