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 RANK_GRAPH_H
12 : #define RANK_GRAPH_H
13 :
14 : #include <memory>
15 : #include <set>
16 : #include <string>
17 : #include <unordered_map>
18 : #include <vector>
19 : #include "log.h"
20 : #include "net_instance.h"
21 : #include "rank_gph.h"
22 : #include "rank_table_info.h"
23 : #include "types.h"
24 : #include "hccl_res.h"
25 :
26 : namespace Hccl {
27 :
28 : using Level2Id2NetInst = std::vector<std::unordered_map<std::string, std::shared_ptr<NetInstance>>>;
29 : using RankId2PeerMap = std::unordered_map<RankId, std::shared_ptr<NetInstance::Peer>>;
30 : constexpr u32 MAX_NET_LAYER = 8;
31 :
32 : class RankGraph {
33 : public:
34 1446 : explicit RankGraph(RankId myRank) : netInsts_(MAX_NET_LAYER), myRank_(myRank) {}
35 : friend class VirtualTopoStub; // 声明虚拟拓扑打桩类为友元类 todo 修改类名
36 :
37 : // 修改接口
38 : void AddPeer(const std::shared_ptr<NetInstance::Peer>& peer);
39 : void AddNetInstance(const std::shared_ptr<NetInstance>& netInstance);
40 : void InitInnerRanks();
41 : void InitFinish();
42 :
43 : // 查询接口
44 : bool HasRank(RankId rankId) const;
45 : u32 GetRankSize() const;
46 : u32 GetInnerRankSize() const;
47 : RankId GetMyRank() const;
48 : LocalId GetLocalId(RankId rankId) const;
49 : LocalId GetReplacedLocalId(RankId rankId) const;
50 : std::set<u32> GetLevels(RankId rankId) const;
51 : u32 GetLevelNum() const;
52 : const NetInstance* GetNetInstanceByNetInstId(u32 netLayer, const std::string& netInstId) const;
53 : NetInstance* GetNetInstanceByNetInstId(u32 netLayer, const std::string& netInstId);
54 : const NetInstance* GetNetInstanceByRankId(u32 netLayer, RankId rankId) const;
55 : NetInstance* GetNetInstanceByRankId(u32 netLayer, RankId rankId);
56 : const std::shared_ptr<NetInstance::Peer> GetPeer(RankId rankId) const;
57 : std::vector<NetInstance::Path> GetPaths(u32 netLayer, RankId sRankId, RankId dRankId) const;
58 : u32 GetLayerRanks(const u32 netLayer) const; // 获取myRank在指定netLayer包含的rank总数
59 : void GetLocalInstRanks(const u32 netLayer, vector<u32>& rankList, u32& rankNum)
60 : const; // 查询myRank在该netLayer下所在的netInstance中的所有ranks列表及总数
61 : u32 GetLocalInstSize(const u32 netLayer) const; // 查询myRank在该netLayer下所在的netInstance中的ranks总数
62 : const NetType GetNetType(const u32 netLayer) const; // 查询netLayer的NetType
63 : HcclResult GetNetInstanceList(const u32 netLayer, vector<u32>& instSizeList, u32& listSize)
64 : const; // 给定netLayer,查询RankGraph在该netLayer分为多少NetInstance,以及每个NetInstance的size
65 : bool IsSymmetric(const u32 netLayer) const; // 给定netLayer,查询RankGraph在该netLayer是否是对称的
66 :
67 : void GetTopoInstsByLayer(const u32 netLayer, std::vector<u32>& topoInsts, u32& topoInstNum) const;
68 : HcclResult GetTopoType(const u32 netLayer, const u32 topoInstId, TopoType& topoType) const;
69 : HcclResult
70 : GetRanksByTopoInst(const u32 netLayer, const u32 topoInstId, std::vector<u32>& ranks, u32& rankNum) const;
71 :
72 : HcclResult GetEndpointNum(uint32_t layer, uint32_t topoInstId, uint32_t* num) const;
73 : HcclResult
74 : GetEndpointDesc(uint32_t layer, uint32_t topoInstId, uint32_t* descNum, EndpointDesc* endpointDesc) const;
75 : HcclResult GetEndpointInfo(
76 : uint32_t rankId, const EndpointDesc* endPointDesc, EndpointAttr endpointAttr, uint32_t infoLen,
77 : void* info) const;
78 :
79 : // 创建子虚拟拓扑
80 : std::unique_ptr<RankGraph> CreateSubRankGraph(const std::vector<u32>& rankIds) const; // 外部接口传入类型为u32
81 : // 打包接口
82 : std::vector<char> GetPackedData(const std::vector<std::pair<u32, RankId>>& levelRankPairs) const;
83 : void Dump() const;
84 :
85 : private:
86 : RankId2PeerMap peers_; // <rankId, Peer>
87 : Level2Id2NetInst netInsts_; // <netLayer, netInstId, group>
88 : std::set<RankId> innerRanks_;
89 : RankId myRank_;
90 : bool initFlag_{false};
91 :
92 : void CreateSubNetInstances(
93 : const std::vector<RankId> rankIds, Level2Id2NetInst& subNetInsts, RankId2PeerMap& peers,
94 : RankGraph* subRankGraph) const;
95 : void AddSubPeers(const std::vector<RankId>& rankIds, RankGraph* subRankGraph, RankId2PeerMap& peers) const;
96 : void AddSubLinks(
97 : const std::vector<RankId>& rankIds, RankId2PeerMap& peers, Level2Id2NetInst& subNetInsts,
98 : RankId parentMyRank) const;
99 : };
100 :
101 : CommProtocol LinkProtocolToCommProtocol(const LinkProtocol& linkProtocol);
102 :
103 : std::shared_ptr<NetInstance> GetOrCreateNetInstance(
104 : u32 netLayer, const string& netInstId, NetType type, Level2Id2NetInst& netInsts, RankGraph* rankGraph);
105 : RankId GetSubRankId(const vector<RankId>& rankIds, RankId rank);
106 :
107 : void GetNewNodeInfo(
108 : u32 layer, RankId newRankId, const NetInstance::Link& oldLink, shared_ptr<NetInstance>& newNetInstance,
109 : RankId2PeerMap& tmpPeers, shared_ptr<NetInstance::Node>& newNode, shared_ptr<NetInstance::ConnInterface>& newIface,
110 : bool isSource);
111 :
112 : void AddNewLink(
113 : u32 layer, const NetInstance::Link& oldLink, RankId srcNewRankId, RankId dstNewRankId,
114 : shared_ptr<NetInstance>& newNetInstance, RankId2PeerMap& tmpPeers, const NetInstance* oldNetInstance,
115 : RankId parentMyRank);
116 :
117 : void AddGroupLinks(
118 : const vector<RankId>& rankIds, const NetInstance* oldNetInstance, shared_ptr<NetInstance>& newNetInstance,
119 : RankId2PeerMap& tmpPeers, RankId parentMyRank);
120 :
121 : bool NeedUpdateTopoInstForSubGraph(const NetInstance* oldNetInstance, u32 topoInstId, RankId parentMyRank);
122 :
123 : HcclResult GetCommAddr(CommAddr& commAddr, const IpAddress& ipAddr);
124 :
125 : EndpointLocType AddrPositionToEndpointLoc(AddrPosition pos);
126 :
127 : } // namespace Hccl
128 :
129 : #endif // RANK_GRAPH_H
|