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 "calc_nhr_v1_transport_req.h"
12 : #include "nonuniform_hierarchical_ring_v1_base_pub.h"
13 :
14 : namespace hccl {
15 0 : CalcNHRV1TransportReq::CalcNHRV1TransportReq(std::vector<std::vector<u32>> &subCommPlaneVector,
16 0 : std::vector<bool> &isBridgeVector, u32 userRank)
17 0 : : CalcTransportReqBase(subCommPlaneVector, isBridgeVector, userRank)
18 : {
19 0 : }
20 :
21 0 : CalcNHRV1TransportReq::~CalcNHRV1TransportReq()
22 : {
23 0 : }
24 :
25 0 : HcclResult CalcNHRV1TransportReq::CalcTransportRequest(const std::string &tag, TransportMemType inputMemType,
26 : TransportMemType outputMemType, const CommParaInfo &commParaInfo,
27 : std::vector<SingleSubCommTransport> &commTransport, u32 subUserRankRoot)
28 : {
29 : (void)subUserRankRoot;
30 0 : u32 ringSize = subCommPlaneVector_.size();
31 0 : commTransport.resize(ringSize);
32 :
33 0 : for (u32 ringIndex = 0; ringIndex < ringSize; ringIndex++) {
34 0 : if (commParaInfo.commPlane == COMM_LEVEL1 && !isBridgeVector_[ringIndex]) {
35 0 : continue; // 跳出本次循环
36 : }
37 :
38 0 : u32 rank = GetSubCollectiveRank(subCommPlaneVector_[ringIndex]);
39 0 : if (rank == INVALID_VALUE_RANKID) {
40 0 : continue;
41 : }
42 :
43 0 : u32 rankSize = subCommPlaneVector_[ringIndex].size();
44 0 : SingleSubCommTransport &subCommTransport = commTransport[ringIndex];
45 0 : subCommTransport.transportRequests.resize(rankSize);
46 : // 只有一张卡时不需要建链
47 0 : if (rankSize == HCCL_RANK_SIZE_EQ_ONE) {
48 0 : HCCL_INFO("comm base needn't to create links, rankSize_[%u].", rankSize);
49 0 : return HCCL_SUCCESS;
50 : }
51 :
52 0 : RingInfo info = NHRV1Base::GetRingInfo(subCommPlaneVector_[ringIndex].size());
53 :
54 0 : u32 hIndex = info.GetHIndex(rank);
55 0 : u32 vIndex = info.GetVIndex(rank);
56 0 : u32 hSize = info.GetHSizeByVIndex(vIndex);
57 0 : u32 vSize = info.GetVSizeByHIndex(hIndex);
58 :
59 : // 收集邻居
60 0 : std::set<u32> links;
61 : // -- 水平方向:左邻居
62 0 : links.insert(info.GetRank(vIndex, (hIndex + hSize - 1) % hSize));
63 : // -- 水平方向:右邻居
64 0 : links.insert(info.GetRank(vIndex, (hIndex + 1) % hSize));
65 : // -- 垂直方向:上邻居
66 0 : links.insert(info.GetRank((vIndex + vSize - 1) % vSize, hIndex));
67 : // -- 垂直方向:下邻居
68 0 : links.insert(info.GetRank((vIndex + 1) % vSize, hIndex));
69 :
70 0 : u32 rankOffset = info.GetRankOffset();
71 0 : if (rankOffset != rankSize) {
72 : // -- 水平方向:建立(x,0)和(x, sqrt-1)之间的链接
73 0 : if (hIndex == info.GetSqrtRankSize() - 1) {
74 0 : links.insert(info.GetRank(vIndex, 0));
75 0 : } else if (hIndex == 0) {
76 0 : links.insert(info.GetRank(vIndex, info.GetSqrtRankSize() - 1));
77 : }
78 : // -- 垂直方向:建立(0,y)和(y,sqrt)之间、(sqrt-1,y)和(y,sqrt)之间的链接
79 0 : if (hIndex < info.GetSqrtRankSize()) {
80 0 : if (info.GetHSizeByVIndex(hIndex) > info.GetSqrtRankSize() \
81 0 : && (vIndex == info.GetVSizeByHIndex(hIndex) - 1 || vIndex == 0)) {
82 0 : links.insert(info.GetRank(hIndex, info.GetSqrtRankSize()));
83 : }
84 : } else {
85 0 : links.insert(info.GetRank(0, vIndex));
86 0 : links.insert(info.GetRank(info.GetVSizeByHIndex(vIndex) - 1, vIndex));
87 : }
88 : }
89 0 : for (u32 dstRank : links) {
90 0 : if (dstRank != rank) {
91 0 : TransportRequest &tmpTransport = subCommTransport.transportRequests[dstRank];
92 0 : tmpTransport.isValid = true;
93 0 : tmpTransport.localUserRank = userRank_;
94 0 : tmpTransport.remoteUserRank = subCommPlaneVector_[ringIndex][dstRank];
95 0 : tmpTransport.inputMemType = inputMemType;
96 0 : tmpTransport.outputMemType = outputMemType;
97 0 : HCCL_INFO("[CommFactory][CalcNHRV1CommInfo] param_.tag[%s] ringIndex[%u], localRank[%u], \
98 : remoteRank[%u], inputMemType[%d], outputMemType[%d]", tag.c_str(), ringIndex, userRank_,
99 : tmpTransport.remoteUserRank, inputMemType, outputMemType);
100 : }
101 : }
102 0 : }
103 0 : return HCCL_SUCCESS;
104 : }
105 :
106 : } // namespace hccl
|