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_ahc_transport_req_base.h"
12 :
13 : namespace hccl {
14 0 : CalcAHCTransportReqBase::CalcAHCTransportReqBase(
15 : std::vector<std::vector<u32>>& subCommPlaneVector, std::vector<bool>& isBridgeVector, u32 userRank,
16 : std::vector<std::vector<std::vector<u32>>>& globalSubGroups, std::map<AHCConcOpType, TemplateType>& ahcAlgOption,
17 0 : std::unordered_map<u32, bool>& isUsedRdmaMap)
18 : : CalcTransportReqBase(subCommPlaneVector, isBridgeVector, userRank),
19 0 : globalSubGroups_(globalSubGroups),
20 0 : ahcAlgOption_(ahcAlgOption),
21 0 : isUsedRdmaMap_(isUsedRdmaMap)
22 0 : {}
23 :
24 0 : CalcAHCTransportReqBase::~CalcAHCTransportReqBase() {}
25 :
26 0 : HcclResult CalcAHCTransportReqBase::DisposeSubGroups(u32 rank)
27 : {
28 : (void)rank;
29 0 : return HCCL_SUCCESS;
30 : }
31 :
32 0 : HcclResult CalcAHCTransportReqBase::CalcDstRanks(u32 rank, std::set<u32>& dstRanks, u32 ringIndex)
33 : {
34 : (void)rank;
35 : (void)dstRanks;
36 : (void)ringIndex;
37 0 : return HCCL_SUCCESS;
38 : }
39 :
40 0 : HcclResult CalcAHCTransportReqBase::CommAHCInfoInit(std::vector<std::vector<u32>>& subGroups)
41 : {
42 : (void)subGroups;
43 0 : return HCCL_SUCCESS;
44 : }
45 :
46 0 : HcclResult CalcAHCTransportReqBase::CalcTransportRequest(
47 : const std::string& tag, TransportMemType inputMemType, TransportMemType outputMemType,
48 : const CommParaInfo& commParaInfo, std::vector<SingleSubCommTransport>& commTransport, u32 subUserRankRoot)
49 : {
50 : (void)subUserRankRoot;
51 0 : u32 ringSize = subCommPlaneVector_.size();
52 0 : commTransport.resize(ringSize);
53 0 : if (tag.find("AllReduce", 0) != std::string::npos) {
54 0 : opType_ = AHCOpType::AHC_OP_TYPE_ALLREDUCE;
55 0 : } else if (tag.find("ReduceScatter", 0) != std::string::npos) {
56 0 : opType_ = AHCOpType::AHC_OP_TYPE_REDUCE_SCATTER;
57 0 : } else if (tag.find("AllGather", 0) != std::string::npos) {
58 0 : opType_ = AHCOpType::AHC_OP_TYPE_ALLGATHER;
59 : }
60 :
61 0 : for (u32 ringIndex = 0; ringIndex < ringSize; ringIndex++) {
62 0 : if (commParaInfo.commPlane == COMM_LEVEL1_AHC && !isBridgeVector_[ringIndex]) {
63 0 : continue; // 跳出本次循环
64 : }
65 :
66 0 : u32 rank = GetSubCollectiveRank(subCommPlaneVector_[ringIndex]);
67 0 : if (rank == INVALID_VALUE_RANKID) {
68 0 : continue;
69 : }
70 :
71 0 : u32 rankSize = subCommPlaneVector_[ringIndex].size();
72 0 : SingleSubCommTransport& subCommTransport = commTransport[ringIndex];
73 0 : subCommTransport.transportRequests.resize(rankSize);
74 : // 只有一张卡时不需要建链
75 0 : if (rankSize == HCCL_RANK_SIZE_EQ_ONE) {
76 0 : HCCL_INFO("[CalcAHCTransportReqBase] comm base needn't to create links, rankSize_[%u].", rankSize);
77 0 : return HCCL_SUCCESS;
78 : }
79 :
80 0 : std::set<u32> dstRanks;
81 0 : CHK_RET(CalcDstRanks(rank, dstRanks, ringIndex));
82 :
83 : // 建链
84 0 : for (u32 dstRank : dstRanks) {
85 0 : CHK_PRT_RET(
86 : dstRank >= rankSize,
87 : HCCL_ERROR(
88 : "[CalcAHCTransportReqBase][CalcTransportRequest] dstRank [%u] exceed rankSize [%u] error", dstRank,
89 : rankSize),
90 : HCCL_E_INTERNAL);
91 :
92 0 : if (dstRank != rank) {
93 0 : TransportRequest& tmpTransport = subCommTransport.transportRequests[dstRank];
94 0 : tmpTransport.isValid = true;
95 0 : tmpTransport.localUserRank = userRank_;
96 0 : tmpTransport.remoteUserRank = subCommPlaneVector_[ringIndex][dstRank];
97 0 : tmpTransport.inputMemType = inputMemType;
98 0 : tmpTransport.outputMemType = outputMemType;
99 0 : HCCL_INFO(
100 : "[CalcAHCTransportReqBase] param_.tag[%s] ringIndex[%u], localRank[%u], "
101 : "remoteRank[%u], inputMemType[%d], outputMemType[%d]",
102 : tag.c_str(), ringIndex, userRank_, tmpTransport.remoteUserRank, inputMemType, outputMemType);
103 : }
104 : }
105 :
106 : // 刷新RDMA建链标记
107 0 : RefreshTransportIsUsedRdma(rank, ringIndex, commTransport);
108 0 : }
109 0 : return HCCL_SUCCESS;
110 : }
111 :
112 0 : void CalcAHCTransportReqBase::RefreshTransportIsUsedRdma(
113 : u32 rank, u32 ringIndex, std::vector<SingleSubCommTransport>& commTransport)
114 : {
115 : // 组内和组间通信域计算
116 0 : std::vector<u32> intraCommGroup;
117 0 : std::vector<std::vector<u32>> interCommGroupList;
118 :
119 0 : commAHCBaseInfo_->GetIntraCommGroup(rank, intraCommGroup);
120 0 : commAHCBaseInfo_->GetInterCommGroupList(rank, interCommGroupList);
121 :
122 0 : SingleSubCommTransport& subCommTransport = commTransport[ringIndex];
123 :
124 : // 组内子通信域粒度刷新
125 0 : bool isUsedRdma = false;
126 0 : for (u32 i = 0; i < intraCommGroup.size(); i++) {
127 0 : u32 dstRank = intraCommGroup[i];
128 0 : HCCL_DEBUG(
129 : "[CalcAHCTransportReqBase][RefreshTransportIsUsedRdma] intraCommGroup localRank[%u], dstRank [%u] ", rank,
130 : dstRank);
131 0 : if (isUsedRdmaMap_[subCommPlaneVector_[ringIndex][dstRank]]) {
132 0 : isUsedRdma = true;
133 0 : HCCL_DEBUG(
134 : "[CalcAHCTransportReqBase][RefreshTransportIsUsedRdma] intraCommGroup userrank[%u] rdma map is true",
135 : subCommPlaneVector_[ringIndex][dstRank]);
136 0 : break;
137 : }
138 : }
139 0 : for (u32 i = 0; i < intraCommGroup.size(); i++) {
140 0 : u32 dstRank = intraCommGroup[i];
141 0 : TransportRequest& tmpTransport = subCommTransport.transportRequests[dstRank];
142 0 : tmpTransport.isUsedRdma = isUsedRdma;
143 : }
144 :
145 : // 组间子通信域粒度刷新
146 0 : for (u32 i = 0; i < interCommGroupList.size(); i++) {
147 0 : isUsedRdma = false;
148 0 : for (u32 j = 0; j < interCommGroupList[i].size(); j++) {
149 0 : u32 dstRank = interCommGroupList[i][j];
150 0 : HCCL_DEBUG(
151 : "[CalcAHCTransportReqBase][RefreshTransportIsUsedRdma] interCommGroupList index[%u] localRank[%u], "
152 : "dstRank [%u] ",
153 : i, rank, dstRank);
154 0 : if (isUsedRdmaMap_[subCommPlaneVector_[ringIndex][dstRank]]) {
155 0 : isUsedRdma = true;
156 0 : HCCL_DEBUG(
157 : "[CalcAHCTransportReqBase][RefreshTransportIsUsedRdma] interCommGroupList userrank[%u] rdma map is "
158 : "true",
159 : subCommPlaneVector_[ringIndex][dstRank]);
160 0 : break;
161 : }
162 : }
163 0 : for (u32 j = 0; j < interCommGroupList[i].size(); j++) {
164 0 : u32 dstRank = interCommGroupList[i][j];
165 0 : TransportRequest& tmpTransport = subCommTransport.transportRequests[dstRank];
166 0 : tmpTransport.isUsedRdma = isUsedRdma;
167 : }
168 : }
169 0 : }
170 : } // namespace hccl
|