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