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(
16 0 : const HcclDispatcher dispatcher, 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 : HcclResult
39 0 : CollAlltoAllDirectFullmeshAIVExecutor::CalcTransportMemType(TransportMemType& inputType, TransportMemType& outputType)
40 : {
41 0 : inputType = TransportMemType::CCL_INPUT;
42 0 : outputType = TransportMemType::CCL_OUTPUT;
43 0 : HCCL_INFO(
44 : "[CollAlltoAllDirectFullmeshAIVExecutor][%s] tag[%s] inputType[%d], outputType[%d].", __func__, tag_.c_str(),
45 : inputType, outputType);
46 0 : return HCCL_SUCCESS;
47 : }
48 :
49 : // level0-level1 打平fullmesh
50 : // RDMA链路
51 0 : HcclResult CollAlltoAllDirectFullmeshAIVExecutor::CalcLevel0CommInfo(
52 : TransportMemType inputType, TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
53 : {
54 0 : CommParaInfo commCombinePara(COMM_COMBINE_ORDER, CommType::COMM_TAG_MESH);
55 0 : commCombinePara.meshSinglePlane = true;
56 0 : CHK_RET(CalcCommPlaneInfo(tag_, commCombinePara, opTransport[COMM_COMBINE_ORDER], inputType, outputType));
57 :
58 0 : LevelNSubCommTransport& commTransportLevel0 = opTransport[COMM_COMBINE_ORDER];
59 0 : for (u32 subCommIndex = 0; subCommIndex < commTransportLevel0.size(); subCommIndex++) {
60 0 : for (auto& transportRequest : commTransportLevel0[subCommIndex].transportRequests) {
61 0 : transportRequest.isUsedRdma = true;
62 : }
63 : }
64 0 : return HCCL_SUCCESS;
65 0 : }
66 :
67 0 : HcclResult CollAlltoAllDirectFullmeshAIVExecutor::PrepareCommInfoToDevice(AlgResourceResponse& algResource)
68 : {
69 0 : HCCL_INFO("[CollAlltoAllDirectFullmeshAIVExecutor][PrepareCommInfoToDevice]"
70 : "alltoall aiv copy comm info to device.");
71 0 : CHK_RET(CopyAivCommInfoToDevice(COMM_COMBINE_ORDER, COMM_INDEX_0, algResource));
72 0 : return HCCL_SUCCESS;
73 : }
74 :
75 0 : HcclResult CollAlltoAllDirectFullmeshAIVExecutor::CopyAivCommInfoToDevice(
76 : const CommPlane levelIndex, const u32 subLevelIndex, AlgResourceResponse& algResource)
77 : {
78 0 : algResResp_ = &algResource;
79 0 : CHK_RET(CheckCommSize(levelIndex, subLevelIndex + 1));
80 0 : SubCommInfo commInfo = GetSubCommInfo(levelIndex, subLevelIndex);
81 0 : u32 localRank = commInfo.localRank;
82 0 : u32 localRankSize = commInfo.localRankSize;
83 0 : void* buffersInOut[MAX_RANK_SIZE_RDMA * 2] = {};
84 :
85 0 : for (u32 i = 0; i < localRankSize; i++) {
86 0 : u32 idx = (i << 1);
87 0 : if (i != localRank) {
88 0 : CHK_RET(commInfo.links[i]->GetRemoteMem(UserMemType::INPUT_MEM, &(buffersInOut[idx])));
89 0 : CHK_RET(commInfo.links[i]->GetRemoteMem(UserMemType::OUTPUT_MEM, &(buffersInOut[idx + 1])));
90 : } else {
91 0 : buffersInOut[idx] = algResource.cclInputMem.ptr();
92 0 : buffersInOut[idx + 1] = algResource.cclOutputMem.ptr();
93 : }
94 : }
95 0 : const u32 bufferNum = 2;
96 0 : CHK_RET(hrtMemSyncCopy(
97 : algResource.aivCommInfoMem.ptr(), sizeof(u64) * localRankSize * bufferNum, buffersInOut,
98 : sizeof(u64) * localRankSize * bufferNum, HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
99 0 : return HCCL_SUCCESS;
100 0 : }
101 :
102 0 : HcclResult CollAlltoAllDirectFullmeshAIVExecutor::CalNumBlocks(
103 : u32& numBlocks, [[maybe_unused]] u32 rankSize, [[maybe_unused]] u64 dataSize, [[maybe_unused]] HcclCMDType cmdType)
104 : {
105 0 : numBlocks = topoAttr_.moduleNum; // 默认情况使用serverNum个AIV
106 0 : CHK_PRT_RET(
107 : numBlocks_ < numBlocks,
108 : HCCL_ERROR(
109 : "[CollAlltoAllDirectFullmeshAIVExecutor][%s]aivCore[%u] is invalid, at least need[%u].", __func__,
110 : numBlocks_, numBlocks),
111 : HCCL_E_PARA);
112 :
113 0 : HCCL_INFO(
114 : "[CollAlltoAllDirectFullmeshAIVExecutor][%s] numBlocks is set to [%u], limit[%u], recommanded[%u]", __func__,
115 : numBlocks, numBlocks_, numBlocks);
116 0 : return HCCL_SUCCESS;
117 : }
118 :
119 0 : HcclResult CollAlltoAllDirectFullmeshAIVExecutor::Orchestrate(OpParam& param, AlgResourceResponse& algRes)
120 : {
121 0 : HcclUs startut = TIME_NOW();
122 0 : tag_ = param.tag;
123 0 : algResResp_ = &algRes;
124 :
125 0 : HcclResult ret = HCCL_SUCCESS;
126 0 : ExecMem execMem;
127 0 : execMem.count = param.DataDes.count;
128 0 : execMem.inputPtr = param.inputPtr;
129 0 : execMem.outputPtr = param.outputPtr;
130 :
131 0 : execMem.inputMem = algRes.cclInputMem;
132 0 : execMem.outputMem = algRes.cclOutputMem;
133 0 : ret = KernelRun(param, execMem);
134 :
135 0 : CHK_PRT_RET(
136 : ret != HCCL_SUCCESS,
137 : HCCL_ERROR(
138 : "[CollAlltoAllDirectFullmeshAIVExecutor][%s]errNo[0x%016llx] tag[%s] executor kernel run failed", __func__,
139 : HCCL_ERROR_CODE(ret), param.tag.c_str()),
140 : ret);
141 :
142 0 : HCCL_INFO(
143 : "tag[%s], AlltoAll executor orchestrate success, take time [%lld]us", param.tag.c_str(),
144 : DURATION_US(TIME_NOW() - startut));
145 0 : return HCCL_SUCCESS;
146 0 : }
147 :
148 0 : HcclResult CollAlltoAllDirectFullmeshAIVExecutor::KernelRun(const OpParam& param, ExecMem& execMem)
149 : {
150 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[CollAlltoAllDirectFullmeshAIVExecutor][KernelRun]alltoall aiv enter.");
151 :
152 0 : CHK_RET(CheckCommSize(COMM_COMBINE_ORDER, COMM_INDEX_0 + 1));
153 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_COMBINE_ORDER, COMM_INDEX_0);
154 :
155 : void* buffersIn[MAX_RANK_SIZE];
156 : void* buffersOut[MAX_RANK_SIZE];
157 :
158 0 : u32 localRank = level0CommInfo.localRank;
159 0 : u32 localRankSize = level0CommInfo.localRankSize;
160 0 : HCCL_DEBUG(
161 : "[CollAlltoAllDirectFullmeshAIVExecutor][%s] userRank [%u] localRank [%u]", __func__, topoAttr_.userRank,
162 : localRank);
163 :
164 0 : buffersIn[0] = execMem.inputMem.ptr();
165 0 : buffersOut[0] = execMem.outputMem.ptr();
166 0 : constexpr u32 BUFFER_IDX_ONE = 1;
167 0 : buffersOut[BUFFER_IDX_ONE] = algResResp_->aivCommInfoMem.ptr(); // 交叉存储cclIn和cclOut
168 :
169 0 : u64 sendCount = *(static_cast<const u64*>(param.All2AllDataDes.sendCountMatrix));
170 0 : AivOpArgs opArgs{
171 : HcclCMDType::HCCL_CMD_ALLTOALL,
172 0 : execMem.inputPtr,
173 0 : execMem.outputPtr,
174 : sendCount,
175 0 : param.All2AllDataDes.sendType,
176 : HCCL_REDUCE_RESERVED,
177 : 0,
178 0 : true};
179 0 : AivTopoArgs topoArgs{localRank, localRankSize, MAX_RANK_SIZE, 0, topoAttr_.serverNum, topoAttr_.deviceType};
180 0 : topoArgs.identify = algoAttr_.identifier;
181 0 : AivResourceArgs resourceArgs{param.tag, param.stream.ptr(), buffersIn, buffersOut, execMem.inputMem.size(),
182 0 : 0, param.aivTag};
183 0 : AivAlgArgs algArgs{0};
184 0 : algArgs.execTimeOut = topoMatcher_->GetExecTimeOutConfig();
185 0 : algArgs.execTimeOutSet = true;
186 0 : algArgs.isNpuDirectRoce = true;
187 0 : algArgs.rmaInfo = reinterpret_cast<uintptr_t>(rmaInfo_);
188 0 : struct AivProfilingInfo aivProfilingInfo;
189 0 : aivProfilingInfo.counter = opCounter_;
190 :
191 : u32 numBlocks;
192 0 : CHK_RET(CalNumBlocks(numBlocks, localRankSize));
193 0 : numBlocks_ = numBlocks;
194 0 : resourceArgs.numBlocks = numBlocks_;
195 0 : HcclResult ret = ExecuteKernelLaunch(opArgs, topoArgs, resourceArgs, algArgs, aivProfilingInfo);
196 :
197 0 : CHK_PRT_RET(
198 : ret != HCCL_SUCCESS,
199 : HCCL_ERROR("[CollAlltoAllDirectFullmeshAIVExecutor][%s]alltoall aiv failed, return[%d]", __func__, ret), ret);
200 :
201 0 : HCCL_INFO("[CollAlltoAllDirectFullmeshAIVExecutor][%s]alltoall aiv run success.", __func__);
202 0 : return HCCL_SUCCESS;
203 0 : }
204 :
205 : REGISTER_EXEC("AlltoAllDirectFullmeshAIVExecutor", AlltoAllDirectFullmeshAIV, CollAlltoAllDirectFullmeshAIVExecutor);
206 :
207 : } // namespace hccl
|