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 <algorithm>
12 : #include <list>
13 : #include <vector>
14 : #include <string>
15 : #include <securec.h>
16 : #include <hccl/hccl_types.h>
17 : #include "hcom_private.h"
18 : #include "config.h"
19 : #include "externalinput_pub.h"
20 : #include "workflow_pub.h"
21 : #include "gradient_segment.h"
22 : #include "hccl/base.h"
23 : #include "rank_consistentcy_checker.h"
24 : #include "param_check_pub.h"
25 : #include "comm_configer.h"
26 :
27 : #include "../common/src/topo/topoinfo_detect.h"
28 : #include "profiling_manager.h"
29 : #include "../op_base/src/op_base.h"
30 : #include "adapter_rts_common.h"
31 : #include "adapter_prof.h"
32 : #include "topoinfo_ranktableParser_pub.h"
33 : #include "hccl_communicator.h"
34 : #include "hccl/hcom.h"
35 : #include "topoinfo_ranktableOffline.h"
36 : #include "mmpa_api.h"
37 : #include "hccl_tbe_task.h"
38 : #include "hcom_private_v2.h"
39 : #include "comm_topo_desc.h"
40 : #include "hcom_common.h"
41 : #include "hcom_pub.h"
42 :
43 : using namespace std;
44 : using namespace hccl;
45 :
46 0 : HcclResult CallMsprofReportHostApi(hccl::hcclComm* hcclComm, HcclCMDType cmdType, uint64_t beginTime, u64 count,
47 : HcclDataType dataType)
48 : {
49 0 : auto &profilingManager = hccl::ProfilingManager::Instance();
50 0 : AlgType algType;
51 0 : if(cmdType == HcclCMDType::HCCL_CMD_RECEIVE || cmdType == HcclCMDType::HCCL_CMD_SEND){
52 0 : algType.algoLevel0 = AlgTypeLevel0::ALG_LEVEL0_RESERVED;
53 0 : algType.algoLevel1 = AlgTypeLevel1::ALG_LEVEL1_RESERVED;
54 : } else {
55 0 : CHK_RET(hcclComm->GetAlgType(algType, cmdType));
56 : }
57 0 : uint64_t groupName = hrtMsprofGetHashId(hcclComm->GetIdentifier().c_str(), hcclComm->GetIdentifier().length());
58 0 : CHK_RET(profilingManager.CallMsprofReportHostApi(cmdType, beginTime, count, dataType, algType, groupName));
59 0 : hcclComm->SetAivCoreLimit(0);
60 0 : HCCL_DEBUG("CallMsprofReportHostApi success, cmdType[%d], count[%llu], dataType[%d], algType[%d], groupName[%llu]",
61 : cmdType, count, dataType, algType.algoLevel0, groupName);
62 0 : return HCCL_SUCCESS;
63 : }
64 :
65 : HcclResult HcomCheckInitClusterInfo(const char *rankTableM, const char *identify);
66 : HcclResult HcomFlushBackloggedGroups();
67 : HcclResult HcomCollRemotePairedParaCheck(const HcomRemoteOperationParams ¶ms);
68 :
69 0 : HcclResult HcomInit(const char *rankTableM, const char *identify, WorkMode commWorkMode)
70 : {
71 0 : HcclResult ret = HCCL_SUCCESS;
72 0 : bool &isAutoTuneModeOpen = HcomGetCtxAutoTuneMode();
73 0 : isAutoTuneModeOpen = false;
74 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
75 :
76 : /*--------------入参合法性检测---------------------*/
77 0 : CHK_PTR_NULL(rankTableM);
78 0 : CHK_PTR_NULL(identify);
79 :
80 : /* 防止重复调用初始化 */
81 0 : CHK_PRT_RET((hcomInfo.pComm != nullptr),
82 : HCCL_ERROR("[Init][Result]errNo[0x%016llx] identify[%s], "\
83 : "multiple initialization is not supported", HCOM_ERROR_CODE(HCCL_E_UNAVAIL), identify), HCCL_E_UNAVAIL);
84 :
85 : /* --------------初始化------------------------- */
86 0 : bool errorFlag = false;
87 0 : s32 logicDevId = 0;
88 0 : hcomInfo.params.commWorkMode = commWorkMode;
89 : do {
90 0 : ret = InitHcomMiscInfo(hcomInfo.params, rankTableM);
91 0 : CHK_PRT_BREAK(ret != HCCL_SUCCESS, HCCL_ERROR("[Init][Result]errNo[0x%016llx] init other Info.",
92 : HCOM_ERROR_CODE(ret)), errorFlag = true);
93 :
94 : DevType deviceType;
95 0 : if (commWorkMode != HCCL_MODE_SCHED_OS) {
96 0 : CHK_PRT_BREAK(hrtGetDevice(&logicDevId) != HCCL_SUCCESS, , errorFlag = true);
97 0 : CHK_RET(hrtGetDeviceType(deviceType));
98 : // 为适配12包,做此修改
99 0 : (void)HcomCheckrtMemcpyAddrAsync(identify);
100 : } else {
101 0 : deviceType = DevType::DEV_TYPE_NOSOC;
102 : }
103 0 : ret = CfgGetClusterInfo(rankTableM, identify, hcomInfo.params, hcomInfo.rankTable,
104 0 : GetExternalInputInterSuperPodRetryEnable(), deviceType);
105 0 : CHK_PRT_BREAK(ret != HCCL_SUCCESS, HCCL_ERROR("[Init][Result]errNo[0x%016llx] cfg get ranktable[%p] info "\
106 : "error: identify[%s]", HCOM_ERROR_CODE(ret), rankTableM, identify), errorFlag = true);
107 :
108 : // HCCL_MODE_SCHED_OS仅在910A等同构集合通信场景才存在,此处仅希望异构资源池化场景进入
109 0 : if (commWorkMode != HCCL_MODE_SCHED_OS) {
110 : /*
111 : 此case仅希望310 soc形态才能进入(此时有reacource.json来配置通信协议)
112 : 但是310有板卡形态有可能进入(无reacource.json),因此设定serverNum!=1的条件
113 : 因为310板卡形态跑大模型切分(AllReduce)当前都是单机(serverNum=1),因此无需通信协议解析
114 : */
115 0 : if (hcomInfo.rankTable.serverNum != SINGLE_SERVER_NUM &&
116 0 : (deviceType == DevType::DEV_TYPE_310P3 || deviceType == DevType::DEV_TYPE_310P1)) {
117 0 : CHK_RET(InitExternalInputHeterog());
118 : }
119 : }
120 :
121 : const char *group;
122 :
123 0 : hcomInfo.pComm.reset(new (std::nothrow) hccl::hcclComm(0, 0, HCCL_WORLD_GROUP));
124 :
125 0 : CHK_PRT_RET(hcomInfo.pComm == nullptr,
126 : HCCL_ERROR("[Init][Result]hcomInfo.pComm is null,\
127 : create failed"),
128 : HCCL_E_PTR);
129 0 : CommConfig commConfig(HCCL_WORLD_GROUP);
130 0 : ret = hcomInfo.pComm->init(hcomInfo.params, commConfig, hcomInfo.rankTable);
131 0 : CHK_PRT_BREAK(ret != HCCL_SUCCESS,
132 : HCCL_ERROR("[Init][Result]errNo[0x%016llx] hcclComm init error", HCOM_ERROR_CODE(ret)),
133 : errorFlag = true);
134 :
135 0 : group = hcomInfo.pComm->GetIdentifier().c_str();
136 :
137 0 : ret = ShowRanktableConfigInfo(hcomInfo.cloudFlag, hcomInfo.params, hcomInfo.rankTable);
138 0 : CHK_PRT_BREAK(ret != HCCL_SUCCESS, HCCL_ERROR("[Init][Result]errNo[0x%016llx] put ranktable info error",
139 : HCOM_ERROR_CODE(ret)), errorFlag = true);
140 0 : if (commWorkMode != HCCL_MODE_SCHED_OS) {
141 0 : ret = InitWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB);
142 0 : CHK_PRT_BREAK(ret != HCCL_SUCCESS, HCCL_ERROR("[Init][Result]errNo[0x%016llx] init work flow mode error",
143 : HCCL_ERROR_CODE(ret)), errorFlag = true);
144 : }
145 :
146 0 : ret = HcomFlushBackloggedGroups();
147 0 : CHK_PRT_BREAK(ret != HCCL_SUCCESS, HCCL_ERROR("[Init][Result]errNo[0x%016llx] create backlogged group failed",
148 : HCOM_ERROR_CODE(ret)), errorFlag = true);
149 :
150 0 : ret = HcomSetGroupTopoInfo(hcomInfo.pComm->GetIdentifier().c_str(), hcomInfo.rankTable.rankNum);
151 :
152 0 : CHK_PRT_BREAK(ret != HCCL_SUCCESS, HCCL_ERROR("[Init][Result]errNo[0x%016llx] SetGroupTopoInfo error, "\
153 : "group[%s]", HCOM_ERROR_CODE(ret), group), errorFlag = true);
154 0 : } while (0);
155 :
156 0 : if (errorFlag) {
157 0 : HCCL_ERROR("[Init][Result]hcom init failed, rankNum[%u], rank[%u], server[%s], device[%d], return[0x%016llx]",
158 : hcomInfo.rankTable.rankNum, hcomInfo.params.rank, hcomInfo.params.serverId.c_str(),
159 : logicDevId, HCOM_ERROR_CODE(ret));
160 0 : (void)HcomDestroy();
161 0 : return ret;
162 : }
163 0 : return HCCL_SUCCESS;
164 : }
165 :
166 0 : HcclResult HcomInitByString(const char *rankTableM, const char *identify, WorkMode commWorkMode, HcomInitConfig *initConfig)
167 : {
168 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
169 0 : CHK_PTR_NULL(rankTableM);
170 0 : CHK_PTR_NULL(identify);
171 :
172 0 : HCCLV2_FUNC_RUN(
173 : [&]() -> HcclResult {
174 : CHK_RET(HcomInitByStringV2(rankTableM, identify));
175 : s32 myRank = std::atoi(identify);
176 : Hccl::RankId rank = static_cast<Hccl::RankId>(myRank);
177 : void *commV2 = nullptr;
178 : CHK_RET(HcomGetCommV2(&commV2));
179 : CHK_RET(HcomInitCollComm(rank, &commV2, hcomInfo.pComm));
180 : u32 rankNum = 0;
181 : CHK_RET(HcomGetRankSize(HCCL_WORLD_GROUP, &rankNum));
182 : CHK_RET(HcomSetGroupTopoInfo(HCCL_WORLD_GROUP, rankNum));
183 : return HCCL_SUCCESS;
184 : }());
185 :
186 0 : if (initConfig != nullptr) {
187 : DevType devType;
188 0 : CHK_RET(hrtGetDeviceType(devType));
189 :
190 0 : CHK_RET(HcomSetAlgorithm(initConfig->algo));
191 0 : CHK_RET(HcomSetExecTimeOut(initConfig->execTimeOut));
192 0 : if (devType != DevType::DEV_TYPE_910_93) {
193 0 : CHK_RET(HcomSetDeterministic(initConfig->deterministic));
194 : } else {
195 0 : HCCL_WARNING("ParserHcclDeterministic: device type is 910_93, use default setting");
196 : }
197 : }
198 :
199 0 : HcclUs startut = TIME_NOW();
200 0 : HcclResult ret = HCCL_SUCCESS;
201 0 : CHK_RET(InitExternalInput());
202 0 : CHK_RET(InitEnvConfig());
203 0 : CHK_RET(HcomCheckInitClusterInfo(rankTableM, identify));
204 0 : HCCL_RUN_INFO("Entry-HcomInitByString, rankTableM[%s], identify[%s], commWorkMode[%d]", rankTableM, identify, commWorkMode);
205 :
206 0 : ret = HcomInit(rankTableM, identify, commWorkMode);
207 :
208 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[HcomInitByString]errNo[0x%016llx] rankTable[%p] identify[%s] "\
209 : "hcom init failed.", HCCL_ERROR_CODE(ret), rankTableM, identify), ret);
210 0 : hcomInfo.isHcomInit = true;
211 :
212 : /* 关键状态记录 */
213 0 : HCCL_RUN_INFO("[HCCL_TRACE]hcom init by string success,take time [%lld]us, rankTableAddr[%p], rankNum[%u], "\
214 : "rank[%u]", DURATION_US(TIME_NOW() - startut), rankTableM, hcomInfo.rankTable.rankNum, hcomInfo.params.rank);
215 0 : return HCCL_SUCCESS;
216 : }
217 :
218 0 : HcclResult GenerateRootInfo(HcclRootHandle &rootInfo)
219 : {
220 0 : std::string identifier = "hccl_world_group";
221 0 : CHK_PRT_RET((identifier.length() >= ROOTINFO_INDENTIFIER_MAX_LENGTH),
222 : HCCL_ERROR("[Setup][Server]rootinfo identifier len[%zu] is invalid.", identifier.length()), HCCL_E_INTERNAL);
223 0 : s32 sret = memcpy_s(&rootInfo.identifier[0], sizeof(rootInfo.identifier), identifier.c_str(),
224 0 : (identifier.length() + 1));
225 0 : CHK_PRT_RET(sret != EOK, HCCL_ERROR("[Setup][Server]errNo[0x%016llx] memcpy failed. ret[%d], params:"\
226 : "destMaxSize[%zu],count[%zu]", HCOM_ERROR_CODE(HCCL_E_MEMORY), sret, sizeof(rootInfo.identifier),
227 : (identifier.length() + 1)), HCCL_E_MEMORY);
228 :
229 0 : s32 sRet = strncpy_s(rootInfo.ip, sizeof(rootInfo.ip), GetExternalInputMasterInfo().serverIp.GetReadableIP(),
230 0 : strlen(GetExternalInputMasterInfo().serverIp.GetReadableIP()));
231 0 : CHK_PRT_RET(sRet != EOK, HCCL_ERROR("[Setup][Server]str copy fail. return[%d]", sRet), HCCL_E_INTERNAL);
232 :
233 0 : rootInfo.port = GetExternalInputMasterInfo().port;
234 0 : rootInfo.nicDeploy = NICDeployment::NIC_DEPLOYMENT_DEVICE;
235 0 : return HCCL_SUCCESS;
236 0 : }
237 :
238 0 : HcclResult HcomGenerteRanktable(std::string &rankTableM, std::string &rankId)
239 : {
240 0 : s32 logicDevId = 0;
241 0 : u32 devPhyId = 0;
242 0 : CHK_RET(hrtGetDevice(&logicDevId));
243 0 : CHK_RET(hrtGetDevicePhyIdByIndex(static_cast<u32>(logicDevId), devPhyId));
244 :
245 : // true代表感知白名单disable配置
246 0 : CHK_RET(HcclNetInit(NICDeployment::NIC_DEPLOYMENT_HOST, devPhyId, logicDevId, true));
247 :
248 0 : HcclIpAddress localHostIp;
249 0 : CHK_RET(GetLocalHostIP(localHostIp, devPhyId));
250 : HcclRootHandle rootHandle;
251 0 : CHK_RET(GenerateRootInfo(rootHandle));
252 :
253 0 : std::shared_ptr<TopoInfoDetect> topoDetectAgent;
254 0 : EXCEPTION_CATCH(topoDetectAgent = std::make_shared<TopoInfoDetect>(), return HCCL_E_PTR);
255 0 : std::shared_ptr<TopoInfoDetect> topoDetectServer;
256 0 : EXCEPTION_CATCH(topoDetectServer = std::make_shared<TopoInfoDetect>(), return HCCL_E_PTR);
257 :
258 : DevType devType;
259 0 : CHK_RET(hrtGetDeviceType(devType));
260 0 : bool retryEnable = devType == DevType::DEV_TYPE_910_93 &&
261 0 : (GetExternalInputInterServerRetryEnable() || GetExternalInputInterSuperPodRetryEnable());
262 0 : HCCL_INFO("[HcomGenerteRanktable] retryEnable is [%d]", retryEnable);
263 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
264 :
265 0 : bool isRoot = (localHostIp == GetExternalInputMasterInfo().serverIp &&
266 0 : logicDevId == static_cast<s32>(GetExternalInputMasterInfo().serverDeviceId));
267 0 : if (isRoot) {
268 : HcclResult ret =
269 0 : topoDetectServer->SetupServerByMasterInfo(localHostIp, GetExternalInputMasterInfo().port, rootHandle);
270 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[%s][%s]%s failed, localHostIp[%s] and localhostPort[%u] ret[%u]",
271 : LOG_KEYWORDS_INIT_GROUP.c_str(), LOG_KEYWORDS_RANKTABLE_DETECT.c_str(), __func__,
272 : localHostIp.GetReadableAddress(), GetExternalInputMasterInfo().port, ret), ret);
273 : }
274 :
275 0 : CHK_PRT_RET(topoDetectAgent->SetupAgentByMasterInfo(localHostIp, rootHandle) != HCCL_SUCCESS,
276 : HCCL_ERROR("[Init][CommMasterInfo]setup topo detect error"), HCCL_E_INTERNAL);
277 0 : RankTable_t rankTable;
278 0 : CHK_PRT_RET(topoDetectAgent->GetCluterInfo(rankTable) != HCCL_SUCCESS,
279 : HCCL_ERROR("[Init][CommMasterInfo]GetCluterInfo error"), HCCL_E_INTERNAL);
280 0 : u32 rankIdNum = 0;
281 0 : CHK_PRT_RET(topoDetectAgent->GetRankId(rankIdNum) != HCCL_SUCCESS,
282 : HCCL_ERROR("[Init][CommMasterInfo]topoDetectAgent error"), HCCL_E_INTERNAL);
283 :
284 0 : CHK_RET(topoDetectAgent->TransformRankTableStr(rankTable, rankTableM));
285 0 : rankId = to_string(rankIdNum);
286 0 : CHK_PRT_RET(topoDetectAgent->WaitComplete(rootHandle) != HCCL_SUCCESS,
287 : HCCL_ERROR("[Init][CommMasterInfo]topoDetectAgent teardown fail"), HCCL_E_INTERNAL);
288 :
289 0 : CHK_PRT_RET(topoDetectAgent->GetAgentListenSocket(hcomInfo.params.commPortConfig) != HCCL_SUCCESS,
290 : HCCL_ERROR("[Init][CommMasterInfo]HcclGetCommListenSockets failed."), HCCL_E_INTERNAL);
291 :
292 0 : if (retryEnable) {
293 0 : hcomInfo.params.commConnections.isRoot = isRoot;
294 0 : if (isRoot) {
295 0 : hcomInfo.hcclCommTopoInfoDetectServer.insert({rootHandle.identifier, topoDetectServer});
296 0 : topoDetectServer->GetServerConnections(hcomInfo.params.commConnections.serverConnections);
297 : }
298 0 : hcomInfo.hcclCommTopoInfoDetectAgent.insert({rootHandle.identifier, topoDetectAgent});
299 0 : topoDetectAgent->GetAgentConnection(hcomInfo.params.commConnections.agentConnection);
300 : }
301 :
302 0 : return HCCL_SUCCESS;
303 0 : }
304 :
305 0 : HcclResult HcomInitByMasterInfo(const char *masterIp, const char *masterPort, const char *masterDeviceId,
306 : const char *rankSize, const char *rankIp, HcomInitConfig *initConfig)
307 : {
308 0 : CHK_RET(SetMasterInfo(masterIp, masterPort, masterDeviceId, rankSize, rankIp));
309 0 : HcclUs startut = TIME_NOW();
310 0 : HcclResult ret = HCCL_SUCCESS;
311 :
312 0 : if (initConfig != nullptr) {
313 : DevType devType;
314 0 : CHK_RET(hrtGetDeviceType(devType));
315 :
316 0 : CHK_RET(HcomSetAlgorithm(initConfig->algo));
317 0 : CHK_RET(HcomSetExecTimeOut(initConfig->execTimeOut));
318 0 : if (devType != DevType::DEV_TYPE_910_93) {
319 0 : CHK_RET(HcomSetDeterministic(initConfig->deterministic));
320 : } else {
321 0 : HCCL_WARNING("ParserHcclDeterministic: device type is 910_93, use default setting");
322 : }
323 : }
324 :
325 0 : s32 logicDevId = 0;
326 0 : CHK_RET(hrtGetDevice(&logicDevId));
327 : // 读取rankTable文件到内存
328 0 : std::string rankTableM;
329 0 : std::string identify;
330 0 : HCCL_RUN_INFO("Entry-HcomInitByMasterInfo:masterIp[%s], masterPort[%s], master device id[%s], rankSize[%s], rankIp[%s], "
331 : "deviceId[%d]", masterIp, masterPort, masterDeviceId, rankSize, rankIp, logicDevId);
332 :
333 0 : CHK_RET(InitExternalInput()); // 生成ranktable前需要提前感知部分配置
334 0 : CHK_RET(InitEnvConfig());
335 0 : ret = HcomGenerteRanktable(rankTableM, identify);
336 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[HcomInitByMasterInfo]errNo[0x%016llx] masterIp[%s], masterPort[%s], "
337 : "masterDeviceId[%s] rankSize[%s] deviceId[%d] load rankTable error.", HCCL_ERROR_CODE(HCCL_E_INTERNAL),
338 : masterIp, masterPort, masterDeviceId, rankSize, logicDevId), HCCL_E_INTERNAL);
339 0 : CHK_RET(HcomCheckInitClusterInfo(rankTableM.c_str(), identify.c_str()));
340 :
341 : // 调用初始化接口
342 0 : ret = HcomInit(rankTableM.c_str(), identify.c_str());
343 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[HcomInitByMasterInfo]errNo[0x%016llx] identify[%s] "
344 : "hcom init failed.", HCCL_ERROR_CODE(ret), identify.c_str()), ret);
345 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
346 0 : hcomInfo.isHcomInit = true;
347 : /* 关键状态记录 */
348 0 : HCCL_RUN_INFO("[HCCL_TRACE]hcom init by masterinfo success,take time [%lld]us, rankNum[%u], rank[%u], "\
349 : "server[%s], device[%d]", DURATION_US(TIME_NOW() - startut), hcomInfo.rankTable.rankNum,
350 : hcomInfo.params.rank, hcomInfo.params.serverId.c_str(), hcomInfo.params.logicDevId);
351 0 : return HCCL_SUCCESS;
352 0 : }
353 :
354 0 : HcclResult HcomSetProfilingMode(HcomProfilingMode profilingMode, const char *profilingOption)
355 : {
356 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
357 0 : HCCL_INFO("Set profiling option[%s].", profilingOption);
358 0 : hcomInfo.params.profilingMode = profilingMode;
359 0 : hcomInfo.params.profilingOption = profilingOption;
360 0 : return HCCL_SUCCESS;
361 : }
362 :
363 0 : HcclResult HcomDestroyOneDeviceHeterog(HcomInfo &hcomInfo)
364 : {
365 0 : return HCCL_SUCCESS;
366 : }
367 :
368 0 : HcclResult HcomAllGather(const char *tag, void *inputPtr, void *outputPtr, u64 inputCount,
369 : HcclDataType dataType, const char *group, rtStream_t stream)
370 : {
371 : HcclResult ret;
372 0 : HcclUs startut = TIME_NOW();
373 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
374 :
375 0 : s32 deviceLogicId = 0;
376 0 : CHK_RET(hrtGetDevice(&deviceLogicId));
377 :
378 0 : CHK_PRT_RET(inputCount == 0, HCCL_WARNING("input count is 0, return AllGather success"), HCCL_SUCCESS);
379 : // 参数合法性校验
380 :
381 0 : RPT_INPUT_ERR(inputPtr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
382 : std::vector<std::string>({"HcomAllGather", "nullptr", "inputPtr", "non-null pointer"}));
383 0 : CHK_PTR_NULL(inputPtr);
384 0 : RPT_INPUT_ERR(outputPtr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
385 : std::vector<std::string>({"HcomAllGather", "nullptr", "outputPtr", "non-null pointer"}));
386 0 : CHK_PTR_NULL(outputPtr);
387 0 : RPT_INPUT_ERR(stream == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
388 : std::vector<std::string>({"HcomAllGather", "nullptr", "stream", "non-null pointer"}));
389 0 : CHK_PTR_NULL(stream);
390 :
391 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
392 0 : s32 streamId = 0;
393 0 : ret = hrtGetStreamId(stream, streamId);
394 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[AllGather][Result]Call hrtGetStreamId error[%d].",
395 : ret), HCCL_E_RUNTIME);
396 : /* 接口交互信息日志 */
397 0 : HCCL_USER_CRITICAL_LOG("Entry-HcomAllGather:tag[%s], inputPtr[%p], outputPtr[%p], inputCount[%llu], dataType[%s], "\
398 : "group[%s], streamId[%d], deviceLogicId[%d]", tag, inputPtr, outputPtr, inputCount, GetDataTypeEnumStr(dataType).c_str(),
399 : strGroup.c_str(), streamId, deviceLogicId);
400 0 : CHK_RET(PrintMemoryAttr(inputPtr));
401 0 : CHK_RET(PrintMemoryAttr(outputPtr));
402 :
403 : // HcomAllGatherV2
404 0 : HCCLV2_FUNC_RUN(HcomAllGatherV2(tag, inputPtr, outputPtr, inputCount, dataType, group, stream));
405 :
406 0 : CHK_RET(HcomCheckOpParam(tag, inputCount, dataType, group, stream));
407 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
408 0 : CHK_RET(HcomGetCommByGroup(strGroup.c_str(), hcclComm));
409 0 : ret = hcclComm->AllGather(tag, inputPtr, outputPtr, inputCount, dataType, stream);
410 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[AllGather][Result]errNo[0x%016llx] hcclComm AllGather error, "\
411 : "tag[%s], input_ptr[%p], output_ptr[%p], count[%llu], data_type[%s]", HCOM_ERROR_CODE(ret),
412 : tag, inputPtr, outputPtr, inputCount, GetDataTypeEnumStr(dataType).c_str()), ret);
413 0 : CHK_RET(CallMsprofReportHostApi(hcclComm.get(), HcclCMDType::HCCL_CMD_ALLGATHER, beginTime, inputCount, dataType));
414 : /* 关键状态记录 */
415 0 : HCCL_RUN_INFO("hcom AllGather success,take time [%lld]us, tag[%s], input_ptr[%p], output_ptr[%p], "\
416 : "count[%llu], data_type[%s]", DURATION_US(TIME_NOW() - startut), tag, inputPtr, outputPtr,
417 : inputCount, GetDataTypeEnumStr(dataType).c_str());
418 :
419 0 : return HCCL_SUCCESS;
420 0 : }
421 :
422 0 : HcclResult HcomAllGatherV(const char *tag, const void *sendBuf, u64 sendCount, const void *recvBuf,
423 : const void *recvCounts, const void *rdispls, HcclDataType dataType, const char *group, rtStream_t stream)
424 : {
425 : HcclResult ret;
426 0 : HcclUs startut = TIME_NOW();
427 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
428 :
429 0 : s32 deviceLogicId = 0;
430 0 : CHK_RET(hrtGetDevice(&deviceLogicId));
431 :
432 : // 参数合法性校验
433 0 : RPT_INPUT_ERR(recvCounts == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "value"}),\
434 : std::vector<std::string>({"HcomAllGatherV", "nullptr", "recvCounts", "non-null pointer"}));
435 0 : CHK_PTR_NULL(recvCounts);
436 0 : RPT_INPUT_ERR(rdispls == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "value"}),\
437 : std::vector<std::string>({"HcomAllGatherV", "nullptr", "rdispls", "non-null pointer"}));
438 0 : CHK_PTR_NULL(rdispls);
439 0 : RPT_INPUT_ERR(stream == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "value"}),\
440 : std::vector<std::string>({"HcomAllGatherV", "nullptr", "stream", "non-null pointer"}));
441 0 : CHK_PTR_NULL(stream);
442 0 : if (UNLIKELY(sendCount > 0 && sendBuf == nullptr)) {
443 0 : RPT_INPUT_ERR(true, "EI0003",\
444 : std::vector<std::string>({"ccl_op", "value", "parameter", "value"}),\
445 : std::vector<std::string>({"HcomAllGatherV", "nullptr", "sendBuf", "non-null pointer"}));
446 0 : CHK_PTR_NULL(sendBuf);
447 : }
448 :
449 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
450 0 : s32 streamId = 0;
451 0 : ret = hrtGetStreamId(stream, streamId);
452 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[AllGatherV][Result]Call hrtGetStreamId error[%d].",
453 : ret), HCCL_E_RUNTIME);
454 : /* 接口交互信息日志 */
455 0 : HCCL_USER_CRITICAL_LOG("Entry-HcomAllGatherV:tag[%s], inputPtr[%p], outputPtr[%p], sendCount[%llu], dataType[%s], "\
456 : "recvCounts[%p], rdispls[%p], group[%s], streamId[%d], deviceLogicId[%d]", tag, sendBuf, recvBuf, sendCount,
457 : GetDataTypeEnumStr(dataType).c_str(), recvCounts, rdispls, strGroup.c_str(), streamId, deviceLogicId);
458 0 : if (sendBuf != nullptr) {
459 0 : CHK_RET(PrintMemoryAttr(sendBuf));
460 : }
461 0 : if (recvBuf != nullptr){
462 0 : CHK_RET(PrintMemoryAttr(recvBuf));
463 : }
464 0 : HCCLV2_FUNC_RUN(
465 : HcomAllGatherVV2(tag, const_cast<void*>(sendBuf), sendCount, const_cast<void*>(recvBuf), const_cast<void*>(recvCounts), const_cast<void*>(rdispls), dataType, group, stream));
466 0 : CHK_RET(HcomCheckOpParam(tag, 0, dataType, group, stream));
467 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
468 0 : CHK_RET(HcomGetCommByGroup(strGroup.c_str(), hcclComm));
469 0 : ret = hcclComm->AllGatherV(tag, sendBuf, sendCount, recvBuf, recvCounts, rdispls, dataType, stream);
470 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[AllGatherV][Result]errNo[0x%016llx] hcclComm AllGatherV error, "\
471 : "tag[%s], input_ptr[%p], output_ptr[%p], count[%llu], data_type[%s]", HCOM_ERROR_CODE(ret),
472 : tag, sendBuf, recvBuf, sendCount, GetDataTypeEnumStr(dataType).c_str()), ret);
473 0 : CHK_RET(CallMsprofReportHostApi(hcclComm.get(), HcclCMDType::HCCL_CMD_ALLGATHER_V, beginTime, sendCount, dataType));
474 : /* 关键状态记录 */
475 0 : HCCL_RUN_INFO("hcom AllGatherv success,take time [%lld]us, tag[%s], input_ptr[%p], output_ptr[%p], "\
476 : "count[%llu], data_type[%s]", DURATION_US(TIME_NOW() - startut), tag, sendBuf, recvBuf,
477 : sendCount, GetDataTypeEnumStr(dataType).c_str());
478 :
479 0 : return HCCL_SUCCESS;
480 0 : }
481 :
482 0 : HcclResult HcomGetInitStatus(bool *initiated)
483 : {
484 0 : CHK_PTR_NULL(initiated);
485 0 : HCCLV2_FUNC_RUN(HcomGetInitStatusV2(*initiated));
486 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
487 0 : *initiated = !(hcomInfo.pComm == nullptr);
488 :
489 0 : HCCL_INFO("Get Hcom Init Status: [%d]", *initiated);
490 0 : return HCCL_SUCCESS;
491 : }
492 :
493 0 : HcclResult HcomAllReduce(const char *tag, void *inputPtr, void *outputPtr, u64 count, HcclDataType dataType,
494 : HcclReduceOp op, const char *group, rtStream_t stream)
495 : {
496 0 : HcclUs startut = TIME_NOW();
497 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
498 :
499 0 : s32 deviceLogicId = 0;
500 0 : CHK_RET(hrtGetDevice(&deviceLogicId));
501 :
502 0 : CHK_PRT_RET(count == 0, HCCL_WARNING("input count is 0, return AllReduce success"), HCCL_SUCCESS);
503 : // 入参合法性校验
504 :
505 0 : RPT_INPUT_ERR(inputPtr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
506 : std::vector<std::string>({"HcomAllReduce", "nullptr", "inputPtr", "non-null pointer"}));
507 0 : CHK_PTR_NULL(inputPtr);
508 0 : RPT_INPUT_ERR(outputPtr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
509 : std::vector<std::string>({"HcomAllReduce", "nullptr", "outputPtr", "non-null pointer"}));
510 0 : CHK_PTR_NULL(outputPtr);
511 0 : RPT_INPUT_ERR(stream == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
512 : std::vector<std::string>({"HcomAllReduce", "nullptr", "stream", "non-null pointer"}));
513 0 : CHK_PTR_NULL(stream);
514 :
515 0 : s32 streamId = 0;
516 0 : CHK_RET(HcomCheckReductionOp("HcomAllReduce", op));
517 0 : CHK_RET(hrtGetStreamId(stream, streamId));
518 :
519 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
520 : /* 接口交互信息日志 */
521 0 : HCCL_USER_CRITICAL_LOG("Entry-HcomAllReduce:tag[%s], inputPtr[%p], outputPtr[%p], count[%llu], dataType[%s], op[%s], "\
522 : "group[%s], streamId[%d], deviceLogicId[%d]",
523 : tag, inputPtr, outputPtr, count, GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str(),
524 : strGroup.c_str(), streamId, deviceLogicId);
525 0 : CHK_RET(PrintMemoryAttr(inputPtr));
526 0 : CHK_RET(PrintMemoryAttr(outputPtr));
527 :
528 : // HcomAllReduceV2
529 0 : HCCLV2_FUNC_RUN(HcomAllReduceV2(tag, inputPtr, outputPtr, count, dataType, op, group, stream));
530 0 : CHK_RET(HcomCheckOpParam(tag, count, dataType, group, stream));
531 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
532 0 : CHK_RET(HcomGetCommByGroup(strGroup.c_str(), hcclComm));
533 : /* 入参的正确性由HCCL确保 */
534 0 : HcclResult ret = hcclComm->AllReduce(tag, inputPtr, outputPtr, count, dataType, op, stream);
535 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[AllReduce][Result]errNo[0x%016llx] hcclComm AllReduce error, "\
536 : "tag[%s], input_ptr[%p], output_ptr[%p], count[%llu], data_type[%s], op[%s]", HCOM_ERROR_CODE(ret), tag,
537 : inputPtr, outputPtr, count, GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str()), ret);
538 0 : CHK_RET(CallMsprofReportHostApi(hcclComm.get(), HcclCMDType::HCCL_CMD_ALLREDUCE, beginTime, count, dataType));
539 : /* 关键状态记录 */
540 0 : HCCL_RUN_INFO("hcom AllReduce success,take time [%lld]us, tag[%s], input_ptr[%p], output_ptr[%p], "\
541 : "count[%llu], data_type[%s], op[%s]", DURATION_US(TIME_NOW() - startut), tag, inputPtr,
542 : outputPtr, count, GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str());
543 :
544 0 : return HCCL_SUCCESS;
545 0 : }
546 :
547 0 : HcclResult HcomBroadcast(const char *tag, void *ptr, u64 count, HcclDataType dataType,
548 : u32 root, const char *group, rtStream_t stream)
549 : {
550 0 : HcclUs startut = TIME_NOW();
551 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
552 :
553 0 : s32 deviceLogicId = 0;
554 0 : CHK_RET(hrtGetDevice(&deviceLogicId));
555 :
556 0 : CHK_PRT_RET(count == 0, HCCL_WARNING("input count is 0, return BroadCast success"), HCCL_SUCCESS);
557 : // 入参合法性校验
558 0 : RPT_INPUT_ERR(ptr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
559 : std::vector<std::string>({"HcomBroadcast", "nullptr", "ptr", "non-null pointer"}));
560 0 : CHK_PTR_NULL(ptr);
561 0 : RPT_INPUT_ERR(stream == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
562 : std::vector<std::string>({"HcomBroadcast", "nullptr", "stream", "non-null pointer"}));
563 0 : CHK_PTR_NULL(stream);
564 :
565 0 : s32 streamId = 0;
566 0 : CHK_RET(hrtGetStreamId(stream, streamId));
567 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
568 : /* 接口交互信息日志 */
569 0 : HCCL_USER_CRITICAL_LOG("Entry-HcomBroadcast:tag[%s], ptr[%p], count[%llu], dataType[%s], root[%u], "\
570 : "group[%s], streamId[%d], deviceLogicId[%d]", tag, ptr, count, GetDataTypeEnumStr(dataType).c_str(), root, strGroup.c_str(),
571 : streamId, deviceLogicId);
572 0 : CHK_RET(PrintMemoryAttr(ptr));
573 :
574 : // HcomBroadcastV2
575 0 : HCCLV2_FUNC_RUN(HcomBroadcastV2(tag, ptr, count, dataType, root, group, stream));
576 0 : CHK_RET(HcomCheckOpParam(tag, count, dataType, group, stream));
577 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
578 0 : CHK_RET(HcomCheckUserRank(hcomInfo.params.totalRanks, root));
579 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
580 0 : CHK_RET(HcomGetCommByGroup(strGroup.c_str(), hcclComm));
581 0 : u32 aivCoreLimit = 0;
582 0 : CHK_RET(hcclComm->GetNumBlocks(aivCoreLimit));
583 : /* 入参的正确性由HCCL确保 */
584 0 : HcclResult ret = hcclComm->Broadcast(tag, ptr, count, dataType, root, stream);
585 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
586 : HCCL_ERROR("[Broadcast][Result]errNo[0x%016llx] hcclComm BroadCast error,tag[%s], input_ptr[%p],"
587 : "count[%llu], data_type[%s], root[%u]", HCOM_ERROR_CODE(ret), tag, ptr, count,
588 : GetDataTypeEnumStr(dataType).c_str(), root), ret);
589 0 : CHK_RET(CallMsprofReportHostApi(hcclComm.get(), HcclCMDType::HCCL_CMD_BROADCAST, beginTime, count, dataType));
590 : /* 关键状态记录 */
591 0 : HCCL_RUN_INFO("hcom BroadCast success,take time [%lld]us,tag[%s], input_ptr[%p], count[%llu], data_type[%s], "\
592 : "root[%u]", DURATION_US(TIME_NOW() - startut), tag, ptr, count, GetDataTypeEnumStr(dataType).c_str(), root);
593 :
594 0 : return HCCL_SUCCESS;
595 0 : }
596 :
597 0 : HcclResult HcomReduce(const char *tag, void *inputPtr, void *outputPtr, u64 count, HcclDataType dataType,
598 : HcclReduceOp op, u32 root, const char *group, rtStream_t stream)
599 : {
600 0 : HcclUs startut = TIME_NOW();
601 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
602 :
603 0 : s32 deviceLogicId = 0;
604 0 : CHK_RET(hrtGetDevice(&deviceLogicId));
605 :
606 0 : CHK_PRT_RET(count == 0, HCCL_WARNING("input count is 0, return Reduce success"), HCCL_SUCCESS);
607 :
608 : // 入参合法性校验
609 0 : RPT_INPUT_ERR(tag == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
610 : std::vector<std::string>({"HcomReduce", "nullptr", "tag", "non-null pointer"}));
611 0 : CHK_PTR_NULL(tag);
612 0 : RPT_INPUT_ERR(inputPtr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
613 : std::vector<std::string>({"HcomReduce", "nullptr", "inputPtr", "non-null pointer"}));
614 0 : CHK_PTR_NULL(inputPtr);
615 0 : RPT_INPUT_ERR(outputPtr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
616 : std::vector<std::string>({"HcomReduce", "nullptr", "outputPtr", "non-null pointer"}));
617 0 : CHK_PTR_NULL(outputPtr);
618 0 : RPT_INPUT_ERR(stream == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
619 : std::vector<std::string>({"HcomReduce", "nullptr", "stream", "non-null pointer"}));
620 0 : CHK_PTR_NULL(stream);
621 0 : CHK_RET(HcomCheckReductionOp("HcomReduce", op));
622 :
623 0 : s32 streamId = 0;
624 0 : CHK_RET(hrtGetStreamId(stream, streamId));
625 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
626 : /* 接口交互信息日志 */
627 0 : HCCL_USER_CRITICAL_LOG("Entry-HcomReduce:tag[%s], inputPtr[%p], outputPtr[%p], count[%llu], dataType[%s], op[%s], "
628 : "root[%u], group[%s], streamId[%d], deviceLogicId[%d]",
629 : tag, inputPtr, outputPtr, count, GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str(), root,
630 : strGroup.c_str(), streamId, deviceLogicId);
631 0 : CHK_RET(PrintMemoryAttr(inputPtr));
632 0 : CHK_RET(PrintMemoryAttr(outputPtr));
633 :
634 : // HcomReduceV2
635 0 : HCCLV2_FUNC_RUN(HcomReduceV2(tag, inputPtr, outputPtr, count, dataType, op, root, group, stream));
636 0 : CHK_RET(HcomCheckOpParam(tag, count, dataType, group, stream));
637 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
638 0 : CHK_PRT_RET(hcomInfo.pComm == nullptr, HCCL_ERROR("[Reduce][Result]hcomInfo.pComm is null, "\
639 : "please check if the initialize process is called."), HCCL_E_PTR);
640 0 : CHK_RET(HcomCheckUserRank(hcomInfo.params.totalRanks, root));
641 :
642 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
643 0 : CHK_RET(HcomGetCommByGroup(group, hcclComm));
644 : /* 入参的正确性由HCCL确保 */
645 0 : HcclResult ret = hcclComm->Reduce(tag, inputPtr, outputPtr, count, dataType, op, root, stream);
646 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Reduce][Result]errNo[0x%016llx] hcclComm Reduce error, tag[%s], "\
647 : "input_ptr[%p], output_ptr[%p], count[%llu], data_type[%s], op[%s], root[%u]", HCOM_ERROR_CODE(ret), tag,
648 : inputPtr, outputPtr, count, GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str(), root), ret);
649 0 : CHK_RET(CallMsprofReportHostApi(hcclComm.get(), HcclCMDType::HCCL_CMD_REDUCE, beginTime, count, dataType));
650 0 : HcclUs endut = TIME_NOW();
651 : /* 关键状态记录 */
652 0 : HCCL_RUN_INFO("hcom Reduce success, take time [%lld]us, tag[%s], input_ptr[%p], output_ptr[%p], count[%llu], "\
653 : "data_type[%s], op[%s], root[%u]", DURATION_US(endut - startut), tag, inputPtr, outputPtr, count,
654 : GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str(), root);
655 :
656 0 : return HCCL_SUCCESS;
657 0 : }
658 :
659 0 : HcclResult HcomReduceScatter(const char *tag, void *inputPtr, void *outputPtr, u64 count,
660 : HcclDataType dataType, HcclReduceOp op, const char *group, rtStream_t stream)
661 : {
662 0 : HcclUs startut = TIME_NOW();
663 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
664 :
665 0 : s32 deviceLogicId = 0;
666 0 : CHK_RET(hrtGetDevice(&deviceLogicId));
667 :
668 0 : CHK_PRT_RET(count == 0, HCCL_WARNING("input count is 0, return ReduceScatter success"), HCCL_SUCCESS);
669 : // 入参合法性校验
670 :
671 0 : RPT_INPUT_ERR(inputPtr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
672 : std::vector<std::string>({"HcomReduceScatter", "nullptr", "inputPtr", "non-null pointer"}));
673 0 : CHK_PTR_NULL(inputPtr);
674 0 : RPT_INPUT_ERR(outputPtr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
675 : std::vector<std::string>({"HcomReduceScatter", "nullptr", "outputPtr", "non-null pointer"}));
676 0 : CHK_PTR_NULL(outputPtr);
677 0 : RPT_INPUT_ERR(stream == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
678 : std::vector<std::string>({"HcomReduceScatter", "nullptr", "stream", "non-null pointer"}));
679 0 : CHK_PTR_NULL(stream);
680 0 : CHK_RET(HcomCheckReductionOp("HcomReduceScatter", op));
681 :
682 0 : s32 streamId = 0;
683 0 : CHK_RET(hrtGetStreamId(stream, streamId));
684 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
685 : /* 接口交互信息日志 */
686 0 : HCCL_USER_CRITICAL_LOG("Entry-HcomReduceScatter:tag[%s], inputPtr[%p], outputPtr[%p], count[%llu], dataType[%s], op[%s], "\
687 : "group[%s], streamId[%d], deviceLogicId[%d]", tag, inputPtr, outputPtr, count, GetDataTypeEnumStr(dataType).c_str(),
688 : GetReduceOpEnumStr(op).c_str(), strGroup.c_str(), streamId, deviceLogicId);
689 0 : CHK_RET(PrintMemoryAttr(inputPtr));
690 0 : CHK_RET(PrintMemoryAttr(outputPtr));
691 :
692 0 : HCCLV2_FUNC_RUN(HcomReduceScatterV2(tag, inputPtr, outputPtr, count, dataType, op, group, stream));
693 0 : CHK_RET(HcomCheckOpParam(tag, count, dataType, group, stream));
694 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
695 0 : CHK_RET(HcomGetCommByGroup(strGroup.c_str(), hcclComm));
696 : /* 入参的正确性由HCCL确保 */
697 0 : HcclResult ret = hcclComm->ReduceScatter(tag, inputPtr, outputPtr, count, dataType, op, stream);
698 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[ReduceScatter][Result]errNo[0x%016llx] hcclComm ReduceScatter "\
699 : "error, tag[%s], input_ptr[%p], output_ptr[%p], count[%llu], data_type[%s], op[%s]", HCOM_ERROR_CODE(ret),
700 : tag, inputPtr, outputPtr, count, GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str());, ret);
701 0 : CHK_RET(CallMsprofReportHostApi(hcclComm.get(), HcclCMDType::HCCL_CMD_REDUCE_SCATTER, beginTime, count, dataType));
702 : /* 关键状态记录 */
703 0 : HCCL_RUN_INFO(
704 : "hcom reduceScatter success, take time [%lld]us, tag[%s], input_ptr[%p], output_ptr[%p], count[%llu], "\
705 : "data_type[%s], op[%s]", DURATION_US(TIME_NOW() - startut), tag, inputPtr, outputPtr, count,
706 : GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str());
707 :
708 0 : return HCCL_SUCCESS;
709 0 : }
710 :
711 :
712 0 : HcclResult HcomReduceScatterV(const char *tag, void *sendBuf, const void *sendCounts, const void *sdispls, void *recvBuf,
713 : u64 recvCount, HcclDataType dataType, HcclReduceOp op, const char *group, rtStream_t stream)
714 : {
715 0 : HcclUs startut = TIME_NOW();
716 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
717 :
718 : // 入参合法性校验
719 0 : RPT_INPUT_ERR(stream == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "value"}),\
720 : std::vector<std::string>({"HcomReduceScatterV", "nullptr", "stream", "non-null pointer"}));
721 0 : CHK_PTR_NULL(stream);
722 0 : RPT_INPUT_ERR(sendCounts == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "value"}),\
723 : std::vector<std::string>({"HcomReduceScatterV", "nullptr", "sendCounts", "non-null pointer"}));
724 0 : CHK_PTR_NULL(sendCounts);
725 0 : RPT_INPUT_ERR(sdispls == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "value"}),\
726 : std::vector<std::string>({"HcomReduceScatterV", "nullptr", "sdispls", "non-null pointer"}));
727 0 : CHK_PTR_NULL(sdispls);
728 0 : if (UNLIKELY(recvCount > 0 && recvBuf == nullptr)) {
729 0 : RPT_INPUT_ERR(true, "EI0003",\
730 : std::vector<std::string>({"ccl_op", "value", "parameter", "value"}),\
731 : std::vector<std::string>({"HcomReduceScatterV", "nullptr", "recvBuf", "non-null pointer"}));
732 0 : CHK_PTR_NULL(recvBuf);
733 : }
734 :
735 0 : s32 streamId = 0;
736 0 : CHK_RET(hrtGetStreamId(stream, streamId));
737 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
738 : /* 接口交互信息日志 */
739 0 : HCCL_RUN_INFO("Entry-HcomReduceScatterV:tag[%s], inputPtr[%p], outputPtr[%p], count[%llu], dataType[%s], op[%s], "\
740 : "group[%s], streamId[%d]", tag, sendBuf, recvBuf, recvCount, GetDataTypeEnumStr(dataType).c_str(),
741 : GetReduceOpEnumStr(op).c_str(), strGroup.c_str(), streamId);
742 0 : if (sendBuf != nullptr) {
743 0 : CHK_RET(PrintMemoryAttr(sendBuf));
744 : }
745 0 : if (recvBuf != nullptr){
746 0 : CHK_RET(PrintMemoryAttr(recvBuf));
747 : }
748 :
749 0 : HCCLV2_FUNC_RUN(
750 : HcomReduceScatterVV2(tag, sendBuf, const_cast<void*>(sendCounts), const_cast<void*>(sdispls), recvBuf, recvCount, dataType, op, group, stream));
751 0 : CHK_RET(HcomCheckOpParam(tag, 0, dataType, group, stream));
752 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
753 0 : CHK_RET(HcomGetCommByGroup(strGroup.c_str(), hcclComm));
754 : /* 入参的正确性由HCCL确保 */
755 0 : HcclResult ret = hcclComm->ReduceScatterV(tag, sendBuf, sendCounts, sdispls, recvBuf, recvCount, dataType, op, stream);
756 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[ReduceScatterV][Result]errNo[0x%016llx] hcclComm ReduceScatter "\
757 : "error, tag[%s], input_ptr[%p], output_ptr[%p], count[%llu], data_type[%s], op[%s]", HCOM_ERROR_CODE(ret),
758 : tag, sendBuf, recvBuf, recvCount, GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str());, ret);
759 0 : CHK_RET(CallMsprofReportHostApi(hcclComm.get(), HcclCMDType::HCCL_CMD_REDUCE_SCATTER, beginTime, 0, dataType));
760 : /* 关键状态记录 */
761 0 : HCCL_RUN_INFO(
762 : "hcom ReduceScatterv success, take time [%lld]us, tag[%s], sendBuf[%p], recvBuf[%p], count[%llu], "\
763 : "data_type[%s], op[%s]", DURATION_US(TIME_NOW() - startut), tag, sendBuf, recvBuf, recvCount,
764 : GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str());
765 :
766 0 : return HCCL_SUCCESS;
767 0 : }
768 :
769 : /*
770 : * 点对点发送接口: 需要对应的hcom_receive执行才会实际发送。先分片,条件满足之后改为不分片
771 : * 发送端需要接收端准备好才会发送
772 : */
773 0 : HcclResult HcomSend(const char *tag, void *inputPtr, u64 count, HcclDataType dataType, u32 destRank,
774 : u32 srTag, const char *group, rtStream_t stream)
775 : {
776 0 : HcclUs startut = TIME_NOW();
777 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
778 :
779 0 : s32 deviceLogicId = 0;
780 0 : CHK_RET(hrtGetDevice(&deviceLogicId));
781 :
782 0 : CHK_PRT_RET(count == 0, HCCL_WARNING("input count is 0, return send success"), HCCL_SUCCESS);
783 :
784 : // 入参合法性校验
785 0 : RPT_INPUT_ERR(inputPtr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
786 : std::vector<std::string>({"HcomSend", "nullptr", "inputPtr", "non-null pointer"}));
787 0 : CHK_PTR_NULL(inputPtr);
788 0 : RPT_INPUT_ERR(stream == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
789 : std::vector<std::string>({"HcomSend", "nullptr", "stream", "non-null pointer"}));
790 0 : CHK_PTR_NULL(stream);
791 :
792 0 : s32 streamId = 0;
793 0 : CHK_RET(hrtGetStreamId(stream, streamId));
794 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
795 : /* 接口交互信息日志 */
796 0 : HCCL_USER_CRITICAL_LOG("Entry-HcomSend:tag[%s], inputPtr[%p], count[%llu], dataType[%s], destRank[%u], srTag[%u], "\
797 : "group[%s], streamId[%d], deviceLogicId[%d]", tag, inputPtr, count, GetDataTypeEnumStr(dataType).c_str(), destRank, srTag,
798 : strGroup.c_str(), streamId, deviceLogicId);
799 0 : CHK_RET(PrintMemoryAttr(inputPtr));
800 :
801 : // HcomSendV2
802 0 : HCCLV2_FUNC_RUN(HcomSendV2(tag, inputPtr, count, dataType, destRank, srTag, group, stream));
803 0 : CHK_RET(HcomCheckOpParam(tag, count, dataType, group, stream));
804 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
805 0 : CHK_RET(HcomCheckUserRank(hcomInfo.params.totalRanks, destRank));
806 :
807 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
808 0 : CHK_RET(HcomGetCommByGroup(strGroup.c_str(), hcclComm));
809 :
810 0 : u32 localGroupRank = INVALID_VALUE_RANKID;
811 0 : CHK_RET(hcclComm->GetGroupRank(localGroupRank));
812 : /* 调用HCCL的send, 入参的正确性由HCCL确保 */
813 0 : HcclResult ret = hcclComm->send(tag, inputPtr, count, dataType, destRank, stream, srTag, localGroupRank);
814 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Send][Result]errNo[0x%016llx] hcclComm send error, tag[%s], "\
815 : "inputPtr[%p], count[%llu], dataType[%s], destRank[%u], group[%s]", HCOM_ERROR_CODE(ret), tag,
816 : inputPtr, count, GetDataTypeEnumStr(dataType).c_str(), destRank, strGroup.c_str()), ret);
817 0 : CHK_RET(CallMsprofReportHostApi(hcclComm.get(), HcclCMDType::HCCL_CMD_SEND, beginTime, count, dataType));
818 : /* 关键状态记录 */
819 0 : HCCL_RUN_INFO("hcom send success,time[%lld]us,tag[%s],inputPtr[%p],count[%llu],dataType[%s],destRank[%u],"
820 : "srTag[%u],localGroupRank[%u]",
821 : DURATION_US(TIME_NOW() - startut), tag, inputPtr, count, GetDataTypeEnumStr(dataType).c_str(), destRank,
822 : srTag,localGroupRank);
823 :
824 0 : return HCCL_SUCCESS;
825 0 : }
826 :
827 : /*
828 : * 点对点接收接口: 需要对应的hcom_receive执行才会实际发送。先分片,条件满足之后改为不分片
829 : * 发送端需要接收端准备好才会发送
830 : */
831 0 : HcclResult HcomReceive(const char *tag, void *outputPtr, u64 count, HcclDataType dataType, u32 srcRank,
832 : u32 srTag, const char *group, rtStream_t stream)
833 : {
834 0 : HcclUs startut = TIME_NOW();
835 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
836 :
837 0 : s32 deviceLogicId = 0;
838 0 : CHK_RET(hrtGetDevice(&deviceLogicId));
839 0 : CHK_PRT_RET(count == 0, HCCL_WARNING("input count is 0, return receive success"), HCCL_SUCCESS);
840 :
841 : // 入参合法性校验
842 0 : RPT_INPUT_ERR(outputPtr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
843 : std::vector<std::string>({"HcomReceive", "nullptr", "outputPtr", "non-null pointer"}));
844 0 : CHK_PTR_NULL(outputPtr);
845 0 : RPT_INPUT_ERR(stream == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
846 : std::vector<std::string>({"HcomReceive", "nullptr", "stream", "non-null pointer"}));
847 0 : CHK_PTR_NULL(stream);
848 :
849 0 : s32 streamId = 0;
850 0 : CHK_RET(hrtGetStreamId(stream, streamId));
851 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
852 : /* 接口交互信息日志 */
853 0 : HCCL_USER_CRITICAL_LOG("Entry-HcomReceive:tag[%s], outputPtr[%p], count[%llu], dataType[%s], srcRank[%u], srTag[%u],"\
854 : "group[%s], streamId[%d], deviceLogicId[%d]", tag, outputPtr, count, GetDataTypeEnumStr(dataType).c_str(), srcRank, srTag,
855 : strGroup.c_str(), streamId, deviceLogicId);
856 0 : CHK_RET(PrintMemoryAttr(outputPtr));
857 :
858 : // HcomReceiveV2
859 0 : HCCLV2_FUNC_RUN(HcomReceiveV2(tag, outputPtr, count, dataType, srcRank, srTag, group, stream));
860 0 : CHK_RET(HcomCheckOpParam(tag, count, dataType, group, stream));
861 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
862 0 : CHK_RET(HcomCheckUserRank(hcomInfo.params.totalRanks, srcRank));
863 :
864 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
865 0 : HcclResult ret = HcomGetCommByGroup(strGroup.c_str(), hcclComm);
866 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Receive][Result]errNo[0x%016llx] get comm by group[%s] error",
867 : HCOM_ERROR_CODE(ret), strGroup.c_str()), ret);
868 :
869 0 : u32 localGroupRank = INVALID_VALUE_RANKID;
870 0 : CHK_RET(hcclComm->GetGroupRank(localGroupRank));
871 0 : ret = hcclComm->receive(tag, outputPtr, count, dataType, srcRank, stream, srTag, localGroupRank);
872 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Receive][Result]errNo[0x%016llx] hcclComm receive error,tag[%s], "\
873 : "outputPtr[%p], count[%llu], dataType[%s], srcRank[%u], group[%s]", HCOM_ERROR_CODE(ret), tag,
874 : outputPtr, count, GetDataTypeEnumStr(dataType).c_str(), srcRank, strGroup.c_str()), ret);
875 :
876 0 : CHK_RET(CallMsprofReportHostApi(hcclComm.get(), HcclCMDType::HCCL_CMD_RECEIVE, beginTime, count, dataType));
877 : /* 关键状态记录 */
878 0 : HCCL_RUN_INFO("hcom receive success,time[%lld]us,tag[%s],outputPtr[%p],count[%llu],dataType[%s],srcRank[%u],"
879 : "srTag[%u], localGroupRank[%u]",
880 : DURATION_US(TIME_NOW() - startut), tag, outputPtr, count, GetDataTypeEnumStr(dataType).c_str(), srcRank,
881 : srTag, localGroupRank);
882 :
883 0 : return HCCL_SUCCESS;
884 0 : }
885 :
886 0 : HcclResult HcclCommGraphAllGather(const char *tag, void *inputPtr, void *outputPtr, u64 inputCount,
887 : HcclDataType dataType, s64 opBaseHcom, rtStream_t stream)
888 : {
889 : HcclResult ret;
890 0 : HcclUs startut = TIME_NOW();
891 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
892 0 : CHK_PRT_RET(inputCount == 0, HCCL_WARNING("input count is 0, return AllGather success"), HCCL_SUCCESS);
893 : // 参数合法性校验
894 0 : RPT_INPUT_ERR(inputPtr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
895 : std::vector<std::string>({"HcclCommGraphAllGather", "nullptr", "inputPtr", "non-null pointer"}));
896 0 : CHK_PTR_NULL(inputPtr);
897 0 : RPT_INPUT_ERR(outputPtr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
898 : std::vector<std::string>({"HcclCommGraphAllGather", "nullptr", "outputPtr", "non-null pointer"}));
899 0 : CHK_PTR_NULL(outputPtr);
900 0 : RPT_INPUT_ERR(stream == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
901 : std::vector<std::string>({"HcclCommGraphAllGather", "nullptr", "stream", "non-null pointer"}));
902 0 : CHK_PTR_NULL(stream);
903 :
904 0 : s32 streamId = 0;
905 0 : ret = hrtGetStreamId(stream, streamId);
906 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[AllGather][Result]Call hrtGetStreamId error[%d].",
907 : ret), HCCL_E_RUNTIME);
908 : /* 接口交互信息日志 */
909 0 : HCCL_RUN_INFO("Entry-HcclCommGraphAllGather:tag[%s], inputPtr[%p], outputPtr[%p], inputCount[%llu], dataType[%s], "\
910 : "opBaseHcom[%lld] streamId[%d]", tag, inputPtr, outputPtr, inputCount, GetDataTypeEnumStr(dataType).c_str(),
911 : opBaseHcom, streamId);
912 0 : CHK_RET(PrintMemoryAttr(inputPtr));
913 0 : CHK_RET(PrintMemoryAttr(outputPtr));
914 :
915 : // HcclCommGraphAllGatherV2
916 0 : HCCLV2_FUNC_RUN([&]() -> HcclResult {
917 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm *>(opBaseHcom);
918 : CHK_PTR_NULL(hcclComm);
919 : HcclComm commV2 = hcclComm->GetCommunicatorV2();
920 : CHK_PTR_NULL(commV2);
921 : opBaseHcom = reinterpret_cast<s64>(commV2);
922 : CHK_RET(HcclCommGraphAllGatherV2(tag, inputPtr, outputPtr, inputCount, dataType, opBaseHcom, stream));
923 : return HCCL_SUCCESS;
924 : }());
925 0 : CHK_RET(HcomCheckOpParam(tag, inputCount, dataType, stream));
926 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
927 0 : CHK_PTR_NULL(hcclComm);
928 0 : CHK_RET(SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB));
929 0 : ret = hcclComm->AllGather(tag, inputPtr, outputPtr, inputCount, dataType, stream);
930 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[AllGather][Result]errNo[0x%016llx] hcclComm AllGather error, "\
931 : "tag[%s], input_ptr[%p], output_ptr[%p], count[%llu], data_type[%s]", HCOM_ERROR_CODE(ret),
932 : tag, inputPtr, outputPtr, inputCount, GetDataTypeEnumStr(dataType).c_str()), ret);
933 0 : CHK_RET(CallMsprofReportHostApi(hcclComm, HcclCMDType::HCCL_CMD_ALLGATHER, beginTime, inputCount, dataType));
934 : /* 关键状态记录 */
935 0 : HCCL_RUN_INFO("HcclCommGraphAllGather success,take time [%lld]us, tag[%s], input_ptr[%p], output_ptr[%p], "\
936 : "count[%llu], data_type[%s], opBaseHcom[%lld]", DURATION_US(TIME_NOW() - startut), tag,
937 : inputPtr, outputPtr, inputCount, GetDataTypeEnumStr(dataType).c_str(), opBaseHcom);
938 :
939 0 : return HCCL_SUCCESS;
940 : }
941 :
942 0 : HcclResult HcclCommGraphAllReduce(const char *tag, void *inputPtr, void *outputPtr, u64 count,
943 : HcclDataType dataType, HcclReduceOp op, s64 opBaseHcom, rtStream_t stream)
944 : {
945 0 : HcclUs startut = TIME_NOW();
946 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
947 0 : CHK_PRT_RET(count == 0, HCCL_WARNING("input count is 0, return AllReduce success"), HCCL_SUCCESS);
948 : // 入参合法性校验
949 0 : RPT_INPUT_ERR(inputPtr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
950 : std::vector<std::string>({"HcclCommGraphAllReduce", "nullptr", "inputPtr", "non-null pointer"}));
951 0 : CHK_PTR_NULL(inputPtr);
952 0 : RPT_INPUT_ERR(outputPtr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
953 : std::vector<std::string>({"HcclCommGraphAllReduce", "nullptr", "outputPtr", "non-null pointer"}));
954 0 : CHK_PTR_NULL(outputPtr);
955 0 : RPT_INPUT_ERR(stream == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
956 : std::vector<std::string>({"HcclCommGraphAllReduce", "nullptr", "stream", "non-null pointer"}));
957 0 : CHK_PTR_NULL(stream);
958 0 : CHK_RET(HcomCheckReductionOp("HcclCommGraphAllReduce", op));
959 0 : s32 streamId = 0;
960 0 : CHK_RET(hrtGetStreamId(stream, streamId));
961 :
962 : /* 接口交互信息日志 */
963 0 : HCCL_RUN_INFO("Entry-HcclCommGraphAllReduce:tag[%s], inputPtr[%p], outputPtr[%p], count[%llu], dataType[%s], "\
964 : "op[%s], opBaseHcom[%lld], streamId[%d]", tag, inputPtr, outputPtr, count,
965 : GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str(), opBaseHcom, streamId);
966 0 : CHK_RET(PrintMemoryAttr(inputPtr));
967 0 : CHK_RET(PrintMemoryAttr(outputPtr));
968 :
969 : // HcomGraphAllReduceV2
970 0 : HCCLV2_FUNC_RUN([&]() -> HcclResult {
971 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm *>(opBaseHcom);
972 : CHK_PTR_NULL(hcclComm);
973 : HcclComm commV2 = hcclComm->GetCommunicatorV2();
974 : CHK_PTR_NULL(commV2);
975 : opBaseHcom = reinterpret_cast<s64>(commV2);
976 : CHK_RET(HcomGraphAllReduceV2(tag, inputPtr, outputPtr, count, dataType, op, opBaseHcom, stream));
977 : return HCCL_SUCCESS;
978 : }());
979 0 : CHK_RET(HcomCheckOpParam(tag, count, dataType, stream));
980 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
981 0 : CHK_PTR_NULL(hcclComm);
982 0 : CHK_RET(SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB));
983 : /* 入参的正确性由HCCL确保 */
984 0 : HcclResult ret = hcclComm->AllReduce(tag, inputPtr, outputPtr, count, dataType, op, stream);
985 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[AllReduce][Result]errNo[0x%016llx] hcclComm AllReduce error, "\
986 : "tag[%s], input_ptr[%p], output_ptr[%p], count[%llu], data_type[%s], op[%s]", HCOM_ERROR_CODE(ret), tag,
987 : inputPtr, outputPtr, count, GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str()), ret);
988 0 : CHK_RET(CallMsprofReportHostApi(hcclComm, HcclCMDType::HCCL_CMD_ALLREDUCE, beginTime, count, dataType));
989 : /* 关键状态记录 */
990 0 : HCCL_RUN_INFO("HcclCommGraphAllReduce success,take time [%lld]us, tag[%s], input_ptr[%p], output_ptr[%p], "\
991 : "count[%llu], data_type[%s], op[%s], opBaseHcom[%lld]", DURATION_US(TIME_NOW() - startut), tag, inputPtr,
992 : outputPtr, count, GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str(), opBaseHcom);
993 :
994 0 : return HCCL_SUCCESS;
995 : }
996 :
997 0 : HcclResult HcclCommGraphReduce(const char *tag, void *inputPtr, void *outputPtr, u64 count, HcclDataType dataType,
998 : HcclReduceOp op, u32 root, s64 opBaseHcom, rtStream_t stream)
999 : {
1000 0 : HcclUs startut = TIME_NOW();
1001 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
1002 0 : CHK_PRT_RET(count == 0, HCCL_WARNING("input count is 0, return Reduce success"), HCCL_SUCCESS);
1003 :
1004 : // 入参合法性校验
1005 0 : RPT_INPUT_ERR(tag == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
1006 : std::vector<std::string>({"HcclCommGraphReduce", "nullptr", "tag", "non-null pointer"}));
1007 0 : CHK_PTR_NULL(tag);
1008 0 : RPT_INPUT_ERR(inputPtr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
1009 : std::vector<std::string>({"HcclCommGraphReduce", "nullptr", "inputPtr", "non-null pointer"}));
1010 0 : CHK_PTR_NULL(inputPtr);
1011 0 : RPT_INPUT_ERR(outputPtr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
1012 : std::vector<std::string>({"HcclCommGraphReduce", "nullptr", "outputPtr", "non-null pointer"}));
1013 0 : CHK_PTR_NULL(outputPtr);
1014 0 : RPT_INPUT_ERR(stream == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
1015 : std::vector<std::string>({"HcclCommGraphReduce", "nullptr", "stream", "non-null pointer"}));
1016 0 : CHK_PTR_NULL(stream);
1017 0 : CHK_RET(HcomCheckReductionOp("HcclCommGraphReduce", op));
1018 0 : u32 totalRanks = 0;
1019 0 : CHK_RET(HcclCommGraphGetRankSize(opBaseHcom, &totalRanks));
1020 0 : CHK_RET(HcomCheckUserRank(totalRanks, root));
1021 0 : s32 streamId = 0;
1022 0 : CHK_RET(hrtGetStreamId(stream, streamId));
1023 : /* 接口交互信息日志 */
1024 0 : HCCL_RUN_INFO("Entry-HcclCommGraphReduce:tag[%s], inputPtr[%p], outputPtr[%p], count[%llu], dataType[%s], op[%s], "
1025 : "root[%u], opBaseHcom[%lld], streamId[%d]",
1026 : tag, inputPtr, outputPtr, count, GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str(), root,
1027 : opBaseHcom, streamId);
1028 0 : CHK_RET(PrintMemoryAttr(inputPtr));
1029 0 : CHK_RET(PrintMemoryAttr(outputPtr));
1030 :
1031 : // HcomGraphReduceV2
1032 0 : HCCLV2_FUNC_RUN([&]() -> HcclResult {
1033 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm *>(opBaseHcom);
1034 : CHK_PTR_NULL(hcclComm);
1035 : HcclComm commV2 = hcclComm->GetCommunicatorV2();
1036 : CHK_PTR_NULL(commV2);
1037 : opBaseHcom = reinterpret_cast<s64>(commV2);
1038 : CHK_RET(HcomGraphReduceV2(tag, inputPtr, outputPtr, count, dataType, op, root, opBaseHcom, stream));
1039 : return HCCL_SUCCESS;
1040 : }());
1041 0 : CHK_RET(HcomCheckOpParam(tag, count, dataType));
1042 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
1043 0 : CHK_PTR_NULL(hcclComm);
1044 0 : CHK_RET(SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB));
1045 : /* 入参的正确性由HCCL确保 */
1046 0 : HcclResult ret = hcclComm->Reduce(tag, inputPtr, outputPtr, count, dataType, op, root, stream);
1047 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Reduce][Result]errNo[0x%016llx] hcclComm Reduce error, tag[%s], "\
1048 : "input_ptr[%p], output_ptr[%p], count[%llu], data_type[%s], op[%s], root[%u]", HCOM_ERROR_CODE(ret), tag,
1049 : inputPtr, outputPtr, count, GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str(), root), ret);
1050 0 : CHK_RET(CallMsprofReportHostApi(hcclComm, HcclCMDType::HCCL_CMD_REDUCE, beginTime, count, dataType));
1051 0 : HcclUs endut = TIME_NOW();
1052 : /* 关键状态记录 */
1053 0 : HCCL_RUN_INFO("HcclCommGraphReduce success, take time [%lld]us, tag[%s], input_ptr[%p], output_ptr[%p], "
1054 : "count[%llu], data_type[%s], op[%s], root[%u], opBaseHcom[%lld]",
1055 : DURATION_US(endut - startut), tag, inputPtr, outputPtr, count, GetDataTypeEnumStr(dataType).c_str(),
1056 : GetReduceOpEnumStr(op).c_str(), root, opBaseHcom);
1057 :
1058 0 : return HCCL_SUCCESS;
1059 : }
1060 :
1061 0 : HcclResult HcclCommGraphBroadcast(const char *tag, void *ptr, u64 count, HcclDataType dataType, u32 root,
1062 : s64 opBaseHcom, rtStream_t stream)
1063 : {
1064 0 : HcclUs startut = TIME_NOW();
1065 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
1066 0 : CHK_PRT_RET(count == 0, HCCL_WARNING("input count is 0, return BroadCast success"), HCCL_SUCCESS);
1067 :
1068 : // 入参合法性校验
1069 0 : RPT_INPUT_ERR(ptr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
1070 : std::vector<std::string>({"HcclCommGraphBroadcast", "nullptr", "ptr", "non-null pointer"}));
1071 0 : CHK_PTR_NULL(ptr);
1072 0 : RPT_INPUT_ERR(stream == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
1073 : std::vector<std::string>({"HcclCommGraphBroadcast", "nullptr", "stream", "non-null pointer"}));
1074 0 : CHK_PTR_NULL(stream);
1075 0 : u32 totalRanks = 0;
1076 0 : CHK_RET(HcclCommGraphGetRankSize(opBaseHcom, &totalRanks));
1077 0 : CHK_RET(HcomCheckUserRank(totalRanks, root));
1078 :
1079 0 : s32 streamId = 0;
1080 0 : CHK_RET(hrtGetStreamId(stream, streamId));
1081 : /* 接口交互信息日志 */
1082 0 : HCCL_RUN_INFO("Entry-HcclCommGraphBroadcast:tag[%s], ptr[%p], count[%llu], dataType[%s], root[%u], "\
1083 : "opBaseHcom[%lld], streamId[%d]", tag, ptr, count, GetDataTypeEnumStr(dataType).c_str(), root, opBaseHcom,
1084 : streamId);
1085 :
1086 0 : CHK_RET(PrintMemoryAttr(ptr));
1087 0 : HCCLV2_FUNC_RUN([&]() -> HcclResult {
1088 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm *>(opBaseHcom);
1089 : CHK_PTR_NULL(hcclComm);
1090 : HcclComm commV2 = hcclComm->GetCommunicatorV2();
1091 : CHK_PTR_NULL(commV2);
1092 : opBaseHcom = reinterpret_cast<s64>(commV2);
1093 : CHK_RET(HcomGraphBroadcastV2(tag, ptr, count, dataType, root, opBaseHcom, stream));
1094 : return HCCL_SUCCESS;
1095 : }());
1096 0 : CHK_RET(HcomCheckOpParam(tag, count, dataType));
1097 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
1098 0 : CHK_PTR_NULL(hcclComm);
1099 0 : CHK_RET(SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB));
1100 0 : u32 aivCoreLimit = 0;
1101 0 : CHK_RET(hcclComm->GetNumBlocks(aivCoreLimit));
1102 : /* 入参的正确性由HCCL确保 */
1103 0 : HcclResult ret = hcclComm->Broadcast(tag, ptr, count, dataType, root, stream);
1104 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
1105 : HCCL_ERROR("[Broadcast][Result]errNo[0x%016llx] hcclComm BroadCast error,tag[%s], input_ptr[%p],"
1106 : "count[%llu], data_type[%s], root[%u]", HCOM_ERROR_CODE(ret), tag, ptr, count,
1107 : GetDataTypeEnumStr(dataType).c_str(), root), ret);
1108 0 : CHK_RET(CallMsprofReportHostApi(hcclComm, HcclCMDType::HCCL_CMD_BROADCAST, beginTime, count, dataType));
1109 : /* 关键状态记录 */
1110 0 : HCCL_RUN_INFO("HcclCommGraphBroadcast success,take time [%lld]us,tag[%s], input_ptr[%p], count[%llu], "\
1111 : "data_type[%s], root[%u], opBaseHcom[%lld]", DURATION_US(TIME_NOW() - startut), tag, ptr, count,
1112 : GetDataTypeEnumStr(dataType).c_str(), root, opBaseHcom);
1113 :
1114 0 : return HCCL_SUCCESS;
1115 : }
1116 0 : HcclResult HcclCommGraphReduceScatter(const char *tag, void *inputPtr, void *outputPtr, u64 count,
1117 : HcclDataType dataType, HcclReduceOp op, s64 opBaseHcom, rtStream_t stream)
1118 : {
1119 0 : HcclUs startut = TIME_NOW();
1120 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
1121 0 : CHK_PRT_RET(count == 0, HCCL_WARNING("input count is 0, return ReduceScatter success"), HCCL_SUCCESS);
1122 : // 入参合法性校验
1123 0 : RPT_INPUT_ERR(inputPtr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
1124 : std::vector<std::string>({"HcclCommGraphReduceScatter", "nullptr", "inputPtr", "non-null pointer"}));
1125 0 : CHK_PTR_NULL(inputPtr);
1126 0 : RPT_INPUT_ERR(outputPtr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
1127 : std::vector<std::string>({"HcclCommGraphReduceScatter", "nullptr", "outputPtr", "non-null pointer"}));
1128 0 : CHK_PTR_NULL(outputPtr);
1129 0 : RPT_INPUT_ERR(stream == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
1130 : std::vector<std::string>({"HcclCommGraphReduceScatter", "nullptr", "stream", "non-null pointer"}));
1131 0 : CHK_PTR_NULL(stream);
1132 0 : CHK_RET(HcomCheckReductionOp("HcclCommGraphReduceScatter", op));
1133 :
1134 0 : s32 streamId = 0;
1135 0 : CHK_RET(hrtGetStreamId(stream, streamId));
1136 : /* 接口交互信息日志 */
1137 0 : HCCL_RUN_INFO("Entry-HcclCommGraphReduceScatter:tag[%s], inputPtr[%p], outputPtr[%p], count[%llu], dataType[%s], "\
1138 : "op[%s], opBaseHcom[%lld], streamId[%d]", tag, inputPtr, outputPtr, count,
1139 : GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str(), opBaseHcom, streamId);
1140 0 : CHK_RET(PrintMemoryAttr(inputPtr));
1141 0 : CHK_RET(PrintMemoryAttr(outputPtr));
1142 :
1143 0 : HCCLV2_FUNC_RUN([&]() -> HcclResult {
1144 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm *>(opBaseHcom);
1145 : CHK_PTR_NULL(hcclComm);
1146 : HcclComm commV2 = hcclComm->GetCommunicatorV2();
1147 : CHK_PTR_NULL(commV2);
1148 : opBaseHcom = reinterpret_cast<s64>(commV2);
1149 : CHK_RET(HcomGraphReduceScatterV2(tag, inputPtr, outputPtr, count, dataType, op, opBaseHcom, stream));
1150 : return HCCL_SUCCESS;
1151 : }());
1152 0 : CHK_RET(HcomCheckOpParam(tag, count, dataType));
1153 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
1154 0 : CHK_PTR_NULL(hcclComm);
1155 0 : CHK_RET(SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB));
1156 : /* 入参的正确性由HCCL确保 */
1157 0 : HcclResult ret = hcclComm->ReduceScatter(tag, inputPtr, outputPtr, count, dataType, op, stream);
1158 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
1159 : HCCL_ERROR("[ReduceScatter][Result]errNo[0x%016llx] hcclComm ReduceScatter error, tag[%s],"
1160 : "input_ptr[%p], output_ptr[%p], count[%llu], data_type[%s], op[%s]", HCOM_ERROR_CODE(ret), tag,
1161 : inputPtr, outputPtr, count, GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str());, ret);
1162 0 : CHK_RET(CallMsprofReportHostApi(hcclComm, HcclCMDType::HCCL_CMD_REDUCE_SCATTER, beginTime, count, dataType));
1163 : /* 关键状态记录 */
1164 0 : HCCL_RUN_INFO(
1165 : "HcclCommGraphReduceScatter success, take time [%lld]us, tag[%s], input_ptr[%p], output_ptr[%p], "\
1166 : "count[%llu], data_type[%s], op[%s], opBaseHcom[%lld]", DURATION_US(TIME_NOW() - startut), tag,
1167 : inputPtr, outputPtr, count, GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str(), opBaseHcom);
1168 :
1169 0 : return HCCL_SUCCESS;
1170 : }
1171 0 : HcclResult HcclCommGraphSend(const char *tag, void *inputPtr, u64 count, HcclDataType dataType,
1172 : u32 destRank, u32 srTag, s64 opBaseHcom, rtStream_t stream)
1173 : {
1174 0 : HcclUs startut = TIME_NOW();
1175 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
1176 0 : CHK_PRT_RET(count == 0, HCCL_WARNING("input count is 0, return send success"), HCCL_SUCCESS);
1177 :
1178 : // 入参合法性校验
1179 0 : RPT_INPUT_ERR(inputPtr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
1180 : std::vector<std::string>({"HcclCommGraphSend", "nullptr", "inputPtr", "non-null pointer"}));
1181 0 : CHK_PTR_NULL(inputPtr);
1182 0 : RPT_INPUT_ERR(stream == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
1183 : std::vector<std::string>({"HcclCommGraphSend", "nullptr", "stream", "non-null pointer"}));
1184 0 : CHK_PTR_NULL(stream);
1185 0 : u32 totalRanks = 0;
1186 0 : CHK_RET(HcclCommGraphGetRankSize(opBaseHcom, &totalRanks));
1187 0 : CHK_RET(HcomCheckUserRank(totalRanks, destRank));
1188 :
1189 0 : s32 streamId = 0;
1190 0 : CHK_RET(hrtGetStreamId(stream, streamId));
1191 : /* 接口交互信息日志 */
1192 0 : HCCL_RUN_INFO("Entry-HcclCommGraphSend:tag[%s], inputPtr[%p], count[%llu], dataType[%s], destRank[%u], srTag[%u], "\
1193 : "opBaseHcom[%lld], streamId[%d]", tag, inputPtr, count, GetDataTypeEnumStr(dataType).c_str(), destRank,
1194 : srTag, opBaseHcom, streamId);
1195 :
1196 0 : CHK_RET(PrintMemoryAttr(inputPtr));
1197 0 : HCCLV2_FUNC_RUN([&]() -> HcclResult {
1198 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm *>(opBaseHcom);
1199 : CHK_PTR_NULL(hcclComm);
1200 : HcclComm commV2 = hcclComm->GetCommunicatorV2();
1201 : CHK_PTR_NULL(commV2);
1202 : opBaseHcom = reinterpret_cast<s64>(commV2);
1203 : CHK_RET(HcomGraphSendV2(tag, inputPtr, count, dataType, destRank, srTag, opBaseHcom, stream));
1204 : return HCCL_SUCCESS;
1205 : }());
1206 0 : CHK_RET(HcomCheckOpParam(tag, count, dataType));
1207 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
1208 0 : CHK_PTR_NULL(hcclComm);
1209 0 : CHK_RET(SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB));
1210 :
1211 0 : u32 rankID = 0;
1212 0 : CHK_RET(HcclCommGraphGetRankId(opBaseHcom, &rankID));
1213 : /* 调用HCCL的send, 入参的正确性由HCCL确保 */
1214 0 : HcclResult ret = hcclComm->send(tag, inputPtr, count, dataType, destRank, stream, srTag, rankID);
1215 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Send][Result]errNo[0x%016llx] hcclComm send error, tag[%s], "\
1216 : "inputPtr[%p], count[%llu], dataType[%s], destRank[%u]", HCOM_ERROR_CODE(ret), tag,
1217 : inputPtr, count, GetDataTypeEnumStr(dataType).c_str(), destRank), ret);
1218 0 : CHK_RET(CallMsprofReportHostApi(hcclComm, HcclCMDType::HCCL_CMD_SEND, beginTime, count, dataType));
1219 : /* 关键状态记录 */
1220 0 : HCCL_RUN_INFO("HcclCommGraphSend success,time[%lld]us,tag[%s],inputPtr[%p],count[%llu],dataType[%s],destRank[%u],"\
1221 : "srTag[%u], opBaseHcom[%lld]", DURATION_US(TIME_NOW() - startut), tag, inputPtr, count,
1222 : GetDataTypeEnumStr(dataType).c_str(), destRank, srTag, opBaseHcom);
1223 :
1224 0 : return HCCL_SUCCESS;
1225 : }
1226 0 : HcclResult HcclCommGraphReceive(const char *tag, void *outputPtr, u64 count, HcclDataType dataType,
1227 : u32 srcRank, u32 srTag, s64 opBaseHcom, rtStream_t stream)
1228 : {
1229 0 : HcclUs startut = TIME_NOW();
1230 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
1231 0 : CHK_PRT_RET(count == 0, HCCL_WARNING("input count is 0, return receive success"), HCCL_SUCCESS);
1232 : // 入参合法性校验
1233 0 : RPT_INPUT_ERR(outputPtr == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
1234 : std::vector<std::string>({"HcclCommGraphReceive", "nullptr", "outputPtr", "non-null pointer"}));
1235 0 : CHK_PTR_NULL(outputPtr);
1236 0 : RPT_INPUT_ERR(stream == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
1237 : std::vector<std::string>({"HcclCommGraphReceive", "nullptr", "stream", "non-null pointer"}));
1238 0 : CHK_PTR_NULL(stream);
1239 0 : u32 totalRanks = 0;
1240 0 : CHK_RET(HcclCommGraphGetRankSize(opBaseHcom, &totalRanks));
1241 0 : CHK_RET(HcomCheckUserRank(totalRanks, srcRank));
1242 :
1243 0 : s32 streamId = 0;
1244 0 : CHK_RET(hrtGetStreamId(stream, streamId));
1245 : /* 接口交互信息日志 */
1246 0 : HCCL_RUN_INFO("Entry-HcclCommGraphReceive:tag[%s], outputPtr[%p], count[%llu], dataType[%s], srcRank[%u], "
1247 : "srTag[%u], opBaseHcom[%lld], streamId[%d]", tag, outputPtr, count, GetDataTypeEnumStr(dataType).c_str(),
1248 : srcRank, srTag, opBaseHcom, streamId);
1249 0 : CHK_RET(PrintMemoryAttr(outputPtr));
1250 0 : HCCLV2_FUNC_RUN([&]() -> HcclResult {
1251 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm *>(opBaseHcom);
1252 : CHK_PTR_NULL(hcclComm);
1253 : HcclComm commV2 = hcclComm->GetCommunicatorV2();
1254 : CHK_PTR_NULL(commV2);
1255 : opBaseHcom = reinterpret_cast<s64>(commV2);
1256 : CHK_RET(HcomGraphReceiveV2(tag, outputPtr, count, dataType, srcRank, srTag, opBaseHcom, stream));
1257 : return HCCL_SUCCESS;
1258 : }());
1259 0 : CHK_RET(HcomCheckOpParam(tag, count, dataType));
1260 :
1261 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
1262 0 : CHK_PTR_NULL(hcclComm);
1263 0 : CHK_RET(SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB));
1264 :
1265 : /* 记录指令信息用于一致性校验 */
1266 0 : u32 rankID = 0;
1267 0 : CHK_RET(HcclCommGraphGetRankId(opBaseHcom, &rankID));
1268 0 : HcclResult ret = hcclComm->receive(tag, outputPtr, count, dataType, srcRank, stream, srTag, rankID);
1269 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Receive][Result]errNo[0x%016llx] hcclComm receive error,tag[%s], "\
1270 : "outputPtr[%p], count[%llu], dataType[%s], srcRank[%u],", HCOM_ERROR_CODE(ret), tag,
1271 : outputPtr, count, GetDataTypeEnumStr(dataType).c_str(), srcRank), ret);
1272 0 : CHK_RET(CallMsprofReportHostApi(hcclComm, HcclCMDType::HCCL_CMD_RECEIVE, beginTime, count, dataType));
1273 : /* 关键状态记录 */
1274 0 : HCCL_RUN_INFO("HcclCommGraphReceive success,time[%lld]us, tag[%s], outputPtr[%p], count[%llu], dataType[%s], "\
1275 : "srcRank[%u], srTag[%u], opBaseHcom[%lld],", DURATION_US(TIME_NOW() - startut), tag, outputPtr,
1276 : count, GetDataTypeEnumStr(dataType).c_str(), srcRank, srTag, opBaseHcom);
1277 :
1278 0 : return HCCL_SUCCESS;
1279 : }
1280 0 : HcclResult HcclCommGraphGetAlltoAllStagedWorkSpaceMemSize(s64 opBaseHcom, u64 *sendCounts, u64 *sdispls,
1281 : HcclDataType sendType, u64 *recvCounts, u64 *rdispls, HcclDataType recvType, u64 &memSize)
1282 : {
1283 0 : CHK_PTR_NULL(sendCounts);
1284 0 : CHK_PTR_NULL(sdispls);
1285 0 : CHK_PTR_NULL(recvCounts);
1286 0 : CHK_PTR_NULL(rdispls);
1287 0 : CHK_RET(HcomCheckDataType(sendType));
1288 0 : CHK_RET(HcomCheckDataType(recvType));
1289 :
1290 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
1291 0 : CHK_PTR_NULL(hcclComm);
1292 0 : CHK_RET(SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB));
1293 0 : CHK_RET(hcclComm->GetAlltoAllStagedWorkSpaceMemSize(sendCounts, sdispls, sendType,
1294 : recvCounts, rdispls, recvType, memSize));
1295 0 : return HCCL_SUCCESS;
1296 : }
1297 0 : HcclResult HcclCommGraphSetWorkspaceResource(const std::string &tag, s64 opBaseHcom, std::vector<rtStream_t> stream,
1298 : void *memPtr, u64 maxSize)
1299 : {
1300 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
1301 0 : CHK_PTR_NULL(hcclComm);
1302 0 : CHK_RET(SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB));
1303 :
1304 0 : CHK_RET(hcclComm->SetWorkspaceResource(tag, memPtr, maxSize, stream));
1305 0 : return HCCL_SUCCESS;
1306 : }
1307 :
1308 0 : HcclResult HcclCommGraphGetRankSize(s64 opBaseHcom, u32 *rankSize)
1309 : {
1310 0 : RPT_INPUT_ERR(rankSize == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
1311 : std::vector<std::string>({"HcclCommGraphGetRankSize", "nullptr", "rankSize", "non-null pointer"}));
1312 0 : CHK_PTR_NULL(rankSize);
1313 0 : bool &isAutoTuneModeOpen = HcomGetCtxAutoTuneMode();
1314 0 : if (isAutoTuneModeOpen) {
1315 0 : *rankSize = 1;
1316 0 : return HCCL_SUCCESS;
1317 : }
1318 0 : HCCL_INFO("HcclCommGraphGetRankSize:opBaseHcom[%lld]", opBaseHcom);
1319 0 : HCCLV2_FUNC_RUN([&]() -> HcclResult {
1320 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm *>(opBaseHcom);
1321 : CHK_PTR_NULL(hcclComm);
1322 : HcclComm commV2 = hcclComm->GetCommunicatorV2();
1323 : CHK_PTR_NULL(commV2);
1324 : opBaseHcom = reinterpret_cast<s64>(commV2);
1325 : CHK_RET(HcclCommGraphGetRankSizeV2(opBaseHcom, rankSize));
1326 : return HCCL_SUCCESS;
1327 : }());
1328 :
1329 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
1330 0 : CHK_PTR_NULL(hcclComm);
1331 0 : CHK_RET(SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB));
1332 0 : CHK_RET(hcclComm->GetRankSize(*rankSize));
1333 :
1334 0 : return HCCL_SUCCESS;
1335 : }
1336 :
1337 0 : HcclResult HcclCommGraphGetRankId(s64 opBaseHcom, u32 *rankId)
1338 : {
1339 0 : RPT_INPUT_ERR(rankId == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
1340 : std::vector<std::string>({"HcclCommGraphGetRankId", "nullptr", "rankId", "non-null pointer"}));
1341 0 : CHK_PTR_NULL(rankId);
1342 0 : bool &isAutoTuneModeOpen = HcomGetCtxAutoTuneMode();
1343 0 : if (isAutoTuneModeOpen) {
1344 0 : *rankId = 0;
1345 0 : return HCCL_SUCCESS;
1346 : }
1347 0 : HCCL_INFO("HcclCommGraphGetRankId:opBaseHcom[%lld]", opBaseHcom);
1348 0 : HCCLV2_FUNC_RUN([&]() -> HcclResult {
1349 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm *>(opBaseHcom);
1350 : CHK_PTR_NULL(hcclComm);
1351 : HcclComm commV2 = hcclComm->GetCommunicatorV2();
1352 : CHK_PTR_NULL(commV2);
1353 : opBaseHcom = reinterpret_cast<s64>(commV2);
1354 : CHK_RET(HcclCommGraphGetRankIdV2(opBaseHcom, rankId));
1355 : return HCCL_SUCCESS;
1356 : }());
1357 :
1358 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
1359 0 : CHK_PTR_NULL(hcclComm);
1360 0 : CHK_RET(SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB));
1361 0 : CHK_RET(hcclComm->GetUserRank(*rankId));
1362 :
1363 0 : return HCCL_SUCCESS;
1364 : }
1365 :
1366 0 : HcclResult HcclCommGraphGetWorkspaceSubStreamNum(u64 count, HcclDataType dataType, HcclReduceOp op, const std::string &algName,
1367 : s64 opBaseHcom, u64 &streamNum, u64 dataSize, bool ifAiv, HcclCMDType opType)
1368 : {
1369 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
1370 0 : return hcclComm->GetWorkspaceSubStreamNum(count, dataType, op, algName, streamNum, dataSize, ifAiv, opType);
1371 : }
1372 :
1373 0 : HcclResult HcclCommGraphGetAllReduceScratchSize(s64 opBaseHcom, const u64 count, const HcclDataType dataType,
1374 : u64 &outScratchSize)
1375 : {
1376 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
1377 0 : CHK_PTR_NULL(hcclComm);
1378 0 : return hcclComm->GetAllReduceScratchSize(count, dataType, outScratchSize);
1379 : }
1380 :
1381 0 : HcclResult HcclCommGraphGetIdentifier(s64 opBaseHcom, std::string &identifier)
1382 : {
1383 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
1384 0 : CHK_PTR_NULL(hcclComm);
1385 0 : identifier = hcclComm->GetIdentifier();
1386 0 : return HCCL_SUCCESS;
1387 : }
1388 :
1389 0 : HcclResult GenerateGroupHash(std::string &group, std::string &groupHash)
1390 : {
1391 : std::hash<std::string> hashString;
1392 0 : size_t nameHash = hashString(group);
1393 0 : groupHash = std::to_string(nameHash);
1394 0 : return HCCL_SUCCESS;
1395 : }
1396 :
1397 0 : HcclResult GenerateCclOpTag(const std::string &opType, const int64_t &hcomComm, std::string& group, std::string &sTag)
1398 : {
1399 0 : HcomOpTagInfo &opTagInfo = HcomGetCtxOpTagInfo();
1400 :
1401 : // middle 在获取到hcomComm时,等于identifier;在获取到group时,等于group
1402 0 : std::string middle;
1403 0 : if (hcomComm == static_cast<int64_t>(CommNumHcom::COMM_VALUE_DEFAULT)) {
1404 0 : middle = group;
1405 : } else {
1406 0 : CHK_RET(HcclCommGraphGetIdentifier(hcomComm, middle));
1407 : }
1408 : std::hash<std::string> hashString;
1409 0 : size_t nameHash = hashString(middle);
1410 0 : sTag = opType + "_" + std::to_string(nameHash);
1411 : // 多张图node name重复导致tag相同,因此对tag添加索引 tag = op type + node name + identifier name + index
1412 :
1413 0 : auto iter = opTagInfo.opIndex.find(middle);
1414 0 : if (iter == opTagInfo.opIndex.end()) {
1415 0 : opTagInfo.opIndex.insert({ middle, 0 });
1416 0 : iter = opTagInfo.opIndex.find(middle);
1417 0 : CHK_PRT_RET((iter == opTagInfo.opIndex.end()),
1418 : HCCL_ERROR("[Generate][OpTag]generate tag fail. get the op index failed. ret[%d]", HCCL_E_INTERNAL),
1419 : HCCL_E_INTERNAL);
1420 : }
1421 :
1422 0 : sTag = sTag + "_" + std::to_string(iter->second++);
1423 :
1424 0 : HCCL_INFO("generate ccl op tag success, tag[%s]", sTag.c_str());
1425 0 : return HCCL_SUCCESS;
1426 0 : }
1427 :
1428 0 : HcclResult HcomGetDevId(const char *group, s32 *devId)
1429 : {
1430 0 : HCCLV2_FUNC_RUN(HcomGetDevIdV2(group, devId));
1431 : /* 获取rankId */
1432 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
1433 0 : if (group != nullptr && HcclGetCommHandle(group, hcclComm) == HCCL_SUCCESS) {
1434 0 : CHK_RET(hcclComm->GetDeviceId(*devId));
1435 : } else {
1436 0 : if (group == nullptr) {
1437 0 : group = HCCL_WORLD_GROUP;
1438 : }
1439 0 : u32 rankId = 0;
1440 0 : CHK_RET(HcomGetRankId(group, &rankId));
1441 0 : u32 worldRankId = 0;
1442 0 : CHK_RET(HcomGetWorldRankFromGroupRank(group, rankId, &worldRankId));
1443 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
1444 :
1445 0 : for (auto it : hcomInfo.rankTable.rankList) {
1446 0 : if (worldRankId == it.rankId) {
1447 0 : u32 deviceLogicId = 0;
1448 0 : CHK_RET(hrtGetDeviceIndexByPhyId(static_cast<u32>(it.deviceInfo.devicePhyId), deviceLogicId));
1449 0 : *devId = static_cast<s32>(deviceLogicId);
1450 0 : return HCCL_SUCCESS;
1451 : }
1452 0 : }
1453 0 : HCCL_WARNING("[Get][DevId]rankList has no item with rankId[%u]", worldRankId);
1454 : }
1455 :
1456 0 : return HCCL_SUCCESS;
1457 0 : }
1458 :
1459 0 : HcclResult HcclCommGraphGetDevId(s64 opBaseHcom, s32 *devId)
1460 : {
1461 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
1462 0 : CHK_PTR_NULL(hcclComm);
1463 0 : CHK_PTR_NULL(devId);
1464 0 : CHK_RET(hcclComm->GetDeviceId(*devId));
1465 0 : HCCL_INFO("HcclCommGraphGetDevId devID[%d]", *devId);
1466 0 : return HCCL_SUCCESS;
1467 : }
1468 :
1469 0 : HcclResult HcomGetLocalRankSize(const char *group, u32 *localRankSize)
1470 : {
1471 0 : RPT_INPUT_ERR(localRankSize == nullptr, "EI0003",
1472 : std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),
1473 : std::vector<std::string>({ "HcomGetLocalRankSize", "nullptr", "localRankSize", "non-null pointer" }));
1474 0 : CHK_PTR_NULL(localRankSize);
1475 :
1476 0 : HCCLV2_FUNC_RUN(HcomGetLocalRankSizeV2(group, localRankSize));
1477 0 : bool &isAutoTuneModeOpen = HcomGetCtxAutoTuneMode();
1478 0 : if (isAutoTuneModeOpen) {
1479 0 : *localRankSize = 1;
1480 0 : return HCCL_SUCCESS;
1481 : }
1482 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
1483 0 : CHK_PRT_RET(hcomInfo.pComm == nullptr, HCCL_ERROR("[Get][LocalRankSize]hcomInfo.pComm is null, "\
1484 : "please check if the initialize process is called."), HCCL_E_PTR);
1485 0 : HcclResult ret = HcomCheckGroupName(group);
1486 0 : RPT_INPUT_ERR(ret != HCCL_SUCCESS,
1487 : "EI0003",
1488 : std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),
1489 : std::vector<std::string>({ "HcomGetLocalRankSize",
1490 : { group, strnlen(group, GROUP_NAME_MAX_LEN + 1) },
1491 : "group",
1492 : "a non-empty string of length 1 to " + std::to_string(GROUP_NAME_MAX_LEN) +
1493 : ", containing only alphanumeric characters and underscores"
1494 : }));
1495 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[%s][%s]errNo[0x%016llx] get local ranksize " \
1496 : "group name is invalid", LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_INVALID_ARGUMENT.c_str(), HCOM_ERROR_CODE(ret)), ret);
1497 :
1498 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
1499 :
1500 0 : u32 rankSize = 0;
1501 0 : u32 serverNum = 0;
1502 0 : CHK_RET(GetGroupRankInfo(strGroup.c_str(), RankInfoType::RANK_SIZE_IN_GROUP, 0, &rankSize));
1503 0 : CHK_RET(GetGroupRankInfo(strGroup.c_str(), RankInfoType::SERVER_NUM_IN_GROUP, 0, &serverNum));
1504 :
1505 0 : CHK_PRT_RET(serverNum == 0, HCCL_ERROR("[Get][LocalRankSize]errNo[0x%016llx] server num is zero",
1506 : HCOM_ERROR_CODE(HCCL_E_PARA)), HCCL_E_PARA);
1507 :
1508 0 : *localRankSize = rankSize / serverNum;
1509 0 : HCCL_INFO("hcom get local rank size success, group[%s]", strGroup.c_str());
1510 :
1511 0 : return HCCL_SUCCESS;
1512 0 : }
1513 :
1514 0 : HcclResult HcomGetRankId(const char *group, u32 *rankId)
1515 : {
1516 0 : RPT_INPUT_ERR(rankId == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
1517 : std::vector<std::string>({"HcomGetRankId", "nullptr", "rankId", "non-null pointer"}));
1518 0 : CHK_PTR_NULL(rankId);
1519 0 : bool &isAutoTuneModeOpen = HcomGetCtxAutoTuneMode();
1520 0 : if (isAutoTuneModeOpen) {
1521 0 : *rankId = 0;
1522 0 : return HCCL_SUCCESS;
1523 : }
1524 :
1525 0 : HcclResult ret = HcomCheckGroupName(group);
1526 0 : RPT_INPUT_ERR(ret != HCCL_SUCCESS,
1527 : "EI0003",
1528 : std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),
1529 : std::vector<std::string>({ "HcomGetRankId",
1530 : { group, strnlen(group, GROUP_NAME_MAX_LEN + 1) },
1531 : "group",
1532 : "a non-empty string of length 1 to " + std::to_string(GROUP_NAME_MAX_LEN) +
1533 : ", containing only alphanumeric characters and underscores"
1534 : }));
1535 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[%s][%s]errNo[0x%016llx] get_rank_id group name is invalid",
1536 : LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_INVALID_ARGUMENT.c_str(), HCOM_ERROR_CODE(ret)), ret);
1537 :
1538 : // HcomGetRankIdV2
1539 0 : HCCLV2_FUNC_RUN(HcomGetRankIdV2(group, rankId));
1540 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
1541 0 : CHK_RET(HcomGetCommByGroup(group, hcclComm));
1542 0 : CHK_RET(hcclComm->GetGroupRank(*rankId));
1543 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
1544 0 : HCCL_INFO("hcom get rank id success, group[%s], rankId[%u]", strGroup.c_str(), *rankId);
1545 :
1546 0 : return HCCL_SUCCESS;
1547 0 : }
1548 :
1549 0 : HcclResult HcomGetLocalRankId(const char *group, u32 *localRankId)
1550 : {
1551 0 : CHK_PTR_NULL(localRankId);
1552 0 : bool &isAutoTuneModeOpen = HcomGetCtxAutoTuneMode();
1553 0 : if (isAutoTuneModeOpen) {
1554 0 : *localRankId = 0;
1555 0 : return HCCL_SUCCESS;
1556 : }
1557 :
1558 0 : HCCLV2_FUNC_RUN(HcomGetLocalRankIdV2(group, localRankId));
1559 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
1560 0 : CHK_PRT_RET(hcomInfo.pComm == nullptr, HCCL_ERROR("[Get][LocalRankId]hcomInfo.pComm is null, "\
1561 : "please check if the initialize process is called."), HCCL_E_PTR);
1562 0 : CHK_RET(HcomCheckGroupName(group));
1563 :
1564 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
1565 :
1566 0 : u32 rankId = 0;
1567 0 : u32 localRankSize = 0;
1568 0 : CHK_RET(GetGroupRankInfo(strGroup.c_str(), RankInfoType::RANK_ID_IN_GROUP, 0, &rankId));
1569 0 : CHK_RET(HcomGetLocalRankSize(strGroup.c_str(), &localRankSize));
1570 :
1571 0 : CHK_PRT_RET(localRankSize == 0, HCCL_ERROR("[Get][LocalRankId]errNo[0x%016llx] local rank size is zero",
1572 : HCOM_ERROR_CODE(HCCL_E_PARA)), HCCL_E_PARA);
1573 :
1574 0 : *localRankId = rankId % localRankSize;
1575 0 : HCCL_INFO("hcom get local rank id success, group[%s]", strGroup.c_str());
1576 :
1577 0 : return HCCL_SUCCESS;
1578 0 : }
1579 :
1580 : HcclResult GetRankListHeterog(u32 rankNum, const u32 *rankIds, HcclGroupParams ¶ms);
1581 0 : HcclResult HcomCreateGroupImplHeterog(const std::string &group, const std::vector<u32> &rankIds)
1582 : {
1583 0 : HcclUs startut = TIME_NOW();
1584 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
1585 0 : std::string rankId;
1586 0 : for (u32 i = 0; i < rankIds.size(); i++) {
1587 0 : if (i < rankIds.size() - 1) {
1588 0 : rankId += to_string(rankIds[i]) + ',';
1589 0 : } else if (i == rankIds.size() - 1) {
1590 0 : rankId += to_string(rankIds[i]);
1591 : }
1592 : }
1593 : /* 接口交互信息日志 */
1594 0 : HCCL_RUN_INFO("Entry-HcomCreateGroup:group[%s], rankNum[%zu], rankIds[%s]", group.c_str(), rankIds.size(),
1595 : rankId.c_str());
1596 :
1597 0 : CHK_PRT_RET(hcomInfo.pComm == nullptr,
1598 : HCCL_ERROR("[Create][Group]hcomInfo.pComm is null, please check if the initialize process is called."),
1599 : HCCL_E_PTR);
1600 :
1601 0 : CHK_PRT_RET(hcomInfo.rankTable.rankList.empty(),
1602 : HCCL_ERROR("[Create][Group]group[%s] rankList is empty", group.c_str()), HCCL_E_INTERNAL);
1603 :
1604 : /* 已经存在的group不允许再次创建 */
1605 0 : std::unique_lock<std::mutex> groupParaLock(hcomInfo.groupParamsLock);
1606 0 : if (hcomInfo.hcomGroupMap.find(group) != hcomInfo.hcomGroupMap.end()) {
1607 0 : HCCL_ERROR("[Create][Group]errNo[0x%016llx] group[%s] is already exist", HCOM_ERROR_CODE(HCCL_E_PARA),
1608 : group.c_str());
1609 0 : return HCCL_E_PARA;
1610 : }
1611 0 : groupParaLock.unlock();
1612 :
1613 0 : HcclGroupParams groupParamsTem;
1614 0 : CHK_RET(GetRankListHeterog(rankIds.size(), rankIds.data(), groupParamsTem));
1615 :
1616 : // 如果是groupRank = INVALID_VALUE_RANKID,即本rank不参与create group
1617 0 : if (groupParamsTem.groupRank == INVALID_VALUE_RANKID) {
1618 0 : HCCL_ERROR("[Create][Group]errNo[0x%016llx] confirm groupRank from worldRank[%u] error",
1619 : HCOM_ERROR_CODE(HCCL_E_NOT_FOUND), hcomInfo.params.rank);
1620 0 : return HCCL_E_NOT_FOUND;
1621 : }
1622 :
1623 : /* 入参的正确性由HCCL确保 */
1624 :
1625 0 : CHK_RET(hcomInfo.pComm->CreateGroup(
1626 : group, groupParamsTem.groupRank, hcomInfo.params.rank, groupParamsTem.groupRanks, groupParamsTem.pSubComm));
1627 0 : CHK_SMART_PTR_NULL(groupParamsTem.pSubComm);
1628 :
1629 0 : groupParaLock.lock();
1630 0 : hcomInfo.hcomGroupMap.insert(std::make_pair(group, groupParamsTem));
1631 0 : groupParaLock.unlock();
1632 :
1633 0 : HCCL_RUN_INFO("hcom create group[%s] success, take time [%lld]us",
1634 : group.c_str(), DURATION_US(TIME_NOW() - startut));
1635 0 : return HCCL_SUCCESS;
1636 0 : }
1637 :
1638 0 : HcclResult HcomAbortGroup(const char *group)
1639 : {
1640 : /* 调优模式直接返回success */
1641 0 : bool &isAutoTuneModeOpen = HcomGetCtxAutoTuneMode();
1642 0 : if (isAutoTuneModeOpen) {
1643 0 : return HCCL_SUCCESS;
1644 : }
1645 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
1646 0 : CHK_PRT_RET(hcomInfo.pComm == nullptr,
1647 : HCCL_ERROR("[Destroy][Group]hcomInfo.pComm is null, "
1648 : "please check if the initialize process is called."),
1649 : HCCL_E_PTR);
1650 :
1651 0 : HCCL_RUN_INFO("Entry-HcomAbortGroup : group[%s]", group);
1652 0 : CHK_RET(DestroyFlag(group, true));
1653 0 : u32 ref = 0;
1654 0 : CHK_RET(HcomQueryGroupRef(group, ref));
1655 0 : while (ref != 0) {
1656 0 : std::shared_ptr<hccl::hcclComm> hcclComm = nullptr;
1657 0 : CHK_RET(HcomGetCommByGroup(group, hcclComm));
1658 0 : SaluSleep(ONE_HUNDRED_MICROSECOND_OF_USLEEP);
1659 0 : CHK_RET(HcomQueryGroupRef(group, ref));
1660 0 : }
1661 :
1662 0 : HCCL_RUN_INFO("hcom abort group[%s] success.", group);
1663 0 : return HCCL_SUCCESS;
1664 : }
1665 :
1666 0 : HcclResult HcomDestroyGroupImplHeterog(const std::string &group)
1667 : {
1668 : /* 接口交互信息日志 */
1669 0 : HCCL_RUN_INFO("Entry-HcomDestroyGroup:group[%s]", group.c_str());
1670 0 : CHK_RET(HcomAbortGroup(group.c_str()));
1671 :
1672 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
1673 0 : std::unique_lock<std::mutex> groupParaLock(hcomInfo.groupParamsLock);
1674 0 : auto iter = hcomInfo.hcomGroupMap.find(group);
1675 0 : if (iter == hcomInfo.hcomGroupMap.end()) {
1676 0 : HCCL_ERROR("[Destroy][Group]errNo[0x%016llx] group[%s] is not exist", HCOM_ERROR_CODE(HCCL_E_PARA),
1677 : group.c_str());
1678 0 : return HCCL_E_PARA;
1679 : }
1680 :
1681 0 : CHK_RET(hcomInfo.pComm->DestroyGroup(group));
1682 :
1683 0 : (iter->second).groupRanks.clear(); // 清除该服务器内相关group的对应信息
1684 :
1685 0 : hcomInfo.hcomGroupMap.erase(group);
1686 0 : groupParaLock.unlock();
1687 :
1688 0 : HCCL_RUN_INFO("hcom destroy group[%s] success.", group.c_str());
1689 0 : return HCCL_SUCCESS;
1690 0 : }
1691 :
1692 0 : bool HcomCallBackGroupIsInitHeterog(HcomInfo &hcomInfo)
1693 : {
1694 0 : return false;
1695 : }
1696 :
1697 0 : HcclResult GetRankListHeterog(u32 rankNum, const u32 *rankIds, HcclGroupParams ¶ms)
1698 : {
1699 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
1700 0 : std::vector<RankInfo_t> rankList;
1701 0 : params.totalRanks = rankNum;
1702 0 : params.worldRank = hcomInfo.params.rank;
1703 0 : params.groupRank = INVALID_VALUE_RANKID;
1704 0 : for (u32 i = 0; i < rankNum; i++) {
1705 0 : params.groupRanks.push_back(rankIds[i]);
1706 : }
1707 :
1708 0 : std::sort(params.groupRanks.begin(), params.groupRanks.end());
1709 0 : if (params.groupRanks[rankNum - 1] >= hcomInfo.rankTable.rankNum) {
1710 0 : HCCL_ERROR("[get][RankList]errNo[0x%016llx] groupRanks[%u]:%u is invalid", HCOM_ERROR_CODE(HCCL_E_PARA),
1711 : rankNum - 1, params.groupRanks[rankNum - 1]);
1712 0 : return HCCL_E_PARA;
1713 : }
1714 0 : if (hcomInfo.rankTable.rankList.size() <= params.groupRanks[0]) {
1715 0 : HCCL_ERROR("[get][RankList]errNo[0x%016llx] groupRanks[0] is invalid:[%u]", HCOM_ERROR_CODE(HCCL_E_PARA),
1716 : params.groupRanks[0]);
1717 0 : return HCCL_E_PARA;
1718 : }
1719 : // groupRanks 个数已经校验非0
1720 0 : std::string serverId = hcomInfo.rankTable.rankList[params.groupRanks[0]].serverId;
1721 0 : u32 serverNum = 1; // severNum初始值应为1,代表groupId为0的serverId;
1722 0 : RankInfo_t rankInfo;
1723 0 : for (u32 i = 0; i < rankNum; i++) {
1724 0 : rankInfo = hcomInfo.rankTable.rankList[params.groupRanks[i]];
1725 : // 校验worldRankID
1726 0 : if (rankInfo.rankId != params.groupRanks[i]) {
1727 0 : HCCL_ERROR("[get][RankList]errNo[0x%016llx] in rankList, worldRanks[%u] is invalid",
1728 : HCOM_ERROR_CODE(HCCL_E_PARA), rankInfo.rankId);
1729 0 : return HCCL_E_PARA;
1730 : }
1731 0 : if (params.groupRanks[i] == params.worldRank) {
1732 0 : params.groupRank = i;
1733 : }
1734 0 : if (rankInfo.serverId != serverId) {
1735 0 : serverNum++;
1736 0 : serverId = rankInfo.serverId;
1737 : }
1738 0 : rankInfo.rankId = i; // 放入groupRankid
1739 0 : rankList.push_back(rankInfo); // ranktable中的ranklist是以rankid的顺序排列的
1740 0 : rankInfo.serverId = ""; // 释放前先指空字符串
1741 : }
1742 0 : params.serverNum = serverNum;
1743 0 : bool isStandardCard = false;
1744 0 : CHK_RET(hcomInfo.pComm->IsStandardCard(isStandardCard));
1745 0 : if (!isStandardCard && hcomInfo.params.deviceType != DevType::DEV_TYPE_910B &&
1746 0 : hcomInfo.params.deviceType != DevType::DEV_TYPE_910_93) {
1747 0 : CHK_RET(CheckRankTableConfigInfo(rankList, rankNum, serverNum));
1748 : }
1749 0 : return HCCL_SUCCESS;
1750 0 : }
1751 :
1752 0 : HcclResult HcomGetSplitStrategy(const char *group, const struct model_feature *feature,
1753 : u32 **segmentIdxPtr, u32 *len, bool *configured, GradSplitForceMode force, OriginalGraphShapeType shapeType)
1754 : {
1755 0 : CHK_PTR_NULL(feature);
1756 0 : CHK_PTR_NULL(feature->model_name);
1757 0 : CHK_PTR_NULL(feature->gradient_size);
1758 0 : CHK_PTR_NULL(feature->gradient_time);
1759 0 : CHK_PTR_NULL(segmentIdxPtr);
1760 0 : CHK_PTR_NULL(len);
1761 0 : CHK_PTR_NULL(configured);
1762 :
1763 0 : bool bRet = feature->gradient_num == 0;
1764 0 : CHK_PRT_RET(
1765 : bRet, HCCL_ERROR("[Get][SplitStrategy]errNo[0x%016llx] gradient num is zero", HCOM_ERROR_CODE(HCCL_E_PARA)),
1766 : HCCL_E_PARA);
1767 0 : CHK_RET(HcomCheckGroupName(group));
1768 :
1769 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
1770 : /* 接口交互信息日志 */
1771 0 : HCCL_RUN_INFO("Entry-HcomGetSplitStrategy:group[%s], feature[%p]", strGroup.c_str(), feature);
1772 :
1773 : /* 获取梯度切分策略 */
1774 0 : std::vector<u32> segmentIdx;
1775 0 : CHK_RET(GetGradientSegment(strGroup, feature, segmentIdx, *configured, force, shapeType));
1776 0 : *len = static_cast<u32>(segmentIdx.size());
1777 0 : *segmentIdxPtr = new u32[*len];
1778 0 : std::copy(segmentIdx.begin(), segmentIdx.end(), *segmentIdxPtr);
1779 :
1780 0 : return HCCL_SUCCESS;
1781 0 : }
1782 :
1783 1 : HcclResult GetGradientSegment(const std::string &group, const struct model_feature *feature,
1784 : std::vector<u32>& segmentList, bool &configured, GradSplitForceMode force, OriginalGraphShapeType shapeType)
1785 : {
1786 : HcclResult ret;
1787 1 : HCCL_INFO("<gradient_segment group %s, model gradient num %u, model name %s>", group.c_str(), feature->gradient_num,
1788 : feature->model_name);
1789 : /* 分段算法实现 */
1790 1 : std::unique_ptr<hccl::GradientSegment> segmentImpl;
1791 1 : segmentImpl.reset(new (std::nothrow) GradientSegment());
1792 1 : CHK_SMART_PTR_NULL(segmentImpl);
1793 :
1794 : /* 校验基于总层数索引是否正确 */
1795 1 : std::unique_lock<std::mutex> segmentIdxMapLock(g_segmentIdxMapLock);
1796 1 : auto gIdxSearch = g_segmentIdxMap.find(group);
1797 1 : if (gIdxSearch != g_segmentIdxMap.end()) {
1798 0 : bool bRet = (gIdxSearch->second.size() != 0) && (gIdxSearch->second.back() != (feature->gradient_num - 1));
1799 0 : CHK_PRT_RET(bRet, HCCL_ERROR("[Get][GradientSegment]illegal segmentIndex maxVal=%u should be equal %u",
1800 : gIdxSearch->second.back(), feature->gradient_num - 1), HCCL_E_PARA);
1801 : }
1802 1 : segmentIdxMapLock.unlock();
1803 1 : ret = segmentImpl->GetGradientSegmentExecutor(group, feature, segmentList, configured, force,
1804 : shapeType);
1805 1 : if (ret == HCCL_SUCCESS) {
1806 1 : std::string printStr;
1807 1 : u32 baseIndex = 0;
1808 2 : for (u32 i = 0; i < segmentList.size(); i++) {
1809 1 : printStr.append("[");
1810 1 : printStr.append(std::to_string(baseIndex));
1811 1 : printStr.append(",");
1812 1 : printStr.append(std::to_string(segmentList[i]));
1813 1 : printStr.append("] ");
1814 1 : baseIndex = segmentList[i] + 1;
1815 : }
1816 1 : HCCL_RUN_INFO("gradient segment result: segment num: %zu, segment index list: %s ", \
1817 : segmentList.size(), printStr.c_str());
1818 1 : }
1819 1 : return ret;
1820 1 : }
1821 :
1822 0 : HcclResult HcomExecSelectAlg(s64 comm, const char *group, HcclCMDType opType, u64 count, HcclDataType dataType, HcclReduceOp op,
1823 : int32_t aivCoreLimit, bool &ifAiv, char *algName)
1824 : {
1825 0 : std::string tempAlgName;
1826 0 : if (comm != static_cast<int64_t>(CommNumHcom::COMM_VALUE_DEFAULT)) {
1827 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm *>(comm);
1828 0 : HcclComm commV2 = hcclComm->GetCommunicatorV2();
1829 0 : CHK_PTR_NULL(commV2);
1830 0 : comm = reinterpret_cast<s64>(commV2);
1831 0 : CHK_RET(HcomSelectAlgV2(comm, group, opType, count, dataType, op, aivCoreLimit, ifAiv, tempAlgName));
1832 : } else {
1833 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
1834 0 : CHK_RET(HcomGraphSelectAlgV2(comm, group, opType, count, dataType, op, aivCoreLimit, ifAiv, tempAlgName));
1835 0 : }
1836 0 : int32_t sret = memcpy_s(algName, ALG_NAME_MAX_LEN, tempAlgName.c_str(), (tempAlgName.length() + 1));
1837 0 : CHK_PRT_RET(sret != EOK, HCCL_ERROR("[HcomExecSelectAlg][algName]memcpy failed. ret[%d],"
1838 : "params:destMaxSize[%zu],count[%zu]", sret, ALG_NAME_MAX_LEN, (tempAlgName.length() + 1)), HCCL_E_PARA);
1839 0 : return HCCL_SUCCESS;
1840 0 : }
1841 :
1842 14 : HcclResult HcomSelectAlg(s64 comm, const char *group, u64 count, void* counts, HcclDataType dataType, HcclReduceOp op,
1843 : HcclCMDType opType, int32_t aivCoreLimit, bool *ifAiv, char *algName)
1844 : {
1845 14 : CHK_PTR_NULL(ifAiv);
1846 13 : HCCLV2_FUNC_RUN(HcomExecSelectAlg(comm, group, opType, count, dataType, op, aivCoreLimit, *ifAiv, algName));
1847 13 : HcclWorkflowMode lastWorkflowMode = GetWorkflowMode();
1848 13 : SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB);
1849 13 : std::string tempAlgName;
1850 13 : if (comm != static_cast<int64_t>(CommNumHcom::COMM_VALUE_DEFAULT)) {
1851 9 : hccl::hcclComm* hcclHcomComm = reinterpret_cast<hccl::hcclComm*>(comm);
1852 9 : CHK_RET(hcclHcomComm->HcclSelectAlg(opType, count, counts, dataType, op, aivCoreLimit, *ifAiv, tempAlgName));
1853 : } else {
1854 4 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
1855 4 : std::shared_ptr<hccl::hcclComm> hcclComm;
1856 4 : CHK_RET(HcomGetCommByGroup(strGroup.c_str(), hcclComm));
1857 3 : CHK_RET(hcclComm->HcclSelectAlg(opType, count, counts, dataType, op, aivCoreLimit, *ifAiv, tempAlgName));
1858 6 : }
1859 10 : int32_t sret = memcpy_s(algName, ALG_NAME_MAX_LEN, tempAlgName.c_str(), (tempAlgName.length() + 1));
1860 10 : CHK_PRT_RET(sret != EOK, HCCL_ERROR("[HcomSelectAlg][algName]memcpy failed. ret[%d],"
1861 : "params:destMaxSize[%zu],count[%zu]", sret, ALG_NAME_MAX_LEN, (tempAlgName.length() + 1)), HCCL_E_PARA);
1862 :
1863 10 : SetWorkflowMode(lastWorkflowMode);
1864 10 : return HCCL_SUCCESS;
1865 13 : }
1866 :
1867 0 : HcclResult HcomCalcAivCoreNum(const char *group, HcclCMDType opType, u64 count, void* counts, HcclDataType dataType, int32_t aivCoreLimit,
1868 : char *algName, u32 *numBlocks)
1869 : {
1870 0 : CHK_PTR_NULL(numBlocks);
1871 0 : std::string algNamV2(algName);
1872 0 : HCCLV2_FUNC_RUN(HcomCalcNumBlocksV2(group, opType, count, dataType, aivCoreLimit, algNamV2, *numBlocks));
1873 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
1874 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
1875 0 : CHK_RET(HcomGetCommByGroup(strGroup.c_str(), hcclComm));
1876 0 : std::string algNam(algName);
1877 0 : CHK_RET(hcclComm->HcclCalcNumBlocks(opType, count, counts, dataType, aivCoreLimit, algNam, *numBlocks));
1878 :
1879 0 : return HCCL_SUCCESS;
1880 0 : }
1881 :
1882 0 : HcclResult HcomGetAlgExecParam(const char *tag, const char *group, u64 count, void *inputPtr, void *outputPtr,
1883 : HcclCMDType opType, bool clearEnable, HcclDataType dataType, HcclReduceOp op,
1884 : void **commContext, u64 *len, u32 aivCoreLimit)
1885 : {
1886 0 : CHK_PTR_NULL(commContext);
1887 0 : CHK_PTR_NULL(len);
1888 0 : HCCLV2_FUNC_RUN(HcclGetAlgExecParamV2(tag, group, count, inputPtr, outputPtr, opType, clearEnable, dataType, op,
1889 : *commContext, *len, aivCoreLimit));
1890 :
1891 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
1892 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
1893 0 : CHK_RET(HcomGetCommByGroup(strGroup.c_str(), hcclComm));
1894 :
1895 0 : CHK_RET(hcclComm->HcclGetAlgExecParam(tag, count, inputPtr, outputPtr, opType, clearEnable, dataType, op,
1896 : *commContext, *len, aivCoreLimit));
1897 0 : return HCCL_SUCCESS;
1898 0 : }
1899 : // 取得所需的从stream数目
1900 6 : HcclResult HcomGetWorkspaceSubStreamNum(const char *group, u64 &streamNum, u64 dataSize, HcclDataType dataType, u32 aivCoreLimit,
1901 : HcclReduceOp reduceOp, u64 count, HcclCMDType optype)
1902 : {
1903 6 : HCCLV2_FUNC_RUN(HcomGetWorkspaceSubStreamNumV2(group, streamNum, dataSize, dataType, optype));
1904 6 : std::shared_ptr<hccl::hcclComm> hcclComm{};
1905 6 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
1906 6 : hcclComm = hcomInfo.pComm;
1907 6 : CHK_RET(HcomCheckGroupName(group));
1908 6 : HcclResult ret = HCCL_SUCCESS;
1909 6 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
1910 6 : if (strGroup != HCCL_WORLD_GROUP && hcomInfo.pComm != nullptr) {
1911 0 : std::unique_lock<std::mutex> groupParaLock(hcomInfo.groupParamsLock);
1912 0 : auto iter = hcomInfo.hcomGroupMap.find(strGroup);
1913 0 : if (iter != hcomInfo.hcomGroupMap.end()) {
1914 0 : hcclComm = (iter->second).pSubComm;
1915 : } else {
1916 0 : HCCL_WARNING("[HcomGetWorkspaceSubStreamNum], please check if the initialize process is called.");
1917 0 : streamNum = 0;
1918 : }
1919 6 : } else if (hcomInfo.pComm == nullptr) {
1920 6 : ret = HcclGetCommHandle(group, hcclComm);
1921 6 : CHK_PRT_RET(ret != HCCL_SUCCESS,
1922 : HCCL_WARNING("[HcomGetWorkspaceSubStreamNum], please check if the initialize process is called."),
1923 : HCCL_SUCCESS);
1924 : }
1925 2 : CHK_PRT_RET(hcclComm == nullptr,
1926 : HCCL_ERROR("[HcomGetWorkspaceSubStreamNum] Get Comm is null"), HCCL_E_PTR);
1927 :
1928 2 : string algName;
1929 2 : bool ifAiv = false;
1930 2 : void* counts = nullptr;
1931 2 : ret = hcclComm->HcclSelectAlg(optype, count, counts, dataType, reduceOp, aivCoreLimit, ifAiv, algName);
1932 2 : CHK_PRT_RET(ret != HCCL_SUCCESS,
1933 : HCCL_ERROR("[HcomGetWorkspaceSubStreamNum] HcclSelectAlg failed, ret[%d], optype[%d], count[%llu],"
1934 : "dataType[%d], reduceOp[%d]", ret, optype, count, dataType, reduceOp), ret);
1935 2 : CHK_RET(hcclComm->GetWorkspaceSubStreamNum(count, dataType, reduceOp, algName, streamNum, dataSize, ifAiv, optype));
1936 2 : HCCL_INFO("[%s] get streamNum[%llu] for optype[%d], algName[%s], count[%llu], dataType[%d], reduceOp[%d], ifAiv[%d]",
1937 : __func__, streamNum, optype, algName.c_str(), count, dataType, reduceOp, ifAiv);
1938 2 : return HCCL_SUCCESS;
1939 6 : }
1940 :
1941 0 : HcclResult HcomGetWorkspaceMemSize(const std::string &opType, u64 count, HcclDataType dataType, const char *group,
1942 : u64 &memSize)
1943 : {
1944 0 : HCCLV2_FUNC_RUN(HcomGetWorkspaceMemSizeV2(opType, count, dataType, group, memSize));
1945 0 : u32 rankSize = 0;
1946 0 : std::shared_ptr<hccl::hcclComm> hcclComm{};
1947 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
1948 0 : if (hcomInfo.pComm != nullptr) {
1949 0 : hcclComm = hcomInfo.pComm;
1950 0 : if (group == nullptr) {
1951 0 : group = HCCL_WORLD_GROUP;
1952 : }
1953 0 : CHK_RET(HcomGetRankSize(group, &rankSize));
1954 0 : CHK_RET(hcclComm->GetWorkspaceMemSize(opType, count, dataType, rankSize, memSize));
1955 0 : } else if (group != nullptr && HcclGetCommHandle(group, hcclComm) == HCCL_SUCCESS) {
1956 0 : CHK_RET(hcclComm->GetRankSize(rankSize));
1957 0 : CHK_RET(hcclComm->GetWorkspaceMemSize(opType, count, dataType, rankSize, memSize));
1958 : } else {
1959 0 : HCCL_WARNING("[GetWorkspaceMemSize] please check if the initialize process is called.");
1960 0 : memSize = 0;
1961 0 : return HCCL_SUCCESS;
1962 : }
1963 : /* 用户申请内存,获取memSize大小 */
1964 :
1965 0 : CHK_PRT_RET(memSize > DEVICE_MEMORY_MAX_ALLOC_SIZE,
1966 : HCCL_ERROR("[GetWorkspaceMemSize]workspace memory size is over than %llu bytes.", DEVICE_MEMORY_MAX_ALLOC_SIZE),
1967 : HCCL_E_PARA);
1968 0 : return HCCL_SUCCESS;
1969 0 : }
1970 :
1971 0 : HcclResult HcomGetAlltoAllStagedWorkSpaceMemSize(const char *group, u64 *sendCounts, u64 *sdispls,
1972 : HcclDataType sendType, u64 *recvCounts, u64 *rdispls, HcclDataType recvType, u64 &memSize)
1973 : {
1974 0 : CHK_PTR_NULL(sendCounts);
1975 0 : CHK_PTR_NULL(sdispls);
1976 0 : CHK_PTR_NULL(recvCounts);
1977 0 : CHK_PTR_NULL(rdispls);
1978 :
1979 0 : HCCLV2_FUNC_RUN(HcomGetAlltoAllStagedWorkSpaceMemSizeV2(group, sendCounts, sdispls, sendType, recvCounts,
1980 : rdispls, recvType, memSize));
1981 0 : CHK_RET(HcomCheckDataType(sendType));
1982 0 : CHK_RET(HcomCheckDataType(recvType));
1983 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
1984 :
1985 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
1986 0 : CHK_RET(HcomGetCommByGroup(strGroup.c_str(), hcclComm));
1987 0 : CHK_RET(hcclComm->GetAlltoAllStagedWorkSpaceMemSize(sendCounts, sdispls, sendType,
1988 : recvCounts, rdispls, recvType, memSize));
1989 0 : return HCCL_SUCCESS;
1990 0 : }
1991 :
1992 0 : HcclResult HcomGetAlltoAllvcStagedWorkSpaceMemSize(const char *group,
1993 : std::vector<SendRecvInfo> &allMeshAggregationSendRecvInfo, u64 &memSize)
1994 : {
1995 0 : HCCLV2_FUNC_RUN(HcomGetAlltoAllvcStagedWorkSpaceMemSizeV2(group, memSize));
1996 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
1997 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
1998 0 : CHK_RET(HcomGetCommByGroup(strGroup.c_str(), hcclComm));
1999 0 : CHK_RET(hcclComm->GetAlltoAllStagedWorkSpaceMemSize(allMeshAggregationSendRecvInfo, memSize));
2000 0 : return HCCL_SUCCESS;
2001 0 : }
2002 :
2003 0 : HcclResult HcomGetAllReduceScratchSize(const char *group, const u64 count, const HcclDataType dataType,
2004 : u64 &outScratchSize)
2005 : {
2006 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
2007 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
2008 0 : if (hcomInfo.pComm != nullptr) {
2009 0 : hcclComm = hcomInfo.pComm;
2010 0 : CHK_RET(HcomCheckGroupName(group));
2011 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
2012 0 : if (strGroup == HCCL_WORLD_GROUP) {
2013 0 : hcclComm = hcomInfo.pComm;
2014 : } else {
2015 0 : std::unique_lock<std::mutex> groupParaLock(hcomInfo.groupParamsLock);
2016 0 : auto iter = hcomInfo.hcomGroupMap.find(strGroup);
2017 0 : if (iter != hcomInfo.hcomGroupMap.end()) {
2018 0 : hcclComm = (iter->second).pSubComm;
2019 0 : CHK_PRT_RET(hcclComm == nullptr, HCCL_ERROR("[Get][CommByGroup] Get Comm is null"), HCCL_E_PTR);
2020 : } else {
2021 0 : u64 memSize = SIZE_TABLE[dataType] * count;
2022 0 : const u32 DEVICE_EIGHT = 8;
2023 0 : if (memSize <= HCCL_SMALL_COUNT_GRAPH_64_KB) {
2024 : // 小数据
2025 0 : outScratchSize = memSize * (DEVICE_EIGHT - 1);
2026 : }
2027 0 : HCCL_DEBUG("[HcomGetAllReduceScratchSize] outScratchSize %llu", outScratchSize);
2028 0 : groupParaLock.unlock();
2029 0 : return HCCL_SUCCESS;
2030 : }
2031 0 : groupParaLock.unlock();
2032 0 : }
2033 0 : } else if (group == nullptr || HcclGetCommHandle(group, hcclComm) != HCCL_SUCCESS) {
2034 0 : HCCL_WARNING("[GetAllReduceScratchSize], please check if the initialize process is called.");
2035 0 : outScratchSize = 0;
2036 0 : return HCCL_SUCCESS;
2037 : }
2038 0 : return hcclComm->GetAllReduceScratchSize(count, dataType, outScratchSize);
2039 0 : }
2040 :
2041 :
2042 0 : HcclResult HcomGetCCLBufferAvailableSize(u64 &size)
2043 : {
2044 0 : size = GetExternalInputCCLBuffSize() - CCL_COMM_INBUFFER_UNALIGNED_RESERVE_SIZE;
2045 0 : return HCCL_SUCCESS;
2046 : }
2047 :
2048 0 : HcclResult HcomCheckCommValidity(const char* group)
2049 : {
2050 0 : HCCLV2_FUNC_RUN(HcomCheckCommValidityV2(group));
2051 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
2052 0 : if (HcomGetCommByGroup(group, hcclComm) != HCCL_SUCCESS) {
2053 0 : return HCCL_E_PTR;
2054 : }
2055 :
2056 0 : return HCCL_SUCCESS;
2057 0 : }
2058 :
2059 0 : HcclResult HcomSetWorkspaceResource(const char *tag, const char *group, rtStream_t *stream,
2060 : s32 len, void *memPtr, u64 maxSize)
2061 : {
2062 0 : if (len < 0) {
2063 0 : HCCL_ERROR("[HcomSetWorkspaceResource] len is %d", len);
2064 0 : return HCCL_E_PARA;
2065 : }
2066 0 : std::vector<rtStream_t> rtStream(stream, stream + len);
2067 :
2068 0 : HCCLV2_FUNC_RUN(HcomSetWorkspaceResourceV2(tag, group, rtStream, memPtr, maxSize));
2069 0 : if (group == nullptr) {
2070 0 : group = HCCL_WORLD_GROUP;
2071 : }
2072 :
2073 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
2074 0 : if (HcomGetCommByGroup(group, hcclComm) == HCCL_SUCCESS) {
2075 : /* 设定 workspace 内存资源 */
2076 0 : CHK_RET(hcclComm->SetWorkspaceResource(tag, memPtr, maxSize, rtStream));
2077 : }
2078 :
2079 0 : return HCCL_SUCCESS;
2080 0 : }
2081 :
2082 0 : HcclResult HcomSetAttachedStream(const char *group, u32 graphId, const rtStream_t *stream, s32 len)
2083 : {
2084 0 : if (len < 0) {
2085 0 : HCCL_ERROR("[HcomSetAttachedStream] len is %d", len);
2086 0 : return HCCL_E_PARA;
2087 : }
2088 0 : if (group == nullptr) {
2089 0 : group = HCCL_WORLD_GROUP;
2090 : }
2091 :
2092 0 : HCCLV2_FUNC_RUN(HcomSetAttachedStreamV2());
2093 0 : std::shared_ptr<hccl::hcclComm> hcclComm = nullptr;
2094 0 : std::vector<rtStream_t> rtStream(stream, stream + len);
2095 0 : if (HcomGetCommByGroup(group, hcclComm) == HCCL_SUCCESS) {
2096 0 : CHK_RET(hcclComm->SetAttachedStream(graphId, rtStream));
2097 : } else {
2098 : // HcclCommBase 场景暂是不支持设置附属从流
2099 0 : HCCL_WARNING("[HcomSetAttachedStream] HcclCommBase now don't support set attached stream");
2100 0 : return HCCL_SUCCESS;
2101 : }
2102 0 : return HCCL_SUCCESS;
2103 0 : }
2104 :
2105 0 : HcclResult HcclCommSetAttachedStream(s64 opBaseHcom, u32 graphId, const std::vector<rtStream_t> &stream)
2106 : {
2107 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
2108 0 : CHK_PTR_NULL(hcclComm);
2109 0 : CHK_RET(hcclComm->SetAttachedStream(graphId, stream));
2110 :
2111 0 : return HCCL_SUCCESS;
2112 : }
2113 :
2114 0 : void HcomSetAutoTuneMode(bool autoTuneMode)
2115 : {
2116 0 : bool &isAutoTuneModeOpen = HcomGetCtxAutoTuneMode();
2117 0 : isAutoTuneModeOpen = autoTuneMode;
2118 0 : }
2119 :
2120 0 : HcclResult HcomSetExecTimeOut(const char *execTimeOut)
2121 : {
2122 0 : HCCL_RUN_INFO("HcomSetExecTimeOut:execTimeOut[%s]s", execTimeOut);
2123 0 : if(execTimeOut == nullptr) {
2124 0 : return HCCL_SUCCESS;
2125 : }
2126 0 : CHK_RET(SetHccLExecTimeOut(execTimeOut, HcclExecTimeoutSet::HCCL_EXEC_TIMEOUT_SET_BY_OPTIONS));
2127 0 : return HCCL_SUCCESS;
2128 : }
2129 :
2130 0 : HcclResult HcomSetAlgorithm(const char* algo)
2131 : {
2132 0 : HCCL_RUN_INFO("HcomSetAlgorithm:algo[%s]", algo);
2133 0 : if(algo == nullptr) {
2134 0 : return HCCL_SUCCESS;
2135 : }
2136 0 : CHK_RET(SetHcclAlgoConfig(algo));
2137 0 : return HCCL_SUCCESS;
2138 : }
2139 :
2140 0 : HcclResult HcomSetDeterministic(u8 deterministic)
2141 : {
2142 0 : HCCL_RUN_INFO("HcomSetDeterministic:deterministic[%u]", deterministic);
2143 0 : CHK_RET(SetDeterministic(deterministic));
2144 0 : return HCCL_SUCCESS;
2145 : }
2146 :
2147 0 : HcclResult HcomGetAlgorithm(u32 level, char** algo)
2148 : {
2149 0 : CHK_PTR_NULL(algo);
2150 0 : std::string str = "none";
2151 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
2152 0 : CHK_PRT_RET(hcomInfo.pComm == nullptr,
2153 : HCCL_ERROR("[Get][Algorithm]hcomInfo.pComm is null, "\
2154 : "please check if the initialize process is called."),
2155 : HCCL_E_PTR);
2156 0 : AlgType algType;
2157 0 : CHK_RET(hcomInfo.pComm->GetAlgType(algType, HcclCMDType::HCCL_CMD_ALL));
2158 0 : if (level == 0) {
2159 0 : if (algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_WHOLE_RING ||
2160 0 : algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_8P_RING ||
2161 0 : algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_4P_RING ||
2162 0 : algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_NP_SINGLE_RING ||
2163 0 : algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_NP_DOUBLE_RING) {
2164 0 : str = "ring";
2165 0 : } else if (algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_NP_MESH ||
2166 0 : algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_4P_MESH ||
2167 0 : algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_2P_MESH ||
2168 0 : algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_1P_MESH) {
2169 0 : str = "mesh";
2170 0 : } else if (algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_RESERVED &&
2171 0 : algType.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_NHR) {
2172 0 : str = "NHR";
2173 0 : } else if (algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_RESERVED &&
2174 0 : algType.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_NHR_V1) {
2175 0 : str = "NHR_V1";
2176 0 : } else if (algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_RESERVED &&
2177 0 : algType.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_AHC) {
2178 0 : str = "AHC";
2179 0 : } else if (algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_RESERVED &&
2180 0 : algType.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_AHC_BROKE) {
2181 0 : str = "AHC_BROKE";
2182 0 : } else if (algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_RESERVED &&
2183 0 : algType.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_NB) {
2184 0 : str = "NB";
2185 : } else {
2186 0 : HCCL_WARNING("[Get][Algorithm] No valid Level 0 AlgType, which is [%d]",
2187 : static_cast<s32>(algType.algoLevel0));
2188 0 : return HCCL_E_NOT_FOUND;
2189 : }
2190 0 : } else if (level == 1) {
2191 0 : if (algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_WHOLE_RING ||
2192 0 : (algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_RESERVED &&
2193 0 : algType.algoLevel1 != AlgTypeLevel1::ALG_LEVEL1_RESERVED)) {
2194 0 : str = "none";
2195 0 : } else if (algType.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_HD) {
2196 0 : str = "H-D";
2197 0 : } else if (algType.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_PIPELINE ||
2198 0 : algType.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_RING) {
2199 0 : str = "ring";
2200 0 : } else if (algType.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_NHR) {
2201 0 : str = "NHR";
2202 0 : } else if (algType.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_NHR_V1) {
2203 0 : str = "NHR_V1";
2204 0 : } else if (algType.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_AHC) {
2205 0 : str = "AHC";
2206 0 : } else if (algType.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_AHC_BROKE) {
2207 0 : str = "AHC_BROKE";
2208 0 : } else if (algType.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_NB) {
2209 0 : str = "NB";
2210 : } else {
2211 0 : HCCL_WARNING("[Get][Algorithm] No valid Level 1 AlgType, which is [%d]",
2212 : static_cast<s32>(algType.algoLevel1));
2213 0 : return HCCL_E_NOT_FOUND;
2214 : }
2215 : }
2216 0 : *algo = const_cast<char *>(str.c_str());
2217 0 : return HCCL_SUCCESS;
2218 0 : }
2219 :
2220 0 : HcclResult HcomGetBandWidthPerNPU(u32 level, float *bandWidth)
2221 : {
2222 0 : CHK_PTR_NULL(bandWidth);
2223 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
2224 0 : CHK_PRT_RET(hcomInfo.pComm == nullptr, HCCL_ERROR("[Get][BandWidth]hcomInfo.pComm is null, "\
2225 : "please check if the initialize process is called."), HCCL_E_PTR);
2226 0 : CHK_RET(hcomInfo.pComm->GetBandWidthPerNPU(level, *bandWidth));
2227 0 : return HCCL_SUCCESS;
2228 : }
2229 :
2230 0 : HcclResult HcomReleaseSubComms()
2231 : {
2232 0 : HCCLV2_FUNC_RUN(HcomReleaseSubCommsV2());
2233 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
2234 0 : if (hcomInfo.pComm) {
2235 0 : CHK_RET(hcomInfo.pComm->ReleaseSubComms());
2236 : }
2237 :
2238 0 : auto iter = hcomInfo.hcomGroupMap.begin();
2239 0 : while (iter != hcomInfo.hcomGroupMap.end()) {
2240 0 : if (iter->second.pSubComm) {
2241 0 : CHK_RET(iter->second.pSubComm->ReleaseSubComms());
2242 : }
2243 0 : iter++;
2244 : }
2245 0 : return HCCL_SUCCESS;
2246 : }
2247 :
2248 0 : HcclResult HcomAlltoAllV(const void *sendBuf, const void *sendCounts, const void *sdispls, HcclDataType sendType,
2249 : const void *recvBuf, const void *recvCounts, const void *rdispls, HcclDataType recvType,
2250 : const char *group, rtStream_t stream, const char *tag)
2251 : {
2252 0 : HcclUs startut = TIME_NOW();
2253 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
2254 : // 入参合法性校验
2255 0 : CHK_PTR_NULL(sendCounts);
2256 0 : CHK_PTR_NULL(sdispls);
2257 0 : CHK_PTR_NULL(recvCounts);
2258 0 : CHK_PTR_NULL(rdispls);
2259 0 : RPT_INPUT_ERR(stream == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
2260 : std::vector<std::string>({"HcomAlltoAllV", "nullptr", "stream", "non-null pointer"}));
2261 0 : CHK_PTR_NULL(stream);
2262 0 : s32 streamId = 0;
2263 0 : CHK_RET(hrtGetStreamId(stream, streamId));
2264 0 : s32 deviceLogicId = 0;
2265 0 : CHK_RET(hrtGetDevice(&deviceLogicId));
2266 :
2267 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
2268 0 : if (sendBuf != nullptr) {
2269 0 : CHK_RET(PrintMemoryAttr(sendBuf));
2270 : }
2271 0 : if (recvBuf != nullptr) {
2272 0 : CHK_RET(PrintMemoryAttr(recvBuf));
2273 : }
2274 :
2275 : /* 接口交互信息日志 */
2276 0 : HCCL_USER_CRITICAL_LOG("Entry-HcomAlltoAllV:tag[%s], sendBuf[%p], recvBuf[%p], sendCounts[%p], recvCounts[%p],"
2277 : "sendType[%s], recvType[%s], group[%s], streamId[%d], deviceLogicId[%d]",
2278 : tag, sendBuf, recvBuf, sendCounts, recvCounts, GetDataTypeEnumStr(sendType).c_str(),
2279 : GetDataTypeEnumStr(recvType).c_str(), strGroup.c_str(), streamId, deviceLogicId);
2280 :
2281 : // HcomAlltoAllV2
2282 0 : HCCLV2_FUNC_RUN(HcomAlltoAllVV2(sendBuf, sendCounts, sdispls, sendType, recvBuf, recvCounts, rdispls, recvType,
2283 : group, stream, tag));
2284 0 : CHK_RET(HcomCheckOpParam(tag, 0, sendType, group, stream));
2285 0 : CHK_RET(HcomCheckDataType(recvType));
2286 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
2287 0 : CHK_RET(HcomGetCommByGroup(strGroup.c_str(), hcclComm));
2288 :
2289 : // 根据ranksize校验相关入参
2290 0 : u32 rankSize = 0;
2291 0 : CHK_RET(hcclComm->GetRankSize(rankSize));
2292 0 : CHK_RET(HcomCheckAlltoAllVExternalMem(sendBuf, sendCounts, recvBuf, recvCounts, rankSize));
2293 0 : u32 rankId = 0;
2294 0 : CHK_RET(hcclComm->GetUserRank(rankId));
2295 0 : u32 aivCoreLimit = 0;
2296 0 : CHK_RET(hcclComm->GetNumBlocks(aivCoreLimit));
2297 0 : HcclWorkflowMode mode = GetWorkflowMode();
2298 0 : CHK_PRT_RET(mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_RESERVED, HCCL_ERROR("Invalid Workflow Mode[%d]", mode),
2299 : HCCL_E_INTERNAL);
2300 0 : CHK_RET(hcclComm->AlltoAllV(sendBuf, sendCounts, sdispls, sendType, recvBuf, recvCounts, rdispls, recvType,
2301 : stream, tag));
2302 :
2303 0 : u64 sendCount = 0;
2304 0 : for (u32 i = 0; i < rankSize; i++) {
2305 0 : sendCount += *(static_cast<const u64 *>(sendCounts) + i);
2306 : }
2307 0 : CHK_RET(CallMsprofReportHostApi(hcclComm.get(), HcclCMDType::HCCL_CMD_ALLTOALLV, beginTime, sendCount, sendType));
2308 : /* 关键状态记录 */
2309 0 : HcclUs endut = TIME_NOW();
2310 0 : HCCL_RUN_INFO("HcomAlltoAllV success,take time [%lld]us, tag[%s], sendBuf[%p], recvBuf[%p], sendCounts[%p], "\
2311 : "recvCounts[%p], sendType[%s], recvType[%s], group[%s], streamId[%d]", DURATION_US(endut - startut),
2312 : tag, sendBuf, recvBuf, sendCounts, recvCounts, GetDataTypeEnumStr(sendType).c_str(),
2313 : GetDataTypeEnumStr(recvType).c_str(), strGroup.c_str(), streamId);
2314 0 : return HCCL_SUCCESS;
2315 0 : }
2316 :
2317 0 : HcclResult HcomAlltoAllVC(const void *sendBuf, const void *sendCountMatrix, HcclDataType sendType,
2318 : const void *recvBuf, HcclDataType recvType, const char *group, rtStream_t stream, const char *tag)
2319 : {
2320 0 : HcclUs startut = TIME_NOW();
2321 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
2322 : // 入参合法性校验
2323 0 : CHK_PTR_NULL(sendCountMatrix);
2324 0 : RPT_INPUT_ERR(stream == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
2325 : std::vector<std::string>({"HcomAlltoAllVC", "nullptr", "stream", "non-null pointer"}));
2326 0 : CHK_PTR_NULL(stream);
2327 0 : s32 streamId = 0;
2328 0 : CHK_RET(hrtGetStreamId(stream, streamId));
2329 0 : s32 deviceLogicId = 0;
2330 0 : CHK_RET(hrtGetDevice(&deviceLogicId));
2331 :
2332 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
2333 0 : if (sendBuf != nullptr) {
2334 0 : CHK_RET(PrintMemoryAttr(sendBuf));
2335 : }
2336 0 : if (recvBuf != nullptr) {
2337 0 : CHK_RET(PrintMemoryAttr(recvBuf));
2338 : }
2339 : // HcomAlltoAllVCV2
2340 0 : HCCLV2_FUNC_RUN(HcomAlltoAllVCV2(sendBuf, sendCountMatrix, sendType, recvBuf, recvType, group, stream, tag));
2341 0 : CHK_RET(HcomCheckOpParam(tag, 0, sendType, group, stream));
2342 0 : CHK_RET(HcomCheckDataType(recvType));
2343 :
2344 : // 根据ranksize校验相关入参
2345 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
2346 0 : CHK_RET(HcomGetCommByGroup(strGroup.c_str(), hcclComm));
2347 0 : u32 rankSize = 0;
2348 0 : CHK_RET(hcclComm->GetRankSize(rankSize));
2349 0 : u32 userRank = 0;
2350 0 : hcclComm->GetGroupRank(userRank);
2351 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
2352 0 : u32 rank = hcomInfo.params.userRank;
2353 0 : CHK_RET(HcomCheckAlltoAllVCExternalMem(sendBuf, sendCountMatrix, recvBuf, rankSize, rank));
2354 :
2355 : u64 sendCountMatrixHash;
2356 0 : HcomGetHashFromSendCountMatrix(sendCountMatrixHash, sendCountMatrix, rankSize, tag);
2357 :
2358 : /* 接口交互信息日志 */
2359 0 : HCCL_USER_CRITICAL_LOG("Entry-HcomAlltoAllVC:tag[%s], sendBuf[%p], sendCountMatrixHash[%llu], sendType[%s], "\
2360 : "recvBuf[%p], recvType[%s], group[%s], streamId[%d], deviceLogicId[%d]",
2361 : tag, sendBuf, sendCountMatrixHash, GetDataTypeEnumStr(sendType).c_str(),
2362 : recvBuf, GetDataTypeEnumStr(recvType).c_str(), strGroup.c_str(), streamId, deviceLogicId);
2363 :
2364 0 : u32 aivCoreLimit = 0;
2365 0 : CHK_RET(hcclComm->GetNumBlocks(aivCoreLimit));
2366 0 : HcclWorkflowMode mode = GetWorkflowMode();
2367 0 : CHK_PRT_RET(mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_RESERVED, HCCL_ERROR("Invalid Workflow Mode[%d]", mode),
2368 : HCCL_E_INTERNAL);
2369 : /* 入参的正确性由HCCL确保 */
2370 0 : CHK_RET(hcclComm->AlltoAllVC(sendBuf, sendCountMatrix, sendType, recvBuf, recvType, stream, tag));
2371 :
2372 0 : u64 sendCount = 0;
2373 0 : for (u32 i = 0; i < rankSize; i++) {
2374 0 : sendCount += *(static_cast<const u64 *>(sendCountMatrix) + userRank * rankSize + i);
2375 : }
2376 0 : CHK_RET(CallMsprofReportHostApi(hcclComm.get(), HcclCMDType::HCCL_CMD_ALLTOALLVC, beginTime, sendCount, sendType));
2377 : /* 关键状态记录 */
2378 0 : HcclUs endut = TIME_NOW();
2379 0 : HCCL_RUN_INFO("HcomAlltoAllVC success,take time [%lld]us, tag[%s], sendBuf[%p], sendCountMatrix[%p], "\
2380 : "sendType[%s], recvBuf[%p], recvType[%s], group[%s], streamId[%d]", DURATION_US(endut - startut),
2381 : tag, sendBuf, sendCountMatrix, GetDataTypeEnumStr(sendType).c_str(), recvBuf,
2382 : GetDataTypeEnumStr(recvType).c_str(), strGroup.c_str(), streamId);
2383 0 : return HCCL_SUCCESS;
2384 0 : }
2385 :
2386 0 : HcclResult HcclCommGraphAlltoAllV(const void *sendBuf, const void *sendCounts, const void *sdispls,
2387 : HcclDataType sendType, const void *recvBuf, const void *recvCounts, const void *rdispls,
2388 : HcclDataType recvType, s64 opBaseHcom, rtStream_t stream, const char *tag)
2389 : {
2390 0 : HcclUs startut = TIME_NOW();
2391 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
2392 : // 入参合法性校验
2393 0 : CHK_PTR_NULL(sendCounts);
2394 0 : CHK_PTR_NULL(sdispls);
2395 0 : CHK_PTR_NULL(recvCounts);
2396 0 : CHK_PTR_NULL(rdispls);
2397 :
2398 0 : CHK_RET(HcomCheckOpParam(tag, 0, sendType, stream));
2399 0 : CHK_RET(HcomCheckDataType(recvType));
2400 0 : s32 streamId = 0;
2401 0 : CHK_RET(hrtGetStreamId(stream, streamId));
2402 0 : s32 deviceLogicId = 0;
2403 0 : CHK_RET(hrtGetDevice(&deviceLogicId));
2404 :
2405 : // 根据ranksize校验相关入参
2406 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
2407 0 : CHK_PTR_NULL(hcclComm);
2408 0 : CHK_RET(SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB));
2409 0 : u32 rankSize = 0;
2410 0 : CHK_RET(hcclComm->GetRankSize(rankSize));
2411 0 : u32 rankId = 0;
2412 0 : CHK_RET(hcclComm->GetUserRank(rankId));
2413 0 : CHK_RET(HcomCheckAlltoAllVExternalMem(sendBuf, sendCounts, recvBuf, recvCounts, rankSize));
2414 0 : if (sendBuf != nullptr) {
2415 0 : CHK_RET(PrintMemoryAttr(sendBuf));
2416 : }
2417 0 : if (recvBuf != nullptr) {
2418 0 : CHK_RET(PrintMemoryAttr(recvBuf));
2419 : }
2420 :
2421 : /* 接口交互信息日志 */
2422 0 : HCCL_USER_CRITICAL_LOG("Entry-HcomAlltoAllV:tag[%s], sendBuf[%p], recvBuf[%p], sendCounts[%p], recvCounts[%p],"
2423 : "sendType[%s], recvType[%s], streamId[%d], deviceLogicId[%d]",
2424 : tag, sendBuf, recvBuf, sendCounts, recvCounts, GetDataTypeEnumStr(sendType).c_str(),
2425 : GetDataTypeEnumStr(recvType).c_str(), streamId, deviceLogicId);
2426 :
2427 0 : u32 aivCoreLimit = 0;
2428 0 : CHK_RET(hcclComm->GetNumBlocks(aivCoreLimit));
2429 0 : HcclWorkflowMode mode = GetWorkflowMode();
2430 0 : CHK_PRT_RET(mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_RESERVED, HCCL_ERROR("Invalid Workflow Mode[%d]", mode),
2431 : HCCL_E_INTERNAL);
2432 : /* 入参的正确性由HCCL确保 */
2433 0 : CHK_RET(hcclComm->AlltoAllV(sendBuf, sendCounts, sdispls, sendType, recvBuf, recvCounts, rdispls, recvType,
2434 : stream, tag));
2435 0 : u64 sendCount = 0;
2436 0 : for (u32 i = 0; i < rankSize; i++) {
2437 0 : sendCount += *(static_cast<const u64 *>(sendCounts) + i);
2438 : }
2439 0 : CHK_RET(CallMsprofReportHostApi(hcclComm, HcclCMDType::HCCL_CMD_ALLTOALLV, beginTime, sendCount, sendType));
2440 : /* 关键状态记录 */
2441 0 : HcclUs endut = TIME_NOW();
2442 0 : HCCL_RUN_INFO("HcomAlltoAllV success,take time [%lld]us, tag[%s], sendBuf[%p], recvBuf[%p], sendCounts[%p], "
2443 : "recvCounts[%p], sendType[%s], recvType[%s], streamId[%d]",
2444 : DURATION_US(endut - startut), tag, sendBuf, recvBuf, sendCounts, recvCounts,
2445 : GetDataTypeEnumStr(sendType).c_str(), GetDataTypeEnumStr(recvType).c_str(), streamId);
2446 0 : return HCCL_SUCCESS;
2447 : }
2448 :
2449 0 : HcclResult HcclCommGraphAlltoAllVC(const void *sendBuf, const void *sendCountMatrix, HcclDataType sendType,
2450 : const void *recvBuf, HcclDataType recvType, s64 opBaseHcom, rtStream_t stream, const char *tag)
2451 : {
2452 0 : HcclUs startut = TIME_NOW();
2453 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
2454 : // 入参合法性校验
2455 0 : CHK_PTR_NULL(sendCountMatrix);
2456 :
2457 0 : CHK_RET(HcomCheckOpParam(tag, 0, sendType, stream));
2458 0 : CHK_RET(HcomCheckDataType(recvType));
2459 0 : s32 streamId = 0;
2460 0 : CHK_RET(hrtGetStreamId(stream, streamId));
2461 0 : s32 deviceLogicId = 0;
2462 0 : CHK_RET(hrtGetDevice(&deviceLogicId));
2463 :
2464 : // 根据ranksize校验相关入参
2465 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
2466 0 : CHK_PTR_NULL(hcclComm);
2467 0 : CHK_RET(SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB));
2468 0 : u32 rankSize = 0;
2469 0 : CHK_RET(hcclComm->GetRankSize(rankSize));
2470 0 : u32 userRank = 0;
2471 0 : hcclComm->GetGroupRank(userRank);
2472 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
2473 0 : u32 rank = hcomInfo.params.userRank;
2474 0 : CHK_RET(HcomCheckAlltoAllVCExternalMem(sendBuf, sendCountMatrix, recvBuf, rankSize, rank));
2475 0 : if (sendBuf != nullptr) {
2476 0 : CHK_RET(PrintMemoryAttr(sendBuf));
2477 : }
2478 0 : if (recvBuf != nullptr) {
2479 0 : CHK_RET(PrintMemoryAttr(recvBuf));
2480 : }
2481 :
2482 : u64 sendCountMatrixHash;
2483 0 : HcomGetHashFromSendCountMatrix(sendCountMatrixHash, sendCountMatrix, rankSize, tag);
2484 :
2485 : /* 接口交互信息日志 */
2486 0 : HCCL_USER_CRITICAL_LOG("Entry-HcomAlltoAllVC:tag[%s], sendBuf[%p], sendCountMatrixHash[%llu], sendType[%s], "\
2487 : "recvBuf[%p], recvType[%s], streamId[%d], deviceLogicId[%d]",
2488 : tag, sendBuf, sendCountMatrixHash, GetDataTypeEnumStr(sendType).c_str(), recvBuf,
2489 : GetDataTypeEnumStr(recvType).c_str(), streamId, deviceLogicId);
2490 :
2491 0 : u32 aivCoreLimit = 0;
2492 0 : CHK_RET(hcclComm->GetNumBlocks(aivCoreLimit));
2493 0 : HcclWorkflowMode mode = GetWorkflowMode();
2494 0 : CHK_PRT_RET(mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_RESERVED, HCCL_ERROR("Invalid Workflow Mode[%d]", mode),
2495 : HCCL_E_INTERNAL);
2496 : /* 入参的正确性由HCCL确保 */
2497 0 : CHK_RET(hcclComm->AlltoAllVC(sendBuf, sendCountMatrix, sendType, recvBuf, recvType, stream, tag));
2498 :
2499 0 : u64 sendCount = 0;
2500 0 : for (u32 i = 0; i < rankSize; i++) {
2501 0 : sendCount += *(static_cast<const u64 *>(sendCountMatrix) + userRank * rankSize + i);
2502 : }
2503 0 : CHK_RET(CallMsprofReportHostApi(hcclComm, HcclCMDType::HCCL_CMD_ALLTOALLVC, beginTime, sendCount, sendType));
2504 : /* 关键状态记录 */
2505 0 : HcclUs endut = TIME_NOW();
2506 0 : HCCL_RUN_INFO("HcomAlltoAllVC success, take time [%lld]us, tag[%s], sendBuf[%p], recvBuf[%p], sendCountMatrix[%p], "\
2507 : "sendType[%s], recvType[%s], streamId[%d]", DURATION_US(endut - startut),
2508 : tag, sendBuf, recvBuf, sendCountMatrix, GetDataTypeEnumStr(sendType).c_str(),
2509 : GetDataTypeEnumStr(recvType).c_str(), streamId);
2510 0 : return HCCL_SUCCESS;
2511 : }
2512 :
2513 0 : HcclResult HcomUnloadTask(const char *group, const char *tag)
2514 : {
2515 0 : CHK_PTR_NULL(tag);
2516 0 : HCCLV2_FUNC_RUN(HcomUnloadTaskV2(group, tag));
2517 0 : std::shared_ptr<hcclComm> hcclComm;
2518 0 : if (HcomGetCommByGroup(group, hcclComm) == HCCL_SUCCESS) {
2519 0 : CHK_PRT_RET(hcclComm == nullptr, HCCL_WARNING("[UnloadAllTask]hcclComm is null, "\
2520 : "please check if the initialize process is called."), HCCL_SUCCESS);
2521 0 : HCCL_INFO("[UnloadTask]HcomUnloadTask: tag[%s]", tag);
2522 0 : CHK_RET(hcclComm->ClearOpResource(tag));
2523 : }
2524 :
2525 0 : return HCCL_SUCCESS;
2526 0 : }
2527 :
2528 0 : HcclResult HcomGetServerNumAndDeviceNumPerServer(u32 *serverNum, u32 *deviceNumPerServer, u32 *deviceNumPerAggregation)
2529 : {
2530 0 : CHK_PTR_NULL(serverNum);
2531 0 : CHK_PTR_NULL(deviceNumPerServer);
2532 0 : CHK_PTR_NULL(deviceNumPerAggregation);
2533 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
2534 0 : CHK_PRT_RET(hcomInfo.pComm == nullptr,
2535 : HCCL_ERROR("[GetServerNumAndDeviceNumPerServer]hcomInfo.pComm is null, "\
2536 : "please check if the initialize process is called."), HCCL_E_INTERNAL);
2537 : u32 totalDevNum;
2538 0 : *serverNum = hcomInfo.rankTable.serverNum;
2539 0 : totalDevNum = hcomInfo.rankTable.deviceNum;
2540 0 : if ((totalDevNum % *serverNum) != 0) {
2541 0 : HCCL_ERROR("devicenum is not Integer.");
2542 : }
2543 0 : *deviceNumPerServer = totalDevNum / *serverNum;
2544 :
2545 0 : CHK_RET(hcomInfo.pComm->GetDeviceNumPerAggregation(*deviceNumPerAggregation));
2546 :
2547 0 : return HCCL_SUCCESS;
2548 : }
2549 :
2550 : static RankTable_t g_rankTableSetInfo;
2551 0 : HcclResult HcomSetRankTableImpl(const char *rankTableStr)
2552 : {
2553 0 : HCCL_RUN_INFO("Entry-HcomSetRankTable: rankTable \"%s\"", rankTableStr);
2554 0 : u32 rankTableSize = 0;
2555 0 : HcclResult ret = HcomCheckRankTable(rankTableStr, rankTableSize);
2556 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
2557 : HCCL_ERROR("[Init][Result]errNo[0x%016llx] input rankTable error", HCOM_ERROR_CODE(ret)), ret);
2558 :
2559 0 : std::string identify = "0";
2560 0 : HcomInfo hcomInfo;
2561 0 : ret = CfgGetClusterInfoWithoutDev(rankTableStr, identify, hcomInfo.params, hcomInfo.rankTable);
2562 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Init][Result]errNo[0x%016llx] cfg get ranktable[%p] info error: "\
2563 : "identify[%s]", HCOM_ERROR_CODE(ret), rankTableStr, identify.c_str()), ret);
2564 :
2565 0 : g_rankTableSetInfo = hcomInfo.rankTable;
2566 0 : return HCCL_SUCCESS;
2567 0 : }
2568 :
2569 0 : HcclResult HcomGetActualRankSizeImpl(const char *group, u32 *rankSize)
2570 : {
2571 : (void)group;
2572 0 : *rankSize = g_rankTableSetInfo.rankNum;
2573 0 : return HCCL_SUCCESS;
2574 : }
2575 :
2576 0 : HcclResult HcclCommGraphUnloadTask(s64 opBaseHcom, const char *tag)
2577 : {
2578 : #if (!defined (HCCD)) && (!defined (CCL_KERNEL_AICPU))
2579 0 : CHK_RET(SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB));
2580 0 : HCCLV2_FUNC_RUN([&]() -> HcclResult {
2581 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm *>(opBaseHcom);
2582 : CHK_PTR_NULL(hcclComm);
2583 : HcclComm commV2 = hcclComm->GetCommunicatorV2();
2584 : CHK_PTR_NULL(commV2);
2585 : opBaseHcom = reinterpret_cast<s64>(commV2);
2586 : CHK_RET(HcclCommGraphUnloadTaskV2(opBaseHcom, tag));
2587 : return HCCL_SUCCESS;
2588 : }());
2589 : #endif
2590 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
2591 0 : CHK_PRT_RET(hcclComm == nullptr, HCCL_WARNING("[HcclCommGraphUnloadTask]hcclComm is null, "\
2592 : "please check if the initialize process is called."), HCCL_SUCCESS);
2593 0 : CHK_RET(SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB));
2594 0 : HCCL_INFO("[UnloadTask]HcclCommGraphUnloadTask: tag[%s]", tag);
2595 0 : CHK_RET(hcclComm->ClearOpResource(tag));
2596 0 : return HCCL_SUCCESS;
2597 : }
2598 :
2599 0 : HcclResult HcomSetGlobalWorkSpace(const char *group, void **globalWorkSpaceAddr, u32 len)
2600 : {
2601 0 : std::vector<void *> workspaceAddrVec(globalWorkSpaceAddr, globalWorkSpaceAddr + len);
2602 0 : HCCLV2_FUNC_RUN(HcomSetGlobalWorkSpaceV2(group, workspaceAddrVec));
2603 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
2604 0 : std::vector<void *> globalWorkSpaceAdd(globalWorkSpaceAddr, globalWorkSpaceAddr + len);
2605 0 : CHK_RET(HcomGetCommByGroup(group, hcclComm));
2606 0 : CHK_RET(hcclComm->SetGlobalWorkSpace(globalWorkSpaceAdd));
2607 0 : return HCCL_SUCCESS;
2608 0 : }
2609 :
2610 0 : HcclResult HcclCommSetGlobalWorkSpace(s64 opBaseHcom, std::vector<void *> &globalWorkSpaceAddr)
2611 : {
2612 : DevType devType;
2613 0 : CHK_RET(hrtGetDeviceType(devType));
2614 0 : if(devType == DevType::DEV_TYPE_950 || devType == DevType::DEV_TYPE_960){
2615 0 : HCCL_WARNING(" A5/A6 does not support this interface");
2616 0 : return HCCL_SUCCESS;
2617 : }
2618 :
2619 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
2620 0 : CHK_PTR_NULL(hcclComm);
2621 0 : CHK_RET(hcclComm->SetGlobalWorkSpace(globalWorkSpaceAddr));
2622 0 : return HCCL_SUCCESS;
2623 : }
2624 :
2625 0 : HcclResult HcomGetandClearOverFlowTasks(const char *group, hccl::HcclDumpInfo **hcclDumpInfoPtr, s32 *len)
2626 : {
2627 : DevType devType;
2628 0 : CHK_RET(hrtGetDeviceType(devType));
2629 0 : if(devType == DevType::DEV_TYPE_950 || devType == DevType::DEV_TYPE_960){
2630 0 : HCCL_WARNING("A5 does not support get and clear hcom over flow tasks.");
2631 0 : return HCCL_SUCCESS;
2632 : }
2633 :
2634 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
2635 0 : CHK_RET(HcomGetCommByGroup(group, hcclComm));
2636 0 : std::vector<hccl::HcclDumpInfo> hcclDumpInfo;
2637 0 : CHK_RET(hcclComm->GetandClearOverFlowTasks(hcclDumpInfo));
2638 0 : if (hcclDumpInfo.size() > 0) {
2639 0 : *hcclDumpInfoPtr = static_cast<hccl::HcclDumpInfo*>(malloc(hcclDumpInfo.size() * sizeof(hccl::HcclDumpInfo)));
2640 0 : if (*hcclDumpInfoPtr == nullptr) {
2641 0 : HCCL_ERROR("[HcomGetandClearOverFlowTasks][HcclDumpInfo]mem malloc size[%zu] failed.",
2642 : hcclDumpInfo.size() * sizeof(hccl::HcclDumpInfo));
2643 0 : return HCCL_E_MEMORY;
2644 : }
2645 0 : int32_t sret = memcpy_s(*hcclDumpInfoPtr, hcclDumpInfo.size() * sizeof(hccl::HcclDumpInfo), hcclDumpInfo.data(),
2646 0 : hcclDumpInfo.size() * sizeof(hccl::HcclDumpInfo));
2647 0 : CHK_PRT_RET(sret != EOK, HCCL_ERROR("[HcomGetandClearOverFlowTasks][HcclDumpInfo]memcpy failed. ret[%d], "
2648 : "hcclDumpInfo:size[%zu]", sret, hcclDumpInfo.size()), HCCL_E_MEMORY);
2649 : }
2650 0 : *len = hcclDumpInfo.size();
2651 0 : return HCCL_SUCCESS;
2652 0 : }
2653 :
2654 0 : HcclResult HcclCommGetandClearOverFlowTasks(s64 opBaseHcom, std::vector<hccl::HcclDumpInfo> &hcclDumpInfo)
2655 : {
2656 : DevType devType;
2657 0 : CHK_RET(hrtGetDeviceType(devType));
2658 0 : if(devType == DevType::DEV_TYPE_950 || devType == DevType::DEV_TYPE_960){
2659 0 : HCCL_WARNING("A5 does not support get and clear hcclcom over flow tasks.");
2660 0 : return HCCL_SUCCESS;
2661 : }
2662 :
2663 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
2664 0 : CHK_PTR_NULL(hcclComm);
2665 0 : CHK_RET(hcclComm->GetandClearOverFlowTasks(hcclDumpInfo));
2666 0 : return HCCL_SUCCESS;
2667 : }
2668 :
2669 0 : HcclResult HcomSupportDeterministicOptim(const char *group, bool *isDeterministicOptim)
2670 : {
2671 0 : CHK_PTR_NULL(isDeterministicOptim);
2672 0 : HCCLV2_FUNC_RUN(HcomSupportDeterministicOptimV2(group, *isDeterministicOptim));
2673 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
2674 0 : CHK_RET(HcomGetCommByGroup(group, hcclComm));
2675 0 : CHK_RET(hcclComm->SupportDeterministicOptim(*isDeterministicOptim));
2676 0 : return HCCL_SUCCESS;
2677 0 : }
2678 :
2679 0 : HcclResult HcomGetHccsLinkNum(const char *group, u32 *numHccsLink)
2680 : {
2681 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
2682 0 : CHK_RET(HcomGetCommByGroup(group, hcclComm));
2683 0 : CHK_RET(hcclComm->GetHccsLinkNum(*numHccsLink));
2684 0 : return HCCL_SUCCESS;
2685 0 : }
2686 :
2687 0 : HcclResult HcclCommSupportDeterministicOptim(s64 opBaseHcom, bool &isDeterministicOptim)
2688 : {
2689 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
2690 0 : CHK_PTR_NULL(hcclComm);
2691 0 : CHK_RET(hcclComm->SupportDeterministicOptim(isDeterministicOptim));
2692 0 : return HCCL_SUCCESS;
2693 : }
2694 :
2695 0 : std::vector<u64> GenerateSendCountMatrix(u64 count, u32 rankSize)
2696 : {
2697 0 : std::vector<u64> sendCountMatrix(rankSize * rankSize, count);
2698 0 : return sendCountMatrix;
2699 : }
2700 :
2701 0 : HcclResult HcomAllToAll(const void *sendBuf, u64 sendCount, HcclDataType sendType,
2702 : const void *recvBuf, u64 recvCount, HcclDataType recvType,
2703 : const char *group, rtStream_t stream, const char *tag)
2704 : {
2705 0 : HcclUs startut = TIME_NOW();
2706 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
2707 : // 入参合法性校验
2708 0 : CHK_PTR_NULL(sendBuf);
2709 0 : CHK_PTR_NULL(recvBuf);
2710 0 : RPT_INPUT_ERR(stream == nullptr, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),\
2711 : std::vector<std::string>({"HcomAllToAll", "nullptr", "stream", "non-null pointer"}));
2712 0 : CHK_PTR_NULL(stream);
2713 :
2714 0 : s32 streamId = 0;
2715 0 : CHK_RET(hrtGetStreamId(stream, streamId));
2716 0 : s32 deviceLogicId = 0;
2717 0 : CHK_RET(hrtGetDevice(&deviceLogicId));
2718 :
2719 0 : if (sendBuf != nullptr) {
2720 0 : CHK_RET(PrintMemoryAttr(sendBuf));
2721 : }
2722 0 : if (recvBuf != nullptr) {
2723 0 : CHK_RET(PrintMemoryAttr(recvBuf));
2724 : }
2725 :
2726 0 : CHK_PRT_RET(sendCount == 0, HCCL_WARNING("send count is 0, return AllToAll success"), HCCL_SUCCESS);
2727 0 : CHK_PRT_RET(recvCount == 0, HCCL_WARNING("recv count is 0, return AllToAll success"), HCCL_SUCCESS);
2728 :
2729 0 : std::string strGroup = (group == nullptr) ? HCCL_WORLD_GROUP : group;
2730 : /* 接口交互信息日志 */
2731 0 : HCCL_USER_CRITICAL_LOG("Entry-HcomAllToAll:tag[%s], sendBuf[%p], recvBuf[%p], sendCount[%llu], recvCount[%llu],"
2732 : "sendType[%s], recvType[%s], group[%s], streamId[%d], deviceLogicId[%d]",
2733 : tag, sendBuf, recvBuf, sendCount, recvCount, GetDataTypeEnumStr(sendType).c_str(),
2734 : GetDataTypeEnumStr(recvType).c_str(), strGroup.c_str(), streamId, deviceLogicId);
2735 :
2736 : // HcomAlltoAllV2
2737 0 : HCCLV2_FUNC_RUN(HcomAlltoAllV2(sendBuf, sendCount, sendType, recvBuf, recvCount, recvType, group, stream, tag));
2738 0 : CHK_RET(HcomCheckOpParam(tag, sendCount, sendType, stream));
2739 0 : CHK_RET(HcomCheckOpParam(tag, recvCount, recvType, stream));
2740 0 : CHK_RET(HcomCheckDataType(sendType));
2741 0 : CHK_RET(HcomCheckDataType(recvType));
2742 : // 根据ranksize校验相关入参
2743 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
2744 0 : CHK_RET(HcomGetCommByGroup(strGroup.c_str(), hcclComm));
2745 0 : u32 rankSize = 0, rankId = 0;
2746 0 : CHK_RET(hcclComm->GetRankSize(rankSize));
2747 0 : CHK_RET(hcclComm->GetUserRank(rankId));
2748 0 : u32 aivCoreLimit = 0;
2749 0 : CHK_RET(hcclComm->GetNumBlocks(aivCoreLimit));
2750 :
2751 0 : HcclWorkflowMode mode = GetWorkflowMode();
2752 0 : CHK_PRT_RET(mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_RESERVED, HCCL_ERROR("Invalid Workflow Mode[%d]", mode),
2753 : HCCL_E_INTERNAL);
2754 : /* 入参的正确性由HCCL确保 */
2755 0 : CHK_RET(hcclComm->AlltoAll(sendBuf, sendCount, sendType, recvBuf, recvCount, recvType, stream, tag));
2756 0 : CHK_RET(CallMsprofReportHostApi(hcclComm.get(), HcclCMDType::HCCL_CMD_ALLTOALLV, beginTime, sendCount, sendType));
2757 : /* 关键状态记录 */
2758 0 : HcclUs endut = TIME_NOW();
2759 0 : HCCL_RUN_INFO("HcomAllToAll success,take time [%lld]us, tag[%s], sendBuf[%p], recvBuf[%p], sendCount[%llu], "\
2760 : "recvCounts[%llu], sendType[%s], recvType[%s], group[%s], streamId[%d]", DURATION_US(endut - startut),
2761 : tag, sendBuf, recvBuf, sendCount, recvCount, GetDataTypeEnumStr(sendType).c_str(),
2762 : GetDataTypeEnumStr(recvType).c_str(), strGroup.c_str(), streamId);
2763 0 : return HCCL_SUCCESS;
2764 0 : }
2765 :
2766 0 : HcclResult HcclIgetLookupRequest(HcclComm comm, s32* tag, ServiceHandle* handle, uint64_t* keys, uint64_t keyMaxNum,
2767 : HcclRequest* request)
2768 : {
2769 0 : HCCL_ERROR("[Iget][LookupRequest] is not support HcclIgetLookupRequest interface");
2770 0 : return HCCL_E_PARA;
2771 : }
2772 :
2773 0 : HcclResult HcomCollRemotePairedParaCheck(const HcomRemoteOperationParams ¶ms)
2774 : {
2775 0 : CHK_PTR_NULL(params.keyAddr);
2776 0 : CHK_PTR_NULL(params.value);
2777 0 : CHK_PTR_NULL(params.tableId);
2778 0 : CHK_PTR_NULL(params.indices);
2779 0 : CHK_PTR_NULL(params.numUniqued);
2780 0 : CHK_PTR_NULL(params.psSeg);
2781 0 : CHK_PTR_NULL(params.psSegNum);
2782 :
2783 0 : return HCCL_SUCCESS;
2784 : }
2785 :
2786 0 : HcclResult HcomInitByRankTable(const char *rankTable, uint32_t rankId)
2787 : {
2788 0 : return HcomInitByString(rankTable, std::to_string(rankId).c_str(), HCCL_MODE_SCHED_OS);
2789 : }
2790 :
2791 : inline void GenerateHcomSendRecvOpTag(HcomOperationType opType, const char *group, u32 tag, u32 selfRank, u32 peerRank,
2792 : std::string &opTag)
2793 : {
2794 : std::string groupStr = (group == nullptr) ? HCCL_WORLD_GROUP : group;
2795 : if (opType == HCOM_OP_TYPE_SEND) {
2796 : opTag = groupStr + "_" + std::to_string(tag) + "_" + std::to_string(selfRank) + "_" + std::to_string(peerRank);
2797 : } else if (opType == HCOM_OP_TYPE_RECV) {
2798 : opTag = groupStr + "_" + std::to_string(tag) + "_" + std::to_string(peerRank) + "_" + std::to_string(selfRank);
2799 : }
2800 : return;
2801 : }
2802 :
2803 0 : HcclResult HcomGetTopoDesc(const char *group, HcclTopoDescs *topoDescs, uint32_t topoSize)
2804 : {
2805 0 : CHK_PTR_NULL(topoDescs);
2806 0 : CHK_PTR_NULL(group);
2807 0 : HCCLV2_FUNC_RUN(HcomGetTopoDescV2(group, topoDescs, topoSize));
2808 :
2809 0 : std::shared_ptr<hcclComm> hcclComm;
2810 0 : s32 deviceLogicId = 0;
2811 0 : CHK_RET(HcclDeviceRefresh(deviceLogicId));
2812 0 : if (HcomGetCommByGroup(group, hcclComm)==HCCL_SUCCESS) {
2813 0 : CHK_RET(hcclComm->GetTopoDesc(topoDescs, topoSize));
2814 : } else {
2815 0 : return HCCL_E_PTR;
2816 : }
2817 :
2818 0 : return HCCL_SUCCESS;
2819 0 : }
2820 :
2821 : #ifdef __cplusplus
2822 : extern "C" {
2823 : #endif // __cplusplus
2824 0 : HcclResult HcomGetL0TopoTypeEx(const char *group, CommTopo *topoType, uint32_t flag)
2825 : {
2826 0 : HCCLV2_FUNC_RUN(HcomGetL0TopoTypeExV2(group, topoType, flag));
2827 : #define IS_SET_DEVICE_MASK 0xfffffffe
2828 0 : CHK_PTR_NULL(topoType);
2829 0 : CHK_PTR_NULL(group);
2830 :
2831 0 : bool isSetDevice = static_cast<bool>(flag & (~(0xfffffffe)));
2832 0 : if (isSetDevice) {
2833 0 : HCCL_ERROR("current only support no setdevice, flag[%u]", flag);
2834 0 : return HCCL_E_PARA;
2835 : }
2836 :
2837 0 : std::string identifier(group);
2838 0 : return CommTopoDesc::GetInstance().GetL0TopoType(identifier, topoType);
2839 0 : }
2840 :
2841 0 : HcclResult HcomGetRankSizeEx(const char *group, uint32_t *rankSize, uint32_t flag)
2842 : {
2843 0 : HCCLV2_FUNC_RUN(HcomGetRankSizeExV2(group, rankSize, flag));
2844 : #define IS_SET_DEVICE_MASK 0xfffffffe
2845 0 : CHK_PTR_NULL(rankSize);
2846 0 : CHK_PTR_NULL(group);
2847 :
2848 0 : bool isSetDevice = static_cast<bool>(flag & (~(0xfffffffe)));
2849 0 : if (isSetDevice) {
2850 0 : HCCL_ERROR("current only support no setdevice, flag[%u]", flag);
2851 0 : return HCCL_E_PARA;
2852 : }
2853 :
2854 0 : std::string identifier(group);
2855 0 : return CommTopoDesc::GetInstance().GetRankSize(identifier, rankSize);
2856 0 : }
2857 : #ifdef __cplusplus
2858 : }
2859 : #endif // __cplusplus
2860 :
2861 0 : HcclResult HcomGetCommCCLBufferSize(const char *group, uint64_t &size)
2862 : {
2863 0 : HCCLV2_FUNC_RUN(HcomGetCommCCLBufferSizeV2());
2864 0 : CHK_PTR_NULL(group);
2865 0 : std::shared_ptr<hcclComm> hcclComm;
2866 0 : CHK_RET(HcomGetCommByGroup(group, hcclComm));
2867 0 : HcclResult ret = hcclComm->GetCommUserMemSize(size);
2868 0 : CHK_PRT_RET(ret == HCCL_SUCCESS, HCCL_INFO("[%s]get comm ccl buffer size from user mem size", __func__), ret);
2869 0 : if (0 == hcclComm->GetConfigInCCLbufferSize()) {
2870 0 : size = GetExternalInputCCLBuffSize();
2871 0 : HCCL_INFO("[%s]get comm ccl buffer size from external input", __func__);
2872 : } else {
2873 0 : size = hcclComm->GetConfigInCCLbufferSize();
2874 0 : HCCL_INFO("[%s]get comm ccl buffer size from comm config", __func__);
2875 : }
2876 0 : return HCCL_SUCCESS;
2877 0 : }
2878 :
2879 0 : bool HcomIsNormalComm(const char *group)
2880 : {
2881 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
2882 0 : return (hcomInfo.pComm != nullptr);
2883 : }
2884 :
2885 0 : HcclResult HcomClearAivSyncBuf(const char *group, bool aivClearEnable)
2886 : {
2887 0 : HCCLV2_FUNC_RUN(HcomSetAivClearEnableV2(group, aivClearEnable));
2888 0 : CHK_PTR_NULL(group);
2889 0 : std::shared_ptr<hcclComm> hcclComm;
2890 0 : if (HcomGetCommByGroup(group, hcclComm) == HCCL_SUCCESS) {
2891 0 : CHK_RET(hcclComm->SetClearAivSyncBuf(aivClearEnable));
2892 : }
2893 :
2894 0 : return HCCL_SUCCESS;
2895 0 : }
2896 :
2897 0 : HcclResult HcclCommGraphClearAivSyncBuf(s64 comm, bool aivClearEnable)
2898 : {
2899 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(comm);
2900 0 : if (hcclComm != nullptr) {
2901 0 : CHK_RET(hcclComm->SetClearAivSyncBuf(aivClearEnable));
2902 : }
2903 0 : return HCCL_SUCCESS;
2904 : }
2905 :
2906 0 : HcclResult HcomSetAivCoreLimit(const char *group, u32 aivCoreLimit)
2907 : {
2908 0 : CHK_PRT_RET(aivCoreLimit == 0,
2909 : HCCL_ERROR("[HcomSetAivCoreLimit] aivCoreLimit[%u] invalid", aivCoreLimit), HCCL_E_PARA);
2910 0 : HCCLV2_FUNC_RUN(HcomSetAivCoreLimitV2(group, aivCoreLimit));
2911 :
2912 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
2913 0 : CHK_RET(HcomGetCommByGroup(group, hcclComm));
2914 0 : CHK_RET(hcclComm->SetAivCoreLimit(aivCoreLimit));
2915 :
2916 0 : HCCL_RUN_INFO("HcomSetAivCoreLimit group[%s] aivCoreLimit[%u]", group ? group : HCCL_WORLD_GROUP, aivCoreLimit);
2917 0 : return HCCL_SUCCESS;
2918 0 : }
2919 :
2920 0 : HcclResult HcclCommGraphSetAivCoreLimit(s64 comm, u32 aivCoreLimit)
2921 : {
2922 0 : CHK_PRT_RET((comm == 0 || aivCoreLimit == 0),
2923 : HCCL_ERROR("[HcclCommGraphSetAivCoreLimit] comm[%lld] or aivCoreLimit[%u] invalid", comm, aivCoreLimit),
2924 : HCCL_E_PARA);
2925 :
2926 : DevType devType;
2927 0 : CHK_RET(hrtGetDeviceType(devType));
2928 0 : if(devType == DevType::DEV_TYPE_950 || devType == DevType::DEV_TYPE_960){
2929 0 : HCCL_WARNING("A5 does not support get and clear hcclcom set aiv core limit.");
2930 0 : return HCCL_SUCCESS;
2931 : }
2932 :
2933 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(comm);
2934 0 : CHK_RET(hcclComm->SetAivCoreLimit(aivCoreLimit));
2935 :
2936 0 : HCCL_RUN_INFO("HcclCommGraphSetAivCoreLimit hcclComm[%p] aivCoreLimit[%u]", hcclComm, aivCoreLimit);
2937 0 : return HCCL_SUCCESS;
2938 : }
2939 :
2940 0 : HcclResult HcomCalcTaskNum(HcomOpParam *hcomOpParam, u32 &taskNum)
2941 : {
2942 0 : CHK_PTR_NULL(hcomOpParam);
2943 0 : HCCLV2_FUNC_RUN(HcomCalcTaskNumV2(hcomOpParam, taskNum));
2944 0 : return HCCL_SUCCESS;
2945 : }
2946 :
2947 41 : __attribute__((constructor)) void CallBackInit()
2948 : {
2949 41 : HcomGroupCallbackFuncInstall(HcomCreateGroupImplHeterog,
2950 : HcomCallBackGroupIsInitHeterog,
2951 : HcomDestroyGroupImplHeterog,
2952 : HcomDestroyOneDeviceHeterog);
2953 41 : }
2954 :
2955 1 : HcclResult HcomGetGroupNameByOpBase(s64 opBaseHcom, char **groupname)
2956 : {
2957 1 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
2958 1 : CHK_PTR_NULL(hcclComm);
2959 1 : *groupname = const_cast<char *>(hcclComm->GetIdentifier().c_str());
2960 1 : return HCCL_SUCCESS;
2961 : }
2962 :
2963 0 : HcclResult GetGroupNameByOpBaseHcom(s64 opBaseHcom, char **groupname)
2964 : {
2965 0 : hccl::hcclComm* hcclComm = reinterpret_cast<hccl::hcclComm*>(opBaseHcom);
2966 0 : CHK_PTR_NULL(hcclComm);
2967 0 : *groupname = const_cast<char *>(hcclComm->GetIdentifier().c_str());
2968 0 : return HCCL_SUCCESS;
2969 : }
2970 :
2971 0 : HcclResult HcomCreateComResourceByComm(HcclComm comm, u32 streamMode, bool isOpbaseMode,
2972 : void** commContext, bool isMC2)
2973 : {
2974 0 : CHK_RET(HcclCreateComResourceByComm(comm, streamMode, isOpbaseMode, commContext, isMC2));
2975 0 : return HCCL_SUCCESS;
2976 : }
2977 :
2978 0 : HcclWorkflowMode HcomGetWorkflowMode()
2979 : {
2980 0 : return GetWorkflowMode();
2981 : }
2982 :
2983 0 : HcclResult HcomSetWorkflowMode(HcclWorkflowMode mode)
2984 : {
2985 0 : SetWorkflowMode(mode);
2986 0 : return HCCL_SUCCESS;
2987 : }
2988 :
2989 0 : HcclResult GetModuleInfo(DevType devType, const std::vector<RankInfo_t> &rankList, bool &multiModuleDiffDeviceNumMode)
2990 : {
2991 0 : multiModuleDiffDeviceNumMode = false;
2992 :
2993 0 : if (devType != DevType::DEV_TYPE_910B || rankList.size() == 0) {
2994 0 : return HCCL_SUCCESS;
2995 : }
2996 :
2997 0 : std::map<u32, std::vector<RankInfo_t>> moduleMap;
2998 0 : for (RankInfo_t rankInfo : rankList) {
2999 0 : if (static_cast<s32>(rankInfo.deviceInfo.devicePhyId) == HOST_DEVICE_ID) {
3000 0 : continue;
3001 : }
3002 0 : u32 moduleIdx = rankInfo.serverIdx * FACTOR_NUM_TWO + rankInfo.deviceInfo.devicePhyId / DEVICE_PER_MODULE;
3003 0 : auto iter = moduleMap.find(moduleIdx);
3004 0 : if (iter == moduleMap.end()) {
3005 0 : std::vector<RankInfo_t> rankInfoList;
3006 0 : rankInfoList.push_back(rankInfo);
3007 0 : moduleMap.insert(std::make_pair(moduleIdx, rankInfoList));
3008 0 : } else {
3009 0 : iter->second.push_back(rankInfo);
3010 : }
3011 0 : }
3012 :
3013 : // 无NPU参与通信
3014 0 : if (moduleMap.size() == 0) {
3015 0 : return HCCL_SUCCESS;
3016 : }
3017 0 : u32 preDeviceNum = moduleMap.begin()->second.size();
3018 0 : u32 curDeviceNum = preDeviceNum;
3019 0 : for (auto moduleInfo: moduleMap) {
3020 0 : curDeviceNum = moduleInfo.second.size();
3021 0 : HCCL_DEBUG("[HcomOpUtils][GetModuleInfo] module[%d] contains [%d]devices", moduleInfo.first, curDeviceNum);
3022 0 : for (auto rankInfo : moduleInfo.second) {
3023 0 : HCCL_DEBUG("[HcomOpUtils][GetModuleInfo] moduleIdx[%d] Info: rankId[%d], serverId[%s], serverIdx[%d], "
3024 : "devicePhyId[%d]", moduleInfo.first, rankInfo.rankId, rankInfo.serverId.c_str(), rankInfo.serverIdx,
3025 : rankInfo.deviceInfo.devicePhyId);
3026 0 : }
3027 0 : if (curDeviceNum != preDeviceNum) {
3028 0 : multiModuleDiffDeviceNumMode = true;
3029 0 : HCCL_INFO("[HcomOpUtils][GetModuleInfo] different module contains different numbers of cards:[%d]",
3030 : multiModuleDiffDeviceNumMode);
3031 0 : return HCCL_SUCCESS;
3032 : }
3033 0 : }
3034 0 : return HCCL_SUCCESS;
3035 0 : }
3036 :
3037 0 : HcclResult HcomCalcOpOnline(HcomOpParam *hcomOpParam, HcomResResponse *hcomResResponse)
3038 : {
3039 0 : HCCL_INFO("[HcomCalcOpOnline] calc online resource start...");
3040 0 : u64 streamNum = 0;
3041 0 : u64 opMemSize = 0;
3042 0 : u32 taskNum = 0;
3043 : HcclResult ret;
3044 0 : std::string sCollectiveType(hcomOpParam->opType);
3045 :
3046 0 : CHK_PTR_NULL(hcomOpParam->socVersion);
3047 0 : std::string socVersionStr(hcomOpParam->socVersion);
3048 : DevType devType;
3049 0 : CHK_RET(GetOffDeviceTypeWithoutDev(socVersionStr, devType));
3050 :
3051 0 : auto iter = HCCL_OPTYPE_NAME_MAP.find(hcomOpParam->opType);
3052 0 : HcclCMDType hcclOpType = (iter != HCCL_OPTYPE_NAME_MAP.end()) ? iter->second : HcclCMDType::HCCL_CMD_INVALID;
3053 :
3054 0 : u32 dataTypeSize = 0;
3055 0 : ret = SalGetDataTypeSize(hcomOpParam->dataType, dataTypeSize);
3056 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[GetOp][WorkspaceMemSize]op[%s]: get data size failed. ret[%d]",
3057 : sCollectiveType.c_str(), ret), ret);
3058 :
3059 0 : CHK_RET(HcomCheckCount(hcomOpParam->count));
3060 :
3061 0 : u64 opDataSize = dataTypeSize * hcomOpParam->count;
3062 :
3063 0 : CHK_RET(HcomGetWorkspaceSubStreamNum(hcomOpParam->group, streamNum, opDataSize, hcomOpParam->dataType,
3064 : hcomOpParam->aivCoreLimit, hcomOpParam->reduceOp, hcomOpParam->count, hcclOpType));
3065 0 : if (devType == DevType::DEV_TYPE_950 || devType == DevType::DEV_TYPE_960) {
3066 0 : CHK_RET(HcomGetWorkspaceMemSize(hcomOpParam->opType, hcomOpParam->count, hcomOpParam->dataType, hcomOpParam->group, opMemSize));
3067 0 : } else {
3068 0 : CHK_RET(GetOpWorkspaceMemSize(false, hcclOpType, hcomOpParam, 0, opMemSize));
3069 : }
3070 :
3071 0 : HcomInfo &hcomInfo = HcomGetCtxHomInfo();
3072 0 : u32 serverNum = hcomInfo.rankTable.serverNum;
3073 0 : u32 deviceNumPerServer = (serverNum == 0) ? 0 : (hcomInfo.rankTable.deviceNum + serverNum - 1) / serverNum;
3074 0 : HCCL_INFO("get HcomInfo from Context");
3075 :
3076 : // 获取multiModuleDiffDeviceNumMode信息
3077 0 : bool multiModuleDiffDeviceNumMode = false;
3078 0 : ret = GetModuleInfo(devType, hcomInfo.rankTable.rankList, multiModuleDiffDeviceNumMode);
3079 0 : if (ret != HCCL_SUCCESS) {
3080 0 : HCCL_WARNING("call GetModuleInfo error, failed to get multiModuleDiffDeviceNumMode.");
3081 : }
3082 :
3083 0 : if (devType == DevType::DEV_TYPE_950 || devType == DevType::DEV_TYPE_960) {
3084 0 : CHK_RET(CalcTaskNumV2(hcomOpParam, taskNum));
3085 0 : } else {
3086 0 : CHK_RET(CalcTaskNum(hcomOpParam, streamNum, deviceNumPerServer, serverNum, multiModuleDiffDeviceNumMode, taskNum, devType));
3087 : }
3088 :
3089 0 : hcomResResponse->streamNum = streamNum;
3090 0 : hcomResResponse->opMemSize = opMemSize;
3091 0 : hcomResResponse->taskNum = taskNum;
3092 :
3093 0 : return HCCL_SUCCESS;
3094 0 : }
3095 :
3096 0 : HcclResult HcomCalcOpResOffline(HcomOpParam *hcomOpParam, HcomResResponse *hcomResResponse)
3097 : {
3098 0 : HCCL_INFO("[HcomCalcOpResOffline] calc offline resource start...");
3099 : // 获取需回传的信息
3100 0 : u64 streamNum = 0;
3101 0 : u64 opMemSize = 0;
3102 0 : u32 taskNum = 0;
3103 :
3104 0 : CHK_PTR_NULL(hcomOpParam->rankTable);
3105 0 : std::string rankTableString(hcomOpParam->rankTable);
3106 :
3107 0 : auto iter = HCCL_OPTYPE_NAME_MAP.find(hcomOpParam->opType);
3108 0 : HcclCMDType hcclOpType = (iter != HCCL_OPTYPE_NAME_MAP.end()) ? iter->second : HcclCMDType::HCCL_CMD_INVALID;
3109 :
3110 0 : CHK_PTR_NULL(hcomOpParam->socVersion);
3111 0 : std::string socVersionStr(hcomOpParam->socVersion);
3112 : DevType devType;
3113 0 : CHK_RET(GetOffDeviceTypeWithoutDev(socVersionStr, devType));
3114 :
3115 : // 先根据逻辑ranktable获取device数量
3116 0 : s32 deviceNumPerServer = 0;
3117 0 : s32 serverNum = 0;
3118 0 : bool multiModuleDiffDeviceNumMode = false;
3119 0 : RankTable_t clusterInfo;
3120 : // world group 从逻辑ranktable里获取device数量
3121 0 : if (hcomOpParam->groupListSize == 0) {
3122 0 : CHK_RET(GetClusterInfoAndDeviceNum(rankTableString, clusterInfo, deviceNumPerServer));
3123 0 : serverNum = clusterInfo.serverNum;
3124 : } else {
3125 0 : CHK_RET(GetServerAndDevNumFromGroupList(hcomOpParam->groupList, hcomOpParam->groupListSize, rankTableString,
3126 : devType, serverNum, deviceNumPerServer, multiModuleDiffDeviceNumMode));
3127 : }
3128 :
3129 0 : if (hcomOpParam->rankSize == 0) {
3130 0 : hcomOpParam->rankSize = deviceNumPerServer;
3131 : }
3132 :
3133 0 : string algName;
3134 0 : bool ifAiv = false;
3135 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
3136 0 : std::string group = hcomOpParam->group == nullptr ? HCCL_WORLD_GROUP : hcomOpParam->group;
3137 0 : CHK_RET(HcomGetCommByGroup(group.c_str(), hcclComm));
3138 0 : void* counts = nullptr;
3139 0 : HcclResult ret = hcclComm->HcclSelectAlg(hcclOpType, hcomOpParam->count, counts, hcomOpParam->dataType,
3140 0 : hcomOpParam->reduceOp, hcomOpParam->aivCoreLimit, ifAiv, algName);
3141 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
3142 : HCCL_ERROR("[HcomGetWorkspaceSubStreamNum] HcclSelectAlg failed, ret[%d], optype[%d], count[%llu],"
3143 : "dataType[%d], reduceOp[%d]", ret, hcclOpType, hcomOpParam->count, hcomOpParam->dataType,
3144 : hcomOpParam->reduceOp), ret);
3145 0 : CHK_RET(GetStreamNumOfflineComp(hcclOpType, serverNum, deviceNumPerServer, ifAiv, devType, streamNum, group));
3146 0 : CHK_RET(GetOpWorkspaceMemSize(true, hcclOpType, hcomOpParam, serverNum, opMemSize));
3147 :
3148 0 : if (devType == DevType::DEV_TYPE_950 || devType == DevType::DEV_TYPE_960) {
3149 : // host展开已日落, Task任务数按照当前需求最多的CCU加速模式[AIV, AICPU使用较少]预估
3150 0 : taskNum = ESTIMATE_CCU_TASK_PER_STREAM;
3151 : }
3152 :
3153 0 : hcomResResponse->streamNum = streamNum;
3154 0 : hcomResResponse->opMemSize = opMemSize;
3155 0 : hcomResResponse->taskNum = taskNum;
3156 :
3157 0 : return HCCL_SUCCESS;
3158 0 : }
3159 :
3160 0 : HcclResult GetOffDeviceTypeWithoutDev(std::string socVersionStr, DevType &devType)
3161 : {
3162 : // 离线编译第一阶段获取devType从SOC_VERSION里获取
3163 0 : DevType tempDevType = DevType::DEV_TYPE_COUNT;
3164 0 : CHK_RET(hrtGetDeviceTypeBySocVersion(socVersionStr, tempDevType));
3165 :
3166 0 : if (tempDevType != DevType::DEV_TYPE_910 && tempDevType != DevType::DEV_TYPE_910B &&
3167 0 : tempDevType != DevType::DEV_TYPE_310P1 && tempDevType != DevType::DEV_TYPE_310P3 &&
3168 0 : tempDevType != DevType::DEV_TYPE_910_93 && tempDevType != DevType::DEV_TYPE_950 &&
3169 0 : tempDevType != DevType::DEV_TYPE_960) {
3170 0 : HCCL_ERROR("[offline][compilation] cur dev type[%u] is not support.", tempDevType);
3171 0 : return HCCL_E_RUNTIME;
3172 : }
3173 0 : devType = tempDevType;
3174 0 : HCCL_DEBUG("[offline] Get devtype[%u]....", devType);
3175 0 : return HCCL_SUCCESS;
3176 : }
3177 :
3178 0 : HcclResult GetStreamNumOfflineComp(HcclCMDType hcclOpType, s32 serverNum, s32 deviceNumPerServer, bool ifAiv,
3179 : DevType devType, u64 &streamNum, const std::string& group)
3180 : {
3181 0 : switch (devType) {
3182 0 : case DevType::DEV_TYPE_310P1:
3183 : case DevType::DEV_TYPE_310P3: {
3184 0 : streamNum = 0;
3185 0 : break;
3186 : }
3187 :
3188 0 : case DevType::DEV_TYPE_910B:
3189 : case DevType::DEV_TYPE_910:
3190 : case DevType::DEV_TYPE_950:
3191 : case DevType::DEV_TYPE_960:
3192 : case DevType::DEV_TYPE_910_93: {
3193 0 : CHK_RET(GetStremNumOfflineByDev(devType, hcclOpType, serverNum, deviceNumPerServer, ifAiv, streamNum, group));
3194 0 : break;
3195 : }
3196 :
3197 0 : default: {
3198 0 : HCCL_ERROR("[Get][OfflineCompStreamNum] The current device type does not support offline compilation, " \
3199 : "The value of device type is [%u]", devType);
3200 0 : return HCCL_E_NOT_SUPPORT;
3201 : }
3202 : }
3203 0 : HCCL_INFO("[GetStreamNumOfflineComp]stream num is [%llu]", streamNum);
3204 0 : return HCCL_SUCCESS;
3205 : }
3206 :
3207 0 : HcclResult GetStremNumOfflineByDev(const DevType &devType, HcclCMDType hcclOpType, s32 serverNum, s32 deviceNumPerServer, bool ifAiv,
3208 : u64 &streamNum, const std::string& group)
3209 : {
3210 0 : if (ifAiv) {
3211 0 : streamNum = 0; // 离线编译下,从流数量设置为0
3212 0 : HCCL_INFO("[GetStremNumOfflineByDev] set AIV stream num is 0 When in Aiv mode");
3213 0 : return HCCL_SUCCESS;
3214 : }
3215 :
3216 0 : if (hcclOpType == HcclCMDType::HCCL_CMD_SEND || hcclOpType == HcclCMDType::HCCL_CMD_RECEIVE) {
3217 0 : streamNum = 0;
3218 0 : return HCCL_SUCCESS;
3219 : }
3220 :
3221 0 : if (devType == DevType::DEV_TYPE_910 && deviceNumPerServer == HCCL_DEVICE_NUM_EIGHT) {
3222 0 : CHK_RET(GetSubStreamNum(devType, deviceNumPerServer, streamNum, serverNum, group));
3223 0 : } else if (devType == DevType::DEV_TYPE_910_93) {
3224 0 : CHK_RET(GetSubStreamNum(devType, deviceNumPerServer, streamNum, serverNum, group));
3225 : } else {
3226 0 : streamNum = deviceNumPerServer > HCCL_DEVICE_NUM_ONE ? deviceNumPerServer - MINUS_MESH_STREAM_NUM : 0;
3227 : }
3228 0 : HCCL_INFO("[GetStremNumOfflineByDev] get device num per server is [%u] streamNum [%u]",
3229 : deviceNumPerServer, streamNum);
3230 0 : return HCCL_SUCCESS;
3231 : }
3232 :
3233 0 : HcclResult GetSubStreamNum(const DevType &devType, s32 deviceNum, u64 &streamNum, s32 &serverNum, const std::string& group)
3234 : {
3235 0 : if (devType == DevType::DEV_TYPE_910B) {
3236 0 : constexpr u64 maxStream = 6;
3237 0 : streamNum = std::min(maxStream, static_cast<u64>(deviceNum) - MINUS_MESH_STREAM_NUM);
3238 0 : if (CommConfiger::GetInstance().GetCommConfigAlgoConfig(group)[HCCL_ALGO_LEVEL_1] == HcclAlgoType::HCCL_ALGO_TYPE_PIPELINE) {
3239 0 : streamNum = static_cast<u64>(deviceNum);
3240 : }
3241 0 : } else if (devType == DevType::DEV_TYPE_910_93) {
3242 0 : if (serverNum == 1) {
3243 0 : streamNum = static_cast<u64>(deviceNum) - MINUS_MESH_STREAM_NUM;
3244 : } else {
3245 0 : constexpr u64 streamNumFor91093 = 3;
3246 0 : streamNum = streamNumFor91093;
3247 : }
3248 : } else {
3249 0 : if (deviceNum > HCCL_DEVICE_NUM_EIGHT) {
3250 0 : streamNum = OFFLINE_BUILD_SUB_STEAM_NUM[HCCL_DEVICE_NUM_EIGHT];
3251 0 : } else if (OFFLINE_BUILD_SUB_STEAM_NUM.count(deviceNum) != 0) {
3252 0 : streamNum = OFFLINE_BUILD_SUB_STEAM_NUM[deviceNum];
3253 : } else {
3254 0 : streamNum = 0;
3255 : }
3256 : }
3257 :
3258 0 : if (SatisfyIntraSuperPod(devType, deviceNum, true)) {
3259 0 : streamNum = std::max(static_cast<u64>(deviceNum - 1u), streamNum);
3260 0 : } else if (FullmeshPairwiseSatisfyHighPerfAlltoallMeshCondition(devType, deviceNum * serverNum, true,
3261 0 : CommConfiger::GetInstance().GetCommConfigAlgoConfig(group, HcclCMDType::HCCL_CMD_ALLTOALL))) {
3262 0 : streamNum = std::max(static_cast<u64>(deviceNum * serverNum - 1u), streamNum);
3263 : }
3264 :
3265 0 : HCCL_DEBUG("[GetSubStreamNum]deviceNum[%d], streamNum[%llu]", deviceNum, streamNum);
3266 0 : return HCCL_SUCCESS;
3267 : }
3268 :
3269 0 : HcclResult GetClusterInfoAndDeviceNum(const std::string rankTableString, RankTable_t &clusterInfo, s32 &deviceNum)
3270 : {
3271 0 : HCCL_DEBUG("[get][offlineStreamNum]rankTableString[%s]", rankTableString.c_str());
3272 0 : TopoinfoRanktableOffline myTopoRanktable(rankTableString);
3273 0 : CHK_RET(myTopoRanktable.Init());
3274 0 : CHK_RET(myTopoRanktable.GetClusterInfo(clusterInfo));
3275 0 : CHK_RET(myTopoRanktable.GetDeviceNumPerServer(deviceNum));
3276 0 : CHK_PRT_RET(deviceNum == 0, HCCL_ERROR("[GetStremNumOfflineByDev]cur device num per server is 0,\
3277 : maybe ranktable is incomplete"), HCCL_E_PARA);
3278 0 : return HCCL_SUCCESS;
3279 0 : }
3280 :
3281 0 : HcclResult GetServerAndDevNumFromGroupList(const u32 *groupList, u32 groupListSize, const std::string rankTableString,
3282 : DevType devType, s32 &serverNum, s32 &deviceNumPerServer, bool &multiModuleDiffDeviceNumMode)
3283 : {
3284 0 : deviceNumPerServer = 0;
3285 0 : serverNum = 0;
3286 :
3287 0 : if (groupListSize == 0) {
3288 0 : return HCCL_SUCCESS;
3289 : }
3290 0 : CHK_PTR_NULL(groupList);
3291 :
3292 : try {
3293 : // 获取并设定stream 数量
3294 : // 能获取到group list时进入离线编译的流程去获取从流个数
3295 0 : CHK_RET(GetServerAndDevNumFromLogRanktable(rankTableString, groupList, groupListSize, devType, serverNum, deviceNumPerServer,
3296 : multiModuleDiffDeviceNumMode));
3297 :
3298 0 : HCCL_INFO("deviceNumPerServer:[%d] serverNum:[%d]", deviceNumPerServer, serverNum);
3299 0 : } catch (const std::exception& e) {
3300 0 : HCCL_ERROR("[HcomCalcOpRunningParam] exception caught. err[%s]", e.what());
3301 0 : return HCCL_E_INTERNAL;
3302 0 : }
3303 0 : return HCCL_SUCCESS;
3304 : }
3305 :
3306 0 : HcclResult GetServerAndDevNumFromLogRanktable(const std::string rankTableString, const u32 *groupList, u32 groupListSize, DevType devType,
3307 : s32 &serverNum, s32 &deviceNum, bool &multiModuleDiffDeviceNumMode)
3308 : {
3309 0 : HCCL_INFO("Entry GetServerAndDevNumFromLogRanktable");
3310 0 : RankTable_t clusterInfo;
3311 0 : TopoinfoRanktableOffline myTopoRanktable(rankTableString);
3312 0 : CHK_RET(myTopoRanktable.Init());
3313 0 : CHK_RET(myTopoRanktable.GetClusterInfo(clusterInfo));
3314 :
3315 0 : CHK_RET(GetServerAndDevNumFromRanklist(groupList, groupListSize, clusterInfo.rankList, devType, serverNum, deviceNum,
3316 : multiModuleDiffDeviceNumMode));
3317 0 : return HCCL_SUCCESS;
3318 0 : }
3319 :
3320 0 : HcclResult GetServerAndDevNumFromRanklist(const u32 *groupList, u32 groupListSize, const std::vector<RankInfo_t> &rankList,
3321 : DevType devType, s32 &serverNum, s32 &deviceNum, bool &multiModuleDiffDeviceNumMode)
3322 : {
3323 0 : u32 serverId = 0;
3324 0 : std::map<u32, s32> serverAndDevNum;
3325 0 : deviceNum = 0;
3326 0 : for (u32 i = 0; i < groupListSize; i++) {
3327 0 : u32 rankId = groupList[i];
3328 0 : CHK_RET(GetServerIdByRankId(rankList, rankId, serverId));
3329 0 : if (serverAndDevNum.find(serverId) == serverAndDevNum.end()) {
3330 0 : serverAndDevNum[serverId] = 1;
3331 : } else {
3332 0 : serverAndDevNum[serverId]++;
3333 : }
3334 : // 可能存在不同server内device数量不一致的情况,因此求最大值
3335 0 : if (serverAndDevNum[serverId] > deviceNum) {
3336 0 : deviceNum = serverAndDevNum[serverId];
3337 : }
3338 : }
3339 0 : serverNum = serverAndDevNum.size();
3340 :
3341 : // 获取multiModuleDiffDeviceNumMode信息
3342 0 : HcclResult ret = GetModuleInfo(devType, rankList, multiModuleDiffDeviceNumMode);
3343 0 : if (ret != HCCL_SUCCESS) {
3344 0 : HCCL_WARNING("call GetModuleInfo error, failed to get multiModuleDiffDeviceNumMode.");
3345 : }
3346 0 : return HCCL_SUCCESS;
3347 0 : }
3348 :
3349 0 : HcclResult GetServerIdByRankId(const std::vector<RankInfo_t> &rankList, const u32 &rankId, u32 &serverId)
3350 : {
3351 0 : for (auto &iter : rankList) {
3352 0 : if (iter.rankId == rankId) {
3353 0 : serverId = iter.serverIdx;
3354 0 : return HCCL_SUCCESS;
3355 : }
3356 : }
3357 0 : return HCCL_E_NOT_FOUND;
3358 : }
3359 :
3360 0 : HcclResult GetOpWorkspaceMemSize(bool isOfflineCompilation, HcclCMDType hcclOpType, HcomOpParam *hcomOpParam, s32 serverNum, u64 &opMemSize)
3361 : {
3362 : HcclResult ret;
3363 0 : const u32 alignSize = HCCL_ALIGN_SIZE;
3364 0 : u32 dataTypeSize = 0;
3365 0 : std::string sCollectiveType(hcomOpParam->opType);
3366 :
3367 0 : ret = SalGetDataTypeSize(hcomOpParam->dataType, dataTypeSize);
3368 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[GetOp][WorkspaceMemSize]op[%s]: get data size failed. ret[%d]",
3369 : sCollectiveType.c_str(), ret), ret);
3370 :
3371 0 : u64 getMemSize = 0;
3372 0 : u32 rankSize = hcomOpParam->rankSize;
3373 0 : CHK_RET(GetOpScratchMemSize(isOfflineCompilation, hcclOpType, hcomOpParam, getMemSize, dataTypeSize, rankSize, serverNum));
3374 :
3375 : // 算子所需的内存大小,加上固定32kb长度,并按对齐后回传
3376 0 : opMemSize = HCCL_WORKSPACE_MEM_32_KB;
3377 0 : opMemSize += getMemSize;
3378 0 : opMemSize = (opMemSize + alignSize - 1) / alignSize * alignSize;
3379 :
3380 0 : HCCL_INFO("workspace memory size: op[%s], data type[%s], count[%llu], "\
3381 : "group[%s], rank size[%u], size[%llu], mem size[%llu].",
3382 : sCollectiveType.c_str(), GetDataTypeEnumStr(hcomOpParam->dataType).c_str(), hcomOpParam->count,
3383 : hcomOpParam->group, rankSize, getMemSize, opMemSize);
3384 :
3385 0 : return HCCL_SUCCESS;
3386 0 : }
3387 :
3388 7 : HcclResult GetOpScratchMemSize(bool isOfflineCompilation, HcclCMDType hcclOpType, HcomOpParam *hcomOpParam,
3389 : u64 &opMemSize, u32 dataTypeSize, s32 rankSize, s32 serverNum)
3390 : {
3391 7 : CHK_PTR_NULL(hcomOpParam);
3392 7 : constexpr u8 devType_950 = 6; // 950枚举值为6,需要统一整改
3393 7 : constexpr u8 devType_960 = 8; // 960枚举值为8
3394 7 : u64 count = hcomOpParam->count;
3395 14 : std::string sCollectiveType(hcomOpParam->opType);
3396 :
3397 7 : std::string socVersionStr(hcomOpParam->socVersion);
3398 7 : DevType devType = DevType::DEV_TYPE_COUNT;
3399 7 : CHK_RET(hrtGetDeviceTypeBySocVersion(socVersionStr, devType));
3400 :
3401 6 : std::shared_ptr<hccl::hcclComm> hcclComm;
3402 6 : std::string group = hcomOpParam->group == nullptr ? HCCL_WORLD_GROUP : hcomOpParam->group;
3403 : // 获取通信域句柄,因为91095不需要获取通信域句柄以感知aivonly,暂时规避
3404 6 : if (static_cast<u8>(devType) != devType_950 && static_cast<u8>(devType) != devType_960) {
3405 6 : CHK_RET(HcomGetCommByGroup(group.c_str(), hcclComm));
3406 : }
3407 :
3408 5 : std::vector<HcclAlgoType> algoTypeArr = CommConfiger::GetInstance().GetCommConfigAlgoConfig(group, HcclCMDType::HCCL_CMD_ALLTOALLV);
3409 5 : bool UseOneLayerAlltoAllv = (algoTypeArr[0] == HcclAlgoType::HCCL_ALGO_TYPE_NA &&
3410 0 : algoTypeArr[1] == HcclAlgoType::HCCL_ALGO_TYPE_PAIRWISE);
3411 :
3412 : // 是否需要额外申请scratch mem
3413 5 : if (hcclOpType == HCCL_CMD_REDUCE_SCATTER_V) {
3414 0 : CHK_RET(GetRedcueScatterVScratchMemSize(hcomOpParam, opMemSize));
3415 5 : } else if (hcclOpType == HCCL_CMD_REDUCE_SCATTER) {
3416 : // ReduceScatter 所需workspace memory: count * 单个数据的size * rank_size
3417 2 : opMemSize = count * dataTypeSize * rankSize;
3418 :
3419 : u8 deterministic;
3420 2 : std::string socVersionStr(hcomOpParam->socVersion);
3421 2 : DevType devType = DevType::DEV_TYPE_COUNT;
3422 2 : CHK_RET(hrtGetDeviceTypeBySocVersion(socVersionStr, devType));
3423 2 : CHK_RET(GetDeterministic(devType, hcomOpParam->geDeterministic, deterministic));
3424 2 : if(deterministic == DETERMINISTIC_ENABLE || deterministic == DETERMINISTIC_STRICT){
3425 0 : const u32 NUM_SIZE_TWO = 2;
3426 0 : opMemSize *= NUM_SIZE_TWO;
3427 : }
3428 5 : } else if (hcclOpType == HCCL_CMD_ALLTOALL) {
3429 : // AlltoAll 所需workspace memory :input mem size
3430 1 : opMemSize += (count * dataTypeSize * rankSize);
3431 2 : } else if (hcclOpType == HCCL_CMD_BROADCAST) {
3432 2 : if (count * dataTypeSize <= HCCL_MID_COUNT_32_MB) {
3433 1 : opMemSize += count * dataTypeSize * HCCL_MEMSIZE_HD_FACTOR;
3434 : }
3435 0 : } else if ((hcclOpType == HCCL_CMD_ALLTOALLV ||
3436 0 : hcclOpType == HCCL_CMD_ALLTOALLVC) &&
3437 0 : !UseOneLayerAlltoAllv && static_cast<u32>(rankSize) > HCCL_ALLTOALLV_P2P_SIZE) {
3438 : // 离线编译场景需要重新计算
3439 0 : if (isOfflineCompilation) {
3440 0 : if (hcclOpType == HCCL_CMD_ALLTOALLV) {
3441 0 : HCCL_ERROR("[GetOpScratchMemSize] offline compilation is not support HcomAllToAllV");
3442 0 : return HCCL_E_PARA;
3443 : }
3444 : }
3445 0 : s32 deviceLogicId = 0;
3446 0 : if (!isOfflineCompilation) {
3447 : // 获取deviceLogicID
3448 0 : CHK_RET(HcomGetDevId(hcomOpParam->group, &deviceLogicId));
3449 0 : CHK_RET(hrtSetDevice(deviceLogicId));
3450 : }
3451 0 : if (hcclOpType == HCCL_CMD_ALLTOALLV) {
3452 0 : CHK_RET(GetAlltoAllvStagedScratchMemSize(hcomOpParam, rankSize, opMemSize));
3453 : } else {
3454 0 : CHK_RET(GetAlltoAllvcStagedScratchMemSize(hcomOpParam, rankSize, opMemSize));
3455 : }
3456 0 : if (!isOfflineCompilation) {
3457 0 : CHK_RET(hrtResetDevice(deviceLogicId));
3458 : }
3459 0 : } else if (hcclOpType == HCCL_CMD_ALLREDUCE) {
3460 : // 判断 aiv_only
3461 0 : bool isAivOnlyMode = false;
3462 : u8 deterministic;
3463 :
3464 : // 91095环境下,暂时不需要感知是否为aivonly模式
3465 0 : if (static_cast<u8>(devType) != devType_950 && static_cast<u8>(devType) != devType_960) {
3466 0 : CHK_RET(hcclComm->GetOnlyAivModeConfig(isAivOnlyMode));
3467 : }
3468 0 : CHK_RET(GetDeterministic(devType, hcomOpParam->geDeterministic, deterministic));
3469 :
3470 0 : if (deterministic != DETERMINISTIC_DISABLE) {
3471 0 : CHK_RET(GetAllReduceScratchMemSize(isOfflineCompilation, hcomOpParam, serverNum, rankSize, opMemSize));
3472 : } else {
3473 : // 数据量大以及aivOnly的情况下需要申请scratch mem
3474 0 : if (count * dataTypeSize <= HCCL_MID_COUNT_16_MB || isAivOnlyMode) {
3475 0 : opMemSize += count * dataTypeSize * HCCL_MEMSIZE_HD_FACTOR;
3476 : }
3477 : }
3478 : }
3479 :
3480 5 : HCCL_INFO("workspace memory size: op[%s], scratch mem size[%llu]", sCollectiveType.c_str(), opMemSize);
3481 5 : return HCCL_SUCCESS;
3482 7 : }
3483 :
3484 0 : HcclResult GetAlltoAllvStagedScratchMemSize(HcomOpParam *hcomOpParam, u32 rankSize, u64 &getMemSize)
3485 : {
3486 0 : if (rankSize > ALLTOALLV_RANK_MAX_NUM) {
3487 0 : HCCL_ERROR("[GetAlltoAllvStagedScratchMemSize] Invalid rankSize[%u]", rankSize);
3488 0 : return HCCL_E_PARA;
3489 : }
3490 0 : u64 memSize = 0;
3491 :
3492 0 : std::vector<u64> sendCountsUnsigned(rankSize, 0);
3493 0 : std::vector<u64> sendDisplsUnsigned(rankSize, 0);
3494 0 : std::vector<u64> recvCountsUnsigned(rankSize, 0);
3495 0 : std::vector<u64> recvDisplsUnsigned(rankSize, 0);
3496 :
3497 0 : for (u32 i = 0; i < rankSize; i++) {
3498 0 : sendCountsUnsigned[i] = static_cast<u64 *>(hcomOpParam->All2AllDataDes.sendCounts)[i];
3499 0 : sendDisplsUnsigned[i] = static_cast<u64 *>(hcomOpParam->All2AllDataDes.sendDispls)[i];
3500 0 : recvCountsUnsigned[i] = static_cast<u64 *>(hcomOpParam->All2AllDataDes.recvCounts)[i];
3501 0 : recvDisplsUnsigned[i] = static_cast<u64 *>(hcomOpParam->All2AllDataDes.recvDispls)[i];
3502 : }
3503 :
3504 0 : CHK_RET(HcomGetAlltoAllStagedWorkSpaceMemSize(hcomOpParam->group,
3505 : sendCountsUnsigned.data(), sendDisplsUnsigned.data(), hcomOpParam->All2AllDataDes.sendType,
3506 : recvCountsUnsigned.data(), recvDisplsUnsigned.data(), hcomOpParam->All2AllDataDes.recvType,
3507 : memSize));
3508 :
3509 0 : getMemSize += memSize;
3510 :
3511 0 : return HCCL_SUCCESS;
3512 0 : }
3513 :
3514 0 : HcclResult GetAlltoAllvcStagedScratchMemSize(HcomOpParam *hcomOpParam, u32 rankSize, u64 &getMemSize)
3515 : {
3516 0 : if (rankSize > ALLTOALLVC_RANK_MAX_NUM) {
3517 0 : HCCL_ERROR("[GetAlltoAllvcStagedScratchMemSize] Invalid rankSize[%u]", rankSize);
3518 0 : return HCCL_E_PARA;
3519 : }
3520 0 : u64 memSize = 0;
3521 0 : HcclDataType sendType = hcomOpParam->All2AllDataDes.sendType;
3522 0 : HcclDataType recvType = hcomOpParam->All2AllDataDes.recvType;
3523 :
3524 0 : int64_t* sendCountMatrix = static_cast<int64_t *>(hcomOpParam->All2AllDataDes.sendCountMatrix);
3525 :
3526 0 : u32 sendTypeSize = 0, recvTypeSize = 0;
3527 0 : CHK_RET(SalGetDataTypeSize(sendType, sendTypeSize));
3528 0 : CHK_RET(SalGetDataTypeSize(recvType, recvTypeSize));
3529 0 : std::vector<SendRecvInfo> allMeshAggregationSendRecvInfo;
3530 0 : for (u32 i = 0; i < rankSize; i++) {
3531 0 : SendRecvInfo sendRecvInfo;
3532 0 : u64 curSendDispls = 0;
3533 0 : u64 curRecvDispls = 0;
3534 0 : for (u32 j = 0; j < rankSize; j++) {
3535 0 : u64 curSendCounts = sendCountMatrix[i * rankSize + j];
3536 0 : sendRecvInfo.sendCounts.push_back(curSendCounts);
3537 0 : sendRecvInfo.sendDispls.push_back(curSendDispls);
3538 0 : sendRecvInfo.sendLength.push_back(curSendCounts * sendTypeSize);
3539 0 : sendRecvInfo.sendOffset.push_back(curSendDispls * sendTypeSize);
3540 0 : HCCL_DEBUG("GetAlltoAllvcStagedScratchMemSize rankID[%u], curSendCounts[%llu], curSendDispls[%llu]",
3541 : i, curSendCounts, curSendDispls);
3542 0 : curSendDispls += curSendCounts;
3543 :
3544 0 : u64 curRecvCounts = sendCountMatrix[i + rankSize * j];
3545 0 : sendRecvInfo.recvCounts.push_back(curRecvCounts);
3546 0 : sendRecvInfo.recvDispls.push_back(curRecvDispls);
3547 0 : sendRecvInfo.recvLength.push_back(curRecvCounts * recvTypeSize);
3548 0 : sendRecvInfo.recvOffset.push_back(curRecvDispls * recvTypeSize);
3549 0 : HCCL_DEBUG("GetAlltoAllvcStagedScratchMemSize rankID[%u], curRecvCounts[%llu], curRecvDispls[%llu]",
3550 : i, curRecvCounts, curRecvDispls);
3551 0 : curRecvDispls += curRecvCounts;
3552 : }
3553 0 : allMeshAggregationSendRecvInfo.push_back(std::move(sendRecvInfo));
3554 0 : }
3555 :
3556 0 : CHK_RET(HcomGetAlltoAllvcStagedWorkSpaceMemSize(hcomOpParam->group, allMeshAggregationSendRecvInfo, memSize));
3557 0 : getMemSize += memSize;
3558 :
3559 0 : return HCCL_SUCCESS;
3560 0 : }
3561 :
3562 0 : HcclResult GetRedcueScatterVScratchMemSize(HcomOpParam *hcomOpParam, u64 &getMemSize)
3563 : {
3564 : DevType devType;
3565 0 : std::string socVerStr(hcomOpParam->socVersion);
3566 0 : CHK_RET(GetOffDeviceTypeWithoutDev(socVerStr, devType));
3567 : u8 deterministic;
3568 0 : CHK_RET(GetDeterministic(devType, hcomOpParam->geDeterministic, deterministic));
3569 :
3570 0 : const u32 deviceEight = 8;
3571 0 : const u32 paddingLen = 1024;
3572 0 : u64 dataTypeSize = SIZE_TABLE[hcomOpParam->dataType];
3573 0 : u64 ranksize = hcomOpParam->rankSize;
3574 : // 910B 确定性 || 910B 多module
3575 0 : if (devType == DevType::DEV_TYPE_910B && (deterministic != DETERMINISTIC_DISABLE || ranksize > deviceEight )) {
3576 0 : u64 maxCount = 0;
3577 0 : for (u32 i = 0; i < ranksize; i++) {
3578 : // reducescatterv复用HcomOpParam的All2AllDataDes字段
3579 0 : maxCount = std::max(maxCount, static_cast<u64 *>(hcomOpParam->All2AllDataDes.sendCounts)[i]);
3580 : }
3581 0 : getMemSize = (maxCount * dataTypeSize + paddingLen) * ranksize;
3582 0 : HCCL_INFO("[GetRedcueScatterVScratchMemSize] maxCount[%llu], getMemSize[%llu]", maxCount, getMemSize);
3583 0 : } else if (devType == DevType::DEV_TYPE_910B && ranksize <= deviceEight) {
3584 0 : getMemSize = hcomOpParam->count * dataTypeSize * ranksize;
3585 0 : HCCL_INFO("[GetRedcueScatterVScratchMemSize] getMemSize[%llu]", getMemSize);
3586 : } else {
3587 0 : getMemSize = hcomOpParam->count * dataTypeSize;
3588 : }
3589 0 : HCCL_DEBUG("[GetRedcueScatterVScratchMemSize] rankSize[%llu] getMemSize[%llu]", ranksize, getMemSize);
3590 0 : return HCCL_SUCCESS;
3591 0 : }
3592 :
3593 0 : HcclResult GetAllReduceScratchMemSize(bool isOfflineCompilation, HcomOpParam *hcomOpParam, s32 serverNum, s32 rankSize, u64 &getMemSize)
3594 : {
3595 0 : u64 scratchSize = 0;
3596 :
3597 0 : bool no_impl_compile = isOfflineCompilation || hcomOpParam->groupListSize > 0;
3598 0 : if (no_impl_compile) {
3599 0 : HcclResult ret = GetAllReduceScratchSizeWithoutDev(hcomOpParam, serverNum, rankSize, scratchSize);
3600 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("no_impl_compile [GetAllReduceScratchMemSize] fail ",
3601 : HCOM_ERROR_CODE(HCCL_E_PARA)), HCCL_E_PARA);
3602 : } else {
3603 0 : CHK_RET(HcomGetAllReduceScratchSize(hcomOpParam->group, hcomOpParam->count, hcomOpParam->dataType, scratchSize));
3604 : }
3605 0 : u64 memSize = SIZE_TABLE[hcomOpParam->dataType] * hcomOpParam->count;
3606 :
3607 0 : HCCL_DEBUG("[GetAllReduceScratchMemSize] serverNum=%u, memSize=%llu, rankSize=%u, scratchSize=%llu",
3608 : serverNum, memSize, rankSize, scratchSize);
3609 0 : getMemSize += scratchSize;
3610 :
3611 0 : return HCCL_SUCCESS;
3612 : }
3613 :
3614 0 : HcclResult GetAllReduceScratchSizeWithoutDev(HcomOpParam *hcomOpParam, s32 serverNum, s32 rankSize, u64 &scratchSize)
3615 : {
3616 : // 检查是否使能确定性计算
3617 :
3618 0 : bool supportInlineReduce = (hcomOpParam->reduceOp != HCCL_REDUCE_PROD) && (hcomOpParam->dataType != HcclDataType::HCCL_DATA_TYPE_INT64);
3619 :
3620 : DevType devType;
3621 0 : std::string socVerStr(hcomOpParam->socVersion);
3622 0 : CHK_RET(GetOffDeviceTypeWithoutDev(socVerStr, devType));
3623 0 : bool isAscend910B = (devType == DevType::DEV_TYPE_910B);
3624 0 : u64 memSize = SIZE_TABLE[hcomOpParam->dataType] * hcomOpParam->count;
3625 : u8 deterministic;
3626 0 : CHK_RET(GetDeterministic(devType, hcomOpParam->geDeterministic, deterministic));
3627 0 : if (deterministic != DETERMINISTIC_DISABLE && serverNum <= 1 && isAscend910B && supportInlineReduce) {
3628 0 : const u32 deviceEight = 8;
3629 0 : const s32 deviceTwo = 2;
3630 :
3631 0 : scratchSize = 0;
3632 0 : if (serverNum == 0) {
3633 : // 无效serverNum,按最大需求申请
3634 0 : if (memSize <= HCCL_SMALL_COUNT_GRAPH_64_KB) {
3635 : // 小数据
3636 0 : scratchSize = memSize * (deviceEight - 1);
3637 : }
3638 0 : } else if (serverNum == 1 && rankSize > deviceTwo) {
3639 : // 有效serverNum,按实际需求申请
3640 0 : if (memSize <= HCCL_SMALL_COUNT_GRAPH_64_KB) {
3641 : // 小数据
3642 0 : if (rankSize == deviceEight) {
3643 0 : scratchSize = 0; // Small Count HD
3644 : } else {
3645 0 : scratchSize = memSize * (rankSize - 1); // Small Count Reduce+Bcast
3646 : }
3647 : }
3648 : }
3649 : }
3650 0 : HCCL_DEBUG("[GetAllReduceScratchMemSizeWithoutDev] serverNum[%d], memSize[%llu], rankSize[%d], scratchSize[%llu]",
3651 : serverNum, memSize, rankSize, scratchSize);
3652 0 : return HCCL_SUCCESS;
3653 0 : }
3654 :
3655 0 : bool IsNeedCalTaskNum(HcclCMDType opType)
3656 : {
3657 : const std::vector<HcclCMDType> hcomNeedCalTaskNumMap = {
3658 : HCCL_CMD_ALLREDUCE,
3659 : HCCL_CMD_ALLGATHER,
3660 : HCCL_CMD_REDUCE_SCATTER,
3661 : HCCL_CMD_ALLTOALL,
3662 : HCCL_CMD_ALLTOALLV,
3663 : HCCL_CMD_ALLTOALLVC
3664 0 : };
3665 0 : auto it = std::find(hcomNeedCalTaskNumMap.begin(), hcomNeedCalTaskNumMap.end(), opType);
3666 0 : return (it != hcomNeedCalTaskNumMap.end()) ? true : false;
3667 0 : }
3668 :
3669 0 : HcclResult GetDefaultAlgoLevel1(s32 serverNum, AlgTypeLevel1 &algType)
3670 : {
3671 0 : u32 num = serverNum;
3672 0 : if (num >= HCCL_INTER_SERVER_RING_ALGO_MAX_SUPPORT_SERVER_NUM) {
3673 : // server 数为 8 以上:使用 HD 算法
3674 0 : algType = AlgTypeLevel1::ALG_LEVEL1_HD;
3675 : } else {
3676 : // server 数为 2 的非整数次幂:使用 RING 算法
3677 : // server 数为 2 的整数次幂:使用 HD 算法
3678 0 : algType = (((num & (num - 1)) != 0) || (num == 1)) ? \
3679 : AlgTypeLevel1::ALG_LEVEL1_RING : AlgTypeLevel1::ALG_LEVEL1_HD;
3680 : }
3681 :
3682 0 : return HCCL_SUCCESS;
3683 : }
3684 :
3685 0 : HcclResult GetAlgoLevel1(s32 serverNum, std::string &opType, AlgTypeLevel1 &algType)
3686 : {
3687 0 : char* mmSysGetEnvValue = nullptr;
3688 0 : MM_SYS_GET_ENV(MM_ENV_HCCL_ALGO, mmSysGetEnvValue);
3689 0 : std::string hcclAlgo = (mmSysGetEnvValue != nullptr) ? mmSysGetEnvValue : "EmptyString";
3690 0 : if (hcclAlgo != "EmptyString") {
3691 : // 删除空格
3692 0 : std::string rawAlgoConfig = hcclAlgo;
3693 0 : rawAlgoConfig.erase(std::remove(rawAlgoConfig.begin(), rawAlgoConfig.end(), ' '), rawAlgoConfig.end());
3694 :
3695 0 : std::string algoConfig;
3696 0 : CHK_RET(SplitHcclOpTypeConfig(rawAlgoConfig, opType, algoConfig));
3697 :
3698 : // 匹配字段"level1:"
3699 0 : std::string level1 = "level1:";
3700 0 : std::size_t found = algoConfig.find(level1);
3701 0 : if ((found == 0) || (found == (algoConfig.length() - level1.size())) || found == std::string::npos) {
3702 : // HCCL_ALGO中"level1:"配置有问题,走默认获取AlgoLevel1方式
3703 0 : HCCL_WARNING("Level 1 is not configured.");
3704 0 : CHK_RET(GetDefaultAlgoLevel1(serverNum, algType));
3705 : } else {
3706 : // 截取HCCL_ALGO中"level1:"之后的字段
3707 0 : std::string remainAlgoConfig = algoConfig.substr(found + level1.size());
3708 0 : std::string level1AlgoConfig = remainAlgoConfig.substr(0, remainAlgoConfig.find(";"));
3709 :
3710 : const std::map<std::string, AlgTypeLevel1> hcclAlgoLevel1Map = {
3711 0 : {"null", AlgTypeLevel1::ALG_LEVEL1_RESERVED},
3712 0 : {"ring", AlgTypeLevel1::ALG_LEVEL1_WHOLE_RING},
3713 0 : {"pipeline", AlgTypeLevel1::ALG_LEVEL1_PIPELINE},
3714 0 : {"fullmesh", AlgTypeLevel1::ALG_LEVEL1_RESERVED},
3715 0 : {"H-D_R", AlgTypeLevel1::ALG_LEVEL1_HD},
3716 0 : {"pairwise", AlgTypeLevel1::ALG_LEVEL1_RESERVED},
3717 0 : {"NHR", AlgTypeLevel1::ALG_LEVEL1_NHR},
3718 0 : {"NHR_V1", AlgTypeLevel1::ALG_LEVEL1_NHR_V1},
3719 0 : {"AHC", AlgTypeLevel1::ALG_LEVEL1_AHC},
3720 0 : {"AHC_BROKE", AlgTypeLevel1::ALG_LEVEL1_AHC_BROKE},
3721 0 : {"NB", AlgTypeLevel1::ALG_LEVEL1_NB},
3722 0 : {"NA", AlgTypeLevel1::ALG_LEVEL1_RESERVED},
3723 0 : };
3724 :
3725 0 : auto iterAlgoLevel1 = hcclAlgoLevel1Map.find(level1AlgoConfig);
3726 0 : if (iterAlgoLevel1 == hcclAlgoLevel1Map.end()) {
3727 0 : HCCL_ERROR("[GetAlgoLevel1] algo config is invalid, level %s is not supported.",
3728 : level1AlgoConfig.c_str());
3729 0 : return HCCL_E_PARA;
3730 : }
3731 :
3732 0 : algType = iterAlgoLevel1->second;
3733 0 : if (algType == AlgTypeLevel1::ALG_LEVEL1_RESERVED) {
3734 0 : CHK_RET(GetDefaultAlgoLevel1(serverNum, algType));
3735 : }
3736 0 : }
3737 0 : } else {
3738 0 : CHK_RET(GetDefaultAlgoLevel1(serverNum, algType));
3739 : }
3740 :
3741 0 : HCCL_INFO("[GetAlgoLevel1] level1[%u].", algType);
3742 0 : auto iter = HCCL_ALGO_LEVEL1_NAME_MAP.find(algType);
3743 0 : CHK_PRT_RET(iter == HCCL_ALGO_LEVEL1_NAME_MAP.end(), HCCL_ERROR("level1: algType[%u] is invalid.", algType),
3744 : HCCL_E_INTERNAL);
3745 0 : HCCL_DEBUG("hccl algorithm: there are %d server in level1,"\
3746 : " the algorithm for setting environment variables is %s algo.", serverNum, iter->second.c_str());
3747 0 : return HCCL_SUCCESS;
3748 0 : }
3749 :
3750 0 : HcclResult SplitHcclOpTypeConfig(const std::string &algoConfig, const std::string &opType,
3751 : std::string &specificAlgoConfig)
3752 : {
3753 : // 对algoConfig进行/切分
3754 0 : std::size_t foundSeparator = algoConfig.find("/");
3755 0 : if ((foundSeparator == algoConfig.length() - 1) || (foundSeparator == 0)) {
3756 0 : HCCL_ERROR("[HcomOpUtils][SplitHcclOpType]algo config is invalid at split sign.");
3757 0 : return HCCL_E_PARA;
3758 0 : } else if (foundSeparator == std::string::npos) {
3759 0 : specificAlgoConfig = algoConfig;
3760 0 : return HCCL_SUCCESS;
3761 : }
3762 :
3763 0 : std::string remainAlgoConfig = algoConfig.substr(foundSeparator + 1);
3764 0 : std::string currentConfig = algoConfig.substr(0, foundSeparator);
3765 0 : std::size_t foundEqual = currentConfig.find("=");
3766 0 : if ((foundEqual == algoConfig.length() - 1) || (foundEqual == 0) || (foundEqual == std::string::npos)) {
3767 0 : HCCL_ERROR("[HcomOpUtils][SplitHcclOpType]algo config is invalid at equal sign.");
3768 0 : return HCCL_E_PARA;
3769 : }
3770 :
3771 0 : std::string currentOpType = currentConfig.substr(0, foundEqual);
3772 0 : if (currentOpType == opType) {
3773 0 : specificAlgoConfig = currentConfig;
3774 0 : return HCCL_SUCCESS;
3775 : }
3776 :
3777 0 : if (!remainAlgoConfig.empty()) {
3778 0 : CHK_RET(SplitHcclOpTypeConfig(remainAlgoConfig, opType, specificAlgoConfig));
3779 : }
3780 0 : return HCCL_SUCCESS;
3781 0 : }
3782 :
3783 0 : HcclResult GetDefaultAlgoLevel0Module(s32 deviceNumPerServer, AlgTypeLevel0 &algType, std::string soc_version)
3784 : {
3785 0 : if (soc_version == "Ascend910B") {
3786 0 : algType = AlgTypeLevel0::ALG_LEVEL0_8P_RING;
3787 0 : } else if (deviceNumPerServer == TASK_NUM_DEVICE_FOUR) {
3788 0 : algType = AlgTypeLevel0::ALG_LEVEL0_4P_MESH;
3789 : } else {
3790 0 : algType = AlgTypeLevel0::ALG_LEVEL0_NP_SINGLE_RING;
3791 : }
3792 0 : auto iter = HCCL_ALGO_LEVEL0_NAME_MAP.find(algType);
3793 0 : CHK_PRT_RET(iter == HCCL_ALGO_LEVEL0_NAME_MAP.end(), HCCL_ERROR("level0: algType[%u] is invalid.", algType),
3794 : HCCL_E_INTERNAL);
3795 0 : HCCL_DEBUG("hccl algorithm: [Module(aiserver)] there are %d device in level0, using %s algo.", \
3796 : deviceNumPerServer, iter->second.c_str());
3797 0 : return HCCL_SUCCESS;
3798 : }
3799 :
3800 0 : HcclResult GetAlgType(s32 deviceNumPerServer,
3801 : s32 serverNum, std::string opType, std::string socVersionStr, AlgType &algType)
3802 : {
3803 : AlgTypeLevel0 algType0;
3804 : AlgTypeLevel1 algType1;
3805 :
3806 : // 因为非标卡计算出来的task num比标卡场景多,因此task num精确评估暂不区分标卡和非标卡
3807 0 : CHK_RET(GetDefaultAlgoLevel0Module(deviceNumPerServer, algType0, socVersionStr));
3808 0 : CHK_RET(GetAlgoLevel1(serverNum, opType, algType1));
3809 0 : algType.algoLevel0 = algType0;
3810 0 : algType.algoLevel1 = algType1;
3811 0 : HCCL_INFO("average device count [%d], algorithm type [%u] is selected.", deviceNumPerServer, algType.algoLevel0);
3812 0 : return HCCL_SUCCESS;
3813 : }
3814 :
3815 0 : HcclResult GetDfxTaskNum(const std::string &sCollectiveType, u32 &taskNum)
3816 : {
3817 0 : if (sCollectiveType == HCCL_KERNEL_OP_TYPE_ALLREDUCE) {
3818 0 : taskNum += DFX_PADDING_TASK_NUM;
3819 : }
3820 0 : taskNum += DFX_DEFAULT_TASK_NUM;
3821 0 : HCCL_DEBUG("[GetDfxTaskNum] cur task num[%u].", taskNum);
3822 0 : return HCCL_SUCCESS;
3823 : }
3824 :
3825 0 : HcclResult GetToSlaveStreamTaskNum(const std::string &sCollectiveType,
3826 : u64 streamNum, u64 piplineSliceNum, u32 &taskNum)
3827 : {
3828 0 : u32 taskNumTmp = 0;
3829 0 : if (sCollectiveType == HCCL_KERNEL_OP_TYPE_ALLREDUCE) {
3830 0 : taskNumTmp = streamNum * MASTER_STREAM_EVENT_NUM * COM_STEP_NUM;
3831 : } else {
3832 0 : taskNumTmp = streamNum * MASTER_STREAM_EVENT_NUM;
3833 : }
3834 0 : if (piplineSliceNum >= MIN_PIPLINE_SLICE_NUM) {
3835 0 : taskNumTmp += piplineSliceNum * PIPLINE_STREAM_EVENT_NUM * COM_STEP_NUM;
3836 : }
3837 0 : taskNum += taskNumTmp;
3838 0 : HCCL_DEBUG("[GetToSlaveStreamTaskNum] cur task num[%u].", taskNum);
3839 0 : return HCCL_SUCCESS;
3840 : }
3841 :
3842 0 : HcclResult GetToMasterStreamTaskNum(const std::string &sCollectiveType, u32 &taskNum)
3843 : {
3844 0 : if (sCollectiveType == HCCL_KERNEL_OP_TYPE_ALLREDUCE) {
3845 0 : taskNum += (SLAVE_STREAM_EVENT_NUM * COM_STEP_NUM);
3846 : } else {
3847 0 : taskNum += SLAVE_STREAM_EVENT_NUM;
3848 : }
3849 0 : HCCL_DEBUG("[GetToMasterStreamTaskNum] cur task num[%u].", taskNum);
3850 0 : return HCCL_SUCCESS;
3851 : }
3852 :
3853 0 : HcclResult GetCombineComTaskNum(const std::string &sCollectiveType, s32 serverNum, s32 deviceNumPerServer,
3854 : u32 &intraTaskNum, u32 &interTaskNum)
3855 : {
3856 : // 打平拓扑server内通信task数量为0
3857 0 : intraTaskNum = 0;
3858 :
3859 0 : interTaskNum = 0;
3860 0 : u32 commStep = deviceNumPerServer * serverNum - 1; // 默认根据ring算法评估
3861 :
3862 : // 计算通信task的数量
3863 0 : if (sCollectiveType == HCCL_KERNEL_OP_TYPE_ALLREDUCE) {
3864 0 : interTaskNum = ALLREDUCE_DEFAULT_COM_STEP * commStep;
3865 0 : } else if (sCollectiveType == HCCL_KERNEL_OP_TYPE_ALLGATHER) {
3866 0 : interTaskNum = ALLGATHER_DEFAULT_COM_STEP * commStep;
3867 0 : } else if (sCollectiveType == HCCL_KERNEL_OP_TYPE_REDUCESCATTER) {
3868 0 : interTaskNum = REDUCESCATTER_DEFAULT_COM_STEP * commStep;
3869 0 : } else if (sCollectiveType == HCCL_KERNEL_OP_TYPE_ALLTOALL ||
3870 0 : sCollectiveType == HCCL_KERNEL_OP_TYPE_ALLTOALLV ||
3871 0 : sCollectiveType == HCCL_KERNEL_OP_TYPE_ALLTOALLVC) {
3872 0 : interTaskNum = ALLTOALL_DEFAULT_COM_STEP * commStep;
3873 : } else {
3874 0 : HCCL_ERROR("[HcomOpUtils][GetCombineComTaskNum]The current operator [%s] do not support tasknum "
3875 : "accurate evaluation.", sCollectiveType.c_str());
3876 0 : return HCCL_E_NOT_SUPPORT;
3877 : }
3878 :
3879 0 : HCCL_INFO("[HcomOpUtils][GetCombineComTaskNum]op[%s], cur intraTaskNum is[%u], interTaskNum is[%u], commStep[%u].",
3880 : sCollectiveType.c_str(), intraTaskNum, interTaskNum, commStep);
3881 0 : return HCCL_SUCCESS;
3882 : }
3883 :
3884 0 : HcclResult GetIntraComTaskNum(const std::string &sCollectiveType, s32 deviceNumPerServer,
3885 : u64 streamNum, const AlgType &algType, u32 &taskNum, u64 totalSize)
3886 : {
3887 0 : taskNum = 0;
3888 0 : u32 commStep = 0;
3889 0 : u32 commStepDeter = 0;
3890 :
3891 : // 获取通信步骤
3892 0 : if (algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_8P_RING) {
3893 0 : commStep += ALG_8P_RING_COMM_STEP;
3894 0 : commStepDeter = commStep;
3895 0 : } else if (algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_NP_DOUBLE_RING) {
3896 0 : commStep += (deviceNumPerServer - 1);
3897 0 : commStepDeter = commStep;
3898 0 : } else if (algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_4P_MESH || deviceNumPerServer > TASK_NUM_DEVICE_ONE) {
3899 : // 涉及确定性计算的通信步骤单独计算
3900 0 : if (totalSize <= HCCL_SMALL_COUNT_GRAPH_64_KB) {
3901 0 : commStepDeter += (deviceNumPerServer - 1);
3902 : } else {
3903 0 : commStepDeter += (GetExternalInputHcclDeterministicV2() != DETERMINISTIC_DISABLE ?
3904 0 : ((deviceNumPerServer - 1) * (deviceNumPerServer - 1)) : (deviceNumPerServer - 1));
3905 : }
3906 0 : commStep += (deviceNumPerServer - 1);
3907 : }
3908 : // 计算通信task的数量
3909 0 : if (sCollectiveType == HCCL_KERNEL_OP_TYPE_ALLREDUCE) {
3910 0 : taskNum = REDUCESCATTER_DEFAULT_COM_STEP * commStepDeter + ALLGATHER_DEFAULT_COM_STEP * commStep;
3911 0 : } else if (sCollectiveType == HCCL_KERNEL_OP_TYPE_ALLGATHER) {
3912 0 : taskNum = ALLGATHER_DEFAULT_COM_STEP * commStep;
3913 0 : } else if (sCollectiveType == HCCL_KERNEL_OP_TYPE_REDUCESCATTER) {
3914 0 : taskNum = REDUCESCATTER_DEFAULT_COM_STEP * commStepDeter;
3915 0 : } else if (sCollectiveType == HCCL_KERNEL_OP_TYPE_ALLTOALL ||
3916 0 : sCollectiveType == HCCL_KERNEL_OP_TYPE_ALLTOALLV ||
3917 0 : sCollectiveType == HCCL_KERNEL_OP_TYPE_ALLTOALLVC) {
3918 0 : commStep = deviceNumPerServer - 1; // 按照pairwise计算server内通信步数
3919 0 : taskNum = ALLTOALL_DEFAULT_COM_STEP * commStep;
3920 : } else {
3921 0 : HCCL_ERROR("The current operator is not supported tasknum accurate evaluation.");
3922 0 : return HCCL_E_NOT_SUPPORT;
3923 : }
3924 0 : HCCL_INFO("[GetIntraComTaskNum] op[%s], cur tasknum is[%u], commStep[%u], totalSize[%llu]",
3925 : sCollectiveType.c_str(), taskNum, commStep, totalSize);
3926 0 : return HCCL_SUCCESS;
3927 : }
3928 :
3929 0 : HcclResult GetBetweenServersStep(s32 serverNum, u32 &commStep)
3930 : {
3931 0 : if ((serverNum & (serverNum - 1)) == 0) {
3932 : // 如果serverNum是2的整数次幂,使用HD算法评估CollectiveOp的taskNum
3933 0 : commStep += SalLog2(serverNum);
3934 0 : } else if (serverNum < SERVER_NUM_EIGHT) {
3935 : // 如果serverNum是2的非整数次幂并且小于8,使用ring算法评估CollectiveOp的taskNum
3936 0 : commStep += (serverNum - 1);
3937 : } else {
3938 : // 计算大于serverNum的最大2的整数次幂的值;以N为rankSize, 使用HD算法评估CollectiveOp的taskNum
3939 0 : s32 bit = 0;
3940 0 : while (serverNum > 0) {
3941 0 : serverNum >>= 1;
3942 0 : bit++;
3943 : }
3944 0 : commStep += bit;
3945 : }
3946 0 : HCCL_DEBUG("Get BetweenServers Step [%u]", commStep);
3947 0 : return HCCL_SUCCESS;
3948 : }
3949 :
3950 0 : HcclResult GetInterComTaskNum(const std::string &sCollectiveType, s32 serverNum, s32 deviceNumPerServer,
3951 : DevType devType, u32 &taskNum, const std::string& group)
3952 : {
3953 0 : taskNum = 0;
3954 0 : u32 commStep = 0;
3955 :
3956 : // 获取server间通信步骤
3957 0 : if (serverNum > SERVER_NUM_ONE) {
3958 0 : CHK_RET(GetBetweenServersStep(serverNum, commStep)); // 默认情况下根据serverNum按ring或HD算法评估
3959 : // 计算通信task的数量
3960 0 : if (sCollectiveType == HCCL_KERNEL_OP_TYPE_ALLREDUCE) {
3961 0 : taskNum = ALLREDUCE_DEFAULT_COM_STEP * commStep;
3962 0 : } else if (sCollectiveType == HCCL_KERNEL_OP_TYPE_ALLGATHER) {
3963 0 : taskNum = ALLGATHER_DEFAULT_COM_STEP * commStep;
3964 0 : } else if (sCollectiveType == HCCL_KERNEL_OP_TYPE_REDUCESCATTER) {
3965 0 : taskNum = REDUCESCATTER_DEFAULT_COM_STEP * commStep;
3966 0 : } else if (sCollectiveType == HCCL_KERNEL_OP_TYPE_ALLTOALL ||
3967 0 : sCollectiveType == HCCL_KERNEL_OP_TYPE_ALLTOALLV ||
3968 0 : sCollectiveType == HCCL_KERNEL_OP_TYPE_ALLTOALLVC) {
3969 0 : std::vector<HcclAlgoType> algoTypeArr = CommConfiger::GetInstance().GetCommConfigAlgoConfig(group);
3970 0 : bool useOneLevelAlgorithm = (algoTypeArr[0] == HcclAlgoType::HCCL_ALGO_TYPE_NA &&
3971 0 : algoTypeArr[1] == HcclAlgoType::HCCL_ALGO_TYPE_PAIRWISE);
3972 0 : s32 meshNum = (devType == DevType::DEV_TYPE_910) ? serverNum * 2 : serverNum;
3973 0 : commStep = useOneLevelAlgorithm ? ((meshNum - 1) * deviceNumPerServer) : (meshNum - 1);
3974 0 : taskNum = ALLTOALL_DEFAULT_COM_STEP * commStep;
3975 0 : } else {
3976 0 : HCCL_ERROR("The current operator is not supported tasknum accurate evaluation.");
3977 0 : return HCCL_E_NOT_SUPPORT;
3978 : }
3979 : }
3980 0 : HCCL_INFO("[GetInterComTaskNum]op[%s], cur tasknum is[%u], commStep[%u].",
3981 : sCollectiveType.c_str(), taskNum, commStep);
3982 0 : return HCCL_SUCCESS;
3983 : }
3984 :
3985 0 : HcclResult CalcTaskNum(HcomOpParam *hcomOpParam, const u64 &streamNum, const s32 &deviceNumPerServer, const s32 &serverNum,
3986 : bool multiModuleDiffDeviceNumMode, u32 &taskNum, DevType devType)
3987 : {
3988 0 : u32 masterTaskNum = 0;
3989 0 : u32 slaveTaskNum = 0;
3990 0 : u32 piplineTaskNum = 0;
3991 :
3992 0 : std::string sCollectiveType(hcomOpParam->opType);
3993 :
3994 : HcclResult ret;
3995 0 : HcclUs startut = TIME_NOW();
3996 :
3997 0 : auto iter = HCCL_OPTYPE_NAME_MAP.find(hcomOpParam->opType);
3998 0 : HcclCMDType hcclOpType = (iter != HCCL_OPTYPE_NAME_MAP.end()) ? iter->second : HcclCMDType::HCCL_CMD_INVALID;
3999 :
4000 0 : string algName;
4001 0 : bool ifAiv = false;
4002 0 : std::shared_ptr<hccl::hcclComm> hcclComm;
4003 : // 获取通信域句柄
4004 0 : std::string group = hcomOpParam->group == nullptr ? HCCL_WORLD_GROUP : hcomOpParam->group;
4005 0 : CHK_RET(HcomGetCommByGroup(group.c_str(), hcclComm));
4006 : // 判断是否是AIV场景
4007 0 : void* counts = nullptr;
4008 0 : ret = hcclComm->HcclSelectAlg(hcclOpType, hcomOpParam->count, counts, hcomOpParam->dataType,
4009 0 : hcomOpParam->reduceOp, hcomOpParam->aivCoreLimit, ifAiv, algName);
4010 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
4011 : HCCL_ERROR("[HcomGetWorkspaceSubStreamNum] HcclSelectAlg failed, ret[%d], optype[%d], count[%llu],"
4012 : "dataType[%d], reduceOp[%d]", ret, hcomOpParam->opType, hcomOpParam->count,
4013 : hcomOpParam->dataType, hcomOpParam->reduceOp), ret);
4014 0 : HCCL_INFO("[%s] HcclSelectAlg success ifAiv[%d] algName[%s] optype[%d] count[%llu] dataType[%d] reduceOp[%d]",
4015 : __func__, ifAiv, algName.c_str(), hcomOpParam->opType, hcomOpParam->count,
4016 : hcomOpParam->dataType, hcomOpParam->reduceOp);
4017 : // AIV和非rdma场景下,task数量固定
4018 0 : if (ifAiv && algName.find("Rdma") == std::string::npos) {
4019 0 : taskNum = AIV_DEFAULT_TASK_NUM;
4020 0 : HCCL_INFO("[%s] GetAndSetTaskNum success taskNum[%u]", __func__, taskNum);
4021 0 : return HCCL_SUCCESS;
4022 : }
4023 :
4024 0 : if (!IsNeedCalTaskNum(hcclOpType)) {
4025 0 : if (hcclOpType == HCCL_CMD_SEND || hcclOpType == HCCL_CMD_RECEIVE) {
4026 0 : taskNum = SEND_RECEIVE_TASK_NUM;
4027 : } else {
4028 0 : taskNum = OP_DEFAULT_TASK_NUM;
4029 : }
4030 : } else {
4031 0 : AlgType algType;
4032 0 : std::string socVersionStr(hcomOpParam->socVersion);
4033 :
4034 : // 获取通信算法
4035 0 : CHK_RET(GetAlgType(deviceNumPerServer, serverNum, hcomOpParam->opType, socVersionStr, algType));
4036 :
4037 : // 如果在线编译没有获取到ranktable file,则返回默认task数量
4038 0 : if ((deviceNumPerServer == 0) && (serverNum == 0)) {
4039 0 : taskNum = OP_DEFAULT_TASK_NUM;
4040 : } else {
4041 : // 计算Server间pipline切分数量
4042 : u32 dataTypeSize;
4043 0 : u64 totalSize = 0;
4044 0 : ret = SalGetDataTypeSize(hcomOpParam->dataType, dataTypeSize);
4045 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Get][OpWorkspaceMemSize]op[%s]: get data size failed. ret[%d]",
4046 : sCollectiveType.c_str(), ret), ret);
4047 :
4048 0 : totalSize = hcomOpParam->count * dataTypeSize;
4049 :
4050 0 : u64 piplineSliceNum = CalculatePiplineSliceNum(hcclOpType, totalSize, algType,
4051 0 : devType, deviceNumPerServer, serverNum);
4052 :
4053 : // 计算DFX校验task数量
4054 0 : CHK_RET(GetDfxTaskNum(sCollectiveType, masterTaskNum));
4055 : // 计算与从stream同步task数量
4056 0 : CHK_RET(GetToSlaveStreamTaskNum(sCollectiveType, streamNum, piplineSliceNum, masterTaskNum));
4057 : // 计算与主stream同步task数量
4058 0 : CHK_RET(GetToMasterStreamTaskNum(sCollectiveType, slaveTaskNum));
4059 : // 计算Server间Pipline从stream和主stream同步的task数量
4060 0 : piplineTaskNum += (piplineSliceNum >= MIN_PIPLINE_SLICE_NUM) ?
4061 : piplineSliceNum * PIPLINE_STREAM_EVENT_NUM * COM_STEP_NUM : 0;
4062 :
4063 0 : u32 intraTaskNum = 0;
4064 0 : u32 interTaskNum = 0;
4065 : // 获取Server内通信task数量
4066 :
4067 0 : if (multiModuleDiffDeviceNumMode) {
4068 : // 获取打平拓扑通信task数量
4069 0 : CHK_RET(GetCombineComTaskNum(sCollectiveType, serverNum, deviceNumPerServer, intraTaskNum,
4070 : interTaskNum));
4071 : } else {
4072 0 : CHK_RET(GetIntraComTaskNum(sCollectiveType, deviceNumPerServer, streamNum,
4073 : algType, intraTaskNum, totalSize));
4074 : // 获取Server间通信task数量, 从stream没有server间task
4075 0 : std::string group = hcomOpParam->group == nullptr ? HCCL_WORLD_GROUP : hcomOpParam->group;
4076 0 : CHK_RET(GetInterComTaskNum(sCollectiveType, serverNum, deviceNumPerServer, devType,
4077 : interTaskNum, group));
4078 0 : }
4079 :
4080 : // 计算通信task
4081 0 : if (piplineSliceNum >= MIN_PIPLINE_SLICE_NUM) {
4082 0 : masterTaskNum += intraTaskNum * piplineSliceNum;
4083 0 : slaveTaskNum += intraTaskNum * piplineSliceNum;
4084 0 : piplineTaskNum += interTaskNum * piplineSliceNum;
4085 : } else {
4086 0 : masterTaskNum += intraTaskNum + interTaskNum;
4087 0 : slaveTaskNum += intraTaskNum;
4088 : }
4089 : }
4090 0 : }
4091 0 : if (taskNum == 0) {
4092 0 : taskNum = std::max(masterTaskNum, std::max(slaveTaskNum, piplineTaskNum));
4093 : }
4094 :
4095 0 : HCCL_INFO("GetAndSetTaskNum success, cost time[%lld]us taskNum[%u]", DURATION_US(TIME_NOW() - startut), taskNum);
4096 0 : return HCCL_SUCCESS;
4097 0 : }
4098 :
4099 0 : HcclResult CalcTaskNumV2(HcomOpParam *hcomOpParam, u32 &taskNum)
4100 : {
4101 0 : HcclUs startut = TIME_NOW();
4102 :
4103 0 : auto iter = HCCL_OPTYPE_NAME_MAP.find(hcomOpParam->opType);
4104 0 : HcclCMDType hcclOpType = (iter != HCCL_OPTYPE_NAME_MAP.end()) ? iter->second : HcclCMDType::HCCL_CMD_INVALID;
4105 :
4106 0 : if (!IsNeedCalTaskNum(hcclOpType)) {
4107 0 : if (hcclOpType == HCCL_CMD_SEND || hcclOpType == HCCL_CMD_RECEIVE) {
4108 0 : taskNum = SEND_RECEIVE_TASK_NUM;
4109 : } else {
4110 0 : taskNum = OP_DEFAULT_TASK_NUM;
4111 : }
4112 : } else {
4113 0 : CHK_RET(HcomCalcTaskNum(hcomOpParam, taskNum));
4114 : }
4115 :
4116 0 : HCCL_INFO("GetAndSetTaskNum success, cost time[%lld]us taskNum[%u]", DURATION_US(TIME_NOW() - startut), taskNum);
4117 0 : return HCCL_SUCCESS;
4118 : }
4119 :
4120 0 : HcclResult HcomGetMemType(const char *group, const char *socVersion, bool isMalloc, u32 *memType, bool *isTsMem,
4121 : bool withoutImplCompile, bool level2Address)
4122 : {
4123 0 : DevType devType = DevType::DEV_TYPE_COUNT;
4124 0 : std::string socVersionStr(socVersion);
4125 0 : const u32 NUM_SIZE_TWO = 2;
4126 :
4127 0 : CHK_RET(hrtGetDeviceTypeBySocVersion(socVersionStr, devType));
4128 :
4129 0 : if (isMalloc) {
4130 0 : if (Is310PDevice()) {
4131 0 : if (devType == DevType::DEV_TYPE_310P3 || devType == DevType::DEV_TYPE_310P1) {
4132 0 : if (level2Address) { // 310P二级地址刷新时申请内存类型为:RT_MEMORY_TS
4133 0 : *isTsMem = true;
4134 0 : *memType = static_cast<int>(ACL_MEM_TYPE_LOW_BAND_WIDTH);
4135 : } else {
4136 0 : *memType = static_cast<int>(ACL_MEM_TYPE_LOW_BAND_WIDTH);
4137 : }
4138 : } else {
4139 0 : *memType = static_cast<int>(ACL_MEM_TYPE_HIGH_BAND_WIDTH);
4140 : }
4141 : } else {
4142 0 : if (devType == DevType::DEV_TYPE_310P3) {
4143 0 : if (level2Address) { // 310P二级地址刷新时申请内存类型为:RT_MEMORY_TS
4144 0 : *isTsMem = true;
4145 0 : *memType = static_cast<int>(ACL_MEM_TYPE_LOW_BAND_WIDTH) |
4146 : static_cast<int>(ACL_MEM_MALLOC_NORMAL_ONLY_P2P);
4147 : } else {
4148 0 : *memType = static_cast<int>(ACL_MEM_TYPE_LOW_BAND_WIDTH) |
4149 : static_cast<int>(ACL_MEM_MALLOC_NORMAL_ONLY_P2P);
4150 : }
4151 0 : } else if (devType == DevType::DEV_TYPE_310P1) {
4152 0 : *memType = static_cast<int>(ACL_MEM_TYPE_LOW_BAND_WIDTH);
4153 : } else {
4154 0 : *memType = static_cast<int>(ACL_MEM_TYPE_HIGH_BAND_WIDTH) |
4155 : static_cast<int>(ACL_MEM_MALLOC_NORMAL_ONLY_P2P);
4156 : }
4157 : }
4158 0 : return HCCL_SUCCESS;
4159 : }
4160 :
4161 0 : if (devType == DevType::DEV_TYPE_310P3 || devType == DevType::DEV_TYPE_310P1) {
4162 0 : u32 numHccsLink = 0;
4163 0 : u32 rankSize = 0;
4164 0 : if (!withoutImplCompile) {
4165 0 : CHK_RET(HcomGetRankSize(group, &rankSize));
4166 0 : CHK_RET(HcomGetHccsLinkNum(group, &numHccsLink));
4167 : }
4168 0 : if ((withoutImplCompile || !(rankSize == NUM_SIZE_TWO && numHccsLink == NUM_SIZE_TWO))) {
4169 : // 所有形态切换子包后,改用acl_mem类型
4170 0 : *memType = RT_MEMORY_P2P_DDR;
4171 : }
4172 : }
4173 :
4174 0 : return HCCL_SUCCESS;
4175 0 : }
4176 :
4177 0 : HcclResult GetDeterministic(DevType devType, u8 geDetOption, u8 &deterministic)
4178 : {
4179 0 : deterministic = DETERMINISTIC_DISABLE; // 默认为不支持
4180 :
4181 0 : char* mmSysGetEnvValue = nullptr;
4182 0 : MM_SYS_GET_ENV(MM_ENV_HCCL_DETERMINISTIC, mmSysGetEnvValue);
4183 0 : std::string hcclDeterministicEnv = (mmSysGetEnvValue != nullptr) ? mmSysGetEnvValue : "EmptyString";
4184 0 : if (hcclDeterministicEnv != "EmptyString") {
4185 : // 环境变量优先
4186 0 : std::transform(
4187 : hcclDeterministicEnv.begin(), hcclDeterministicEnv.end(), hcclDeterministicEnv.begin(), ::toupper);
4188 0 : if (hcclDeterministicEnv == "FALSE") {
4189 0 : deterministic = DETERMINISTIC_DISABLE;
4190 0 : } else if(hcclDeterministicEnv == "TRUE") {
4191 0 : deterministic = DETERMINISTIC_ENABLE;
4192 0 : } else if(hcclDeterministicEnv == "STRICT") {
4193 0 : CHK_PRT_RET(devType != DevType::DEV_TYPE_910B && devType != DevType::DEV_TYPE_910_93,
4194 : HCCL_ERROR("ParserHcclDeterministic: reduce order preservation is not supported for devType[%d]", devType),
4195 : HCCL_E_NOT_SUPPORT);
4196 0 : deterministic = DETERMINISTIC_STRICT;
4197 : } else {
4198 0 : HCCL_ERROR("[GetDeterministic] HCCL_DETERMINISTIC is set to [%s], which is incorrect. Please check",
4199 : hcclDeterministicEnv.c_str());
4200 0 : return HCCL_E_PARA;
4201 : }
4202 : } else {
4203 : // 未配环境变量,检查ge option
4204 0 : if (geDetOption == 1) {
4205 0 : deterministic = DETERMINISTIC_ENABLE;
4206 0 : } else if (geDetOption == 2) {
4207 0 : CHK_PRT_RET(devType != DevType::DEV_TYPE_910B && devType != DevType::DEV_TYPE_910_93,
4208 : HCCL_ERROR("ParserHcclDeterministic: reduce order preservation is not supported for devType[%d]", devType),
4209 : HCCL_E_NOT_SUPPORT);
4210 0 : deterministic = DETERMINISTIC_STRICT;
4211 : }
4212 : }
4213 :
4214 0 : return HCCL_SUCCESS;
4215 0 : }
4216 :
4217 0 : HcclResult HcomGenerateCclOpTag(const char *opType, s64 hcomComm, const char *group, char *sTag)
4218 : {
4219 0 : CHK_PTR_NULL(group);
4220 0 : std::string groupName(group);
4221 0 : std::string tag;
4222 0 : GenerateCclOpTag(opType, hcomComm, groupName, tag);
4223 0 : int32_t sret = memcpy_s(sTag, CCL_OP_TAG_MAX_LEN, tag.c_str(), (tag.length() + 1));
4224 0 : CHK_PRT_RET(sret != EOK, HCCL_ERROR("[HcomGenerateCclOpTag][Tag]memcpy failed. ret[%d],"
4225 : "params:destMaxSize[%zu],count[%zu]", sret, CCL_OP_TAG_MAX_LEN, (tag.length() + 1)), HCCL_E_PARA);
4226 0 : return HCCL_SUCCESS;
4227 0 : }
4228 :
4229 :
4230 0 : void HcomSetDumpDebugMode(const bool dumpDebug)
4231 : {
4232 0 : SetDumpDebugMode(dumpDebug);
4233 0 : }
4234 :
4235 0 : void HcomSetLaunchKernelMode(bool state)
4236 : {
4237 0 : SetLaunchKernelMode(state);
4238 0 : }
4239 :
4240 0 : HcclResult HcomTbeMemClean(int64_t addrList[], int64_t sizeList[], uint32_t count,
4241 : aclrtStream stream, int32_t deviceLogicId)
4242 : {
4243 0 : CHK_RET(HcclTbeMemClean(addrList, sizeList, count, stream,deviceLogicId));
4244 0 : return HCCL_SUCCESS;
4245 : }
|