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 <memory>
12 : #include "hccl_impl.h"
13 : #include "alltoall_operator.h"
14 : #include "all_reduce_operator.h"
15 : #include "coll_alg_op_registry.h"
16 : #include "topo_matcher.h"
17 : #include "topo_info_extractor.h"
18 : #include "alg_configurator.h"
19 : #include "hccl_alg.h"
20 :
21 : namespace hccl {
22 : constexpr u32 TINY_MEMORY_SIZE = 32; // sendBuff或recvBuff为空时, 使用的DeviceMem大小
23 :
24 535 : HcclAlg::HcclAlg(CCLBufferManager& cclBufferManager, const HcclDispatcher dispatcher, const HcclDispatcher vDispatcher)
25 535 : : cclBufferManager_(cclBufferManager),
26 535 : dispatcher_(dispatcher),
27 535 : vDispatcher_(vDispatcher)
28 535 : {}
29 :
30 1070 : HcclAlg::~HcclAlg()
31 : {
32 : #ifndef OPEN_HCCL_TEST
33 535 : pimpl_ = nullptr;
34 : #endif
35 1070 : }
36 :
37 524 : HcclResult HcclAlg::Init(
38 : std::unique_ptr<WorkspaceResource>& workSpaceRes, const std::unique_ptr<NotifyPool>& notifyPool,
39 : std::map<HcclIpAddress, HcclNetDevCtx>& netDevCtxMap, const std::unique_ptr<QueueNotifyManager>& queueNotifyManager,
40 : HcclAlgoAttr& algoAttr, HcclTopoAttr& topoAttr, bool isHeterogComm)
41 : {
42 524 : CHK_RET(Init(algoAttr, topoAttr, isHeterogComm));
43 :
44 : #ifndef OPEN_HCCL_TEST
45 : // 老流程使用,新流程的LLT不编译相关的代码
46 523 : pimpl_.reset((new (std::nothrow) hcclImpl(
47 523 : dispatcher_, notifyPool, netDevCtxMap, queueNotifyManager, workSpaceRes, cclBufferManager_, algoAttr_,
48 1046 : topoAttr_, algConfigurator_, topoInfoEx_)));
49 523 : CHK_SMART_PTR_NULL(pimpl_);
50 523 : CHK_RET(pimpl_->Init(isHeterogComm));
51 : #endif
52 523 : return HCCL_SUCCESS;
53 : }
54 :
55 524 : HcclResult HcclAlg::Init(HcclAlgoAttr& algoAttr, HcclTopoAttr& topoAttr, bool isHeterogComm)
56 : {
57 524 : algoAttr_ = algoAttr;
58 524 : topoAttr_ = topoAttr;
59 524 : algConfigurator_.reset(new (std::nothrow) AlgConfigurator(algoAttr_, topoAttr_));
60 524 : CHK_SMART_PTR_NULL(algConfigurator_);
61 524 : CHK_RET(algConfigurator_->Init(isHeterogComm));
62 :
63 523 : TopoType topoType = TopoType::TOPO_TYPE_RESERVED;
64 523 : algConfigurator_->GetTopoType(topoType);
65 523 : topoInfoEx_.reset(new (std::nothrow) TopoInfoExtractor(algoAttr_, topoAttr_, topoType));
66 523 : CHK_SMART_PTR_NULL(topoInfoEx_);
67 523 : CHK_RET(topoInfoEx_->Init(algoAttr_.commAlgoConfig));
68 :
69 523 : std::vector<std::vector<std::vector<u32>>> CommPlaneRanks;
70 523 : CHK_RET(topoInfoEx_->GetCommPlaneRanks(CommPlaneRanks));
71 :
72 523 : std::vector<bool> isBridgeVector;
73 523 : topoInfoEx_->GetIsBridgeVector(isBridgeVector);
74 :
75 523 : std::vector<std::vector<std::vector<u32>>> serverAndsuperPodToRank;
76 523 : CHK_RET(topoInfoEx_->GetRankVecInfo(serverAndsuperPodToRank));
77 :
78 523 : HcclTopoInfo topoInfo;
79 523 : CHK_RET(InitTopoInfo(topoInfo, topoAttr_));
80 :
81 523 : HcclAlgoInfo algoInfo;
82 523 : CHK_RET(InitAlgoInfo(algoInfo, algoAttr_));
83 :
84 523 : HcclExternalEnable externalEnable;
85 523 : CHK_RET(InitExternalEnable(externalEnable));
86 :
87 523 : topoMatcher_.reset((new (std::nothrow) TopoMatcher(
88 1046 : CommPlaneRanks, isBridgeVector, topoInfo, algoInfo, externalEnable, serverAndsuperPodToRank)));
89 523 : CHK_SMART_PTR_NULL(topoMatcher_);
90 :
91 523 : parallelTaskLoader_.reset(
92 523 : static_cast<ParallelTaskLoader*>(new (std::nothrow) ParallelTaskLoader(topoAttr_.deviceLogicId, dispatcher_)));
93 523 : CHK_SMART_PTR_NULL(parallelTaskLoader_);
94 :
95 : #ifndef OPEN_HCCL_TEST
96 523 : if (static_cast<s32>(topoAttr_.devicePhyId) != HOST_DEVICE_ID) {
97 523 : CHK_RET(DeviceMem::alloc(tinySendRecvMem_, TINY_MEMORY_SIZE));
98 : }
99 : #endif
100 523 : return HCCL_SUCCESS;
101 523 : }
102 :
103 83 : std::unique_ptr<CollAlgOperator> HcclAlg::GetAlgOperator(const HcclCMDType& opType, HcclWorkflowMode workflowMode)
104 : {
105 : (void)workflowMode;
106 83 : if (!topoMatcher_) {
107 0 : HCCL_ERROR("[HcclAlg][GetAlgOperator] topoMatcher ptr is null, get algorithm operator failed.");
108 0 : return nullptr;
109 : }
110 81 : std::unique_ptr<CollAlgOperator> operation = CollAlgOpRegistry::Instance().GetAlgOp(
111 81 : opType, algConfigurator_.get(), cclBufferManager_, dispatcher_, topoMatcher_);
112 81 : CHK_PRT_RET(
113 : operation == nullptr, HCCL_ERROR("[HcclAlg][GetAlgOperator] GetAlgOp return nullptr, opType[%d]", opType),
114 : nullptr);
115 81 : if (opType == HcclCMDType::HCCL_CMD_ALLTOALL || opType == HcclCMDType::HCCL_CMD_ALLTOALLV
116 74 : || opType == HcclCMDType::HCCL_CMD_ALLTOALLVC) {
117 8 : AlltoAllOperator* alltoAllOperator = dynamic_cast<AlltoAllOperator*>(operation.get());
118 8 : alltoAllOperator->SetVirtualDispatcher(vDispatcher_);
119 8 : alltoAllOperator->SetParallelTaskLoader(parallelTaskLoader_.get());
120 : }
121 81 : HCCL_INFO("[AIG][GetAlgOperator] GetAlgOperator done");
122 84 : return operation;
123 82 : }
124 :
125 : HcclResult
126 0 : HcclAlg::GetAlltoAllStagedWorkSpaceMemSize(std::vector<SendRecvInfo>& allMeshAggregationSendRecvInfo, u64& memSize)
127 : {
128 0 : AlltoAllOperator operation(algConfigurator_.get(), cclBufferManager_, dispatcher_, topoMatcher_);
129 0 : operation.SetVirtualDispatcher(vDispatcher_);
130 0 : operation.SetParallelTaskLoader(parallelTaskLoader_.get());
131 0 : return operation.GetAlltoAllStagedWorkSpaceMemSize(allMeshAggregationSendRecvInfo, memSize);
132 0 : }
133 :
134 0 : HcclResult HcclAlg::GetAllReduceScratchSize(const u64 count, const HcclDataType dataType, u64& scratchSize)
135 : {
136 0 : AllReduceOperator operation(algConfigurator_.get(), cclBufferManager_, dispatcher_, topoMatcher_);
137 0 : return operation.GetAllReduceScratchSize(count, dataType, scratchSize);
138 0 : }
139 :
140 0 : HcclResult HcclAlg::GetTopoType(TopoType& topoType)
141 : {
142 0 : algConfigurator_->GetTopoType(topoType);
143 0 : return HCCL_SUCCESS;
144 : }
145 :
146 0 : HcclResult HcclAlg::SetAlgType(AlgType algType, HcclCMDType opType)
147 : {
148 0 : return algConfigurator_->SetAlgType(algType, opType);
149 : }
150 :
151 233 : HcclResult HcclAlg::GetAlgType(AlgType& algType, HcclCMDType opType)
152 : {
153 233 : return algConfigurator_->GetAlgType(algType, opType);
154 : }
155 :
156 0 : HcclResult HcclAlg::SupportDeterministicOptim(bool& isDeterministicOptim)
157 : {
158 0 : isDeterministicOptim = algConfigurator_->SupportDeterministicOptim();
159 0 : return HCCL_SUCCESS;
160 : }
161 :
162 80 : u8 HcclAlg::GetDeterministicConfig() const { return topoMatcher_->GetDeterministicConfig(); }
163 :
164 235 : HcclResult HcclAlg::SetDeterministicConfig(const u8 deterministic)
165 : {
166 235 : CHK_RET(topoMatcher_->SetDeterministicConfig(deterministic));
167 235 : return HCCL_SUCCESS;
168 : }
169 :
170 235 : HcclResult HcclAlg::SetAivModeConfig(const bool aivMode)
171 : {
172 235 : CHK_RET(topoMatcher_->SetAivModeConfig(aivMode));
173 235 : return HCCL_SUCCESS;
174 : }
175 :
176 0 : bool HcclAlg::GetAicpuUnfoldConfig() const { return topoMatcher_->GetAicpuUnfoldConfig(); }
177 :
178 98 : bool HcclAlg::GetAivModeConfig() const { return topoMatcher_->GetAivModeConfig(); }
179 :
180 235 : HcclResult HcclAlg::SetAicpuUnfoldConfig(const bool aicpuUnfold)
181 : {
182 235 : CHK_RET(topoMatcher_->SetAicpuUnfoldConfig(aicpuUnfold));
183 235 : return HCCL_SUCCESS;
184 : }
185 :
186 235 : HcclResult HcclAlg::SetExecTimeOutConfig(const s32 execTimeOut)
187 : {
188 235 : CHK_RET(topoMatcher_->SetExecTimeOutConfig(execTimeOut));
189 235 : return HCCL_SUCCESS;
190 : }
191 :
192 235 : HcclResult HcclAlg::SetAlgoConfig(const std::map<HcclCMDType, std::vector<HcclAlgoType>>& algoMap)
193 : {
194 235 : CHK_RET(topoMatcher_->SetAlgoConfig(algoMap));
195 235 : return HCCL_SUCCESS;
196 : }
197 :
198 0 : HcclResult HcclAlg::GetRankVecInfo(std::vector<std::vector<std::vector<u32>>>& serverAndsuperPodToRank)
199 : {
200 0 : CHK_RET(topoInfoEx_->GetRankVecInfo(serverAndsuperPodToRank));
201 0 : return HCCL_SUCCESS;
202 : }
203 :
204 0 : HcclResult HcclAlg::GetIsBridgeVector(std::vector<bool>& isBridgeVector)
205 : {
206 0 : topoInfoEx_->GetIsBridgeVector(isBridgeVector);
207 0 : return HCCL_SUCCESS;
208 : }
209 0 : HcclResult HcclAlg::GetCommPlaneRanks(std::vector<std::vector<std::vector<u32>>>& commPlaneRanks)
210 : {
211 0 : CHK_RET(topoInfoEx_->GetCommPlaneRanks(commPlaneRanks));
212 0 : return HCCL_SUCCESS;
213 : }
214 :
215 0 : void HcclAlg::GetCommPlaneVector(std::vector<std::vector<std::vector<RankInfo>>>& commPlaneVector)
216 : {
217 0 : topoInfoEx_->GetCommPlaneVector(commPlaneVector);
218 0 : }
219 :
220 235 : HcclResult HcclAlg::SetOnlyAivModeConfig(const bool isOnlyAiv)
221 : {
222 235 : CHK_RET(topoMatcher_->SetOnlyAivModeConfig(isOnlyAiv));
223 235 : return HCCL_SUCCESS;
224 : }
225 :
226 : HcclResult
227 0 : HcclAlg::GetCommPlaneSubGroupVector(std::vector<std::vector<std::vector<std::vector<u32>>>>& commPlaneSubGroupVector)
228 : {
229 0 : topoMatcher_->GetCommPlaneSubGroupVector(commPlaneSubGroupVector);
230 0 : return HCCL_SUCCESS;
231 : }
232 :
233 0 : HcclResult HcclAlg::GetAHCAlgOption(std::map<AHCConcOpType, TemplateType>& ahcAlgOption)
234 : {
235 0 : topoMatcher_->GetAHCAlgOption(ahcAlgOption);
236 0 : return HCCL_SUCCESS;
237 : }
238 :
239 0 : HcclResult HcclAlg::GetIsUsedRdmaMap(std::unordered_map<u32, bool>& isUsedRdmaMap)
240 : {
241 0 : CHK_RET(topoInfoEx_->GetIsUsedRdmaMap(isUsedRdmaMap));
242 0 : return HCCL_SUCCESS;
243 : }
244 :
245 5 : HcclResult HcclAlg::GetTinyMem(DeviceMem& tinySendRecvMem)
246 : {
247 5 : tinySendRecvMem = tinySendRecvMem_;
248 5 : return HCCL_SUCCESS;
249 : }
250 :
251 523 : HcclResult HcclAlg::InitExternalEnable(HcclExternalEnable& externalEnable)
252 : {
253 523 : externalEnable.enableFfts = GetExternalInputHcclEnableFfts();
254 523 : externalEnable.deterministic = GetExternalInputHcclDeterministicV2();
255 523 : externalEnable.intraRoceSwitch = GetExternalInputIntraRoceSwitch();
256 523 : externalEnable.dumpDebug = GetExternalInputHcclDumpDebug();
257 523 : externalEnable.aivMode = GetExternalInputHcclAivMode();
258 523 : externalEnable.aicpuUnfold = GetExternalInputHcclAicpuUnfold();
259 523 : externalEnable.execTimeOut = GetInternalExecTimeOut();
260 523 : return HCCL_SUCCESS;
261 : }
262 :
263 523 : HcclResult HcclAlg::InitTopoInfo(HcclTopoInfo& topoInfo, HcclTopoAttr& topoAttr)
264 : {
265 523 : topoInfo.userRank = topoAttr.userRank;
266 523 : topoInfo.userRankSize = topoAttr.userRankSize;
267 523 : topoInfo.devicePhyId = topoAttr.devicePhyId;
268 523 : topoInfo.deviceLogicId = topoAttr.deviceLogicId;
269 523 : topoInfo.nicList = topoAttr.nicList;
270 523 : topoInfo.isSingleMeshAggregation = topoAttr.isSingleMeshAggregation;
271 523 : topoInfo.deviceNumPerAggregation = topoAttr.deviceNumPerAggregation;
272 523 : topoInfo.superPodNum = topoAttr.superPodNum;
273 523 : topoInfo.deviceType = topoAttr.deviceType;
274 523 : topoInfo.serverNum = topoAttr.serverNum;
275 523 : topoInfo.meshAggregationRankSize = topoAttr.meshAggregationRankSize;
276 523 : topoInfo.multiModuleDiffDeviceNumMode = topoAttr.multiModuleDiffDeviceNumMode;
277 523 : topoInfo.multiSuperPodDiffServerNumMode = topoAttr.multiSuperPodDiffServerNumMode;
278 523 : topoInfo.multiSuperPodDiffDeviceNumMode = topoAttr.multiSuperPodDiffDeviceNumMode;
279 523 : topoInfo.isDiffDeviceType = topoAttr.isDiffDeviceType;
280 523 : topoInfo.gcdDeviceNumPerAggregation = topoAttr.gcdDeviceNumPerAggregation;
281 523 : topoInfo.pairLinkCounter = topoAttr.pairLinkCounter;
282 523 : topoInfo.isDiffDeviceModule = topoAttr.isDiffDeviceModule;
283 523 : topoInfo.realUserRank = topoAttr.realUserRank;
284 523 : topoInfo.moduleNum = topoAttr.moduleNum;
285 523 : topoInfo.useSuperPodMode = topoAttr.useSuperPodMode;
286 523 : topoInfo.isARSDoubleRing = topoAttr.isARSDoubleRing;
287 :
288 523 : topoInfoEx_->GetCommPlaneSubGroupVector(topoInfo.CommPlaneSubGroupVector);
289 523 : topoInfoEx_->GetAHCAlgOption(topoInfo.ahcAlgOption);
290 :
291 523 : algConfigurator_->GetTopoType(topoInfo.topoType);
292 523 : topoInfo.is310P3Common = Is310P3Common(algoAttr_.isHaveCpuRank, topoAttr_.deviceType);
293 523 : std::unordered_map<u32, bool> isUsedRdmaMap;
294 523 : CHK_RET(topoInfoEx_->GetIsUsedRdmaMap(isUsedRdmaMap));
295 523 : topoInfo.isUsedRdmaMap = isUsedRdmaMap;
296 523 : return HCCL_SUCCESS;
297 523 : }
298 :
299 523 : HcclResult HcclAlg::InitAlgoInfo(HcclAlgoInfo& algoInfo, HcclAlgoAttr& algoAttr)
300 : {
301 523 : algoInfo.identifier = algoAttr.identifier;
302 523 : algoInfo.inlineReduceSwitchOn = algoAttr.inlineReduceSwitchOn;
303 523 : algoInfo.isUsedRdmaLevel0 = algoAttr.isUsedRdmaLevel0;
304 523 : algoInfo.isSupportAtomicWrite = false; // 涉及到任务编排,当前不能只判断本机驱动版本是否支持
305 523 : return HCCL_SUCCESS;
306 : }
307 :
308 : #ifndef OPEN_HCCL_TEST
309 : // 上层保证,以下方法在初始化成功后才会调用,所以未对pimpl_进行保护判断
310 0 : HcclResult HcclAlg::ReleaseCommInfos() { return pimpl_->ReleaseCommInfos(); }
311 :
312 16 : HcclResult HcclAlg::ClearOpResource(const std::string& tag) { return pimpl_->ClearOpResource(tag); }
313 :
314 1 : HcclResult HcclAlg::CreateMutiStreamRes(
315 : const std::string& tag, Stream& stream, level1StreamInfo_t& streamInfo, AlgType algType, bool isAicpuModeEn)
316 : {
317 1 : return pimpl_->CreateMutiStreamRes(tag, stream, streamInfo, algType, isAicpuModeEn);
318 : }
319 :
320 1 : HcclResult HcclAlg::CreateComm(
321 : const std::string& tag, DeviceMem& inputMem, DeviceMem& outputMem, AlgType algType,
322 : std::unique_ptr<CommInfo>& commInfo, u32 root, bool isP2p, bool isAicpuModeEn)
323 : {
324 1 : return pimpl_->CreateComm(tag, inputMem, outputMem, algType, commInfo, root, isP2p, isAicpuModeEn);
325 : }
326 :
327 0 : HcclResult HcclAlg::CreateComm(
328 : const std::string& tag, DeviceMem& inputMem, DeviceMem& outputMem, AlgType algType, u32 root, bool isP2p)
329 : {
330 0 : return pimpl_->CreateComm(tag, inputMem, outputMem, algType, root, isP2p);
331 : }
332 :
333 0 : void HcclAlg::Break() { pimpl_->Break(); }
334 :
335 148 : HcclResult HcclAlg::SetHDCModeInfo(
336 : std::unordered_map<std::string, std::map<u32, HcclIpAddress>>& rankDevicePhyIdNicInfoMap,
337 : std::vector<u32>& ranksPort, std::vector<u32>& vnicRanksPort, bool isSetHDCModeInfo, bool isUseRankPort)
338 : {
339 148 : pimpl_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap, ranksPort, vnicRanksPort, isSetHDCModeInfo, isUseRankPort);
340 144 : return HCCL_SUCCESS;
341 : }
342 : #endif
343 : } // namespace hccl
|