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 "ins_all_gather_parallel_executor.h"
12 :
13 : #include <cmath>
14 :
15 : #include "log.h"
16 :
17 : #include "ins_coll_alg_registry.h"
18 :
19 : #include "topo_match_mesh_nhr.h"
20 : #include "topo_match_concurr_mesh_nhr.h"
21 : #include "topo_match_mesh_nhr_pcie.h"
22 :
23 : #include "alg_data_trans_wrapper.h"
24 :
25 : #include "ins_temp_all_gather_mesh.h"
26 : #include "ins_temp_all_gather_mesh_2D.h"
27 : #include "ins_temp_all_gather_nhr.h"
28 :
29 : #ifndef CCL_KERNEL_AICPU
30 : #include "ccu_temp_all_gather_nhr_1D_mem2mem.h"
31 : #include "ccu_temp_all_gather_mesh_1D_mem2mem_with_stride.h"
32 : #endif
33 :
34 : namespace Hccl {
35 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
36 0 : InsAllGatherParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::InsAllGatherParallelExecutor()
37 0 : : InsCollAlgBase()
38 0 : {}
39 :
40 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
41 0 : InsAllGatherParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::~InsAllGatherParallelExecutor()
42 0 : {}
43 :
44 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
45 0 : HcclResult InsAllGatherParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::CalcResOffload(
46 : const RankGraph* rankGraph, const u64& dataSize, CollOffloadOpResReq& resReq)
47 : {
48 : (void)dataSize;
49 :
50 0 : u64 scratchMemSize = 200 * 1024 * 1024;
51 0 : resReq.requiredScratchMemSize = scratchMemSize; // 200MB
52 :
53 : // Topo Match
54 0 : AlgTopoMatch topoMatch(myRank_, rankSize_, rankGraph, devType_);
55 0 : CHK_RET(topoMatch.MatchTopo(vTopo_, virtRanks_, virtRankMap_));
56 :
57 0 : rankSizeLevel0_ = virtRanks_[0].size();
58 0 : rankSizeLevel1_ = virtRanks_[1].size();
59 0 : InsAlgTemplate0 intraTempAlg(myRank_, rankSizeLevel0_, vTopo_[0], virtRankMap_[0]);
60 0 : InsAlgTemplate1 interTempAlg(myRank_, rankSizeLevel1_, vTopo_[1], virtRankMap_[1]);
61 0 : std::vector<map<u32, u32>> rank2PathNumMap;
62 0 : HCCL_INFO("[InsAllGatherParallelExecutor] CalcResOffload SetPathNumMap");
63 0 : CHK_RET(SetPathNumMapByRankGraphMultiLevel(rankGraph, virtRanks_, myRank_, rank2PathNumMap));
64 0 : intraTempAlg.setPathNumMap(rank2PathNumMap[0]);
65 0 : interTempAlg.setPathNumMap(rank2PathNumMap[1]);
66 :
67 : // calculate required insQues and prepare queue
68 0 : AlgTempResReq resReqIntra;
69 0 : AlgTempResReq resReqInter;
70 0 : if (enableDetour_) {
71 0 : HCCL_DEBUG("[%s] Rank[%d], CalcResOffload with detouring enabled.", __func__, myRank_);
72 0 : CHK_RET(intraTempAlg.CalcResDetour(rankGraph, resReqIntra));
73 : } else {
74 0 : HCCL_DEBUG(
75 : "[%s] Rank[%d], CalcResOffload with detouring disabled. rankSizeLevel0[%u] rankSizeLevel1[%u]", __func__,
76 : myRank_, rankSizeLevel0_, rankSizeLevel1_);
77 0 : CHK_RET(intraTempAlg.CalcRes(resReqIntra));
78 : }
79 :
80 0 : CHK_RET(interTempAlg.CalcRes(resReqInter));
81 :
82 0 : resReq.requiredSubQueNum = resReqIntra.streamNum + resReqInter.streamNum - 1;
83 :
84 0 : return HcclResult::HCCL_SUCCESS;
85 0 : }
86 :
87 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
88 0 : HcclResult InsAllGatherParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::CalcRes(
89 : const RankGraph* rankGraph, CollAlgResReq& algResReq)
90 : {
91 : // Topo Match
92 0 : AlgTopoMatch topoMatch(myRank_, rankSize_, rankGraph, devType_);
93 0 : CHK_RET(topoMatch.MatchTopo(vTopo_, virtRanks_, virtRankMap_));
94 :
95 0 : algResReq.topoInfo.UpdateMultiLevelTopo(virtRanks_, virtRankMap_, vTopo_);
96 :
97 0 : rankSizeLevel0_ = virtRanks_[0].size();
98 0 : rankSizeLevel1_ = virtRanks_[1].size();
99 :
100 : // instantiate a template
101 0 : InsAlgTemplate0 intraTempAlg(myRank_, rankSizeLevel0_, vTopo_[0], virtRankMap_[0]);
102 0 : InsAlgTemplate1 interTempAlg(myRank_, rankSizeLevel1_, vTopo_[1], virtRankMap_[1]);
103 0 : std::vector<map<u32, u32>> rank2PathNumMap;
104 0 : HCCL_INFO("[InsAllGatherParallelExecutor] CalcRes SetPathNumMap");
105 0 : CHK_RET(SetPathNumMapByRankGraphMultiLevel(rankGraph, virtRanks_, myRank_, rank2PathNumMap));
106 0 : intraTempAlg.setPathNumMap(rank2PathNumMap[0]);
107 0 : interTempAlg.setPathNumMap(rank2PathNumMap[1]);
108 :
109 : // calculate required insQues and prepare queue
110 0 : AlgTempResReq resReqIntra;
111 0 : AlgTempResReq resReqInter;
112 0 : if (enableDetour_) {
113 0 : HCCL_DEBUG("[InsAllGatherParallelExecutor] Rank[%d], CalcRes with detouring enabled.", myRank_);
114 0 : CHK_RET(intraTempAlg.CalcResDetour(rankGraph, resReqIntra));
115 : } else {
116 0 : HCCL_DEBUG(
117 : "[InsAllGatherParallelExecutor] Rank[%d], CalcRes with detouring disabled."
118 : "rankSizeLevel0[%u] rankSizeLevel1[%u]",
119 : myRank_, rankSizeLevel0_, rankSizeLevel1_);
120 0 : CHK_RET(intraTempAlg.CalcRes(resReqIntra));
121 : }
122 0 : CHK_RET(interTempAlg.CalcRes(resReqInter));
123 :
124 0 : CHK_RET(CalcLinkInfo(myRank_, rankGraph, resReqIntra.links, algResReq.levelRankPairs));
125 0 : CHK_RET(CalcLinkInfo(myRank_, rankGraph, resReqInter.links, algResReq.levelRankPairs));
126 0 : algResReq.primQueueNum = resReqIntra.streamNum + resReqInter.streamNum;
127 0 : CHK_RET(CalcParallelNotifyReq(algResReq.primQueueNum, resReqIntra.queNum, algResReq.queueNotifys));
128 0 : HCCL_DEBUG("[InsAllGatherParallelExecutor] algResReq.primQueueNum %u", algResReq.primQueueNum);
129 0 : CHK_RET(CalcResLinks(myRank_, rankGraph, linkPriority_, resReqIntra.links, algResReq.links));
130 0 : CHK_RET(CalcResLinks(myRank_, rankGraph, linkPriority_, resReqInter.links, algResReq.links));
131 :
132 0 : return HcclResult::HCCL_SUCCESS;
133 0 : }
134 :
135 : // HOST 侧算法入口,将对应的 instruction 添加到指令队列中
136 : // 传入的insQue为一条主流
137 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
138 0 : void InsAllGatherParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::GenTemplateAlgParamsIntra0(
139 : const u64 dataOffset, const u64 dataCountPerLoopAixs0, const u64 scratchOffset,
140 : TemplateDataParams& tempAlgParamsIntra0) const
141 : {
142 0 : tempAlgParamsIntra0.buffInfo.inBuffType = BufferType::INPUT;
143 0 : tempAlgParamsIntra0.buffInfo.outBuffType = BufferType::OUTPUT;
144 0 : tempAlgParamsIntra0.buffInfo.scratBuffType = BufferType::SCRATCH;
145 0 : tempAlgParamsIntra0.buffInfo.inBuffBaseOff = dataOffset;
146 0 : tempAlgParamsIntra0.buffInfo.outBuffBaseOff = rankIdxLevel1_ * rankSizeLevel0_ * dataSize_ + dataOffset;
147 0 : tempAlgParamsIntra0.buffInfo.scratchBuffBaseOff = scratchOffset;
148 0 : tempAlgParamsIntra0.sliceSize = dataCountPerLoopAixs0 * dataTypeSize_;
149 0 : tempAlgParamsIntra0.tailSize = dataCountPerLoopAixs0 * dataTypeSize_;
150 0 : tempAlgParamsIntra0.inputSliceStride = 0;
151 0 : tempAlgParamsIntra0.outputSliceStride = dataSize_;
152 0 : tempAlgParamsIntra0.repeatNum = 1;
153 0 : tempAlgParamsIntra0.inputRepeatStride = 0;
154 0 : tempAlgParamsIntra0.outputRepeatStride = 0;
155 :
156 0 : HCCL_DEBUG(
157 : "[InsAllGatherParallelExecutor][GenTemplateAlgParamsIntra0] rank[%d] inBuffBaseOff[%llu] "
158 : "outBuffBaseOff[%llu] scratchBuffBaseOff[%llu] sliceSize[%llu] outputSliceStride[%llu] rankSizeLevel0[%u] "
159 : "rankSizeLevel1[%u] rankIdxLevel0[%u] rankIdxLevel1[%u]",
160 : myRank_, tempAlgParamsIntra0.buffInfo.inBuffBaseOff, tempAlgParamsIntra0.buffInfo.outBuffBaseOff,
161 : tempAlgParamsIntra0.buffInfo.scratchBuffBaseOff, tempAlgParamsIntra0.sliceSize,
162 : tempAlgParamsIntra0.outputSliceStride, rankSizeLevel0_, rankSizeLevel1_, rankIdxLevel0_, rankIdxLevel1_);
163 0 : return;
164 : }
165 :
166 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
167 0 : void InsAllGatherParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::GenTemplateAlgParamsInter0(
168 : const u64 dataOffset, const u64 dataCountPerLoopAixs0, const u64 scratchOffset,
169 : TemplateDataParams& tempAlgParamsInter0) const
170 : {
171 0 : tempAlgParamsInter0.buffInfo.inBuffType = BufferType::OUTPUT;
172 0 : tempAlgParamsInter0.buffInfo.outBuffType = BufferType::OUTPUT;
173 0 : tempAlgParamsInter0.buffInfo.scratBuffType = BufferType::SCRATCH;
174 0 : tempAlgParamsInter0.buffInfo.inBuffBaseOff = dataOffset;
175 0 : tempAlgParamsInter0.buffInfo.outBuffBaseOff = dataOffset;
176 0 : tempAlgParamsInter0.buffInfo.scratchBuffBaseOff = scratchOffset;
177 0 : tempAlgParamsInter0.sliceSize = dataCountPerLoopAixs0 * dataTypeSize_;
178 0 : tempAlgParamsInter0.tailSize = dataCountPerLoopAixs0 * dataTypeSize_;
179 0 : tempAlgParamsInter0.inputSliceStride = dataSize_ * rankSizeLevel0_;
180 0 : tempAlgParamsInter0.outputSliceStride = dataSize_ * rankSizeLevel0_;
181 0 : tempAlgParamsInter0.repeatNum = rankSizeLevel0_;
182 0 : tempAlgParamsInter0.inputRepeatStride = dataSize_;
183 0 : tempAlgParamsInter0.outputRepeatStride = dataSize_;
184 0 : HCCL_DEBUG(
185 : "[InsAllGatherParallelExecutor][GenTemplateAlgParamsInter0] rank[%d] inBuffBaseOff[%llu] "
186 : "outBuffBaseOff[%llu] scratchBuffBaseOff[%llu] sliceSize[%llu] outputSliceStride[%llu] "
187 : "outputRepeatStride[%llu]",
188 : myRank_, tempAlgParamsInter0.buffInfo.inBuffBaseOff, tempAlgParamsInter0.buffInfo.outBuffBaseOff,
189 : tempAlgParamsInter0.buffInfo.scratchBuffBaseOff, tempAlgParamsInter0.sliceSize,
190 : tempAlgParamsInter0.outputSliceStride, tempAlgParamsInter0.outputRepeatStride);
191 0 : return;
192 : }
193 :
194 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
195 0 : void InsAllGatherParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::GenTemplateAlgParamsInter1(
196 : const u64 dataOffset, const u64 dataCountPerLoopAixs1, const u64 scratchOffset,
197 : TemplateDataParams& tempAlgParamsInter1) const
198 : {
199 0 : tempAlgParamsInter1.buffInfo.inBuffType = BufferType::INPUT;
200 0 : tempAlgParamsInter1.buffInfo.outBuffType = BufferType::OUTPUT;
201 0 : tempAlgParamsInter1.buffInfo.scratBuffType = BufferType::SCRATCH;
202 0 : tempAlgParamsInter1.buffInfo.inBuffBaseOff = dataOffset;
203 0 : tempAlgParamsInter1.buffInfo.outBuffBaseOff = rankIdxLevel0_ * dataSize_ + dataOffset; // for example 0 2 4 | 1 3 5
204 0 : tempAlgParamsInter1.buffInfo.scratchBuffBaseOff = scratchOffset;
205 0 : tempAlgParamsInter1.sliceSize = dataCountPerLoopAixs1 * dataTypeSize_;
206 0 : tempAlgParamsInter1.tailSize = dataCountPerLoopAixs1 * dataTypeSize_;
207 0 : tempAlgParamsInter1.inputSliceStride = 0;
208 0 : tempAlgParamsInter1.outputSliceStride = dataSize_ * rankSizeLevel0_;
209 0 : tempAlgParamsInter1.repeatNum = 1;
210 0 : tempAlgParamsInter1.inputRepeatStride = 0;
211 0 : tempAlgParamsInter1.outputRepeatStride = 0;
212 0 : HCCL_DEBUG(
213 : "[InsAllGatherParallelExecutor][GenTemplateAlgParamsInter1] rank[%d] inBuffBaseOff[%llu] "
214 : "outBuffBaseOff[%llu] scratchBuffBaseOff[%llu] sliceSize[%llu] outputSliceStride[%llu]",
215 : myRank_, tempAlgParamsInter1.buffInfo.inBuffBaseOff, tempAlgParamsInter1.buffInfo.outBuffBaseOff,
216 : tempAlgParamsInter1.buffInfo.scratchBuffBaseOff, tempAlgParamsInter1.sliceSize,
217 : tempAlgParamsInter1.outputSliceStride);
218 0 : return;
219 : }
220 :
221 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
222 0 : void InsAllGatherParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::GenTemplateAlgParamsIntra1(
223 : const u64 dataOffset, const u64 dataCountPerLoopAixs1, const u64 scratchOffset,
224 : TemplateDataParams& tempAlgParamsIntra1) const
225 : {
226 0 : tempAlgParamsIntra1.buffInfo.inBuffType = BufferType::OUTPUT;
227 0 : tempAlgParamsIntra1.buffInfo.outBuffType = BufferType::OUTPUT;
228 0 : tempAlgParamsIntra1.buffInfo.scratBuffType = BufferType::SCRATCH;
229 0 : tempAlgParamsIntra1.buffInfo.inBuffBaseOff = dataOffset;
230 0 : tempAlgParamsIntra1.buffInfo.outBuffBaseOff = dataOffset;
231 0 : tempAlgParamsIntra1.buffInfo.scratchBuffBaseOff = scratchOffset;
232 0 : tempAlgParamsIntra1.sliceSize = dataCountPerLoopAixs1 * dataTypeSize_;
233 0 : tempAlgParamsIntra1.tailSize = dataCountPerLoopAixs1 * dataTypeSize_;
234 0 : tempAlgParamsIntra1.inputSliceStride = dataSize_;
235 0 : tempAlgParamsIntra1.outputSliceStride = dataSize_;
236 0 : tempAlgParamsIntra1.repeatNum = rankSizeLevel1_;
237 0 : tempAlgParamsIntra1.inputRepeatStride = dataSize_ * rankSizeLevel0_;
238 0 : tempAlgParamsIntra1.outputRepeatStride = dataSize_ * rankSizeLevel0_;
239 0 : HCCL_DEBUG(
240 : "[InsAllGatherParallelExecutor][GenTemplateAlgParamsIntra1] rank[%d] inBuffBaseOff[%llu] "
241 : "outBuffBaseOff[%llu] scratchBuffBaseOff[%llu] sliceSize[%llu] outputSliceStride[%llu] "
242 : "outputRepeatStride[%llu]",
243 : myRank_, tempAlgParamsIntra1.buffInfo.inBuffBaseOff, tempAlgParamsIntra1.buffInfo.outBuffBaseOff,
244 : tempAlgParamsIntra1.buffInfo.scratchBuffBaseOff, tempAlgParamsIntra1.sliceSize,
245 : tempAlgParamsIntra1.outputSliceStride, tempAlgParamsIntra1.outputRepeatStride);
246 0 : return;
247 : }
248 :
249 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
250 0 : void InsAllGatherParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::GetParallelDataSplit(
251 : std::vector<float>& splitDataSize) const
252 : {
253 : // to do 先做等分,后续根据性能做调整
254 0 : double splitData = 0.5;
255 0 : splitDataSize.push_back(splitData);
256 0 : splitDataSize.push_back(splitData);
257 0 : return;
258 : }
259 :
260 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
261 0 : HcclResult InsAllGatherParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::PrepareResForTemplate(
262 : const RankGraph* rankGraph, InsAlgTemplate0& tempAlgIntra, InsAlgTemplate1& tempAlgInter)
263 : {
264 0 : AlgTempResReq resReqInter;
265 0 : AlgTempResReq resReqIntra;
266 0 : if (enableDetour_) {
267 0 : HCCL_DEBUG("[%s] Rank[%d], CalcRes with detour enabled.", __func__, myRank_);
268 0 : CHK_RET(tempAlgIntra.CalcResDetour(rankGraph, resReqIntra));
269 : } else {
270 0 : CHK_RET(tempAlgIntra.CalcRes(resReqIntra));
271 : }
272 0 : CHK_RET(tempAlgInter.CalcRes(resReqInter));
273 :
274 : // 申请算法模板所需资源
275 0 : if (!(resReqIntra.queNum > 0 && resReqInter.queNum > 0)) {
276 0 : HCCL_ERROR("Intra queNum and Inter queNum must larger than 0.");
277 0 : return HcclResult::HCCL_E_INTERNAL;
278 : }
279 0 : u32 totalQueueNum = resReqIntra.queNum + resReqInter.queNum;
280 0 : CHK_RET(InitQueue(totalQueueNum, requiredQue_));
281 0 : for (u32 i = 0; i < requiredQue_.size(); i++) {
282 0 : if (i < resReqIntra.queNum) {
283 0 : intraQue_.push_back(requiredQue_[i]);
284 : } else {
285 0 : interQue_.push_back(requiredQue_[i]);
286 : }
287 : }
288 0 : syncQueues_.emplace_back(intraQue_[0]);
289 0 : syncQueues_.emplace_back(interQue_[0]);
290 :
291 0 : CHK_RET(PrepResLinks(myRank_, rankGraph, linkPriority_, resReqIntra.links, intraLinks_));
292 0 : CHK_RET(PrepResLinks(myRank_, rankGraph, linkPriority_, resReqInter.links, interLinks_));
293 0 : HCCL_INFO(
294 : "[InsAllGatherParallelExecutor] intraLinks_ size[%zu], interLinks_ size[%zu]", intraLinks_.size(),
295 : interLinks_.size());
296 0 : return HCCL_SUCCESS;
297 0 : }
298 :
299 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
300 0 : HcclResult InsAllGatherParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::PrepareResForTemplate(
301 : ConnectedLinkMgr* linkMgr, InsAlgTemplate0& tempAlgIntra, InsAlgTemplate1& tempAlgInter)
302 : {
303 0 : AlgTempResReq resReqIntra;
304 0 : AlgTempResReq resReqInter;
305 0 : if (enableDetour_) {
306 0 : HCCL_DEBUG("[%s] Rank[%d], detouring enabled.", __func__, myRank_);
307 0 : CHK_RET(tempAlgIntra.CalcResDetour(linkMgr, resReqIntra));
308 : } else {
309 0 : HCCL_DEBUG("[%s] Rank[%d], detouring disabled.", __func__, myRank_);
310 0 : CHK_RET(tempAlgIntra.CalcRes(resReqIntra));
311 : }
312 0 : CHK_RET(tempAlgInter.CalcRes(resReqInter));
313 :
314 : // 申请算法模板所需资源
315 0 : if (!(resReqIntra.queNum > 0 && resReqInter.queNum > 0)) {
316 0 : HCCL_ERROR("[InsAllGatherParallelExecutor]resReqIntra.queNum and resReqInter.queNum must larger than 0.");
317 0 : return HcclResult::HCCL_E_INTERNAL;
318 : }
319 0 : u32 totalQueueNum = resReqIntra.queNum + resReqInter.queNum;
320 0 : CHK_RET(InitQueue(totalQueueNum, requiredQue_));
321 0 : for (u32 q = 0; q < requiredQue_.size(); q++) {
322 0 : if (q < resReqIntra.queNum) {
323 0 : intraQue_.push_back(requiredQue_[q]);
324 : } else {
325 0 : interQue_.push_back(requiredQue_[q]);
326 : }
327 : }
328 0 : syncQueues_.emplace_back(intraQue_[0]);
329 0 : syncQueues_.emplace_back(interQue_[0]);
330 :
331 0 : CHK_RET(PrepResLinks(myRank_, resReqIntra.links, linkMgr, intraLinks_));
332 0 : CHK_RET(PrepResLinks(myRank_, resReqInter.links, linkMgr, interLinks_));
333 0 : HCCL_INFO(
334 : "[InsAllGatherParallelExecutor] intraLinks_ size[%zu], interLinks_ size[%zu]", intraLinks_.size(),
335 : interLinks_.size());
336 0 : return HCCL_SUCCESS;
337 0 : }
338 :
339 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
340 0 : HcclResult InsAllGatherParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::Orchestrate(
341 : const AlgTopoInfo& topoInfo, const CollAlgOperator& op, const CollAlgParams& params, ConnectedLinkMgr* linkMgr,
342 : InsQuePtr insQue)
343 : {
344 0 : HCCL_INFO("[InsAllGatherParallelExecutor] Orchestrate begins.");
345 : // init and check params
346 0 : CHK_RET(Init(op, params, insQue));
347 0 : dataType_ = op.dataType;
348 0 : virtRanks_ = topoInfo.virtRanks;
349 0 : vTopo_ = topoInfo.vTopo;
350 0 : virtRankMap_ = topoInfo.virtRankMap;
351 :
352 0 : CHK_RET(CalcLocalRankSize(myRank_, virtRanks_, rankSizeLevel0_, rankSizeLevel1_));
353 0 : rankIdxLevel0_ = myRank_ % virtRanks_[0].size();
354 0 : rankIdxLevel1_ = myRank_ / virtRanks_[0].size();
355 :
356 : // 实例化算法模板类
357 0 : InsAlgTemplate0 tempAlgIntra(myRank_, rankSizeLevel0_, vTopo_[0], virtRankMap_[0]); // server内算法,比如mesh
358 0 : InsAlgTemplate1 tempAlgInter(myRank_, rankSizeLevel1_, vTopo_[1], virtRankMap_[1]); // server间算法,比如nhr
359 :
360 : // 实例化算法模板类
361 :
362 0 : tempAlgIntra.SetDmaMode(dmaMode_);
363 0 : tempAlgIntra.SetCollOp(op); // CCU template需要传递op信息
364 0 : tempAlgIntra.SetDataType(dataType_);
365 :
366 0 : tempAlgInter.SetDmaMode(dmaMode_);
367 0 : tempAlgInter.SetCollOp(op); // CCU template需要传递op信息
368 0 : tempAlgInter.SetDataType(dataType_);
369 :
370 0 : std::vector<std::map<u32, u32>> rank2PathNumMap;
371 0 : CHK_RET(SetPathNumMapByLinkMgrMultiLevel(linkMgr, virtRanks_, myRank_, rank2PathNumMap));
372 0 : tempAlgIntra.setPathNumMap(rank2PathNumMap[0]);
373 0 : tempAlgInter.setPathNumMap(rank2PathNumMap[1]);
374 :
375 : // 计算算法模板所需资源
376 0 : CHK_RET(PrepareResForTemplate(linkMgr, tempAlgIntra, tempAlgInter));
377 :
378 0 : CHK_RET(GenInsQuesHost(tempAlgIntra, tempAlgInter));
379 0 : HCCL_INFO("[InsAllGatherParallelExecutor] Orchestrate success.");
380 :
381 0 : return HcclResult::HCCL_SUCCESS;
382 0 : }
383 :
384 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
385 0 : HcclResult InsAllGatherParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::Orchestrate(
386 : const RankGraph* rankGraph, const CollAlgOperator& op, const CollAlgParams& params, InsQuePtr insQue)
387 : {
388 0 : HCCL_INFO("[InsAllGatherParallelExecutor] Host Orchestrate begins.");
389 : // init and check params
390 0 : CHK_RET(Init(op, params, insQue));
391 :
392 : // Topo Match
393 0 : AlgTopoMatch topoMatch(myRank_, rankSize_, rankGraph, devType_);
394 0 : CHK_RET(topoMatch.MatchTopo(vTopo_, virtRanks_, virtRankMap_));
395 0 : CHK_RET(CalcLocalRankSize(myRank_, virtRanks_, rankSizeLevel0_, rankSizeLevel1_));
396 0 : rankIdxLevel0_ = myRank_ % virtRanks_[0].size();
397 0 : rankIdxLevel1_ = myRank_ / virtRanks_[0].size();
398 0 : HCCL_DEBUG(
399 : "[InsAllGatherParallelExecutor] my rank is [%d] ranksize is [%u], rankIdxLevel0_ = [%u], rankIdxLevel1_ "
400 : "= [%u] .",
401 : myRank_, rankSize_, rankIdxLevel0_, rankIdxLevel1_);
402 : // 实例化算法模板类
403 0 : InsAlgTemplate0 tempAlgIntra(myRank_, rankSizeLevel0_, vTopo_[0], virtRankMap_[0]); // server内算法,比如mesh
404 0 : InsAlgTemplate1 tempAlgInter(myRank_, rankSizeLevel1_, vTopo_[1], virtRankMap_[1]); // server间算法,比如nhr
405 :
406 0 : tempAlgIntra.SetDmaMode(dmaMode_);
407 0 : tempAlgIntra.SetDataType(dataType_);
408 0 : tempAlgIntra.SetCollOp(op); // CCU template需要传递op信息
409 :
410 0 : tempAlgInter.SetDmaMode(dmaMode_);
411 0 : tempAlgInter.SetDataType(dataType_);
412 0 : tempAlgInter.SetCollOp(op); // CCU template需要传递op信息
413 :
414 : // 计算算法模板所需资源
415 0 : CHK_RET(PrepareResForTemplate(rankGraph, tempAlgIntra, tempAlgInter));
416 :
417 0 : CHK_RET(GenInsQuesHost(tempAlgIntra, tempAlgInter));
418 0 : HCCL_INFO("[InsAllGatherParallelExecutor]Host Orchestrate success.");
419 :
420 0 : return HcclResult::HCCL_SUCCESS;
421 0 : }
422 :
423 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
424 0 : HcclResult InsAllGatherParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::GenInsQuesHost(
425 : InsAlgTemplate0& tempAlgIntra, InsAlgTemplate1& tempAlgInter)
426 : {
427 0 : HCCL_INFO("[InsAllGatherParallelExecutor] AlgTemplate inter server is [%s]", tempAlgIntra.Describe().c_str());
428 0 : HCCL_INFO("[InsAllGatherParallelExecutor] AlgTemplate intra server is [%s]", tempAlgInter.Describe().c_str());
429 0 : std::vector<float> dataSplitSize;
430 0 : GetParallelDataSplit(dataSplitSize);
431 0 : u64 alignedSize = 16 * 1024; // 假设需要16K对齐
432 0 : BufferType inBuffType = BufferType::INPUT;
433 0 : BufferType outBuffType = BufferType::OUTPUT;
434 0 : u32 intraScatchteMultipleStage0 = tempAlgIntra.CalcScratchMultiple(inBuffType, outBuffType);
435 0 : u32 interScatchteMultipleStage0 = tempAlgInter.CalcScratchMultiple(inBuffType, outBuffType);
436 0 : u32 intraScatchteMultipleStage1 = tempAlgIntra.CalcScratchMultiple(outBuffType, outBuffType);
437 0 : u32 interScatchteMultipleStage1 = tempAlgInter.CalcScratchMultiple(outBuffType, outBuffType);
438 0 : u32 scratchMultipleIntra = static_cast<u32>(std::max(
439 0 : std::ceil(dataSplitSize[0] * intraScatchteMultipleStage0),
440 0 : std::ceil(dataSplitSize[1] * intraScatchteMultipleStage1 * rankSizeLevel1_)));
441 0 : u32 scratchMultipleInter = static_cast<u32>(std::max(
442 0 : std::ceil(dataSplitSize[1] * interScatchteMultipleStage0),
443 0 : std::ceil(dataSplitSize[0] * interScatchteMultipleStage1 * rankSizeLevel0_)));
444 0 : u32 totalScratchMultiple = scratchMultipleIntra + scratchMultipleInter;
445 0 : u64 scratchMemBlockSize = maxTmpMemSize_;
446 0 : if (totalScratchMultiple > 0) {
447 0 : scratchMemBlockSize = (maxTmpMemSize_ / alignedSize / totalScratchMultiple) * alignedSize;
448 : }
449 0 : u64 intraScratchOffset = 0;
450 0 : u64 interScratchOffset = scratchMultipleIntra * scratchMemBlockSize;
451 :
452 : // dataSplitSize为分数,这里maxCountPerLoop对10取整
453 0 : u64 maxCountPerLoop
454 0 : = (std::min(static_cast<u64>(scratchMemBlockSize), static_cast<u64>(UB_MAX_DATA_SIZE)) / dataTypeSize_ / 10)
455 : * 10;
456 :
457 0 : u32 loopTimes = dataCount_ / maxCountPerLoop + ((dataCount_ % maxCountPerLoop == 0) ? 0 : 1);
458 :
459 0 : TemplateDataParams tempAlgParamsIntra0, tempAlgParamsInter0;
460 0 : TemplateDataParams tempAlgParamsInter1, tempAlgParamsIntra1;
461 0 : TempFuncs tempFuncs;
462 0 : tempFuncs.opMode = opMode_;
463 0 : tempFuncs.enableCounterNotify = false;
464 0 : tempFuncs.isBottom = true;
465 0 : tempFuncs.isForepart = true;
466 0 : for (u32 loopIndex = 0; loopIndex < loopTimes; loopIndex++) {
467 0 : u64 currCount = (loopIndex == loopTimes - 1) ? (dataCount_ - loopIndex * maxCountPerLoop) : maxCountPerLoop;
468 0 : u64 dataCountPerLoopAixs0 = static_cast<u64>(dataSplitSize[0] * currCount);
469 0 : u64 dataCountPerLoopAixs1 = currCount - dataCountPerLoopAixs0;
470 : // 第一步开始前同步
471 0 : CHK_RET(PreSyncQues(syncQueues_, 0));
472 0 : u64 dataOffset0 = loopIndex * maxCountPerLoop * dataTypeSize_;
473 0 : u64 dataOffset1 = dataOffset0 + dataCountPerLoopAixs0 * dataTypeSize_;
474 : // 数据0的server内的mesh算法
475 0 : GenTemplateAlgParamsIntra0(dataOffset0, dataCountPerLoopAixs0, intraScratchOffset, tempAlgParamsIntra0);
476 : // 把每个template需要的queue传进去,比如stars的mesh要传多条queue
477 0 : CHK_RET(tempAlgIntra.GenExtIns(
478 : tempFuncs, tempAlgParamsIntra0, intraLinks_, intraQue_)); // Todo: 这里要把tempFuncs去掉
479 : // 数据1的server间的nhr算法
480 0 : GenTemplateAlgParamsInter1(dataOffset1, dataCountPerLoopAixs1, interScratchOffset, tempAlgParamsInter1);
481 0 : CHK_RET(tempAlgInter.GenExtIns(tempFuncs, tempAlgParamsInter1, interLinks_, interQue_));
482 : // 第一步做完后回到主流做尾同步
483 0 : CHK_RET(PostSyncQues(syncQueues_, 0));
484 :
485 : // 第二步开始前同步
486 0 : CHK_RET(PreSyncQues(syncQueues_, 0));
487 : // 数据0的server间的nhr算法
488 0 : GenTemplateAlgParamsInter0(dataOffset0, dataCountPerLoopAixs0, interScratchOffset, tempAlgParamsInter0);
489 0 : CHK_RET(tempAlgInter.GenExtIns(tempFuncs, tempAlgParamsInter0, interLinks_, interQue_));
490 : // 数据1的server内的mesh算法
491 0 : GenTemplateAlgParamsIntra1(dataOffset1, dataCountPerLoopAixs1, intraScratchOffset, tempAlgParamsIntra1);
492 0 : CHK_RET(tempAlgIntra.GenExtIns(tempFuncs, tempAlgParamsIntra1, intraLinks_, intraQue_));
493 : // 尾同步
494 0 : CHK_RET(PostSyncQues(syncQueues_, 0));
495 : }
496 0 : return HcclResult::HCCL_SUCCESS;
497 0 : }
498 :
499 : INS_REGISTER_IMPL_BY_TWO_TEMPS(
500 : OpType::ALLGATHER, InsAllGatherParallelMesh1DNHR, InsAllGatherParallelExecutor, TopoMatchMeshNHR,
501 : InsTempAllGatherMesh1D, InsTempAllGatherNHR);
502 : INS_REGISTER_IMPL_BY_TWO_TEMPS(
503 : OpType::ALLGATHER, InsAllGatherParallelMesh2DNHR, InsAllGatherParallelExecutor, TopoMatchConcurrMeshNHR,
504 : InsTempAllGatherMesh2D, InsTempAllGatherNHR);
505 : INS_REGISTER_IMPL_BY_TWO_TEMPS(
506 : OpType::ALLGATHER, InsAllGatherParallelNHRNHR, InsAllGatherParallelExecutor, TopoMatchMeshNHR,
507 : InsTempAllGatherMesh2D, InsTempAllGatherNHR);
508 : INS_REGISTER_IMPL_BY_TWO_TEMPS(
509 : OpType::ALLGATHER, InsAllGatherParallelMesh1DNHRPcie, InsAllGatherParallelExecutor, TopoMatchMeshNHRPcie,
510 : InsTempAllGatherMesh1D, InsTempAllGatherNHR);
511 :
512 : // 算法注册
513 : #ifndef CCL_KERNEL_AICPU
514 : INS_REGISTER_IMPL_BY_TWO_TEMPS(
515 : OpType::ALLGATHER, CcuAllGatherParallelMesh1DNHR, InsAllGatherParallelExecutor, TopoMatchMeshNHR,
516 : CcuTempAllGatherMesh1DMem2MemWithStride, CcuTempAllGatherNHRMem2Mem1D);
517 : #endif
518 :
519 : } // namespace Hccl
|