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