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 "coll_broadcast_mesh_aiv_for_910_93_executor.h"
12 :
13 : namespace hccl {
14 :
15 : constexpr u32 BUFFER_IDX_ONE = 1;
16 :
17 0 : CollBroadcastMeshAivFor91093Executor::CollBroadcastMeshAivFor91093Executor(const HcclDispatcher dispatcher,
18 0 : std::unique_ptr<TopoMatcher> &topoMatcher)
19 0 : : CollBroadcastExecutor(dispatcher, topoMatcher)
20 : {
21 0 : DMAReduceFlag_ = false;
22 0 : desc_.isAivMode = true;
23 0 : desc_.isAivCrossNode = true;
24 0 : }
25 :
26 0 : HcclResult CollBroadcastMeshAivFor91093Executor::CalcStreamNum(u32& streamNum)
27 : {
28 0 : streamNum = 0; // AIV通信不需要申请从流
29 0 : HCCL_INFO("[CollBroadcastMeshAivFor91093Executor][CalcStreamNum] tag[%s] streamNum[%u].",
30 : tag_.c_str(), streamNum);
31 0 : return HCCL_SUCCESS;
32 : }
33 :
34 0 : HcclResult CollBroadcastMeshAivFor91093Executor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
35 : {
36 0 : TransportMemType inputType = TransportMemType::CCL_INPUT;
37 0 : if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB) {
38 0 : inputType = TransportMemType::SCRATCH;
39 : }
40 0 : TransportMemType outputType = TransportMemType::AIV_OUTPUT;
41 :
42 0 : HCCL_INFO("[CollBroadcastMeshAivFor91093Executor][CalcTransportMemType] tag[%s] inputType[%d], outputType[%d].",
43 : tag_.c_str(), inputType, outputType);
44 0 : CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
45 0 : return HCCL_SUCCESS;
46 : }
47 :
48 0 : HcclResult CollBroadcastMeshAivFor91093Executor::CalcLevel0CommInfo(TransportMemType inputType,
49 : TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
50 : {
51 0 : CommParaInfo commCombinePara(COMM_COMBINE_ORDER, CommType::COMM_TAG_MESH);
52 0 : commCombinePara.meshSinglePlane = true;
53 0 : CHK_RET(CalcCommPlaneInfo(tag_, commCombinePara, opTransport[COMM_COMBINE_ORDER], inputType, outputType));
54 :
55 0 : LevelNSubCommTransport &commTransportLevel0 = opTransport[COMM_COMBINE_ORDER];
56 0 : for (u32 subCommIndex = 0; subCommIndex < commTransportLevel0.size(); subCommIndex++) {
57 0 : for (auto &transportRequest : commTransportLevel0[subCommIndex].transportRequests) {
58 0 : transportRequest.isUsedRdma = false;
59 : }
60 : }
61 0 : return HCCL_SUCCESS;
62 0 : }
63 0 : void CollBroadcastMeshAivFor91093Executor::ParseParam(const OpParam& param)
64 : {
65 0 : tag_ = param.tag;
66 0 : totalSize_ = param.DataDes.count * SIZE_TABLE[param.DataDes.dataType];
67 0 : }
68 :
69 0 : HcclResult CollBroadcastMeshAivFor91093Executor::CalcScratchMemSize(u64& scratchMemSize)
70 : {
71 0 : scratchMemSize = 0;
72 0 : if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB) {
73 0 : scratchMemSize = totalSize_;
74 : }
75 0 : HCCL_INFO("[%s]tag[%s] scratchMemSize[%llu]", __func__, tag_.c_str(), scratchMemSize);
76 0 : return HCCL_SUCCESS;
77 : }
78 :
79 0 : HcclResult CollBroadcastMeshAivFor91093Executor::CalNumBlocks(u32& numBlocks, u32 rankSize, u64 dataSize, HcclCMDType cmdType)
80 : {
81 : // Step1. 计算最佳块维度
82 0 : const u32 bestNumBlock = std::min(rankSize - 1, MAX_NUM_BLOCKS);
83 0 : const u32 minNumBlock = (rankSize + MAX_TARGET_NUM - 1) / MAX_TARGET_NUM;
84 :
85 : // 参数校验
86 0 : if (numBlocks_ < NUM_BLOCKS_FACTOR_TWO) {
87 0 : HCCL_ERROR("[CollBroadcastMeshAivFor91093Executor][CalNumBlocks] aivCore[%u] is invalid, at least need 2", numBlocks_);
88 0 : return HCCL_E_PARA;
89 : }
90 :
91 : // 选择合适的块数量
92 0 : numBlocks = numBlocks_ < bestNumBlock ? numBlocks_ : bestNumBlock;
93 :
94 : // 检查最小块数量限制
95 0 : if (numBlocks_ < minNumBlock) {
96 0 : HCCL_ERROR("[CollBroadcastMeshAivFor91093Executor][CalNumBlocks] limit[%u], at least[%u]", numBlocks_, minNumBlock);
97 0 : return HCCL_E_PARA;
98 : }
99 :
100 : // 记录日志
101 0 : HCCL_INFO("[CollBroadcastMeshAivFor91093Executor][CalNumBlocks] numBlock is set to [%u], limit[%u], best[%u]",
102 : numBlocks, numBlocks_, bestNumBlock);
103 0 : return HCCL_SUCCESS;
104 : }
105 :
106 0 : HcclResult CollBroadcastMeshAivFor91093Executor::PrepareCommInfoToDevice(AlgResourceResponse& algResource)
107 : {
108 0 : HCCL_INFO("[CollBroadcastMeshAivFor91093Executor][PrepareCommInfoToDevice]Broadcast aiv copy comm info to device.");
109 0 : CHK_RET(CopyAivCommInfoToDevice(COMM_COMBINE_ORDER, COMM_INDEX_0, algResource));
110 0 : return HCCL_SUCCESS;
111 : }
112 :
113 0 : HcclResult CollBroadcastMeshAivFor91093Executor::CopyAivCommInfoToDevice(const CommPlane levelIndex, const u32 subLevelIndex,
114 : AlgResourceResponse& algResource)
115 : {
116 0 : algResResp_ = &algResource;
117 0 : CHK_RET(CheckCommSize(levelIndex, subLevelIndex + 1));
118 0 : SubCommInfo commInfo = GetSubCommInfo(levelIndex, subLevelIndex);
119 0 : u32 localRank = commInfo.localRank;
120 0 : u32 localRankSize = commInfo.localRankSize;
121 :
122 0 : void* buffersInOut[MAX_RANK_SIZE_A3 * 2] = {};
123 0 : bool isOpbaseMode = GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE;
124 :
125 0 : for (u32 i = 0; i < localRankSize; i++) {
126 0 : u32 idx = (i << 1);
127 0 : if (i != localRank) {
128 0 : CHK_RET(commInfo.links[i]->GetRemoteMem(UserMemType::INPUT_MEM, &(buffersInOut[idx])));
129 0 : CHK_RET(commInfo.links[i]->GetRemoteMem(UserMemType::OUTPUT_MEM, &(buffersInOut[idx + 1])));
130 : } else {
131 0 : buffersInOut[idx] = isOpbaseMode ? algResource.cclInputMem.ptr() : algResource.scratchMem.ptr();
132 0 : buffersInOut[idx + 1] = algResource.aivOutputMem.ptr();
133 : }
134 : }
135 0 : constexpr u32 bufferNum = 2;
136 0 : CHK_RET(hrtMemSyncCopy(algResource.aivCommInfoMem.ptr(), sizeof(u64) * localRankSize * bufferNum,
137 : buffersInOut, sizeof(u64) * localRankSize * bufferNum, HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
138 0 : return HCCL_SUCCESS;
139 0 : }
140 :
141 0 : HcclResult CollBroadcastMeshAivFor91093Executor::Orchestrate(OpParam& param, AlgResourceResponse& algRes)
142 : {
143 0 : HcclUs startut = TIME_NOW();
144 0 : tag_ = param.tag;
145 0 : algResResp_ = &algRes;
146 :
147 0 : ExecMem execMem;
148 0 : execMem.count = param.DataDes.count;
149 0 : execMem.inputPtr = param.inputPtr;
150 0 : execMem.outputPtr = param.inputPtr; // broadcast使用一块内存
151 :
152 0 : execMem.inputMem = (workflowMode_ != HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE ? algRes.scratchMem : algRes.cclInputMem);
153 0 : execMem.outputMem = algRes.aivOutputMem;
154 :
155 0 : HcclResult ret = KernelRun(param, execMem);
156 :
157 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
158 : HCCL_ERROR("[CollBroadcastMeshAivFor91093Executor][Orchestrate]errNo[0x%016llx] tag[%s] excutor kernel run failed",
159 : HCCL_ERROR_CODE(ret), param.tag.c_str()), ret);
160 :
161 0 : HCCL_INFO("tag[%s], Broadcast executor orchestrate success, take time [%lld]us",
162 : param.tag.c_str(), DURATION_US(TIME_NOW() - startut));
163 0 : return HCCL_SUCCESS;
164 0 : }
165 :
166 0 : HcclResult CollBroadcastMeshAivFor91093Executor::KernelRun(const OpParam ¶m, ExecMem &execMem)
167 : {
168 0 : HCCL_INFO("[CollBroadcastMeshAivFor91093Executor][KernelRun]broadcast aiv enter.");
169 :
170 0 : CHK_RET(CheckCommSize(COMM_COMBINE_ORDER, COMM_INDEX_0 + 1));
171 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_COMBINE_ORDER, COMM_INDEX_0);
172 :
173 : void *buffersIn[MAX_RANK_SIZE];
174 : void *buffersOut[MAX_RANK_SIZE];
175 :
176 0 : u32 rootRank = 0;
177 0 : HcclResult ret = GetRankByUserRank(COMM_COMBINE_ORDER, COMM_INDEX_0, param.root, rootRank);
178 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
179 : HCCL_ERROR("[BroadCastOperator][CollBroadcastMeshAivFor91093Executor]invalid root[%u] to get userrank", param.root),
180 : ret);
181 :
182 0 : u32 localRank = level0CommInfo.localRank;
183 0 : u32 localRankSize = level0CommInfo.localRankSize;
184 0 : HCCL_DEBUG("[CollBroadcastMeshAivFor91093Executor][KernelRun] userRank [%u] localRank [%u]", topoAttr_.userRank, localRank);
185 :
186 0 : buffersIn[0] = execMem.inputMem.ptr();
187 0 : buffersOut[0] = execMem.outputMem.ptr();
188 0 : buffersOut[BUFFER_IDX_ONE] = algResResp_->aivCommInfoMem.ptr();
189 :
190 0 : bool isOpbase = (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
191 0 : AivOpArgs opArgs{HcclCMDType::HCCL_CMD_BROADCAST,
192 0 : execMem.inputPtr,
193 0 : execMem.outputPtr,
194 0 : execMem.count,
195 0 : param.DataDes.dataType,
196 0 : param.reduceType,
197 : rootRank,
198 0 : isOpbase};
199 0 : AivTopoArgs topoArgs { localRank, localRankSize, MAX_RANK_SIZE, 0, topoAttr_.serverNum, topoAttr_.deviceType, algoAttr_.identifier};
200 :
201 : u32 numBlocks;
202 0 : CHK_RET(CalNumBlocks(numBlocks, localRankSize));
203 0 : numBlocks_ = numBlocks;
204 :
205 : AivResourceArgs resourceArgs {
206 0 : param.tag, param.stream.ptr(), buffersIn, buffersOut, execMem.inputMem.size(), numBlocks_, param.aivTag
207 0 : };
208 0 : AivAlgArgs algArgs{};
209 0 : algArgs.execTimeOut = topoMatcher_->GetExecTimeOutConfig();
210 0 : algArgs.execTimeOutSet = true;
211 0 : algArgs.argsType = KernelArgsType::ARGS_TYPE_SIMPLE;
212 :
213 0 : struct AivProfilingInfo aivProfilingInfo;
214 0 : aivProfilingInfo.counter = opCounter_;
215 0 : ret = ExecuteKernelLaunch(opArgs, topoArgs, resourceArgs, algArgs, aivProfilingInfo);
216 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
217 : HCCL_ERROR("[CollBroadcastMeshAivFor91093Executor][KernelRun]broadcast aiv failed, return[%d]", ret),
218 : ret);
219 :
220 0 : ExtraArgs extraArgs;
221 0 : CHK_RET(SetOpCache(opArgs, topoArgs, resourceArgs, algArgs, extraArgs, aivProfilingInfo, true));
222 :
223 0 : HCCL_INFO("[CollBroadcastMeshAivFor91093Executor][KernelRun]broadcast aiv run success.");
224 0 : return HCCL_SUCCESS;
225 0 : }
226 :
227 : REGISTER_EXEC("BroadcastMeshAivFor91093Executor", BroadcastMeshAivFor91093, CollBroadcastMeshAivFor91093Executor);
228 :
229 : } // namespace hccl
|