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_reduce_parallel_executor.h"
12 : #include <cmath>
13 : #include "log.h"
14 : #include "ins_coll_alg_registry.h"
15 : #include "topo_match_mesh_nhr.h"
16 : #include "topo_match_concurr_mesh_nhr.h"
17 : #include "topo_match_mesh_nhr_pcie.h"
18 : #include "alg_data_trans_wrapper.h"
19 : #include "ins_temp_all_reduce_nhr.h"
20 : #include "ins_temp_all_reduce_mesh_1D_two_shot.h"
21 : #include "ins_temp_all_reduce_mesh_2D_two_shot.h"
22 : #include "ccu_temp_all_reduce_nhr_1D_mem2mem.h"
23 : #include "ccu_temp_all_reduce_mesh_1D_mem2mem.h"
24 :
25 : namespace Hccl {
26 : constexpr u64 MAX_OFFLOAD_SCRATCH_SIZE = 200 * 1024 * 1024; // 200M
27 :
28 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
29 0 : InsAllReduceParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::InsAllReduceParallelExecutor()
30 0 : : InsCollAlgBase()
31 : {
32 0 : }
33 :
34 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
35 0 : InsAllReduceParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::~InsAllReduceParallelExecutor()
36 : {
37 0 : }
38 :
39 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
40 0 : HcclResult InsAllReduceParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::CalcResOffload(const RankGraph *rankGraph, const u64 &dataSize,
41 : CollOffloadOpResReq &resReq)
42 : {
43 0 : HCCL_INFO("[InsAllReduceParallelExecutor] CalcResOffload begins.");
44 : (void)dataSize;
45 0 : uint64_t tempSize = 2;
46 0 : u64 scratchMemSize = MAX_OFFLOAD_SCRATCH_SIZE;
47 0 : resReq.requiredScratchMemSize = scratchMemSize; // 200MB
48 : // Topo Match
49 0 : AlgTopoMatch topoMatch(myRank_, rankSize_, rankGraph, devType_);
50 0 : CHK_RET(topoMatch.MatchTopo(vTopo_, virtRanks_, virtRankMap_));
51 0 : CHK_RET(CalcLocalRankSize(myRank_, virtRanks_, rankSizeLevel0_, rankSizeLevel1_));
52 0 : InsAlgTemplate0 intraTempAlg(myRank_, rankSizeLevel0_, vTopo_[0], virtRankMap_[0]);
53 0 : InsAlgTemplate1 interTempAlg(myRank_, rankSizeLevel1_, vTopo_[1], virtRankMap_[1]);
54 :
55 0 : std::vector<map<u32, u32>> rank2PathNumMap;
56 0 : HCCL_INFO("[InsV2AllGatherSoleExecutor] CalcResOffload SetPathNumMap");
57 0 : CHK_RET(SetPathNumMapByRankGraphMultiLevel(rankGraph, virtRanks_, myRank_, rank2PathNumMap));
58 0 : intraTempAlg.setPathNumMap(rank2PathNumMap[0]);
59 0 : interTempAlg.setPathNumMap(rank2PathNumMap[1]);
60 :
61 : // calculate required insQues and prepare queue
62 0 : AlgTempResReq resReqIntra;
63 0 : AlgTempResReq resReqInter;
64 0 : if (enableDetour_) {
65 0 : HCCL_DEBUG("[InsAllReduceParallelExecutor] Rank[%d], CalcRes with detouring enabled.", myRank_);
66 0 : CHK_RET(intraTempAlg.CalcResDetour(rankGraph, resReqIntra));
67 : } else {
68 0 : HCCL_DEBUG("[InsAllReduceParallelExecutor] Rank[%d], CalcRes with detouring disabled.", myRank_);
69 0 : CHK_RET(intraTempAlg.CalcRes(resReqIntra));
70 : }
71 :
72 0 : CHK_RET(interTempAlg.CalcRes(resReqInter));
73 :
74 : // 算法从流数量 = Σ(temp的que数量 + temp的从流数量 * temp调用次数) - 算法主流数量
75 0 : resReq.requiredSubQueNum = resReqIntra.queNum + (resReqIntra.streamNum - resReqIntra.queNum) * tempSize
76 0 : + resReqInter.queNum + (resReqInter.streamNum - resReqInter.queNum) * tempSize
77 0 : - 1;
78 0 : HCCL_INFO("[InsAllReduceParallelExecutor::CalcResOffload]requiredSubQueNum = %llu", resReq.requiredSubQueNum);
79 :
80 0 : return HcclResult::HCCL_SUCCESS;
81 0 : }
82 :
83 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
84 0 : HcclResult InsAllReduceParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::CalcRes(const RankGraph *rankGraph, CollAlgResReq &algResReq)
85 : {
86 0 : HCCL_INFO("[InsAllReduceParallelExecutor] CalcRes begins.");
87 : // Topo Match
88 0 : AlgTopoMatch topoMatch(myRank_, rankSize_, rankGraph, devType_);
89 0 : CHK_RET(topoMatch.MatchTopo(vTopo_, virtRanks_, virtRankMap_));
90 0 : algResReq.topoInfo.UpdateMultiLevelTopo(virtRanks_, virtRankMap_, vTopo_);
91 0 : CHK_RET(CalcLocalRankSize(myRank_, virtRanks_, rankSizeLevel0_, rankSizeLevel1_));
92 :
93 : // instantiate a template
94 0 : InsAlgTemplate0 intraTempAlg(myRank_, rankSizeLevel0_, vTopo_[0], virtRankMap_[0]);
95 0 : InsAlgTemplate1 interTempAlg(myRank_, rankSizeLevel1_, vTopo_[1], virtRankMap_[1]);
96 :
97 0 : std::vector<map<u32, u32>> rank2PathNumMap;
98 0 : HCCL_INFO("[InsAllReduceParallelExecutor] CalcResOffload SetPathNumMap");
99 0 : CHK_RET(SetPathNumMapByRankGraphMultiLevel(rankGraph, virtRanks_, myRank_, rank2PathNumMap));
100 0 : intraTempAlg.setPathNumMap(rank2PathNumMap[0]);
101 0 : interTempAlg.setPathNumMap(rank2PathNumMap[1]);
102 :
103 : // calculate required insQues and prepare queue
104 0 : AlgTempResReq resReqIntra;
105 0 : AlgTempResReq resReqInter;
106 0 : if (enableDetour_) {
107 0 : HCCL_DEBUG("[InsAllReduceParallelExecutor] Rank[%d], CalcRes with detouring enabled.", myRank_);
108 0 : CHK_RET(intraTempAlg.CalcResDetour(rankGraph, resReqIntra));
109 : } else {
110 0 : HCCL_DEBUG("[InsAllReduceParallelExecutor] Rank[%d], CalcRes with detouring disabled.", myRank_);
111 0 : CHK_RET(intraTempAlg.CalcRes(resReqIntra));
112 : }
113 0 : CHK_RET(interTempAlg.CalcRes(resReqInter));
114 :
115 0 : CHK_RET(CalcLinkInfo(myRank_, rankGraph, resReqIntra.links, algResReq.levelRankPairs));
116 0 : CHK_RET(CalcLinkInfo(myRank_, rankGraph, resReqInter.links, algResReq.levelRankPairs));
117 0 : algResReq.primQueueNum = resReqIntra.queNum + resReqInter.queNum;
118 0 : CHK_RET(CalcParallelNotifyReq(algResReq.primQueueNum, resReqIntra.queNum, algResReq.queueNotifys));
119 0 : CHK_RET(CalcResLinks(myRank_, rankGraph, linkPriority_, resReqIntra.links, algResReq.links));
120 0 : CHK_RET(CalcResLinks(myRank_, rankGraph, linkPriority_, resReqIntra.links, algResReq.links));
121 0 : CHK_RET(CalcResLinks(myRank_, rankGraph, linkPriority_, resReqInter.links, algResReq.links));
122 0 : return HcclResult::HCCL_SUCCESS;
123 0 : }
124 :
125 : // HOST 侧算法入口,将对应的 instruction 添加到指令队列中
126 : // 传入的insQue为一条主流
127 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
128 0 : void InsAllReduceParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::GenTemplateAlgParams0(
129 : const u64 dataOffset, const u64 dataCount, const u64 scratchOffset, TemplateDataParams &tempAlgParams) const
130 : {
131 0 : tempAlgParams.buffInfo.inBuffType = BufferType::INPUT;
132 0 : tempAlgParams.buffInfo.outBuffType = BufferType::OUTPUT;
133 0 : tempAlgParams.buffInfo.scratBuffType = BufferType::SCRATCH;
134 0 : tempAlgParams.buffInfo.inBuffBaseOff = dataOffset;
135 0 : tempAlgParams.buffInfo.outBuffBaseOff = dataOffset;
136 0 : tempAlgParams.buffInfo.scratchBuffBaseOff = scratchOffset;
137 0 : tempAlgParams.sliceSize = dataCount * dataTypeSize_;
138 0 : tempAlgParams.tailSize = tempAlgParams.sliceSize;
139 0 : tempAlgParams.inputSliceStride = 0; // 输入数据仅有 1 个 slice, 不需要 stride
140 0 : tempAlgParams.outputSliceStride = 0;
141 :
142 0 : return;
143 : }
144 :
145 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
146 0 : void InsAllReduceParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::GenTemplateAlgParams1(
147 : const u64 dataOffset, const u64 dataCount, const u64 scratchOffset, TemplateDataParams &tempAlgParams) const
148 : {
149 0 : tempAlgParams.buffInfo.inBuffType = BufferType::OUTPUT;
150 0 : tempAlgParams.buffInfo.outBuffType = BufferType::OUTPUT;
151 0 : tempAlgParams.buffInfo.scratBuffType = BufferType::SCRATCH;
152 0 : tempAlgParams.buffInfo.inBuffBaseOff = dataOffset;
153 0 : tempAlgParams.buffInfo.outBuffBaseOff = dataOffset;
154 0 : tempAlgParams.buffInfo.scratchBuffBaseOff = scratchOffset;
155 0 : tempAlgParams.sliceSize = dataCount * dataTypeSize_;
156 0 : tempAlgParams.tailSize = tempAlgParams.sliceSize;
157 0 : tempAlgParams.inputSliceStride = 0; // 输入数据仅有 1 个 slice, 不需要 stride
158 0 : tempAlgParams.outputSliceStride = 0;
159 0 : return;
160 : }
161 :
162 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
163 0 : void InsAllReduceParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::GetParallelDataSplitRate(
164 : std::vector<float> &splitDataSize) const
165 : {
166 : // to do 先做等分,后续根据性能做调整
167 0 : double splitData = 0.5;
168 0 : splitDataSize.push_back(splitData);
169 0 : splitDataSize.push_back(splitData);
170 0 : return;
171 : }
172 :
173 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
174 0 : HcclResult InsAllReduceParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::PrepareResForTemplate(
175 : const RankGraph *rankGraph, InsAlgTemplate0 &tempAlgIntra, InsAlgTemplate1 &tempAlgInter)
176 : {
177 0 : AlgTempResReq resReqInter;
178 0 : AlgTempResReq resReqIntra;
179 0 : if (enableDetour_) {
180 0 : HCCL_DEBUG("[%s] Rank[%d], detouring enabled.", __func__, myRank_);
181 0 : CHK_RET(tempAlgIntra.CalcResDetour(rankGraph, resReqIntra));
182 : } else {
183 0 : HCCL_DEBUG("[%s] Rank[%d], detouring disabled.", __func__, myRank_);
184 0 : CHK_RET(tempAlgIntra.CalcRes(resReqIntra));
185 : }
186 0 : CHK_RET(tempAlgInter.CalcRes(resReqInter));
187 :
188 : // 申请算法模板所需资源
189 0 : if (!(resReqIntra.queNum > 0 && resReqInter.queNum > 0)) {
190 0 : HCCL_ERROR("[InsAllReduceParallelExecutor]resReqIntra.queNum and resReqInter.queNum must larger than 0.");
191 0 : return HcclResult::HCCL_E_INTERNAL;
192 : }
193 0 : u32 totalQueueNum = resReqIntra.queNum + resReqInter.queNum;
194 0 : CHK_RET(InitQueue(totalQueueNum, requiredQue_));
195 0 : for (u32 qIdx = 0; qIdx < requiredQue_.size(); qIdx++) {
196 0 : if (qIdx < resReqIntra.queNum) {
197 0 : intraQue_.push_back(requiredQue_[qIdx]);
198 : } else {
199 0 : interQue_.push_back(requiredQue_[qIdx]);
200 : }
201 : }
202 0 : syncQueues_.emplace_back(intraQue_[0]);
203 0 : syncQueues_.emplace_back(interQue_[0]);
204 :
205 0 : CHK_RET(PrepResLinks(myRank_, rankGraph, linkPriority_, resReqIntra.links, intraLinks_));
206 0 : CHK_RET(PrepResLinks(myRank_, rankGraph, linkPriority_, resReqInter.links, interLinks_));
207 0 : HCCL_INFO("[InsAllReduceParallelExecutor] intraLinks_ size[%zu], interLinks_ size[%zu]",
208 : intraLinks_.size(),
209 : interLinks_.size());
210 0 : return HCCL_SUCCESS;
211 0 : }
212 :
213 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
214 0 : HcclResult InsAllReduceParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::PrepareResForTemplate(ConnectedLinkMgr *linkMgr,
215 : InsAlgTemplate0 &tempAlgIntra,
216 : InsAlgTemplate1 &tempAlgInter)
217 : {
218 0 : AlgTempResReq resReqIntra;
219 0 : AlgTempResReq resReqInter;
220 0 : if (enableDetour_) {
221 0 : HCCL_DEBUG("[InsAllReduceParallelExecutor] Rank[%d], CalcRes with detouring enabled.", myRank_);
222 0 : CHK_RET(tempAlgIntra.CalcResDetour(linkMgr, resReqIntra));
223 : } else {
224 0 : HCCL_DEBUG("[InsAllReduceParallelExecutor] Rank[%d], CalcRes with detouring disabled.", myRank_);
225 0 : CHK_RET(tempAlgIntra.CalcRes(resReqIntra));
226 : }
227 0 : CHK_RET(tempAlgInter.CalcRes(resReqInter));
228 :
229 : // 申请算法模板所需资源
230 0 : if(!(resReqIntra.queNum > 0 && resReqInter.queNum > 0)) {
231 0 : HCCL_ERROR("[InsAllReduceParallelExecutor] Intra queNum and Inter queNum must larger than 0.");
232 0 : return HcclResult::HCCL_E_INTERNAL;
233 : }
234 0 : u32 totalQueueNum = resReqIntra.queNum + resReqInter.queNum;
235 0 : CHK_RET(InitQueue(totalQueueNum, requiredQue_));
236 0 : for(u32 i = 0 ; i < requiredQue_.size(); i++) {
237 0 : if (i < resReqIntra.queNum) {
238 0 : intraQue_.push_back(requiredQue_[i]);
239 : } else {
240 0 : interQue_.push_back(requiredQue_[i]);
241 : }
242 : }
243 0 : syncQueues_.emplace_back(intraQue_[0]);
244 0 : syncQueues_.emplace_back(interQue_[0]);
245 :
246 0 : CHK_RET(PrepResLinks(myRank_, resReqIntra.links, linkMgr, intraLinks_));
247 0 : CHK_RET(PrepResLinks(myRank_, resReqInter.links, linkMgr, interLinks_));
248 0 : HCCL_INFO("[InsAllReduceParallelExecutor] intraLinks_ size[%zu], interLinks_ size[%zu]", intraLinks_.size(), interLinks_.size());
249 0 : return HCCL_SUCCESS;
250 0 : }
251 :
252 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
253 0 : HcclResult InsAllReduceParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::CalcSendDataSize(
254 : u64 &memBlockSize, float &SplitRate, u32 &multipleIntra, u32 &multipleInter)
255 : {
256 0 : std::vector<float> dataSplitSize;
257 0 : GetParallelDataSplitRate(dataSplitSize);
258 0 : uint64_t templateNum = 2;
259 0 : if (multipleIntra == 0 && multipleInter == 0) {
260 0 : memBlockSize = UB_MAX_DATA_SIZE + UB_MAX_DATA_SIZE;
261 0 : } else if ((multipleIntra == 0 && multipleInter > 0) || (multipleInter == 0 && multipleIntra > 0)) {
262 : // 因为数据要交替在两个template中执行,因此最终要以数据处理量小的template为准
263 0 : if (multipleIntra > 0) {
264 0 : memBlockSize = std::min(static_cast<u64>(UB_MAX_DATA_SIZE), maxTmpMemSize_ / multipleIntra) * templateNum;
265 0 : Intra0ScratchSize = maxTmpMemSize_;
266 0 : Intra1ScratchSize = maxTmpMemSize_;
267 : } else {
268 0 : memBlockSize = std::min(static_cast<u64>(UB_MAX_DATA_SIZE), maxTmpMemSize_ / multipleInter) * templateNum;
269 0 : Inter0ScratchSize = maxTmpMemSize_;
270 0 : Inter1ScratchSize = maxTmpMemSize_;
271 : }
272 : } else { // multipleIntra >0 && multipleInter >0, 理论上dataSplitSize[0]=0.5时,scratch buffer利用率最大
273 0 : SplitRate = dataSplitSize[0];
274 0 : u32 subMultiple0 = static_cast<u32>(std::ceil(SplitRate * multipleIntra+(1-SplitRate)*multipleInter));
275 0 : u32 subMultiple1 = static_cast<u32>(std::ceil((1-SplitRate) * multipleIntra+SplitRate*multipleInter));
276 0 : u64 totalScratchMultiple = std::max(subMultiple0, subMultiple1);
277 0 : memBlockSize = std::min(static_cast<u64>(UB_MAX_DATA_SIZE), maxTmpMemSize_/totalScratchMultiple);
278 :
279 0 : interScratchOffset0 = static_cast<u64>(memBlockSize*SplitRate*multipleIntra);
280 0 : interScratchOffset1 = static_cast<u64>(memBlockSize*(1-SplitRate)*multipleIntra);
281 0 : Intra0ScratchSize = interScratchOffset0;
282 0 : Inter0ScratchSize = interScratchOffset1;
283 0 : Intra1ScratchSize = interScratchOffset1;
284 0 : Inter1ScratchSize = interScratchOffset0;
285 : }
286 0 : return HCCL_SUCCESS;
287 0 : }
288 :
289 : /*
290 : *@Desc: AICPU算法编排
291 : */
292 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
293 0 : HcclResult InsAllReduceParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::Orchestrate(
294 : const AlgTopoInfo &topoInfo, const CollAlgOperator &op, const CollAlgParams ¶ms, ConnectedLinkMgr *linkMgr,
295 : InsQuePtr insQue)
296 : {
297 0 : HCCL_INFO("[InsAllReduceParallelExecutor] AICPU Orchestrate begins.");
298 : // init and check params
299 0 : CHK_RET(Init(op, params, insQue));
300 : // 所以获取取级通信域的信息
301 0 : vTopo_ = topoInfo.vTopo; // 本通信域内的通信平面
302 0 : virtRankMap_ = topoInfo.virtRankMap; // 本通信域内的 rank 映射表
303 0 : virtRanks_ = topoInfo.virtRanks; // 本通信域内的 rank 集合
304 0 : CHK_RET(CalcLocalRankSize(myRank_, virtRanks_, rankSizeLevel0_, rankSizeLevel1_));
305 :
306 : // 实例化算法模板类
307 0 : InsAlgTemplate0 tempAlgIntra(myRank_, rankSizeLevel0_, vTopo_[0], virtRankMap_[0]); // server内算法,比如mesh
308 0 : InsAlgTemplate1 tempAlgInter(myRank_, rankSizeLevel1_, vTopo_[1], virtRankMap_[1]); // server间算法,比如nhr
309 :
310 0 : tempAlgInter.SetDmaMode(dmaMode_);
311 0 : tempAlgInter.InitReduceInfo(redOp_, dataType_);
312 0 : tempAlgInter.SetCollOp(op);
313 :
314 0 : tempAlgIntra.SetDmaMode(dmaMode_);
315 0 : tempAlgIntra.InitReduceInfo(redOp_, dataType_);
316 0 : tempAlgIntra.SetCollOp(op);
317 :
318 0 : std::vector<std::map<u32, u32>>rank2PathNumMap;
319 0 : SetPathNumMapByLinkMgrMultiLevel(linkMgr, virtRanks_, myRank_, rank2PathNumMap);
320 0 : tempAlgIntra.setPathNumMap(rank2PathNumMap[0]);
321 0 : tempAlgInter.setPathNumMap(rank2PathNumMap[1]);
322 :
323 : // 计算算法模板所需资源
324 0 : CHK_RET(PrepareResForTemplate(linkMgr, tempAlgIntra, tempAlgInter));
325 0 : CHK_RET(GenInsQues(tempAlgIntra, tempAlgInter));
326 0 : HCCL_INFO("[InsAllReduceParallelExecutor] Orchestrate success.");
327 :
328 0 : return HcclResult::HCCL_SUCCESS;
329 0 : }
330 :
331 : /*
332 : *@Desc: Host算法编排
333 : */
334 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
335 0 : HcclResult InsAllReduceParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::Orchestrate(
336 : const RankGraph *rankGraph, const CollAlgOperator &op, const CollAlgParams ¶ms, InsQuePtr insQue)
337 : {
338 0 : HCCL_INFO("[InsAllReduceParallelExecutor] Host Orchestrate begins.");
339 : // init and check params
340 0 : CHK_RET(Init(op, params, insQue));
341 :
342 : // Topo Match
343 0 : AlgTopoMatch topoMatch(myRank_, rankSize_, rankGraph, devType_);
344 0 : CHK_RET(topoMatch.MatchTopo(vTopo_, virtRanks_, virtRankMap_));
345 :
346 0 : CHK_RET(CalcLocalRankSize(myRank_, virtRanks_, rankSizeLevel0_, rankSizeLevel1_));
347 :
348 : // 实例化算法模板类
349 0 : InsAlgTemplate0 tempAlgIntra(myRank_, rankSizeLevel0_, vTopo_[0], virtRankMap_[0]); // server内算法,比如mesh
350 0 : InsAlgTemplate1 tempAlgInter(myRank_, rankSizeLevel1_, vTopo_[1], virtRankMap_[1]); // server间算法,比如nhr
351 :
352 0 : tempAlgIntra.InitReduceInfo(redOp_, dataType_);
353 0 : tempAlgIntra.SetDmaMode(dmaMode_);
354 0 : tempAlgIntra.SetCollOp(op);
355 :
356 0 : tempAlgInter.SetDmaMode(dmaMode_);
357 0 : tempAlgInter.SetCollOp(op); // CCU template需要传递op信息
358 0 : tempAlgInter.InitReduceInfo(redOp_, dataType_);
359 :
360 : // 计算算法模板所需资源
361 0 : CHK_RET(PrepareResForTemplate(rankGraph, tempAlgIntra, tempAlgInter));
362 :
363 0 : CHK_RET(GenInsQues(tempAlgIntra, tempAlgInter));
364 0 : HCCL_INFO("[InsAllReduceParallelExecutor] Orchestrate success.");
365 :
366 0 : return HcclResult::HCCL_SUCCESS;
367 0 : }
368 :
369 : /*
370 : @Desc: 本方法主要实现的是跨框算法实现,如下图,框内和框间分别用不同的算法实现
371 : /-------------------\ /-------------------\
372 : | /----\ /----\ | | /----\ /----\ |
373 : | |card| |card| | | |card| |card| |
374 : | \----/ \----/ | | \----/ \----/ |
375 : | | | |
376 : | Machine 1 | | Machine 2 |
377 : \-------------------/ \-------------------/
378 : */
379 : template <typename AlgTopoMatch, typename InsAlgTemplate0, typename InsAlgTemplate1>
380 0 : HcclResult InsAllReduceParallelExecutor<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>::GenInsQues(
381 : InsAlgTemplate0 &tempAlgIntra, InsAlgTemplate1 &tempAlgInter)
382 : {
383 0 : u64 alignedSize = 128; // 假设需要128字节对齐,太大会导致后续maxCountPerLoop计算有问题
384 0 : u32 multipleIntra = tempAlgIntra.CalcScratchMultiple(BufferType::INPUT, BufferType::OUTPUT);
385 0 : u32 multipleInter = tempAlgInter.CalcScratchMultiple(BufferType::INPUT, BufferType::OUTPUT);
386 0 : u64 memBlockSize = UB_MAX_DATA_SIZE;
387 0 : CalcSendDataSize(memBlockSize, dataSplitRate, multipleIntra, multipleInter);
388 : // dataSplitSize为分数,这里maxCountPerLoop对10取整,ScratchBufferSize为1M时可能会导致maxCountPerLoop为0;
389 0 : u64 maxCountPerLoop = (memBlockSize / dataTypeSize_ / 10 / alignedSize) * 10 * alignedSize;
390 0 : CHK_PRT_RET(maxCountPerLoop == 0,
391 : HCCL_ERROR("[InsAllReduceParallelExecutor] memBlockSize:%llu,maxCountPerLoop==0!.", memBlockSize),
392 : HcclResult::HCCL_E_INTERNAL);
393 0 : u32 loopTimes = dataCount_ / maxCountPerLoop + ((dataCount_ % maxCountPerLoop == 0) ? 0 : 1);
394 :
395 0 : TemplateDataParams tempAlgParamsIntra0, tempAlgParamsInter0, tempAlgParamsInter1, tempAlgParamsIntra1;
396 0 : TempFuncs tempFuncs;
397 0 : tempFuncs.enableCounterNotify = false;
398 0 : tempFuncs.opMode = opMode_;
399 0 : tempFuncs.isBottom = true;
400 0 : tempFuncs.isForepart = true;
401 0 : for (u32 loopIndex = 0; loopIndex < loopTimes; loopIndex++) {
402 0 : u64 currCount = (loopIndex == loopTimes - 1) ? (dataCount_ - loopIndex * maxCountPerLoop) : maxCountPerLoop;
403 0 : u64 dataCountPerLoopAixs0 = static_cast<u64>(dataSplitRate * currCount);
404 0 : u64 dataCountPerLoopAixs1 = currCount - dataCountPerLoopAixs0;
405 : // 第一步开始前同步
406 0 : CHK_RET(PreSyncQues(syncQueues_, 0));
407 0 : u64 dataOffset0 = loopIndex * maxCountPerLoop * dataTypeSize_;
408 0 : u64 dataOffset1 = dataOffset0 + dataCountPerLoopAixs0 * dataTypeSize_;
409 :
410 0 : tempAlgParamsIntra0.buffInfo.scratchBuffSize = Intra0ScratchSize;
411 0 : GenTemplateAlgParams0(dataOffset0, dataCountPerLoopAixs0, 0, tempAlgParamsIntra0);
412 : // 把每个template需要的queue传进去,比如stars的mesh要传多条queue
413 0 : CHK_RET(tempAlgIntra.GenExtIns(tempFuncs, tempAlgParamsIntra0, intraLinks_, intraQue_));
414 0 : tempAlgParamsInter0.buffInfo.scratchBuffSize = Inter0ScratchSize;
415 0 : GenTemplateAlgParams0(dataOffset1, dataCountPerLoopAixs1, interScratchOffset0, tempAlgParamsInter0);
416 0 : CHK_RET(tempAlgInter.GenExtIns(tempFuncs, tempAlgParamsInter0, interLinks_, interQue_));
417 0 : CHK_RET(PostSyncQues(syncQueues_, 0));
418 :
419 : // 第二步开始前同步
420 0 : CHK_RET(PreSyncQues(syncQueues_, 0));
421 0 : tempAlgParamsInter1.buffInfo.scratchBuffSize = Inter1ScratchSize;
422 0 : GenTemplateAlgParams1(dataOffset0, dataCountPerLoopAixs0, interScratchOffset1, tempAlgParamsInter1);
423 0 : CHK_RET(tempAlgInter.GenExtIns(tempFuncs, tempAlgParamsInter1, interLinks_, interQue_));
424 0 : tempAlgParamsIntra1.buffInfo.scratchBuffSize = Intra1ScratchSize;
425 0 : GenTemplateAlgParams1(dataOffset1, dataCountPerLoopAixs1, 0, tempAlgParamsIntra1);
426 0 : CHK_RET(tempAlgIntra.GenExtIns(tempFuncs, tempAlgParamsIntra1, intraLinks_, intraQue_));
427 0 : CHK_RET(PostSyncQues(syncQueues_, 0));
428 : }
429 0 : return HcclResult::HCCL_SUCCESS;
430 0 : }
431 :
432 : // 算法注册
433 : INS_REGISTER_IMPL_BY_TWO_TEMPS(OpType::ALLREDUCE, InsAllReduceParallelMesh1DNHR, InsAllReduceParallelExecutor,
434 : TopoMatchMeshNHR, InsTempAllReduceMesh1DTwoShot, InsTempAllReduceNHR);
435 : INS_REGISTER_IMPL_BY_TWO_TEMPS(OpType::ALLREDUCE, InsAllReduceParallelMesh2DNHR, InsAllReduceParallelExecutor,
436 : TopoMatchConcurrMeshNHR, InsTempAllReduceMesh2DTwoShot, InsTempAllReduceNHR);
437 : INS_REGISTER_IMPL_BY_TWO_TEMPS(OpType::ALLREDUCE, InsAllReduceParallelNHRNHR, InsAllReduceParallelExecutor,
438 : TopoMatchMeshNHR, InsTempAllReduceNHR, InsTempAllReduceNHR);
439 : INS_REGISTER_IMPL_BY_TWO_TEMPS(OpType::ALLREDUCE, InsAllReduceParallelMesh1DNHRPcie, InsAllReduceParallelExecutor,
440 : TopoMatchMeshNHRPcie, InsTempAllReduceMesh1DTwoShot, InsTempAllReduceNHR);
441 :
442 : #ifndef CCL_KERNEL_AICPU
443 : INS_REGISTER_IMPL_BY_TWO_TEMPS(OpType::ALLREDUCE, CcuAllReduceParallelMesh1DNHR, InsAllReduceParallelExecutor, TopoMatchMeshNHR,
444 : CcuTempAllReduceMeshMem2Mem1D, CcuTempAllReduceNHRMem2Mem1D);
445 : #endif
446 : }
|