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_v2_broadcast_sole_executor.h"
12 : #include "log.h"
13 : #include "ins_coll_alg_registry.h"
14 :
15 : #include "topo_match_mesh.h"
16 : #include "topo_match_nhr.h"
17 : #include "topo_match_concurr_mesh.h"
18 : #include "ccu_temp_broadcast_mesh_1D_mem2mem.h"
19 : #include "ins_temp_broadcast_mesh1D_oneshot.h"
20 : #include "ins_temp_broadcast_mesh_2D_two_shot.h"
21 : #include "ins_temp_broadcast_mesh_1D_two_shot.h"
22 : #include "ins_temp_broadcast_nhr.h"
23 :
24 : #ifndef CCL_KERNEL_AICPU
25 : #include "ccu_temp_broadcast_mesh_2D_mem2mem.h"
26 : #include "aiv_temp_broadcast_mesh_1D.h"
27 : #include "ccu_temp_broadcast_nhr_1D_mem2mem.h"
28 : #endif
29 :
30 : namespace Hccl {
31 : constexpr u64 MAX_OFFLOAD_SCRATCH_SIZE = 200 * 1024 * 1024; // 200M
32 : template <typename AlgTopoMatch, typename InsAlgTemplate>
33 0 : InsV2BroadcastSoleExecutor<AlgTopoMatch, InsAlgTemplate>::InsV2BroadcastSoleExecutor() : InsCollAlgBase()
34 : {
35 0 : }
36 :
37 : template <typename AlgTopoMatch, typename InsAlgTemplate>
38 0 : InsV2BroadcastSoleExecutor<AlgTopoMatch, InsAlgTemplate>::~InsV2BroadcastSoleExecutor()
39 : {
40 0 : }
41 :
42 : template <typename AlgTopoMatch, typename InsAlgTemplate>
43 0 : HcclResult InsV2BroadcastSoleExecutor<AlgTopoMatch, InsAlgTemplate>::InitCommInfo(const RankGraph *rankGraph)
44 : {
45 0 : AlgTopoMatch topoMatch(myRank_, rankSize_, rankGraph, devType_);
46 0 : CHK_RET(topoMatch.MatchTopo(vTopo_, virtRanks_, virtRankMap_));
47 0 : return HcclResult::HCCL_SUCCESS;
48 0 : }
49 :
50 : template <typename AlgTopoMatch, typename InsAlgTemplate>
51 0 : HcclResult InsV2BroadcastSoleExecutor<AlgTopoMatch, InsAlgTemplate>::InitCommInfo(const AlgTopoInfo &topoInfo)
52 : {
53 0 : CHK_PRT_RET(topoInfo.vTopo.size() == 0,
54 : HCCL_ERROR("[InsV2BroadcastSoleExecutor] [Orchestrate] vTopo size is 0."),
55 : HcclResult::HCCL_E_INTERNAL);
56 0 : vTopo_ = topoInfo.vTopo[0]; // 本通信域内的通信平面
57 0 : virtRankMap_ = topoInfo.virtRankMap[0]; // 本通信域内的 rank 映射表
58 0 : virtRanks_ = topoInfo.virtRanks[0]; // 本通信域内的 rank 集合
59 0 : return HcclResult::HCCL_SUCCESS;
60 : }
61 :
62 : template <typename AlgTopoMatch, typename InsAlgTemplate>
63 0 : HcclResult InsV2BroadcastSoleExecutor<AlgTopoMatch, InsAlgTemplate>::GetTemplateResRequest(
64 : const RankGraph *rankGraph, std::shared_ptr<InsAlgTemplate> &algTemplate, AlgTempResReq &tempResReq) const
65 : {
66 0 : if (enableDetour_) {
67 0 : HCCL_DEBUG("[InsV2BroadcastSoleExecutor] Rank[%d], CalcRes with detouring enabled.", myRank_);
68 0 : CHK_RET(algTemplate->CalcResDetour(rankGraph, tempResReq));
69 : } else {
70 0 : HCCL_DEBUG("[InsV2BroadcastSoleExecutor] Rank[%d], CalcRes with detouring disabled.", myRank_);
71 0 : CHK_RET(algTemplate->CalcRes(tempResReq));
72 : }
73 0 : return HcclResult::HCCL_SUCCESS;
74 : }
75 :
76 : template <typename AlgTopoMatch, typename InsAlgTemplate>
77 0 : HcclResult InsV2BroadcastSoleExecutor<AlgTopoMatch, InsAlgTemplate>::GetTemplateResRequest(
78 : ConnectedLinkMgr *linkMgr, std::shared_ptr<InsAlgTemplate> &algTemplate, AlgTempResReq &tempResReq) const
79 : {
80 0 : if (enableDetour_) {
81 0 : HCCL_DEBUG("[%s] Rank[%d]. CalcRes with detouring enabled.", __func__, myRank_);
82 0 : CHK_RET(algTemplate->CalcResDetour(linkMgr, tempResReq));
83 : } else {
84 0 : HCCL_DEBUG("[%s] Rank[%d], CalcRes with detouring disabled.", __func__, myRank_);
85 0 : CHK_RET(algTemplate->CalcRes(tempResReq));
86 : }
87 0 : return HcclResult::HCCL_SUCCESS;
88 : }
89 :
90 : template <typename AlgTopoMatch, typename InsAlgTemplate>
91 0 : HcclResult InsV2BroadcastSoleExecutor<AlgTopoMatch, InsAlgTemplate>::CalcResOffload(const RankGraph *rankGraph,
92 : const u64 &dataSize,
93 : CollOffloadOpResReq &resReq)
94 : {
95 : (void)dataSize;
96 0 : resReq.requiredScratchMemSize = 0;
97 :
98 : // Topo Match
99 0 : CHK_RET(InitCommInfo(rankGraph));
100 :
101 0 : std::shared_ptr<InsAlgTemplate> algTemplate = nullptr;
102 0 : CHK_RET(CreateTemplates(algTemplate));
103 0 : AlgTempResReq tempResReq;
104 0 : CHK_RET(GetTemplateResRequest(rankGraph, algTemplate, tempResReq));
105 0 : resReq.requiredScratchMemSize = MAX_OFFLOAD_SCRATCH_SIZE;
106 0 : resReq.requiredSubQueNum = tempResReq.streamNum - 1;
107 :
108 0 : return HcclResult::HCCL_SUCCESS;
109 0 : }
110 :
111 : template <typename AlgTopoMatch, typename InsAlgTemplate>
112 0 : HcclResult InsV2BroadcastSoleExecutor<AlgTopoMatch, InsAlgTemplate>::CreateTemplates(std::shared_ptr<InsAlgTemplate> &algTemplatePtr)
113 : {
114 0 : algTemplatePtr = std::make_shared<InsAlgTemplate>(myRank_, rankSize_, vTopo_, virtRankMap_);
115 0 : CHK_PTR_NULL(algTemplatePtr); // 检查是否成功分配内存
116 0 : InsAlgTemplate tempAlg(myRank_, rankSize_, vTopo_, virtRankMap_);
117 0 : algTemplatePtr->SetDmaMode(dmaMode_);
118 0 : algTemplatePtr->SetCollOp(op_); // CCU template需要传递op信息
119 0 : algTemplatePtr->SetRoot(root_);
120 0 : algTemplatePtr->SetDataType(dataType_);
121 0 : return HCCL_SUCCESS;
122 0 : }
123 :
124 : template <typename AlgTopoMatch, typename InsAlgTemplate>
125 0 : HcclResult InsV2BroadcastSoleExecutor<AlgTopoMatch, InsAlgTemplate>::CalcRes(const RankGraph *rankGraph,
126 : CollAlgResReq &algResReq)
127 : {
128 : // Topo Match
129 0 : CHK_RET(InitCommInfo(rankGraph));
130 0 : algResReq.topoInfo.UpdateSingleLevelTopo(virtRanks_, virtRankMap_, vTopo_);
131 :
132 : // instantiate a template
133 0 : InsAlgTemplate tempAlg(myRank_, rankSize_, vTopo_, virtRankMap_);
134 :
135 0 : std::shared_ptr<InsAlgTemplate> algTemplate = nullptr;
136 0 : CHK_RET(CreateTemplates(algTemplate));
137 :
138 0 : AlgTempResReq tempResReq;
139 0 : CHK_RET(GetTemplateResRequest(rankGraph, algTemplate, tempResReq));
140 0 : CHK_RET(CalcLinkInfo(myRank_, rankGraph, tempResReq.links, algResReq.levelRankPairs));
141 0 : algResReq.primQueueNum = tempResReq.streamNum;
142 0 : algResReq.queueNotifys = tempResReq.queNotifys;
143 0 : algResReq.localWaitGroupCntNotify = tempResReq.localWaitGroupCntNotify;
144 0 : algResReq.localBcastPostCntNotify = tempResReq.localBcastPostCntNotify;
145 0 : HCCL_DEBUG("[InsV2BroadcastSoleExecutor][InsCollAlgFactory] Rank[%d], requiredQueNum [%u].", myRank_, algResReq.primQueueNum);
146 0 : CHK_RET(CalcResLinks(myRank_, rankGraph, linkPriority_, tempResReq.links, algResReq.links));
147 :
148 0 : return HcclResult::HCCL_SUCCESS;
149 0 : }
150 :
151 : // host
152 : template <typename AlgTopoMatch, typename InsAlgTemplate>
153 0 : HcclResult InsV2BroadcastSoleExecutor<AlgTopoMatch, InsAlgTemplate>::Orchestrate(const RankGraph *rankGraph,
154 : const CollAlgOperator &op, const CollAlgParams ¶ms, InsQuePtr insQue)
155 : {
156 0 : HCCL_INFO("[InsCollAlgFactory] [InsV2BroadcastSoleExecutor] Host Orchestrate begins.");
157 : // init and check params
158 0 : CHK_RET(Init(op, params, insQue));
159 :
160 : // Topo Match
161 0 : CHK_RET(InitCommInfo(rankGraph));
162 0 : dataTypeSize_ = DataTypeSizeGet(dataType_);
163 0 : dataSize_ = dataCount_ * dataTypeSize_;
164 0 : CHK_PRT_RET(dataTypeSize_ == 0,
165 : HCCL_ERROR("[InsV2BroadcastSoleExecutor] [CollAlgFactory] Rank [%d], Invalid dataTypeSize_ [%u].", myRank_, dataTypeSize_),
166 : HcclResult::HCCL_E_INTERNAL);
167 :
168 : // 实例化算法模板类
169 0 : HCCL_DEBUG("[InsV2BroadcastSoleExecutor] Rank[%d], Init insAlgTemplate with rankSize [%u] and dmaMode [%s].",
170 : myRank_, rankSize_, dmaMode_.Describe().c_str());
171 0 : std::shared_ptr<InsAlgTemplate> tempAlg = nullptr;
172 0 : CHK_RET(CreateTemplates(tempAlg));
173 :
174 0 : AlgTempResReq tempResReq;
175 0 : CHK_RET(GetTemplateResRequest(rankGraph, tempAlg, tempResReq));
176 : // 申请算法模板所需资源
177 0 : CHK_RET(InitQueue(tempResReq.queNum, requiredQue_));
178 0 : CHK_RET(PrepResLinks(myRank_, rankGraph, linkPriority_, tempResReq.links, tempResLinks_));
179 :
180 0 : CHK_RET(OrchestrateLoop(tempAlg));
181 :
182 0 : return HcclResult::HCCL_SUCCESS;
183 0 : }
184 :
185 : // 算子执行aicpu接口
186 : template <typename AlgTopoMatch, typename InsAlgTemplate>
187 0 : HcclResult InsV2BroadcastSoleExecutor<AlgTopoMatch, InsAlgTemplate>::Orchestrate(const AlgTopoInfo &topoInfo,
188 : const CollAlgOperator &op, const CollAlgParams ¶ms, ConnectedLinkMgr *linkMgr,
189 : InsQuePtr insQue)
190 : {
191 0 : HCCL_INFO("[InsCollAlgFactory] [InsV2BroadcastSoleExecutor] AiCpu Orchestrate begins.");
192 : // 参数校验和初始化
193 0 : CHK_RET(Init(op, params, insQue));
194 : // soleEsecutor 只支持单层拓扑, 所以只取第 0 级通信域的信息
195 0 : CHK_RET(InitCommInfo(topoInfo));
196 0 : dataTypeSize_ = DataTypeSizeGet(dataType_);
197 0 : dataSize_ = dataCount_ * dataTypeSize_;
198 0 : CHK_PRT_RET(dataTypeSize_ == 0,
199 : HCCL_ERROR("[InsV2BroadcastSoleExecutor] [CollAlgFactory] Rank [%d], Invalid dataTypeSize_ [%u].", myRank_, dataTypeSize_),
200 : HcclResult::HCCL_E_INTERNAL);
201 :
202 : // 实例化算法模板类
203 0 : HCCL_DEBUG("[InsV2BroadcastSoleExecutor] Rank[%d], Init insAlgTemplate with rankSize [%u] and dmaMode [%s].",
204 : myRank_, rankSize_, dmaMode_.Describe().c_str());
205 0 : std::shared_ptr<InsAlgTemplate> tempAlg = nullptr;
206 0 : CHK_RET(CreateTemplates(tempAlg));
207 :
208 0 : AlgTempResReq tempResReq;
209 0 : CHK_RET(GetTemplateResRequest(linkMgr, tempAlg, tempResReq));
210 :
211 : // 申请算法模板所需资源
212 0 : CHK_RET(InitQueue(tempResReq.queNum, requiredQue_));
213 0 : CHK_RET(PrepResLinks(myRank_, tempResReq.links, linkMgr, tempResLinks_));
214 :
215 0 : CHK_RET(OrchestrateLoop(tempAlg));
216 :
217 0 : return HcclResult::HCCL_SUCCESS;
218 0 : }
219 :
220 : template <typename AlgTopoMatch, typename InsAlgTemplate>
221 0 : HcclResult InsV2BroadcastSoleExecutor<AlgTopoMatch, InsAlgTemplate>::OrchestrateLoop(std::shared_ptr<InsAlgTemplate> &tempAlg)
222 : {
223 : // 基本参数配置
224 0 : TempFuncs tempFuncs;
225 0 : tempFuncs.opMode = opMode_;
226 0 : tempFuncs.enableCounterNotify = IsEnableCounterNotify();
227 0 : tempFuncs.isForepart = true; // Usr Buff to CCL Buff required
228 0 : tempFuncs.isBottom = true; // CCL Buff to Usr Buff required
229 :
230 0 : TemplateDataParams tempAlgParams;
231 0 : tempAlgParams.buffInfo.inBuffType = BufferType::INPUT;
232 0 : tempAlgParams.buffInfo.outBuffType = BufferType::INPUT;
233 0 : tempAlgParams.buffInfo.scratBuffType = BufferType::SCRATCH;
234 0 : tempAlgParams.buffInfo.scratchBuffBaseOff = 0;
235 0 : tempAlgParams.inputSliceStride = 0;
236 0 : tempAlgParams.outputSliceStride = 0;
237 : // 不需要重复
238 0 : tempAlgParams.repeatNum = 1;
239 0 : tempAlgParams.inputRepeatStride = 0;
240 0 : tempAlgParams.outputRepeatStride = 0;
241 :
242 : // 根据CCL Buffer大小和UB_MAX_DATA_SIZE,计算出一轮中最多能输出多少数据
243 0 : u64 maxDataSizePerLoop = 0;
244 0 : u64 transportBoundDataSize = UB_MAX_DATA_SIZE; // algTemplate->CalcLoopMaxCount();
245 : u32 templateScratchMultiplier =
246 0 : tempAlg->CalcScratchMultiple(BufferType::INPUT, BufferType::INPUT);
247 0 : if (templateScratchMultiplier != 0) {
248 0 : u64 scratchBoundDataSize = maxTmpMemSize_ / templateScratchMultiplier;
249 0 : maxDataSizePerLoop = min(transportBoundDataSize, scratchBoundDataSize);
250 : } else {
251 0 : maxDataSizePerLoop = transportBoundDataSize;
252 : }
253 0 : u64 maxDataCountPerLoop = maxDataSizePerLoop / dataTypeSize_;
254 :
255 0 : u64 dataSize = dataCount_ * dataTypeSize_;
256 :
257 0 : u64 maxLoopOutputSize = maxDataCountPerLoop * dataTypeSize_;
258 :
259 0 : u64 loopTimes = dataSize / maxLoopOutputSize + static_cast<u64>(dataSize % maxLoopOutputSize != 0);
260 :
261 0 : for (u64 loop = 0; loop < loopTimes; loop++) {
262 0 : u64 currloopOffset = loop * maxLoopOutputSize;
263 0 : u64 currSize = (loop == (loopTimes - 1)) ? dataSize - currloopOffset : maxLoopOutputSize;
264 : // 当前搬运的数据片
265 0 : tempAlgParams.buffInfo.inBuffBaseOff = currloopOffset;
266 0 : tempAlgParams.buffInfo.outBuffBaseOff = currloopOffset;
267 :
268 0 : tempAlgParams.sliceSize = currSize;
269 0 : tempAlgParams.tailSize = tempAlgParams.sliceSize;
270 :
271 0 : CHK_RET(tempAlg->GenExtIns(tempFuncs, tempAlgParams, tempResLinks_, requiredQue_));
272 0 : HCCL_DEBUG("[InsCollAlgFactory] Rank[%d], done generating instruction queues, currSize[%llu], currOffset[%llu].",
273 : myRank_, currSize, currloopOffset);
274 : }
275 :
276 0 : return HcclResult::HCCL_SUCCESS;
277 0 : }
278 :
279 :
280 : INS_REGISTER_IMPL_BY_TEMP(OpType::BROADCAST, InsBroadcastMesh1DOneShot, InsV2BroadcastSoleExecutor, TopoMatchMesh,
281 : InsTempBroadcastMesh1DOneShot);
282 : INS_REGISTER_IMPL_BY_TEMP(OpType::BROADCAST, InsBroadcastMesh1DTwoShot, InsV2BroadcastSoleExecutor, TopoMatchMesh,
283 : InsTempBroadcastMesh1DTwoShot);
284 : INS_REGISTER_IMPL_BY_TEMP(OpType::BROADCAST, InsBroadcastMesh2DTwoShot, InsV2BroadcastSoleExecutor, TopoMatchConcurrMesh,
285 : InsTempBroadcastMesh2DTwoShot);
286 : INS_REGISTER_IMPL_BY_TEMP(OpType::BROADCAST, InsBroadcastNHR, InsV2BroadcastSoleExecutor, TopoMatchNHR,
287 : InsTempBroadcastNHR);
288 : #ifndef CCL_KERNEL_AICPU
289 : INS_REGISTER_IMPL_BY_TEMP(OpType::BROADCAST, CcuBroadcastMeshMem2Mem1D, InsV2BroadcastSoleExecutor, TopoMatchMesh,
290 : CcuTempBroadcastMesh1DMem2Mem);
291 : INS_REGISTER_IMPL_BY_TEMP(OpType::BROADCAST, CcuBroadcastMeshMem2Mem2D, InsV2BroadcastSoleExecutor, TopoMatchConcurrMesh,
292 : CcuTempBroadcastMeshMem2Mem2D);
293 : INS_REGISTER_IMPL_BY_TEMP(OpType::BROADCAST, AivBroadcastMesh1D, InsV2BroadcastSoleExecutor, TopoMatchMesh, AivTempBroadcastMesh1D);
294 : INS_REGISTER_IMPL_BY_TEMP(OpType::BROADCAST, CcuBroadcastNHRMem2Mem1D, InsV2BroadcastSoleExecutor, TopoMatchMesh,
295 : CcuTempBroadcastNHRMem2Mem1D);
296 : #endif
297 : } // namespace Hccl
|