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 "rank_graph_interface.h"
12 : #include <set>
13 : #include <functional>
14 : #include <unordered_map>
15 : #include "topo_common_types.h"
16 :
17 : namespace Hccl {
18 :
19 0 : HcclResult IRankGraph::GetRankId(uint32_t* rank)
20 : {
21 0 : CHK_PTR_NULL(rankGraphPtr_);
22 0 : RankGraph* rankGraph = static_cast<RankGraph*>(rankGraphPtr_);
23 0 : *rank = rankGraph->GetMyRank();
24 0 : return HCCL_SUCCESS;
25 : }
26 :
27 535 : HcclResult IRankGraph::GetRankSize(uint32_t* rankSize)
28 : {
29 535 : CHK_PTR_NULL(rankGraphPtr_);
30 535 : RankGraph* rankGraph = static_cast<RankGraph*>(rankGraphPtr_);
31 535 : *rankSize = rankGraph->GetRankSize();
32 535 : return HCCL_SUCCESS;
33 : }
34 :
35 2 : HcclResult IRankGraph::GetDevicePort(const uint32_t rank, uint32_t* devPort)
36 : {
37 2 : CHK_PTR_NULL(devPort);
38 2 : CHK_PTR_NULL(rankGraphPtr_);
39 2 : RankGraph* rankGraph = static_cast<RankGraph*>(rankGraphPtr_);
40 2 : auto peer = rankGraph->GetPeer(rank);
41 2 : CHK_PTR_NULL(peer);
42 2 : *devPort = peer->GetDevicePort();
43 2 : return HCCL_SUCCESS;
44 2 : }
45 :
46 0 : HcclResult IRankGraph::GetHostPort(const uint32_t rank, uint32_t* hostPort)
47 : {
48 0 : CHK_PTR_NULL(hostPort);
49 0 : CHK_PTR_NULL(rankGraphPtr_);
50 0 : RankGraph* rankGraph = static_cast<RankGraph*>(rankGraphPtr_);
51 0 : auto peer = rankGraph->GetPeer(rank);
52 0 : CHK_PTR_NULL(peer);
53 0 : *hostPort = peer->GetHostPort();
54 0 : return HCCL_SUCCESS;
55 0 : }
56 :
57 0 : HcclResult IRankGraph::GetRankGraphInfo(void** graph, uint32_t* len)
58 : {
59 0 : CHK_PTR_NULL(rankGraphPtr_);
60 0 : RankGraph* rankGraph = static_cast<RankGraph*>(rankGraphPtr_);
61 0 : *graph = rankGraph;
62 0 : *len = sizeof(RankGraph);
63 0 : return HCCL_SUCCESS;
64 : }
65 :
66 0 : HcclResult IRankGraph::GetDeviceId(uint32_t rankId, uint32_t* deviceId)
67 : {
68 0 : CHK_PTR_NULL(rankGraphPtr_);
69 0 : RankGraph* rankGraph = static_cast<RankGraph*>(rankGraphPtr_);
70 0 : if (rankGraph->GetPeer(rankId) == nullptr) {
71 0 : HCCL_ERROR("[GetDeviceId] rankGraph peer is null!");
72 0 : return HCCL_E_PTR;
73 : }
74 0 : *deviceId = rankGraph->GetPeer(rankId)->GetDeviceId();
75 0 : return HCCL_SUCCESS;
76 : }
77 :
78 1 : HcclResult IRankGraph::GetNetLayers(uint32_t** netLayers, uint32_t* netLayerNum)
79 : {
80 1 : CHK_PTR_NULL(rankGraphPtr_);
81 1 : RankGraph* rankGraph = static_cast<RankGraph*>(rankGraphPtr_);
82 1 : u32 rankId = rankGraph->GetMyRank();
83 1 : std::set<u32> levels = rankGraph->GetLevels(rankId);
84 1 : netLayersVec_.clear();
85 1 : netLayersVec_ = std::vector<u32>(levels.begin(), levels.end());
86 1 : *netLayers = netLayersVec_.data();
87 1 : *netLayerNum = rankGraph->GetLevelNum();
88 1 : return HCCL_SUCCESS;
89 1 : }
90 :
91 2 : HcclResult IRankGraph::GetInstTopoTypeByNetLayer(uint32_t netLayer, CommTopo* topoType)
92 : {
93 2 : CHK_PTR_NULL(rankGraphPtr_);
94 2 : RankGraph* rankGraph = static_cast<RankGraph*>(rankGraphPtr_);
95 2 : u32 rankId = rankGraph->GetMyRank();
96 2 : std::set<u32> levels = rankGraph->GetLevels(rankId);
97 2 : if (levels.find(netLayer) == levels.end()) {
98 1 : HCCL_ERROR("[IRankGraph::GetInstTopoTypeByNetLayer] netLayer[%u] is invalid", netLayer);
99 1 : return HCCL_E_PARA;
100 : }
101 1 : auto type = rankGraph->GetNetType(netLayer);
102 : static const std::unordered_map<NetType, CommTopo> netTypeMap
103 : = {{NetType::CLOS, CommTopo::COMM_TOPO_CLOS},
104 : {NetType::MESH_1D, CommTopo::COMM_TOPO_1DMESH},
105 : {NetType::A3_SERVER, CommTopo::COMM_TOPO_910_93},
106 : {NetType::A2_AX_SERVER, CommTopo::COMM_TOPO_A2AXSERVER},
107 3 : {NetType::TOPO_FILE_DESC, CommTopo::COMM_TOPO_CUSTOM}};
108 :
109 1 : auto it = netTypeMap.find(type);
110 1 : if (it == netTypeMap.end()) {
111 0 : HCCL_ERROR("[GetInstTopoTypeByNetLayer] netType[%s] not in netTypeMap", type.Describe().c_str());
112 0 : return HCCL_E_PARA;
113 : }
114 1 : *topoType = it->second;
115 1 : return HCCL_SUCCESS;
116 2 : }
117 :
118 2 : HcclResult IRankGraph::GetInstSizeByNetLayer(uint32_t netLayer, uint32_t* rankNum)
119 : {
120 2 : CHK_PTR_NULL(rankGraphPtr_);
121 2 : RankGraph* rankGraph = static_cast<RankGraph*>(rankGraphPtr_);
122 2 : u32 rankId = rankGraph->GetMyRank();
123 2 : std::set<u32> levels = rankGraph->GetLevels(rankId);
124 2 : if (levels.find(netLayer) == levels.end()) {
125 1 : HCCL_ERROR("[IRankGraph::GetInstSizeByNetLayer] netLayer[%u] is invalid", netLayer);
126 1 : return HCCL_E_PARA;
127 : }
128 1 : u32 num = rankGraph->GetLocalInstSize(netLayer);
129 1 : *rankNum = static_cast<uint32_t>(num);
130 1 : return HCCL_SUCCESS;
131 2 : }
132 :
133 2 : HcclResult IRankGraph::GetInstRanksByNetLayer(uint32_t netLayer, uint32_t** rankList, uint32_t* rankNum)
134 : {
135 2 : CHK_PTR_NULL(rankGraphPtr_);
136 2 : RankGraph* rankGraph = static_cast<RankGraph*>(rankGraphPtr_);
137 2 : u32 rankId = rankGraph->GetMyRank();
138 2 : std::set<u32> levels = rankGraph->GetLevels(rankId);
139 2 : if (levels.find(netLayer) == levels.end()) {
140 1 : HCCL_ERROR("[IRankGraph::GetInstRanksByNetLayer] netLayer[%u] is invalid", netLayer);
141 1 : return HCCL_E_PARA;
142 : }
143 1 : u32 num = 0;
144 1 : rankListVec_.clear();
145 1 : rankGraph->GetLocalInstRanks(netLayer, rankListVec_, num);
146 1 : *rankList = rankListVec_.data();
147 1 : *rankNum = num;
148 1 : return HCCL_SUCCESS;
149 2 : }
150 :
151 2 : HcclResult IRankGraph::GetInstSizeListByNetLayer(uint32_t netLayer, uint32_t** instSizeList, uint32_t* listSize)
152 : {
153 2 : CHK_PTR_NULL(rankGraphPtr_);
154 2 : RankGraph* rankGraph = static_cast<RankGraph*>(rankGraphPtr_);
155 2 : u32 rankId = rankGraph->GetMyRank();
156 2 : std::set<u32> levels = rankGraph->GetLevels(rankId);
157 2 : if (levels.find(netLayer) == levels.end()) {
158 1 : HCCL_ERROR("[IRankGraph::GetInstSizeListByNetLayer] netLayer[%u] is invalid", netLayer);
159 1 : return HCCL_E_PARA;
160 : }
161 1 : u32 size = 0;
162 1 : instSizeVec_.clear();
163 1 : auto ret = rankGraph->GetNetInstanceList(netLayer, instSizeVec_, size);
164 1 : CHK_PRT_RET(
165 : ret != HCCL_SUCCESS,
166 : HCCL_ERROR(
167 : "[IRankGraph::GetInstSizeListByNetLayer] Failed to get instSizeList at netLayer[%u], "
168 : "myRank[%u], ret[%d]",
169 : netLayer, rankId, ret),
170 : ret);
171 1 : *instSizeList = instSizeVec_.data();
172 1 : *listSize = size;
173 1 : return HCCL_SUCCESS;
174 2 : }
175 :
176 2 : static HcclResult SetCommAddress(CommAddr& commAddr, const IpAddress& ipAddr)
177 : {
178 2 : s32 family = ipAddr.GetFamily();
179 2 : if (family == AF_INET) {
180 2 : string addr = ipAddr.GetIpStr();
181 2 : if (ipAddr.IsEID(addr)) {
182 0 : commAddr.type = COMM_ADDR_TYPE_EID;
183 0 : const auto& eid = ipAddr.GetEid();
184 0 : for (u32 i = 0; i < URMA_EID_LEN && i < sizeof(commAddr.eid); i++) {
185 0 : commAddr.eid[i] = eid.raw[i];
186 : }
187 : } else {
188 2 : commAddr.type = COMM_ADDR_TYPE_IP_V4;
189 2 : commAddr.addr = ipAddr.GetBinaryAddress().addr;
190 : }
191 2 : } else if (family == AF_INET6) {
192 0 : commAddr.type = COMM_ADDR_TYPE_IP_V6;
193 0 : commAddr.addr6 = ipAddr.GetBinaryAddress().addr6;
194 : } else {
195 0 : HCCL_ERROR("[SetCommAddress] invalid commAddrType");
196 0 : return HCCL_E_INTERNAL;
197 : }
198 2 : return HCCL_SUCCESS;
199 : }
200 :
201 2 : static HcclResult SetEndpointLoc(EndpointLocType& locType, const AddrPosition& position)
202 : {
203 2 : if (position == AddrPosition::DEVICE) {
204 2 : locType = ENDPOINT_LOC_TYPE_DEVICE;
205 0 : } else if (position == AddrPosition::HOST) {
206 0 : locType = ENDPOINT_LOC_TYPE_HOST;
207 : } else {
208 0 : locType = ENDPOINT_LOC_TYPE_RESERVED;
209 : }
210 2 : return HCCL_SUCCESS;
211 : }
212 :
213 1 : static HcclResult InsertInnerLink(u32 netLayer, const NetInstance::Path& path, std::vector<CommLink>& linkListVec)
214 : {
215 2 : for (const auto& link : path.links) {
216 1 : const NetInstance::Link* peer2peer = &link;
217 2 : for (LinkProtocol protocol : link.GetLinkProtocols()) {
218 : CommLink commLink;
219 1 : CommLinkInit(&commLink, 1);
220 1 : const CommProtocol& commProtocol = LinkProtocolToCommProtocol(protocol);
221 1 : commLink.linkAttr.linkProtocol = commProtocol;
222 1 : commLink.linkAttr.hop = peer2peer->GetHop();
223 1 : commLink.srcEndpointDesc.protocol = commProtocol;
224 1 : commLink.dstEndpointDesc.protocol = commProtocol;
225 :
226 : // 设置源端点
227 1 : std::shared_ptr<NetInstance::ConnInterface> srcConnInterface = link.GetSourceIface();
228 1 : CHK_PTR_NULL(srcConnInterface);
229 1 : HcclResult result = SetCommAddress(commLink.srcEndpointDesc.commAddr, srcConnInterface->GetAddr());
230 1 : if (result != HCCL_SUCCESS) {
231 0 : HCCL_ERROR(
232 : "[IRankGraph::%s] SetCommAddress FAILED for srcConn: %s.", __func__,
233 : srcConnInterface->Describe().c_str());
234 0 : return result;
235 : }
236 1 : CHK_RET(SetEndpointLoc(commLink.srcEndpointDesc.loc.locType, srcConnInterface->GetPos()));
237 1 : CHK_RET(SetEndpointTopoInfo(commLink.srcEndpointDesc, netLayer, srcConnInterface->GetTopoInstId()));
238 :
239 : // 设置目标端点
240 1 : std::shared_ptr<NetInstance::ConnInterface> dstConnInterface = link.GetTargetIface();
241 1 : CHK_PTR_NULL(dstConnInterface);
242 1 : result = SetCommAddress(commLink.dstEndpointDesc.commAddr, dstConnInterface->GetAddr());
243 1 : if (result != HCCL_SUCCESS) {
244 0 : HCCL_ERROR(
245 : "[IRankGraph::%s] SetCommAddress FAILED for dstConn: %s.", __func__,
246 : dstConnInterface->Describe().c_str());
247 0 : return result;
248 : }
249 :
250 1 : CHK_RET(SetEndpointLoc(commLink.dstEndpointDesc.loc.locType, dstConnInterface->GetPos()));
251 1 : CHK_RET(SetEndpointTopoInfo(commLink.dstEndpointDesc, netLayer, dstConnInterface->GetTopoInstId()));
252 :
253 1 : if (commLink.srcEndpointDesc.loc.locType == ENDPOINT_LOC_TYPE_DEVICE) {
254 1 : std::shared_ptr<NetInstance::Node> srcNode = peer2peer->GetSourceNode();
255 1 : std::shared_ptr<NetInstance::Node> dstNode = peer2peer->GetTargetNode();
256 1 : std::shared_ptr<NetInstance::Peer> srcPeer = std::dynamic_pointer_cast<NetInstance::Peer>(srcNode);
257 1 : std::shared_ptr<NetInstance::Peer> dstPeer = std::dynamic_pointer_cast<NetInstance::Peer>(dstNode);
258 1 : commLink.srcEndpointDesc.loc.device.devPhyId = srcPeer->GetDeviceId();
259 1 : commLink.dstEndpointDesc.loc.device.devPhyId = dstPeer->GetDeviceId();
260 1 : }
261 :
262 1 : linkListVec.emplace_back(std::move(commLink));
263 2 : }
264 : }
265 :
266 1 : return HCCL_SUCCESS;
267 : }
268 :
269 0 : static HcclResult InsertClosLinks(u32 netLayer, const NetInstance::Path& path, std::vector<CommLink>& linkListVec)
270 : {
271 0 : const NetInstance::Link* peer2net = nullptr;
272 0 : const NetInstance::Link* net2peer = nullptr;
273 0 : for (const auto& link : path.links) {
274 0 : bool srcNull = (link.GetSourceIface() == nullptr);
275 0 : bool dstNull = (link.GetTargetIface() == nullptr);
276 0 : if (!srcNull && dstNull) {
277 0 : peer2net = &link;
278 0 : } else if (srcNull && !dstNull) {
279 0 : net2peer = &link;
280 : }
281 : }
282 0 : CHK_PTR_NULL(peer2net);
283 0 : CHK_PTR_NULL(net2peer);
284 :
285 0 : auto srcInterface = peer2net->GetSourceIface();
286 0 : auto dstInterface = net2peer->GetTargetIface();
287 0 : CHK_PTR_NULL(srcInterface);
288 0 : CHK_PTR_NULL(dstInterface);
289 0 : for (LinkProtocol protocol : peer2net->GetLinkProtocols()) {
290 : CommLink commLink;
291 0 : CommLinkInit(&commLink, 1);
292 0 : const CommProtocol& commProtocol = LinkProtocolToCommProtocol(protocol);
293 :
294 0 : commLink.linkAttr.linkProtocol = commProtocol;
295 0 : commLink.linkAttr.hop = peer2net->GetHop();
296 0 : commLink.srcEndpointDesc.protocol = commProtocol;
297 0 : commLink.dstEndpointDesc.protocol = commProtocol;
298 :
299 : // 设置源端点
300 0 : CHK_RET(SetCommAddress(commLink.srcEndpointDesc.commAddr, srcInterface->GetAddr()));
301 0 : CHK_RET(SetEndpointLoc(commLink.srcEndpointDesc.loc.locType, srcInterface->GetPos()));
302 0 : CHK_RET(SetEndpointTopoInfo(commLink.srcEndpointDesc, netLayer, srcInterface->GetTopoInstId()));
303 0 : if (commLink.srcEndpointDesc.loc.locType == ENDPOINT_LOC_TYPE_DEVICE) {
304 0 : std::shared_ptr<NetInstance::Node> srcNode = peer2net->GetSourceNode();
305 0 : std::shared_ptr<NetInstance::Peer> srcPeer = std::dynamic_pointer_cast<NetInstance::Peer>(srcNode);
306 0 : commLink.srcEndpointDesc.loc.device.devPhyId = srcPeer->GetDeviceId();
307 0 : }
308 :
309 : // 设置目标端点
310 0 : CHK_RET(SetCommAddress(commLink.dstEndpointDesc.commAddr, dstInterface->GetAddr()));
311 0 : CHK_RET(SetEndpointLoc(commLink.dstEndpointDesc.loc.locType, dstInterface->GetPos()));
312 0 : CHK_RET(SetEndpointTopoInfo(commLink.dstEndpointDesc, netLayer, dstInterface->GetTopoInstId()));
313 0 : if (commLink.dstEndpointDesc.loc.locType == ENDPOINT_LOC_TYPE_DEVICE) {
314 0 : std::shared_ptr<NetInstance::Node> dstNode = net2peer->GetTargetNode();
315 0 : std::shared_ptr<NetInstance::Peer> dstPeer = std::dynamic_pointer_cast<NetInstance::Peer>(dstNode);
316 0 : commLink.dstEndpointDesc.loc.device.devPhyId = dstPeer->GetDeviceId();
317 0 : }
318 :
319 0 : linkListVec.emplace_back(std::move(commLink));
320 0 : }
321 0 : return HCCL_SUCCESS;
322 0 : }
323 :
324 : HcclResult
325 2 : IRankGraph::GetLinks(uint32_t netLayer, uint32_t srcRank, uint32_t dstRank, CommLink** linkList, uint32_t* listSize)
326 : {
327 2 : CHK_PTR_NULL(rankGraphPtr_);
328 2 : RankGraph* rankGraph = static_cast<RankGraph*>(rankGraphPtr_);
329 2 : u32 rankId = rankGraph->GetMyRank();
330 2 : std::set<u32> levels = rankGraph->GetLevels(rankId);
331 2 : if (levels.find(netLayer) == levels.end()) {
332 1 : HCCL_ERROR("[IRankGraph::GetLinks] netLayer[%u] is invalid", netLayer);
333 1 : return HCCL_E_PARA;
334 : }
335 1 : std::vector<NetInstance::Path> paths = rankGraph->GetPaths(netLayer, srcRank, dstRank);
336 1 : linkListVec_.clear();
337 : // 遍历每条path
338 2 : for (const auto& path : paths) {
339 : // 检查是否是Clos网络(有nullptr接口)
340 1 : bool isClos = false;
341 2 : for (const auto& link : path.links) {
342 : // fabric没有接口
343 1 : if (link.GetSourceIface() == nullptr || link.GetTargetIface() == nullptr) {
344 0 : isClos = true;
345 0 : break;
346 : }
347 : }
348 1 : if (!isClos) {
349 : // Peer2Peer网络:直接处理每条link
350 1 : HcclResult ret = InsertInnerLink(netLayer, path, linkListVec_);
351 1 : CHK_PRT_RET(
352 : ret != HCCL_SUCCESS,
353 : HCCL_ERROR(
354 : "[IRankGraph::%s] InsertInnerLink failed for Peer2Peer, linkNum[%zu], ret[%d]", __func__,
355 : path.links.size(), ret),
356 : ret);
357 : } else {
358 : // Clos网络:找到peer2net和net2peer,组合成一条链路
359 0 : HcclResult ret = InsertClosLinks(netLayer, path, linkListVec_);
360 0 : CHK_PRT_RET(
361 : ret != HCCL_SUCCESS,
362 : HCCL_ERROR(
363 : "[IRankGraph::%s] InsertClosLinks failed for Clos, linkNum[%zu], ret[%d]", __func__,
364 : path.links.size(), ret),
365 : ret);
366 : }
367 : }
368 1 : *linkList = linkListVec_.data();
369 1 : *listSize = linkListVec_.size();
370 1 : return HCCL_SUCCESS;
371 2 : }
372 :
373 1 : HcclResult IRankGraph::GetTopoInstsByLayer(uint32_t netLayer, uint32_t** topoInsts, uint32_t* topoInstNum)
374 : {
375 1 : CHK_PTR_NULL(rankGraphPtr_);
376 1 : RankGraph* rankGraph = static_cast<RankGraph*>(rankGraphPtr_);
377 1 : u32 rankId = rankGraph->GetMyRank();
378 1 : std::set<u32> levels = rankGraph->GetLevels(rankId);
379 1 : if (levels.find(netLayer) == levels.end()) {
380 0 : HCCL_ERROR("[IRankGraph::GetTopoInstsByLayer] netLayer[%u] is invalid", netLayer);
381 0 : return HCCL_E_PARA;
382 : }
383 1 : u32 num = 0;
384 1 : topoInstsVec_.clear();
385 1 : rankGraph->GetTopoInstsByLayer(netLayer, topoInstsVec_, num);
386 1 : *topoInsts = topoInstsVec_.data();
387 1 : *topoInstNum = num;
388 1 : return HCCL_SUCCESS;
389 1 : }
390 :
391 2 : HcclResult IRankGraph::GetTopoType(const uint32_t netLayer, const uint32_t topoInstId, CommTopo* topoType)
392 : {
393 2 : CHK_PTR_NULL(rankGraphPtr_);
394 2 : RankGraph* rankGraph = static_cast<RankGraph*>(rankGraphPtr_);
395 2 : u32 rankId = rankGraph->GetMyRank();
396 2 : std::set<u32> levels = rankGraph->GetLevels(rankId);
397 2 : if (levels.find(netLayer) == levels.end()) {
398 0 : HCCL_ERROR("[IRankGraph::GetTopoType] netLayer[%u] is invalid", netLayer);
399 0 : return HCCL_E_PARA;
400 : }
401 2 : Hccl::TopoType type;
402 2 : HcclResult ret = rankGraph->GetTopoType(netLayer, topoInstId, type);
403 2 : if (ret != HCCL_SUCCESS) {
404 1 : HCCL_ERROR(
405 : "[IRankGraph::GetTopoType] Failed to get topo type at netLayer [%u] topoInstId [%u] ret[%d]", netLayer,
406 : topoInstId, ret);
407 1 : return ret;
408 : }
409 : static const std::unordered_map<Hccl::TopoType, CommTopo> topoTypeMap
410 : = {{Hccl::TopoType::CLOS, COMM_TOPO_CLOS},
411 : {Hccl::TopoType::MESH_1D, COMM_TOPO_1DMESH},
412 : {Hccl::TopoType::A3_SERVER, COMM_TOPO_910_93},
413 3 : {Hccl::TopoType::A2_AX_SERVER, COMM_TOPO_A2AXSERVER}};
414 1 : auto it = topoTypeMap.find(type);
415 1 : if (it != topoTypeMap.end()) {
416 1 : *topoType = it->second;
417 1 : return HCCL_SUCCESS;
418 : }
419 0 : HCCL_ERROR(
420 : "[IRankGraph::GetTopoType] topoType[%s] is not supported, netLayer[%u], "
421 : "topoInstId[%u], myRank[%u]",
422 : type.Describe().c_str(), netLayer, topoInstId, rankId);
423 0 : return HCCL_E_PARA;
424 2 : }
425 :
426 : HcclResult
427 2 : IRankGraph::GetRanksByTopoInst(const uint32_t netLayer, const uint32_t topoInstId, uint32_t** ranks, uint32_t* rankNum)
428 : {
429 2 : CHK_PTR_NULL(rankGraphPtr_);
430 2 : RankGraph* rankGraph = static_cast<RankGraph*>(rankGraphPtr_);
431 2 : u32 rankId = rankGraph->GetMyRank();
432 2 : std::set<u32> levels = rankGraph->GetLevels(rankId);
433 2 : if (levels.find(netLayer) == levels.end()) {
434 0 : HCCL_ERROR("[IRankGraph::GetRanksByTopoInst] netLayer[%u] is invalid", netLayer);
435 0 : return HCCL_E_PARA;
436 : }
437 2 : u32 num = 0;
438 2 : auto ret = rankGraph->GetRanksByTopoInst(netLayer, topoInstId, ranksVec_, num);
439 2 : if (ret != HCCL_SUCCESS) {
440 1 : HCCL_ERROR(
441 : "[IRankGraph::GetRanksByTopoInst] Failed to get ranks at netLayer [%u] topoInstId [%u] ret[%d]", netLayer,
442 : topoInstId, ret);
443 1 : return ret;
444 : }
445 1 : *ranks = ranksVec_.data();
446 1 : *rankNum = ranksVec_.size();
447 1 : return HCCL_SUCCESS;
448 2 : }
449 :
450 1 : HcclResult IRankGraph::GetEndpointNum(uint32_t netLayer, uint32_t topoInstId, uint32_t* num)
451 : {
452 1 : CHK_PTR_NULL(rankGraphPtr_);
453 1 : RankGraph* rankGraph = static_cast<RankGraph*>(rankGraphPtr_);
454 1 : u32 rankId = rankGraph->GetMyRank();
455 1 : std::set<u32> levels = rankGraph->GetLevels(rankId);
456 1 : if (levels.find(netLayer) == levels.end()) {
457 0 : HCCL_ERROR("[IRankGraph::GetEndpointNum] netLayer[%u] is invalid", netLayer);
458 0 : return HCCL_E_PARA;
459 : }
460 1 : auto ret = rankGraph->GetEndpointNum(netLayer, topoInstId, num);
461 1 : CHK_PRT_RET(
462 : ret != HCCL_SUCCESS,
463 : HCCL_ERROR(
464 : "[IRankGraph::GetEndpointNum] Failed to get endpoint num at netLayer[%u], "
465 : "topoInstId[%u], myRank[%u], ret[%d]",
466 : netLayer, topoInstId, rankId, ret),
467 : ret);
468 1 : return HCCL_SUCCESS;
469 1 : }
470 :
471 : HcclResult
472 1 : IRankGraph::GetEndpointDesc(uint32_t netLayer, uint32_t topoInstId, uint32_t* descNum, EndpointDesc* endpointDesc)
473 : {
474 1 : CHK_PTR_NULL(rankGraphPtr_);
475 1 : RankGraph* rankGraph = static_cast<RankGraph*>(rankGraphPtr_);
476 1 : u32 rankId = rankGraph->GetMyRank();
477 1 : std::set<u32> levels = rankGraph->GetLevels(rankId);
478 1 : if (levels.find(netLayer) == levels.end()) {
479 0 : HCCL_ERROR("[IRankGraph::GetEndpointDesc] netLayer[%u] is invalid", netLayer);
480 0 : return HCCL_E_PARA;
481 : }
482 1 : auto ret = rankGraph->GetEndpointDesc(netLayer, topoInstId, descNum, endpointDesc);
483 1 : CHK_PRT_RET(
484 : ret != HCCL_SUCCESS,
485 : HCCL_ERROR(
486 : "[IRankGraph::GetEndpointDesc] Failed to get endpoint desc at netLayer[%u], "
487 : "topoInstId[%u], myRank[%u], descNum[%u], ret[%d]",
488 : netLayer, topoInstId, rankId, *descNum, ret),
489 : ret);
490 1 : return HCCL_SUCCESS;
491 1 : }
492 :
493 0 : static const char* EndpointAttrToString(EndpointAttr endpointAttr)
494 : {
495 0 : switch (endpointAttr) {
496 0 : case ENDPOINT_ATTR_BW_COEFF:
497 0 : return "ENDPOINT_ATTR_BW_COEFF";
498 0 : case ENDPOINT_ATTR_DIE_ID:
499 0 : return "ENDPOINT_ATTR_DIE_ID";
500 0 : case ENDPOINT_ATTR_LOCATION:
501 0 : return "ENDPOINT_ATTR_LOCATION";
502 0 : default:
503 0 : return "ENDPOINT_ATTR_INVALID";
504 : }
505 : }
506 :
507 0 : HcclResult IRankGraph::GetEndpointInfo(
508 : uint32_t rankId, const EndpointDesc* endPointDesc, EndpointAttr endpointAttr, uint32_t infoLen, void* info)
509 : {
510 0 : CHK_PTR_NULL(rankGraphPtr_);
511 0 : RankGraph* rankGraph = static_cast<RankGraph*>(rankGraphPtr_);
512 0 : HcclResult ret = rankGraph->GetEndpointInfo(rankId, endPointDesc, endpointAttr, infoLen, info);
513 0 : CHK_PRT_RET(
514 : ret != HCCL_SUCCESS,
515 : HCCL_ERROR(
516 : "[IRankGraph::GetEndpointInfo] Failed to get endpoint info, rankId[%u], "
517 : "endpointAttr[%s], ret[%d]",
518 : rankId, EndpointAttrToString(endpointAttr), ret),
519 : ret);
520 0 : return HCCL_SUCCESS;
521 : }
522 :
523 : } // namespace Hccl
|