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 1308 : explicit RankGraph(RankId myRank) : netInsts_(MAX_NET_LAYER), myRank_(myRank)
35 : {
36 436 : }
37 : friend class VirtualTopoStub; //声明虚拟拓扑打桩类为友元类 todo 修改类名
38 :
39 : // 修改接口
40 : void AddPeer(const std::shared_ptr<NetInstance::Peer> &peer);
41 : void AddNetInstance(const std::shared_ptr<NetInstance> &netInstance);
42 : void InitInnerRanks();
43 : void InitFinish();
44 :
45 : // 查询接口
46 : bool HasRank(RankId rankId) const;
47 : u32 GetRankSize() const;
48 : u32 GetInnerRankSize() const;
49 : RankId GetMyRank() const;
50 : LocalId GetLocalId(RankId rankId) const;
51 : LocalId GetReplacedLocalId(RankId rankId) const;
52 : std::set<u32> GetLevels(RankId rankId) const;
53 : u32 GetLevelNum() const;
54 : const NetInstance *GetNetInstanceByNetInstId(u32 netLayer, const std::string &netInstId) const;
55 : NetInstance *GetNetInstanceByNetInstId(u32 netLayer, const std::string &netInstId);
56 : const NetInstance *GetNetInstanceByRankId(u32 netLayer, RankId rankId) const;
57 : NetInstance *GetNetInstanceByRankId(u32 netLayer, RankId rankId);
58 : const std::shared_ptr<NetInstance::Peer> GetPeer(RankId rankId) const;
59 : std::vector<NetInstance::Path> GetPaths(u32 netLayer, RankId sRankId, RankId dRankId) const;
60 : u32 GetLayerRanks(const u32 netLayer) const; // 获取myRank在指定netLayer包含的rank总数
61 : void GetLocalInstRanks(const u32 netLayer, vector<u32> &rankList, u32 &rankNum) const; // 查询myRank在该netLayer下所在的netInstance中的所有ranks列表及总数
62 : u32 GetLocalInstSize(const u32 netLayer) const; // 查询myRank在该netLayer下所在的netInstance中的ranks总数
63 : const NetType GetNetType(const u32 netLayer) const; // 查询netLayer的NetType
64 : HcclResult GetNetInstanceList(const u32 netLayer, vector<u32> &instSizeList, u32 &listSize) 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 GetRanksByTopoInst(const u32 netLayer, const u32 topoInstId, std::vector<u32> &ranks, u32 &rankNum) const;
70 :
71 : HcclResult GetEndpointNum(uint32_t layer, uint32_t topoInstId, uint32_t* num) const;
72 : HcclResult GetEndpointDesc(uint32_t layer, uint32_t topoInstId, uint32_t *descNum, EndpointDesc *endpointDesc) const;
73 : HcclResult GetEndpointInfo(uint32_t rankId, const EndpointDesc* endPointDesc, EndpointAttr endpointAttr,
74 : uint32_t infoLen, void* info) const;
75 :
76 : // 创建子虚拟拓扑
77 : std::unique_ptr<RankGraph> CreateSubRankGraph(const std::vector<u32> &rankIds) const; // 外部接口传入类型为u32
78 : // 打包接口
79 : std::vector<char> GetPackedData(const std::vector<std::pair<u32, RankId>> &levelRankPairs) const;
80 : void Dump() const;
81 :
82 : private:
83 : RankId2PeerMap peers_; // <rankId, Peer>
84 : Level2Id2NetInst netInsts_; // <netLayer, netInstId, group>
85 : std::set<RankId> innerRanks_;
86 : RankId myRank_;
87 : bool initFlag_{false};
88 :
89 : void CreateSubNetInstances(const std::vector<RankId> rankIds, Level2Id2NetInst &subNetInsts,
90 : RankId2PeerMap &peers, RankGraph *subRankGraph) const;
91 : void AddSubPeers(const std::vector<RankId> &rankIds, RankGraph *subRankGraph, RankId2PeerMap &peers) const;
92 : void AddSubLinks(const std::vector<RankId> &rankIds, RankId2PeerMap &peers, Level2Id2NetInst &subNetInsts,
93 : RankId parentMyRank) const;
94 : };
95 :
96 : CommProtocol LinkProtocolToCommProtocol(const LinkProtocol &linkProtocol);
97 :
98 : std::shared_ptr<NetInstance> GetOrCreateNetInstance(u32 netLayer, const string &netInstId, NetType type,
99 : Level2Id2NetInst &netInsts, RankGraph *rankGraph);
100 : RankId GetSubRankId(const vector<RankId> &rankIds, RankId rank);
101 :
102 : void GetNewNodeInfo(u32 layer, RankId newRankId, const NetInstance::Link &oldLink,
103 : shared_ptr<NetInstance> &newNetInstance, RankId2PeerMap &tmpPeers,
104 : shared_ptr<NetInstance::Node> &newNode, shared_ptr<NetInstance::ConnInterface> &newIface,
105 : bool isSource);
106 :
107 : void AddNewLink(u32 layer, const NetInstance::Link &oldLink, RankId srcNewRankId, RankId dstNewRankId,
108 : shared_ptr<NetInstance> &newNetInstance, RankId2PeerMap &tmpPeers,
109 : const NetInstance *oldNetInstance, RankId parentMyRank);
110 :
111 : void AddGroupLinks(const vector<RankId> &rankIds, const NetInstance *oldNetInstance, shared_ptr<NetInstance> &newNetInstance,
112 : RankId2PeerMap &tmpPeers, RankId parentMyRank);
113 :
114 : HcclResult GetCommAddr(CommAddr &commAddr, const IpAddress &ipAddr);
115 :
116 : EndpointLocType AddrPositionToEndpointLoc(AddrPosition pos);
117 :
118 : } // namespace Hccl
119 :
120 : #endif // RANK_GRAPH_H
|