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 PHY_TOPO_H
12 : #define PHY_TOPO_H
13 :
14 : #include <vector>
15 : #include <set>
16 : #include <unordered_map>
17 : #include <memory>
18 : #include "topo_common_types.h"
19 : #include "iterator.h"
20 : #include "graph.h"
21 :
22 : namespace Hccl {
23 :
24 : class PhyTopo {
25 : public:
26 : static std::unique_ptr<PhyTopo>& GetInstance();
27 : class ConnInterface {
28 : public:
29 : // 使用地址信息、位置信息、链路类型、链路协议构造接口
30 : ConnInterface(
31 : const std::set<std::string> inputPorts, const AddrPosition inputPos, const LinkType inputLinkType,
32 : std::set<LinkProtocol> inputLinkProtocols);
33 : std::set<std::string> GetPorts() const;
34 : AddrPosition GetPos() const;
35 : LinkType GetLinkType() const;
36 : std::set<LinkProtocol> GetLinkProtocols() const;
37 : std::string Describe() const;
38 : bool operator==(const ConnInterface& rhs) const;
39 : bool operator!=(const ConnInterface& rhs) const;
40 :
41 : private:
42 : std::set<std::string> ports{};
43 : AddrPosition pos{};
44 : LinkType linkType{};
45 : std::set<LinkProtocol> linkProtocols{};
46 : };
47 :
48 : class Node {
49 : public:
50 : using IfaceIterator = BaseConstIterator<std::vector, std::shared_ptr<PhyTopo::ConnInterface>>;
51 649 : MAKE_ENUM(NodeType, PEER, FABRIC);
52 : explicit Node(const NodeType inputType);
53 : NodeType GetType() const;
54 : void AddConnInterface(const std::shared_ptr<PhyTopo::ConnInterface>& interface);
55 : IfaceIterator IterIfaces() const;
56 : virtual std::string Describe() const;
57 :
58 : private:
59 : NodeType type{};
60 : std::vector<std::shared_ptr<PhyTopo::ConnInterface>> interfaces{};
61 : };
62 :
63 : class Peer : public Node {
64 : public:
65 : explicit Peer(const LocalId localId);
66 : LocalId GetLocalId() const;
67 : static NodeId GetId(const LocalId localId);
68 : std::string Describe() const override;
69 :
70 : private:
71 : LocalId localId{};
72 : };
73 :
74 : class Fabric : public Node {
75 : public:
76 : explicit Fabric();
77 : static NodeId GetId();
78 : std::string Describe() const override;
79 : };
80 :
81 : struct LinkAttributes {
82 : LinkType linktype;
83 : std::set<LinkProtocol> protocols;
84 : };
85 :
86 : class Link {
87 : public:
88 : // 使用源节点、目的节点、链路类型、链路协议、topo类型、topoInstId 构造链路
89 : Link(
90 : std::shared_ptr<PhyTopo::Node> inputSource, std::shared_ptr<PhyTopo::Node> inputTarget,
91 : const LinkAttributes& linkAttrs, const TopoType topoType, const u32 topoInstId);
92 : void SetSourceIface(std::shared_ptr<PhyTopo::ConnInterface> inputSourceIface);
93 : void SetTargetIface(std::shared_ptr<PhyTopo::ConnInterface> inputTargetIface);
94 : LinkType GetType() const;
95 : std::set<LinkProtocol> GetLinkProtocols() const;
96 : LinkDirection GetLinkDirection() const;
97 : TopoType GetTopoType() const;
98 : u32 GetTopoInstId() const;
99 : u32 GetHop() const;
100 : std::shared_ptr<PhyTopo::ConnInterface> GetSourceIFace();
101 : std::shared_ptr<PhyTopo::ConnInterface> GetTargetIFace();
102 : std::shared_ptr<PhyTopo::Node> GetSourceNode();
103 : std::shared_ptr<PhyTopo::Node> GetTargetNode();
104 : std::string Describe() const;
105 :
106 : private:
107 : std::shared_ptr<PhyTopo::ConnInterface> sourceIface{nullptr};
108 : std::shared_ptr<PhyTopo::ConnInterface> targetIface{nullptr}; // 如果target为Fabric节点,则为空
109 : std::shared_ptr<PhyTopo::Node> source{nullptr};
110 : std::shared_ptr<PhyTopo::Node> target{nullptr};
111 : std::set<LinkProtocol> linkProtocols{};
112 : LinkType linkType{};
113 : LinkDirection direction{LinkDirection::BOTH};
114 : TopoType topoType{TopoType::CLOS};
115 : u32 topoInstId{0};
116 : u32 hop{1};
117 : };
118 :
119 : void AddTopoGraph(const u32 netLayer, std::shared_ptr<Graph<PhyTopo::Node, PhyTopo::Link>> topo);
120 : std::shared_ptr<Graph<PhyTopo::Node, PhyTopo::Link>> GetTopoGraph(const u32 netLayer) const;
121 : void InitFinish();
122 : bool IsInitFinished() const;
123 : void Clear();
124 : void Dump() const;
125 : bool IsNetLayerExisted(const u32 netLayer) const;
126 :
127 : private:
128 : std::unordered_map<u32, std::shared_ptr<Graph<PhyTopo::Node, PhyTopo::Link>>> topos;
129 : bool initFlag{false};
130 : };
131 : } // namespace Hccl
132 :
133 : #endif // PHY_TOPO_H
|