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 "log.h"
12 :
13 : #include "alg_data_trans_wrapper.h"
14 : #include "ins_temp_reduce_aicpu_reduce_mesh_2D.h"
15 :
16 : namespace Hccl {
17 0 : InsTempReduceAicpuReduceMesh2D::InsTempReduceAicpuReduceMesh2D(
18 : const RankId virtualRank, const u32 tempRankSize, const std::vector<std::vector<RankId>>& tempVTopo,
19 0 : const std::map<RankId, u32>& tempVirtRankMap)
20 : : InsAlgTemplateBase(virtualRank, tempRankSize, tempVTopo, tempVirtRankMap),
21 0 : sizeX_(static_cast<u32>(tempVTopo[0].size())),
22 0 : sizeY_(static_cast<u32>(tempVTopo[1].size())),
23 0 : curX_(myRank_ / sizeX_),
24 0 : curY_(myRank_ % sizeX_)
25 0 : {}
26 :
27 0 : InsTempReduceAicpuReduceMesh2D::~InsTempReduceAicpuReduceMesh2D() {}
28 :
29 0 : HcclResult InsTempReduceAicpuReduceMesh2D::CalcRes(AlgTempResReq& tempResReq)
30 : {
31 0 : tempResReq.queNum = sizeX_ - 1 + sizeY_ - 1 > 0 ? sizeX_ - 1 + sizeY_ - 1 : 1;
32 0 : tempResReq.streamNum = tempResReq.queNum;
33 0 : tempResReq.queNotifys = CreateMasterSlaveQueNotifiesRequest(tempResReq.queNum);
34 0 : QId centerQ = 0;
35 0 : tempResReq.localWaitGroupCntNotify.emplace_back(centerQ, 0);
36 0 : tempResReq.localBcastPostCntNotify.emplace_back(centerQ, 0);
37 0 : CHK_RET(CalcResLinksMesh2D(myRank_, tempVTopo_, linkNumBtwPeers_, tempResReq));
38 0 : HCCL_INFO(
39 : "[InsTempReduceAicpuReduceMesh2D]CalcRes: queNum[%u], myRank[%d], tempRankSize[%u]", tempResReq.queNum, myRank_,
40 : tempRankSize_);
41 0 : return HcclResult::HCCL_SUCCESS;
42 : }
43 :
44 0 : u32 InsTempReduceAicpuReduceMesh2D::CalcScratchMultiple(BufferType inBuffType, BufferType outBuffType) const
45 : {
46 : (void)inBuffType;
47 : (void)outBuffType;
48 0 : return tempRankSize_;
49 : }
50 :
51 0 : HcclResult InsTempReduceAicpuReduceMesh2D::RunAicpuLocalReduce(
52 : const TemplateDataParams& templateDataParams, std::vector<InsQuePtr>& tempInsQues)
53 : {
54 0 : CHK_PRT_RET(
55 : tempInsQues.empty(), HCCL_ERROR("[InsTempReduceAicpuReduceMesh2D][RunAicpuLocalReduce] empty queue"),
56 : HcclResult::HCCL_E_INTERNAL);
57 0 : CHK_PTR_NULL(tempInsQues[0]);
58 0 : if (u32(myRank_) != root_) {
59 0 : return HCCL_SUCCESS;
60 : }
61 0 : DataSlice dataSlice = DataSlice(BufferType::SCRATCH, 0, templateDataParams.sliceSize);
62 0 : for (u32 rankId = 1; rankId < tempRankSize_; rankId++) {
63 : DataSlice addSlice
64 0 : = DataSlice(BufferType::SCRATCH, templateDataParams.sliceSize * rankId, templateDataParams.sliceSize);
65 0 : AicpuReduce(tempInsQues[0], addSlice, dataSlice, dataType_, redOp_);
66 : }
67 : DataSlice outputSlice
68 0 : = DataSlice(BufferType::OUTPUT, templateDataParams.buffInfo.inBuffBaseOff, templateDataParams.sliceSize);
69 0 : LocalCopy(tempInsQues[0], dataSlice, outputSlice);
70 0 : return HCCL_SUCCESS;
71 : }
72 :
73 0 : HcclResult InsTempReduceAicpuReduceMesh2D::RunGatherToRootX(
74 : const TemplateDataParams& templateDataParams, const ResLinks& tempLinks, std::vector<InsQuePtr>& tempInsQues)
75 : {
76 : // send from x-axis
77 0 : DataSlice srcX(BufferType::INPUT, 0, dataSizeX_);
78 0 : DataSlice dstX(BufferType::SCRATCH, templateDataParams.sliceSize * myRank_, dataSizeX_);
79 0 : if (curY_ == rootY_) {
80 0 : LocalCopy(tempInsQues[0], srcX, dstX);
81 0 : for (u32 y = 0; y < sizeX_ - 1; y++) {
82 0 : u32 calcY = (rootY_ + y + 1) % sizeX_;
83 0 : u32 peerRank = curX_ * sizeX_ + calcY;
84 0 : const LinkData& linkRecv = tempLinks.at(peerRank)[0];
85 0 : std::vector<DataSlice> recvSrc;
86 0 : std::vector<DataSlice> recvDst;
87 0 : recvSrc.emplace_back(BufferType::INPUT, 0, dataSizeX_);
88 0 : recvDst.emplace_back(BufferType::SCRATCH, templateDataParams.sliceSize * peerRank, dataSizeX_);
89 0 : SlicesList rxSlicesList(recvSrc, recvDst);
90 0 : DataInfo recvData(linkRecv, rxSlicesList);
91 0 : CHK_PRT_RET(
92 : Recv(recvData, tempInsQues[y], 0, true, DmaMode::PUT),
93 : HCCL_ERROR("[InsTempReduceAicpuReduceMesh2D] BatchRecv failed"), HcclResult::HCCL_E_INTERNAL);
94 0 : }
95 : } else {
96 0 : u32 peerRankX = curX_ * sizeX_ + rootY_;
97 0 : const LinkData& linkSendX = tempLinks.at(peerRankX)[0];
98 0 : std::vector<DataSlice> srcSlicesX = {srcX};
99 0 : std::vector<DataSlice> dstSlicesX = {dstX};
100 0 : SlicesList txSlicesListX(srcSlicesX, dstSlicesX);
101 0 : DataInfo sendDataX(linkSendX, txSlicesListX);
102 0 : CHK_PRT_RET(
103 : Send(sendDataX, tempInsQues[0], 0, true, DmaMode::PUT),
104 : HCCL_ERROR("[InsTempReduceAicpuReduceMesh2D] BatchSend failed"), HcclResult::HCCL_E_INTERNAL);
105 0 : }
106 0 : return HCCL_SUCCESS;
107 : }
108 :
109 0 : HcclResult InsTempReduceAicpuReduceMesh2D::RunGatherToRootY(
110 : const TemplateDataParams& templateDataParams, const ResLinks& tempLinks, std::vector<InsQuePtr>& tempInsQues)
111 : {
112 : // send from y-axis
113 0 : DataSlice srcY(BufferType::INPUT, rankOffsetY_, dataSizeY_);
114 0 : DataSlice dstY(BufferType::SCRATCH, templateDataParams.sliceSize * myRank_ + rankOffsetY_, dataSizeY_);
115 0 : if (curX_ == rootX_) {
116 0 : LocalCopy(tempInsQues[sizeX_ - 1], srcY, dstY);
117 0 : for (u32 x = 0; x < sizeY_ - 1; x++) {
118 0 : u32 calcX = (rootX_ + x + 1) % sizeY_;
119 0 : u32 peerRank = calcX * sizeX_ + curY_;
120 0 : const LinkData& linkRecv = tempLinks.at(peerRank)[0];
121 0 : std::vector<DataSlice> recvSrc;
122 0 : std::vector<DataSlice> recvDst;
123 0 : recvSrc.emplace_back(BufferType::INPUT, rankOffsetY_, dataSizeY_);
124 0 : recvDst.emplace_back(
125 0 : BufferType::SCRATCH, templateDataParams.sliceSize * peerRank + rankOffsetY_, dataSizeY_);
126 0 : SlicesList rxSlicesList(recvSrc, recvDst);
127 0 : DataInfo recvData(linkRecv, rxSlicesList);
128 0 : CHK_PRT_RET(
129 : Recv(recvData, tempInsQues[x + sizeX_ - 1], 0, true, DmaMode::PUT),
130 : HCCL_ERROR("[InsTempReduceAicpuReduceMesh2D] BatchRecv failed"), HcclResult::HCCL_E_INTERNAL);
131 0 : }
132 : } else {
133 0 : u32 peerRankY = rootX_ * sizeX_ + curY_;
134 0 : const LinkData& linkSendY = tempLinks.at(peerRankY)[0];
135 0 : std::vector<DataSlice> srcSlicesY = {srcY};
136 0 : std::vector<DataSlice> dstSlicesY = {dstY};
137 0 : SlicesList txSlicesListY(srcSlicesY, dstSlicesY);
138 0 : DataInfo sendDataY(linkSendY, txSlicesListY);
139 0 : CHK_PRT_RET(
140 : Send(sendDataY, tempInsQues[sizeX_ - 1], 0, true, DmaMode::PUT),
141 : HCCL_ERROR("[InsTempReduceAicpuReduceMesh2D] BatchSend failed"), HcclResult::HCCL_E_INTERNAL);
142 0 : }
143 0 : return HCCL_SUCCESS;
144 : }
145 :
146 0 : HcclResult InsTempReduceAicpuReduceMesh2D::RunGatherToRootXY(
147 : const TemplateDataParams& templateDataParams, const ResLinks& tempLinks, std::vector<InsQuePtr>& tempInsQues)
148 : {
149 : // Step 1
150 0 : CHK_RET(PreSyncInterQueues(tempInsQues));
151 0 : CHK_RET(RunGatherToRootX(templateDataParams, tempLinks, tempInsQues));
152 0 : CHK_RET(RunGatherToRootY(templateDataParams, tempLinks, tempInsQues));
153 0 : CHK_RET(PostSyncInterQueues(tempInsQues));
154 0 : return HCCL_SUCCESS;
155 : }
156 :
157 0 : HcclResult InsTempReduceAicpuReduceMesh2D::RunXGatherToRoot(
158 : const TemplateDataParams& templateDataParams, const ResLinks& tempLinks, std::vector<InsQuePtr>& tempInsQues) const
159 : {
160 0 : if (curX_ == rootX_ && curY_ == rootY_) {
161 : // recv data from x-axis
162 0 : for (u32 y = 0; y < sizeX_ - 1; y++) {
163 0 : u32 calcY = (rootY_ + y + 1) % sizeX_;
164 0 : u32 recvRank = rootX_ * sizeX_ + calcY;
165 0 : const LinkData& linkRecv = tempLinks.at(recvRank)[0];
166 : // calc recv data
167 0 : std::vector<DataSlice> srcDstRecvSlices;
168 0 : for (u32 x = 0; x < sizeY_ - 1; x++) {
169 0 : u32 calcX = (rootX_ + x + 1) % sizeY_;
170 0 : u32 peerRank = calcX * sizeX_ + calcY;
171 : DataSlice srcDstSlice(
172 0 : BufferType::SCRATCH, templateDataParams.sliceSize * peerRank + rankOffsetY_, dataSizeY_);
173 0 : srcDstRecvSlices.emplace_back(srcDstSlice);
174 : }
175 0 : SlicesList rxSlicesList(srcDstRecvSlices, srcDstRecvSlices);
176 0 : DataInfo recvData(linkRecv, rxSlicesList);
177 0 : CHK_PRT_RET(
178 : Recv(recvData, tempInsQues[y], 0, true, DmaMode::PUT),
179 : HCCL_ERROR("[InsTempReduceAicpuReduceMesh2D] BatchRecv failed"), HcclResult::HCCL_E_INTERNAL);
180 0 : }
181 0 : } else if (curX_ == rootX_) {
182 : // x-axis gather to root
183 0 : std::vector<DataSlice> srcDstSlicesX;
184 0 : const LinkData& linkSendXY = tempLinks.at(root_)[0];
185 0 : for (u32 x = 0; x < sizeY_; x++) {
186 0 : u32 peerRank = x * sizeX_ + curY_;
187 : DataSlice srcDstSlice(
188 0 : BufferType::SCRATCH, templateDataParams.sliceSize * peerRank + rankOffsetY_, dataSizeY_);
189 0 : srcDstSlicesX.emplace_back(srcDstSlice);
190 : }
191 0 : SlicesList txSlicesList(srcDstSlicesX, srcDstSlicesX);
192 0 : DataInfo sendDataX(linkSendXY, txSlicesList);
193 0 : CHK_PRT_RET(
194 : Send(sendDataX, tempInsQues[0], 0, true, DmaMode::PUT),
195 : HCCL_ERROR("[InsTempReduceAicpuReduceMesh2D] BatchSend failed"), HcclResult::HCCL_E_INTERNAL);
196 0 : }
197 0 : return HCCL_SUCCESS;
198 : }
199 :
200 0 : HcclResult InsTempReduceAicpuReduceMesh2D::RunYGatherToRoot(
201 : const TemplateDataParams& templateDataParams, const ResLinks& tempLinks, std::vector<InsQuePtr>& tempInsQues) const
202 : {
203 0 : if (curX_ == rootX_ && curY_ == rootY_) {
204 : // recv data from y-axis
205 0 : for (u32 x = 0; x < sizeY_ - 1; x++) {
206 0 : u32 calcX = (rootX_ + x + 1) % sizeY_;
207 0 : u32 recvRank = calcX * sizeX_ + rootY_;
208 0 : const LinkData& linkRecv = tempLinks.at(recvRank)[0];
209 : // calc recv data
210 0 : std::vector<DataSlice> srcDstRecvSlices;
211 0 : for (u32 y = 0; y < sizeX_ - 1; y++) {
212 0 : u32 calcY = (rootY_ + y + 1) % sizeX_;
213 0 : u32 peerRank = calcX * sizeX_ + calcY;
214 0 : DataSlice srcDstSlice(BufferType::SCRATCH, templateDataParams.sliceSize * peerRank, dataSizeX_);
215 0 : srcDstRecvSlices.emplace_back(srcDstSlice);
216 : }
217 0 : SlicesList rxSlicesList(srcDstRecvSlices, srcDstRecvSlices);
218 0 : DataInfo recvData(linkRecv, rxSlicesList);
219 0 : CHK_PRT_RET(
220 : Recv(recvData, tempInsQues[sizeX_ - 1 + x], 0, true, DmaMode::PUT),
221 : HCCL_ERROR("[InsTempReduceAicpuReduceMesh2D] BatchRecv failed"), HcclResult::HCCL_E_INTERNAL);
222 0 : }
223 0 : } else if (curY_ == rootY_) {
224 : // y-axis gather to root
225 0 : std::vector<DataSlice> srcDstSlicesY;
226 0 : const LinkData& linkSendXY = tempLinks.at(root_)[0];
227 0 : for (u32 y = 0; y < sizeX_; y++) {
228 0 : u32 peerRank = curX_ * sizeX_ + y;
229 0 : DataSlice srcDstSlice(BufferType::SCRATCH, templateDataParams.sliceSize * peerRank, dataSizeX_);
230 0 : srcDstSlicesY.emplace_back(srcDstSlice);
231 : }
232 0 : SlicesList txSlicesList(srcDstSlicesY, srcDstSlicesY);
233 0 : DataInfo sendDataY(linkSendXY, txSlicesList);
234 0 : CHK_PRT_RET(
235 : Send(sendDataY, tempInsQues[sizeX_ - 1], 0, true, DmaMode::PUT),
236 : HCCL_ERROR("[InsTempReduceAicpuReduceMesh2D] BatchSend failed"), HcclResult::HCCL_E_INTERNAL);
237 0 : }
238 0 : return HCCL_SUCCESS;
239 : }
240 :
241 0 : HcclResult InsTempReduceAicpuReduceMesh2D::RunXYGatherToRoot(
242 : const TemplateDataParams& templateDataParams, const ResLinks& tempLinks, std::vector<InsQuePtr>& tempInsQues) const
243 : {
244 : // Step 2
245 0 : CHK_RET(PreSyncInterQueues(tempInsQues));
246 0 : CHK_RET(RunXGatherToRoot(templateDataParams, tempLinks, tempInsQues));
247 0 : CHK_RET(RunYGatherToRoot(templateDataParams, tempLinks, tempInsQues));
248 0 : CHK_RET(PostSyncInterQueues(tempInsQues));
249 0 : return HCCL_SUCCESS;
250 : }
251 :
252 0 : HcclResult InsTempReduceAicpuReduceMesh2D::GenExtIns(
253 : const TempFuncs& tempFuncs, const TemplateDataParams& templateDataParams, const ResLinks& tempLinks,
254 : std::vector<InsQuePtr>& tempInsQues)
255 : {
256 0 : HCCL_INFO("[InsTempReduceAicpuReduceMesh2D] Run start");
257 0 : if (tempVTopo_[0].size() == 1) {
258 0 : return HcclResult::HCCL_SUCCESS;
259 : }
260 0 : opMode_ = tempFuncs.opMode;
261 0 : queNum_ = tempVTopo_[0].size() - 1 + tempVTopo_[1].size() - 1;
262 0 : CHK_PRT_RET(
263 : queNum_ > tempInsQues.size(),
264 : HCCL_ERROR("[CollAlgFactory] [InsTempReduceAicpuReduceMesh2D] Rank [%d], requiredQue Error.", myRank_),
265 : HcclResult::HCCL_E_INTERNAL);
266 0 : dataTypeSize_ = DataTypeSizeGet(dataType_);
267 0 : rootX_ = root_ / sizeX_;
268 0 : rootY_ = root_ % sizeX_;
269 0 : const int splitDataXYFactor = 2;
270 0 : dataSizeX_ = templateDataParams.sliceSize / dataTypeSize_ / splitDataXYFactor * dataTypeSize_;
271 0 : rankOffsetY_ = dataSizeX_;
272 0 : dataSizeY_ = templateDataParams.sliceSize - dataSizeX_;
273 0 : RunGatherToRootXY(templateDataParams, tempLinks, tempInsQues);
274 0 : RunXYGatherToRoot(templateDataParams, tempLinks, tempInsQues);
275 0 : StreamSync(tempInsQues);
276 0 : RunAicpuLocalReduce(templateDataParams, tempInsQues);
277 0 : HCCL_INFO("[InsTempReduceAicpuReduceMesh2D] Run finished");
278 0 : return HCCL_SUCCESS;
279 : }
280 :
281 : } // namespace Hccl
|