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 : #include "executor_utils.h"
12 :
13 : namespace Hccl {
14 1 : bool IsEnableCounterNotifyByDevType(const RankId myRank, const DevType devType)
15 : {
16 1 : switch (devType) {
17 1 : case DevType::DEV_TYPE_950:
18 : case DevType::DEV_TYPE_960:
19 3 : HCCL_DEBUG("[CollAlgFactory] Rank [%d], CounterNotify func enabled.", myRank);
20 1 : return true;
21 0 : default:
22 0 : HCCL_DEBUG("[CollAlgFactory] Rank [%d], CounterNotify func disabled.", myRank);
23 0 : return false;
24 : }
25 : }
26 :
27 1 : HcclResult InitOpInfo(const CollAlgOperator& op, OpType& opType, ReduceOp& redOp, u32& root)
28 : {
29 1 : opType = op.opType;
30 1 : switch (opType) {
31 0 : case OpType::ALLREDUCE:
32 : case OpType::REDUCESCATTER:
33 0 : redOp = op.reduceOp;
34 0 : break;
35 0 : case OpType::SCATTER:
36 : case OpType::BROADCAST:
37 0 : root = op.root;
38 0 : break;
39 0 : case OpType::REDUCE:
40 0 : redOp = op.reduceOp;
41 0 : root = op.root;
42 0 : break;
43 1 : default:
44 1 : break;
45 : }
46 1 : return HcclResult::HCCL_SUCCESS;
47 : }
48 :
49 0 : HcclResult InitDataInfo(const CollAlgOperator& op, DataType& dataType, DataType& outputDataType, u64& dataCount)
50 : {
51 0 : dataType = op.dataType;
52 0 : dataCount = op.dataCount;
53 0 : outputDataType = op.outputDataType;
54 0 : if (outputDataType == DataType::INVALID) {
55 0 : outputDataType = dataType;
56 : }
57 :
58 0 : return HcclResult::HCCL_SUCCESS;
59 : }
60 :
61 : // Get Prior Link from virtual topo
62 : const std::vector<NetInstance::Path>
63 4 : GetPathsFromRankGraph(const RankGraph* rankGraph, const RankId srcRank, const RankId dstRank)
64 : {
65 : // 遍历当前节点的所有层级,返回两个节点间查到到的所有path
66 4 : std::vector<NetInstance::Path> pathList;
67 4 : std::set<u32> levelSet = rankGraph->GetLevels(srcRank);
68 8 : for (u32 levelIdx : levelSet) {
69 4 : std::vector<NetInstance::Path> paths = rankGraph->GetPaths(levelIdx, srcRank, dstRank);
70 4 : pathList.insert(pathList.end(), paths.begin(), paths.end());
71 4 : }
72 4 : return pathList;
73 4 : }
74 :
75 2 : HcclResult AddToResLinks(const RankId vNeighborRank, const LinkData& linkData, ResLinks& resLinks)
76 : {
77 6 : HCCL_DEBUG(
78 : "RankId [%d] linkData.des[%s] resLinks[%zu]", vNeighborRank, linkData.Describe().c_str(), resLinks.size());
79 2 : auto rankLinkIter = resLinks.find(vNeighborRank);
80 2 : if (rankLinkIter == resLinks.end()) {
81 4 : std::vector<LinkData> tmpLinks = {linkData};
82 2 : resLinks.insert(std::pair<RankId, std::vector<LinkData>>(vNeighborRank, tmpLinks));
83 2 : } else {
84 0 : rankLinkIter->second.push_back(linkData);
85 : }
86 2 : return HcclResult::HCCL_SUCCESS;
87 : }
88 :
89 1 : HcclResult PrepResLinks(
90 : const RankId myRank, const RankGraph* rankGraph, const std::vector<BasePortType>& linkPriority,
91 : const LinkReq& linkReq, ResLinks& resLinks)
92 : {
93 3 : HCCL_DEBUG("PrepResLinks linkPriority.size()[%zu], linkReq.size()[%zu]", linkPriority.size(), linkReq.size());
94 3 : for (auto resReqIter = linkReq.begin(); resReqIter != linkReq.end(); resReqIter++) {
95 2 : const std::vector<NetInstance::Path> tmpPaths = GetPathsFromRankGraph(rankGraph, myRank, resReqIter->first);
96 2 : if (resReqIter->second == 1) {
97 2 : CHK_PRT_RET(
98 : tmpPaths.size() == 0,
99 : HCCL_ERROR(
100 : "[CollAlgFactory] Unable to obtain valid link, srcRank [%d], dstRank [%d].", myRank,
101 : resReqIter->first),
102 : HcclResult::HCCL_E_INTERNAL);
103 2 : LinkData requiredLinkData(tmpPaths[0]); // 当前只取第一条path
104 : // updata res
105 2 : CHK_PRT_RET(
106 : AddToResLinks(resReqIter->first, requiredLinkData, resLinks) != HcclResult::HCCL_SUCCESS,
107 : HCCL_ERROR("[CollAlgFactory] Rank [%d], Fail to prepare links.", myRank), HcclResult::HCCL_E_INTERNAL);
108 : } else {
109 0 : CHK_PRT_RET(
110 : tmpPaths.size() < resReqIter->second,
111 : HCCL_ERROR("[CollAlgFactory] Rank [%d], available linkNum smaller than required.", myRank),
112 : HcclResult::HCCL_E_INTERNAL);
113 : // 从所有path中选择前resReqIter->second条
114 0 : for (u32 linkNum = 0; linkNum < resReqIter->second; linkNum++) {
115 0 : LinkData requiredLinkData(tmpPaths[linkNum]);
116 : // updata res
117 0 : CHK_PRT_RET(
118 : AddToResLinks(resReqIter->first, requiredLinkData, resLinks) != HcclResult::HCCL_SUCCESS,
119 : HCCL_ERROR("[CollAlgFactory] Rank [%d], Fail to prepare links.", myRank),
120 : HcclResult::HCCL_E_INTERNAL);
121 : }
122 : }
123 2 : }
124 :
125 1 : return HcclResult::HCCL_SUCCESS;
126 : }
127 :
128 0 : HcclResult PrepResLinks(const RankId myRank, const LinkReq& linkReq, ConnectedLinkMgr* linkMgr, ResLinks& resLinks)
129 : {
130 0 : CHK_PTR_NULL(linkMgr);
131 0 : HCCL_DEBUG("PrepResLinks linkReq.size()[%zu]", linkReq.size());
132 0 : for (auto resReqIter = linkReq.begin(); resReqIter != linkReq.end(); resReqIter++) {
133 0 : if (resReqIter->second == 1) {
134 0 : auto rankId = resReqIter->first;
135 0 : auto links = linkMgr->GetLinks(rankId);
136 0 : CHK_PRT_RET(
137 : links.size() == 0, HCCL_ERROR("[PrepResLinks] Rank [%d], Fail to get peer links.", myRank),
138 : HcclResult::HCCL_E_INTERNAL);
139 0 : LinkData requiredLinkData = links[0];
140 : // updata res
141 0 : CHK_PRT_RET(
142 : AddToResLinks(resReqIter->first, requiredLinkData, resLinks) != HcclResult::HCCL_SUCCESS,
143 : HCCL_ERROR("[CollAlgFactory] Rank [%d], Fail to prepare links.", myRank), HcclResult::HCCL_E_INTERNAL);
144 0 : } else {
145 0 : for (u32 linkNum = 0; linkNum < resReqIter->second; linkNum++) {
146 0 : LinkData requiredLinkData = linkMgr->GetLinks(resReqIter->first)[linkNum];
147 : // updata res
148 0 : CHK_PRT_RET(
149 : AddToResLinks(resReqIter->first, requiredLinkData, resLinks) != HcclResult::HCCL_SUCCESS,
150 : HCCL_ERROR("[CollAlgFactory] Rank [%d], Fail to prepare links.", myRank),
151 : HcclResult::HCCL_E_INTERNAL);
152 : }
153 : }
154 : }
155 0 : return HcclResult::HCCL_SUCCESS;
156 : }
157 :
158 1 : HcclResult CalcResLinks(
159 : const RankId myRank, const RankGraph* rankGraph, const std::vector<BasePortType>& linkPriority,
160 : const LinkReq& linkReq, std::vector<LinkData>& links)
161 : {
162 3 : HCCL_DEBUG("CalcResLinks linkPriority.size()[%zu]", linkPriority.size());
163 3 : for (auto resReqIter = linkReq.begin(); resReqIter != linkReq.end(); resReqIter++) {
164 2 : const std::vector<NetInstance::Path> tmpPaths = GetPathsFromRankGraph(rankGraph, myRank, resReqIter->first);
165 2 : if (resReqIter->second == 1) {
166 2 : CHK_PRT_RET(
167 : tmpPaths.size() == 0,
168 : HCCL_ERROR(
169 : "[CollAlgFactory] Unable to obtain valid link, srcRank [%d], dstRank [%d].", myRank,
170 : resReqIter->first),
171 : HcclResult::HCCL_E_INTERNAL);
172 : // updata res
173 2 : links.emplace_back(tmpPaths[0]);
174 : } else {
175 0 : CHK_PRT_RET(
176 : tmpPaths.size() < resReqIter->second,
177 : HCCL_ERROR("[CollAlgFactory] Rank [%d], available linkNum smaller than required.", myRank),
178 : HcclResult::HCCL_E_INTERNAL);
179 0 : for (u32 linkNum = 0; linkNum < resReqIter->second; linkNum++) {
180 : // updata res
181 0 : links.emplace_back(tmpPaths[linkNum]);
182 : }
183 : }
184 2 : }
185 :
186 1 : return HcclResult::HCCL_SUCCESS;
187 : }
188 :
189 1 : HcclResult CalcLinkInfo(
190 : const RankId myRank, const RankGraph* rankGraph, const LinkReq& linkReq,
191 : std::vector<std::pair<u32, RankId>>& algTempLinksInfo)
192 : {
193 1 : std::set<u32> levelSet = rankGraph->GetLevels(myRank);
194 3 : for (auto resReqIter = linkReq.begin(); resReqIter != linkReq.end(); resReqIter++) {
195 2 : RankId remoteRank = resReqIter->first;
196 2 : if (resReqIter->second == 0) {
197 2 : continue;
198 : }
199 2 : if (levelSet.size() == 1) {
200 2 : algTempLinksInfo.push_back(std::make_pair(0, remoteRank));
201 2 : continue;
202 : }
203 : // 当前场景只考虑两层拓扑场景
204 0 : u32 levelIdx = 0;
205 0 : const NetInstance* netInstance = rankGraph->GetNetInstanceByRankId(levelIdx, myRank);
206 0 : std::set<RankId> rankSet = netInstance->GetRankIds();
207 0 : auto rankInRankSet = std::find(rankSet.begin(), rankSet.end(), remoteRank);
208 0 : if (rankInRankSet != rankSet.end()) {
209 0 : algTempLinksInfo.push_back(std::make_pair(0, remoteRank));
210 : } else {
211 0 : algTempLinksInfo.push_back(std::make_pair(1, remoteRank));
212 : }
213 0 : }
214 1 : return HcclResult::HCCL_SUCCESS;
215 1 : }
216 :
217 0 : HcclResult SetPathNumMapByRankGraphMultiLevel(
218 : const RankGraph* rankGraph, std::vector<std::vector<RankId>>& virtRanks_, RankId myRank_,
219 : std::vector<map<u32, u32>>& rank2PathNumMap)
220 : {
221 0 : uint64_t levelNum = 2;
222 0 : for (uint64_t levelNumIdx = 0; levelNumIdx < levelNum; levelNumIdx++) {
223 0 : rank2PathNumMap.emplace_back();
224 0 : for (auto rankIdx : virtRanks_[levelNumIdx]) {
225 0 : if (rankIdx == myRank_) {
226 0 : continue;
227 : }
228 0 : std::vector<NetInstance::Path> tmpPaths = rankGraph->GetPaths(levelNumIdx, myRank_, rankIdx);
229 0 : auto pathNum = 0;
230 0 : for (const auto& path : tmpPaths) {
231 0 : bool isWithPcie = false;
232 0 : for (const auto& link : path.links) {
233 0 : if (*link.GetLinkProtocols().begin() == LinkProtocol::PCIE) {
234 0 : isWithPcie = true;
235 0 : break;
236 : }
237 : }
238 0 : if (!isWithPcie) {
239 0 : pathNum++;
240 : }
241 : }
242 0 : rank2PathNumMap[levelNumIdx][rankIdx] = pathNum;
243 0 : HCCL_INFO("[%s]levelNumIdx[%u] rankIdx[%d] pathNum[%d]", __func__, levelNumIdx, rankIdx, pathNum);
244 0 : }
245 : }
246 0 : if (rank2PathNumMap.size() == 0) {
247 0 : HCCL_ERROR("No path to all remoteRank");
248 0 : return HcclResult::HCCL_E_INTERNAL;
249 : }
250 0 : return HcclResult::HCCL_SUCCESS;
251 : }
252 :
253 0 : HcclResult SetPathNumMapByRankGraphMultiLevel(
254 : const RankGraph* rankGraph, std::vector<RankId>& virtRanks_, RankId myRank_, std::map<u32, u32>& rank2PathNumMap)
255 : {
256 0 : std::set<u32> levelSet = rankGraph->GetLevels(myRank_);
257 0 : for (auto level : levelSet) {
258 0 : bool levelFlag = 1;
259 0 : for (auto rankIdx : virtRanks_) {
260 0 : if (rankIdx == myRank_) {
261 0 : continue;
262 : }
263 0 : std::vector<NetInstance::Path> tmpPaths = rankGraph->GetPaths(level, myRank_, rankIdx);
264 0 : if (tmpPaths.size() == 0) {
265 0 : rank2PathNumMap.clear();
266 0 : levelFlag = 0;
267 0 : break;
268 : }
269 0 : auto pathNum = 0;
270 0 : for (const auto& path : tmpPaths) {
271 0 : bool isWithPcie = false;
272 0 : for (const auto& link : path.links) {
273 0 : if (*link.GetLinkProtocols().begin() == LinkProtocol::PCIE) {
274 0 : isWithPcie = true;
275 0 : break;
276 : }
277 : }
278 0 : if (!isWithPcie) {
279 0 : pathNum++;
280 : }
281 : }
282 0 : rank2PathNumMap[rankIdx] = pathNum;
283 0 : HCCL_INFO("[%s]rankIdx[%d] pathNum[%d]", __func__, rankIdx, pathNum);
284 0 : }
285 0 : if (levelFlag) {
286 0 : break;
287 : }
288 : }
289 0 : if (rank2PathNumMap.size() == 0) {
290 0 : HCCL_ERROR("No path to all remoteRank");
291 0 : return HcclResult::HCCL_E_INTERNAL;
292 : }
293 0 : return HcclResult::HCCL_SUCCESS;
294 0 : }
295 :
296 0 : HcclResult SetPathNumMapByLinkMgrMultiLevel(
297 : ConnectedLinkMgr* linkMgr, std::vector<std::vector<RankId>>& virtRanks_, RankId myRank_,
298 : std::vector<map<u32, u32>>& rank2PathNumMap)
299 : {
300 : (void)myRank_;
301 0 : uint64_t levelNum = 2;
302 0 : for (uint64_t levelNumIdx = 0; levelNumIdx < levelNum; levelNumIdx++) {
303 0 : rank2PathNumMap.emplace_back();
304 0 : for (auto rankIdx : virtRanks_[levelNumIdx]) {
305 0 : auto links = linkMgr->GetLinks(levelNumIdx, rankIdx);
306 0 : auto linkNum = 0;
307 0 : for (const auto& link : links) {
308 0 : if (link.GetLinkProtocol() != LinkProtocol::PCIE) {
309 0 : linkNum++;
310 : }
311 : }
312 0 : if (linkNum != 0) {
313 0 : rank2PathNumMap[levelNumIdx][rankIdx] = linkNum;
314 : }
315 0 : HCCL_INFO("[%s]levelNumIdx[%u] rankIdx[%d] linkNum[%d]", __func__, levelNumIdx, rankIdx, linkNum);
316 0 : }
317 : }
318 0 : if (rank2PathNumMap.size() == 0) {
319 0 : HCCL_ERROR("No path to all remoteRank");
320 0 : return HcclResult::HCCL_E_INTERNAL;
321 : }
322 0 : return HcclResult::HCCL_SUCCESS;
323 : }
324 :
325 0 : HcclResult SetPathNumMapByLinkMgrMultiLevel(
326 : ConnectedLinkMgr* linkMgr, std::vector<RankId>& virtRanks_, RankId myRank_, map<u32, u32>& rank2PathNumMap)
327 : {
328 : (void)myRank_;
329 0 : for (u32 rankIdx : virtRanks_) {
330 0 : auto links = linkMgr->GetLinks(rankIdx);
331 0 : auto linkNum = 0;
332 0 : for (const auto& link : links) {
333 0 : if (link.GetLinkProtocol() != LinkProtocol::PCIE) {
334 0 : linkNum++;
335 : }
336 : }
337 0 : if (linkNum != 0) {
338 0 : rank2PathNumMap[rankIdx] = linkNum;
339 : }
340 0 : HCCL_INFO("[%s]rankIdx[%u] linkNum[%d]", __func__, rankIdx, linkNum);
341 0 : }
342 0 : if (rank2PathNumMap.size() == 0) {
343 0 : HCCL_ERROR("No path to all remoteRank");
344 0 : return HcclResult::HCCL_E_INTERNAL;
345 : }
346 0 : return HcclResult::HCCL_SUCCESS;
347 : }
348 :
349 : } // namespace Hccl
|