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 TOPO_MATCHER_H
12 : #define TOPO_MATCHER_H
13 :
14 : #include <condition_variable>
15 : #include "dispatcher.h"
16 : #include "comm_base_pub.h"
17 : #include "externalinput_pub.h"
18 : #include "coll_alg_param.h"
19 : #include "comm_factory_pub.h"
20 : #include "hccl_common.h"
21 : #include "calc_impl.h"
22 : #include "alg_env_config.h"
23 :
24 : namespace hccl {
25 : constexpr u32 COMM_LEVEL1_INDEX = COMM_LEVEL1;
26 : using HcclAlgoInfo = struct HcclAlgoInfoDef {
27 : bool inlineReduceSwitchOn; // 收到数量时同时完成Reduce计算
28 : std::string identifier;
29 : bool isUsedRdmaLevel0;
30 : bool isSupportAtomicWrite;
31 :
32 1236 : HcclAlgoInfoDef() : inlineReduceSwitchOn(true), identifier(""), isUsedRdmaLevel0(false), isSupportAtomicWrite(false)
33 618 : {}
34 : };
35 :
36 : struct HcclTopoInfo {
37 : u32 userRank; // 通信域 RankID
38 : u32 userRankSize; // 通信域的 Rank数量
39 : u32 devicePhyId;
40 : s32 deviceLogicId;
41 : std::vector<u32> nicList;
42 : bool isSingleMeshAggregation;
43 : u32 deviceNumPerAggregation; // 每个module中的Device数量
44 : u32 superPodNum; // 集群中总的超节点数
45 : DevType deviceType;
46 : TopoType topoType;
47 : bool is310P3Common;
48 : u32 serverNum;
49 : u32 meshAggregationRankSize;
50 : u32 multiModuleDiffDeviceNumMode;
51 : u32 multiSuperPodDiffServerNumMode;
52 : u32 multiSuperPodDiffDeviceNumMode;
53 : bool isDiffDeviceType;
54 : u32 gcdDeviceNumPerAggregation;
55 : u32 realUserRank;
56 : bool isDiffDeviceModule;
57 : u32 moduleNum;
58 : bool useSuperPodMode;
59 : std::unordered_map<u32, bool> isUsedRdmaMap;
60 : std::unordered_map<u32, u32> pairLinkCounter; // server内所有device间的链路类型计数
61 : bool isARSDoubleRing;
62 :
63 : std::vector<std::vector<std::vector<std::vector<u32>>>> CommPlaneSubGroupVector; // 保存所有 level 的通信分组信息
64 : std::map<AHCConcOpType, TemplateType> ahcAlgOption;
65 :
66 680 : HcclTopoInfo()
67 680 : : userRank(0),
68 680 : userRankSize(0),
69 680 : devicePhyId(0),
70 680 : deviceLogicId(0),
71 1360 : nicList(0),
72 680 : isSingleMeshAggregation(false),
73 680 : deviceNumPerAggregation(0),
74 680 : superPodNum(0),
75 680 : deviceType(DevType::DEV_TYPE_COUNT),
76 680 : topoType(TopoType::TOPO_TYPE_COMMON),
77 680 : is310P3Common(false),
78 680 : serverNum(0),
79 680 : meshAggregationRankSize(0),
80 680 : multiModuleDiffDeviceNumMode(0),
81 680 : multiSuperPodDiffServerNumMode(0),
82 680 : multiSuperPodDiffDeviceNumMode(0),
83 680 : isDiffDeviceType(false),
84 680 : realUserRank(0),
85 680 : isDiffDeviceModule(false),
86 680 : moduleNum(0),
87 680 : useSuperPodMode(false),
88 680 : isARSDoubleRing(true)
89 680 : {}
90 : };
91 :
92 : using HcclExternalEnable = struct HcclExternalEnableDef {
93 : u32 enableFfts;
94 : u32 deterministic;
95 : u32 intraRoceSwitch;
96 : u32 dumpDebug;
97 : u32 interHccsDisable;
98 : bool aivMode;
99 : bool aicpuUnfold;
100 : bool isOnlyAiv;
101 : s32 execTimeOut;
102 : std::map<HcclCMDType, std::vector<HcclAlgoType>> algoConfig;
103 :
104 618 : HcclExternalEnableDef()
105 618 : : enableFfts(1),
106 618 : deterministic(0),
107 618 : intraRoceSwitch(0),
108 618 : dumpDebug(0),
109 618 : interHccsDisable(0),
110 618 : aivMode(false),
111 618 : aicpuUnfold(false),
112 618 : isOnlyAiv(false),
113 618 : execTimeOut(GetInternalExecTimeOut())
114 : {
115 618 : SetDefaultAlgo();
116 618 : }
117 618 : void SetDefaultAlgo()
118 : {
119 64890 : for (u32 opType = 0; opType < static_cast<u32>(HcclCMDType::HCCL_CMD_MAX); opType++) {
120 64272 : algoConfig[static_cast<HcclCMDType>(opType)]
121 128544 : = GetExternalInputHcclAlgoConfig(static_cast<HcclCMDType>(opType));
122 : }
123 618 : }
124 : };
125 :
126 : bool CheckRankNeighbors(const std::vector<u32>& nicList);
127 : bool CheckSdmaWithRohTopo(const std::vector<u32>& nicList, std::vector<u32>& topoList);
128 :
129 : class TopoMatcher {
130 : public:
131 : explicit TopoMatcher(
132 : const std::vector<std::vector<std::vector<u32>>> CommPlaneRanks, const std::vector<bool> isBridgeVector,
133 : HcclTopoInfo& topoInfo, HcclAlgoInfo& algoInfo, HcclExternalEnable& externalEnable,
134 : std::vector<std::vector<std::vector<u32>>>& serverAndsuperPodToRank);
135 : HcclResult CalcCommPlaneInfo(
136 : const std::string& tag, const CommParaInfo& commParaInfo, std::vector<SingleSubCommTransport>& commTransport,
137 : TransportMemType inputMemType, TransportMemType outputMemType);
138 : HcclTopoInfo GetTopoInfo();
139 : HcclAlgoInfo GetAlgoInfo();
140 : u32 GetExternalInputHcclEnableFfts();
141 : u32 GetExternalInputHcclDeterministic();
142 : u32 GetExternalInputIntraRoceSwitch();
143 : u32 GetExternalInputHcclDumpDebug();
144 : u32 GetExternalInputInterHccsDisable();
145 : bool GetARSFlag();
146 : bool CheckSdmaWithRohTopo(const std::vector<u32>& nicList, std::vector<u32>& topoList);
147 : HcclResult GetSubRootForScatter(const u32 root, u32& subRoot);
148 : u32 GetSubRootUserRank(const u32 userRank, const u32 rootUserRank);
149 : u32 GetSubRootUserRankWithSuperPod(const u32 userRank, const u32 rootUserRank);
150 : u32 GetSubRootWithSuperPod(const u32 userRank, const u32 rootUserRank);
151 : HcclResult GetLocalSuperPodRankSize(const u32 userRank, u32& devNumInlocalPod, u32& rankIdxInPod);
152 : HcclResult GetLocalServerRankSize(const u32 userRank, u32& devNumInlocalServer, u32& rankIdxInServer);
153 : HcclResult SetDeterministicConfig(const u8 deterministic);
154 : HcclResult SetAivModeConfig(const bool aivMode);
155 : HcclResult SetOnlyAivModeConfig(const bool isOnlyAiv);
156 : bool GetIsOnlyAivConfig() const;
157 : HcclResult SetAicpuUnfoldConfig(const bool aicpuUnfold);
158 : HcclResult SetExecTimeOutConfig(const s32 execTimeOut);
159 : HcclResult SetAlgoConfig(const std::map<HcclCMDType, std::vector<HcclAlgoType>>& algoMap);
160 : u8 GetDeterministicConfig() const;
161 : bool GetAivModeConfig() const;
162 : bool GetAicpuUnfoldConfig() const;
163 : s32 GetExecTimeOutConfig() const;
164 : std::vector<HcclAlgoType> GetAlgoConfig(HcclCMDType opType = HcclCMDType::HCCL_CMD_ALL);
165 : HcclResult GetGlobalSubGroups(const CommPlane level, std::vector<std::vector<std::vector<u32>>>& globalSubGroups);
166 : HcclResult SetGlobalSubGroups(const CommPlane level, std::vector<std::vector<std::vector<u32>>>& globalSubGroups);
167 : HcclResult
168 : GetCommPlaneSubGroupVector(std::vector<std::vector<std::vector<std::vector<u32>>>>& commPlaneSubGroupVector);
169 : HcclResult
170 : SetCommPlaneSubGroupVector(std::vector<std::vector<std::vector<std::vector<u32>>>>& commPlaneSubGroupVector);
171 : void GetAHCAlgOption(std::map<AHCConcOpType, TemplateType>& ahcAlgOption);
172 : void SetAHCAlgOption(std::map<AHCConcOpType, TemplateType>& ahcAlgOption);
173 : std::vector<std::vector<u32>> GetCommPlaneRanks(CommPlane commPlane);
174 : HcclResult SetRankMap();
175 : HcclResult EditCommPlaneVector(CommPlane commPlane, std::vector<std::vector<u32>> commVector);
176 :
177 : protected:
178 : private:
179 : HcclResult GetRankMap(const CommParaInfo& commParaInfo, std::vector<SingleSubCommTransport>& commTransport);
180 :
181 : HcclResult SetIsUsedRdma(const CommParaInfo& commParaInfo, std::vector<SingleSubCommTransport>& commTransport);
182 :
183 : HcclResult GetSub2UserRankMap(CommPlane commPlane, u32 ringIndex, std::map<u32, u32>& subCommRank2UserRank);
184 :
185 : HcclResult GetUserRank2SubMap(CommPlane commPlane, u32 ringIndex, std::map<u32, u32>& userRank2subCommRank);
186 :
187 : HcclResult GetIsUsedRdma(const CommParaInfo& commParaInfo, bool& isUsedRdma);
188 :
189 : u32 GetSubCollectiveRank(const std::vector<u32>& vecPara) const;
190 :
191 : std::vector<std::vector<std::vector<u32>>> CommPlaneVector_;
192 : std::vector<bool> isBridgeVector_;
193 : HcclTopoInfo topoInfo_;
194 : HcclAlgoInfo algoInfo_;
195 : HcclExternalEnable externalEnable_;
196 : u32 userRank_;
197 : std::vector<std::vector<std::map<u32, u32>>> subCommRank2UserRank_;
198 : std::vector<std::vector<std::map<u32, u32>>> userRank2subCommRank_;
199 :
200 : // serverAndsuperPodToRank_[0]: 通信域在当前superPod内, 按照serverIdx划分的所有rank信息
201 : // serverAndsuperPodToRank_[1]: 通信域所有rank的信息, 按照superPodId -> RankInfo 的结构划分
202 : std::vector<std::vector<std::vector<u32>>> serverAndsuperPodToRank_;
203 :
204 : u32 userRankIdx_ = 0;
205 : };
206 : } // namespace hccl
207 :
208 : #endif /* * TOPO_MATCHER_H */
|