Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "coll_all_to_all_direct_fullmesh_aiv_executor.h"
12 :
13 : namespace hccl {
14 :
15 0 : CollAlltoAllDirectFullmeshAIVExecutor::CollAlltoAllDirectFullmeshAIVExecutor(const HcclDispatcher dispatcher,
16 0 : std::unique_ptr<TopoMatcher> &topoMatcher)
17 0 : : CollAlltoAllExecutor(dispatcher, topoMatcher)
18 : {
19 0 : desc_.isAivMode = true;
20 0 : }
21 :
22 0 : HcclResult CollAlltoAllDirectFullmeshAIVExecutor::CalcStreamNum(u32& streamNum)
23 : {
24 0 : streamNum = 0; // AIV通信不需要申请从流
25 0 : HCCL_INFO("[CollAlltoAllDirectFullmeshAIVExecutor][%s] tag[%s] streamNum[%u].", __func__, tag_.c_str(), streamNum);
26 0 : return HCCL_SUCCESS;
27 : }
28 :
29 0 : HcclResult CollAlltoAllDirectFullmeshAIVExecutor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
30 : {
31 0 : TransportMemType inputType = TransportMemType::RESERVED;
32 0 : TransportMemType outputType = TransportMemType::RESERVED;
33 0 : CHK_RET(CalcTransportMemType(inputType, outputType));
34 0 : CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
35 0 : return HCCL_SUCCESS;
36 : }
37 :
38 0 : HcclResult CollAlltoAllDirectFullmeshAIVExecutor::CalcTransportMemType(TransportMemType &inputType,
39 : TransportMemType &outputType)
40 : {
41 0 : inputType = TransportMemType::CCL_INPUT;
42 0 : outputType = TransportMemType::CCL_OUTPUT;
43 0 : HCCL_INFO("[CollAlltoAllDirectFullmeshAIVExecutor][%s] tag[%s] inputType[%d], outputType[%d].", __func__,
44 : tag_.c_str(), inputType, outputType);
45 0 : return HCCL_SUCCESS;
46 : }
47 :
48 : // level0-level1 打平fullmesh
49 : // RDMA链路
50 0 : HcclResult CollAlltoAllDirectFullmeshAIVExecutor::CalcLevel0CommInfo(TransportMemType inputType,
51 : TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
52 : {
53 0 : CommParaInfo commCombinePara(COMM_COMBINE_ORDER, CommType::COMM_TAG_MESH);
54 0 : commCombinePara.meshSinglePlane = true;
55 0 : CHK_RET(CalcCommPlaneInfo(tag_, commCombinePara, opTransport[COMM_COMBINE_ORDER], inputType, outputType));
56 :
57 0 : LevelNSubCommTransport &commTransportLevel0 = opTransport[COMM_COMBINE_ORDER];
58 0 : for (u32 subCommIndex = 0; subCommIndex < commTransportLevel0.size(); subCommIndex++) {
59 0 : for (auto &transportRequest : commTransportLevel0[subCommIndex].transportRequests) {
60 0 : transportRequest.isUsedRdma = true;
61 : }
62 : }
63 0 : return HCCL_SUCCESS;
64 0 : }
65 :
66 0 : HcclResult CollAlltoAllDirectFullmeshAIVExecutor::PrepareCommInfoToDevice(AlgResourceResponse& algResource)
67 : {
68 0 : HCCL_INFO("[CollAlltoAllDirectFullmeshAIVExecutor][PrepareCommInfoToDevice]"
69 : "alltoall aiv copy comm info to device.");
70 0 : CHK_RET(CopyAivCommInfoToDevice(COMM_COMBINE_ORDER, COMM_INDEX_0, algResource));
71 0 : return HCCL_SUCCESS;
72 : }
73 :
74 0 : HcclResult CollAlltoAllDirectFullmeshAIVExecutor::CopyAivCommInfoToDevice(const CommPlane levelIndex, const u32 subLevelIndex,
75 : AlgResourceResponse& algResource)
76 : {
77 0 : algResResp_ = &algResource;
78 0 : CHK_RET(CheckCommSize(levelIndex, subLevelIndex + 1));
79 0 : SubCommInfo commInfo = GetSubCommInfo(levelIndex, subLevelIndex);
80 0 : u32 localRank = commInfo.localRank;
81 0 : u32 localRankSize = commInfo.localRankSize;
82 0 : void* buffersInOut[MAX_RANK_SIZE_RDMA * 2] = {};
83 :
84 0 : for (u32 i = 0; i < localRankSize; i++) {
85 0 : u32 idx = (i << 1);
86 0 : if (i != localRank) {
87 0 : CHK_RET(commInfo.links[i]->GetRemoteMem(UserMemType::INPUT_MEM, &(buffersInOut[idx])));
88 0 : CHK_RET(commInfo.links[i]->GetRemoteMem(UserMemType::OUTPUT_MEM, &(buffersInOut[idx + 1])));
89 : } else {
90 0 : buffersInOut[idx] = algResource.cclInputMem.ptr();
91 0 : buffersInOut[idx + 1] = algResource.cclOutputMem.ptr();
92 : }
93 : }
94 0 : const u32 bufferNum = 2;
95 0 : CHK_RET(hrtMemSyncCopy(algResource.aivCommInfoMem.ptr(), sizeof(u64) * localRankSize * bufferNum,
96 : buffersInOut, sizeof(u64) * localRankSize * bufferNum, HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
97 0 : return HCCL_SUCCESS;
98 0 : }
99 :
100 0 : HcclResult CollAlltoAllDirectFullmeshAIVExecutor::CalNumBlocks(u32& numBlocks, u32 rankSize, u64 dataSize,
101 : HcclCMDType cmdType)
102 : {
103 0 : numBlocks = topoAttr_.moduleNum; // 默认情况使用serverNum个AIV
104 0 : CHK_PRT_RET(numBlocks_ < numBlocks,
105 : HCCL_ERROR("[CollAlltoAllDirectFullmeshAIVExecutor][%s]aivCore[%u] is invalid, at least need[%u].", __func__,
106 : numBlocks_, numBlocks), HCCL_E_PARA);
107 :
108 0 : HCCL_INFO("[CollAlltoAllDirectFullmeshAIVExecutor][%s] numBlocks is set to [%u], limit[%u], recommanded[%u]", __func__,
109 : numBlocks, numBlocks_, numBlocks);
110 0 : return HCCL_SUCCESS;
111 : }
112 :
113 0 : HcclResult CollAlltoAllDirectFullmeshAIVExecutor::Orchestrate(OpParam& param, AlgResourceResponse& algRes)
114 : {
115 0 : HcclUs startut = TIME_NOW();
116 0 : tag_ = param.tag;
117 0 : algResResp_ = &algRes;
118 :
119 0 : HcclResult ret = HCCL_SUCCESS;
120 0 : ExecMem execMem;
121 0 : execMem.count = param.DataDes.count;
122 0 : execMem.inputPtr = param.inputPtr;
123 0 : execMem.outputPtr = param.outputPtr;
124 :
125 0 : execMem.inputMem = algRes.cclInputMem;
126 0 : execMem.outputMem = algRes.cclOutputMem;
127 0 : ret = KernelRun(param, execMem);
128 :
129 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
130 : HCCL_ERROR("[CollAlltoAllDirectFullmeshAIVExecutor][%s]errNo[0x%016llx] tag[%s] executor kernel run failed",
131 : __func__, HCCL_ERROR_CODE(ret), param.tag.c_str()), ret);
132 :
133 0 : HCCL_INFO("tag[%s], AlltoAll executor orchestrate success, take time [%lld]us",
134 : param.tag.c_str(), DURATION_US(TIME_NOW() - startut));
135 0 : return HCCL_SUCCESS;
136 0 : }
137 :
138 0 : HcclResult CollAlltoAllDirectFullmeshAIVExecutor::KernelRun(const OpParam ¶m, ExecMem &execMem)
139 : {
140 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[CollAlltoAllDirectFullmeshAIVExecutor][KernelRun]alltoall aiv enter.");
141 :
142 0 : CHK_RET(CheckCommSize(COMM_COMBINE_ORDER, COMM_INDEX_0 + 1));
143 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_COMBINE_ORDER, COMM_INDEX_0);
144 :
145 : void *buffersIn[MAX_RANK_SIZE];
146 : void *buffersOut[MAX_RANK_SIZE];
147 :
148 0 : u32 localRank = level0CommInfo.localRank;
149 0 : u32 localRankSize = level0CommInfo.localRankSize;
150 0 : HCCL_DEBUG("[CollAlltoAllDirectFullmeshAIVExecutor][%s] userRank [%u] localRank [%u]", __func__,
151 : topoAttr_.userRank, localRank);
152 :
153 0 : buffersIn[0] = execMem.inputMem.ptr();
154 0 : buffersOut[0] = execMem.outputMem.ptr();
155 0 : constexpr u32 BUFFER_IDX_ONE = 1;
156 0 : buffersOut[BUFFER_IDX_ONE] = algResResp_->aivCommInfoMem.ptr(); // 交叉存储cclIn和cclOut
157 :
158 0 : u64 sendCount = *(static_cast<const u64 *>(param.All2AllDataDes.sendCountMatrix));
159 0 : AivOpArgs opArgs { HcclCMDType::HCCL_CMD_ALLTOALL, execMem.inputPtr, execMem.outputPtr, sendCount,
160 0 : param.All2AllDataDes.sendType, HCCL_REDUCE_RESERVED, 0, true};
161 0 : AivTopoArgs topoArgs { localRank, localRankSize, MAX_RANK_SIZE, 0, topoAttr_.serverNum, topoAttr_.deviceType };
162 0 : topoArgs.identify = algoAttr_.identifier;
163 0 : AivResourceArgs resourceArgs { param.tag, param.stream.ptr(), buffersIn, buffersOut, execMem.inputMem.size(),
164 0 : 0, param.aivTag};
165 0 : AivAlgArgs algArgs {0};
166 0 : algArgs.execTimeOut = topoMatcher_->GetExecTimeOutConfig();
167 0 : algArgs.execTimeOutSet = true;
168 0 : algArgs.isNpuDirectRoce = true;
169 0 : algArgs.rmaInfo = reinterpret_cast<uintptr_t>(rmaInfo_);
170 0 : struct AivProfilingInfo aivProfilingInfo;
171 0 : aivProfilingInfo.counter = opCounter_;
172 :
173 : u32 numBlocks;
174 0 : CHK_RET(CalNumBlocks(numBlocks, localRankSize));
175 0 : numBlocks_ = numBlocks;
176 0 : resourceArgs.numBlocks = numBlocks_;
177 0 : HcclResult ret = ExecuteKernelLaunch(opArgs, topoArgs, resourceArgs, algArgs, aivProfilingInfo);
178 :
179 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
180 : HCCL_ERROR("[CollAlltoAllDirectFullmeshAIVExecutor][%s]alltoall aiv failed, return[%d]", __func__, ret), ret);
181 :
182 0 : HCCL_INFO("[CollAlltoAllDirectFullmeshAIVExecutor][%s]alltoall aiv run success.", __func__);
183 0 : return HCCL_SUCCESS;
184 0 : }
185 :
186 : REGISTER_EXEC("AlltoAllDirectFullmeshAIVExecutor", AlltoAllDirectFullmeshAIV, CollAlltoAllDirectFullmeshAIVExecutor);
187 :
188 : } // namespace hccl
|