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_transport_req.h"
12 : #include "calc_ahc_template_register.h"
13 :
14 : namespace hccl {
15 15 : CalcNHRTransportReq::CalcNHRTransportReq(std::vector<std::vector<u32>> &subCommPlaneVector,
16 15 : std::vector<bool> &isBridgeVector, u32 userRank)
17 15 : : CalcTransportReqBase(subCommPlaneVector, isBridgeVector, userRank)
18 : {
19 15 : }
20 :
21 30 : CalcNHRTransportReq::~CalcNHRTransportReq()
22 : {
23 30 : }
24 :
25 15 : HcclResult CalcNHRTransportReq::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 15 : u32 ringSize = subCommPlaneVector_.size();
31 15 : commTransport.resize(ringSize);
32 :
33 29 : for (u32 ringIndex = 0; ringIndex < ringSize; ringIndex++) {
34 16 : if (commParaInfo.commPlane == COMM_LEVEL1 && !isBridgeVector_[ringIndex]) {
35 1 : continue; // 跳出本次循环
36 : }
37 :
38 15 : u32 rank = GetSubCollectiveRank(subCommPlaneVector_[ringIndex]);
39 15 : if (rank == INVALID_VALUE_RANKID) {
40 0 : continue;
41 : }
42 :
43 15 : u32 rankSize = subCommPlaneVector_[ringIndex].size();
44 15 : SingleSubCommTransport &subCommTransport = commTransport[ringIndex];
45 15 : subCommTransport.transportRequests.resize(rankSize);
46 : // 只有一张卡时不需要建链
47 15 : if (rankSize == HCCL_RANK_SIZE_EQ_ONE) {
48 2 : HCCL_INFO("comm base needn't to create links, rankSize_[%u].", rankSize);
49 2 : return HCCL_SUCCESS;
50 : }
51 :
52 45 : for (u32 delta = 1; delta < rankSize; delta <<= 1) {
53 32 : const u32 targetRankPos = static_cast<u32>(rank + delta) % rankSize;
54 32 : TransportRequest &tmpTransport = subCommTransport.transportRequests[targetRankPos];
55 32 : tmpTransport.isValid = true;
56 32 : tmpTransport.localUserRank = userRank_;
57 32 : tmpTransport.remoteUserRank = subCommPlaneVector_[ringIndex][targetRankPos];
58 32 : tmpTransport.inputMemType = inputMemType;
59 32 : tmpTransport.outputMemType = outputMemType;
60 32 : HCCL_INFO("[CommFactory][CalcNHRCommInfo] param_.tag[%s] ringIndex[%u], localRank[%u], "\
61 : "remoteRank[%u], inputMemType[%d], outputMemType[%d]", tag.c_str(), ringIndex, userRank_,
62 : tmpTransport.remoteUserRank, inputMemType, outputMemType);
63 :
64 32 : const u32 targetRankNeg = static_cast<u32>(rank + rankSize - delta) % rankSize;
65 32 : TransportRequest &tmpTransportNeg = subCommTransport.transportRequests[targetRankNeg];
66 32 : tmpTransportNeg.isValid = true;
67 32 : tmpTransportNeg.localUserRank = userRank_;
68 32 : tmpTransportNeg.remoteUserRank = subCommPlaneVector_[ringIndex][targetRankNeg];
69 32 : tmpTransportNeg.inputMemType = inputMemType;
70 32 : tmpTransportNeg.outputMemType = outputMemType;
71 32 : HCCL_INFO("[CommFactory][CalcNHRCommInfo] param_.tag[%s] ringIndex[%u], localRank[%u], "\
72 : "remoteRank[%u], inputMemType[%d], outputMemType[%d]", tag.c_str(), ringIndex, userRank_,
73 : tmpTransportNeg.remoteUserRank, inputMemType, outputMemType);
74 : }
75 13 : subCommTransport.enableUseOneDoorbell = true;
76 : }
77 13 : return HCCL_SUCCESS;
78 : }
79 :
80 0 : HcclResult CalcNHRTransportReq::CalcDstRanks(const u32 rank, const std::vector<u32> commGroups,
81 : std::set<u32> &dstRanks)
82 : {
83 0 : CHK_PRT_RET(rank >= commGroups.size(),
84 : HCCL_ERROR("[CalcNHRTransportReq][CalcDstRanks] rank [%u] exceed commGroups Size [%u] error",
85 : rank, commGroups.size() ), HCCL_E_INTERNAL);
86 :
87 0 : for (auto i = 0; static_cast<u32>(1 << i) < commGroups.size(); ++i) {
88 : // 正方向第2^i个节点的rank号
89 0 : const u32 targetRankPos = static_cast<u32>(rank + (1 << i)) % commGroups.size();
90 0 : dstRanks.insert(commGroups[targetRankPos]);
91 :
92 : // 反方向第2^i个节点的rank号
93 0 : const u32 targetRankNeg = static_cast<u32>(rank + commGroups.size() - (1 << i)) % commGroups.size();
94 :
95 0 : HCCL_DEBUG("[CalcNHRTransportReq][CalcDstRanks] local rank[%u], remote rank[%u]", commGroups[rank], commGroups[targetRankNeg]);
96 :
97 0 : dstRanks.insert(commGroups[targetRankNeg]);
98 : }
99 :
100 0 : return HCCL_SUCCESS;
101 : }
102 :
103 : REGISTER_AHC_COMM_CALC_FUNC(AHCTemplateType::AHC_TEMPLATE_NHR, CalcNHRTransportReq, CalcNHRTransportReq::CalcDstRanks);
104 : } // namespace hccl
|