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_nb_transport_req.h"
12 : #include "calc_ahc_template_register.h"
13 :
14 : namespace hccl {
15 0 : CalcNBTransportReq::CalcNBTransportReq(
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 : CalcNBTransportReq::~CalcNBTransportReq() {}
21 :
22 0 : HcclResult CalcNBTransportReq::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 : for (u32 delta = 1; delta < rankSize; delta <<= 1) {
50 0 : const u32 targetRankPos = static_cast<u32>(rank + delta) % rankSize;
51 0 : TransportRequest& tmpTransport = subCommTransport.transportRequests[targetRankPos];
52 0 : tmpTransport.isValid = true;
53 0 : tmpTransport.localUserRank = userRank_;
54 0 : tmpTransport.remoteUserRank = subCommPlaneVector_[ringIndex][targetRankPos];
55 0 : tmpTransport.inputMemType = inputMemType;
56 0 : tmpTransport.outputMemType = outputMemType;
57 0 : HCCL_INFO(
58 : "[CalcNBTransportReq][CalcTransportRequest] param_.tag[%s], ringIndex[%u], localRank[%u], "
59 : "remoteRank[%u], inputMemType[%d], outputMemType[%d]",
60 : tag.c_str(), ringIndex, userRank_, tmpTransport.remoteUserRank, inputMemType, outputMemType);
61 :
62 0 : const u32 targetRankNeg = static_cast<u32>(rank + rankSize - delta) % rankSize;
63 0 : TransportRequest& tmpTransportNeg = subCommTransport.transportRequests[targetRankNeg];
64 0 : tmpTransportNeg.isValid = true;
65 0 : tmpTransportNeg.localUserRank = userRank_;
66 0 : tmpTransportNeg.remoteUserRank = subCommPlaneVector_[ringIndex][targetRankNeg];
67 0 : tmpTransportNeg.inputMemType = inputMemType;
68 0 : tmpTransportNeg.outputMemType = outputMemType;
69 0 : HCCL_INFO(
70 : "[CalcNBTransportReq][CalcTransportRequest] param_.tag[%s], ringIndex[%u], localRank[%u], "
71 : "remoteRank[%u], inputMemType[%d], outputMemType[%d]",
72 : tag.c_str(), ringIndex, userRank_, tmpTransportNeg.remoteUserRank, inputMemType, outputMemType);
73 : }
74 0 : subCommTransport.enableUseOneDoorbell = true;
75 : }
76 0 : return HCCL_SUCCESS;
77 : }
78 :
79 0 : HcclResult CalcNBTransportReq::CalcDstRanks(const u32 rank, const std::vector<u32> commGroups, std::set<u32>& dstRanks)
80 : {
81 0 : CHK_PRT_RET(
82 : rank >= commGroups.size(),
83 : HCCL_ERROR(
84 : "[CalcNBTransportReq][CalcDstRanks] rank [%u] exceed commGroups Size [%u] error", rank, commGroups.size()),
85 : 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(
96 : "[CalcNBTransportReq][CalcDstRanks] local rank[%u], remote rank[%u]", commGroups[rank],
97 : commGroups[targetRankNeg]);
98 :
99 0 : dstRanks.insert(commGroups[targetRankNeg]);
100 : }
101 :
102 0 : return HCCL_SUCCESS;
103 : }
104 :
105 : REGISTER_AHC_COMM_CALC_FUNC(AHCTemplateType::AHC_TEMPLATE_NB, CalcNBTransportReq, CalcNBTransportReq::CalcDstRanks);
106 : } // namespace hccl
|