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_all_reduce_mesh_aiv_for_910_93_executor.h"
12 :
13 : namespace hccl {
14 :
15 0 : CollAllReduceMeshAivFor91093Executor::CollAllReduceMeshAivFor91093Executor(const HcclDispatcher dispatcher,
16 0 : std::unique_ptr<TopoMatcher> &topoMatcher):
17 0 : CollAllReduceExecutor(dispatcher, topoMatcher)
18 : {
19 0 : DMAReduceFlag_ = false;
20 0 : desc_.isAivMode = true;
21 0 : desc_.isAivCrossNode = true;
22 0 : desc_.deterministic = 1;
23 0 : }
24 :
25 0 : HcclResult CollAllReduceMeshAivFor91093Executor::CalcStreamNum(u32& streamNum)
26 : {
27 0 : streamNum = 0; // AIV通信不需要申请从流
28 0 : HCCL_INFO("[CollAllReduceMeshAivFor91093Executor][CalcStreamNum] tag[%s] streamNum[%u].",
29 : tag_.c_str(), streamNum);
30 0 : return HCCL_SUCCESS;
31 : }
32 :
33 0 : HcclResult CollAllReduceMeshAivFor91093Executor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
34 : {
35 0 : TransportMemType inputType = TransportMemType::RESERVED;
36 0 : TransportMemType outputType = TransportMemType::RESERVED;
37 0 : CHK_RET(CalcTransportMemType(inputType, outputType));
38 0 : CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
39 0 : return HCCL_SUCCESS;
40 : }
41 :
42 0 : HcclResult CollAllReduceMeshAivFor91093Executor::CalcTransportMemType(TransportMemType &inputType,
43 : TransportMemType &outputType)
44 : {
45 0 : inputType = (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE ?
46 : TransportMemType::CCL_INPUT : TransportMemType::SCRATCH);
47 0 : outputType = TransportMemType::AIV_OUTPUT;
48 0 : HCCL_INFO("[CollAllReduceMeshAivFor91093Executor][CalcTransportMemType] tag[%s] inputType[%d],"
49 : " outputType[%d].", tag_.c_str(), inputType, outputType);
50 0 : return HCCL_SUCCESS;
51 : }
52 :
53 0 : HcclResult CollAllReduceMeshAivFor91093Executor::CalcLevel0CommInfo(TransportMemType inputType,
54 : TransportMemType outputType,
55 : std::vector<LevelNSubCommTransport>& opTransport)
56 : {
57 0 : CommParaInfo commCombinePara(COMM_COMBINE_ORDER, CommType::COMM_TAG_MESH);
58 0 : commCombinePara.meshSinglePlane = true;
59 0 : CHK_RET(CalcCommPlaneInfo(tag_, commCombinePara, opTransport[COMM_COMBINE_ORDER], inputType, outputType));
60 :
61 0 : LevelNSubCommTransport &commTransportLevel0 = opTransport[COMM_COMBINE_ORDER];
62 0 : for (u32 subCommIndex = 0; subCommIndex < commTransportLevel0.size(); subCommIndex++) {
63 0 : for (auto &transportRequest : commTransportLevel0[subCommIndex].transportRequests) {
64 0 : transportRequest.isUsedRdma = false;
65 : }
66 : }
67 0 : return HCCL_SUCCESS;
68 0 : }
69 :
70 0 : void CollAllReduceMeshAivFor91093Executor::ParseParam(const OpParam& param)
71 : {
72 0 : tag_ = param.tag;
73 :
74 0 : u64 reservedSize = (topoAttr_.userRankSize + 1) * (topoAttr_.userRankSize + 1) * SIZE_TABLE[param.DataDes.dataType];
75 :
76 0 : totalSize_ = BUFFER_DIVIDE * param.DataDes.count * SIZE_TABLE[param.DataDes.dataType] + reservedSize;
77 0 : }
78 :
79 0 : HcclResult CollAllReduceMeshAivFor91093Executor::CalcScratchMemSize(u64& scratchMemSize)
80 : {
81 0 : scratchMemSize = 0;
82 : // 确定性时图模式需要计算scratchMem
83 0 : scratchMemSize = totalSize_;
84 0 : HCCL_INFO("[%s]tag[%s] scratchMemSize[%llu]", __func__, tag_.c_str(), scratchMemSize);
85 0 : return HCCL_SUCCESS;
86 : }
87 :
88 0 : HcclResult CollAllReduceMeshAivFor91093Executor::CalNumBlocks(u32& numBlocks, u32 rankSize, u64 dataSize, HcclCMDType cmdType)
89 : {
90 : // Step1. Calculate the best block dimension
91 0 : u32 bestNumBlocks = (rankSize < MAX_NUM_BLOCKS ? rankSize : MAX_NUM_BLOCKS);
92 0 : u32 minNumBlocks = std::max((rankSize + MAX_TARGET_NUM - 1) / MAX_TARGET_NUM, NUM_BLOCKS_FACTOR_TWO);
93 :
94 : // Step2. Compare User Given numBlocks_ with bestNumBlocks
95 0 : numBlocks = bestNumBlocks;
96 0 : if (numBlocks_ < numBlocks) {
97 0 : if (numBlocks_ > rankSize) {
98 0 : numBlocks = numBlocks_ / rankSize * rankSize;
99 0 : minNumBlocks = (minNumBlocks + rankSize - 1) / rankSize * rankSize;
100 : } else {
101 0 : numBlocks = numBlocks_ / NUM_BLOCKS_FACTOR_TWO * NUM_BLOCKS_FACTOR_TWO;
102 0 : minNumBlocks = (minNumBlocks + NUM_BLOCKS_FACTOR_TWO - 1) / NUM_BLOCKS_FACTOR_TWO * NUM_BLOCKS_FACTOR_TWO;
103 : }
104 : }
105 :
106 0 : CHK_PRT_RET(numBlocks < minNumBlocks,
107 : HCCL_ERROR("[CollAllReduceMeshAivFor91093Executor][CalNumBlocks]aivCore[%u] is invalid, at least need [%u].",
108 : numBlocks_, minNumBlocks),
109 : HCCL_E_PARA);
110 :
111 0 : HCCL_INFO("[CollAllReduceMeshAivFor91093Executor][CalNumBlocks] numBlocks is set to [%u], limit[%u], recommanded[%u]",
112 : numBlocks, numBlocks_, bestNumBlocks);
113 0 : return HCCL_SUCCESS;
114 : }
115 :
116 0 : HcclResult CollAllReduceMeshAivFor91093Executor::GetAivExecParam(const OpParam& param, AlgResourceResponse& algRes, AivSuperKernelArgs &args)
117 : {
118 0 : HcclUs startut = TIME_NOW();
119 0 : tag_ = param.tag;
120 0 : algResResp_ = &algRes;
121 :
122 0 : HcclResult ret = HCCL_SUCCESS;
123 0 : ExecMem execMem;
124 0 : execMem.count = param.DataDes.count;
125 0 : execMem.inputPtr = param.inputPtr;
126 0 : execMem.outputPtr = param.outputPtr;
127 :
128 0 : execMem.inputMem = algRes.scratchMem;
129 :
130 0 : execMem.outputMem = algRes.aivOutputMem;
131 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_COMBINE_ORDER, COMM_INDEX_0);
132 :
133 0 : u32 localRank = level0CommInfo.localRank;
134 0 : u32 localRankSize = level0CommInfo.localRankSize;
135 0 : HCCL_DEBUG("[CollAllReduceMeshAivFor91093Executor][GetAivExecParam] userRank [%d] localRank [%d]",
136 : topoAttr_.userRank, localRank);
137 :
138 0 : args.buffersIn[0] = execMem.inputMem.ptr();
139 0 : args.buffersOut[0] = execMem.outputMem.ptr();
140 0 : constexpr u32 BUFFER_IDX_ONE = 1;
141 0 : args.buffersOut[BUFFER_IDX_ONE] = algRes.aivCommInfoMem.ptr(); // 通信域信息
142 :
143 0 : args.rank = localRank;
144 0 : args.rankSize = localRankSize;
145 0 : args.len = execMem.count;
146 0 : args.dataType = param.DataDes.dataType;
147 0 : args.unitSize = SIZE_TABLE[param.DataDes.dataType];
148 0 : args.reduceOp = param.reduceType;
149 :
150 0 : HCCL_INFO("SPK [CollAllReduceMeshAivFor91093Executor][GetAivExecParam], rank[%llu], rankSize[%llu], len[%llu],datatype[%llu], op[%llu]", args.rank, args.rankSize, args.len, args.dataType, args.reduceOp);
151 :
152 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
153 : HCCL_ERROR("[CollAllReduceMeshAivFor91093Executor][Orchestrate]errNo[0x%016llx] tag[%s] executor kernel "
154 : "run failed", HCCL_ERROR_CODE(ret), param.tag.c_str()), ret);
155 :
156 0 : HCCL_INFO("tag[%s], AllReduce executor getalgexecparam success, take time [%lld]us.",
157 : param.tag.c_str(), DURATION_US(TIME_NOW() - startut));
158 0 : return HCCL_SUCCESS;
159 0 : }
160 :
161 0 : HcclResult CollAllReduceMeshAivFor91093Executor::CopyAivCommInfoToDevice(const CommPlane levelIndex, const u32 subLevelIndex,
162 : AlgResourceResponse& algResource)
163 : {
164 0 : algResResp_ = &algResource;
165 0 : CHK_RET(CheckCommSize(levelIndex, subLevelIndex + 1));
166 0 : SubCommInfo commInfo = GetSubCommInfo(levelIndex, subLevelIndex);
167 0 : u32 localRank = commInfo.localRank;
168 0 : u32 localRankSize = commInfo.localRankSize;
169 :
170 0 : void* buffersInOut[MAX_RANK_SIZE_A3 * 2] = {};
171 0 : bool isOpbaseMode = GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE;
172 :
173 0 : for (u32 i = 0; i < localRankSize; i++) {
174 0 : u32 idx = (i << 1);
175 0 : if (i != localRank) {
176 0 : CHK_RET(commInfo.links[i]->GetRemoteMem(UserMemType::INPUT_MEM, &(buffersInOut[idx])));
177 0 : CHK_RET(commInfo.links[i]->GetRemoteMem(UserMemType::OUTPUT_MEM, &(buffersInOut[idx + 1])));
178 : } else {
179 0 : buffersInOut[idx] = isOpbaseMode ? algResource.cclInputMem.ptr() : algResource.scratchMem.ptr();
180 0 : buffersInOut[idx + 1] = algResource.aivOutputMem.ptr();
181 : }
182 : }
183 0 : const u32 bufferNum = 2;
184 0 : CHK_RET(hrtMemSyncCopy(algResource.aivCommInfoMem.ptr(), sizeof(u64) * localRankSize * bufferNum,
185 : buffersInOut, sizeof(u64) * localRankSize * bufferNum, HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
186 0 : return HCCL_SUCCESS;
187 0 : }
188 :
189 0 : HcclResult CollAllReduceMeshAivFor91093Executor::PrepareCommInfoToDevice(AlgResourceResponse& algResource)
190 : {
191 0 : HCCL_INFO("[CollAllReduceMeshAivFor91093Executor][PrepareCommInfoToDevice]"
192 : "allreduce aiv copy comm info to device.");
193 0 : CHK_RET(CopyAivCommInfoToDevice(COMM_COMBINE_ORDER, COMM_INDEX_0, algResource));
194 0 : return HCCL_SUCCESS;
195 : }
196 :
197 0 : HcclResult CollAllReduceMeshAivFor91093Executor::Orchestrate(OpParam& param, AlgResourceResponse& algRes)
198 : {
199 0 : HcclUs startut = TIME_NOW();
200 0 : tag_ = param.tag;
201 0 : algResResp_ = &algRes;
202 :
203 0 : ExecMem execMem;
204 0 : execMem.count = param.DataDes.count;
205 0 : execMem.inputPtr = param.inputPtr;
206 0 : execMem.outputPtr = param.outputPtr;
207 :
208 0 : execMem.inputMem = (workflowMode_ != HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE ?
209 0 : algRes.scratchMem : algRes.cclInputMem);
210 0 : execMem.outputMem = algRes.aivOutputMem;
211 0 : HcclResult ret = KernelRun(param, execMem);
212 :
213 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
214 : HCCL_ERROR("[CollAllReduceMeshAivFor91093Executor][Orchestrate]errNo[0x%016llx] tag[%s] executor kernel "
215 : "run failed", HCCL_ERROR_CODE(ret), param.tag.c_str()), ret);
216 :
217 0 : HCCL_INFO("tag[%s], allreduce executor orchestrate success, take time [%lld]us.",
218 : param.tag.c_str(), DURATION_US(TIME_NOW() - startut));
219 0 : return HCCL_SUCCESS;
220 0 : }
221 :
222 0 : HcclResult CollAllReduceMeshAivFor91093Executor::KernelRun(const OpParam ¶m, ExecMem &execMem)
223 : {
224 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[%s] allreduce aiv enter.", __func__);
225 :
226 0 : CHK_RET(CheckCommSize(COMM_COMBINE_ORDER, COMM_INDEX_0 + 1));
227 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_COMBINE_ORDER, COMM_INDEX_0);
228 :
229 : void *buffersIn[MAX_RANK_SIZE];
230 : void *buffersOut[MAX_RANK_SIZE];
231 :
232 0 : u32 localRank = level0CommInfo.localRank;
233 0 : u32 localRankSize = level0CommInfo.localRankSize;
234 0 : HCCL_DEBUG("[CollAllReduceMeshAivFor91093Executor][KernelRun] userRank [%d] localRank [%d]",
235 : topoAttr_.userRank, localRank);
236 :
237 0 : buffersIn[0] = execMem.inputMem.ptr();
238 0 : buffersOut[0] = execMem.outputMem.ptr();
239 0 : constexpr u32 BUFFER_IDX_ONE = 1;
240 0 : buffersOut[BUFFER_IDX_ONE] = algResResp_->aivCommInfoMem.ptr(); // 通信域信息
241 :
242 0 : bool isOpbase = GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE;
243 :
244 0 : AivOpArgs opArgs {
245 0 : HcclCMDType::HCCL_CMD_ALLREDUCE, execMem.inputPtr, execMem.outputPtr, execMem.count,
246 0 : param.DataDes.dataType, param.reduceType, 0, isOpbase
247 0 : };
248 0 : AivTopoArgs topoArgs { localRank, localRankSize, MAX_RANK_SIZE, 0, topoAttr_.serverNum, topoAttr_.deviceType, algoAttr_.identifier};
249 :
250 : u32 numBlocks;
251 0 : CHK_PRT_RET(CalNumBlocks(numBlocks, localRankSize, opArgs.count * SIZE_TABLE[opArgs.dataType]) != HCCL_SUCCESS,
252 : HCCL_ERROR("[%s] CalNumBlocks failed", __func__),
253 : HCCL_E_PARA);
254 0 : numBlocks_ = numBlocks;
255 :
256 : AivResourceArgs resourceArgs {
257 0 : param.tag, param.stream.ptr(), buffersIn, buffersOut, execMem.inputMem.size(), numBlocks_, param.aivTag
258 0 : };
259 0 : AivAlgArgs algArgs {};
260 0 : algArgs.execTimeOut = topoMatcher_->GetExecTimeOutConfig();
261 0 : algArgs.execTimeOutSet = true;
262 0 : algArgs.argsType = KernelArgsType::ARGS_TYPE_SIMPLE;
263 0 : struct AivProfilingInfo aivProfilingInfo;
264 0 : if (topoMatcher_->GetDeterministicConfig() != DETERMINISTIC_DISABLE){
265 0 : algArgs.deterministic = 1;
266 : }
267 0 : aivProfilingInfo.counter = opCounter_;
268 :
269 0 : HcclResult ret = ExecuteKernelLaunch(opArgs, topoArgs, resourceArgs, algArgs, aivProfilingInfo);
270 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
271 : HCCL_ERROR("[CollAllReduceMeshAivFor91093Executor][KernelRun]allreduce aiv failed, return[%d]", ret),
272 : ret);
273 :
274 0 : ExtraArgs extraArgs;
275 0 : CHK_RET(SetOpCache(opArgs, topoArgs, resourceArgs, algArgs, extraArgs, aivProfilingInfo, true));
276 :
277 0 : HCCL_INFO("[CollAllReduceMeshAivFor91093Executor][KernelRun]allreduce aiv run success.");
278 0 : return HCCL_SUCCESS;
279 0 : }
280 :
281 : REGISTER_EXEC("AllReduceMeshAivFor91093Executor", AllReduceMeshAivFor91093, CollAllReduceMeshAivFor91093Executor);
282 :
283 : } // namespace hccl
|