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 <atomic>
13 : #include <chrono>
14 : #include <thread>
15 : #include <algorithm>
16 : #include <numeric>
17 : #include <unordered_set>
18 : #include <sys/time.h>
19 : #include "externalinput_pub.h"
20 : #include "env_config.h"
21 : #include "p2p_mgmt_pub.h"
22 : #include "opexecounter_pub.h"
23 : #include "config.h"
24 : #include "stream_active_manager.h"
25 : #include "device_capacity.h"
26 : #include "profiling_manager_pub.h"
27 : #include "task_exception_handler_pub.h"
28 : #include "rank_consistentcy_checker.h"
29 : #include "hccl_aiv.h"
30 : #include "adapter_rts_common.h"
31 : #include "coll_alg_utils.h"
32 : #include "../common/src/state_guard.h"
33 : #include "detect_connect_anomalies.h"
34 : #include "alg_profiling.h"
35 : #include "mmpa_api.h"
36 : #include "stream_utils.h"
37 : #include "config_log.h"
38 : #include "../nslbdp/hccl_nslbdp.h"
39 : #include "../common/src/h2d_tlv/hccl_h2dtlv.h"
40 : #include "hccl_one_sided_service.h"
41 : #include "launch_device.h"
42 : #include "hccl_communicator.h"
43 : #include "launch_aicpu.h"
44 : #include "order_launch/order_launch.h"
45 : #include "comm_configer.h"
46 : #include "snapshot_control.h"
47 : #include "comm_topo_desc.h"
48 : #include "hccl_net_dev_defs.h"
49 : #include "aclgraph_callback.h"
50 :
51 : using namespace std;
52 : constexpr u32 MODULE_NUM_FOUR = 4;
53 : constexpr u16 MAX_VALUE_U16 = 0xFFFF;
54 :
55 : namespace hccl
56 : {
57 : static std::mutex g_hcomInitMutex;
58 : static std::atomic<u32> g_enableBackupLinkCommCount{0}; // 开启借轨的通信域计数
59 : constexpr u32 MEMORY_CAPACITY = 256 * 1024;
60 : constexpr u32 WAIT_PREPARE_SLEEP_TIME = 5000;
61 : constexpr u32 SINGLE_SERVER_NUM = 1;
62 : constexpr u32 CONN_LIMIT = 4096;
63 : constexpr u32 COMM_DEV_TYPE_DIGIT_NUM = 8;
64 : constexpr u32 TILINGDATA_BUF_SIZE = 32 * 1024; // 单位:字节
65 : constexpr u32 ALLTOALL_INFO_MATRIX_SIZE = 4;
66 : constexpr u32 AICPU_RETRY_LINKROCE_DEFAULT = 0;
67 : constexpr u32 AICPU_RETRY_LINKROCE_BACKUP = 1;
68 : constexpr u32 SINGLE_PROCESS_MIN_PORT = 1024;
69 : constexpr u32 SINGLE_PROCESS_MAX_PORT = 65535;
70 : constexpr u32 TYPE_USER_MEM = 1;
71 : constexpr u32 NON_BATCH_WRITE_MAX_STREAM_NUM = 19U;
72 : constexpr u64 GIGABYTE_TO_BYTE = 1024ULL * 1024ULL * 1024ULL;
73 : constexpr u8 AICPU_ORDERLAUNCH_INVALID_HCOM_MODE = 255; // 图模式下无附属从流,不进行按序下发
74 : enum TransferMemInfoIdx
75 : {
76 : TRANSFER_MEM_INFO_KEY_IDX = 0,
77 : TRANSFER_MEM_INFO_VALUE_IDX = 1,
78 : TRANSFER_MEM_INFO_RDMA_ENVELOPE_IDX = 2,
79 : TRANSFER_MEM_INFO_IDX_NUM = 3
80 : };
81 :
82 : enum class AicpuLocalNotifyIdx : u32
83 : {
84 : // host-aicpu同步
85 : HOST_TO_AICPU_0 = 0,
86 : HOST_TO_AICPU_1 = 1,
87 :
88 : // 用于控制单算子模式各通信域kernel按序占核的notify
89 : ORDER_INDEX_OPBASE_0 = 2, // host_order流 record, kernel流 wait
90 : ORDER_INDEX_OPBASE_1 = 3, // aicpu_order流 record, host_order流 wait
91 :
92 : // 用于控制Aclgraph模式各通信域kernel按序占核的notify
93 : ORDER_INDEX_ACLGRAPH_0 = 4, // host_order流 record, kernel流 wait
94 : ORDER_INDEX_ACLGRAPH_1 = 5, // aicpu_order流 record, host_order流 wait
95 :
96 : // 用于控制图模式各通信域kernel按序占核的notify
97 : ORDER_INDEX_HCOM_0 = 6, // host_order流 record, kernel流 wait
98 : ORDER_INDEX_HCOM_1 = 7 // aicpu_order流 record, host_order流 wait
99 : };
100 :
101 : enum class AicpuLocalEventIdx : u32
102 : {
103 : /**
104 : *@brief 用于控制Aclgraph模式按序下发控制流入图的event
105 : *@note 通信域绑定Context,而Stream是Context管理的资源,因此对应下在Stream上的event与通信域强相关,需要communicator管理
106 : **/
107 : ORDER_INDEX_ACLGRAPH_EVENT_0 = 0, // kernel流 record, host_order流 wait
108 : ORDER_INDEX_ACLGRAPH_EVENT_1 = 1, // host_order流 record, kernel流 wait
109 : };
110 :
111 391 : HcclCommunicator::HcclCommunicator()
112 391 : : dispatcher_(nullptr), vDispatcher_(nullptr), notifyPool_(nullptr),
113 391 : initializedFlag_(ATOMIC_FLAG_INIT), userRank_(INVALID_VALUE_RANKID), realUserRank_(INVALID_VALUE_RANKID),
114 1173 : userRankSize_(INVALID_VALUE_RANKSIZE), drvInit_(false), inlineReduceSwitchOn_(true),
115 391 : nicDeployment_(NICDeployment::NIC_DEPLOYMENT_DEVICE), devicePhyId_(INVALID_UINT),
116 782 : deviceLogicId_(-1), localRank_(INVALID_VALUE_RANKID), hostSocketHandle_(nullptr),
117 782 : isUsedRdmaLevel0_(false), nicInitialized_(0), hcomGroupNicInit_(false),
118 782 : profilingMode_(HcomProfilingMode::PROFILING_CLOSE), raResourceInit_(false),
119 391 : interServer_(false), isSingleMeshAggregation_(false), cclBufferManager_(CCLBufferManager()),
120 391 : isExecuteProfilingInit_(false), deviceType_(DevType::DEV_TYPE_COUNT),
121 391 : commHandle_(nullptr),
122 1173 : commWorkMode_(WorkMode::HCCL_MODE_NORMAL), meshAggregationRankSize_(0), isHaveCpuRank_(false), ranktableCrc_(0),
123 391 : multiModuleDiffDeviceNumMode_(false), multiSuperPodDiffServerNumMode_(false), multiSuperPodDiffDeviceNumMode_(false),
124 391 : isStandardCard_(false), is310PDuoCard_(false), hccsPortNum_(-1),
125 782 : loopBackIp_(HcclIpAddress(COMM_LOOPBACK_IP)), profilingInitiated_(false), callbackThreadId_(INVALID_U64),
126 391 : role_(SERVER_ROLE_SOCKET),
127 391 : isHostUseDevNic_(false),
128 301461 : isAllRankSamePlane_(false), serverNum_(0), moduleNum_(0)
129 : {
130 391 : zeroCopyAclGraph_.reset(new (std::nothrow) ZeroCopyAclGraph());
131 391 : if (zeroCopyAclGraph_ == nullptr)
132 : {
133 0 : HCCL_ERROR("new ZeroCopyAclGraph failed!");
134 : }
135 391 : commConfig_ = CommConfig();
136 391 : dpuManager_.reset(new (std::nothrow) DpuManager());
137 391 : if (dpuManager_ == nullptr) {
138 0 : HCCL_ERROR("new DpuManager failed!");
139 : }
140 391 : }
141 :
142 413 : HcclCommunicator::HcclCommunicator(const CommConfig &commConfig)
143 413 : : dispatcher_(nullptr), vDispatcher_(nullptr), notifyPool_(nullptr),
144 413 : initializedFlag_(ATOMIC_FLAG_INIT), userRank_(INVALID_VALUE_RANKID), realUserRank_(INVALID_VALUE_RANKID),
145 1239 : userRankSize_(INVALID_VALUE_RANKSIZE), drvInit_(false), inlineReduceSwitchOn_(true),
146 413 : nicDeployment_(NICDeployment::NIC_DEPLOYMENT_DEVICE), devicePhyId_(INVALID_UINT),
147 826 : deviceLogicId_(-1), localRank_(INVALID_VALUE_RANKID), hostSocketHandle_(nullptr),
148 826 : isUsedRdmaLevel0_(false), nicInitialized_(0), hcomGroupNicInit_(false),
149 826 : profilingMode_(HcomProfilingMode::PROFILING_CLOSE), raResourceInit_(false),
150 412 : interServer_(false), isSingleMeshAggregation_(false), cclBufferManager_(CCLBufferManager()),
151 413 : isExecuteProfilingInit_(false), deviceType_(DevType::DEV_TYPE_COUNT),
152 412 : commHandle_(nullptr),
153 1237 : commWorkMode_(WorkMode::HCCL_MODE_NORMAL), meshAggregationRankSize_(0), isHaveCpuRank_(false), ranktableCrc_(0),
154 412 : multiModuleDiffDeviceNumMode_(false), multiSuperPodDiffServerNumMode_(false),
155 413 : isStandardCard_(false), is310PDuoCard_(false), hccsPortNum_(-1),
156 825 : loopBackIp_(HcclIpAddress(COMM_LOOPBACK_IP)), profilingInitiated_(false), callbackThreadId_(INVALID_U64),
157 412 : role_(SERVER_ROLE_SOCKET),
158 413 : isHostUseDevNic_(false),
159 318132 : isAllRankSamePlane_(false), serverNum_(0), moduleNum_(0)
160 : {
161 413 : zeroCopyAclGraph_.reset(new (std::nothrow) ZeroCopyAclGraph());
162 413 : if (zeroCopyAclGraph_ == nullptr)
163 : {
164 0 : HCCL_ERROR("new ZeroCopyAclGraph failed!");
165 : }
166 413 : commConfig_ = commConfig;
167 413 : dpuManager_.reset(new (std::nothrow) DpuManager());
168 413 : if (dpuManager_ == nullptr) {
169 0 : HCCL_ERROR("new DpuManager failed!");
170 : }
171 413 : }
172 :
173 7754 : HcclCommunicator::~HcclCommunicator()
174 : {
175 804 : HCCL_DEBUG("Enter ~HcclCommunicator.");
176 :
177 804 : DeinitZeroCopyMemoryAgent(true);
178 804 : if (!isInvalidComm_) {
179 804 : (void)DestroyAicpuComm();
180 804 : (void)UnRegisterBackGroundThread();
181 : } else {
182 0 : HCCL_WARNING("The comm[%s] is invalid in snapshot, rank[%u]. deviceLogicId[%u]. "
183 : "There is no aicpu comm in device, skip aicpu comm destroy in destructor.",
184 : identifier_.c_str(), userRank_, deviceLogicId_);
185 : }
186 :
187 804 : if (dpuManager_ != nullptr) {
188 804 : (void)dpuManager_->DeInitDpuKernel();
189 804 : dpuManager_ = nullptr;
190 : }
191 :
192 803 : UnRegisterToHeartBeat();
193 798 : DeleteOpInfoToHeartBeat();
194 802 : AlgWrap::GetInstance().UnregisterAlgCallBack(identifier_);
195 804 : DetectConnectionAnomalies::GetInstance(deviceLogicId_).Deinit();
196 804 : UnRegisterToCommConfiger();
197 804 : AclgraphCallback::GetInstance().CleanCaptureRes(this);
198 :
199 804 : if (zeroCopyAclGraph_ != nullptr) {
200 804 : zeroCopyAclGraph_ = nullptr;
201 : }
202 :
203 803 : if (implAlg_ != nullptr) {
204 518 : implAlg_ = nullptr;
205 : }
206 :
207 878 : for (auto &res : resMap_) {
208 74 : DestroyAlgResource(res.second);
209 : }
210 :
211 804 : if (releaseChannel_ != nullptr) {
212 403 : releaseChannel_();
213 : }
214 :
215 803 : if (opRetryManager_ != nullptr) {
216 0 : OpRetryManager::DeleteLinkInfoByIdentifier(deviceLogicId_, identifier_);
217 0 : opRetryManager_->UnRegisterOpRetryManager(identifier_);
218 0 : opRetryManager_ = nullptr;
219 : }
220 :
221 803 : if (IsEnableBackupLink()) {
222 0 : if (g_enableBackupLinkCommCount.load() == 0) {
223 0 : HCCL_ERROR("[Destroy] g_enableBackupLinkCommCount is 0");
224 : } else {
225 0 : g_enableBackupLinkCommCount--;
226 : }
227 : }
228 :
229 803 : resMap_.clear();
230 803 : deviceResOrigMem_.clear();
231 803 : hostResMap_.clear();
232 802 : tagCommInfo_.clear();
233 802 : tagWorkSpaceMem_.clear();
234 801 : tagStreamInfo_.clear();
235 :
236 800 : if (opRetryStreamPtr_ != nullptr) {
237 517 : opRetryStreamPtr_->clear();
238 518 : opRetryStreamPtr_ = nullptr;
239 : }
240 :
241 802 : OrderLaunch::GetInstance(deviceLogicId_).UnRegisterOrderLaunch(identifier_);
242 2408 : for (u32 i = 0; i < AICPU_LOCAL_EVENT_SIZE; ++i) {
243 1605 : if (localAicpuOpEvent_[i] != nullptr) {
244 0 : (void)hrtEventDestroy(localAicpuOpEvent_[i]);
245 0 : localAicpuOpEvent_[i] = nullptr;
246 : }
247 : }
248 :
249 803 : (void)UnRegistTaskExceptionHandler();
250 802 : for (auto streamId : aicpuStreamIds_) {
251 0 : UnregisterGetAicpuTaskExceptionCallBack(streamId, deviceLogicId_);
252 : }
253 803 : aicpuStreamIds_.clear();
254 799 : kfcControlTransferH2D_ = nullptr;
255 803 : kfcStatusTransferD2H_ = nullptr;
256 803 : customControlTransferH2D_ = nullptr;
257 803 : customStatusTransferD2H_ = nullptr;
258 :
259 803 : oneSideService_ = nullptr;
260 803 : if (isOneSidedServiceNetDevCtxInited) {
261 0 : DeInitOneSidedServiceNetDevCtx();
262 : }
263 :
264 : /* 网络资源销毁 */
265 802 : DestroyNetworkResources();
266 804 : notifyPool_ = nullptr;
267 804 : queueNotifyManager_ = nullptr;
268 : /* driver关联资源释放 */
269 804 : if (drvInit_){
270 305 : if (DisablePreResource() != HCCL_SUCCESS) {
271 0 : HCCL_WARNING("driver resource is not released successfully");
272 : }
273 : }
274 :
275 804 : if (isExecuteProfilingInit_) {
276 488 : (void)DeinitProfiling();
277 : }
278 :
279 804 : if (OpExeCounter::GetInstance(deviceLogicId_).DeInitCounter() != HCCL_SUCCESS) {
280 0 : HCCL_WARNING("op exec counter resource free failed");
281 : }
282 :
283 : /* 销毁当前trace句柄 */
284 804 : if (opBaseAtraceInfo_ != nullptr) {
285 521 : opBaseAtraceInfo_->DeInit();
286 521 : opBaseAtraceInfo_ = nullptr;
287 : }
288 :
289 804 : ReleaseWorkSpacebuffer();
290 803 : ReleaseCommContextbuffer();
291 :
292 7232 : for (u32 i = 0; i < AICPU_LOCAL_NOTIFY_SIZE; i++) {
293 6429 : if (localAiCpuOpNotify_[i]) {
294 0 : HcclResult ret = localAiCpuOpNotify_[i]->Destroy();
295 0 : localAiCpuOpNotify_[i] = nullptr;
296 0 : if (ret != RT_ERROR_NONE) {
297 0 : HCCL_ERROR("[Destroy][AicpuNotify]errNo[0x%016llx] rt notify destroy fail, "
298 : "aicpuOpNotify[%u] return[%d].",
299 : HCCL_ERROR_CODE(HCCL_E_RUNTIME), i, ret);
300 : }
301 : }
302 : }
303 :
304 804 : while (!aiCpuNoIpcEvnet_.empty()) {
305 1 : rtEvent_t eventInfo = aiCpuNoIpcEvnet_.back();
306 1 : HcclResult ret = hrtEventDestroy(eventInfo);
307 1 : if (ret != HCCL_SUCCESS) {
308 0 : HCCL_ERROR("[Destroy][AicpuNoIpcEvnet]errNo[0x%016llx] rt event destroy fail, "
309 : "return[%d].",
310 : HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret);
311 : }
312 1 : aiCpuNoIpcEvnet_.pop_back();
313 : }
314 :
315 804 : UnloadAICPUKernel();
316 802 : UnloadCustomKernel();
317 804 : if (dispatcher_ != nullptr) {
318 521 : HcclDispatcherDestroy(dispatcher_);
319 518 : dispatcher_ = nullptr;
320 : }
321 801 : if (dispatcherCtx_ != nullptr) {
322 518 : DestroyDispatcherCtx(dispatcherCtx_, identifier_.c_str());
323 521 : dispatcherCtx_ = nullptr;
324 : }
325 804 : if (vDispatcher_ != nullptr) {
326 521 : HcclDispatcherDestroy(vDispatcher_);
327 521 : vDispatcher_ = nullptr;
328 : }
329 804 : if (deviceType_ == DevType::DEV_TYPE_910B || deviceType_ == DevType::DEV_TYPE_910_93){
330 212 : UnRegisterFromSnapshot();
331 : }
332 804 : HCCL_DEBUG("~HcclCommunicator success.");
333 9359 : }
334 :
335 520 : HcclResult HcclCommunicator::SaveTopoDesc(std::string &identifier)
336 : {
337 520 : CommTopo topoType = CommTopo::COMM_TOPO_RESERVED;
338 520 : CHK_RET(GetInstTopoTypeByNetLayer(0, &topoType)); // layer 0
339 :
340 520 : CommTopoDesc::GetInstance().SaveRankSize(identifier, userRankSize_);
341 520 : CommTopoDesc::GetInstance().SaveL0TopoType(identifier, topoType);
342 520 : return HCCL_SUCCESS;
343 : }
344 :
345 498 : HcclResult HcclCommunicator::Init(HcclCommParams ¶ms, const RankTable_t &rankTable)
346 : {
347 498 : CHK_RET(InitCommParams(params));
348 498 : CHK_RET(attrCollector_.Init(params, rankTable, commConfig_.GetConfigHcclAlgoMap()));
349 494 : CHK_RET(InitRankInfo(rankTable));
350 495 : CHK_RET(InitNetResource(rankTable));
351 488 : CHK_RET(InitDebug());
352 488 : CHK_RET(InitNotifyManager());
353 488 : CHK_RET(InitStreamManager());
354 487 : CHK_RET(InitProfiler());
355 487 : CHK_RET(InitDispatcher());
356 488 : CHK_RET(InitTransportManager());
357 488 : CHK_RET(InitCombinOpara());
358 488 : CHK_RET(RegisterRanksToDca());
359 : /*--------------加锁区--------------*/
360 488 : std::unique_lock<std::mutex> lock(g_hcomInitMutex);
361 488 : CHK_RET(RegistTaskExceptionHandler());
362 :
363 488 : attrCollector_.GenCollectiveId(params, rankTable);
364 488 : collectiveId_ = attrCollector_.GetCollectiveId();
365 :
366 : // 初始化参数(需要放置在ranktable解析之后)
367 488 : HcclResult ret = InitPara();
368 488 : CHK_PRT_RET(ret != HCCL_SUCCESS,
369 : HCCL_ERROR("[HcclCommunicator][Init]errNo[0x%016llx] collectiveid[%s] parameter initialization failed",
370 : HCCL_ERROR_CODE(ret), params.id.internal),
371 : ret);
372 488 : lock.unlock();
373 : /*--------------加锁区--------------*/
374 488 : if (deviceType_ == DevType::DEV_TYPE_910B || deviceType_ == DevType::DEV_TYPE_910_93){
375 196 : CHK_RET(RegisterKernel(deviceType_));
376 : }
377 488 : CHK_RET(LoadCustomKernel());
378 488 : CHK_RET(LoadAICPUKernel());
379 488 : CHK_RET(InitHDCommunicate());
380 488 : CHK_RET(InitOpRetry());
381 488 : CHK_RET(InitOpResPara());
382 :
383 488 : CHK_RET(InitOneSidedService(rankTable));
384 488 : CHK_RET(OrderLaunch::GetInstance(deviceLogicId_).RegisterOrderLaunch(identifier_));
385 488 : HcclTopoAttr topoAttr;
386 488 : attrCollector_.GetTopoAttr(topoAttr);
387 488 : CHK_RET(rankGraph_.Init(rankTable, topoAttr));
388 488 : CHK_RET(SaveTopoDesc(params.identifier));
389 488 : CHK_RET(RegisterToSnapshot());
390 488 : CHK_RET(InitSymmetricMemory());
391 :
392 488 : CHK_RET(InitMyRankConnectMode(params, rankTable));
393 488 : if (dpuManager_ != nullptr && myRankConnectMode_) { /* 当前只有host nic--device nic使用 */
394 0 : CHK_RET(dpuManager_->Init(identifier_, deviceLogicId_));
395 : }
396 :
397 488 : return HCCL_SUCCESS;
398 488 : }
399 :
400 33 : HcclResult HcclCommunicator::Init(HcclCommParams ¶ms, const std::vector<RankInfo> &rankList,
401 : WorldGroupInfo &groupCommonData)
402 : {
403 33 : CHK_RET(InitCommParams(params));
404 33 : CHK_RET(attrCollector_.Init(params, rankList, groupCommonData, commConfig_.GetConfigHcclAlgoMap()));
405 33 : CHK_RET(InitRankInfoSubGroup(groupCommonData));
406 33 : CHK_RET(InitDebugSubGroup());
407 33 : CHK_RET(InitNotifyManager());
408 33 : CHK_RET(InitDispatcher());
409 33 : CHK_RET(InitStreamManager());
410 33 : CHK_RET(InitRaResource());
411 33 : CHK_RET(InitTransportManager());
412 33 : CHK_RET(InitHcclAlg());
413 32 : CHK_RET(LoadCustomKernel());
414 32 : CHK_RET(LoadAICPUKernel());
415 32 : CHK_RET(InitHDCommunicate());
416 32 : CHK_RET(InitOpRetry());
417 32 : CHK_RET(InitOpResPara());
418 32 : CHK_RET(RegisterRanksToDca());
419 32 : CHK_RET(OrderLaunch::GetInstance(deviceLogicId_).RegisterOrderLaunch(identifier_));
420 32 : HcclTopoAttr topoAttr;
421 32 : attrCollector_.GetTopoAttr(topoAttr);
422 32 : CHK_RET(rankGraph_.Init(topoAttr));
423 32 : CHK_RET(SaveTopoDesc(params.identifier));
424 32 : CHK_RET(RegisterToSnapshot());
425 32 : CHK_RET(InitSymmetricMemory());
426 32 : return HCCL_SUCCESS;
427 32 : }
428 :
429 520 : HcclResult HcclCommunicator::LoadAICPUKernel(void)
430 : {
431 520 : if (binHandle_ == nullptr) {
432 520 : std::string jsonPath;
433 520 : CHK_RET(GetKernelFilePath(jsonPath));
434 520 : jsonPath += "ccl_kernel.json";
435 520 : HcclResult ret = LoadBinaryFromFile(jsonPath.c_str(), ACL_RT_BINARY_LOAD_OPT_CPU_KERNEL_MODE, 0,
436 520 : binHandle_);
437 520 : CHK_PRT_RET(ret != HCCL_SUCCESS,
438 : HCCL_ERROR("[LoadAICPUKernel]errNo[0x%016llx]load aicpu file fail, path[%s] optionType[%u]"
439 : "cpuKernelMode[%u].", ret, jsonPath.c_str(), ACL_RT_BINARY_LOAD_OPT_CPU_KERNEL_MODE, 0), ret);
440 520 : }
441 520 : return HCCL_SUCCESS;
442 : }
443 :
444 804 : void HcclCommunicator::UnloadAICPUKernel(void)
445 : {
446 804 : if (binHandle_ != nullptr) {
447 3 : aclError aclRet = aclrtBinaryUnLoad(binHandle_);
448 3 : if (aclRet != ACL_SUCCESS) {
449 0 : HCCL_ERROR("[UnloadAICPUKernel]errNo[0x%016llx] unload binary from binHandel[%p] error.",
450 : aclRet, binHandle_);
451 : }
452 3 : binHandle_ = nullptr;
453 : }
454 804 : return;
455 : }
456 :
457 520 : HcclResult HcclCommunicator::LoadCustomKernel(void)
458 : {
459 : // 加载自定义算子
460 : // 请勿删除,该函数为用户自定义算子时使用,应加载句柄
461 : // 读取customEnable环境变量,开启了就执行
462 520 : std::string jsonPath;
463 520 : CHK_RET(GetCustomKernelFilePath(jsonPath));
464 520 : jsonPath += "libaicpu_custom.json";
465 520 : CHK_RET(LoadCustomFile(jsonPath.c_str(), ACL_RT_BINARY_LOAD_OPT_CPU_KERNEL_MODE, 1, binHandle_));
466 520 : return HCCL_SUCCESS;
467 520 : }
468 :
469 804 : void HcclCommunicator::UnloadCustomKernel(void)
470 : {
471 : // 卸载自定义算子
472 : // 请勿删除,该函数为用户自定义算子时使用,应释放句柄:UnloadBinary(binCustomHandle_);
473 804 : return;
474 : }
475 :
476 488 : HcclResult HcclCommunicator::InitOneSidedService(const RankTable_t &rankTable)
477 : {
478 488 : EXCEPTION_CATCH((oneSideService_ = std::make_unique<HcclOneSidedService>(socketManager_, notifyPool_, commConfig_)),
479 : return HCCL_E_INTERNAL);
480 488 : hcclRankLinkInfo_.userRank = userRank_;
481 488 : hcclRankLinkInfo_.devicePhyId = devicePhyId_;
482 :
483 488 : if (devIpAddr_.empty()) {
484 0 : HCCL_ERROR("[%s] device ip is invalid, please set device ip first.", __func__);
485 0 : return HCCL_E_NOT_FOUND;
486 : }
487 488 : hcclRankLinkInfo_.ip = devIpAddr_[0];
488 488 : if (nicRanksPort_.size() <= userRank_) {
489 0 : HCCL_ERROR("[%s] userRank_[%u] port is invalid, please set port first", __func__, userRank_);
490 0 : return HCCL_E_NOT_FOUND;
491 : }
492 488 : hcclRankLinkInfo_.port = nicRanksPort_[userRank_];
493 488 : hcclRankLinkInfo_.socketsPerLink = 1;
494 488 : HCCL_DEBUG("[%s]hcclRankLinkInfo_ userRank[%u], devicePhyId[%u], ip[%s], port[%u]", __func__,
495 : hcclRankLinkInfo_.userRank, hcclRankLinkInfo_.devicePhyId, hcclRankLinkInfo_.ip.GetReadableIP(),
496 : hcclRankLinkInfo_.port);
497 488 : CHK_RET(oneSideService_->Config(dispatcher_, hcclRankLinkInfo_, &rankTable, identifier_, isStandardCard_, enableP2PRankIds_));
498 488 : return HCCL_SUCCESS;
499 : }
500 :
501 0 : HcclResult HcclCommunicator::InitOneSidedServiceNetDevCtx(u32 remoteRankId)
502 : {
503 0 : if (nicDeployment_ != NICDeployment::NIC_DEPLOYMENT_DEVICE) {
504 : // 单边操作当前只支持Device网卡,不支持host
505 0 : HCCL_ERROR("[%s]nicDeployment_[%d], userRankSize_[%u], do not support oneSidedService.",
506 : __func__, nicDeployment_, userRankSize_);
507 0 : return HCCL_E_INTERNAL;
508 : }
509 :
510 0 : std::string localServerId = serverId_;
511 0 : std::string localSuperPodId = superPodId_;
512 0 : std::string remoteServerId = rankInfoList_.at(remoteRankId).serverId;
513 0 : std::string remoteSuperPodId = rankInfoList_.at(remoteRankId).superPodId;
514 0 : u32 intraRoceSwitch = GetExternalInputIntraRoceSwitch();
515 0 : bool useRdma = false;
516 0 : if (intraRoceSwitch ||
517 0 : (!useSuperPodMode_ && localServerId != remoteServerId) ||
518 0 : (localSuperPodId != remoteSuperPodId)) {
519 : // 1. 初始化网口
520 0 : CHK_RET(InitNic());
521 0 : isOneSidedServiceNicInited = true;
522 :
523 : // 2. 单边操作SetNetDevCtx, RDMA
524 0 : if (netDevCtxMap_.find(devIpAddr_[0]) == netDevCtxMap_.end()) {
525 0 : HCCL_ERROR("[%s] nicDeployment_[%d], device nic init fail, please check", __func__, nicDeployment_);
526 0 : return HCCL_E_NOT_FOUND;
527 : }
528 0 : useRdma = true;
529 0 : oneSideService_->SetNetDevCtx(netDevCtxMap_[devIpAddr_[0]], useRdma);
530 0 : HCCL_INFO("[%s]init device Nic for oneSidedService success.", __func__);
531 : }else {
532 : // 单边操作SetNetDevCtx, IPC
533 0 : oneSideService_->SetNetDevCtx(netDevCtxMap_[localVnicIp_], useRdma);
534 0 : HCCL_INFO("[%s]init vNic for oneSidedService success.", __func__);
535 : }
536 0 : isOneSidedServiceNetDevCtxInited = true;
537 0 : HCCL_DEBUG("[%s]nicDeployment_[%d], intraRoceSwitch[%u]", __func__, nicDeployment_, intraRoceSwitch);
538 0 : return HCCL_SUCCESS;
539 0 : }
540 :
541 0 : HcclResult HcclCommunicator::DeInitOneSidedServiceNetDevCtx()
542 : {
543 0 : if (nicDeployment_ != NICDeployment::NIC_DEPLOYMENT_DEVICE) {
544 : // 单边操作当前只支持Device网卡,不支持host
545 0 : HCCL_ERROR("[%s]nicDeployment_[%d], userRankSize_[%u], do not support oneSidedService.",
546 : __func__, nicDeployment_, userRankSize_);
547 0 : return HCCL_E_INTERNAL;
548 : }
549 0 : if (isOneSidedServiceNicStartListen_) {
550 0 : socketManager_->DestroySockets();
551 0 : u32 port = GetLocalNicPort(NicType::DEVICE_NIC_TYPE);
552 0 : CHK_RET(socketManager_->ServerDeInit(onesidedServiceNicIpAddr_, port));
553 0 : isOneSidedServiceNicStartListen_ = false;
554 0 : HCCL_INFO("[HcclCommunicator][%s] DeInit socket server success.tag[%s].", __func__, identifier_.c_str());
555 : }
556 0 : u32 intraRoceSwitch = GetExternalInputIntraRoceSwitch();
557 0 : if (isOneSidedServiceNicInited) {
558 : // 1. close sockets
559 0 : if (raResourceInit_) {
560 0 : socketManager_->DestroySockets();
561 : }
562 : // 2. 去初始化网口
563 0 : CHK_RET(DeinitNic());
564 0 : isOneSidedServiceNicInited = false;
565 0 : HCCL_INFO("[%s]Deinit device Nic for oneSidedService success.", __func__);
566 : }
567 0 : isOneSidedServiceNetDevCtxInited = false;
568 0 : HCCL_DEBUG("[%s]nicDeployment_[%d], intraRoceSwitch[%u]", __func__, nicDeployment_, intraRoceSwitch);
569 0 : return HCCL_SUCCESS;
570 : }
571 :
572 0 : HcclResult HcclCommunicator::GetOneSidedService(IHcclOneSidedService **service)
573 : {
574 0 : *service = oneSideService_.get();
575 0 : return HCCL_SUCCESS;
576 : }
577 :
578 0 : HcclResult HcclCommunicator::OneSidedServiceStartListen(NicType nicType, HcclNetDevCtx netDevCtx)
579 : {
580 0 : HCCL_INFO("[HcclCommunicator][%s] Start prepare netDevCtx.", __func__);
581 0 : u32 port = GetLocalNicPort(nicType);
582 0 : CHK_RET(socketManager_->ServerInit(netDevCtx, port));
583 0 : if (nicType == NicType::DEVICE_NIC_TYPE) {
584 0 : CHK_RET(HcclNetDevGetLocalIp(netDevCtx, onesidedServiceNicIpAddr_));
585 0 : isOneSidedServiceNicStartListen_ = true;
586 : }
587 0 : isOneSidedServiceNetDevCtxInited = true;
588 0 : HCCL_INFO("[HcclCommunicator][%s] netDevCtx[%p] port[%u] server init success.", __func__, netDevCtx, port);
589 0 : return HCCL_SUCCESS;
590 : }
591 :
592 0 : HcclResult HcclCommunicator::GetOneSidedServiceDevIpAndPort(NicType nicType, HcclIpAddress &ipAddress, u32& port)
593 : {
594 0 : if (nicDeployment_ != NICDeployment::NIC_DEPLOYMENT_DEVICE) {
595 : // 单边操作当前只支持Device网卡,不支持host
596 0 : HCCL_ERROR("[%s]nicDeployment_[%d], userRankSize_[%u], do not support oneSidedService.",
597 : __func__, nicDeployment_, userRankSize_);
598 0 : return HCCL_E_INTERNAL;
599 : }
600 0 : port = GetLocalNicPort(nicType);
601 0 : if (nicType == NicType::VNIC_TYPE) {
602 0 : ipAddress = localVnicIp_;
603 0 : HCCL_INFO("[GetOneSidedServiceDevIpAddr] vnic ipAddress[%s] get success.", ipAddress.GetReadableAddress());
604 0 : return HCCL_SUCCESS;
605 0 : }else if (nicType == NicType::DEVICE_NIC_TYPE) {
606 0 : u32 nicNum = devIpAddr_.size();
607 0 : for (u32 i = 0; i < nicNum; i++) {
608 0 : if (devIpAddr_[i].IsInvalid()) {
609 0 : HCCL_INFO("[GetOneSidedServiceDevIpAddr]nic num[%u] deviceip is invalid, total nicNum[%u]", i, nicNum);
610 0 : continue;
611 : }
612 0 : ipAddress = devIpAddr_[i];
613 0 : HCCL_INFO("[GetOneSidedServiceDevIpAddr] nic ipAddress[%s] get success.", ipAddress.GetReadableAddress());
614 0 : return HCCL_SUCCESS;
615 : }
616 : }
617 0 : HCCL_ERROR("[HcclCommunicator][%s] ipAddress get fail. tag[%s]", __func__, identifier_.c_str());
618 0 : return HCCL_E_NOT_FOUND;
619 : }
620 :
621 0 : HcclResult HcclCommunicator::DeinitOneSidedService()
622 : {
623 0 : if (oneSideService_ != nullptr) {
624 0 : CHK_RET(oneSideService_->DeInit());
625 : }
626 0 : return HCCL_SUCCESS;
627 : }
628 :
629 81 : bool HcclCommunicator::IsSupportSymmetricMemory(HcclCMDType opType, OpParam &opParam)
630 : {
631 81 : CHK_PRT_RET(symmetricMemory_ == nullptr, HCCL_DEBUG("symmetricMemory_ is a nullptr"), false);
632 14 : HCCL_INFO("[%s] aicpuUnfold[%d], workflowMode[%d], deviceType[%d], "
633 : "deviceNumPerAggregation_[%d], multiModuleDiffDeviceNumMode_[%d], tag[%s].",
634 : __func__, opParam.aicpuUnfoldMode, GetWorkflowMode(), deviceType_,
635 : deviceNumPerAggregation_, multiModuleDiffDeviceNumMode_, opParam.tag.c_str());
636 :
637 : // 目前只支持allgather, allreduce, reducescatter
638 14 : CHK_PRT_RET(opType != HcclCMDType::HCCL_CMD_ALLGATHER &&
639 : opType != HcclCMDType::HCCL_CMD_ALLREDUCE &&
640 : opType != HcclCMDType::HCCL_CMD_ALLTOALL &&
641 : opType != HcclCMDType::HCCL_CMD_REDUCE_SCATTER,
642 : HCCL_INFO("[%s] opType[%d] not support symmetric memory",
643 : __func__, opType),
644 : false);
645 :
646 : // 只支持aicpu展开、单算子模式、910_93芯片
647 14 : CHK_PRT_RET(!opParam.aicpuUnfoldMode,
648 : HCCL_INFO("[%s] aicpuUnfold:%d not support symmetric memory", __func__, opParam.aicpuUnfoldMode), false);
649 4 : CHK_PRT_RET(GetWorkflowMode() != HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE,
650 : HCCL_INFO("[%s] workflowMode:%d not support symmetric memory", __func__, GetWorkflowMode()), false);
651 3 : CHK_PRT_RET(deviceType_ != DevType::DEV_TYPE_910_93,
652 : HCCL_INFO("[%s] deviceType:%d not support symmetric memory", __func__, deviceType_), false);
653 3 : CHK_PRT_RET(superPodNum_ == 1 && serverNum_ > 1 && GetExternalInputInterHccsDisable(),
654 : HCCL_INFO("[%s] mutilSever use roce not support symmetric memory", __func__), false);
655 :
656 : // 判断拓扑逻辑是否支持symmetric memory
657 : // 每个节点只有一张卡或节点间非对称场景不支持对称内存
658 3 : CHK_PRT_RET(deviceNumPerAggregation_ == 1 || multiModuleDiffDeviceNumMode_,
659 : HCCL_INFO("[%s] deviceNumPerAggregation[%u], multiModuleDiffDeviceNumMode_[%d] not support symmetric memory",
660 : __func__, deviceNumPerAggregation_, multiModuleDiffDeviceNumMode_),
661 : false);
662 :
663 : // 判断输入输出地址是否都注册为对称内存
664 2 : HcclResult ret = symmetricMemory_->FindSymmetricWindow(opParam.inputPtr, opParam.inputSize, &opParam.inputSymWindow, &opParam.inputOffset);
665 2 : CHK_PRT_RET(ret != HCCL_SUCCESS || opParam.inputSymWindow == nullptr,
666 : HCCL_INFO("[%s] input[%p] size[%llu] is not support symmetric memory", __func__, opParam.inputPtr, opParam.inputSize), false);
667 1 : ret = symmetricMemory_->FindSymmetricWindow(opParam.outputPtr, opParam.outputSize, &opParam.outputSymWindow, &opParam.outputOffset);
668 1 : CHK_PRT_RET(ret != HCCL_SUCCESS || opParam.outputSymWindow == nullptr,
669 : HCCL_INFO("[%s] output[%p] size[%llu] is not support symmetric memory", __func__, opParam.outputPtr, opParam.outputSize), false);
670 :
671 1 : HCCL_INFO("[HcclCommunicator][IsSupportSymmetricMemory] opParam.inputPtr[%p], inputOffset[%llu], inputSymWindow[%p]",
672 : opParam.inputPtr, opParam.inputOffset, opParam.inputSymWindow);
673 1 : HCCL_INFO("[HcclCommunicator][IsSupportSymmetricMemory] opParam.outputPtr[%p], outputOffset[%llu], outputSymWindow[%p]",
674 : opParam.outputPtr, opParam.outputOffset, opParam.outputSymWindow);
675 :
676 1 : return true;
677 : }
678 :
679 74 : bool HcclCommunicator::IsSupportZeroCopy(const OpParam &opParam)
680 : {
681 74 : HCCL_INFO("[%s] aicpuUnfold[%d], workflowMode[%d], deviceType[%d], "
682 : "deviceNumPerAggregation_[%d], multiModuleDiffDeviceNumMode_[%d], tag[%s].",
683 : __func__, opParam.aicpuUnfoldMode, GetWorkflowMode(), deviceType_,
684 : deviceNumPerAggregation_, multiModuleDiffDeviceNumMode_, opParam.tag.c_str());
685 :
686 : // 只支持aicpu展开、非重执行、单算子模式、910_93芯片
687 73 : CHK_PRT_RET(!opParam.aicpuUnfoldMode,
688 : HCCL_INFO("[%s] aicpuUnfold:%d not support zero copy", __func__, opParam.aicpuUnfoldMode), false);
689 4 : CHK_PRT_RET(GetWorkflowMode() != HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE,
690 : HCCL_INFO("[%s] workflowMode:%d not support zero copy", __func__, GetWorkflowMode()), false);
691 0 : CHK_PRT_RET(deviceType_ != DevType::DEV_TYPE_910_93,
692 : HCCL_INFO("[%s] deviceType:%d not support zero copy", __func__, deviceType_), false);
693 :
694 : // 判断拓扑逻辑是否支持zero copy
695 : // 每个节点只有一张卡或节点间非对称场景不支持零拷贝
696 0 : CHK_PRT_RET(deviceNumPerAggregation_ == 1 || multiModuleDiffDeviceNumMode_,
697 : HCCL_INFO("[%s] deviceNumPerAggregation[%u], multiModuleDiffDeviceNumMode_[%d] not support zero copy",
698 : __func__, deviceNumPerAggregation_, multiModuleDiffDeviceNumMode_),
699 : false);
700 :
701 : // 判断输入输出地址是否都是支持零Copy特性的
702 0 : CHK_PRT_RET(!ZeroCopyMemoryAgent::IsActivateCommMemoryAddr(opParam.inputPtr, opParam.inputSize),
703 : HCCL_INFO("[%s] input[%p] size[%llu] is not support zero copy", __func__, opParam.inputPtr, opParam.inputSize), false);
704 0 : CHK_PRT_RET(!ZeroCopyMemoryAgent::IsActivateCommMemoryAddr(opParam.outputPtr, opParam.outputSize),
705 : HCCL_INFO("[%s] output[%p] size[%llu] is not support zero copy", __func__, opParam.outputPtr, opParam.outputSize), false);
706 :
707 0 : return true;
708 : }
709 :
710 71 : HcclResult HcclCommunicator::PrepareZeroCopy(const std::string &algName, const AlgDesc &algDesc, OpParam &opParam)
711 : {
712 71 : if (!algDesc.isZeroCopy) {
713 71 : opParam.supportSymmetricMemory = false; // 当前对称内存与零拷贝算法绑定,对称内存使能关闭,确保aicpu侧不走对称内存分支
714 71 : HCCL_INFO("[HcclCommunicator][PrepareZeroCopy] algName[%s] not support zerocopy.", algName.c_str());
715 78 : return HCCL_SUCCESS;
716 : }
717 :
718 0 : if (opParam.supportSymmetricMemory) {
719 0 : HCCL_INFO("[HcclCommunicator][PrepareZeroCopy] algName[%s] symmetric memory is enabled, not use zerocopy.",
720 : algName.c_str());
721 0 : return HCCL_SUCCESS;
722 : }
723 : // ARS特性不支持零拷贝
724 0 : if ((opParam.opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER || opParam.opType == HcclCMDType::HCCL_CMD_ALLGATHER ||
725 0 : opParam.opType == HcclCMDType::HCCL_CMD_ALLREDUCE) && deviceType_ == DevType::DEV_TYPE_910_93 &&
726 0 : multiModuleDiffDeviceNumMode_ && !multiSuperPodDiffDeviceNumMode_){
727 0 : return HCCL_SUCCESS;
728 : }
729 :
730 : // 如果自己侧的共享内存没有申请,那么进行申请,并设置给transportManager,后续p2p建链时进行交换
731 0 : if (zeroCopyLocalBuffer_.ptr() == nullptr) {
732 0 : CHK_RET(DeviceMem::alloc(zeroCopyLocalBuffer_, ZERO_COPY_IPC_BUFFER_LENGTH));
733 0 : CHK_RET(hrtMemSet(zeroCopyLocalBuffer_.ptr(), zeroCopyLocalBuffer_.size(), zeroCopyLocalBuffer_.size()));
734 0 : zeroCopyIpcPtrs_[userRank_ % deviceNumPerAggregation_] = zeroCopyLocalBuffer_.ptr();
735 :
736 0 : HCCL_RUN_INFO("[HCCL_TRACE][PrepareZeroCopy]Create ZeroCopy buffer success. buffer ptr[%p] size[%llu]",
737 : zeroCopyLocalBuffer_.ptr(), zeroCopyLocalBuffer_.size());
738 : }
739 0 : opParam.isZeroCopy = true;
740 0 : HCCL_INFO("[HcclCommunicator][PrepareZeroCopy] success to use zero copy feature");
741 0 : return HCCL_SUCCESS;
742 : }
743 :
744 23 : HcclResult HcclCommunicator::UpdateZeroCopy(const OpParam &opParam, const AlgResourceResponse &algResource)
745 : {
746 23 : if (!opParam.isZeroCopy) {
747 23 : return HCCL_SUCCESS;
748 : }
749 :
750 : // 遍历所有transport,找出里面的p2p链路对应的对端地址
751 0 : for (auto &singleSubCommTransport : algResource.opTransportResponse[COMM_LEVEL0]) {
752 0 : for (u64 i = 0; i < singleSubCommTransport.links.size(); ++i) {
753 0 : LINK link = singleSubCommTransport.links[i];
754 0 : if (link == nullptr || !singleSubCommTransport.transportRequests[i].isValid) {
755 : // 无效或者不支持的链路
756 0 : continue;
757 : }
758 :
759 : // 在使能零拷贝场景,我们使用控制面内存做OpenIpc交换,因此这里取出input即可
760 0 : u32 remoteRank = link->GetRemoteRank();
761 :
762 0 : void *remotePtr = nullptr;
763 0 : CHK_RET(link->GetRemoteMem(UserMemType::INPUT_MEM, &remotePtr));
764 0 : CHK_PRT_RET(remotePtr == nullptr,
765 : HCCL_ERROR("[BuildZeroCopyParam] invalid remotePtr[%p]", remotePtr), HCCL_E_PARA);
766 0 : CHK_PRT_RET(zeroCopyIpcPtrs_[remoteRank % deviceNumPerAggregation_] != nullptr && zeroCopyIpcPtrs_[remoteRank % deviceNumPerAggregation_] != remotePtr,
767 : HCCL_ERROR("[BuildZeroCopyParam] zeroCopyIpcPtrs_[%u] is [%p] not equal to %p", remoteRank, zeroCopyIpcPtrs_[remoteRank % deviceNumPerAggregation_],
768 : remotePtr),
769 : HCCL_E_PARA);
770 :
771 0 : zeroCopyIpcPtrs_[remoteRank % deviceNumPerAggregation_] = remotePtr;
772 0 : }
773 : }
774 0 : return HCCL_SUCCESS;
775 : }
776 :
777 3 : HcclResult HcclCommunicator::BuildZeroCopyParam()
778 : {
779 : // 不支持ZeroCopy
780 3 : if (zeroCopyLocalBuffer_.ptr() == nullptr) {
781 1 : return HCCL_SUCCESS;
782 : }
783 :
784 66 : for (u32 i = 0; i < AICPU_ZERO_COPY_MAX_DEVICE_NUM_A3; ++i) {
785 64 : opResPara_.zeroCopyIpcPtrs[i] = reinterpret_cast<u64>(zeroCopyIpcPtrs_[i]);
786 : }
787 :
788 2 : for (u32 i = 0; i < rankInfoList_.size(); ++i) {
789 0 : opResPara_.zeroCopyDevicePhyId[i % deviceNumPerAggregation_] = rankInfoList_[i].devicePhyId;
790 : }
791 :
792 2 : CHK_RET(ZeroCopyMemoryAgent::GetRingBufferAddr(opResPara_.zeroCopyRingBuffer,
793 : opResPara_.zeroCopyHeadPtr, opResPara_.zeroCopyTailPtr));
794 1 : return HCCL_SUCCESS;
795 : }
796 :
797 531 : HcclResult HcclCommunicator::InitCommParams(HcclCommParams ¶ms)
798 : {
799 531 : commHandle_ = params.commHandle;
800 531 : userRank_ = params.rank;
801 531 : realUserRank_ = params.userRank;
802 531 : userRankSize_ = params.totalRanks;
803 531 : deviceLogicId_ = params.logicDevId;
804 531 : profilingOption_ = params.profilingOption;
805 531 : profilingInitiated_ = params.profilingInitiated;
806 531 : deviceType_ = params.deviceType;
807 531 : commWorkMode_ = params.commWorkMode;
808 531 : hcomGroupNicInit_ = params.hcomGroupNicInit;
809 531 : identifier_ = params.identifier;
810 531 : collectiveId_ = params.id.internal;
811 531 : ranktableCrc_ = params.ranktableCrc;
812 531 : commConnections_ = params.commConnections;
813 531 : commPortConfig_ = params.commPortConfig;
814 531 : cclBuffName_ = params.cclBuffName;
815 531 : isShareComm_ = !cclBuffName_.empty();
816 :
817 531 : HCCL_DEBUG(
818 : " userRank_: %u realUserRank_: %u userRankSize_: %u deviceLogicId_: %u deviceType_: %u commWorkMode_: %u.",
819 : userRank_,
820 : realUserRank_,
821 : userRankSize_,
822 : deviceLogicId_,
823 : deviceType_,
824 : commWorkMode_);
825 :
826 531 : return HCCL_SUCCESS;
827 : }
828 :
829 0 : bool HcclCommunicator::Is310PDuoCard()
830 : {
831 0 : return (Is310P3Common(isHaveCpuRank_, deviceType_) &&
832 0 : (pairLinkInfo_[static_cast<u32>(LinkTypeInServer::HCCS_TYPE)].size() == userRankSize_));
833 : }
834 :
835 : // 910B A+X 在RDMA未启用情况下,两模块间的device数目需要一致且两模块中使用的卡都在同一平面上
836 492 : HcclResult HcclCommunicator::CheckSingleServerComm(const std::vector<RankInfo_t> &rankList) const
837 : {
838 492 : if (serverNum_ == 1 && moduleNum_ == HCCL_MODULE_NUM_TWO && GetExternalInputIntraRoceSwitch() == 0 && !isStandardCard_) {
839 0 : std::vector<u32> devIdList0;
840 0 : std::vector<u32> devIdList1;
841 0 : for (RankInfo_t rankInfo : rankList){
842 0 : if (rankInfo.deviceInfo.devicePhyId == HOST_DEVICE_ID) {
843 0 : HCCL_ERROR("[Check][SingleServerComm]not support cpu rank");
844 0 : return HCCL_E_NOT_SUPPORT;
845 : }
846 0 : if (rankInfo.deviceInfo.devicePhyId < DEVICE_PER_MODULE) {
847 0 : devIdList0.push_back(rankInfo.deviceInfo.devicePhyId);
848 : } else {
849 0 : devIdList1.push_back(rankInfo.deviceInfo.devicePhyId);
850 : }
851 0 : }
852 0 : std::sort(devIdList0.begin(), devIdList0.end());
853 0 : std::sort(devIdList1.begin(), devIdList1.end());
854 :
855 0 : auto buildDeviceListStr = [](const std::vector<u32>& list) -> std::string {
856 0 : std:: string result;
857 0 : for(const auto& id : list) {
858 0 : if (!result.empty()) {
859 0 : result += " ";
860 : }
861 0 : result += std::to_string(id);
862 : }
863 0 : return result;
864 0 : };
865 :
866 0 : std::string devList0Str = buildDeviceListStr(devIdList0);
867 0 : std::string devList1Str = buildDeviceListStr(devIdList1);
868 :
869 0 : if (devIdList0.size() != devIdList1.size()) {
870 0 : std::string errormessage = "Device ID " + devList0Str + " in module 0 and device ID " + devList1Str + " in module 1 are not on the same plane.";
871 0 : RPT_INPUT_ERR(true, "EI0010", std::vector<std::string>({"reason"}),
872 : std::vector<std::string>({ errormessage }));
873 0 : HCCL_ERROR("[%s][%s]%s",
874 : LOG_KEYWORDS_INIT_CHANNEL.c_str(), LOG_KEYWORDS_TIMEOUT.c_str(), errormessage.c_str());
875 0 : return HCCL_E_NOT_SUPPORT;
876 0 : }
877 0 : for (size_t i = 0; i < devIdList0.size(); i++) {
878 0 : if (devIdList0[i] % DEVICE_PER_MODULE != devIdList1[i] % DEVICE_PER_MODULE) {
879 0 : std::string errormessage = "Device ID " + std::to_string(devIdList0[i]) + " in module 0 and device ID " + std::to_string(devIdList1[i]) + " in module 1 are not on the same plane.";
880 0 : RPT_INPUT_ERR(true, "EI0010", std::vector<std::string>({"reason"}),
881 : std::vector<std::string>({ errormessage }));
882 0 : HCCL_ERROR("[%s][%s]%s",
883 : LOG_KEYWORDS_INIT_CHANNEL.c_str(), LOG_KEYWORDS_TIMEOUT.c_str(), errormessage.c_str());
884 0 : return HCCL_E_NOT_SUPPORT;
885 0 : }
886 : }
887 0 : }
888 492 : return HCCL_SUCCESS;
889 0 : }
890 :
891 388 : HcclResult HcclCommunicator::CheckDataType(const HcclDataType dataType, bool needReduce)
892 : {
893 3406 : const vector<string> infoTitle({"ccl_op", "value", "parameter", "expect"});
894 405 : if (needReduce) {
895 184 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
896 0 : if ((dataType == HCCL_DATA_TYPE_INT64) || (dataType == HCCL_DATA_TYPE_BFP16)) {
897 0 : RPT_INPUT_ERR(true,
898 : "EI0003",
899 : infoTitle,
900 : vector<string>(
901 : {"CheckDataType", GetDataTypeEnumStr(dataType), "dataType", "HCCL_DATA_TYPE_INT8, HCCL_DATA_TYPE_INT16, HCCL_DATA_TYPE_INT32, "\
902 : "HCCL_DATA_TYPE_FP16, HCCL_DATA_TYPE_FP32"}));
903 0 : HCCL_ERROR("[%s][%s]errNo[0x%016llx] data type[%s] not supported, support range=[%s]",
904 : LOG_KEYWORDS_TASK_EXEC.c_str(),
905 : LOG_KEYWORDS_INVALID_ARGUMENT.c_str(),
906 : HCCL_ERROR_CODE(HCCL_E_NOT_SUPPORT),
907 : GetDataTypeEnumStr(dataType).c_str(),
908 : GetSupportDataType(needReduce).c_str());
909 0 : return HCCL_E_NOT_SUPPORT;
910 : }
911 : }
912 180 : if ((dataType == HCCL_DATA_TYPE_UINT64) ||
913 180 : (dataType == HCCL_DATA_TYPE_UINT8) || (dataType == HCCL_DATA_TYPE_UINT16) ||
914 180 : (dataType == HCCL_DATA_TYPE_UINT32) || (dataType == HCCL_DATA_TYPE_FP64) ||
915 : (dataType == HCCL_DATA_TYPE_RESERVED)) {
916 12 : RPT_INPUT_ERR(true,
917 : "EI0003",
918 : infoTitle,
919 : vector<string>(
920 : {"CheckDataType", GetDataTypeEnumStr(dataType), "dataType", "HCCL_DATA_TYPE_INT8, HCCL_DATA_TYPE_INT16, HCCL_DATA_TYPE_INT32, "\
921 : "HCCL_DATA_TYPE_FP16, HCCL_DATA_TYPE_FP32"}));
922 4 : HCCL_ERROR("[%s][%s]errNo[0x%016llx] data type[%s] not supported, support range=[%s]",
923 : LOG_KEYWORDS_TASK_EXEC.c_str(),
924 : LOG_KEYWORDS_INVALID_ARGUMENT.c_str(),
925 : HCCL_ERROR_CODE(HCCL_E_NOT_SUPPORT),
926 : GetDataTypeEnumStr(dataType).c_str(),
927 : GetSupportDataType(needReduce).c_str());
928 1 : return HCCL_E_NOT_SUPPORT;
929 : }
930 : } else {
931 442 : if ((dataType >= HCCL_DATA_TYPE_RESERVED) || (dataType < HCCL_DATA_TYPE_INT8) ||
932 221 : (Is310P3Common(isHaveCpuRank_, deviceType_) && dataType == HCCL_DATA_TYPE_BFP16)) {
933 0 : RPT_INPUT_ERR(true,
934 : "EI0003",
935 : infoTitle,
936 : vector<string>(
937 : {"CheckDataType", GetDataTypeEnumStr(dataType), "dataType", "HCCL_DATA_TYPE_INT8, HCCL_DATA_TYPE_INT16, HCCL_DATA_TYPE_INT32, "\
938 : "HCCL_DATA_TYPE_FP16, HCCL_DATA_TYPE_FP32, HCCL_DATA_TYPE_UINT8, HCCL_DATA_TYPE_UINT16, HCCL_DATA_TYPE_UINT32"}));
939 0 : HCCL_ERROR("[%s][%s]errNo[0x%016llx] data type[%s] not supported, support range=[%s]",
940 : LOG_KEYWORDS_TASK_EXEC.c_str(),
941 : LOG_KEYWORDS_INVALID_ARGUMENT.c_str(),
942 : HCCL_ERROR_CODE(HCCL_E_NOT_SUPPORT),
943 : GetDataTypeEnumStr(dataType).c_str(),
944 : GetSupportDataType(needReduce).c_str());
945 0 : return HCCL_E_NOT_SUPPORT;
946 : }
947 : }
948 397 : return HCCL_SUCCESS;
949 400 : }
950 :
951 0 : HcclResult HcclCommunicator::InitZeroCopyMemoryAgent()
952 : {
953 0 : CHK_PRT_RET(zeroCopyMemoryAgent_ != nullptr,
954 : HCCL_ERROR("[HcclCommunicator][InitZeroCopyMemoryAgent] ipc memory agent has init"), HCCL_E_INTERNAL);
955 :
956 : // 获取节点内的ranktable
957 0 : std::vector<std::vector<std::vector<RankInfo>>> commPlaneVector;
958 0 : CHK_SMART_PTR_NULL(implAlg_);
959 0 : implAlg_->GetCommPlaneVector(commPlaneVector);
960 0 : rankInfoListIntraServer_ = commPlaneVector[COMM_LEVEL0][COMM_INDEX_0];
961 0 : zeroCopyMemoryAgent_.reset(static_cast<ZeroCopyMemoryAgent *>(new (std::nothrow) ZeroCopyMemoryAgent(socketManager_, devicePhyId_,
962 0 : deviceLogicId_, localVnicIp_, rankInfoListIntraServer_, userRank_, useSuperPodMode_, identifier_)));
963 0 : CHK_PTR_NULL(zeroCopyMemoryAgent_);
964 0 : CHK_RET(zeroCopyMemoryAgent_->Init());
965 0 : return HCCL_SUCCESS;
966 0 : }
967 :
968 1038 : HcclResult HcclCommunicator::DeinitZeroCopyMemoryAgent(bool inDestructor)
969 : {
970 1038 : if (zeroCopyMemoryAgent_ != nullptr) {
971 0 : if (!inDestructor && zeroCopyMemoryAgent_->IsResumed()) {
972 : // 析构函数释放场景不做barrier close
973 0 : CHK_RET(zeroCopyMemoryAgent_->BarrierClose());
974 : }
975 0 : CHK_RET(zeroCopyMemoryAgent_->DeInit());
976 0 : zeroCopyMemoryAgent_ = nullptr;
977 : }
978 1038 : return HCCL_SUCCESS;
979 : }
980 :
981 77 : u8 HcclCommunicator::GetConfigAclGraphZeroCopyEnable()
982 : {
983 77 : return commConfig_.GetConfigAclGraphZeroCopyEnable();
984 : }
985 :
986 61 : HcclResult HcclCommunicator::ClearResMap(const std::string &tag, bool &findTag, bool aclGraphDestroyCbk)
987 : {
988 61 : auto resIter = resMap_.find(tag);
989 60 : if (resIter != resMap_.end()) {
990 12 : findTag = true;
991 12 : DestroyAlgResource(resIter->second, aclGraphDestroyCbk);
992 12 : CHK_RET(StreamActiveManager::GetInstance(deviceLogicId_).StreamsUnactive(resIter->second.slaveStreams));
993 12 : resMap_.erase(resIter);
994 12 : HCCL_INFO("[%s] clear resMap[%s]", __func__, tag.c_str());
995 : }
996 59 : return HCCL_SUCCESS;
997 : }
998 :
999 4 : HcclResult HcclCommunicator::ClearAclgraphHostLinks(const std::unordered_set<std::string> &tags)
1000 : {
1001 4 : std::lock_guard<std::mutex> lock(commResMutex_);
1002 8 : for (const auto &tag : tags) {
1003 4 : auto it = tagsRequiringHostCleanup_.find(tag);
1004 4 : if (it == tagsRequiringHostCleanup_.end()) {
1005 1 : continue;
1006 : }
1007 4 : for (auto &rankIt : rankTagRemoteRes_) {
1008 1 : auto tagIt = rankIt.second.find(tag);
1009 1 : if (tagIt == rankIt.second.end()) {
1010 0 : continue;
1011 : }
1012 1 : HccltagRemoteResV2 *hostPtr = tagIt->second.tagRemoteResPtr;
1013 1 : if (hostPtr != nullptr) {
1014 : // 顺序敏感: ListCommonRemove 必须先于 erase shared_ptr,
1015 : // 否则 hostPtr 成野指针, ListCommonRemove 访问 segfault。
1016 1 : ListCommonRemove(&hostPtr->nextTagRes);
1017 1 : for (auto vIt = hostMemVec_.begin(); vIt != hostMemVec_.end(); ++vIt) {
1018 1 : if (*vIt && (*vIt)->ptr() == hostPtr) {
1019 1 : size_t idx = static_cast<size_t>(vIt - hostMemVec_.begin());
1020 1 : if (idx < deviceMemVec_.size()) {
1021 1 : deviceMemVec_.erase(deviceMemVec_.begin() + idx);
1022 : }
1023 1 : hostMemVec_.erase(vIt);
1024 1 : break;
1025 : }
1026 : }
1027 : }
1028 1 : rankIt.second.erase(tagIt);
1029 : }
1030 3 : ibverbsLocalNotify_.erase(tag);
1031 3 : ibverbsRemoteNotify_.erase(tag);
1032 3 : tagsRequiringHostCleanup_.erase(it);
1033 : }
1034 4 : return HCCL_SUCCESS;
1035 4 : }
1036 :
1037 19 : HcclResult HcclCommunicator::ClearOpResource(const std::string &tag, bool aclGraphDestroyCbk)
1038 : {
1039 19 : std::lock_guard<std::mutex> lock(commResMutex_);
1040 19 : bool findTag = false;
1041 19 : CHK_RET(ClearResMap(tag, findTag, aclGraphDestroyCbk));
1042 19 : CHK_RET(ClearResMap(tag + "_host", findTag, aclGraphDestroyCbk));
1043 19 : CHK_RET(ClearResMap(tag + "_device", findTag, aclGraphDestroyCbk));
1044 18 : if (!findTag) {
1045 12 : HCCL_WARNING("[%s] not find tag[%s] in resMap", __func__, tag.c_str());
1046 : }
1047 :
1048 19 : tagCommInfo_.erase(tag);
1049 : // stream解绑定
1050 16 : auto iterStream = tagStreamInfo_.find(tag);
1051 17 : if (iterStream != tagStreamInfo_.end()) {
1052 0 : CHK_RET(StreamActiveManager::GetInstance(deviceLogicId_).StreamsUnactive(iterStream->second.ringStreams));
1053 : }
1054 16 : tagStreamInfo_.erase(tag);
1055 16 : if (opRetryStreamPtr_ != nullptr) {
1056 13 : opRetryStreamPtr_->erase(tag);
1057 : }
1058 15 : if (implAlg_ != nullptr) {
1059 14 : CHK_RET(implAlg_->ClearOpResource(tag));
1060 : }
1061 14 : DestroyWorkspaceResource(tag);
1062 19 : return HCCL_SUCCESS;
1063 19 : }
1064 :
1065 95 : HcclResult HcclCommunicator::CreateOpBasedResources(const HcclCMDType &opType, const std::string &tag,
1066 : const HcomCollOpInfo &opInfo)
1067 : {
1068 95 : return workSpaceRes_->CreateOpBasedResources(opType, tag, opInfo);
1069 : }
1070 :
1071 0 : HcclResult HcclCommunicator::CreateRemoteOpBasedResources(u64 memSize, const std::string &tag)
1072 : {
1073 0 : return workSpaceRes_->CreateRemoteOpBasedResources(memSize, tag);
1074 : }
1075 :
1076 0 : HcclResult HcclCommunicator::DestroyRemoteOpBasedMem(const std::string &tag)
1077 : {
1078 0 : return workSpaceRes_->DestroyRemoteOpBasedMem(tag);
1079 : }
1080 :
1081 138 : bool HcclCommunicator::IsAtomicInit()
1082 : {
1083 276 : if (!initializedFlag_.test_and_set()) {
1084 0 : initializedFlag_.clear();
1085 0 : return false;
1086 : }
1087 138 : return true;
1088 : }
1089 :
1090 5 : bool HcclCommunicator::IsNeedNicInit()
1091 : {
1092 10 : return ((nicInitialized_ == 0) && (!hcomGroupNicInit_) && (userRankSize_ > 1) && !isSingleMeshAggregation_ &&
1093 10 : (superPodNum_ > 1 || !isUsedInterHccsMode_));
1094 : }
1095 :
1096 0 : HcclResult HcclCommunicator::GetBandWidthPerNPU(u32 level, float &bandWidth)
1097 : {
1098 0 : return hccl::GetBandWidthPerNPU(level, userRankSize_, deviceNumPerAggregation_, bandWidth);
1099 : }
1100 :
1101 0 : HcclResult HcclCommunicator::GetDeviceNumPerAggregation(u32 &deviceNumPerAggregation)
1102 : {
1103 0 : deviceNumPerAggregation = deviceNumPerAggregation_;
1104 0 : return HCCL_SUCCESS;
1105 : }
1106 :
1107 234 : HcclResult HcclCommunicator::InitHccpChannel()
1108 : {
1109 234 : return hcclH2dTlv::GetInstance().InitHccpChannel(devicePhyId_);
1110 : }
1111 :
1112 403 : std::vector<RankInfo> HcclCommunicator::GetRankLists()
1113 : {
1114 403 : return rankInfoList_;
1115 : }
1116 :
1117 159 : HcclResult HcclCommunicator::CheckReduceDataType(const HcclDataType dataType, const HcclReduceOp op)
1118 : {
1119 159 : if ((deviceType_ == DevType::DEV_TYPE_910B) || (deviceType_ == DevType::DEV_TYPE_910_93)) {
1120 51 : if ((op == HCCL_REDUCE_PROD) &&
1121 6 : ((dataType == HCCL_DATA_TYPE_INT16) || (dataType == HCCL_DATA_TYPE_BFP16))) {
1122 0 : RPT_INPUT_ERR(true,
1123 : "EI0003",
1124 : std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),
1125 : std::vector<std::string>({"CheckReduceDataType",
1126 : GetDataTypeEnumStr(dataType),
1127 : "dataType",
1128 : "HCCL_DATA_TYPE_INT8, HCCL_DATA_TYPE_INT32, HCCL_DATA_TYPE_FP16, HCCL_DATA_TYPE_FP32"}));
1129 0 : HCCL_ERROR("[%s][%s]errNo[0x%016llx] device type[%d] does not support the data type[%s] and data "
1130 : "type[%s] for Op[%s]",
1131 : LOG_KEYWORDS_TASK_EXEC.c_str(),
1132 : LOG_KEYWORDS_INVALID_ARGUMENT.c_str(),
1133 : HCCL_ERROR_CODE(HCCL_E_NOT_SUPPORT),
1134 : deviceType_,
1135 : GetDataTypeEnumStr(HCCL_DATA_TYPE_BFP16).c_str(),
1136 : GetDataTypeEnumStr(HCCL_DATA_TYPE_INT16).c_str(),
1137 : GetReduceOpEnumStr(op).c_str());
1138 0 : return HCCL_E_NOT_SUPPORT;
1139 : }
1140 108 : } else if (deviceType_ == DevType::DEV_TYPE_910) {
1141 108 : if (dataType == HCCL_DATA_TYPE_INT16) {
1142 0 : RPT_INPUT_ERR(true,
1143 : "EI0003",
1144 : std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),
1145 : std::vector<std::string>({"CheckReduceDataType",
1146 : GetDataTypeEnumStr(dataType),
1147 : "dataType",
1148 : "HCCL_DATA_TYPE_INT8, HCCL_DATA_TYPE_INT32, HCCL_DATA_TYPE_FP16, HCCL_DATA_TYPE_FP32"}));
1149 0 : HCCL_ERROR("[%s][%s]errNo[0x%016llx] device type[%d] does not support the data type[%s]",
1150 : LOG_KEYWORDS_TASK_EXEC.c_str(),
1151 : LOG_KEYWORDS_INVALID_ARGUMENT.c_str(),
1152 : HCCL_ERROR_CODE(HCCL_E_NOT_SUPPORT),
1153 : deviceType_,
1154 : GetDataTypeEnumStr(dataType).c_str());
1155 0 : return HCCL_E_NOT_SUPPORT;
1156 : }
1157 0 : } else if (deviceType_ == DevType::DEV_TYPE_310P3) {
1158 0 : if (dataType == HcclDataType::HCCL_DATA_TYPE_INT16 && op != HcclReduceOp::HCCL_REDUCE_SUM) {
1159 0 : RPT_INPUT_ERR(true,
1160 : "EI0003",
1161 : std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),
1162 : std::vector<std::string>({"CheckReduceDataType",
1163 : GetReduceOpEnumStr(op),
1164 : "op",
1165 : "sum"}));
1166 0 : HCCL_ERROR("[%s][%s]errNo[0x%016llx] device type[%d] does not support the data type[%s] for Op[%s]",
1167 : LOG_KEYWORDS_TASK_EXEC.c_str(),
1168 : LOG_KEYWORDS_INVALID_ARGUMENT.c_str(),
1169 : HCCL_ERROR_CODE(HCCL_E_NOT_SUPPORT),
1170 : deviceType_,
1171 : GetDataTypeEnumStr(HcclDataType::HCCL_DATA_TYPE_INT16).c_str(),
1172 : GetReduceOpEnumStr(op).c_str());
1173 0 : return HCCL_E_NOT_SUPPORT;
1174 : }
1175 : }
1176 159 : return HCCL_SUCCESS;
1177 0 : }
1178 :
1179 234 : HcclResult HcclCommunicator::GetAlgType(AlgType &algType, HcclCMDType opType)
1180 : {
1181 234 : CHK_SMART_PTR_NULL(implAlg_);
1182 233 : return implAlg_->GetAlgType(algType, opType);
1183 : }
1184 :
1185 1 : HcclResult HcclCommunicator::GetCommParams(HcclCommParams ¶ms)
1186 : {
1187 1 : params.commHandle = commHandle_;
1188 1 : params.rank = userRank_;
1189 1 : params.userRank = realUserRank_;
1190 1 : params.totalRanks = userRankSize_;
1191 1 : params.logicDevId = deviceLogicId_;
1192 1 : params.deviceType = deviceType_;
1193 1 : params.hcomGroupNicInit = hcomGroupNicInit_;
1194 1 : params.identifier = identifier_;
1195 1 : params.ranktableCrc = ranktableCrc_;
1196 1 : params.commConnections = commConnections_;
1197 1 : params.commPortConfig.devPortSwitchOn = commPortConfig_.devPortSwitchOn;
1198 1 : return HCCL_SUCCESS;
1199 : }
1200 :
1201 1 : HcclResult HcclCommunicator::GetCommRankTable(RankTable_t &rankTable)
1202 : {
1203 2 : for (auto &server : servRankInfo_) {
1204 2 : for (auto &rank : server.second) {
1205 1 : rankTable.rankList.emplace_back(rank);
1206 : }
1207 : }
1208 1 : rankTable.serverNum = serverNum_;
1209 1 : rankTable.superPodNum = superPodNum_;
1210 1 : rankTable.nicDeploy = nicDeployment_;
1211 1 : rankTable.version = attrCollector_.GetRankTableVersion();
1212 1 : return HCCL_SUCCESS;
1213 : }
1214 :
1215 488 : HcclResult HcclCommunicator::InitPara()
1216 : {
1217 : // 检查当前user_rank 对应的devid和rt查到的一致
1218 488 : CHK_RET(attrCollector_.CheckLocalRankInfo());
1219 488 : CHK_RET(attrCollector_.CalAndSetMeshAggRankSize());
1220 488 : meshAggregationRankSize_ = attrCollector_.GetMeshAggregationRankSize();
1221 :
1222 : // 初始化计数任务
1223 488 : CHK_RET(OpExeCounter::GetInstance(deviceLogicId_).InitCounter());
1224 :
1225 488 : notifyPool_.reset(new (std::nothrow) NotifyPool());
1226 488 : CHK_SMART_PTR_NULL(notifyPool_);
1227 488 : CHK_RET(notifyPool_->Init(devicePhyId_));
1228 :
1229 488 : callbackTask_.reset(new (std::nothrow) HcclCallbackTask(devicePhyId_, deviceLogicId_,
1230 488 : dispatcher_, nicDeployment_));
1231 488 : CHK_SMART_PTR_NULL(callbackTask_);
1232 :
1233 488 : workSpaceRes_.reset(new (std::nothrow)
1234 488 : WorkspaceResource(devicePhyId_, deviceLogicId_, &cclBufferManager_));
1235 488 : CHK_SMART_PTR_NULL(workSpaceRes_);
1236 :
1237 488 : CHK_RET(InitAlgResource());
1238 488 : return HCCL_SUCCESS;
1239 : }
1240 :
1241 0 : bool HcclCommunicator::IsStandardCard()
1242 : {
1243 0 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
1244 0 : HCCL_INFO("The current device just support this StandardCard case.");
1245 0 : return true;
1246 : }
1247 :
1248 0 : return ((pairLinkInfo_[static_cast<u32>(LinkTypeInServer::HCCS_TYPE)].size() == 0) &&
1249 0 : (pairLinkInfo_[static_cast<u32>(LinkTypeInServer::HCCS_SW_TYPE)].size() == 0) &&
1250 0 : (pairLinkInfo_[static_cast<u32>(LinkTypeInServer::SIO_TYPE)].size() == 0));
1251 : }
1252 :
1253 520 : HcclResult HcclCommunicator::InitOpRetry()
1254 : {
1255 520 : EXCEPTION_CATCH((opRetryStreamPtr_ = std::make_shared<HcclOpStreamRes>()), return HCCL_E_PTR);
1256 520 : if (retryEnable_) {
1257 0 : opRetryManager_.reset(new (std::nothrow) OpRetryManager());
1258 0 : CHK_SMART_PTR_NULL(opRetryManager_);
1259 0 : HcclIpAddress hostIp = !rankInfoList_.empty() ? rankInfoList_[0].hostIp : HcclIpAddress();
1260 0 : u32 hostPort = !rankInfoList_.empty() ? rankInfoList_[0].hostPort : HCCL_INVALID_PORT;
1261 0 : s32 hostDevId = !rankInfoList_.empty() ? rankInfoList_[0].devicePhyId : 0;
1262 0 : HcclIpAddress localIp = rankInfoList_.size() > userRank_ ? rankInfoList_[userRank_].hostIp : HcclIpAddress();
1263 0 : auto notifyResetCallback = [this](bool isSendRecv, s64 destRank) {
1264 0 : return isSendRecv ? this->ResetNotifyForDestRank(destRank) : this->ResetNotify();
1265 0 : };
1266 :
1267 0 : auto setTransportStatusCallback = [this](const HcclOpIdentifier &opId, bool statusStop,
1268 : const std::map<u32, bool> &remoteRankPortMap, const std::map<u32, bool> &isChangeLinkMap, bool isChangeLinkFlag) {
1269 0 : return this->SetTransportStatus(opId, statusStop, remoteRankPortMap, isChangeLinkMap, isChangeLinkFlag);
1270 0 : };
1271 : auto getSwitchRanksCallback =
1272 0 : [this](u32 *distSwitchRankList, bool *distSwitchUseBackup, u32 &distSwitchRankNum,
1273 : u8 *distRemoteRankNicStatus, u32 &distRankSize, bool &needCheckDefaultNic, bool &needCheckBackupNic) {
1274 0 : return this->GetSwitchRanks(distSwitchRankList, distSwitchUseBackup, distSwitchRankNum,
1275 0 : distRemoteRankNicStatus, distRankSize, needCheckDefaultNic, needCheckBackupNic);
1276 0 : };
1277 0 : auto setTransportResumeStatusCallback = [this](const std::map<u32, bool> &remoteRankPortMap,
1278 : const std::map<u32, bool> &isChangeLinkMap, bool isChangeLinkFlag, bool statusStop){
1279 0 : return this->SetTransportResumeStatus(remoteRankPortMap, isChangeLinkMap, isChangeLinkFlag, statusStop); };
1280 0 : HcclNetDevCtx netDevCtx = netDevCtxMap_[devIpAddr_[0]];
1281 0 : HcclNetDevCtx backUpNetDevCtx = {};
1282 0 : if (IsEnableBackupLink()) {
1283 0 : g_enableBackupLinkCommCount++;
1284 : }
1285 0 : if (IsEnableBackupLink() && netDevCtxMap_.find(devBackupIpAddr_[0]) != netDevCtxMap_.end()) {
1286 0 : backUpNetDevCtx = netDevCtxMap_[devBackupIpAddr_[0]];
1287 : }
1288 0 : OpRetryServerInfo serverInfo = {hostIp, hostPort, hostDevId};
1289 0 : OpRetryAgentInfo agentInfo = {userRank_, deviceLogicId_, localIp, devIpAddr_[0], netDevCtx, backUpNetDevCtx};
1290 :
1291 0 : OpRetryAgentParam agentParam;
1292 0 : agentParam.group = identifier_;
1293 0 : agentParam.agentConnection = commConnections_.agentConnection;
1294 0 : agentParam.h2dPtr = kfcControlTransferH2D_;
1295 0 : agentParam.d2hPtr = kfcStatusTransferD2H_;
1296 0 : agentParam.opStreamPtr = opRetryStreamPtr_;
1297 0 : agentParam.notifyResetCallback = notifyResetCallback;
1298 0 : agentParam.setTransportStatusCallback = setTransportStatusCallback;
1299 0 : agentParam.setTransportResumeStatusCallback = setTransportResumeStatusCallback;
1300 0 : agentParam.getSwitchRanksCallback = getSwitchRanksCallback;
1301 0 : agentParam.isEnableBackupLink = IsEnableBackupLink();
1302 0 : agentParam.isEnableSdmaRetry = commConfig_.GetConfigInterServerRetryEnable();
1303 0 : agentParam.agentInfo = agentInfo;
1304 :
1305 0 : CHK_RET(opRetryManager_->RegisterOpRetryMachine(agentParam, userRankSize_, commConnections_.isRoot,
1306 : commConnections_.serverConnections, serverInfo));
1307 0 : HCCL_RUN_INFO("[InitOpRetry] group[%s], isEnableBackupLink[%d], g_enableBackupLinkCommCount[%u]",
1308 : identifier_.c_str(), IsEnableBackupLink(), g_enableBackupLinkCommCount.load());
1309 0 : }
1310 520 : return HCCL_SUCCESS;
1311 : }
1312 :
1313 1 : bool HcclCommunicator::CompareWithServerId(const ServerInfo_t &left, const ServerInfo_t &right)
1314 : {
1315 1 : return (strcmp(left.serverId.c_str(), right.serverId.c_str()) < 0);
1316 : }
1317 :
1318 0 : bool HcclCommunicator::CompareWithNicName(const NetworkInfo_t &left, const NetworkInfo_t &right)
1319 : {
1320 0 : return (strcmp(left.ethName.c_str(), right.ethName.c_str()) < 0);
1321 : }
1322 :
1323 0 : bool HcclCommunicator::CompareWithUserRank(const RankInfo &left, const RankInfo &right)
1324 : {
1325 0 : return left.userRank < right.userRank;
1326 : }
1327 :
1328 304 : HcclResult HcclCommunicator::InitPreResource(const RankTable_t &rankTable)
1329 : {
1330 304 : if (static_cast<s32>(devicePhyId_) == HOST_DEVICE_ID) {
1331 0 : HCCL_ERROR("[Init][PreResource]not support cpu rank");
1332 0 : return HCCL_E_NOT_SUPPORT;
1333 : }
1334 : (void)rankTable;
1335 : // 判断是否为A3多docker场景,该场景需要使用sdid获取到的serverId判断是否属于同一server,若属于同一server则需要enablep2p
1336 304 : if (deviceType_ == DevType::DEV_TYPE_910_93) {
1337 16 : uint32_t localRankServerId = 0;
1338 16 : uint32_t remoteRankServerId = 0;
1339 16 : rtError_t ret = rtGetServerIDBySDID(rankInfoList_[userRank_].superDeviceId, &localRankServerId);
1340 16 : CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Init][PreResource]rtGetServerIDBySDID failed sdid[0x%08x], serverID[%u], ret[%u]",
1341 : rankInfoList_[userRank_].superDeviceId, localRankServerId, ret), HCCL_E_RUNTIME);
1342 65 : for (size_t index = 0; index < rankInfoList_.size(); ++index)
1343 : {
1344 49 : const RankInfo &rankInfo = rankInfoList_[index];
1345 49 : ret = rtGetServerIDBySDID(rankInfo.superDeviceId, &remoteRankServerId);
1346 49 : CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Init][PreResource]rtGetServerIDBySDID failed sdid[0x%08x], serverID[%u], ret[%u]",
1347 : rankInfo.superDeviceId, remoteRankServerId, ret), HCCL_E_RUNTIME);
1348 49 : if (serverId_ != rankInfo.serverId && localRankServerId == remoteRankServerId) {
1349 8 : enableP2PDevices_.push_back(rankInfo.devicePhyId);
1350 8 : enableP2PRankIds_.insert(rankInfo.userRank);
1351 8 : HCCL_INFO("[Init][PreResource]localRankID[%u]-localDevicePhyId[%u] needs to enablep2p with remoteRankId[%u]-remoteDevicePhyId[%u], " \
1352 : "and localServerId[%s], localServerIdBySDID[%u], remoteServerId[%s], remoteServerIdBySDID[%u]",
1353 : userRank_, rankInfoList_[userRank_].devicePhyId, rankInfo.userRank, rankInfo.devicePhyId,
1354 : serverId_.c_str(), localRankServerId, rankInfo.serverId.c_str(), remoteRankServerId);
1355 : }
1356 : }
1357 : }
1358 : // 查询本rank所在服务器
1359 304 : auto iterServ = servRankInfo_.find(serverId_);
1360 :
1361 305 : bool check = (iterServ == servRankInfo_.end());
1362 304 : CHK_PRT_RET(check, HCCL_ERROR("[Init][PreResource]can't find serverId[%s] in server map", serverId_.c_str()),
1363 : HCCL_E_NOT_FOUND);
1364 :
1365 1241 : for (u32 i = 0; i < iterServ->second.size(); i++) {
1366 940 : if (iterServ->second[i].deviceInfo.devicePhyId != HOST_DEVICE_ID) {
1367 940 : enableP2PDevices_.push_back(iterServ->second[i].deviceInfo.devicePhyId);
1368 940 : enableP2PRankIds_.insert(iterServ->second[i].rankId);
1369 938 : HCCL_INFO("[Init][PreResource]In the current server[%s], localRank[%u]-localDevicePhyId[%u] needs to enableP2P " \
1370 : "with remoteRankId[%u]-remoteDevicePhyId[%u]",
1371 : serverId_.c_str(), userRank_, rankInfoList_[userRank_].devicePhyId,
1372 : iterServ->second[i].rankId, iterServ->second[i].deviceInfo.devicePhyId);
1373 : }
1374 : }
1375 305 : HCCL_INFO("[Init][PreResource]Current deviceType[%d], isStandardCard[%s]", deviceType_, isStandardCard_ ? "true" : "false");
1376 305 : if (deviceType_ != DevType::DEV_TYPE_310P3 && !isStandardCard_) {
1377 155 : HcclResult ret = P2PMgmtPub::EnableP2P(enableP2PDevices_);
1378 155 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Init][PreResource]Enable P2P Failed, deviceLogicId[%d], ret[%u]", deviceLogicId_, ret), ret);
1379 : }
1380 :
1381 305 : drvInit_ = true;
1382 305 : return HCCL_SUCCESS;
1383 : }
1384 :
1385 494 : HcclResult HcclCommunicator::InitTcpMode(const RankTable_t &rankTable) const
1386 : {
1387 494 : bool isTcpMode = false;
1388 494 : HCCL_INFO("[TcpMode][%u] [1:TCP, 2:RDMA, 3:RESERVED]", GetExternalInputProtocolType());
1389 494 : if (GetExternalInputProtocolType() == ProtocolType::TCP) {
1390 0 : isTcpMode = true;
1391 : }
1392 494 : else if (GetExternalInputProtocolType() == ProtocolType::RDMA) {
1393 : // 通信协议选择RDMA
1394 : } else {
1395 295 : isTcpMode = (rankTable.nicDeploy == NICDeployment::NIC_DEPLOYMENT_HOST);
1396 295 : HCCL_INFO("[Init][TcpMode]isTcpMode[%d] nicDeploy[%d]", isTcpMode, rankTable.nicDeploy);
1397 : }
1398 496 : SetTcpMode(isTcpMode);
1399 :
1400 : // 异构场景解析外部输入,放在SetTcpMode前防止Tcp用例走错分支,放在RecordProtocolType确保hdc模式下建链通信协议校验正确
1401 494 : CHK_RET(InitExternalInputHeterog());
1402 495 : return HCCL_SUCCESS;
1403 : }
1404 :
1405 488 : HcclResult HcclCommunicator::InitMyRankConnectMode(HcclCommParams ¶ms, const RankTable_t &rankTable)
1406 : {
1407 488 : if (deviceType_ != DevType::DEV_TYPE_910B) { /* 910B才支持host网卡特性 */
1408 325 : myRankConnectMode_ = 0;
1409 325 : return HCCL_SUCCESS;
1410 : }
1411 163 : uint32_t localRank = params.rank;
1412 163 : if (rankTable.nicDeploy != NICDeployment::NIC_DEPLOYMENT_HOST) {
1413 163 : myRankConnectMode_ = 0;
1414 163 : return HCCL_SUCCESS;
1415 : }
1416 0 : for (auto it : rankTable.rankList) {
1417 0 : if (it.rankId == localRank) {
1418 0 : continue;
1419 : }
1420 0 : if (it.deviceInfo.nicDeploy == NICDeployment::NIC_DEPLOYMENT_DEVICE) {
1421 0 : myRankConnectMode_ = 1;
1422 0 : return HCCL_SUCCESS;
1423 : }
1424 0 : }
1425 0 : myRankConnectMode_ = 0;
1426 0 : return HCCL_SUCCESS;
1427 : }
1428 :
1429 233 : uint32_t HcclCommunicator::GetConnectMode()
1430 : {
1431 233 : return myRankConnectMode_;
1432 : }
1433 :
1434 1789 : bool HcclCommunicator::IsEnableBackupLink()
1435 : {
1436 223 : return deviceType_ == DevType::DEV_TYPE_910_93 && IsEnableRoce() && GetAicpuUnfoldConfig() && retryEnable_ &&
1437 2012 : commConfig_.GetConfigInterSuperPodRetryEnable() && !devBackupIpAddr_[0].IsInvalid() && rtsSupportChangeLink_ &&
1438 1789 : !isDiffDeviceType_;
1439 : }
1440 :
1441 162 : HcclResult HcclCommunicator::InitRaNetResource()
1442 : {
1443 162 : CHK_RET(IsHostUseDevNic(isHostUseDevNic_));
1444 :
1445 162 : if (static_cast<s32>(devicePhyId_) != HOST_DEVICE_ID ||
1446 0 : nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_DEVICE) {
1447 162 : CHK_RET(HcclNetInit(NICDeployment::NIC_DEPLOYMENT_DEVICE, devicePhyId_, deviceLogicId_, false));
1448 161 : if (IsEnableBackupLink()) {
1449 : // 超节点 && level2支持重执行 && Aicpu -> 初始化主备hccp资源(Pid粒度)
1450 1 : CHK_RET(hrtGetPairDevicePhyId(devicePhyId_, deviceBackUpPhyId_));
1451 1 : if (hrtGetDeviceIndexByPhyId(deviceBackUpPhyId_, deviceBackUpLogicId_) != HCCL_SUCCESS) {
1452 1 : rtsSupportChangeLink_ = false;
1453 1 : HCCL_ERROR("[%s]Runtime does not support changelink, deviceLogicId_[%d], devicePhyId_[%u], "
1454 : "deviceBackUpPhyId_[%u], deviceBackUpLogicId_[%u], nicDeployment_[%d], IsEnableBackupLink[%d]"
1455 : "rtsSupportChangeLink_[%d]",
1456 : __func__, deviceLogicId_, devicePhyId_, deviceBackUpPhyId_,
1457 : deviceBackUpLogicId_, nicDeployment_, IsEnableBackupLink(), rtsSupportChangeLink_);
1458 1 : return HCCL_E_NOT_SUPPORT;
1459 : } else {
1460 0 : CHK_RET(HcclNetInit(NICDeployment::NIC_DEPLOYMENT_DEVICE, deviceBackUpPhyId_, deviceBackUpLogicId_,
1461 : false, true));
1462 0 : HCCL_DEBUG("[%s]Default & backup NetworkManager Init, deviceLogicId[%d], devicePhyId[%u], "
1463 : "deviceBackUpPhyId_[%u], deviceBackUpLogicId_[%u], nicDeployment_[%d], IsEnableBackupLink[%d]",
1464 : __func__, deviceLogicId_, devicePhyId_, deviceBackUpPhyId_, deviceBackUpLogicId_,
1465 : nicDeployment_, IsEnableBackupLink());
1466 : }
1467 : }
1468 : }
1469 :
1470 160 : if ((static_cast<s32>(devicePhyId_) != HOST_DEVICE_ID && isHaveCpuRank_) ||
1471 480 : (IsEnableRoce() && nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST) ||
1472 160 : (Is310PDevice() && nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST)) {
1473 0 : u32 devicePhyID = (static_cast<s32>(devicePhyId_) == HOST_DEVICE_ID) ? 0 : devicePhyId_;
1474 0 : u32 whiteListEn = (GetExternalInputHcclEnableWhitelist() == HCCL_WHITELIST_ON ? 1 : 0);
1475 0 : CHK_RET(HcclNetInit(NICDeployment::NIC_DEPLOYMENT_HOST, devicePhyID, deviceLogicId_, whiteListEn));
1476 : }
1477 160 : return HCCL_SUCCESS;
1478 : }
1479 :
1480 160 : HcclResult HcclCommunicator::InitRaNic()
1481 : {
1482 160 : CHK_RET(InitSocketManager());
1483 :
1484 160 : if (Is310PDevice()) {
1485 0 : CHK_RET(InitNic());
1486 160 : } else if (static_cast<s32>(devicePhyId_) != HOST_DEVICE_ID) {
1487 160 : std::shared_ptr<HcclSocket> &devVnicSocket = commPortConfig_.devVnicListen.first;
1488 160 : if (devVnicSocket) {
1489 1 : localVnicIp_ = devVnicSocket->GetLocalIp();
1490 1 : localVnicListenPort_ = devVnicSocket->GetLocalPort();
1491 1 : HcclNetDevCtx &devVnicCtx = commPortConfig_.devVnicListen.second;
1492 1 : CHK_PTR_NULL(devVnicCtx);
1493 1 : netDevCtxMap_.insert(std::make_pair(localVnicIp_, devVnicCtx));
1494 1 : CHK_RET(socketManager_->ServerInit(devVnicCtx, localVnicListenPort_));
1495 1 : commPortConfig_.devVnicListen.second = nullptr;
1496 1 : HCCL_INFO("[HcclCommunicator][InitRaResource] init vnic with listened socket success, "
1497 : "listened ip[%s] port[%u]",
1498 : localVnicIp_.GetReadableAddress(), localVnicListenPort_);
1499 : } else {
1500 159 : localVnicListenPort_ = GetLocalNicPort(NicType::VNIC_TYPE);
1501 159 : localVnicIp_ = HcclIpAddress(devicePhyId_);
1502 159 : if (useSuperPodMode_) {
1503 6 : CHK_RET(hrtRaGetSingleSocketVnicIpInfo(
1504 : devicePhyId_, DeviceIdType::DEVICE_ID_TYPE_SDID, superDeviceId_, localVnicIp_));
1505 : } else {
1506 159 : CHK_RET(hrtRaGetSingleSocketVnicIpInfo(
1507 : devicePhyId_, DeviceIdType::DEVICE_ID_TYPE_PHY_ID, devicePhyId_, localVnicIp_));
1508 : }
1509 :
1510 : HcclNetDevCtx vnicPortCtx;
1511 159 : CHK_RET(HcclNetOpenDev(&vnicPortCtx, NicType::VNIC_TYPE, devicePhyId_, deviceLogicId_, localVnicIp_));
1512 159 : CHK_PTR_NULL(vnicPortCtx);
1513 159 : netDevCtxMap_.insert(std::make_pair(localVnicIp_, vnicPortCtx));
1514 159 : CHK_RET(socketManager_->ServerInit(vnicPortCtx, localVnicListenPort_));
1515 152 : HCCL_INFO("[HcclCommunicator][InitRaResource] init vnic with ip[%s] port[%u] success",
1516 : localVnicIp_.GetReadableAddress(), localVnicListenPort_);
1517 : }
1518 :
1519 154 : if (IsEnableRoce()) {
1520 1 : CHK_RET(InitNic());
1521 : }
1522 : }
1523 153 : return HCCL_SUCCESS;
1524 : }
1525 :
1526 277 : HcclResult HcclCommunicator::InitRaResource()
1527 : {
1528 : /* 本通信域内只有1个device时,不需要初始化ra资源 */
1529 277 : if (userRankSize_ <= 1) {
1530 116 : HCCL_INFO("user rank size <= 1, ra is not needed for single device.");
1531 116 : return HCCL_SUCCESS;
1532 : }
1533 :
1534 161 : CHK_RET(InitRaNetResource());
1535 160 : CHK_RET(InitRaNic());
1536 :
1537 154 : HCCL_INFO("isUsedRdmaLevel0_[%u] nicNum[%u] hostIP[%s], nicDeployment[%d].",
1538 : isUsedRdmaLevel0_, devIpAddr_.size(), hostIp_.GetReadableAddress(), nicDeployment_);
1539 :
1540 154 : raResourceInit_ = true; // 全局通信域会初始化,子通信域不会初始化,但是析构均会进入此逻辑,需要标记
1541 154 : attrCollector_.GenSupportRdmaLite();
1542 154 : CHK_RET(attrCollector_.GenSupportHccsAndSio());
1543 153 : isSupportRdmaLite_ = attrCollector_.GetSupportRdmaLite(); // 是否支持Rdma Lite
1544 154 : isSupportHccsAndSio_ = attrCollector_.GetSupportHccsAndSio(); // 是否支持Hccs Sio并发
1545 153 : return HCCL_SUCCESS;
1546 : }
1547 :
1548 305 : HcclResult HcclCommunicator::DisablePreResource()
1549 : {
1550 : // 查询本rank所在服务器
1551 305 : auto iterServ = servRankInfo_.find(serverId_);
1552 305 : bool check = (iterServ == servRankInfo_.end());
1553 305 : CHK_PRT_RET(check, HCCL_ERROR("[Disable][PreResource]can't find serverId[%s] in server map", serverId_.c_str()),
1554 : HCCL_E_NOT_FOUND);
1555 305 : HcclResult ret = P2PMgmtPub::DisableP2P(enableP2PDevices_);
1556 305 : CHK_PRT_RET(ret != HCCL_SUCCESS,
1557 : HCCL_ERROR("[Disable][PreResource]Disable all P2P Failed, deviceLogicId[%d], ret[%u]",
1558 : deviceLogicId_, ret),
1559 : ret);
1560 305 : enableP2PDevices_.clear();
1561 305 : enableP2PRankIds_.clear();
1562 304 : return HCCL_SUCCESS;
1563 : }
1564 :
1565 156 : HcclResult HcclCommunicator::GetWorkspaceSubStreamNum(u64 count, HcclDataType dataType, HcclReduceOp op,
1566 : const std::string &algName,u64 &streamNum, u64 dataSize, bool ifAiv, HcclCMDType opType)
1567 : {
1568 156 : AlgType algType;
1569 :
1570 156 : CHK_RET(GetAlgType(algType, opType));
1571 :
1572 : std::map<HcclCMDType, u64> gapMap = {
1573 : {HcclCMDType::HCCL_CMD_REDUCE_SCATTER, HCCL_SMALL_COUNT_512_KB + HCCL_SMALL_COUNT_512_KB},
1574 : {HcclCMDType::HCCL_CMD_ALLGATHER, HCCL_SMALL_COUNT_512_KB + HCCL_SMALL_COUNT_512_KB},
1575 310 : {HcclCMDType::HCCL_CMD_ALLREDUCE, (HCCL_SMALL_COUNT_512_KB + HCCL_SMALL_COUNT_512_KB) * userRankSize_}};
1576 :
1577 : // 图模式下AIV展开,需要重新计算streamNum
1578 156 : bool ifHcomWithAiv = ifAiv && (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB);
1579 156 : HCCL_INFO("[GetWorkspaceSubStreamNum] ifAiv[%d], workflowMode[%d], ifHcomWithAiv[%d]",
1580 : ifAiv, GetWorkflowMode(), ifHcomWithAiv);
1581 158 : if (ifHcomWithAiv && (deviceType_ == DevType::DEV_TYPE_910_93 || deviceType_ == DevType::DEV_TYPE_910B)) {
1582 0 : HCCL_INFO("[GetWorkspaceSubStreamNum] Hcom AIV enabled, calculating the streamNum.");
1583 : // A3 和 A2 公用以下的参数
1584 0 : std::string newTag;
1585 0 : std::unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(opType);
1586 0 : CHK_SMART_PTR_NULL(algOperator);
1587 0 : OpParam param;
1588 0 : param.reduceType = op;
1589 0 : param.opType = opType;
1590 :
1591 0 : if (opType == HcclCMDType::HCCL_CMD_ALLTOALL || opType == HcclCMDType::HCCL_CMD_ALLTOALLV) {
1592 0 : param.All2AllDataDes.sendType = dataType;
1593 0 : param.All2AllDataDes.recvType = dataType;
1594 0 : param.All2AllDataDes.sendCount = count;
1595 : } else { //不论 A2 还是 A3,AIV场景下的AllReduce/ReduceScatter还是A2上单独支持AIV的算子都用以下参数
1596 0 : param.DataDes.count = count;
1597 0 : param.DataDes.dataType = dataType;
1598 : }
1599 0 : AlgResourceRequest resRequest;
1600 0 : CHK_RET(algOperator->CalcResRequest(algName, param, resRequest)); // 计算资源请求
1601 0 : streamNum = resRequest.streamNum;
1602 0 : HCCL_INFO("[GetWorkspaceSubStreamNum] Hcom AIV enabled on DeviceType[%d], the streamNum is [%llu]",
1603 : deviceType_, streamNum);
1604 0 : return HCCL_SUCCESS;
1605 0 : }
1606 :
1607 155 : if (serverNum_ == 1 && deviceType_ == DevType::DEV_TYPE_910_93 && opType == HcclCMDType::HCCL_CMD_ALLGATHER &&
1608 313 : dataSize <= gapMap[opType] &&
1609 0 : deviceNumPerAggregation_ > HCCL_DEVICE_NUM_TWO) {
1610 0 : constexpr u64 streamForSmallCount = 3;
1611 0 : streamNum = streamForSmallCount;
1612 0 : HCCL_DEBUG("[GetWorkspaceSubStreamNum]DEV_TYPE_910_93 Single Server, the streamNum is %llu", streamNum);
1613 0 : return HCCL_SUCCESS;
1614 : }
1615 :
1616 155 : if (serverNum_ == 1 && deviceType_ == DevType::DEV_TYPE_910_93 && gapMap.find(opType) != gapMap.end() &&
1617 313 : dataSize <= gapMap[opType] &&
1618 0 : deviceNumPerAggregation_ > HCCL_DEVICE_NUM_TWO) {
1619 0 : streamNum = deviceNumPerAggregation_ - HCCL_SUB_STREAM_NP_MESH;
1620 0 : HCCL_DEBUG("[GetWorkspaceSubStreamNum]DEV_TYPE_910_93 Single Server, the streamNum is %llu", streamNum);
1621 0 : return HCCL_SUCCESS;
1622 : }
1623 :
1624 158 : if (deviceType_ == DevType::DEV_TYPE_910_93) {
1625 8 : streamNum = HCCL_SUB_STREAM_NUM_DOUBLE_RING + RDMA_PLANE_NUM_IN_NPRING_DOUBLE;
1626 8 : if (algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_NP_DOUBLE_RING) {
1627 0 : streamNum += 1U; // semi_ring算法server内增加一条从流,需要2条从流
1628 : }
1629 8 : if (opType == HcclCMDType::HCCL_CMD_ALLTOALLV || opType == HcclCMDType::HCCL_CMD_ALLTOALL ||
1630 8 : opType == HcclCMDType::HCCL_CMD_ALLTOALLVC) {
1631 0 : streamNum = MAX_RANK_SIZE;
1632 : }
1633 8 : HCCL_DEBUG("[GetWorkspaceSubStreamNum]DEV_TYPE_910_93, the streamNum is %llu", streamNum);
1634 8 : return HCCL_SUCCESS;
1635 : }
1636 :
1637 : // AR RS 在开启Strict && 静态图、RSv 在开启确定性 && 静态图时, 需要重新计算StreamNum
1638 300 : if (deviceType_ == DevType::DEV_TYPE_910B
1639 262 : && (((opType == HcclCMDType::HCCL_CMD_ALLREDUCE || opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER)
1640 0 : && GetExternalInputHcclDeterministicV2() == DETERMINISTIC_STRICT)
1641 112 : || (opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V
1642 0 : && GetExternalInputHcclDeterministicV2() != DETERMINISTIC_DISABLE)
1643 112 : || (opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V
1644 0 : && !isSingleMeshAggregation_ && !multiModuleDiffDeviceNumMode_//多机&对称&图模式
1645 0 : && GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB))) {
1646 : // 图模式 A2规约保序场景,需要重新计算需要的streamNum
1647 0 : streamNum = CalcStreamNumForReduceOrderPreservation();
1648 0 : HCCL_DEBUG("[GetWorkspaceSubStreamNum]A2 reduce order preservation, the streamNum is %llu", streamNum);
1649 0 : return HCCL_SUCCESS;
1650 : }
1651 :
1652 150 : if (deviceType_ == DevType::DEV_TYPE_910B && opType == HcclCMDType::HCCL_CMD_ALLREDUCE && algType.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_PIPELINE) {
1653 0 : streamNum = userRankSize_ / moduleNum_ - 1;
1654 0 : HCCL_DEBUG("[GetWorkspaceSubStreamNum]A2 pipeline AllReduce, the streamNum is %llu", streamNum);
1655 0 : return HCCL_SUCCESS;
1656 : }
1657 :
1658 : // 设置AG和RS的图模式pipeline算法能够申请的streamNum
1659 150 : if (deviceType_ == DevType::DEV_TYPE_910B && // 910B
1660 112 : algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_NP_MESH && // fullmesh
1661 88 : (opType == HcclCMDType::HCCL_CMD_ALLGATHER || opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER ||
1662 88 : opType == HcclCMDType::HCCL_CMD_ALLGATHER_V) && // AG或RS或AGV
1663 0 : moduleNum_ > 1 && deviceNumPerAggregation_ > 1 && // 多机且每机器出多卡
1664 0 : (moduleNum_ <= MODULE_NUM_FOUR || // "机器数量小于等于4"
1665 0 : dataSize > HCCL_SMALL_COUNT_1_MB || // "大数据量"
1666 0 : algType.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_PIPELINE)) { // "指定level1的算法为pipeline"
1667 0 : streamNum = userRankSize_ / moduleNum_;
1668 0 : HCCL_DEBUG("[GetWorkspaceSubStreamNum]DEV_TYPE_910B, the streamNum is %llu", streamNum);
1669 0 : return HCCL_SUCCESS;
1670 : }
1671 :
1672 : // 设置310P图模式 alltoall 的streamNum
1673 150 : if(deviceType_ == DevType::DEV_TYPE_310P3 && (opType == HcclCMDType::HCCL_CMD_ALLTOALL || opType == HcclCMDType::HCCL_CMD_ALLTOALLV || opType == HcclCMDType::HCCL_CMD_ALLTOALLVC)){
1674 0 : streamNum = userRankSize_ * RANK_SET_COMPUTE_CONST;
1675 0 : HCCL_DEBUG("[GetWorkspaceSubStreamNum]DEV_TYPE_310P3, the streamNum is %llu", streamNum);
1676 0 : return HCCL_SUCCESS;
1677 : }
1678 :
1679 : // 根据所用算法,选择所需的从stream数目
1680 150 : switch (algType.algoLevel0) {
1681 88 : case AlgTypeLevel0::ALG_LEVEL0_NP_MESH:
1682 88 : streamNum = userRankSize_ / moduleNum_ - HCCL_SUB_STREAM_NP_MESH;
1683 88 : break;
1684 16 : case AlgTypeLevel0::ALG_LEVEL0_8P_RING:
1685 16 : streamNum = HCCL_SUB_STREAM_NUM_8P_RING;
1686 16 : break;
1687 0 : case AlgTypeLevel0::ALG_LEVEL0_NP_DOUBLE_RING:
1688 0 : streamNum = HCCL_SUB_STREAM_NUM_DOUBLE_RING;
1689 0 : break;
1690 12 : case AlgTypeLevel0::ALG_LEVEL0_4P_MESH:
1691 12 : streamNum = HCCL_SUB_STREAM_NUM_4P_MESH;
1692 12 : break;
1693 34 : default:
1694 34 : streamNum = HCCL_SUB_STREAM_NUM_ZERO;
1695 34 : break;
1696 : }
1697 :
1698 150 : if (SatisfyIntraSuperPod(deviceType_, userRankSize_, useSuperPodMode_, superPodNum_)) {
1699 0 : streamNum = std::max(static_cast<u64>(userRankSize_ - 1u), streamNum);
1700 445 : } else if (FullmeshPairwiseSatisfyHighPerfAlltoallMeshCondition(deviceType_,
1701 148 : meshAggregationRankSize_, useSuperPodMode_, commConfig_.GetConfigHcclAlgo(HcclCMDType::HCCL_CMD_ALLTOALL))) {
1702 0 : streamNum = std::max(static_cast<u64>(meshAggregationRankSize_ - 1u), streamNum);
1703 : }
1704 :
1705 148 : auto iter = HCCL_ALGO_LEVEL0_NAME_MAP.find(algType.algoLevel0);
1706 148 : CHK_PRT_RET(iter == HCCL_ALGO_LEVEL0_NAME_MAP.end(),
1707 : HCCL_ERROR("[GetWorkspaceSubStreamNum]level0: algType[%u] is invalid.", algType.algoLevel0),
1708 : HCCL_E_INTERNAL);
1709 148 : HCCL_DEBUG("[GetWorkspaceSubStreamNum]hccl algorithm: In level0, using %s algo, the streamNum is %llu",
1710 : iter->second.c_str(), streamNum);
1711 :
1712 150 : u64 sliceNum = CalculatePiplineSliceNum(opType, dataSize, algType, deviceType_, deviceNumPerServer_, serverNum_);
1713 : // 图模式下数据量固定, 按照当前数据量判断是否支持pipline切分并申请从流
1714 150 : if (implAlg_ != nullptr && sliceNum >= MIN_PIPLINE_SLICE_NUM) {
1715 0 : streamNum++;
1716 : }
1717 150 : return HCCL_SUCCESS;
1718 158 : }
1719 :
1720 804 : HcclResult HcclCommunicator::DestroyNetworkResources()
1721 : {
1722 804 : transportManager_ = nullptr;
1723 804 : if (raResourceInit_) {
1724 153 : socketManager_->DestroySockets();
1725 : }
1726 :
1727 : /* 本通信域内只有1个device时,不需要卸载ra资源 */
1728 804 : if (userRankSize_ <= 1) {
1729 208 : HCCL_INFO("user rank size <= 1, ra is not needed for single device");
1730 208 : return HCCL_SUCCESS;
1731 : }
1732 :
1733 : // nic的初始化独立调用,在此单独判断是否需要解初始化
1734 596 : if (nicInitialized_ > 0) {
1735 0 : CHK_RET(DeinitNic());
1736 : }
1737 :
1738 594 : if (raResourceInit_ && (static_cast<s32>(devicePhyId_) != HOST_DEVICE_ID) && !Is310PDevice()) {
1739 150 : CHK_RET(socketManager_->ServerDeInit(netDevCtxMap_[localVnicIp_], localVnicListenPort_));
1740 153 : HcclNetCloseDev(netDevCtxMap_[localVnicIp_]);
1741 153 : netDevCtxMap_.erase(localVnicIp_);
1742 : }
1743 :
1744 594 : CHK_RET(ReleasePreemptSocket());
1745 :
1746 595 : if (raResourceInit_) {
1747 153 : if (static_cast<s32>(devicePhyId_) != HOST_DEVICE_ID ||
1748 0 : nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_DEVICE) {
1749 153 : if (IsEnableBackupLink()) {
1750 : // 超节点 && level2支持重执行 && Aicpu -> 释放主备hccp资源
1751 0 : CHK_RET(HcclNetDeInit(NICDeployment::NIC_DEPLOYMENT_DEVICE, devicePhyId_, deviceLogicId_));
1752 0 : CHK_RET(HcclNetDeInit(NICDeployment::NIC_DEPLOYMENT_DEVICE, deviceBackUpPhyId_,
1753 : deviceBackUpLogicId_, true));
1754 0 : HCCL_DEBUG("[%s]Default & backup HcclNetDeInit, deviceLogicId[%d], devicePhyId[%u], "
1755 : "deviceBackUpPhyId_[%u], deviceBackUpLogicId_[%u], nicDeployment_[%d], IsEnableBackupLink[%d]",
1756 : __func__, deviceLogicId_, devicePhyId_, deviceBackUpPhyId_, deviceBackUpLogicId_,
1757 : nicDeployment_, IsEnableBackupLink());
1758 : }
1759 : else {
1760 153 : CHK_RET(HcclNetDeInit(NICDeployment::NIC_DEPLOYMENT_DEVICE, devicePhyId_, deviceLogicId_));
1761 : }
1762 : }
1763 :
1764 153 : if ((static_cast<s32>(devicePhyId_) != HOST_DEVICE_ID && isHaveCpuRank_) ||
1765 459 : (IsEnableRoce() && nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST) ||
1766 153 : (Is310PDevice() && nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST)) {
1767 0 : u32 devicePhyID = (static_cast<s32>(devicePhyId_) == HOST_DEVICE_ID) ? 0 : devicePhyId_;
1768 0 : CHK_RET(HcclNetDeInit(NICDeployment::NIC_DEPLOYMENT_HOST, devicePhyID, deviceLogicId_));
1769 : }
1770 :
1771 153 : socketManager_ = nullptr;
1772 : }
1773 :
1774 595 : raResourceInit_ = false;
1775 595 : return HCCL_SUCCESS;
1776 : }
1777 :
1778 155 : HcclResult HcclCommunicator::SetWorkspaceResource(const std::string &tag, void *memPtr, u64 &maxSize,
1779 : std::vector<rtStream_t> &stream)
1780 : {
1781 155 : return workSpaceRes_->SetWorkspaceResource(tag, memPtr, maxSize, stream);
1782 : }
1783 :
1784 17 : void HcclCommunicator::DestroyWorkspaceResource(const std::string &tag)
1785 : {
1786 17 : if (workSpaceRes_ == nullptr) {
1787 3 : return;
1788 : }
1789 15 : workSpaceRes_->DestroyWorkspaceResource(tag);
1790 : }
1791 :
1792 438 : HcclResult HcclCommunicator::AtomicInitSet()
1793 : {
1794 876 : CHK_PRT_RET(initializedFlag_.test_and_set(),
1795 : HCCL_ERROR("[HcclCommunicator][AtomicInitSet]errNo[0x%016llx] instance "
1796 : "already been initialized",
1797 : HCCL_ERROR_CODE(HCCL_E_INTERNAL)),
1798 : HCCL_E_INTERNAL);
1799 437 : return HCCL_SUCCESS;
1800 : }
1801 :
1802 5 : void HcclCommunicator::AtomicInitClear()
1803 : {
1804 5 : initializedFlag_.clear();
1805 5 : }
1806 :
1807 347 : u32 HcclCommunicator::GetUserRank()
1808 : {
1809 347 : return realUserRank_;
1810 : }
1811 :
1812 72 : u32 HcclCommunicator::GetGroupRank()
1813 : {
1814 72 : return userRank_;
1815 : }
1816 :
1817 337 : u32 HcclCommunicator::GetRankSize()
1818 : {
1819 337 : return userRankSize_;
1820 : }
1821 :
1822 0 : u32 HcclCommunicator::GetRankInParentComm()
1823 : {
1824 0 : return rankInParentComm_;
1825 : }
1826 :
1827 0 : bool HcclCommunicator::GetNicInitialized()
1828 : {
1829 0 : return nicInitialized_ > 0;
1830 : }
1831 :
1832 : /*
1833 : 1. 选择算法
1834 : 2. 计算resource,存到request内
1835 : 3. 创建和分配资源
1836 : */
1837 0 : HcclResult HcclCommunicator::HcclSelectAlg(HcclCMDType opType, u64 count, void* counts, HcclDataType dataType,
1838 : HcclReduceOp op, int32_t aivCoreLimit, bool &ifAiv, std::string &algName)
1839 : {
1840 0 : HCCL_INFO("[HcclCommunicator][HcclSelectAlg] start to run with opType[%d], count[%llu], dataType[%d], reduceOp[%d], aivCoreLimit[%d]",
1841 : opType, count, dataType, op, aivCoreLimit);
1842 0 : ifAiv = false;
1843 0 : if (opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V || opType == HcclCMDType::HCCL_CMD_ALLGATHER_V ||
1844 0 : opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV || opType == HcclCMDType::HCCL_CMD_BATCH_WRITE) {
1845 0 : HCCL_INFO("[HcclCommunicator][HcclSelectAlg] opType[%d] no need select AIV algorithm", opType);
1846 0 : return HCCL_SUCCESS;
1847 : }
1848 : /* 选择算法前,先更新成图模式 */
1849 0 : auto originWorkflowMode = GetWorkflowMode();
1850 0 : SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB);
1851 :
1852 0 : std::unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(opType);
1853 0 : CHK_SMART_PTR_NULL(algOperator);
1854 :
1855 0 : OpParam param;
1856 0 : param.reduceType = op;
1857 0 : param.opType = opType;
1858 0 : if (opType == HcclCMDType::HCCL_CMD_ALLTOALL || opType == HcclCMDType::HCCL_CMD_ALLTOALLV ||
1859 0 : opType == HcclCMDType::HCCL_CMD_ALLTOALLVC) {
1860 0 : param.All2AllDataDes.sendType = dataType;
1861 0 : param.All2AllDataDes.recvType = dataType;
1862 0 : param.All2AllDataDes.sendCount = count;
1863 0 : } else if (opType == HcclCMDType::HCCL_CMD_ALLGATHER_V || opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V) {
1864 0 : param.VDataDes.counts = counts;
1865 0 : param.VDataDes.dataType = dataType;
1866 : } else {
1867 0 : param.DataDes.count = count;
1868 0 : param.DataDes.dataType = dataType;
1869 : }
1870 :
1871 0 : AlgDesc algDesc;
1872 0 : std::string newTag;
1873 0 : ResourceLimit limit{true, true, 0};
1874 0 : limit.aivCoreLimit = aivCoreLimit;
1875 0 : CHK_RET(algOperator->SelectAlg("", param, limit, algName, algDesc, newTag));
1876 :
1877 : /* 非AIV算法直接返回 */
1878 0 : if (!algDesc.isAivMode) {
1879 0 : HCCL_INFO("[HcclCommunicator][HcclSelectAlg] select non-Aiv alg, early return");
1880 0 : return HCCL_SUCCESS;
1881 : }
1882 :
1883 : /* 完成算法选择和记录后,恢复成原来的模式 */
1884 0 : SetWorkflowMode(originWorkflowMode);
1885 0 : ifAiv = true;
1886 0 : HCCL_INFO("[HcclCommunicator][HcclSelectAlg] compile for aiv, select algName is [%s]", algName.c_str());
1887 0 : return HCCL_SUCCESS;
1888 0 : }
1889 :
1890 0 : HcclResult HcclCommunicator::HcclCalcNumBlocks(HcclCMDType opType, u64 count, void* counts, HcclDataType dataType,
1891 : int32_t aivCoreLimit, std::string &algName, u32 &numBlocks)
1892 : {
1893 0 : auto originWorkflowMode = GetWorkflowMode();
1894 0 : SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB);
1895 0 : std::unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(opType);
1896 0 : CHK_SMART_PTR_NULL(algOperator);
1897 0 : OpParam param;
1898 :
1899 0 : param.opType = opType;
1900 0 : if (opType == HcclCMDType::HCCL_CMD_ALLTOALL || opType == HcclCMDType::HCCL_CMD_ALLTOALLV ||
1901 0 : opType == HcclCMDType::HCCL_CMD_ALLTOALLVC) {
1902 0 : param.All2AllDataDes.sendType = dataType;
1903 0 : param.All2AllDataDes.recvType = dataType;
1904 0 : param.All2AllDataDes.sendCount = count;
1905 0 : } else if (opType == HcclCMDType::HCCL_CMD_ALLGATHER_V || opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V) {
1906 0 : param.VDataDes.counts = counts;
1907 0 : param.VDataDes.dataType = dataType;
1908 : } else {
1909 0 : param.DataDes.count = count;
1910 0 : param.DataDes.dataType = dataType;
1911 : }
1912 :
1913 0 : CHK_PRT_RET(algOperator->CalNumBlocks(algName, param, numBlocks, aivCoreLimit) != HCCL_SUCCESS,
1914 : HCCL_ERROR("[%s] CalNumBlocks failed", __func__),
1915 : HCCL_E_PARA);
1916 0 : SetWorkflowMode(originWorkflowMode);
1917 0 : return HCCL_SUCCESS;
1918 0 : }
1919 :
1920 1 : HcclResult HcclCommunicator::HcclGetAlgExecParam(const std::string &tag, HcclCMDType opType, u64 count, void *inputPtr, void *outputPtr,
1921 : bool clearEnable, HcclDataType dataType, HcclReduceOp op, void *&commContext, u64 &len, u32 aivCoreLimit)
1922 : {
1923 : /* 将Host申请和注册好的资源,传给AICPU */
1924 : // 1\ algName 从getstr里某一个名字里获取出来(要防止名字重复) commContext & len 从 response里拿
1925 : // 2\ rtmemcopy 先获取一下algoperator对象,用这个调用getalgxxx
1926 1 : AivSuperKernelArgs aivSuperKernelArgs;
1927 1 : SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB);
1928 :
1929 1 : OpParam param;
1930 1 : param.DataDes.count = count;
1931 1 : param.DataDes.dataType = dataType;
1932 1 : param.reduceType = op;
1933 1 : param.tag = tag;
1934 1 : param.inputPtr = inputPtr;
1935 1 : param.outputPtr = outputPtr;
1936 1 : param.opType = opType;
1937 : u64 totalSize;
1938 1 : std::vector<u64> sendCountMatrix(userRankSize_ * userRankSize_, count);
1939 1 : if (opType == HcclCMDType::HCCL_CMD_ALLTOALL) {
1940 0 : param.All2AllDataDes.sendType = dataType;
1941 0 : param.All2AllDataDes.recvType = dataType;
1942 0 : param.All2AllDataDes.sendCount = count;
1943 0 : param.All2AllDataDes.sendCountMatrix =static_cast<void *>(sendCountMatrix.data());
1944 : }
1945 :
1946 1 : if (opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER || opType == HcclCMDType::HCCL_CMD_ALLTOALL) {
1947 0 : totalSize = count * SIZE_TABLE[dataType] * userRankSize_;
1948 : } else {
1949 1 : totalSize = count * SIZE_TABLE[dataType]; // allreduce就是输入
1950 : }
1951 1 : param.inputSize = totalSize;
1952 1 : std::unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(opType);
1953 1 : CHK_SMART_PTR_NULL(algOperator);
1954 1 : std::string algName;
1955 1 : AlgResourceResponse algResResponse;
1956 1 : std::string newTag;
1957 1 : ResourceLimit limit;
1958 1 : limit.ifLimit = true;
1959 1 : limit.aivCoreLimit = aivCoreLimit;
1960 1 : AlgDesc algDesc;
1961 1 : algDesc.isLastSelect = true;
1962 1 : CHK_RET(algOperator->SelectAlg(param.tag, param, limit, algName, algDesc, newTag));
1963 :
1964 : // 资源创建
1965 1 : InsertNewTagToTagMap(newTag, param.tag);
1966 1 : if (resMap_.find(newTag) == resMap_.end()) {
1967 1 : HCCL_INFO("[HcclCoommunicator][HcclAllocRes] algName[%s], alloc new res", algName.c_str());
1968 1 : AlgResourceRequest resRequest;
1969 1 : CHK_RET(algOperator->CalcResRequest(algName, param, resRequest)); // [重构建议] 计算和alloc可以拆开
1970 1 : CHK_RET(AllocAlgResource(newTag, opType, param, resRequest, resMap_[newTag]));
1971 1 : CHK_RET(algOperator->PrepareCommInfoToDevice(algName, resMap_[newTag]));
1972 : // 暂不作心跳注册
1973 1 : }
1974 :
1975 1 : CHK_RET(algOperator->GetAivExecParam(algName, param, resMap_[newTag], aivSuperKernelArgs));
1976 :
1977 : // gettag
1978 1 : HCCL_INFO("SPK, rank %llu.", userRank_);
1979 : u32 numBlocks;
1980 1 : CHK_PRT_RET(algOperator->CalNumBlocks(algName, param, numBlocks, aivCoreLimit) != HCCL_SUCCESS,
1981 : HCCL_ERROR("[%s] CalNumBlocks failed", __func__),
1982 : HCCL_E_PARA);
1983 1 : if (clearEnable) {
1984 1 : aivOffloadTag_ = 1;
1985 : }
1986 1 : GetAivTag(algDesc.aivTagNum, false, aivSuperKernelArgs.tag); // workflowmode为图模式
1987 1 : aivSuperKernelArgs.numBlocks = numBlocks;
1988 :
1989 1 : HCCL_INFO("SPK, Tag %llu aivCoreLimit %u, numBlocks %llu.", aivSuperKernelArgs.tag,
1990 : aivCoreLimit, aivSuperKernelArgs.numBlocks);
1991 : // clearenable
1992 : // 拷贝到Device
1993 1 : SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
1994 :
1995 1 : void *sendAlgParamMemPtr = nullptr;
1996 : // alloc device 地址
1997 1 : CHK_RET(hrtMalloc(&sendAlgParamMemPtr, sizeof(AivSuperKernelArgs)));
1998 1 : HCCL_INFO("SPK sendalgparam %p.", sendAlgParamMemPtr);
1999 :
2000 1 : HcclResult hcclRet = hrtMemSyncCopy(sendAlgParamMemPtr, sizeof(AivSuperKernelArgs),
2001 : &aivSuperKernelArgs, sizeof(AivSuperKernelArgs),
2002 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE);
2003 1 : if (hcclRet != HCCL_SUCCESS) {
2004 0 : HCCL_ERROR("[HcclCommunicator][%s]hrtMemSyncCopy error, ret[%d]", __func__, hcclRet);
2005 0 : CHK_RET(hrtFree(sendAlgParamMemPtr));
2006 0 : return hcclRet;
2007 : }
2008 1 : commContext = sendAlgParamMemPtr;
2009 1 : len = sizeof(AivSuperKernelArgs);
2010 1 : return HCCL_SUCCESS;
2011 1 : }
2012 :
2013 6 : HcclResult HcclCommunicator::GetAivTag(s32 tagNum, bool isCapture, s32 &aivTag)
2014 : {
2015 6 : bool useOpbaseFlag = (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE && !isCapture);
2016 6 : if (useOpbaseFlag) {
2017 2 : aivTag = aivOpbaseTag_;
2018 2 : aivOpbaseTag_ = GetNextAivTag(aivOpbaseTag_, tagNum);
2019 : } else {
2020 4 : aivTag = aivOffloadTag_;
2021 4 : aivOffloadTag_ = GetNextAivTag(aivOffloadTag_, tagNum);
2022 : }
2023 6 : return HCCL_SUCCESS;
2024 : }
2025 :
2026 2 : HcclResult HcclCommunicator::CheckDeviceType(const DevType deviceType) const
2027 : {
2028 2 : if ((deviceType >= DevType::DEV_TYPE_COUNT) || (deviceType < DevType::DEV_TYPE_910)) {
2029 2 : HCCL_ERROR("[Check][DeviceType]errNo[0x%016llx] device Type[%d] out of range[%d, %d]",
2030 : HCCL_ERROR_CODE(HCCL_E_PARA), deviceType, DevType::DEV_TYPE_910, DevType::DEV_TYPE_NOSOC);
2031 2 : return HCCL_E_PARA;
2032 : }
2033 0 : return HCCL_SUCCESS;
2034 : }
2035 :
2036 43 : HcclResult HcclCommunicator::CheckReductionOp(const HcclReduceOp op) const
2037 : {
2038 43 : if ((op >= HCCL_REDUCE_RESERVED) || (op < HCCL_REDUCE_SUM)) {
2039 1 : HCCL_ERROR("[Check][ReductionOp]errNo[0x%016llx] op:[%d] not supported", HCCL_ERROR_CODE(HCCL_E_PARA), op);
2040 1 : return HCCL_E_PARA;
2041 : }
2042 42 : return HCCL_SUCCESS;
2043 : }
2044 :
2045 131 : HcclResult HcclCommunicator::CheckUserRank(const u32 userRank) const
2046 : {
2047 131 : if (userRankSize_ <= userRank) {
2048 3 : HCCL_ERROR("[Check][UserRank]errNo[0x%016llx] userRank:[%u] is out of range[0 ~ %u]",
2049 : HCCL_ERROR_CODE(HCCL_E_PARA), userRank, userRankSize_);
2050 3 : return HCCL_E_PARA;
2051 : }
2052 128 : return HCCL_SUCCESS;
2053 : }
2054 :
2055 86 : HcclResult HcclCommunicator::CheckCount(const u64 count) const
2056 : {
2057 86 : if (count > SYS_MAX_COUNT) {
2058 1 : HCCL_ERROR("[Check][Count]errNo[0x%016llx] count[%llu] is invalid(bigger than MAX count[%llu])",
2059 : HCCL_ERROR_CODE(HCCL_E_PARA), count, SYS_MAX_COUNT);
2060 1 : return HCCL_E_PARA;
2061 : }
2062 85 : return HCCL_SUCCESS;
2063 : }
2064 :
2065 0 : HcclResult HcclCommunicator::GetGroupRanksInfo(const std::vector<u32> &groupRanks, std::vector<RankInfo> &ranksInfo)
2066 : {
2067 0 : ranksInfo.clear();
2068 0 : std::vector<RankInfo> tmpRankInfoList;
2069 0 : tmpRankInfoList.assign(rankInfoList_.begin(), rankInfoList_.end());
2070 :
2071 0 : for (u32 index = 0; index < groupRanks.size(); index++) {
2072 0 : if (tmpRankInfoList.size() <= groupRanks[index]) {
2073 0 : HCCL_ERROR("[Get][GroupRanksInfo]errNo[0x%016llx] groupRanks[%u]=[%u], >= rankinfolist size[%zu]",
2074 : HCCL_ERROR_CODE(HCCL_E_PARA), index, groupRanks[index], tmpRankInfoList.size());
2075 0 : return HCCL_E_PARA;
2076 : }
2077 0 : tmpRankInfoList[groupRanks[index]].userRank = index;
2078 0 : ranksInfo.push_back(tmpRankInfoList[groupRanks[index]]);
2079 0 : HCCL_DEBUG("index: %d userRank: %dhost ip: %s host port: %u dev phy id: %d serverIdx:%d",
2080 : index,
2081 : tmpRankInfoList[groupRanks[index]].userRank,
2082 : tmpRankInfoList[groupRanks[index]].hostIp.GetReadableAddress(),
2083 : tmpRankInfoList[groupRanks[index]].hostPort,
2084 : tmpRankInfoList[groupRanks[index]].devicePhyId,
2085 : tmpRankInfoList[groupRanks[index]].serverIdx);
2086 : }
2087 :
2088 : // 按rank id从小到大的顺序返回
2089 0 : std::sort(ranksInfo.begin(), ranksInfo.end(), CompareWithUserRank);
2090 :
2091 0 : for (u32 index = 0; index < ranksInfo.size(); ++index) {
2092 0 : if (index != ranksInfo[index].userRank) {
2093 0 : HCCL_ERROR("[Get][GroupRanksInfo]errNo[0x%016llx] index[%u] != user rank[%u]",
2094 : HCCL_ERROR_CODE(HCCL_E_PARA), index, ranksInfo[index].userRank);
2095 0 : return HCCL_E_PARA;
2096 : }
2097 : }
2098 0 : return HCCL_SUCCESS;
2099 0 : }
2100 :
2101 0 : HcclResult HcclCommunicator::GetGroupCommonData(WorldGroupInfo &groupCommonData) const
2102 : {
2103 0 : groupCommonData.inlineReduceSwitchOn = inlineReduceSwitchOn_;
2104 0 : groupCommonData.deviceType = deviceType_;
2105 0 : groupCommonData.deviceLogicId = deviceLogicId_;
2106 0 : groupCommonData.profilingInitiated = profilingInitiated_;
2107 0 : groupCommonData.serverId = serverId_;
2108 0 : groupCommonData.phyIdNicInfoMap = rankDevicePhyIdNicInfoMap_;
2109 0 : groupCommonData.worldRankInfoList = rankInfoList_;
2110 0 : groupCommonData.ranksPort = nicRanksPort_;
2111 0 : groupCommonData.vnicRanksPort = vnicRanksPort_;
2112 0 : groupCommonData.useSuperPodMode = useSuperPodMode_;
2113 0 : groupCommonData.devPortSwitchOn = commPortConfig_.devPortSwitchOn;
2114 0 : return HCCL_SUCCESS;
2115 : }
2116 :
2117 91 : HcclResult HcclCommunicator::GetWorkspaceMemSize(const std::string &opType, u64 count, HcclDataType dataType,
2118 : u32 &rankSize, u64 &memSize, DevType &deviceType) const
2119 : {
2120 91 : return workSpaceRes_->GetWorkspaceMemSize(opType, count, dataType, rankSize, memSize, deviceType);
2121 : }
2122 :
2123 24 : DeviceMem HcclCommunicator::GetWorkspaceScracthMem(const std::string &tag, u64 allocMemSize)
2124 : {
2125 24 : return workSpaceRes_->AllocDeviceMem(tag, allocMemSize);
2126 : }
2127 :
2128 15 : std::vector<Stream> HcclCommunicator::GetWorkspaceSubStreams(const std::string &tag, u32 num)
2129 : {
2130 15 : return workSpaceRes_->AllocSlaveStreams(tag, num);
2131 : }
2132 :
2133 487 : HcclResult HcclCommunicator::InitProfiling()
2134 : {
2135 487 : if (static_cast<s32>(devicePhyId_) == HOST_DEVICE_ID) {
2136 0 : HCCL_ERROR("[Init][Profiling]not support cpu rank");
2137 0 : return HCCL_E_NOT_SUPPORT;
2138 : }
2139 487 : CHK_PRT_RET(profilingInitiated_, HCCL_DEBUG("Profiling plugin has already been Initiated."), HCCL_SUCCESS);
2140 :
2141 487 : if (profilingMode_ != HcomProfilingMode::PROFILING_OPEN && GetExternalInputProfilingMode()) {
2142 0 : profilingMode_ = HcomProfilingMode::PROFILING_OPEN;
2143 0 : profilingOption_ = GetExternalInputProfilingOption();
2144 : }
2145 489 : HCCL_INFO("profiling config information:options[%s], mode[%d]", profilingOption_.c_str(), profilingMode_);
2146 :
2147 : // profilingInitiated_会广播给所有子通信域,用于避免taskInfoSaver的重复初始化
2148 488 : profilingInitiated_ = true;
2149 : // isExecuteProfilingInit_用于记录本impl是否执行了taskInfoSaver的初始化,用于进行对应的释放
2150 488 : isExecuteProfilingInit_ = true;
2151 488 : return HCCL_SUCCESS;
2152 : }
2153 :
2154 487 : HcclResult HcclCommunicator::DeinitProfiling()
2155 : {
2156 487 : CHK_PRT_RET(!profilingInitiated_, HCCL_DEBUG("Profiling plugin has not been Initiated"), HCCL_SUCCESS);
2157 487 : profilingInitiated_ = false;
2158 487 : HCCL_INFO("Profiling is deinitiated.");
2159 488 : return HCCL_SUCCESS;
2160 : }
2161 :
2162 488 : HcclResult HcclCommunicator::RegistTaskExceptionHandler() const
2163 : {
2164 488 : CHK_RET(TaskExceptionHandler::Init());
2165 488 : return HCCL_SUCCESS;
2166 : }
2167 :
2168 803 : HcclResult HcclCommunicator::UnRegistTaskExceptionHandler() const
2169 : {
2170 803 : CHK_RET(TaskExceptionHandler::DeInit());
2171 804 : return HCCL_SUCCESS;
2172 : }
2173 :
2174 0 : HcclResult HcclCommunicator::GetInCCLbuffer(void *&buffer, u64 &size)
2175 : {
2176 0 : return cclBufferManager_.GetInCCLbuffer(buffer, size);
2177 : }
2178 :
2179 0 : HcclResult HcclCommunicator::GetOutCCLbuffer(void *&buffer, u64 &size)
2180 : {
2181 0 : return cclBufferManager_.GetOutCCLbuffer(buffer, size);
2182 : }
2183 :
2184 0 : void HcclCommunicator::ReleaseCommCCLbuffer()
2185 : {
2186 0 : cclBufferManager_.ReleaseCommCCLbuffer();
2187 0 : }
2188 :
2189 0 : HcclResult HcclCommunicator::ReleaseCommInfos()
2190 : {
2191 0 : if (implAlg_ != nullptr) {
2192 0 : return implAlg_->ReleaseCommInfos();
2193 : }
2194 0 : return HCCL_SUCCESS;
2195 : }
2196 :
2197 309 : HcclResult HcclCommunicator::InitProfiler()
2198 : {
2199 309 : profilerManager_.reset(new (std::nothrow) ProfilerManager(devicePhyId_, deviceLogicId_, realUserRank_, userRankSize_));
2200 309 : CHK_SMART_PTR_NULL(profilerManager_);
2201 309 : HcclResult ret = profilerManager_->InitProfiler();
2202 309 : CHK_PRT_RET((ret != HCCL_SUCCESS), HCCL_ERROR("[BASE][InitProfiler]profilerManager_ InitProfiler failed."),
2203 : HCCL_E_PARA);
2204 :
2205 309 : HCCL_INFO("[BASE][InitProfiler]Register CtrlCallBack success.");
2206 309 : return HCCL_SUCCESS;
2207 : }
2208 :
2209 74 : HcclResult HcclCommunicator::CreateCommCCLbuffer()
2210 : {
2211 : // user mem和CCL buffer互斥,不支持同时使用
2212 74 : if (isUserMemRegisted_) {
2213 0 : HCCL_ERROR("[HcclCommunicator][%s]tag[%s]The user mem has been registered, "
2214 : "does not support create CCL Buffer.", __func__, identifier_.c_str());
2215 0 : return HCCL_E_NOT_SUPPORT;
2216 : }
2217 74 : return cclBufferManager_.CreateCommCCLbuffer(cclBuffName_);
2218 : }
2219 :
2220 242 : HcclResult HcclCommunicator::InitCCLbuffer(u64 inCCLbufferSize, u64 outCCLbufferSize)
2221 : {
2222 242 : return cclBufferManager_.InitCCLbuffer(inCCLbufferSize, outCCLbufferSize);
2223 : }
2224 :
2225 180 : u32 HcclCommunicator::GetLocalNicPort(NicType nicType)
2226 : {
2227 180 : u32 port = HCCL_INVALID_PORT;
2228 180 : if (nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST) {
2229 0 : return GetHostPort(devicePhyId_);
2230 : }
2231 : // isUseRankPort_在ranksPort初始化时一同配置:1. 异构场景 2. 开启device侧端口配置
2232 : // groupRanksPort_为空说明此时处于全局通信域,要从ranksPort_取监听端口;否则取groupRanksPort_
2233 180 : bool devicePortSwitchOn = commPortConfig_.devPortSwitchOn;
2234 180 : if (nicType == NicType::HOST_NIC_TYPE) {
2235 0 : port = GetHostPort(devicePhyId_);
2236 180 : } else if (devicePortSwitchOn && nicType == NicType::VNIC_TYPE) {
2237 : // vnic ports仅在开启device侧端口配置时单独配置
2238 0 : std::vector<u32> &ranksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
2239 0 : port = GetNicPort(devicePhyId_, ranksPorts, userRank_, isUseRankPort_);
2240 0 : } else {
2241 : // 1. 开启device侧端口配置时的nic port时使用ranksPorts
2242 : // 2. 异构场景使用ranksPorts
2243 : // 3. 其余场景场景isUseRankPort_应当为false,使用默认port
2244 180 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
2245 180 : port = GetNicPort(devicePhyId_, ranksPorts, userRank_, isUseRankPort_);
2246 : }
2247 180 : HCCL_INFO("[HcclCommunicator][GetLocalNicPort] nicType[%u], devicePortSwitchOn[%u], isUseRankPort[%u], "
2248 : "get port[%u], devId[%u]",
2249 : nicType, devicePortSwitchOn, isUseRankPort_, port, devicePhyId_);
2250 180 : return port;
2251 : }
2252 :
2253 1 : HcclResult HcclCommunicator::CheckOneSidedBackupAndSetDevId(u32 &backupDevPhyId, u32 &backupDevLogicId,
2254 : std::vector<HcclIpAddress> &localIpList, bool &isOneSidedTaskAndBackupInitA3)
2255 : {
2256 1 : if (!IsOneSidedIdentifier(identifier_)) {
2257 1 : isOneSidedTaskAndBackupInitA3 = false;
2258 1 : HCCL_INFO("[%s] comm[%s] is not one sided comm.", __func__, identifier_.c_str());
2259 1 : return HCCL_SUCCESS;
2260 : }
2261 0 : DevType deviceType = DevType::DEV_TYPE_COUNT;
2262 0 : CHK_RET(hrtGetDeviceType(deviceType));
2263 0 : if (deviceType != DevType::DEV_TYPE_910_93) {
2264 0 : isOneSidedTaskAndBackupInitA3 = false;
2265 0 : HCCL_INFO("[HcclCommunicator::CheckOneSidedBackupAndSetDevId] DeviceType[%d] is not 910_93, one sided backup not support",
2266 : static_cast<u32>(deviceType));
2267 0 : return HCCL_SUCCESS;
2268 : }
2269 0 : CHK_RET(hrtGetPairDevicePhyId(devicePhyId_, backupDevPhyId));
2270 :
2271 0 : std::vector<HcclIpAddress> backupIpList;
2272 0 : std::vector<std::vector<HcclIpAddress>> chipDeviceIPs;
2273 0 : CHK_RET(hrtRaGetDeviceAllNicIP(chipDeviceIPs));
2274 0 : u32 ipIdex = 1U - (devicePhyId_ % 2U);
2275 0 : std::copy_if(chipDeviceIPs[ipIdex].begin(), chipDeviceIPs[ipIdex].end(),
2276 0 : std::back_inserter(backupIpList), [](const HcclIpAddress& ip) { return !ip.IsIPv6(); });
2277 0 : HCCL_INFO("devicePhysicID[%u], backupDeviceId[%d], backupDeviceIP[0]:[%s], devIpAddr_[%s], ",
2278 : devicePhyId_, backupDevPhyId, backupIpList[0].GetReadableAddress(), devIpAddr_[0].GetReadableAddress());
2279 0 : CHK_RET(hrtRaGetDeviceIP(devicePhyId_, localIpList));
2280 0 : auto equalToLocal = [this](const HcclIpAddress &entry) { return entry == devIpAddr_[0];};
2281 0 : isOneSidedTaskAndBackupInitA3 = any_of(backupIpList.begin(), backupIpList.end(), equalToLocal) &&
2282 0 : !any_of(localIpList.begin(), localIpList.end(), equalToLocal);
2283 0 : if (isOneSidedTaskAndBackupInitA3) {
2284 0 : CHK_RET(hrtGetDeviceIndexByPhyId(backupDevPhyId, backupDevLogicId));
2285 : }
2286 :
2287 0 : HCCL_INFO("[HcclCommunicator::CheckOneSidedBackupAndSetDevId] isOneSidedTaskAndBackupInitA3[%s]",
2288 : isOneSidedTaskAndBackupInitA3 ? "true" : "false");
2289 0 : return HCCL_SUCCESS;
2290 0 : }
2291 :
2292 0 : HcclResult HcclCommunicator::OneSidedBackupInitNetResource(HcclNetDevCtx &nicPortBackUpCtx, u32 &backupDevPhyId,
2293 : u32 &backupDevLogicId, std::vector<HcclIpAddress> &localIpList)
2294 : {
2295 0 : devBackupIpAddr_[0] = devIpAddr_[0];
2296 0 : deviceBackUpPhyId_ = backupDevPhyId;
2297 0 : deviceBackUpLogicId_ = backupDevLogicId;
2298 0 : CHK_RET(HcclNetInit(NICDeployment::NIC_DEPLOYMENT_DEVICE, backupDevPhyId, backupDevLogicId, false, true));
2299 0 : HCCL_INFO("[HcclCommunicator::OneSidedBackupInitNetResource] OpenDev with backupDevPhyId[%d], backupDevLogicId[%d], localIpList[%s], backupIp[%s]",
2300 : backupDevPhyId, backupDevLogicId, localIpList[0].GetReadableAddress(), devIpAddr_[0].GetReadableAddress());
2301 0 : CHK_RET(HcclNetOpenDev(&nicPortBackUpCtx, NicType::DEVICE_NIC_TYPE, backupDevPhyId, backupDevLogicId, devIpAddr_[0], localIpList[0]));
2302 0 : return HCCL_SUCCESS;
2303 : }
2304 :
2305 0 : HcclResult HcclCommunicator::OneSidedBackupServerInit(HcclNetDevCtx &nicPortBackUpCtx)
2306 : {
2307 0 : u32 backupPort = HCCL_INVALID_PORT;
2308 0 : for (const auto &rankInfo : rankInfoList_) {
2309 0 : if (rankInfo.userRank == userRank_) {
2310 0 : backupPort = rankInfo.deviceNicPort;
2311 : }
2312 : }
2313 0 : CHK_RET(socketManager_->ServerInit(nicPortBackUpCtx, backupPort));
2314 0 : return HCCL_SUCCESS;
2315 : }
2316 :
2317 0 : HcclResult HcclCommunicator::InitDevicePrimaryNic(bool isMC2ReInit, bool isOneSidedTaskAndBackupInitA3)
2318 : {
2319 0 : std::shared_ptr<HcclSocket> &devNicSocket = commPortConfig_.devNicListen.first;
2320 0 : if (devNicSocket && !isOneSidedTaskAndBackupInitA3) {
2321 0 : HcclNetDevCtx &devNicCtx = commPortConfig_.devNicListen.second;
2322 0 : CHK_PTR_NULL(devNicCtx);
2323 0 : netDevCtxMap_.insert(std::make_pair(devNicSocket->GetLocalIp(), devNicCtx));
2324 0 : CHK_RET(socketManager_->ServerInit(devNicCtx, devNicSocket->GetLocalPort()));
2325 0 : commPortConfig_.devNicListen.second = nullptr;
2326 0 : HCCL_INFO("[HcclCommunicator][InitNic] init nic with listened socket success, "
2327 : "listened ip[%s] port[%u]",
2328 : devNicSocket->GetLocalIp().GetReadableAddress(), devNicSocket->GetLocalPort());
2329 0 : } else if (!isOneSidedTaskAndBackupInitA3) {
2330 0 : u32 port = GetLocalNicPort(NicType::DEVICE_NIC_TYPE);
2331 0 : u32 nicNum = devIpAddr_.size();
2332 0 : for (u32 i = 0; i < nicNum; i++) {
2333 0 : if (devIpAddr_[i].IsInvalid()) {
2334 0 : HCCL_INFO("[Init][Nic]nic num[%u] deviceip is invalid, total nicNum[%u]", i, nicNum);
2335 0 : continue;
2336 : }
2337 : HcclNetDevCtx nicPortCtx;
2338 0 : CHK_RET(HcclNetOpenDev(&nicPortCtx, NicType::DEVICE_NIC_TYPE, devicePhyId_, deviceLogicId_, devIpAddr_[i]));
2339 0 : CHK_PTR_NULL(nicPortCtx);
2340 0 : netDevCtxMap_.insert(std::make_pair(devIpAddr_[i], nicPortCtx));
2341 0 : CHK_RET(socketManager_->ServerInit(nicPortCtx, port));
2342 0 : HCCL_INFO("[HcclCommunicator][InitNic] init nic with ip[%s] port[%u] success",
2343 : devIpAddr_[i].GetReadableAddress(), port);
2344 : }
2345 : }
2346 0 : return HCCL_SUCCESS;
2347 : }
2348 :
2349 0 : HcclResult HcclCommunicator::InitDeviceBackupNic(u32 backupDevPhyId, u32 backupDevLogicId,
2350 : std::vector<HcclIpAddress> &localIpList, bool isOneSidedTaskAndBackupInitA3)
2351 : {
2352 0 : if (IsEnableBackupLink() || isOneSidedTaskAndBackupInitA3) {
2353 0 : std::shared_ptr<HcclSocket> &backupNicSocket = commPortConfig_.backupDevNicListen.first;
2354 0 : if (backupNicSocket) {
2355 0 : HcclNetDevCtx &backupNicCtx = commPortConfig_.backupDevNicListen.second;
2356 0 : CHK_PTR_NULL(backupNicCtx);
2357 0 : netDevCtxMap_.insert(std::make_pair(backupNicSocket->GetLocalIp(), backupNicCtx));
2358 0 : CHK_RET(socketManager_->ServerInit(backupNicCtx, backupNicSocket->GetLocalPort()));
2359 0 : commPortConfig_.backupDevNicListen.second = nullptr;
2360 0 : HCCL_INFO("[HcclCommunicator][InitNic] init backup nic with listened socket success, "
2361 : "listened ip[%s] port[%u]",
2362 : backupNicSocket->GetLocalIp().GetReadableAddress(), backupNicSocket->GetLocalPort());
2363 : } else {
2364 : HcclNetDevCtx nicPortBackUpCtx;
2365 0 : if (isOneSidedTaskAndBackupInitA3) {
2366 0 : CHK_RET(OneSidedBackupInitNetResource(nicPortBackUpCtx, backupDevPhyId, backupDevLogicId, localIpList));
2367 : } else {
2368 0 : CHK_RET(HcclNetOpenDev(&nicPortBackUpCtx, NicType::DEVICE_NIC_TYPE, deviceBackUpPhyId_,
2369 : deviceBackUpLogicId_, devBackupIpAddr_[0], devIpAddr_[0]));
2370 : }
2371 0 : CHK_PTR_NULL(nicPortBackUpCtx);
2372 0 : netDevCtxMap_.insert(std::make_pair(devBackupIpAddr_[0], nicPortBackUpCtx));
2373 0 : if (isOneSidedTaskAndBackupInitA3) {
2374 0 : CHK_RET(OneSidedBackupServerInit(nicPortBackUpCtx));
2375 : } else {
2376 0 : CHK_RET(socketManager_->ServerInit(nicPortBackUpCtx, devBackupPort_));
2377 : }
2378 0 : HCCL_DEBUG("[%s]finish backup ServerInit, deviceBackUpPhyId_[%u], deviceBackUpLogicId_[%u], "
2379 : "devBackupIpAddr_[%s], devBackupPort_[%u], nicDeployment_[%d], IsEnableBackupLink[%d], "
2380 : "netDevCtxMap_.size[%d]",
2381 : __func__, deviceBackUpPhyId_, deviceBackUpLogicId_, devBackupIpAddr_[0].GetReadableAddress(),
2382 : devBackupPort_, nicDeployment_, IsEnableBackupLink(), netDevCtxMap_.size());
2383 0 : HCCL_INFO("[HcclCommunicator][InitNic] init backup nic with ip[%s] port[%u] success",
2384 : devBackupIpAddr_[0].GetReadableAddress(), devBackupPort_);
2385 : }
2386 : }
2387 0 : return HCCL_SUCCESS;
2388 : }
2389 :
2390 0 : HcclResult HcclCommunicator::InitNicDeviceDeploy(bool isMC2ReInit, u32 backupDevPhyId, u32 backupDevLogicId,
2391 : std::vector<HcclIpAddress> &localIpList, bool isOneSidedTaskAndBackupInitA3)
2392 : {
2393 0 : CHK_RET(InitDevicePrimaryNic(isMC2ReInit, isOneSidedTaskAndBackupInitA3));
2394 0 : attrCollector_.GenUsedRdmaLevel0();
2395 0 : isUsedRdmaLevel0_ = attrCollector_.GetUsedRdmaLevel0();
2396 0 : CHK_RET(InitDeviceBackupNic(backupDevPhyId, backupDevLogicId, localIpList, isOneSidedTaskAndBackupInitA3));
2397 0 : return HCCL_SUCCESS;
2398 : }
2399 :
2400 0 : HcclResult HcclCommunicator::InitNicHostDeploy()
2401 : {
2402 0 : u32 port = GetLocalNicPort(NicType::HOST_NIC_TYPE);
2403 0 : CHK_PRT_RET((hostIp_.IsInvalid()), HCCL_ERROR("[Init][Nic] host ip is invalid when NIC "
2404 : "deployment is host. "),
2405 : HCCL_E_PARA);
2406 0 : attrCollector_.GenUsedRdmaLevel0();
2407 0 : isUsedRdmaLevel0_ = attrCollector_.GetUsedRdmaLevel0();
2408 0 : u32 devicePhyID = (static_cast<s32>(devicePhyId_) == HOST_DEVICE_ID) ? 0 : devicePhyId_;
2409 :
2410 0 : u32 i = 0;
2411 : HcclNetDevCtx nicPortCtx;
2412 0 : for (i = 0; i < devIpAddr_.size(); i++) {
2413 0 : if (devIpAddr_[i].IsInvalid()) {
2414 0 : HCCL_INFO("[Init][Nic]nic num[%u] deviceip is invalid, total nicNum[%u]", i, devIpAddr_.size());
2415 0 : continue;
2416 : }
2417 0 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
2418 0 : port = GetNicPort(devicePhyId_, ranksPorts, userRank_, isUseRankPort_);
2419 0 : CHK_RET(HcclNetOpenDev(&nicPortCtx, NicType::HOST_NIC_TYPE, devicePhyId_, deviceLogicId_, devIpAddr_[i]));
2420 0 : CHK_PTR_NULL(nicPortCtx);
2421 0 : netDevCtxMap_.insert(std::make_pair(devIpAddr_[i], nicPortCtx));
2422 0 : HcclNetDevSetProtoType(nicPortCtx, HCCL_PROTO_TYPE_ROCE);
2423 0 : break;
2424 : }
2425 :
2426 0 : if (i == devIpAddr_.size()) {
2427 0 : port = GetLocalNicPort(NicType::HOST_NIC_TYPE);
2428 0 : CHK_RET(HcclNetOpenDev(&nicPortCtx, NicType::HOST_NIC_TYPE, devicePhyId_, deviceLogicId_, hostIp_));
2429 0 : CHK_PTR_NULL(nicPortCtx);
2430 0 : netDevCtxMap_.insert(std::make_pair(hostIp_, nicPortCtx));
2431 : }
2432 0 : HCCL_INFO("[Init][Nic], hostPort[%u], devicePhyID[%u]", port, devicePhyID);
2433 0 : CHK_RET(socketManager_->ServerInit(nicPortCtx, port));
2434 0 : return HCCL_SUCCESS;
2435 : }
2436 :
2437 1 : HcclResult HcclCommunicator::InitNic(bool isMC2ReInit)
2438 : {
2439 1 : if (!GetExternalInputIntraRoceSwitch() && servRankInfo_.size() == 1 && isDiffDeviceModule_ && !isMC2ReInit) {
2440 0 : return HCCL_SUCCESS;
2441 : }
2442 1 : u32 backupDevPhyId = INVALID_INT;
2443 1 : u32 backupDevLogicId = INVALID_INT;
2444 1 : bool isOneSidedTaskAndBackupInitA3 = false;
2445 1 : vector<HcclIpAddress> localIpList;
2446 1 : CHK_RET(CheckOneSidedBackupAndSetDevId(backupDevPhyId, backupDevLogicId, localIpList, isOneSidedTaskAndBackupInitA3));
2447 :
2448 1 : if (nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_DEVICE) {
2449 0 : CHK_RET(InitNicDeviceDeploy(isMC2ReInit, backupDevPhyId, backupDevLogicId, localIpList, isOneSidedTaskAndBackupInitA3));
2450 1 : } else if (nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST) {
2451 0 : CHK_RET(InitNicHostDeploy());
2452 : } else {
2453 1 : HCCL_ERROR("[Init][Nic]nic deployment[%d] is not supported", nicDeployment_);
2454 1 : return HCCL_E_PARA;
2455 : }
2456 0 : isNeedInitNic_ = true;
2457 0 : attrCollector_.SetNeedInitNicFlag(isNeedInitNic_);
2458 0 : nicInitialized_++;
2459 0 : return HCCL_SUCCESS;
2460 1 : }
2461 :
2462 0 : HcclResult HcclCommunicator::DeinitNicHostDeploy()
2463 : {
2464 0 : u32 port = GetLocalNicPort(NicType::HOST_NIC_TYPE);
2465 0 : CHK_PRT_RET((hostIp_.IsInvalid()), HCCL_ERROR("[DeInit][Nic] host ip is invalid when NIC "
2466 : "deployment is host. "),
2467 : HCCL_E_PARA);
2468 :
2469 0 : u32 i = 0;
2470 0 : for (i = 0; i < devIpAddr_.size(); i++) {
2471 0 : if (devIpAddr_[i].IsInvalid()) {
2472 0 : HCCL_INFO("[Init][Nic]nic num[%u] deviceip is invalid, total nicNum[%u]", i, devIpAddr_.size());
2473 0 : continue;
2474 : }
2475 0 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
2476 0 : port = GetNicPort(devicePhyId_, ranksPorts, userRank_, isUseRankPort_);
2477 :
2478 0 : CHK_RET(socketManager_->ServerDeInit(netDevCtxMap_[devIpAddr_[i]], port));
2479 0 : HcclNetCloseDev(netDevCtxMap_[devIpAddr_[i]]);
2480 0 : netDevCtxMap_.erase(devIpAddr_[i]);
2481 :
2482 0 : break;
2483 : }
2484 :
2485 0 : if (i == devIpAddr_.size()) {
2486 0 : CHK_RET(socketManager_->ServerDeInit(netDevCtxMap_[hostIp_], port));
2487 0 : HcclNetCloseDev(netDevCtxMap_[hostIp_]);
2488 0 : netDevCtxMap_.erase(hostIp_);
2489 : }
2490 0 : return HCCL_SUCCESS;
2491 : }
2492 :
2493 0 : HcclResult HcclCommunicator::DeinitNic()
2494 : {
2495 0 : if (nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_DEVICE) {
2496 0 : u32 port = GetLocalNicPort(NicType::DEVICE_NIC_TYPE);
2497 0 : u32 nicNum = devIpAddr_.size();
2498 0 : for (u32 i = 0; i < nicNum; i++) {
2499 0 : if (devIpAddr_[i].IsInvalid()) {
2500 0 : HCCL_INFO("continue invalid devIp %s", devIpAddr_[i].GetReadableAddress());
2501 0 : continue;
2502 : }
2503 0 : if (netDevCtxMap_.find(devIpAddr_[i]) == netDevCtxMap_.end()) {
2504 0 : HCCL_INFO("devIp[%s] not found in netDevCtxMap_", devIpAddr_[i].GetReadableAddress());
2505 0 : continue;
2506 : }
2507 0 : CHK_RET(socketManager_->ServerDeInit(netDevCtxMap_[devIpAddr_[i]], port));
2508 : // 最后一次调用才删除netCtx
2509 0 : if (nicInitialized_ - 1 <= 0) {
2510 0 : HcclNetCloseDev(netDevCtxMap_[devIpAddr_[i]]);
2511 0 : netDevCtxMap_.erase(devIpAddr_[i]);
2512 : }
2513 : }
2514 0 : if (IsEnableBackupLink() && netDevCtxMap_.find(devBackupIpAddr_[0]) != netDevCtxMap_.end()) {
2515 : // 超节点 && level2支持重执行 && Aicpu -> 备用网卡 deinit
2516 0 : CHK_RET(socketManager_->ServerDeInit(netDevCtxMap_[devBackupIpAddr_[0]], devBackupPort_));
2517 0 : if (nicInitialized_ - 1 <= 0) {
2518 0 : HcclNetCloseDev(netDevCtxMap_[devBackupIpAddr_[0]]);
2519 0 : netDevCtxMap_.erase(devBackupIpAddr_[0]);
2520 0 : HCCL_DEBUG("[%s]finish backup ServerDeInit devBackupIpAddr_[%s], port[%u], IsEnableBackupLink[%d]",
2521 : __func__, devBackupIpAddr_[0].GetReadableAddress(), devBackupPort_, IsEnableBackupLink());
2522 : }
2523 : }
2524 0 : } else if (nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST) {
2525 0 : CHK_RET(DeinitNicHostDeploy());
2526 : } else {
2527 0 : HCCL_ERROR("[Deinit][Nic]nic deployment[%d] is not supported", nicDeployment_);
2528 0 : return HCCL_E_PARA;
2529 : }
2530 0 : nicInitialized_--;
2531 0 : return HCCL_SUCCESS;
2532 : }
2533 :
2534 517 : HcclResult HcclCommunicator::RegisterRanksToDca()
2535 : {
2536 517 : if (deviceType_ != DevType::DEV_TYPE_910_93 && deviceType_ != DevType::DEV_TYPE_910B) {
2537 324 : HCCL_WARNING("[RegisterRanksToDca] not support deviceType[%d]", deviceType_);
2538 324 : return HCCL_SUCCESS;
2539 : }
2540 193 : CHK_RET(setVnicIpToRankInfoList());
2541 196 : DetectConnectionAnomalies::GetInstance(deviceLogicId_).Init(rankInfoList_, isNeedInitNic_);
2542 196 : return HCCL_SUCCESS;
2543 : }
2544 :
2545 0 : HcclResult HcclCommunicator::AddOpInfoToHeartBeat(const OpInfoDesc &opInfo, const std::string &tag)
2546 : {
2547 0 : if (Is310PDevice() || deviceType_ == DevType::DEV_TYPE_310P3 ||
2548 0 : GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB) {
2549 0 : return HCCL_SUCCESS;
2550 : }
2551 0 : return Heartbeat::GetInstance(deviceLogicId_).AddOpInfoToHeartBeat(identifier_, opInfo, tag);
2552 : }
2553 :
2554 800 : void HcclCommunicator::DeleteOpInfoToHeartBeat()
2555 : {
2556 1596 : if (Is310PDevice() || deviceType_ == DevType::DEV_TYPE_310P3 ||
2557 797 : GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB) {
2558 161 : return ;
2559 : }
2560 639 : for (const auto &tag : hbSendRecvTags_) {
2561 0 : Heartbeat::GetInstance(deviceLogicId_).DeleteOpInfoToHeartBeat(identifier_, tag);
2562 : }
2563 1920 : Heartbeat::GetInstance(deviceLogicId_).DeleteOpInfoToHeartBeat(identifier_, "");
2564 : }
2565 :
2566 21 : HcclResult HcclCommunicator::RegisterToHeartBeat()
2567 : {
2568 21 : if (Is310PDevice() || deviceType_ == DevType::DEV_TYPE_310P3) {
2569 0 : return HCCL_SUCCESS;
2570 : }
2571 21 : u32 localPort = commPortConfig_.devPortSwitchOn ? HCCL_INVALID_PORT : GetLocalNicPort(NicType::DEVICE_NIC_TYPE);
2572 21 : return Heartbeat::GetInstance(deviceLogicId_).RegisterToHeartBeat(userRank_, deviceType_, rankInfoList_, localPort, isNeedInitNic_, identifier_,useSuperPodMode_, isUsedRdmaLevel0_, retryEnable_, IsEnableBackupLink());
2573 : }
2574 :
2575 0 : HcclResult HcclCommunicator::RegisterToHeartBeat(u32 peerRankId, string &tag)
2576 : {
2577 0 : u32 localPort = commPortConfig_.devPortSwitchOn ? HCCL_INVALID_PORT : GetLocalNicPort(NicType::DEVICE_NIC_TYPE);
2578 0 : return Heartbeat::GetInstance(deviceLogicId_).RegisterToHeartBeat(userRank_, deviceType_, rankInfoList_, localPort, isNeedInitNic_, peerRankId, identifier_, tag, useSuperPodMode_, isUsedRdmaLevel0_, retryEnable_, IsEnableBackupLink());
2579 : }
2580 :
2581 801 : void HcclCommunicator::UnRegisterToHeartBeat()
2582 : {
2583 801 : for (auto tag : hbSendRecvTags_) {
2584 0 : Heartbeat::GetInstance(deviceLogicId_).UnRegisterToHeartBeat(deviceType_, identifier_, tag);
2585 0 : }
2586 801 : Heartbeat::GetInstance(deviceLogicId_).UnRegisterToHeartBeat(deviceType_, identifier_);
2587 798 : }
2588 :
2589 804 : void HcclCommunicator::UnRegisterToCommConfiger()
2590 : {
2591 804 : CommConfiger::GetInstance().UnRegisterToCommConfiger(identifier_);
2592 804 : }
2593 :
2594 92 : HcclResult HcclCommunicator::SetGlobalWorkSpace(std::vector<void *> &globalWorkSpaceAddr)
2595 : {
2596 92 : CHK_RET(HcclSetGlobalWorkSpace(dispatcher_, globalWorkSpaceAddr));
2597 92 : return HCCL_SUCCESS;
2598 : }
2599 :
2600 0 : HcclResult HcclCommunicator::GetandClearOverFlowTasks(std::vector<HcclDumpInfo> &hcclDumpInfo)
2601 : {
2602 0 : if (profilerManager_ != nullptr) {
2603 0 : CHK_RET(profilerManager_->GetandClearOverFlowTasks(hcclDumpInfo));
2604 : } else {
2605 0 : HCCL_WARNING("[impl][GetDumpTask] profilerManager_ not set");
2606 : }
2607 0 : return HCCL_SUCCESS;
2608 : }
2609 :
2610 235 : HcclResult HcclCommunicator::GetDeviceId(s32 &deviceId) const
2611 : {
2612 235 : deviceId = deviceLogicId_;
2613 235 : return HCCL_SUCCESS;
2614 : }
2615 :
2616 1 : HcclResult HcclCommunicator::GetCqeError(HcclResult &result)
2617 : {
2618 1 : CHK_RET(Heartbeat::GetInstance(deviceLogicId_).CheckErrorCqe(identifier_, result));
2619 1 : return HCCL_SUCCESS;
2620 : }
2621 :
2622 0 : HcclResult HcclCommunicator::GetOpInconsistentError(HcclResult &result)
2623 : {
2624 0 : CHK_RET(Heartbeat::GetInstance(deviceLogicId_).CheckOpInconsistentError(identifier_, result));
2625 0 : return HCCL_SUCCESS;
2626 : }
2627 :
2628 0 : HcclResult HcclCommunicator::SupportDeterministicOptim(bool &isDeterministicOptim)
2629 : {
2630 0 : CHK_SMART_PTR_NULL(implAlg_);
2631 0 : CHK_RET(implAlg_->SupportDeterministicOptim(isDeterministicOptim));
2632 0 : return HCCL_SUCCESS;
2633 : }
2634 :
2635 0 : HcclResult HcclCommunicator::GetHccsLinkNum(u32 &numHccsLink)
2636 : {
2637 0 : auto iter = pairLinkInfo_.find(static_cast<u32>(LinkTypeInServer::HCCS_TYPE));
2638 0 : if (iter == pairLinkInfo_.end()) {
2639 0 : HCCL_ERROR("[HcclCommunicator][GetHccsLinkNum]HCCS_TYPE is not found");
2640 0 : return HCCL_E_PARA;
2641 : }
2642 0 : numHccsLink = iter->second.size();
2643 0 : return HCCL_SUCCESS;
2644 : }
2645 :
2646 6 : HcclResult HcclCommunicator::AllGather(const std::string &tag, void *inputPtr, void *outputPtr, u64 inputCount,
2647 : HcclDataType dataType, HcclRtStream stream, HcomCollOpInfo *opInfo)
2648 : {
2649 6 : bool isCapture = StreamIsCapture(stream);
2650 :
2651 11 : bool aicpuUnfoldMode = false;
2652 11 : if (EnableAicpuUnfold(isCapture) && (userRankSize_ != 1)) {
2653 0 : aicpuUnfoldMode = true;
2654 : }
2655 :
2656 7 : if (!IsAtomicInit()) {
2657 0 : HCCL_ERROR("[HcclCommunicator][AllGather]errNo[0x%016llx] hccl init must be called before call this function",
2658 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
2659 0 : return HCCL_E_UNAVAIL;
2660 : }
2661 :
2662 9 : Stream streamObj(stream);
2663 11 : CHK_RET(callbackTask_->CallbackRegStream(stream));
2664 :
2665 6 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
2666 5 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
2667 4 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
2668 4 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
2669 :
2670 4 : u32 perDataSize = SIZE_TABLE[dataType];
2671 4 : u64 totalSize = inputCount * perDataSize;
2672 :
2673 4 : OpParam opParam;
2674 4 : opParam.tag = tag;
2675 10 : opParam.inputPtr = inputPtr;
2676 10 : opParam.inputSize = totalSize;
2677 10 : opParam.outputPtr = outputPtr;
2678 10 : opParam.outputSize = totalSize * userRankSize_;
2679 10 : opParam.DataDes.count = inputCount;
2680 10 : opParam.DataDes.dataType = dataType;
2681 10 : opParam.reduceType = HcclReduceOp::HCCL_REDUCE_RESERVED;
2682 10 : opParam.stream = streamObj;
2683 5 : opParam.syncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
2684 5 : opParam.isCapture = isCapture;
2685 5 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
2686 5 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
2687 5 : opParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
2688 :
2689 5 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_ALLGATHER, opParam));
2690 :
2691 5 : return HCCL_SUCCESS;
2692 5 : }
2693 :
2694 1 : HcclResult HcclCommunicator::AllGatherV(const std::string &tag, const void *sendBuf, u64 sendCount, const void *recvBuf,
2695 : const void *recvCounts, const void *rdispls, HcclDataType dataType, HcclRtStream stream)
2696 : {
2697 1 : bool aicpuUnfoldMode = false;
2698 :
2699 1 : if (GetAicpuUnfoldConfig() && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
2700 0 : aicpuUnfoldMode = true;
2701 : }
2702 :
2703 1 : if (!IsAtomicInit()) {
2704 0 : HCCL_ERROR("[HcclCommunicator][AllGatherV]errNo[0x%016llx] hccl init must be called before call this function",
2705 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
2706 0 : return HCCL_E_UNAVAIL;
2707 : }
2708 :
2709 1 : Stream streamObj(stream);
2710 1 : CHK_RET(callbackTask_->CallbackRegStream(stream));
2711 :
2712 1 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
2713 1 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
2714 1 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
2715 1 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
2716 :
2717 1 : u32 perDataSize = SIZE_TABLE[dataType];
2718 1 : u64 totalSize = sendCount * perDataSize;
2719 :
2720 1 : u64 outputSize = 0;
2721 1 : const u64 *counts = static_cast<const u64 *>(recvCounts);
2722 3 : for (u32 i = 0; i < userRankSize_; i++) {
2723 2 : outputSize += counts[i] * perDataSize;
2724 : }
2725 :
2726 1 : bool isCapture = StreamIsCapture(stream);
2727 :
2728 1 : OpParam opParam;
2729 1 : opParam.tag = tag;
2730 1 : opParam.inputPtr = const_cast<void *>(sendBuf);
2731 1 : opParam.inputSize = totalSize;
2732 1 : opParam.outputPtr = const_cast<void *>(recvBuf);
2733 1 : opParam.outputSize = outputSize;
2734 1 : opParam.VDataDes.dataType = dataType;
2735 1 : opParam.VDataDes.counts = const_cast<void *>(recvCounts);
2736 1 : opParam.VDataDes.displs = const_cast<void *>(rdispls);
2737 1 : opParam.reduceType = HcclReduceOp::HCCL_REDUCE_RESERVED;
2738 1 : opParam.stream = streamObj;
2739 1 : opParam.syncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
2740 1 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
2741 1 : opParam.isCapture = isCapture;
2742 1 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
2743 1 : opParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
2744 :
2745 1 : if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
2746 0 : for (u32 i = 0; i < userRankSize_; i++) {
2747 0 : HCCL_CONFIG_DEBUG(HCCL_ALG,
2748 : "[HcclCommunicator][AllGatherV]userRank_[%u], rankIdx[%u], recvCounts[%llu], rdispls[%llu]",
2749 : userRank_, i, counts[i], static_cast<const u64 *>(rdispls)[i]);
2750 : }
2751 : }
2752 :
2753 1 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_ALLGATHER_V, opParam));
2754 :
2755 1 : return HCCL_SUCCESS;
2756 1 : }
2757 :
2758 0 : HcclResult HcclCommunicator::AicpuUnfold(const std::string &tag, void *inputPtr, void *outputPtr, u64 count,
2759 : HcclDataType dataType, HcclReduceOp op, HcclRtStream stream, HcclCMDType cmdType)
2760 : {
2761 0 : Stream streamObj(stream);
2762 0 : u32 perDataSize = SIZE_TABLE[dataType];
2763 0 : u64 totalSize = count * perDataSize;
2764 0 : bool isCapture = StreamIsCapture(stream);
2765 0 : OpParam opParam;
2766 0 : opParam.tag = tag;
2767 0 : opParam.inputPtr = inputPtr;
2768 0 : opParam.inputSize = totalSize;
2769 0 : opParam.outputPtr = outputPtr;
2770 0 : opParam.outputSize = totalSize;
2771 0 : opParam.DataDes.count = count;
2772 0 : opParam.DataDes.dataType = dataType;
2773 0 : opParam.reduceType = op;
2774 0 : opParam.stream = streamObj;
2775 0 : opParam.isCapture = isCapture;
2776 0 : opParam.syncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
2777 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
2778 0 : AlgType algType;
2779 0 : algType.algoLevel0 = AlgTypeLevel0::ALG_LEVEL0_NP_MESH;
2780 0 : algType.algoLevel1 = AlgTypeLevel1::ALG_LEVEL1_RING;
2781 :
2782 : // 构造空vector用于入参,无实际意义
2783 0 : const std::vector<Stream> slaveStreams;
2784 0 : CHK_RET(RegisterDfxInfo(opParam, algType, slaveStreams));
2785 0 : HcclResult ret = HCCL_SUCCESS;
2786 0 : if (!IsExistCommRes(identifier_)) {
2787 0 : HCCL_INFO("[AicpuUnfold] tag[%s] count[%llu] dataType[%s] op[%s].", identifier_.c_str(),
2788 : count, GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str());
2789 0 : uint64_t streamMode = 0;
2790 0 : CHK_RET(hrtStreamGetMode(stream, &streamMode));
2791 :
2792 : rtStream_t aicpuStream;
2793 0 : ret = Mc2AiCpuStreamAllocAndGet(streamMode, aicpuStream);
2794 0 : void *commContext = nullptr;
2795 0 : ret = CreateCommResource(identifier_, stream, true, &commContext);
2796 0 : if (ret != HCCL_SUCCESS) {
2797 0 : HCCL_ERROR("[hcclImpl][CreateComm]create aicpu unfold comminfo by tag[%s] failed. return[%d]",
2798 : identifier_.c_str(), ret);
2799 0 : return ret;
2800 : }
2801 : }
2802 :
2803 0 : std::string kernelName = "RunAicpuRpcSrvLaunch";
2804 0 : AicpuOpTiling opTilingInfo;
2805 0 : ret = AicpuKfcTilingDataLaunch(opParam, cmdType, commContext_, kernelName, opTilingInfo);
2806 0 : if (ret != HCCL_SUCCESS) {
2807 0 : HCCL_ERROR("[hcclImpl][TilingData]aicpu unfold tiling data launch failed. return[%d] inputPtr[%p]"
2808 : "outputPtr[%p] count[%llu] dataType[%s] op[%s]",
2809 : ret, inputPtr, outputPtr, count,
2810 : GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str());
2811 0 : return ret;
2812 : }
2813 0 : CHK_RET(UnRegisterDfxInfo(opParam, slaveStreams));
2814 0 : return HCCL_SUCCESS;
2815 0 : }
2816 :
2817 3 : HcclResult HcclCommunicator::AllGatherOutPlace(const std::string &tag, void *inputPtr, void *outputPtr,
2818 : u64 inputCount, HcclDataType dataType, HcclRtStream stream)
2819 : {
2820 3 : CHK_RET(CheckSuspendingStatus());
2821 3 : if (!IsAtomicInit()) {
2822 0 : HCCL_ERROR(
2823 : "[HcclCommunicator][AllGatherOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
2824 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
2825 0 : return HCCL_E_UNAVAIL;
2826 : }
2827 :
2828 6 : bool isCapture = StreamIsCapture(stream);
2829 :
2830 6 : bool aicpuUnfoldMode = false;
2831 6 : if (EnableAicpuUnfold(isCapture) && (userRankSize_ != 1)) {
2832 3 : aicpuUnfoldMode = true;
2833 : }
2834 :
2835 5 : Stream streamObj(stream);
2836 8 : CHK_RET(callbackTask_->CallbackRegStream(stream));
2837 :
2838 6 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
2839 5 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
2840 4 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
2841 4 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
2842 :
2843 4 : u32 perDataSize = SIZE_TABLE[dataType];
2844 4 : u64 totalSize = inputCount * perDataSize * userRankSize_;
2845 :
2846 4 : OpParam opParam;
2847 6 : opParam.tag = tag;
2848 8 : opParam.inputPtr = inputPtr;
2849 8 : opParam.inputSize = inputCount * perDataSize;
2850 8 : opParam.outputPtr = outputPtr;
2851 8 : opParam.outputSize = totalSize;
2852 8 : opParam.DataDes.count = inputCount;
2853 8 : opParam.DataDes.dataType = dataType;
2854 8 : opParam.reduceType = HcclReduceOp::HCCL_REDUCE_RESERVED;
2855 8 : opParam.stream = streamObj;
2856 8 : opParam.syncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
2857 8 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
2858 8 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
2859 8 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
2860 8 : opParam.isCapture = isCapture;
2861 8 : opParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
2862 :
2863 8 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_ALLGATHER, opParam));
2864 :
2865 0 : return HCCL_SUCCESS;
2866 8 : }
2867 :
2868 1 : HcclResult HcclCommunicator::AllGatherVOutPlace(const std::string &tag, void *inputPtr, void *outputPtr,
2869 : u64 inputCount, const void *outputCounts, const void *outputDispls, HcclDataType dataType, HcclRtStream stream)
2870 : {
2871 1 : CHK_RET(CheckSuspendingStatus());
2872 1 : if (userRankSize_ == 1) {
2873 : // rankSize为1时,退化为AllGather
2874 0 : return AllGatherOutPlace(tag, inputPtr, outputPtr, inputCount, dataType, stream);
2875 : }
2876 :
2877 1 : if (!IsAtomicInit()) {
2878 0 : HCCL_ERROR(
2879 : "[HcclCommunicator][AllGatherVOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
2880 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
2881 0 : return HCCL_E_UNAVAIL;
2882 : }
2883 :
2884 1 : bool aicpuUnfoldMode = false;
2885 1 : if (GetAicpuUnfoldConfig() && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
2886 0 : aicpuUnfoldMode = true;
2887 : }
2888 :
2889 1 : bool isCapture = StreamIsCapture(stream);
2890 :
2891 1 : Stream streamObj(stream);
2892 1 : CHK_RET(callbackTask_->CallbackRegStream(stream));
2893 :
2894 1 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
2895 1 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
2896 1 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
2897 1 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
2898 :
2899 1 : u32 perDataSize = SIZE_TABLE[dataType];
2900 1 : u64 outputSize = 0;
2901 1 : const u64 *counts = static_cast<const u64 *>(outputCounts);
2902 3 : for (u32 i = 0; i < userRankSize_; i++) {
2903 2 : outputSize += counts[i] * perDataSize;
2904 : }
2905 :
2906 1 : OpParam opParam;
2907 1 : opParam.tag = tag;
2908 1 : opParam.inputPtr = inputPtr;
2909 1 : opParam.inputSize = inputCount * perDataSize;
2910 1 : opParam.outputPtr = outputPtr;
2911 1 : opParam.outputSize = outputSize;
2912 1 : opParam.VDataDes.counts = const_cast<void *>(outputCounts);
2913 1 : opParam.VDataDes.displs = const_cast<void *>(outputDispls);
2914 1 : opParam.VDataDes.dataType = dataType;
2915 1 : opParam.reduceType = HcclReduceOp::HCCL_REDUCE_RESERVED;
2916 1 : opParam.stream = streamObj;
2917 1 : opParam.syncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
2918 1 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
2919 1 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
2920 1 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
2921 1 : opParam.isCapture = isCapture;
2922 1 : opParam.rankSize = userRankSize_;
2923 1 : opParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
2924 :
2925 1 : if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
2926 0 : for (u32 i = 0; i < userRankSize_; i++) {
2927 0 : HCCL_CONFIG_DEBUG(HCCL_ALG,
2928 : "[HcclCommunicator][AllGatherVOutPlace]userRank_[%u], rankIdx[%u],"
2929 : "outputCounts[%llu], outputDispls[%llu]",
2930 : userRank_, i, counts[i], static_cast<const u64 *>(outputDispls)[i]);
2931 : }
2932 : }
2933 :
2934 1 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_ALLGATHER_V, opParam));
2935 :
2936 1 : return HCCL_SUCCESS;
2937 1 : }
2938 :
2939 79 : void HcclCommunicator::GetAndSetSyncMode(SyncMode &preSyncMode, SyncMode newSyncMode)
2940 : {
2941 79 : if (newSyncMode == SyncMode::UNLIMITED_TIMEWAITSYNCMODE) {
2942 0 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
2943 0 : HCCL_WARNING("310P don't support unlimited notify wait mode");
2944 : } else {
2945 0 : HcclGetNotifyWaitMode(dispatcher_, &preSyncMode);
2946 0 : HcclSetNotifyWaitMode(dispatcher_, newSyncMode);
2947 : }
2948 : }
2949 79 : }
2950 :
2951 69 : void HcclCommunicator::RestorePreSyncMode(SyncMode preSyncMode, SyncMode newSyncMode)
2952 : {
2953 69 : if (newSyncMode == SyncMode::UNLIMITED_TIMEWAITSYNCMODE && !Is310P3Common(isHaveCpuRank_, deviceType_)) {
2954 0 : HcclSetNotifyWaitMode(dispatcher_, preSyncMode);
2955 : }
2956 69 : }
2957 :
2958 34 : HcclResult HcclCommunicator::AllReduce(const std::string &tag, void *inputPtr, void *outputPtr, u64 count,
2959 : HcclDataType dataType, HcclReduceOp op, HcclRtStream stream,
2960 : SyncMode syncMode, const HcomCollOpInfo *opInfo)
2961 : {
2962 34 : CHK_RET(CheckSuspendingStatus());
2963 36 : bool aicpuUnfoldMode = false;
2964 36 : if (GetAicpuUnfoldConfig() == true &&
2965 10 : IsSupportSDMAReduce(inputPtr, outputPtr, dataType, op) &&
2966 45 : deviceType_ == DevType::DEV_TYPE_910_93 && (userRankSize_ != 1)) {
2967 0 : aicpuUnfoldMode = true;
2968 : }
2969 :
2970 35 : if (!IsAtomicInit()) {
2971 0 : HCCL_ERROR("[HcclCommunicator][AllReduce]errNo[0x%016llx] hccl init must be called before call this function",
2972 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
2973 0 : return HCCL_E_UNAVAIL;
2974 : }
2975 :
2976 : // 设置notify wait模式
2977 59 : SyncMode preSyncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
2978 59 : GetAndSetSyncMode(preSyncMode, syncMode);
2979 :
2980 59 : Stream streamObj(stream);
2981 61 : CHK_RET(callbackTask_->CallbackRegStream(stream));
2982 :
2983 60 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
2984 57 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
2985 50 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
2986 48 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
2987 :
2988 47 : u32 perDataSize = SIZE_TABLE[dataType];
2989 47 : u64 totalSize = count * perDataSize;
2990 :
2991 47 : OpParam opParam;
2992 53 : opParam.tag = tag;
2993 60 : opParam.inputPtr = inputPtr;
2994 60 : opParam.inputSize = totalSize;
2995 60 : opParam.outputPtr = outputPtr;
2996 60 : opParam.outputSize = totalSize;
2997 60 : opParam.DataDes.count = count;
2998 60 : opParam.DataDes.dataType = dataType;
2999 60 : opParam.reduceType = op;
3000 60 : opParam.stream = streamObj;
3001 55 : opParam.syncMode = syncMode;
3002 55 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3003 55 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3004 57 : opParam.opType = HcclCMDType::HCCL_CMD_ALLREDUCE;
3005 : // 用于inplace支持重执行场景的图模式归一至单算子模式
3006 57 : retryOrigWorkflowMode_ = GetWorkflowMode();
3007 0 : bool isHcclOpInplace = IsHcclOpInplace(HcclCMDType::HCCL_CMD_ALLREDUCE, opParam, userRank_, userRankSize_,
3008 55 : isInplaceStatus_);
3009 58 : if (aicpuUnfoldMode && retryEnable_ && isHcclOpInplace) {
3010 0 : HCCL_DEBUG("The retry with inplace case is expected to be supported, "
3011 : "aicpuUnfoldMode[%d], retryEnable_[%d], isHcclOpInplace[%d], "
3012 : "therefore HcclWorkflowMode is converted from [%d] to HCCL_WORKFLOW_MODE_OP_BASE",
3013 : aicpuUnfoldMode, retryEnable_, isHcclOpInplace, static_cast<u8>(retryOrigWorkflowMode_));
3014 0 : CHK_RET(SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE));
3015 : }
3016 :
3017 58 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_ALLREDUCE, opParam));
3018 :
3019 52 : RestorePreSyncMode(preSyncMode, syncMode);
3020 51 : CHK_RET(SetWorkflowMode(retryOrigWorkflowMode_));
3021 51 : return HCCL_SUCCESS;
3022 56 : }
3023 :
3024 0 : HcclResult HcclCommunicator::AllReduceAicpuUnfold(const std::string &tag, void *inputPtr, void *outputPtr, u64 count,
3025 : HcclDataType dataType, HcclReduceOp op, HcclRtStream stream)
3026 : {
3027 0 : Stream streamObj(stream);
3028 0 : u32 perDataSize = SIZE_TABLE[dataType];
3029 0 : u64 totalSize = count * perDataSize;
3030 0 : OpParam opParam;
3031 0 : opParam.tag = tag;
3032 0 : opParam.inputPtr = inputPtr;
3033 0 : opParam.inputSize = totalSize;
3034 0 : opParam.outputPtr = outputPtr;
3035 0 : opParam.outputSize = totalSize;
3036 0 : opParam.DataDes.count = count;
3037 0 : opParam.DataDes.dataType = dataType;
3038 0 : opParam.reduceType = op;
3039 0 : opParam.stream = streamObj;
3040 0 : opParam.isCapture = StreamIsCapture(stream);
3041 0 : opParam.syncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
3042 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3043 0 : AlgType algType;
3044 0 : algType.algoLevel0 = AlgTypeLevel0::ALG_LEVEL0_NP_SINGLE_RING;
3045 0 : algType.algoLevel1 = AlgTypeLevel1::ALG_LEVEL1_RING;
3046 0 : auto slaveStreams = opParam.isCapture ? std::vector<Stream>{opStream_} : std::vector<Stream>{};
3047 0 : CaptureSlaveStreams(streamObj.ptr(), slaveStreams);
3048 0 : CHK_RET(RegisterDfxInfo(opParam, algType, slaveStreams));
3049 : HcclResult ret;
3050 0 : if (!IsExistCommRes(tag)) {
3051 0 : uint64_t streamMode = 0;
3052 0 : CHK_RET(hrtStreamGetMode(stream, &streamMode));
3053 : rtStream_t aicpuStream;
3054 0 : ret = Mc2AiCpuStreamAllocAndGet(streamMode, aicpuStream);
3055 0 : void *commContext = nullptr;
3056 0 : ret = CreateCommResource(tag, aicpuStream, true, &commContext);
3057 0 : if (ret != HCCL_SUCCESS) {
3058 0 : HCCL_ERROR("[hcclImpl][CreateComm]create aicpu unfold comminfo by tag[%s] failed. return[%d]",
3059 : tag.c_str(), ret);
3060 0 : return ret;
3061 : }
3062 : }
3063 0 : AicpuOpTiling opTilingInfo;
3064 0 : std::string kernelName = "RunAicpuRpcSrvLaunch";
3065 0 : ret = AicpuKfcTilingDataLaunch(opParam, HcclCMDType::HCCL_CMD_ALLREDUCE, commContext_, kernelName, opTilingInfo);
3066 0 : if (ret != HCCL_SUCCESS) {
3067 0 : HCCL_ERROR("[hcclImpl][TilingData]aicpu unfold tiling data launch failed. return[%d] inputPtr[%p]"
3068 : "outputPtr[%p] count[%llu] dataType[%s] op[%s]",
3069 : ret, inputPtr, outputPtr, count,
3070 : GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str());
3071 0 : return ret;
3072 : }
3073 0 : CHK_RET(UnRegisterDfxInfo(opParam, slaveStreams));
3074 0 : return HCCL_SUCCESS;
3075 0 : }
3076 :
3077 14 : HcclResult HcclCommunicator::AllReduceOutPlace(const std::string &tag, void *inputPtr, void *outputPtr, u64 count,
3078 : HcclDataType dataType, HcclReduceOp op, HcclRtStream stream,
3079 : SyncMode syncMode)
3080 : {
3081 14 : CHK_RET(CheckSuspendingStatus());
3082 18 : const u32 RANK_SIZE_TWO = 2;
3083 18 : bool aicpuUnfoldMode = false;
3084 18 : if (GetAicpuUnfoldConfig() == true &&
3085 0 : IsSupportSDMAReduce(inputPtr, outputPtr, dataType, op)) {
3086 0 : if (userRankSize_ >= RANK_SIZE_TWO && Is310P3Common(isHaveCpuRank_, deviceType_)) {
3087 0 : HcclResult ret = AllReduceAicpuUnfold(tag, inputPtr, outputPtr, count, dataType, op, stream);
3088 0 : CHK_PRT_RET((ret != HCCL_SUCCESS),
3089 : HCCL_ERROR("[HcclCommunicator][AllReduce]errNo[0x%016llx] tag[%s], AllReduce aicpu unfold failed",
3090 : HCCL_ERROR_CODE(ret), tag.c_str()),
3091 : ret);
3092 :
3093 0 : return HCCL_SUCCESS;
3094 : }
3095 0 : if ((deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
3096 0 : aicpuUnfoldMode = true;
3097 : }
3098 : }
3099 :
3100 18 : bool isCapture = StreamIsCapture(stream);
3101 :
3102 17 : if (!IsAtomicInit()) {
3103 0 : HCCL_ERROR(
3104 : "[HcclCommunicator][AllReduceOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
3105 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3106 0 : return HCCL_E_UNAVAIL;
3107 : }
3108 :
3109 : // 设置notify wait模式
3110 19 : SyncMode preSyncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
3111 19 : GetAndSetSyncMode(preSyncMode, syncMode);
3112 :
3113 21 : Stream streamObj(stream);
3114 21 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3115 :
3116 19 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3117 16 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3118 13 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
3119 13 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3120 :
3121 15 : u32 perDataSize = SIZE_TABLE[dataType];
3122 15 : u64 totalSize = count * perDataSize;
3123 :
3124 15 : OpParam opParam;
3125 16 : opParam.tag = tag;
3126 21 : opParam.inputPtr = inputPtr;
3127 21 : opParam.inputSize = totalSize;
3128 21 : opParam.outputPtr = outputPtr;
3129 21 : opParam.outputSize = totalSize;
3130 21 : opParam.DataDes.count = count;
3131 21 : opParam.DataDes.dataType = dataType;
3132 21 : opParam.reduceType = op;
3133 21 : opParam.stream = streamObj;
3134 18 : opParam.syncMode = syncMode;
3135 18 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3136 18 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3137 17 : opParam.isCapture = isCapture;
3138 17 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
3139 17 : opParam.opType = HcclCMDType::HCCL_CMD_ALLREDUCE;
3140 :
3141 17 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_ALLREDUCE, opParam));
3142 :
3143 18 : RestorePreSyncMode(preSyncMode, syncMode);
3144 18 : return HCCL_SUCCESS;
3145 19 : }
3146 :
3147 4 : HcclResult HcclCommunicator::AlltoAllV(const void *sendBuf, const void *sendCounts, const void *sdispls,
3148 : HcclDataType sendType, const void *recvBuf, const void *recvCounts, const void *rdispls, HcclDataType recvType,
3149 : rtStream_t stream, const std::string &tag)
3150 : {
3151 4 : CHK_RET(CheckSuspendingStatus());
3152 :
3153 4 : if (!IsAtomicInit()) {
3154 0 : HCCL_ERROR("[HcclCommunicator][AlltoAllV]errNo[0x%016llx] hccl init must be called before call this function",
3155 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3156 0 : return HCCL_E_UNAVAIL;
3157 : }
3158 :
3159 4 : if (IsNeedNicInit()) {
3160 4 : HCCL_INFO("InitNic.");
3161 4 : CHK_RET(InitNic());
3162 : }
3163 :
3164 4 : bool isCapture = StreamIsCapture(stream);
3165 :
3166 4 : Stream streamObj(stream);
3167 4 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3168 :
3169 4 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3170 4 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3171 4 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
3172 4 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3173 :
3174 4 : OpParam opParam;
3175 4 : opParam.tag = tag;
3176 4 : opParam.inputPtr = const_cast<void *>(sendBuf);
3177 4 : opParam.outputPtr = const_cast<void *>(recvBuf);
3178 4 : opParam.All2AllDataDes.sendType = sendType;
3179 4 : opParam.All2AllDataDes.recvType = recvType;
3180 4 : opParam.All2AllDataDes.sendCounts = const_cast<void *>(sendCounts);
3181 4 : opParam.All2AllDataDes.recvCounts = const_cast<void *>(recvCounts);
3182 4 : opParam.All2AllDataDes.sdispls = const_cast<void *>(sdispls);
3183 4 : opParam.All2AllDataDes.rdispls = const_cast<void *>(rdispls);
3184 4 : opParam.stream = streamObj;
3185 4 : opParam.opType = HcclCMDType::HCCL_CMD_ALLTOALLV;
3186 4 : opParam.aicpuUnfoldMode = EnableAicpuUnfold(isCapture);
3187 4 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3188 4 : opParam.isCapture = isCapture;
3189 :
3190 4 : if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
3191 0 : for (u32 i = 0; i < userRankSize_; i++) {
3192 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[HcclCommunicator][AlltoAllV] rank[%u], sendCounts[%llu], sendDispls[%llu] "
3193 : "recvCounts[%llu], recvDispls[%llu]",
3194 : userRank_,
3195 : *(static_cast<const u64 *>(opParam.All2AllDataDes.sendCounts) + i),
3196 : *(static_cast<const u64 *>(opParam.All2AllDataDes.sdispls) + i),
3197 : *(static_cast<const u64 *>(opParam.All2AllDataDes.recvCounts) + i),
3198 : *(static_cast<const u64 *>(opParam.All2AllDataDes.rdispls) + i));
3199 : }
3200 : }
3201 :
3202 4 : CHK_RET(ExecOpAlltoAll(HcclCMDType::HCCL_CMD_ALLTOALLV, opParam));
3203 4 : return HCCL_SUCCESS;
3204 4 : }
3205 :
3206 0 : HcclResult HcclCommunicator::AlltoAllVOutPlace(const void *sendBuf, const void *sendCounts, const void *sdispls,
3207 : HcclDataType sendType, const void *recvBuf, const void *recvCounts, const void *rdispls, HcclDataType recvType,
3208 : rtStream_t stream, const std::string &tag)
3209 : {
3210 0 : CHK_RET(CheckSuspendingStatus());
3211 0 : CHK_PRT_RET(Is310P3Common(isHaveCpuRank_, deviceType_),
3212 : HCCL_RUN_INFO("[AlltoAllVOutPlace]This method cannot be invoked in the current scenario."), HCCL_SUCCESS);
3213 0 : if (!IsAtomicInit()) {
3214 0 : HCCL_ERROR(
3215 : "[HcclCommunicator][AlltoAllVOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
3216 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3217 0 : return HCCL_E_UNAVAIL;
3218 : }
3219 :
3220 0 : if (IsNeedNicInit()) {
3221 0 : HCCL_INFO("InitNic.");
3222 0 : CHK_RET(InitNic());
3223 : }
3224 :
3225 0 : bool isCapture = StreamIsCapture(stream);
3226 :
3227 0 : Stream streamObj(stream);
3228 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3229 :
3230 0 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3231 0 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3232 0 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
3233 0 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3234 :
3235 0 : OpParam opParam;
3236 0 : opParam.tag = tag;
3237 0 : opParam.inputPtr = const_cast<void *>(sendBuf);
3238 0 : opParam.outputPtr = const_cast<void *>(recvBuf);
3239 0 : opParam.All2AllDataDes.sendType = sendType;
3240 0 : opParam.All2AllDataDes.recvType = recvType;
3241 0 : opParam.All2AllDataDes.sendCounts = const_cast<void *>(sendCounts);
3242 0 : opParam.All2AllDataDes.recvCounts = const_cast<void *>(recvCounts);
3243 0 : opParam.All2AllDataDes.sdispls = const_cast<void *>(sdispls);
3244 0 : opParam.All2AllDataDes.rdispls = const_cast<void *>(rdispls);
3245 0 : opParam.stream = streamObj;
3246 0 : opParam.opType = HcclCMDType::HCCL_CMD_ALLTOALLV;
3247 0 : opParam.aicpuUnfoldMode = EnableAicpuUnfold(isCapture);
3248 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3249 0 : opParam.isCapture = isCapture;
3250 :
3251 0 : if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
3252 0 : for (u32 i = 0; i < userRankSize_; i++) {
3253 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[HcclCommunicator][AlltoAllVOutPlace] rank[%u], sendCounts[%llu],"
3254 : "sendDispls[%llu], recvCounts[%llu], recvDispls[%llu]",
3255 : userRank_,
3256 : *(static_cast<const u64 *>(opParam.All2AllDataDes.sendCounts) + i),
3257 : *(static_cast<const u64 *>(opParam.All2AllDataDes.sdispls) + i),
3258 : *(static_cast<const u64 *>(opParam.All2AllDataDes.recvCounts) + i),
3259 : *(static_cast<const u64 *>(opParam.All2AllDataDes.rdispls) + i));
3260 : }
3261 : }
3262 :
3263 0 : CHK_RET(ExecOpAlltoAll(HcclCMDType::HCCL_CMD_ALLTOALLV, opParam));
3264 0 : return HCCL_SUCCESS;
3265 0 : }
3266 :
3267 1 : HcclResult HcclCommunicator::AlltoAllVC(const void *sendBuf, const void *sendCountMatrix, HcclDataType sendType,
3268 : const void *recvBuf, HcclDataType recvType, rtStream_t stream, const std::string &tag)
3269 : {
3270 1 : CHK_RET(CheckSuspendingStatus());
3271 1 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
3272 0 : HCCL_ERROR("[%s][%s]AlltoAllVC is not supported",
3273 : LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_NOT_SUPPORTED.c_str());
3274 0 : return HCCL_E_NOT_SUPPORT;
3275 : }
3276 1 : if (!IsAtomicInit()) {
3277 0 : HCCL_ERROR("[HcclCommunicator][AlltoAllVC]errNo[0x%016llx] hccl init must be called before call this function",
3278 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3279 0 : return HCCL_E_UNAVAIL;
3280 : }
3281 :
3282 1 : if (IsNeedNicInit()) {
3283 1 : HCCL_INFO("InitNic.");
3284 1 : CHK_RET(InitNic());
3285 : }
3286 :
3287 1 : bool isCapture = StreamIsCapture(stream);
3288 :
3289 1 : Stream streamObj(stream);
3290 1 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3291 :
3292 1 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3293 1 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3294 1 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
3295 1 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3296 :
3297 1 : OpParam opParam;
3298 1 : opParam.tag = tag;
3299 1 : opParam.inputPtr = const_cast<void *>(sendBuf);
3300 1 : opParam.outputPtr = const_cast<void *>(recvBuf);
3301 1 : opParam.All2AllDataDes.sendType = sendType;
3302 1 : opParam.All2AllDataDes.recvType = recvType;
3303 1 : opParam.All2AllDataDes.sendCountMatrix = const_cast<void *>(sendCountMatrix);
3304 1 : opParam.stream = streamObj;
3305 1 : opParam.opType = HcclCMDType::HCCL_CMD_ALLTOALLVC;
3306 1 : opParam.aicpuUnfoldMode = EnableAicpuUnfold(isCapture);
3307 1 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3308 1 : opParam.isCapture = isCapture;
3309 :
3310 1 : if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
3311 0 : for (u32 i = 0; i < userRankSize_; i++) {
3312 0 : for (u32 j = 0; j < userRankSize_; j++) {
3313 0 : HCCL_CONFIG_DEBUG(HCCL_ALG, "[HcclCommunicator][AlltoAllVC] usrRank[%u] rank[%u] to remoteRank[%u], "
3314 : "sendCounts[%llu]",
3315 : userRank_, i, j,
3316 : *(static_cast<const u64 *>(opParam.All2AllDataDes.sendCountMatrix) + i * userRankSize_ + j));
3317 : }
3318 : }
3319 : }
3320 :
3321 1 : CHK_RET(ExecOpAlltoAll(HcclCMDType::HCCL_CMD_ALLTOALLVC, opParam));
3322 1 : return HCCL_SUCCESS;
3323 1 : }
3324 :
3325 0 : HcclResult HcclCommunicator::AlltoAllVCOutPlace(const void *sendBuf, const void *sendCountMatrix, HcclDataType sendType,
3326 : const void *recvBuf, HcclDataType recvType, rtStream_t stream, const std::string &tag)
3327 : {
3328 0 : CHK_RET(CheckSuspendingStatus());
3329 0 : CHK_PRT_RET(Is310P3Common(isHaveCpuRank_, deviceType_),
3330 : HCCL_RUN_INFO("[AlltoAllVCOutPlace]This method cannot be invoked in the current scenario."), HCCL_SUCCESS);
3331 :
3332 0 : if (!IsAtomicInit()) {
3333 0 : HCCL_ERROR(
3334 : "[HcclCommunicator][AlltoAllVCOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
3335 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3336 0 : return HCCL_E_UNAVAIL;
3337 : }
3338 :
3339 0 : if (IsNeedNicInit()) {
3340 0 : HCCL_INFO("InitNic");
3341 0 : CHK_RET(InitNic());
3342 : }
3343 :
3344 0 : bool isCapture = StreamIsCapture(stream);
3345 :
3346 0 : Stream streamObj(stream);
3347 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3348 :
3349 0 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3350 0 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3351 0 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
3352 0 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3353 :
3354 0 : OpParam opParam;
3355 0 : opParam.tag = tag;
3356 0 : opParam.inputPtr = const_cast<void *>(sendBuf);
3357 0 : opParam.outputPtr = const_cast<void *>(recvBuf);
3358 0 : opParam.All2AllDataDes.sendType = sendType;
3359 0 : opParam.All2AllDataDes.recvType = recvType;
3360 0 : opParam.All2AllDataDes.sendCountMatrix = const_cast<void *>(sendCountMatrix);
3361 0 : opParam.stream = streamObj;
3362 0 : opParam.opType = HcclCMDType::HCCL_CMD_ALLTOALLVC;
3363 0 : opParam.aicpuUnfoldMode = EnableAicpuUnfold(isCapture);
3364 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3365 0 : opParam.isCapture = isCapture;
3366 :
3367 0 : if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
3368 0 : for (u32 i = 0; i < userRankSize_; i++) {
3369 0 : for (u32 j = 0; j < userRankSize_; j++) {
3370 0 : HCCL_CONFIG_DEBUG(HCCL_ALG, "[HcclCommunicator][AlltoAllVCOutPlace] usrRank[%u] rank[%u]"
3371 : "to remoteRank[%u], sendCounts[%llu]",
3372 : userRank_, i, j,
3373 : *(static_cast<const u64 *>(opParam.All2AllDataDes.sendCountMatrix) + i * userRankSize_ + j));
3374 : }
3375 : }
3376 : }
3377 :
3378 0 : CHK_RET(ExecOpAlltoAll(HcclCMDType::HCCL_CMD_ALLTOALLVC, opParam));
3379 0 : return HCCL_SUCCESS;
3380 0 : }
3381 :
3382 0 : HcclResult HcclCommunicator::AlltoAll(const void *sendBuf, u64 sendCount, HcclDataType sendType,
3383 : const void *recvBuf, u64 recvCount, HcclDataType recvType, rtStream_t stream, const std::string &tag)
3384 : {
3385 0 : CHK_RET(CheckSuspendingStatus());
3386 0 : if (!IsAtomicInit()) {
3387 0 : HCCL_ERROR("[HcclCommunicator][AlltoAll]errNo[0x%016llx] hccl init must be called before call this function",
3388 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3389 0 : return HCCL_E_UNAVAIL;
3390 : }
3391 :
3392 0 : if (IsNeedNicInit()) {
3393 0 : HCCL_INFO("InitNic.");
3394 0 : CHK_RET(InitNic());
3395 : }
3396 :
3397 0 : bool isCapture = StreamIsCapture(stream);
3398 :
3399 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3400 :
3401 : // 生成sendCountMatrix矩阵,alltoall的底层实现走alltoallvc
3402 0 : std::vector<u64> sendCountMatrix(userRankSize_ * userRankSize_, sendCount);
3403 :
3404 0 : OpParam opParam;
3405 0 : opParam.tag = tag;
3406 0 : opParam.inputPtr = const_cast<void *>(sendBuf);
3407 0 : opParam.outputPtr = const_cast<void *>(recvBuf);
3408 0 : opParam.All2AllDataDes.sendType = sendType;
3409 0 : opParam.All2AllDataDes.recvType = recvType;
3410 0 : opParam.All2AllDataDes.sendCount = sendCount;
3411 0 : opParam.All2AllDataDes.recvCount = recvCount;
3412 0 : opParam.All2AllDataDes.sendCountMatrix = static_cast<void *>(sendCountMatrix.data());
3413 0 : opParam.stream = Stream(stream);
3414 0 : opParam.opType = HcclCMDType::HCCL_CMD_ALLTOALL;
3415 0 : opParam.aicpuUnfoldMode = false;
3416 0 : opParam.aicpuCacheEnable = 0;
3417 0 : opParam.isCapture = isCapture;
3418 0 : opParam.inputSize = sendCount * SIZE_TABLE[sendType] * userRankSize_;
3419 0 : opParam.outputSize = recvCount * SIZE_TABLE[recvType] * userRankSize_;
3420 0 : if (EnableAicpuUnfold(isCapture)) {
3421 0 : opParam.aicpuUnfoldMode = true;
3422 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3423 : }
3424 :
3425 0 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3426 0 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3427 0 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
3428 0 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3429 0 : CHK_RET(ExecOpAlltoAll(HcclCMDType::HCCL_CMD_ALLTOALL, opParam));
3430 0 : return HCCL_SUCCESS;
3431 0 : }
3432 :
3433 2 : HcclResult HcclCommunicator::Broadcast(const std::string &tag, void *ptr, u64 count, HcclDataType dataType, u32 root,
3434 : HcclRtStream stream)
3435 : {
3436 2 : CHK_RET(CheckSuspendingStatus());
3437 2 : bool aicpuUnfoldMode = false;
3438 2 : if (GetAicpuUnfoldConfig() == true && deviceType_ == DevType::DEV_TYPE_910_93 && (userRankSize_ != 1)) {
3439 0 : aicpuUnfoldMode = true;
3440 : }
3441 :
3442 2 : if (!IsAtomicInit()) {
3443 0 : HCCL_ERROR("[HcclCommunicator][Broadcast]errNo[0x%016llx] hccl init must be called before call this function",
3444 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3445 0 : return HCCL_E_UNAVAIL;
3446 : }
3447 :
3448 2 : Stream streamObj(stream);
3449 2 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3450 :
3451 2 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3452 2 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3453 2 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
3454 2 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3455 2 : u32 perDataSize = SIZE_TABLE[dataType];
3456 2 : u64 totalSize = count * perDataSize;
3457 :
3458 2 : OpParam opParam;
3459 2 : opParam.tag = tag;
3460 2 : opParam.inputPtr = ptr;
3461 2 : opParam.outputPtr = ptr;
3462 2 : opParam.inputSize = totalSize;
3463 2 : opParam.outputSize = totalSize;
3464 2 : opParam.DataDes.count = count;
3465 2 : opParam.DataDes.dataType = dataType;
3466 2 : opParam.root = root;
3467 2 : opParam.stream = streamObj;
3468 2 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3469 2 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3470 2 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
3471 2 : opParam.opType = HcclCMDType::HCCL_CMD_BROADCAST;
3472 :
3473 2 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_BROADCAST, opParam));
3474 :
3475 2 : return HCCL_SUCCESS;
3476 2 : }
3477 :
3478 0 : HcclResult HcclCommunicator::BroadcastOutPlace(const std::string &tag, void *ptr, u64 count, HcclDataType dataType,
3479 : u32 root, HcclRtStream stream)
3480 : {
3481 0 : CHK_RET(CheckSuspendingStatus());
3482 0 : bool aicpuUnfoldMode = false;
3483 0 : if (GetAicpuUnfoldConfig() == true && deviceType_ == DevType::DEV_TYPE_910_93 && (userRankSize_ != 1)) {
3484 0 : aicpuUnfoldMode = true;
3485 : }
3486 :
3487 0 : CHK_PRT_RET(Is310P3Common(isHaveCpuRank_, deviceType_),
3488 : HCCL_RUN_INFO("[BroadcastOutPlace]This method cannot be invoked in the current scenario."), HCCL_SUCCESS);
3489 :
3490 0 : if (!IsAtomicInit()) {
3491 0 : HCCL_ERROR("[HcclCommunicator][BroadcastOutPlace]errNo[0x%016llx] hccl init must be called before"
3492 : " call this function",
3493 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3494 0 : return HCCL_E_UNAVAIL;
3495 : }
3496 :
3497 0 : bool isCapture = StreamIsCapture(stream);
3498 :
3499 0 : Stream streamObj(stream);
3500 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3501 :
3502 0 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3503 0 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3504 0 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
3505 0 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3506 :
3507 0 : u32 perDataSize = SIZE_TABLE[dataType];
3508 0 : u64 totalSize = count * perDataSize;
3509 :
3510 0 : OpParam opParam;
3511 0 : opParam.tag = tag;
3512 0 : opParam.inputPtr = ptr;
3513 0 : opParam.outputPtr = ptr;
3514 0 : opParam.inputSize = totalSize;
3515 0 : opParam.outputSize = totalSize;
3516 0 : opParam.DataDes.count = count;
3517 0 : opParam.DataDes.dataType = dataType;
3518 0 : opParam.root = root;
3519 0 : opParam.stream = streamObj;
3520 0 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3521 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3522 0 : opParam.isCapture = isCapture;
3523 0 : opParam.opType = HcclCMDType::HCCL_CMD_BROADCAST;
3524 :
3525 0 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_BROADCAST, opParam));
3526 :
3527 0 : return HCCL_SUCCESS;
3528 0 : }
3529 :
3530 0 : HcclResult HcclCommunicator::Scatter(const std::string &tag, void *inputPtr, void *outputPtr, u64 recvCount,
3531 : HcclDataType dataType, u32 root, HcclRtStream stream)
3532 : {
3533 0 : CHK_RET(CheckSuspendingStatus());
3534 0 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
3535 0 : HCCL_ERROR("[%s][%s]Scatter Not Supported Yet",
3536 : LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_NOT_SUPPORTED.c_str());
3537 0 : return HCCL_E_NOT_SUPPORT;
3538 : }
3539 0 : bool aicpuUnfoldMode = false;
3540 0 : if (GetAicpuUnfoldConfig() == true && deviceType_ == DevType::DEV_TYPE_910_93 && (userRankSize_ != 1)) {
3541 0 : aicpuUnfoldMode = true;
3542 : }
3543 :
3544 0 : if (!IsAtomicInit()) {
3545 0 : HCCL_ERROR("[HcclCommunicator][Scatter]errNo[0x%016llx] hccl init must be called before call this function",
3546 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3547 0 : return HCCL_E_UNAVAIL;
3548 : }
3549 :
3550 0 : Stream streamObj(stream);
3551 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3552 :
3553 0 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3554 0 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3555 0 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
3556 0 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3557 :
3558 0 : u32 perDataSize = SIZE_TABLE[dataType];
3559 0 : u64 outputSize = recvCount * perDataSize;
3560 0 : u64 totalSize = outputSize * userRankSize_;
3561 :
3562 0 : OpParam opParam;
3563 0 : opParam.tag = tag;
3564 0 : opParam.inputPtr = inputPtr;
3565 0 : opParam.inputSize = totalSize;
3566 0 : opParam.outputPtr = outputPtr;
3567 0 : opParam.outputSize = totalSize;
3568 0 : opParam.DataDes.count = recvCount;
3569 0 : opParam.DataDes.dataType = dataType;
3570 0 : opParam.stream = streamObj;
3571 0 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3572 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3573 0 : opParam.root = root;
3574 0 : opParam.opType = HcclCMDType::HCCL_CMD_SCATTER;
3575 0 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_SCATTER, opParam));
3576 0 : return HCCL_SUCCESS;
3577 0 : }
3578 :
3579 8 : HcclResult HcclCommunicator::ScatterOutPlace(const std::string &tag, void *inputPtr, void *outputPtr, u64 recvCount,
3580 : HcclDataType dataType, u32 root, HcclRtStream stream)
3581 : {
3582 8 : CHK_RET(CheckSuspendingStatus());
3583 10 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
3584 0 : HCCL_ERROR("[%s][%s]ScatterOutPlace Not Supported Yet",
3585 : LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_NOT_SUPPORTED.c_str());
3586 0 : return HCCL_E_NOT_SUPPORT;
3587 : }
3588 :
3589 10 : bool aicpuUnfoldMode = false;
3590 10 : if (GetAicpuUnfoldConfig() == true && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
3591 0 : aicpuUnfoldMode = true;
3592 : }
3593 :
3594 10 : bool isCapture = StreamIsCapture(stream);
3595 :
3596 12 : if (!IsAtomicInit()) {
3597 0 : HCCL_ERROR("[HcclCommunicator][ScatterOutPlace]errNo[0x%016llx] hccl init must be called before"
3598 : " call this function",
3599 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3600 0 : return HCCL_E_UNAVAIL;
3601 : }
3602 :
3603 12 : Stream streamObj(stream);
3604 12 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3605 :
3606 12 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3607 10 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3608 6 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
3609 5 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3610 :
3611 4 : u32 perDataSize = SIZE_TABLE[dataType];
3612 4 : u64 outputSize = recvCount * perDataSize;
3613 4 : u64 totalSize = outputSize * userRankSize_;
3614 :
3615 4 : OpParam opParam;
3616 5 : opParam.tag = tag;
3617 12 : opParam.inputPtr = inputPtr;
3618 12 : opParam.inputSize = totalSize;
3619 12 : opParam.outputPtr = outputPtr;
3620 12 : opParam.outputSize = totalSize;
3621 12 : opParam.DataDes.count = recvCount;
3622 12 : opParam.DataDes.dataType = dataType;
3623 12 : opParam.stream = streamObj;
3624 6 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3625 6 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3626 6 : opParam.isCapture = isCapture;
3627 6 : opParam.root = root;
3628 6 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
3629 6 : opParam.opType = HcclCMDType::HCCL_CMD_SCATTER;
3630 :
3631 6 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_SCATTER, opParam));
3632 :
3633 4 : return HCCL_SUCCESS;
3634 8 : }
3635 :
3636 5 : HcclResult HcclCommunicator::Reduce(const std::string &tag, void *inputPtr, void *outputPtr, u64 count,
3637 : HcclDataType dataType, HcclReduceOp op, u32 root, HcclRtStream stream)
3638 : {
3639 5 : CHK_RET(CheckSuspendingStatus());
3640 5 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
3641 0 : HCCL_ERROR("[%s][%s]Reduce Not Supported Yet",
3642 : LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_NOT_SUPPORTED.c_str());
3643 0 : return HCCL_E_NOT_SUPPORT;
3644 : }
3645 5 : bool aicpuUnfoldMode = false;
3646 5 : if (GetAicpuUnfoldConfig() == true &&
3647 5 : IsSupportSDMAReduce(inputPtr, outputPtr, dataType, op) && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
3648 0 : aicpuUnfoldMode = true;
3649 : }
3650 :
3651 5 : if (!IsAtomicInit()) {
3652 0 : HCCL_ERROR("[HcclCommunicator][Reduce]errNo[0x%016llx] hccl init must be called before call this function",
3653 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3654 0 : return HCCL_E_UNAVAIL;
3655 : }
3656 :
3657 5 : Stream streamObj(stream);
3658 5 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3659 :
3660 5 : u32 perDataSize = SIZE_TABLE[dataType];
3661 5 : u64 totalSize = count * perDataSize;
3662 5 : OpParam opParam;
3663 5 : opParam.tag = tag;
3664 5 : opParam.inputPtr = inputPtr;
3665 5 : opParam.inputSize = totalSize;
3666 5 : opParam.outputPtr = outputPtr;
3667 5 : opParam.outputSize = totalSize;
3668 5 : opParam.DataDes.count = count;
3669 5 : opParam.DataDes.dataType = dataType;
3670 5 : opParam.reduceType = op;
3671 5 : opParam.root = root;
3672 5 : opParam.stream = streamObj;
3673 5 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3674 5 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3675 5 : opParam.opType = HcclCMDType::HCCL_CMD_REDUCE;
3676 :
3677 5 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_REDUCE, opParam));
3678 :
3679 5 : return HCCL_SUCCESS;
3680 5 : }
3681 :
3682 2 : HcclResult HcclCommunicator::ReduceOutPlace(const std::string &tag, void *inputPtr, void *outputPtr, u64 count,
3683 : HcclDataType dataType, HcclReduceOp op, u32 root, HcclRtStream stream)
3684 : {
3685 2 : CHK_RET(CheckSuspendingStatus());
3686 2 : CHK_PRT_RET(Is310P3Common(isHaveCpuRank_, deviceType_),
3687 : HCCL_RUN_INFO("[ReduceOutPlace]This method cannot be invoked in the current scenario."), HCCL_SUCCESS);
3688 :
3689 2 : bool aicpuUnfoldMode = false;
3690 2 : if (GetAicpuUnfoldConfig() == true &&
3691 2 : IsSupportSDMAReduce(inputPtr, outputPtr, dataType, op) && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
3692 0 : aicpuUnfoldMode = true;
3693 : }
3694 :
3695 2 : bool isCapture = StreamIsCapture(stream);
3696 :
3697 4 : if (!IsAtomicInit()) {
3698 0 : HCCL_ERROR(
3699 : "[HcclCommunicator][ReduceOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
3700 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3701 0 : return HCCL_E_UNAVAIL;
3702 : }
3703 :
3704 4 : Stream streamObj(stream);
3705 4 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3706 :
3707 4 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3708 4 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3709 4 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
3710 4 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3711 :
3712 3 : u32 perDataSize = SIZE_TABLE[dataType];
3713 3 : u64 totalSize = count * perDataSize;
3714 3 : OpParam opParam;
3715 3 : opParam.tag = tag;
3716 4 : opParam.inputPtr = inputPtr;
3717 4 : opParam.inputSize = totalSize;
3718 4 : opParam.outputPtr = outputPtr;
3719 4 : opParam.outputSize = totalSize;
3720 4 : opParam.DataDes.count = count;
3721 4 : opParam.DataDes.dataType = dataType;
3722 4 : opParam.reduceType = op;
3723 4 : opParam.root = root;
3724 4 : opParam.stream = streamObj;
3725 2 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
3726 2 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3727 2 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3728 2 : opParam.isCapture = isCapture;
3729 2 : opParam.opType = HcclCMDType::HCCL_CMD_REDUCE;
3730 :
3731 2 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_REDUCE, opParam));
3732 :
3733 0 : return HCCL_SUCCESS;
3734 4 : }
3735 :
3736 12 : HcclResult HcclCommunicator::ReduceScatter(const std::string &tag, void *inputPtr, void *outputPtr, u64 count,
3737 : HcclDataType dataType, HcclReduceOp op, HcclRtStream stream, HcomCollOpInfo *opInfo)
3738 : {
3739 12 : CHK_RET(CheckSuspendingStatus());
3740 13 : bool aicpuUnfoldMode = false;
3741 13 : if (GetAicpuUnfoldConfig() == true &&
3742 12 : IsSupportSDMAReduce(inputPtr, outputPtr, dataType, op) && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
3743 0 : aicpuUnfoldMode = true;
3744 : }
3745 :
3746 12 : if (!IsAtomicInit()) {
3747 0 : HCCL_ERROR(
3748 : "[HcclCommunicator][ReduceScatter]errNo[0x%016llx] hccl init must be called before call this function",
3749 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3750 0 : return HCCL_E_UNAVAIL;
3751 : }
3752 :
3753 20 : Stream streamObj(stream);
3754 21 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3755 :
3756 21 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3757 18 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3758 15 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
3759 14 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3760 :
3761 13 : u32 perDataSize = SIZE_TABLE[dataType];
3762 :
3763 13 : OpParam opParam;
3764 14 : opParam.tag = tag;
3765 21 : opParam.inputPtr = inputPtr;
3766 21 : opParam.inputSize = userRankSize_ * count * perDataSize;
3767 21 : opParam.outputPtr = outputPtr;
3768 21 : opParam.outputSize = count * perDataSize;
3769 21 : opParam.DataDes.count = count;
3770 21 : opParam.DataDes.dataType = dataType;
3771 21 : opParam.reduceType = op;
3772 21 : opParam.stream = streamObj;
3773 19 : opParam.opType = HcclCMDType::HCCL_CMD_REDUCE_SCATTER;
3774 19 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3775 19 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3776 : // 用于inplace支持重执行场景的图模式归一至单算子模式
3777 19 : retryOrigWorkflowMode_ = GetWorkflowMode();
3778 0 : bool isHcclOpInplace = IsHcclOpInplace(HcclCMDType::HCCL_CMD_REDUCE_SCATTER, opParam, userRank_, userRankSize_,
3779 19 : isInplaceStatus_);
3780 20 : if (aicpuUnfoldMode && retryEnable_ && isHcclOpInplace) {
3781 0 : HCCL_DEBUG("The retry with inplace case is expected to be supported, "
3782 : "aicpuUnfoldMode[%d], retryEnable_[%d], isHcclOpInplace[%d], "
3783 : "therefore HcclWorkflowMode is converted from [%d] to HCCL_WORKFLOW_MODE_OP_BASE",
3784 : aicpuUnfoldMode, retryEnable_, isHcclOpInplace, static_cast<u8>(retryOrigWorkflowMode_));
3785 0 : CHK_RET(SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE));
3786 : }
3787 :
3788 20 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_REDUCE_SCATTER, opParam));
3789 :
3790 10 : CHK_RET(SetWorkflowMode(retryOrigWorkflowMode_));
3791 10 : return HCCL_SUCCESS;
3792 21 : }
3793 :
3794 22 : HcclResult HcclCommunicator::ReduceScatterOutPlace(const std::string &tag, void *inputPtr, void *outputPtr,
3795 : u64 count, HcclDataType dataType, HcclReduceOp op, HcclRtStream stream)
3796 : {
3797 22 : CHK_RET(CheckSuspendingStatus());
3798 26 : if (userRankSize_ > 1) {
3799 27 : CHK_RET(CreateCommCCLbuffer());
3800 : }
3801 :
3802 36 : bool aicpuUnfoldMode = false;
3803 36 : if (GetAicpuUnfoldConfig() == true &&
3804 16 : IsSupportSDMAReduce(cclBufferManager_.GetInCCLbuffer().ptr(), cclBufferManager_.GetOutCCLbuffer().ptr(),
3805 12 : dataType, op) &&
3806 53 : (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
3807 4 : aicpuUnfoldMode = true;
3808 : }
3809 :
3810 37 : bool isCapture = StreamIsCapture(stream);
3811 :
3812 37 : if (!IsAtomicInit()) {
3813 0 : HCCL_ERROR("[HcclCommunicator][ReduceScatterOutPlace]errNo[0x%016llx] hccl init must be called before"
3814 : " call this function",
3815 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3816 0 : return HCCL_E_UNAVAIL;
3817 : }
3818 :
3819 37 : Stream streamObj(stream);
3820 37 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3821 :
3822 37 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3823 37 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3824 37 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
3825 37 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3826 :
3827 35 : u32 perDataSize = SIZE_TABLE[dataType];
3828 :
3829 35 : OpParam opParam;
3830 34 : opParam.tag = tag;
3831 36 : opParam.inputPtr = inputPtr;
3832 36 : opParam.inputSize = userRankSize_ * count * perDataSize;
3833 36 : opParam.outputPtr = outputPtr;
3834 36 : opParam.outputSize = count * perDataSize;
3835 36 : opParam.DataDes.count = count;
3836 36 : opParam.DataDes.dataType = dataType;
3837 36 : opParam.reduceType = op;
3838 36 : opParam.stream = streamObj;
3839 37 : opParam.opType = HcclCMDType::HCCL_CMD_REDUCE_SCATTER;
3840 37 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3841 37 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3842 37 : opParam.isCapture = isCapture;
3843 37 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
3844 :
3845 37 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_REDUCE_SCATTER, opParam));
3846 :
3847 13 : return HCCL_SUCCESS;
3848 37 : }
3849 :
3850 0 : HcclResult HcclCommunicator::ReduceScatterV(const std::string &tag, void *inputPtr,
3851 : const void *inputCounts, const void *inputDispls, void *outputPtr, u64 outputCount,
3852 : HcclDataType dataType, HcclReduceOp op, HcclRtStream stream, HcomCollOpInfo *opInfo)
3853 : {
3854 0 : CHK_RET(CheckSuspendingStatus());
3855 0 : if (userRankSize_ == 1) {
3856 : // rankSize为1时,退化为ReduceScatter
3857 0 : return ReduceScatter(tag, inputPtr, outputPtr, outputCount, dataType, op, stream);
3858 : }
3859 :
3860 0 : if (!IsAtomicInit()) {
3861 0 : HCCL_ERROR(
3862 : "[HcclCommunicator][ReduceScatterV]errNo[0x%016llx] hccl init must be called before call this function",
3863 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3864 0 : return HCCL_E_UNAVAIL;
3865 : }
3866 :
3867 0 : Stream streamObj(stream);
3868 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3869 :
3870 0 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3871 0 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3872 0 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
3873 0 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3874 :
3875 0 : const bool aicpuUnfoldMode = GetAicpuUnfoldConfig() &&
3876 0 : IsSupportSDMAReduce(inputPtr, outputPtr, dataType, op) && (deviceType_ == DevType::DEV_TYPE_910_93);
3877 :
3878 0 : u32 perDataSize = SIZE_TABLE[dataType];
3879 0 : u64 inputSize = 0;
3880 0 : const u64 *counts = static_cast<const u64 *>(inputCounts);
3881 0 : for (u32 i = 0; i < userRankSize_; i++) {
3882 0 : inputSize += counts[i] * perDataSize;
3883 : }
3884 0 : CHK_PRT_RET(inputSize == 0, HCCL_WARNING("inputSize is 0, return ReduceScatterV success"), HCCL_SUCCESS);
3885 :
3886 0 : OpParam opParam;
3887 0 : opParam.tag = tag;
3888 0 : opParam.inputPtr = inputPtr;
3889 0 : opParam.inputSize = inputSize;
3890 0 : opParam.outputPtr = outputPtr;
3891 0 : opParam.outputSize = outputCount * perDataSize;
3892 0 : opParam.srcRank = userRank_; // rankId for access counts
3893 0 : opParam.VDataDes.counts = const_cast<void *>(inputCounts);
3894 0 : opParam.VDataDes.displs = const_cast<void *>(inputDispls);
3895 0 : opParam.VDataDes.dataType = dataType;
3896 0 : opParam.reduceType = op;
3897 0 : opParam.stream = streamObj;
3898 0 : opParam.opType = HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V;
3899 0 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3900 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3901 :
3902 0 : if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
3903 0 : for (u32 i = 0; i < userRankSize_; i++) {
3904 0 : HCCL_CONFIG_DEBUG(HCCL_ALG,
3905 : "[HcclCommunicator][ReduceScatterV]userRank_[%u], rankIdx[%u], inputCounts[%llu], inputDispls[%llu]",
3906 : userRank_, i, counts[i], static_cast<const u64 *>(inputDispls)[i]);
3907 : }
3908 : }
3909 :
3910 0 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V, opParam));
3911 :
3912 0 : return HCCL_SUCCESS;
3913 0 : }
3914 :
3915 0 : HcclResult HcclCommunicator::ReduceScatterVOutPlace(const std::string &tag, void *inputPtr, void *outputPtr,
3916 : const void *inputCounts, const void *inputDispls, u64 outputCount,
3917 : HcclDataType dataType, HcclReduceOp op, HcclRtStream stream)
3918 : {
3919 0 : CHK_RET(CheckSuspendingStatus());
3920 0 : if (userRankSize_ == 1) {
3921 : // rankSize为1时,退化为ReduceScatter
3922 0 : return ReduceScatterOutPlace(tag, inputPtr, outputPtr, outputCount, dataType, op, stream);
3923 : }
3924 :
3925 0 : CHK_RET(CreateCommCCLbuffer());
3926 0 : if (!IsAtomicInit()) {
3927 0 : HCCL_ERROR("[HcclCommunicator][ReduceScatterVOutPlace]errNo[0x%016llx] hccl init must be called before"
3928 : " call this function",
3929 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3930 0 : return HCCL_E_UNAVAIL;
3931 : }
3932 :
3933 0 : bool isCapture = StreamIsCapture(stream);
3934 :
3935 0 : Stream streamObj(stream);
3936 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3937 :
3938 0 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3939 0 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3940 0 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
3941 0 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3942 :
3943 0 : const bool aicpuUnfoldMode = GetAicpuUnfoldConfig() &&
3944 0 : IsSupportSDMAReduce(inputPtr, outputPtr, dataType, op) && (deviceType_ == DevType::DEV_TYPE_910_93);
3945 :
3946 0 : u32 perDataSize = SIZE_TABLE[dataType];
3947 0 : u64 inputSize = 0;
3948 0 : const u64 *counts = static_cast<const u64 *>(inputCounts);
3949 0 : for (u32 i = 0; i < userRankSize_; i++) {
3950 0 : inputSize += counts[i] * perDataSize;
3951 : }
3952 :
3953 0 : OpParam opParam;
3954 0 : opParam.tag = tag;
3955 0 : opParam.inputPtr = inputPtr;
3956 0 : opParam.inputSize = inputSize;
3957 0 : opParam.outputPtr = outputPtr;
3958 0 : opParam.outputSize = outputCount * perDataSize;
3959 0 : opParam.srcRank = userRank_; // rankId for access counts
3960 0 : opParam.VDataDes.counts = const_cast<void *>(inputCounts);
3961 0 : opParam.VDataDes.displs = const_cast<void *>(inputDispls);
3962 0 : opParam.VDataDes.dataType = dataType;
3963 0 : opParam.reduceType = op;
3964 0 : opParam.stream = streamObj;
3965 0 : opParam.opType = HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V;
3966 0 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
3967 0 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3968 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3969 0 : opParam.isCapture = isCapture;
3970 0 : opParam.rankSize = userRankSize_;
3971 :
3972 0 : if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
3973 0 : for (u32 i = 0; i < userRankSize_; i++) {
3974 0 : HCCL_CONFIG_DEBUG(HCCL_ALG,
3975 : "[HcclCommunicator][ReduceScatterVOutPlace]userRank_[%u],"
3976 : "rankIdx[%u], inputCounts[%llu], inputDispls[%llu]",
3977 : userRank_, i, counts[i], static_cast<const u64 *>(inputDispls)[i]);
3978 : }
3979 : }
3980 :
3981 0 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V, opParam));
3982 :
3983 0 : return HCCL_SUCCESS;
3984 0 : }
3985 :
3986 0 : HcclResult HcclCommunicator::BatchSendRecv(const std::string &tag, HcclSendRecvItem *sendRecvItemsPtr, u32 itemNum,
3987 : rtStream_t stream)
3988 : {
3989 0 : if (!IsAtomicInit()) {
3990 0 : HCCL_ERROR(
3991 : "[HcclCommunicator][BatchSendRecv]errNo[0x%016llx] hccl init must be called before call this function",
3992 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3993 0 : return HCCL_E_UNAVAIL;
3994 : }
3995 :
3996 0 : bool aicpuUnfoldMode = false;
3997 0 : if (GetAicpuUnfoldConfig() == true && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
3998 0 : aicpuUnfoldMode = true;
3999 : }
4000 :
4001 0 : bool isCapture = StreamIsCapture(stream);
4002 :
4003 0 : if (!IsAtomicInit()) {
4004 0 : HCCL_ERROR(
4005 : "[HcclCommunicator][BatchSendRecv]errNo[0x%016llx] hccl init must be called before call this function",
4006 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
4007 0 : return HCCL_E_UNAVAIL;
4008 : }
4009 0 : Stream streamObj(stream);
4010 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
4011 :
4012 0 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
4013 0 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
4014 0 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
4015 0 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
4016 0 : OpParam opParam;
4017 0 : opParam.tag = tag;
4018 0 : opParam.stream = streamObj;
4019 0 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
4020 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
4021 0 : opParam.isCapture = isCapture;
4022 0 : opParam.BatchSendRecvDataDes.sendRecvItemsPtr = sendRecvItemsPtr;
4023 0 : opParam.BatchSendRecvDataDes.itemNum = itemNum;
4024 0 : opParam.opType = HcclCMDType::HCCL_CMD_BATCH_SEND_RECV;
4025 0 : opParam.isGroupMode = isGroupMode_;
4026 0 : if (isGroupMode_) {
4027 0 : opParam.aicpuUnfoldMode = true; // A2的GroupSendRecv也走aicpu模式
4028 : }
4029 :
4030 0 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_BATCH_SEND_RECV, opParam));
4031 :
4032 0 : return HCCL_SUCCESS;
4033 0 : }
4034 :
4035 0 : HcclResult HcclCommunicator::Send(const std::string &tag, void *inputPtr, u64 count, HcclDataType dataType,
4036 : u32 destRank, rtStream_t stream, u32 srTag, u32 localGroupRank)
4037 : {
4038 0 : CHK_RET(CheckSuspendingStatus());
4039 0 : bool aicpuUnfoldMode = false;
4040 0 : if (GetAicpuUnfoldConfig() == true && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
4041 0 : aicpuUnfoldMode = true;
4042 : }
4043 :
4044 0 : if (!IsAtomicInit()) {
4045 0 : HCCL_ERROR("[HcclCommunicator][Send]errNo[0x%016llx] hccl init must be called before call this function",
4046 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
4047 0 : return HCCL_E_UNAVAIL;
4048 : }
4049 :
4050 0 : Stream streamObj(stream);
4051 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
4052 :
4053 0 : u32 perDataSize = SIZE_TABLE[dataType];
4054 0 : u64 totalSize = count * perDataSize;
4055 :
4056 0 : OpParam opParam;
4057 0 : opParam.tag = tag;
4058 0 : opParam.inputPtr = inputPtr;
4059 0 : opParam.inputSize = totalSize;
4060 0 : opParam.outputPtr = inputPtr;
4061 0 : opParam.outputSize = totalSize;
4062 0 : opParam.DataDes.count = count;
4063 0 : opParam.DataDes.dataType = dataType;
4064 0 : opParam.stream = streamObj;
4065 0 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
4066 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
4067 0 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
4068 0 : opParam.dstRank = destRank;
4069 0 : opParam.opType = HcclCMDType::HCCL_CMD_SEND;
4070 0 : opParam.srTag = srTag;
4071 0 : opParam.localGroupRank = localGroupRank;
4072 0 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_SEND, opParam));
4073 :
4074 0 : return HCCL_SUCCESS;
4075 0 : }
4076 :
4077 0 : HcclResult HcclCommunicator::SendOutPlace(const std::string &tag, void *inputPtr, u64 count, HcclDataType dataType,
4078 : u32 destRank, rtStream_t stream)
4079 : {
4080 0 : CHK_RET(CheckSuspendingStatus());
4081 0 : bool aicpuUnfoldMode = false;
4082 0 : if (GetAicpuUnfoldConfig() == true && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
4083 0 : aicpuUnfoldMode = true;
4084 : }
4085 :
4086 0 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
4087 0 : HCCL_ERROR("[%s][%s]SendOutPlace is not supported",
4088 : LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_NOT_SUPPORTED.c_str());
4089 0 : return HCCL_E_NOT_SUPPORT;
4090 : }
4091 0 : if (!IsAtomicInit()) {
4092 0 : HCCL_ERROR(
4093 : "[HcclCommunicator][SendOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
4094 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
4095 0 : return HCCL_E_UNAVAIL;
4096 : }
4097 :
4098 0 : bool isCapture = StreamIsCapture(stream);
4099 :
4100 0 : Stream streamObj(stream);
4101 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
4102 :
4103 0 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
4104 0 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
4105 0 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
4106 0 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
4107 :
4108 0 : u32 perDataSize = SIZE_TABLE[dataType];
4109 0 : u64 totalSize = count * perDataSize;
4110 :
4111 0 : OpParam opParam;
4112 0 : opParam.tag = tag;
4113 0 : opParam.inputPtr = inputPtr;
4114 0 : opParam.inputSize = totalSize;
4115 0 : opParam.outputPtr = inputPtr;
4116 0 : opParam.outputSize = totalSize;
4117 0 : opParam.DataDes.count = count;
4118 0 : opParam.DataDes.dataType = dataType;
4119 0 : opParam.stream = streamObj;
4120 0 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
4121 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
4122 0 : opParam.isCapture = isCapture;
4123 0 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
4124 0 : opParam.dstRank = destRank;
4125 0 : opParam.opType = HcclCMDType::HCCL_CMD_SEND;
4126 0 : opParam.localGroupRank = userRank_;
4127 0 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_SEND, opParam));
4128 :
4129 0 : return HCCL_SUCCESS;
4130 0 : }
4131 :
4132 0 : HcclResult HcclCommunicator::Receive(const std::string &tag, void *outputPtr, u64 count, HcclDataType dataType,
4133 : u32 srcRank, rtStream_t stream, u32 srTag, u32 localGroupRank)
4134 : {
4135 0 : CHK_RET(CheckSuspendingStatus());
4136 0 : bool aicpuUnfoldMode = false;
4137 0 : if (GetAicpuUnfoldConfig() == true && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
4138 0 : aicpuUnfoldMode = true;
4139 : }
4140 :
4141 0 : if (!IsAtomicInit()) {
4142 0 : HCCL_ERROR("[HcclCommunicator][Receive]errNo[0x%016llx] hccl init must be called before call this function",
4143 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
4144 0 : return HCCL_E_UNAVAIL;
4145 : }
4146 :
4147 0 : Stream streamObj(stream);
4148 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
4149 :
4150 0 : u32 perDataSize = SIZE_TABLE[dataType];
4151 0 : u64 totalSize = count * perDataSize;
4152 :
4153 0 : OpParam opParam;
4154 0 : opParam.tag = tag;
4155 0 : opParam.inputPtr = outputPtr;
4156 0 : opParam.inputSize = totalSize;
4157 0 : opParam.outputPtr = outputPtr;
4158 0 : opParam.outputSize = totalSize;
4159 0 : opParam.DataDes.count = count;
4160 0 : opParam.DataDes.dataType = dataType;
4161 0 : opParam.stream = streamObj;
4162 0 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
4163 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
4164 0 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
4165 0 : opParam.srcRank = srcRank;
4166 0 : opParam.opType = HcclCMDType::HCCL_CMD_RECEIVE;
4167 0 : opParam.srTag = srTag;
4168 0 : opParam.localGroupRank = localGroupRank;
4169 0 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_RECEIVE, opParam));
4170 :
4171 0 : return HCCL_SUCCESS;
4172 0 : }
4173 :
4174 0 : HcclResult HcclCommunicator::ReceiveOutPlace(const std::string &tag, void *outputPtr, u64 count,
4175 : HcclDataType dataType, u32 srcRank, rtStream_t stream)
4176 : {
4177 0 : CHK_RET(CheckSuspendingStatus());
4178 0 : bool aicpuUnfoldMode = false;
4179 0 : if (GetAicpuUnfoldConfig() == true && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
4180 0 : aicpuUnfoldMode = true;
4181 : }
4182 :
4183 0 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
4184 0 : HCCL_ERROR("[%s][%s]ReceiveOutPlace is not supported",
4185 : LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_NOT_SUPPORTED.c_str());
4186 0 : return HCCL_E_NOT_SUPPORT;
4187 : }
4188 0 : if (!IsAtomicInit()) {
4189 0 : HCCL_ERROR(
4190 : "[HcclCommunicator][ReceiveOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
4191 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
4192 0 : return HCCL_E_UNAVAIL;
4193 : }
4194 :
4195 0 : bool isCapture = StreamIsCapture(stream);
4196 :
4197 0 : Stream streamObj(stream);
4198 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
4199 :
4200 0 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
4201 0 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
4202 0 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
4203 0 : vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
4204 :
4205 0 : u32 perDataSize = SIZE_TABLE[dataType];
4206 0 : u64 totalSize = count * perDataSize;
4207 :
4208 0 : OpParam opParam;
4209 0 : opParam.tag = tag;
4210 0 : opParam.inputPtr = outputPtr;
4211 0 : opParam.inputSize = totalSize;
4212 0 : opParam.outputPtr = outputPtr;
4213 0 : opParam.outputSize = totalSize;
4214 0 : opParam.DataDes.count = count;
4215 0 : opParam.DataDes.dataType = dataType;
4216 0 : opParam.stream = streamObj;
4217 0 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
4218 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
4219 0 : opParam.isCapture = isCapture;
4220 0 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
4221 0 : opParam.srcRank = srcRank;
4222 0 : opParam.opType = HcclCMDType::HCCL_CMD_RECEIVE;
4223 0 : opParam.localGroupRank = userRank_;
4224 0 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_RECEIVE, opParam));
4225 :
4226 0 : return HCCL_SUCCESS;
4227 0 : }
4228 :
4229 0 : HcclResult HcclCommunicator::RegressCalPreOp(AlltoAllOperator *&alltoAllOperator, const OpParam &opParam,
4230 : std::unique_ptr<PreProcessMetaInfo> &preMetaInfo)
4231 : {
4232 0 : HCCL_INFO("Run with Graph, alloc new stream");
4233 0 : Stream stream(StreamType::STREAM_TYPE_ONLINE);
4234 0 : return RegressCalPreOp(alltoAllOperator, opParam, preMetaInfo, stream);
4235 0 : }
4236 :
4237 0 : HcclResult HcclCommunicator::RegressCalPreOp(AlltoAllOperator *&alltoAllOperator, const OpParam &opParam,
4238 : std::unique_ptr<PreProcessMetaInfo> &preMetaInfo, Stream &preProcessStream)
4239 : {
4240 0 : OpParam preProcessOpParam;
4241 0 : HcclWorkflowMode mode = GetWorkflowMode();
4242 0 : CHK_PRT_RET(mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_RESERVED, HCCL_ERROR("Invalid Workflow Mode[%d]", mode), HCCL_E_INTERNAL);
4243 :
4244 : // h to d
4245 0 : CHK_RET(SetInfoToDevice(preMetaInfo, preProcessStream));
4246 : // opParam准备
4247 0 : CHK_RET(alltoAllOperator->PreparePreOpParam(preProcessOpParam, preMetaInfo, preProcessStream));
4248 :
4249 : // 回归调用其它算子
4250 0 : HCCL_INFO("[HcclCommunicator][RegressCalPreOp] Regression calls other operators and opType[%u]",
4251 : preMetaInfo->opType);
4252 0 : CHK_RET(ExecOp(preMetaInfo->opType, preProcessOpParam));
4253 0 : CHK_RET(hcclStreamSynchronize(preProcessStream.ptr(), commConfig_.GetConfigExecTimeOut()));
4254 0 : HCCL_DEBUG("[HcclCommunicator][RegressCalPreOp] preProcess tag[%s].", preProcessOpParam.tag.c_str());
4255 0 : SetWorkflowMode(mode);
4256 :
4257 : // d to h
4258 0 : HostMem hostCollectBuffer = HostMem::alloc(preMetaInfo->outputSize);
4259 0 : CHK_PTR_NULL(hostCollectBuffer.ptr());
4260 0 : CHK_RET(GetInfoFromDevice(preMetaInfo, mode, hostCollectBuffer));
4261 :
4262 0 : hostCollectBuffer_ = hostCollectBuffer;
4263 0 : alltoAllOperator->SetPreProcessResult(std::move(hostCollectBuffer));
4264 0 : HCCL_INFO("[HcclCommunicator][RegressCalPreOp] run success!");
4265 0 : return HCCL_SUCCESS;
4266 0 : }
4267 :
4268 0 : HcclResult HcclCommunicator::SaveRankInfoHasLinked(const AlgResourceRequest& resRequest)
4269 : {
4270 0 : for (auto &levelNSubCommTransport : resRequest.opTransport) {
4271 0 : for (auto &singleSubCommTransport : levelNSubCommTransport) {
4272 0 : for (auto &transportRequest : singleSubCommTransport.transportRequests) {
4273 0 : if (transportRequest.isValid) {
4274 0 : ranksLinked_.insert(transportRequest.remoteUserRank);
4275 0 : HCCL_INFO("[HcclCommunicator][SaveRankInfoHasLinked]Insert remote Rank[%u] to ranksLinked Set.",
4276 : transportRequest.remoteUserRank);
4277 : }
4278 : }
4279 : }
4280 : }
4281 :
4282 0 : return HCCL_SUCCESS;
4283 : }
4284 :
4285 2 : HcclResult HcclCommunicator::GetCacheMap(std::unique_ptr<CollAlgOperator>& algOperator , OpParam& opParam,
4286 : AlgType& algType, bool selectAivAlg, std::string& newTag)
4287 : {
4288 2 : HcclCacheInfo cacheInfo;
4289 2 : CHK_RET(algOperator->GetCache(cacheInfo));
4290 2 : if (cacheInfo.isUseCache == false) {
4291 2 : return HCCL_SUCCESS;
4292 : }
4293 0 : cacheInfo.algType = algType;
4294 0 : cacheInfo.selectAivAlg = selectAivAlg;
4295 0 : cacheInfo.newTag = newTag;
4296 :
4297 0 : if (hcclCacheMap_.size() > CACHEMAP_MAXSIZE) {
4298 0 : size_t clearCount = static_cast<size_t>(CACHEMAP_MAXSIZE * CACHEMAP_CLEARPERCENT);
4299 0 : for (auto it = hcclCacheMap_.begin(); clearCount > 0 && it != hcclCacheMap_.end(); clearCount--) {
4300 0 : it = hcclCacheMap_.erase(it);
4301 : }
4302 : }
4303 :
4304 0 : hcclCacheMap_.emplace(std::make_pair(opParam, std::move(cacheInfo)));
4305 :
4306 0 : HCCL_INFO("[HcclCommunicator][GetCacheMap] algType %s, selectAivAlg %d, newTag %s", AlgTypeToStr(algType).c_str(),
4307 : selectAivAlg, newTag.c_str());
4308 0 : return HCCL_SUCCESS;
4309 2 : }
4310 :
4311 0 : HcclResult HcclCommunicator::ExecOpCache(HcclCMDType opType, OpParam &opParam, HcclCacheInfo& cacheInfo)
4312 : {
4313 : //可用核数也需要作为key的一部分,防止cache中拿出来的和计算出来的实际核数不一致
4314 : //cache目前仅支持executor的kernel为1的情况
4315 0 : cacheInfo.resourceArgs.buffersIn = cacheInfo.buffersIn;
4316 0 : cacheInfo.resourceArgs.buffersOut = cacheInfo.buffersOut;
4317 0 : cacheInfo.resourceArgs.stream = opParam.stream.ptr(); // 刷新cache下发的stream
4318 0 : cacheInfo.opArgs.input = opParam.inputPtr;
4319 0 : cacheInfo.opArgs.output = opParam.outputPtr;
4320 0 : AlgType& algType = cacheInfo.algType;
4321 0 : bool selectAivAlg = cacheInfo.selectAivAlg;
4322 0 : std::string newTag = cacheInfo.newTag;
4323 0 : HcclResult ret = HCCL_SUCCESS;
4324 : //更新aivtag
4325 0 : GetAivTag(1, opParam.isCapture, cacheInfo.resourceArgs.aivTag);
4326 0 : HCCL_INFO("[HcclCommunicator][ExecOpCache]buffersIn[%p] buffersOut[%p] tag[%s] opType[%d] "
4327 : "deterministic [%u] count[%llu] op[%d] userRank[%u] aiv tag [%d] stream [%d]",
4328 : cacheInfo.buffersIn, cacheInfo.buffersOut, identifier_.c_str(), opType, opParam.deterministic,
4329 : cacheInfo.opArgs.count, cacheInfo.opArgs.op, userRank_, cacheInfo.resourceArgs.aivTag, opParam.stream.id());
4330 0 : CHK_RET(HandleAclGraphFirstOpAivBuff(opParam.stream.ptr()));
4331 : //保留dfx
4332 0 : CHK_RET(RegisterDfxInfo(opParam, algType, resMap_[newTag].slaveStreams, selectAivAlg));
4333 : // 头计数
4334 0 : CHK_RET(StarsCounter(dispatcher_, opParam.stream, HEAD, opParam.aicpuUnfoldMode, retryEnable_, selectAivAlg));
4335 0 : u64 dataSize = (opParam.opType == HcclCMDType::HCCL_CMD_ALLTOALL ?
4336 0 : opParam.All2AllDataDes.sendCount * SIZE_TABLE[opParam.All2AllDataDes.sendType] : 0);
4337 0 : if (opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V || opType == HcclCMDType::HCCL_CMD_ALLGATHER_V ||
4338 0 : (opType == HcclCMDType::HCCL_CMD_ALLTOALL && dataSize >= AIV_ALL_TO_ALL_BIG_SIZE)) {
4339 0 : ret = ExecuteKernelLaunch(cacheInfo.opArgs, cacheInfo.topoArgs, cacheInfo.resourceArgs,
4340 0 : cacheInfo.algArgs, cacheInfo.extraArgs, cacheInfo.profilingInfo);
4341 : } else {
4342 0 : ret = ExecuteKernelLaunch(cacheInfo.opArgs, cacheInfo.topoArgs, cacheInfo.resourceArgs,
4343 0 : cacheInfo.algArgs, cacheInfo.profilingInfo);
4344 : }
4345 : //刷新核数
4346 0 : numBlocks_ = cacheInfo.resourceArgs.numBlocks;
4347 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[ExecOpCache]launch aiv failed, return[%d]", ret), ret);
4348 0 : CHK_RET(StarsCounter(dispatcher_, opParam.stream, TAIL, opParam.aicpuUnfoldMode, retryEnable_, selectAivAlg));
4349 0 : CHK_RET(UnRegisterDfxInfo(opParam, resMap_[newTag].slaveStreams));
4350 0 : if (selectAivAlg) {
4351 0 : aivClearEnable_ = false;
4352 : }
4353 0 : return HCCL_SUCCESS;
4354 0 : }
4355 :
4356 1 : void HcclCommunicator::SplitBsrData(OpParam &opParam, std::vector<u8>& isDirectRemoteRank,
4357 : std::vector<HcclSendRecvItem>& hostSendRecvInfo, std::vector<HcclSendRecvItem>& aicpuSendRecvInfo)
4358 : {
4359 1 : u32 itemNum = opParam.BatchSendRecvDataDes.itemNum;
4360 1 : isDirectRemoteRank.resize(userRankSize_);
4361 1 : HCCL_INFO("[HcclCommunicator][SplitBsrData] rankSize %u", userRankSize_);
4362 1 : HcclSendRecvItem* sendRecvInfo = opParam.BatchSendRecvDataDes.sendRecvItemsPtr;
4363 3 : for (u32 i = 0; i < itemNum; i++) {
4364 2 : if (sendRecvInfo->buf == nullptr) {
4365 2 : sendRecvInfo++;
4366 2 : continue;
4367 : }
4368 0 : if (remoteTransportMap_[sendRecvInfo->remoteRank] == TransportType::TRANS_TYPE_DEVICE_DIRECT) {
4369 : //host 侧需要下发的数据
4370 0 : HCCL_INFO("[HcclCommunicator][SplitBsrData]host localRank %u remoteRank %u type %d sendRecvType %d count %llu",
4371 : userRank_, sendRecvInfo->remoteRank, remoteTransportMap_[sendRecvInfo->remoteRank],
4372 : sendRecvInfo->sendRecvType, sendRecvInfo->count);
4373 0 : isDirectRemoteRank[sendRecvInfo->remoteRank] = true;
4374 0 : hostSendRecvInfo.push_back(*sendRecvInfo);
4375 : } else {
4376 : //aicpu侧需要下发的数据
4377 0 : HCCL_INFO("[HcclCommunicator][SplitBsrData]aicpu localRank %u remoteRank %u type %d sendRecvType %d count %llu",
4378 : userRank_, sendRecvInfo->remoteRank, remoteTransportMap_[sendRecvInfo->remoteRank],
4379 : sendRecvInfo->sendRecvType, sendRecvInfo->count);
4380 0 : isDirectRemoteRank[sendRecvInfo->remoteRank] = false;
4381 0 : aicpuSendRecvInfo.push_back(*sendRecvInfo);
4382 : }
4383 0 : sendRecvInfo++;
4384 : }
4385 1 : HCCL_INFO("[HcclCommunicator][SplitBsrData] itemNum %u hostItemNum %zu aicpuItemNum %zu", itemNum, hostSendRecvInfo.size(),
4386 : aicpuSendRecvInfo.size());
4387 1 : return;
4388 : }
4389 :
4390 0 : bool HcclCommunicator::IsReduceWithInt64OrProd(HcclCMDType opType, const OpParam &opParam) const
4391 : {
4392 0 : if (opType == HcclCMDType::HCCL_CMD_ALLREDUCE || opType == HcclCMDType::HCCL_CMD_REDUCE ||
4393 : opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER) {
4394 0 : if (opParam.reduceType == HcclReduceOp::HCCL_REDUCE_PROD ||
4395 0 : opParam.DataDes.dataType == HcclDataType::HCCL_DATA_TYPE_INT64) {
4396 0 : return true;
4397 : }
4398 : }
4399 :
4400 0 : if (opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V) {
4401 0 : if (opParam.reduceType == HcclReduceOp::HCCL_REDUCE_PROD ||
4402 0 : opParam.VDataDes.dataType == HcclDataType::HCCL_DATA_TYPE_INT64) {
4403 0 : return true;
4404 : }
4405 : }
4406 0 : return false;
4407 : }
4408 :
4409 71 : HcclResult HcclCommunicator::ExecOp(HcclCMDType opType, OpParam &opParam, bool isCustom)
4410 : {
4411 71 : CHK_PRT_RET(isInvalidComm_,
4412 : HCCL_ERROR("[HcclCommunicator][%s] comm[%s], rank[%u], devId[%d], snapshot recoverying, "
4413 : "this comm is invalid, no operator is allowed to execute.",
4414 : __func__, identifier_.c_str(), userRank_, deviceLogicId_), HCCL_E_UNAVAIL);
4415 :
4416 71 : if (retryEnable_ && needWarnAboutReduceProdInt64_ && IsReduceWithInt64OrProd(opType, opParam)) {
4417 0 : HCCL_RUN_WARNING("[HcclCommunicator][%s]comm[%s], opType[%d], reduceType[%d]. Reduce operators with prod operation or int64 data type. This operator type unsupportd for AICPU mode, retry disabled",
4418 : __func__, identifier_.c_str(), opType, opParam.reduceType);
4419 0 : needWarnAboutReduceProdInt64_ = false;
4420 : }
4421 71 : std::string tag = opParam.tag;
4422 70 : u32 aivCoreLimit = numBlocks_;
4423 : //单机AIV场景下cache复用,提升下发性能
4424 70 : if (implAlg_->GetAivModeConfig() && GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
4425 0 : if (aivCoreLimit == 0) {
4426 0 : aclError acl_ret = aclrtGetResInCurrentThread(ACL_RT_DEV_RES_VECTOR_CORE, &aivCoreLimit);
4427 0 : CHK_PRT_RET(acl_ret != ACL_SUCCESS,
4428 : HCCL_ERROR("[HcclCommunicator][ExecOp] aclrtGetResInCurrentThread failed, ret=[%d]", acl_ret),
4429 : HCCL_E_PARA);
4430 : }
4431 0 : opParam.deterministic = implAlg_->GetDeterministicConfig();
4432 0 : opParam.aivCoreLimit = aivCoreLimit;
4433 0 : auto it = hcclCacheMap_.find(opParam);
4434 0 : if (it != hcclCacheMap_.end()) {
4435 0 : CHK_RET(ExecOpCache(opType, opParam, it->second));
4436 0 : return HCCL_SUCCESS;
4437 : }
4438 : }
4439 :
4440 69 : ForceProf(opParam.isCapture);
4441 68 : opParam.supportSymmetricMemory = IsSupportSymmetricMemory(opType, opParam);
4442 72 : opParam.supportZeroCopy = !opParam.supportSymmetricMemory && IsSupportZeroCopy(opParam);
4443 72 : opParam.aclGraphZeroCopyEnable = GetConfigAclGraphZeroCopyEnable();
4444 70 : bool isInGraphCaptureZeroCopy = false;
4445 70 : zeroCopyAclGraph_->SetRetryEnable(retryEnable_);
4446 68 : isInGraphCaptureZeroCopy = zeroCopyAclGraph_->SetAclGraphZeroCopyMode(
4447 : deviceType_, opType, opParam, implAlg_.get(), cclBufferManager_.GetOutCCLbufferSize());
4448 72 : if (isInGraphCaptureZeroCopy && userRankSize_ > 1) {
4449 0 : CHK_RET(CreateCommCCLbuffer());
4450 : }
4451 72 : if (isShareComm_) {
4452 0 : CHK_RET(ShareCCLbufferMgr::GetInstance().CheckCCLbuffConflict(cclBuffName_, opParam.stream.id()));
4453 : }
4454 72 : std::unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(opType);
4455 63 : CHK_SMART_PTR_NULL(algOperator);
4456 : // 算法选择
4457 63 : std::string algName;
4458 63 : std::string newTag;
4459 63 : if (opParam.aicpuUnfoldMode) {
4460 : // 用于inplace支持重执行判断
4461 4 : CHK_RET(algOperator->SetRetryEnable(retryEnable_));
4462 : }
4463 63 : if (GetExternalInputHcclAivMode()) {
4464 : // 用于判断图模式是否清零
4465 0 : CHK_RET(algOperator->SetAivClearEnable(aivClearEnable_));
4466 : }
4467 :
4468 63 : std::unique_lock<std::mutex> lock(commResMutex_);
4469 71 : ResourceLimit limit;
4470 71 : limit.ifLimit = true;
4471 71 : limit.aivCoreLimit = aivCoreLimit;
4472 71 : AlgDesc algDesc;
4473 71 : algDesc.isLastSelect = true;
4474 71 : CHK_RET(algOperator->SelectAlg(opParam.tag, opParam, limit, algName, algDesc, newTag));
4475 66 : if (isOnlyAiv_ && !algDesc.isAivMode) {
4476 0 : std::string opTypeName = GetCMDTypeEnumStr(opType);
4477 0 : HCCL_ERROR("[HcclCommunicator][ExecOp] opType[%s] currently do not select aiv mode, aiv only not support.",
4478 : opTypeName.c_str());
4479 0 : return HCCL_E_NOT_SUPPORT;
4480 0 : }
4481 66 : CHK_RET(PrepareZeroCopy(algName, algDesc, opParam));
4482 :
4483 71 : if (opParam.isCapture) {
4484 : // aclgraph使用新的Tag,避免影响其他操作
4485 0 : newTag += "_Capture";
4486 : // aclgraph零拷贝场景下,每个算子都有单独的tag,需要记录,在graph销毁时清理相关资源
4487 0 : if (isInGraphCaptureZeroCopy) {
4488 0 : CHK_RET(AclgraphCallback::GetInstance().InsertNewTagToCaptureResMap(this, newTag, opParam));
4489 0 : tagsRequiringHostCleanup_.insert(newTag);
4490 : }
4491 : }
4492 :
4493 71 : if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE && userRankSize_ > 1) {
4494 40 : CHK_RET(CreateCommCCLbuffer());
4495 : }
4496 68 : if (hcclNslbDp::GetInstance().GetGlobalCommTaskId() != 0) {
4497 0 : NslbDp_CollectOperTable(opType, opParam, algOperator->GetAlgType(), algName);
4498 : }
4499 :
4500 : // 资源创建
4501 72 : if ((resMap_.find(newTag) != resMap_.end()) && opParam.isCapture) {
4502 0 : AlgResourceRequest resRequest;
4503 0 : CHK_RET(algOperator->CalcResRequest(algName, opParam, resRequest));
4504 0 : if (HasRoceTransportLinks(resRequest.opTransport)) {
4505 0 : auto resTmp = resMap_[newTag];
4506 0 : ++captureCnt_;
4507 0 : newTag += std::to_string(captureCnt_);
4508 0 : resMap_[newTag] = resTmp;
4509 0 : resRequest.isInGraphCaptureZeroCopy = isInGraphCaptureZeroCopy;
4510 0 : CHK_RET(CleanTransportLinks(resRequest.opTransport, resMap_[newTag].opTransportResponse));
4511 0 : if (IsEnableBackupLink()) {
4512 0 : CHK_RET(CleanTransportLinks(resRequest.opTransport, resMap_[newTag].opTransportResponseBackUp));
4513 : }
4514 : // 记录指令信息用于一致性校验
4515 0 : CHK_RET(RecordOpPara(opType, opParam));
4516 0 : CHK_RET(IncreAllocLink(newTag, opParam, resRequest, resMap_[newTag]));
4517 : // 移除tag对应的指令信息
4518 0 : CHK_RET(RankConsistentcyChecker::GetInstance().DelOpPara(opParam.tag));
4519 : // aclgraph零拷贝场景下,除第一个capture外,需要记录,在graph销毁时清理相关资源
4520 0 : CHK_RET(AclgraphCallback::GetInstance().InsertNewTagToCaptureResMap(this, newTag, opParam));
4521 0 : tagsRequiringHostCleanup_.insert(newTag);
4522 0 : }
4523 0 : }
4524 72 : InsertNewTagToTagMap(newTag, opParam.tag);
4525 72 : bool needIncreLink = false;
4526 : // aiv算法不需要申请host和device侧的从流
4527 72 : bool selectAivAlg = algDesc.isAivMode;
4528 72 : if (resMap_.find(newTag) == resMap_.end()) {
4529 73 : AlgResourceRequest resRequest;
4530 72 : CHK_RET(algOperator->CalcResRequest(algName, opParam, resRequest));
4531 73 : if (opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV) {
4532 0 : CHK_RET(SaveRankInfoHasLinked(resRequest));
4533 : }
4534 73 : resRequest.isInGraphCaptureZeroCopy = isInGraphCaptureZeroCopy;
4535 73 : CHK_RET(RecordOpPara(opType, opParam));
4536 69 : HcclResult ret = AllocAlgResource(newTag, opType, opParam, resRequest, resMap_[newTag], selectAivAlg);
4537 73 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[HcclCommunicator][ExecOp] AllocAlgResource failed, algName=[%s]", algName.c_str()), ret);
4538 20 : CHK_RET(RankConsistentcyChecker::GetInstance().DelOpPara(opParam.tag));
4539 :
4540 : // 对于91093超节点内aiv跨机通信算子,将不同机的CCLbuffer地址存在约定好的aiv将读取的HBM位置
4541 20 : CHK_RET(algOperator->PrepareCommInfoToDevice(algName, resMap_[newTag]));
4542 :
4543 20 : if (!isHaveCpuRank_) {
4544 20 : if (isUseRankPort_) {
4545 20 : std::vector<u32> &nicPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
4546 20 : std::vector<u32> &vnicPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
4547 20 : Heartbeat::GetInstance(deviceLogicId_).SetRankPortInfo(isUseRankPort_, nicPorts, vnicPorts, commPortConfig_.devPortSwitchOn);
4548 : }
4549 : // 开始注册心跳
4550 20 : if (opType == HcclCMDType::HCCL_CMD_SEND) {
4551 0 : CHK_RET(RegisterToHeartBeat(opParam.dstRank, tag));
4552 0 : hbSendRecvTags_.emplace(tag);
4553 20 : } else if (opType == HcclCMDType::HCCL_CMD_RECEIVE) {
4554 0 : CHK_RET(RegisterToHeartBeat(opParam.srcRank, tag));
4555 0 : hbSendRecvTags_.emplace(tag);
4556 : } else {
4557 20 : CHK_RET(RegisterToHeartBeat());
4558 : }
4559 : }
4560 19 : CHK_RET(UpdateZeroCopy(opParam, resMap_[newTag]));
4561 73 : } else if (opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV) {
4562 : // batchsendrecv需要根据任务来确定和哪些卡建链,因此复用tag,并在此基础上实现增量建链
4563 0 : AlgResourceRequest resRequest;
4564 0 : CHK_RET(algOperator->CalcIncreLinkRequest(algName, opParam, ranksLinked_, resRequest, needIncreLink));
4565 0 : if (needIncreLink) {
4566 0 : CHK_RET(RecordOpPara(opType, opParam));
4567 0 : CHK_RET(IncreAllocLink(newTag, opParam, resRequest, resMap_[newTag]));
4568 0 : CHK_RET(RankConsistentcyChecker::GetInstance().DelOpPara(opParam.tag));
4569 0 : opParam.needIncreLink = true;
4570 : }
4571 0 : }
4572 :
4573 : // 算法执行
4574 19 : if (selectAivAlg) {
4575 0 : CHK_RET(HandleAclGraphFirstOpAivBuff(opParam.stream.ptr()));
4576 0 : if (aivClearEnable_) {
4577 : // 用于判断图模式是否清零
4578 0 : CHK_RET(algOperator->SetAivClearEnable(aivClearEnable_));
4579 0 : aivOffloadTag_ = 1;
4580 : }
4581 0 : GetAivTag(algDesc.aivTagNum, opParam.isCapture, opParam.aivTag);
4582 0 : HCCL_INFO("[HcclCommunicator][ExecOp] tag[%s] userRank[%u] cur aiv tag [%d]",
4583 : identifier_.c_str(), userRank_, opParam.aivTag);
4584 0 : opParam.aicpuUnfoldMode = false;
4585 0 : opParam.aicpuCacheEnable = 0;
4586 0 : CHK_RET(algOperator->SetNumBlocks(aivCoreLimit));
4587 : }
4588 19 : std::vector<HcclSendRecvItem> hostSendRecvInfo;
4589 19 : std::vector<HcclSendRecvItem> aicpuSendRecvInfo;
4590 19 : std::vector<u8> isDirectRemoteRank;
4591 19 : if (opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV && deviceType_ == DevType::DEV_TYPE_910_93) {
4592 0 : SplitBsrData(opParam, isDirectRemoteRank, hostSendRecvInfo, aicpuSendRecvInfo);
4593 : // A3 bsr记录Direct下发方式数据
4594 0 : opParam.BatchSendRecvDataDes.isDirectRemoteRank = isDirectRemoteRank.data();
4595 0 : if (!retryEnable_) {
4596 0 : opParam.BatchSendRecvDataDes.sendRecvItemsPtr = aicpuSendRecvInfo.data();
4597 0 : opParam.BatchSendRecvDataDes.itemNum = aicpuSendRecvInfo.size();
4598 : }
4599 : }
4600 : // A2 Group SendRecv 将isDirectRemoteRank全部置为false
4601 19 : if (opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV && deviceType_ == DevType::DEV_TYPE_910B && isGroupMode_) {
4602 0 : isDirectRemoteRank.resize(userRankSize_, 0);
4603 0 : opParam.BatchSendRecvDataDes.isDirectRemoteRank = isDirectRemoteRank.data();
4604 : }
4605 19 : auto algType = algOperator->GetAlgType();
4606 19 : CHK_RET(RegisterDfxInfo(opParam, algType, resMap_[newTag].slaveStreams, selectAivAlg, tag));
4607 : // 头计数
4608 19 : CHK_RET(StarsCounter(dispatcher_, opParam.stream, HEAD, opParam.aicpuUnfoldMode, retryEnable_, selectAivAlg));
4609 19 : if (opParam.aicpuUnfoldMode) {
4610 0 : isInplaceStatus_ = 0;
4611 0 : inPlaceSupportRetryStatus_ = InplaceSupportRetryStatus::INPLACE_STATUS_END;
4612 : // algOperator->SupportRetryWithInplaceCheck 依赖 algOperator->SetRetryEnable 才能正确返回是否支持inplace
4613 :
4614 0 : inplaceSupportRetry_ = algOperator->SupportRetryWithInplaceCheck(
4615 0 : opType, opParam, algName, isInplaceStatus_, inPlaceSupportRetryStatus_);
4616 0 : HCCL_INFO("[HcclCommunicator][ExecOp] aicpu Unfold mode algType[%s], inplaceSupportRetry_[%d], opType[%d], "
4617 : "isInplaceStatus_[%d], inPlaceSupportRetryStatus_[%d].",
4618 : AlgTypeToStr(algType).c_str(), inplaceSupportRetry_, opType, isInplaceStatus_, inPlaceSupportRetryStatus_);
4619 0 : CHK_RET(OrchestrateAicpu(opType, algName, opParam, resMap_[newTag], newTag, algType, isCustom,
4620 : needIncreLink));
4621 : } else {
4622 : // HOST展开aclgraph场景,capture从流
4623 19 : if (!selectAivAlg) {
4624 22 : CHK_RET(CaptureSlaveStreams(opParam.stream.ptr(), resMap_[newTag].slaveStreams));
4625 : }
4626 19 : OpCounterInfo opCounter;
4627 19 : CHK_RET(GetOpCountInfo(opCounter));
4628 19 : CHK_RET(algOperator->SetOpCounter(opCounter));
4629 19 : CHK_RET(algOperator->Orchestrate(algName, opParam, resMap_[newTag]));
4630 16 : if (hostResMap_.find(newTag) == hostResMap_.end()) {
4631 16 : hostResMap_.insert(newTag);
4632 : }
4633 16 : CHK_RET(algOperator->GetNumBlocks(numBlocks_));
4634 16 : if (implAlg_->GetAivModeConfig() && GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE && !opParam.isCapture) {
4635 0 : CHK_RET(GetCacheMap(algOperator, opParam, algType, selectAivAlg, newTag));
4636 : }
4637 : }
4638 : //A3 bsr 只有走NPU直驱的时候hostSendRecvInfo才有内容
4639 16 : if (!hostSendRecvInfo.empty()) {
4640 : // A3 bsr获取到host侧需要下发的数据
4641 0 : HCCL_INFO("[HcclCommunicator][ExecOp] hostSendRecvInfo size %zu", hostSendRecvInfo.size());
4642 0 : opParam.BatchSendRecvDataDes.sendRecvItemsPtr = hostSendRecvInfo.data();
4643 0 : opParam.BatchSendRecvDataDes.itemNum = hostSendRecvInfo.size();
4644 0 : opParam.aicpuUnfoldMode = false;
4645 0 : opParam.aicpuCacheEnable = 0;
4646 0 : std::string tempTag;
4647 0 : std::unique_ptr<CollAlgOperator> newalgOperator = implAlg_->GetAlgOperator(opType);
4648 0 : CHK_SMART_PTR_NULL(newalgOperator);
4649 0 : CHK_RET(newalgOperator->SelectAlg(opParam.tag, opParam, limit, algName, algDesc, tempTag));
4650 0 : CHK_RET(newalgOperator->Orchestrate(algName, opParam, resMap_[newTag]));
4651 0 : }
4652 16 : lock.unlock();
4653 : // 尾计数
4654 16 : CHK_RET(StarsCounter(dispatcher_, opParam.stream, TAIL, opParam.aicpuUnfoldMode, retryEnable_, selectAivAlg));
4655 16 : CHK_RET(UnRegisterDfxInfo(opParam, resMap_[newTag].slaveStreams));
4656 16 : if (selectAivAlg) {
4657 0 : CHK_RET(algOperator->SetAivClearEnable(false));
4658 0 : aivClearEnable_ = false;
4659 : }
4660 16 : if (hcclNslbDp::GetInstance().GetGlobalCommTaskId() != 0 && hcclNslbDp::GetInstance().GetInitNetCoFlag() == true) {
4661 0 : AdjInfo nslbAdjInfo = {};
4662 0 : CHK_RET(algOperator->GetAdjInfo(algName, opParam, resMap_[newTag], nslbAdjInfo));
4663 0 : NslbDp_CollectSendAdjTable(opType, opParam, algOperator->GetAlgType(), nslbAdjInfo);
4664 0 : }
4665 16 : if (isInGraphCaptureZeroCopy) {
4666 0 : SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
4667 : }
4668 16 : return HCCL_SUCCESS;
4669 73 : }
4670 :
4671 0 : HcclResult HcclCommunicator::FreeScratchMemOnOpBaseMode(DeviceMem &scratchMem, const OpParam &opParam,
4672 : const HcclCMDType &opType)
4673 : {
4674 : // 当前单算子模式下scratch内存为手动申请,需要手动进行释放
4675 0 : if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE || IsForceAicpuOpBaseMode(opParam, opType)) {
4676 0 : scratchMem.free();
4677 : }
4678 0 : return HCCL_SUCCESS;
4679 : }
4680 :
4681 0 : HcclResult HcclCommunicator::ReAllocScratchMemForAlltoall(HcclCMDType opType, const OpParam &opParam,
4682 : AlgResourceRequest &resRequest, AlgResourceResponse &algResResponse)
4683 : {
4684 0 : if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB &&
4685 0 : !IsForceAicpuOpBaseMode(opParam, opType)) {
4686 0 : if (resRequest.scratchMemSize > 0) {
4687 0 : algResResponse.scratchMem = GetWorkspaceScracthMem(opParam.tag, resRequest.scratchMemSize);
4688 : }
4689 0 : HCCL_DEBUG("[%s] WorkflowMode set for workspace opType[%u] tag[%s]", __func__, opType, opParam.tag.c_str());
4690 0 : } else if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE ||
4691 0 : IsForceAicpuOpBaseMode(opParam, opType)) {
4692 0 : CHK_RET(AllocOpBaseModeScratchMem(opType, opParam, resRequest, algResResponse));
4693 0 : HCCL_DEBUG("[%s] WorkflowMode set for opType[%u] tag[%s]", __func__, opType, opParam.tag.c_str());
4694 : } else {
4695 0 : HCCL_ERROR("[%s] WorkflowMode is not set for opType[%u] tag[%s]", __func__, opType, opParam.tag.c_str());
4696 0 : return HCCL_E_PARA;
4697 : }
4698 0 : return HCCL_SUCCESS;
4699 : }
4700 :
4701 1 : HcclResult HcclCommunicator::HandleExistAlgResource(const std::string& newTag, const std::string& algName,
4702 : HcclCMDType opType, const OpParam& opParam, std::unique_ptr<CollAlgOperator>& algOperator,
4703 : bool selectAivAlg, bool aicpuUnfoldModeFor910B, bool needRecreateAlltoallComm)
4704 : {
4705 1 : if (needRecreateAlltoallComm) {
4706 0 : CHK_RET(hcclStreamSynchronize(opParam.stream.ptr(), commConfig_.GetConfigExecTimeOut()));
4707 :
4708 0 : AlgResourceRequest resRequest;
4709 0 : CHK_RET(algOperator->CalcResRequest(algName, opParam, resRequest));
4710 :
4711 : // 释放旧内存防止泄漏
4712 0 : CHK_RET(FreeScratchMemOnOpBaseMode(resMap_[newTag].scratchMem, opParam, opType));
4713 :
4714 0 : if (aicpuUnfoldModeFor910B) {
4715 0 : CHK_RET(ReAllocScratchMemForAlltoall(opType, opParam, resRequest, resMap_[newTag]));
4716 0 : isContextLaunched_ = true;
4717 : } else {
4718 0 : CHK_RET(RecordOpPara(opType, opParam));
4719 0 : CHK_RET(AllocAlgResource(newTag, opType, opParam, resRequest, resMap_[newTag], selectAivAlg));
4720 0 : CHK_RET(RankConsistentcyChecker::GetInstance().DelOpPara(opParam.tag));
4721 :
4722 0 : if (!isHaveCpuRank_) {
4723 0 : if (isUseRankPort_) {
4724 0 : std::vector<u32>& nicPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
4725 0 : std::vector<u32>& vnicPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
4726 0 : Heartbeat::GetInstance(deviceLogicId_).SetRankPortInfo(
4727 0 : isUseRankPort_, nicPorts, vnicPorts, commPortConfig_.devPortSwitchOn);
4728 : }
4729 0 : CHK_RET(RegisterToHeartBeat());
4730 : }
4731 : }
4732 0 : } else {
4733 1 : DeviceMem tinySendRecvMem;
4734 1 : CHK_RET(implAlg_->GetTinyMem(tinySendRecvMem));
4735 1 : CHK_RET(CalcTinySendRecvMem(opParam, resMap_[newTag], tinySendRecvMem));
4736 1 : }
4737 1 : return HCCL_SUCCESS;
4738 : }
4739 :
4740 5 : HcclResult HcclCommunicator::ExecOpAlltoAll(HcclCMDType opType, OpParam &opParam, bool isCustom)
4741 : {
4742 5 : CHK_PRT_RET(isInvalidComm_,
4743 : HCCL_ERROR("[HcclCommunicator][%s] comm[%s], rank[%u], devId[%d], snapshot recoverying, "
4744 : "this comm is invalid, no operator is allowed to execute.",
4745 : __func__, identifier_.c_str(), userRank_, deviceLogicId_), HCCL_E_UNAVAIL);
4746 :
4747 5 : std::string &tag = opParam.tag;
4748 5 : u32 aivCoreLimit = numBlocks_;
4749 : //单机AIV场景下cache复用,提升下发性能
4750 5 : if (implAlg_->GetAivModeConfig() && GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
4751 5 : if (aivCoreLimit == 0) {
4752 4 : aclError acl_ret = aclrtGetResInCurrentThread(ACL_RT_DEV_RES_VECTOR_CORE, &aivCoreLimit);
4753 4 : CHK_PRT_RET(acl_ret != ACL_SUCCESS,
4754 : HCCL_ERROR("[HcclCommunicator][ExecOpAlltoAll] aclrtGetResInCurrentThread failed, ret=[%d]", acl_ret),
4755 : HCCL_E_PARA);
4756 : }
4757 5 : opParam.deterministic = implAlg_->GetDeterministicConfig();
4758 5 : opParam.aivCoreLimit = aivCoreLimit;
4759 5 : auto it = hcclCacheMap_.find(opParam);
4760 5 : if (it != hcclCacheMap_.end()) {
4761 0 : CHK_RET(ExecOpCache(opType, opParam, it->second));
4762 0 : return HCCL_SUCCESS;
4763 : }
4764 : }
4765 :
4766 5 : ForceProf(opParam.isCapture);
4767 5 : bool isInGraphCaptureZeroCopy = false;
4768 5 : zeroCopyAclGraph_->SetRetryEnable(retryEnable_);
4769 5 : opParam.supportSymmetricMemory = IsSupportSymmetricMemory(opType, opParam);
4770 5 : opParam.supportZeroCopy = !opParam.supportSymmetricMemory && IsSupportZeroCopy(opParam);
4771 5 : opParam.aclGraphZeroCopyEnable = GetConfigAclGraphZeroCopyEnable();
4772 5 : isInGraphCaptureZeroCopy = zeroCopyAclGraph_->SetAclGraphZeroCopyMode(
4773 : deviceType_, opType, opParam, implAlg_.get(), cclBufferManager_.GetOutCCLbufferSize());
4774 5 : if (isInGraphCaptureZeroCopy && userRankSize_ > 1) {
4775 0 : CHK_RET(CreateCommCCLbuffer());
4776 : }
4777 5 : if (isShareComm_) {
4778 0 : CHK_RET(ShareCCLbufferMgr::GetInstance().CheckCCLbuffConflict(cclBuffName_, opParam.stream.id()));
4779 : }
4780 5 : std::unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(opType);
4781 5 : AlltoAllOperator *alltoAllOperator = dynamic_cast<AlltoAllOperator *>(algOperator.get());
4782 5 : CHK_PTR_NULL(alltoAllOperator);
4783 :
4784 5 : bool isSatisfyA2ACPForA3Condition = alltoAllOperator->IsSatisfyA2AContinuousPipelineFor91093Condition(opParam);
4785 5 : bool IsSatisfyA2ACPForA2Condition = alltoAllOperator->IsSatisfyAlltoallContinuousPipelineCondition(opParam);
4786 5 : if (IsSatisfyA2ACPForA2Condition || isSatisfyA2ACPForA3Condition) {
4787 0 : opParam.aicpuUnfoldMode = true;
4788 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
4789 : }
4790 :
4791 : // 算法选择
4792 5 : std::string algName;
4793 5 : std::string newTag;
4794 5 : if (opParam.aicpuUnfoldMode) {
4795 : // 用于inplace支持重执行判断
4796 0 : CHK_RET(algOperator->SetRetryEnable(retryEnable_));
4797 : }
4798 5 : std::unique_ptr<PreProcessMetaInfo> preMetaInfo = std::make_unique<PreProcessMetaInfo>();
4799 5 : CHK_SMART_PTR_NULL(preMetaInfo);
4800 :
4801 5 : bool preProcessFlag = alltoAllOperator->JudgeIfNeedPreProcessAndGetParam(opParam, preMetaInfo);
4802 5 : if (preProcessFlag) {
4803 0 : if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
4804 0 : CHK_RET(RegressCalPreOp(alltoAllOperator, opParam, preMetaInfo, const_cast<Stream &>(opParam.stream)));
4805 : } else {
4806 0 : CHK_RET(RegressCalPreOp(alltoAllOperator, opParam, preMetaInfo));
4807 : }
4808 : }
4809 :
4810 5 : if (deviceType_ == DevType::DEV_TYPE_910B && userRankSize_ > 1) {
4811 : // 用于AIV支持Roce直驱判断
4812 5 : CHK_RET(IsSupportAIVNormalQP(devicePhyId_, opParam.supportRoceDirect));
4813 : }
4814 :
4815 5 : std::unique_lock<std::mutex> lock(commResMutex_);
4816 5 : ResourceLimit limit;
4817 5 : limit.ifLimit = true;
4818 5 : limit.aivCoreLimit = aivCoreLimit;
4819 5 : AlgDesc algDesc;
4820 5 : algDesc.isLastSelect = true;
4821 5 : CHK_RET(algOperator->SelectAlg(opParam.tag, opParam, limit, algName, algDesc, newTag));
4822 : // 是否是AIV直驱Roce场景
4823 5 : opParam.isNpuDirectRoce = algName == "AlltoAllDirectFullmeshAIVExecutor";
4824 5 : if (isOnlyAiv_ && !algDesc.isAivMode) {
4825 0 : std::string opTypeName = GetCMDTypeEnumStr(opType);
4826 0 : HCCL_ERROR("[HcclCommunicator][ExecOp] opType[%s] currently do not select aiv mode, aiv only not support.",
4827 : opTypeName.c_str());
4828 0 : return HCCL_E_NOT_SUPPORT;
4829 0 : }
4830 5 : CHK_RET(PrepareZeroCopy(algName, algDesc, opParam));
4831 :
4832 5 : if (opParam.isCapture) {
4833 : // aclgraph使用新的Tag,避免影响其他操作
4834 3 : newTag += "_Capture";
4835 : // aclgraph零拷贝场景下,每个算子都有单独的tag,需要记录,在graph销毁时清理相关资源
4836 3 : if (isInGraphCaptureZeroCopy) {
4837 0 : CHK_RET(AclgraphCallback::GetInstance().InsertNewTagToCaptureResMap(this, newTag, opParam));
4838 0 : tagsRequiringHostCleanup_.insert(newTag);
4839 : }
4840 : }
4841 :
4842 0 : auto isSupportAlg = [](const std::string &algName, bool aicpuUnfoldMode) -> bool {
4843 0 : return ((algName == "RunAlltoAllVFullMesh" || algName == "RunAlltoAllVTwoLevelPipeline") && aicpuUnfoldMode) ||
4844 0 : (algName == "RunAlltoAllDirectFullmesh" || algName == "RunAlltoAllFullMeshSymmetricMemory");
4845 : };
4846 5 : bool isOpbaseMode = GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE;
4847 5 : if ((isOpbaseMode && userRankSize_ > 1) || (isSupportAlg(algName, opParam.aicpuUnfoldMode))) {
4848 5 : CHK_RET(CreateCommCCLbuffer());
4849 : }
4850 : // 资源创建
4851 5 : bool selectAivAlg = algDesc.isAivMode;
4852 5 : if ((resMap_.find(newTag) != resMap_.end()) && opParam.isCapture) {
4853 1 : AlgResourceRequest resRequest;
4854 1 : CHK_RET(algOperator->CalcResRequest(algName, opParam, resRequest));
4855 1 : if (HasRoceTransportLinks(resRequest.opTransport)) {
4856 0 : auto resTmp = resMap_[newTag];
4857 0 : ++captureCnt_;
4858 0 : newTag += std::to_string(captureCnt_);
4859 0 : resMap_[newTag] = resTmp;
4860 0 : resRequest.isInGraphCaptureZeroCopy = isInGraphCaptureZeroCopy;
4861 0 : CHK_RET(CleanTransportLinks(resRequest.opTransport, resMap_[newTag].opTransportResponse));
4862 0 : if (IsEnableBackupLink()) {
4863 0 : CHK_RET(CleanTransportLinks(resRequest.opTransport, resMap_[newTag].opTransportResponseBackUp));
4864 : }
4865 : // 记录指令信息用于一致性校验
4866 0 : CHK_RET(RecordOpPara(opType, opParam));
4867 0 : CHK_RET(IncreAllocLink(newTag, opParam, resRequest, resMap_[newTag]));
4868 : // 移除tag对应的指令信息
4869 0 : CHK_RET(RankConsistentcyChecker::GetInstance().DelOpPara(opParam.tag));
4870 : // aclgraph零拷贝场景下,除第一个capture外,需要记录,在graph销毁时清理相关资源
4871 0 : CHK_RET(AclgraphCallback::GetInstance().InsertNewTagToCaptureResMap(this, newTag, opParam));
4872 0 : tagsRequiringHostCleanup_.insert(newTag);
4873 0 : }
4874 1 : }
4875 5 : InsertNewTagToTagMap(newTag, opParam.tag);
4876 : bool aicpuUnfoldModeFor910B =
4877 5 : deviceType_ == DevType::DEV_TYPE_910B && opParam.aicpuUnfoldMode &&
4878 0 : (algName == "RunAlltoAllVStaged" || algName == "RunAlltoAllVFullMesh");
4879 5 : bool needRecreateAlltoallComm = false;
4880 5 : if (resMap_.find(newTag) == resMap_.end()) {
4881 4 : AlgResourceRequest resRequest;
4882 4 : CHK_RET(algOperator->CalcResRequest(algName, opParam, resRequest));
4883 4 : resRequest.isInGraphCaptureZeroCopy = isInGraphCaptureZeroCopy;
4884 4 : CHK_RET(RecordOpPara(opType, opParam));
4885 4 : CHK_RET(AllocAlgResource(newTag, opType, opParam, resRequest, resMap_[newTag], selectAivAlg));
4886 4 : CHK_RET(RankConsistentcyChecker::GetInstance().DelOpPara(opParam.tag));
4887 4 : if (opParam.isNpuDirectRoce) {
4888 : // AIV直驱roce多机场景,需要生成RMAInfo并拷贝至Device
4889 0 : CHK_RET(GenAiRMAInfoV2(newTag));
4890 0 : CHK_RET(H2DAiRMAInfoV2(newTag, opParam.stream.ptr()));
4891 : }
4892 : // 对于91093超节点内aiv跨机通信算子,将不同机的CCLbuffer地址存在约定好的aiv将读取的HBM位置
4893 4 : CHK_RET(algOperator->PrepareCommInfoToDevice(algName, resMap_[newTag]));
4894 :
4895 4 : if (!isHaveCpuRank_) {
4896 4 : if (isUseRankPort_) {
4897 4 : std::vector<u32> &nicPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
4898 4 : std::vector<u32> &vnicPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
4899 4 : Heartbeat::GetInstance(deviceLogicId_).SetRankPortInfo(isUseRankPort_, nicPorts, vnicPorts, commPortConfig_.devPortSwitchOn);
4900 : }
4901 4 : CHK_RET(RegisterToHeartBeat());
4902 : }
4903 4 : CHK_RET(UpdateZeroCopy(opParam, resMap_[newTag]));
4904 4 : } else {
4905 1 : CHK_RET(alltoAllOperator->CheckNeedRecreateComm(algName, opParam, resMap_[newTag].scratchMem.size(),
4906 : needRecreateAlltoallComm));
4907 1 : HCCL_INFO("resMap_ find this newTag[%s], and need to judge whether recreate comm [%d]", newTag.c_str(),
4908 : needRecreateAlltoallComm);
4909 1 : CHK_RET(HandleExistAlgResource(newTag, algName, opType, opParam, algOperator,
4910 : selectAivAlg, aicpuUnfoldModeFor910B, needRecreateAlltoallComm));
4911 : }
4912 5 : auto &algRes = resMap_[newTag];
4913 :
4914 5 : if (hcclNslbDp::GetInstance().GetGlobalCommTaskId() != 0 && hcclNslbDp::GetInstance().GetInitNetCoFlag() == true) {
4915 : /* NSLB 填充 表 */
4916 0 : u32 srcLocalRankId = userRank_;
4917 0 : u32 rootRank = (opParam.root == INVALID_VALUE_RANKID) ? 0 : opParam.root;
4918 0 : AlgType nslbAlgType = algOperator->GetAlgType();
4919 0 : AlgTypeLevel1 algValue = nslbAlgType.algoLevel1;
4920 0 : uint8_t nslbAlg = hcclNslbDp::GetInstance().GetNslbLevel1AlgType(algValue);
4921 :
4922 0 : if (algName == "RunAlltoAllVFullMesh" || algName == "RunAlltoAllDirectFullmesh") {
4923 0 : nslbAlg = NSLBDP_PAIRWISE;
4924 0 : if (deviceType_ == DevType::DEV_TYPE_910_93) {
4925 0 : nslbAlg = NSLB_ALGO_TYPE_FULLMESH;
4926 : }
4927 : }
4928 :
4929 0 : std::string nslb_identifier = identifier_;
4930 0 : HCCL_INFO("NSLBDP-SWK NslbDp_CollectOperTable nslb_identifier[%s] .", nslb_identifier.c_str());
4931 0 : u32 rankSize = userRankSize_;
4932 0 : u64 count = opParam.All2AllDataDes.sendCount * SIZE_TABLE[opParam.All2AllDataDes.sendType];
4933 : // 填充表2
4934 0 : hcclNslbDp::GetInstance().GenerateOpAndAdjTable(opType, rootRank, srcLocalRankId, nslbAlg, nslb_identifier, count, rankSize);
4935 0 : AdjInfo nslbAdjInfo = {};
4936 0 : CHK_RET(algOperator->GetAdjInfo(algName, opParam, algRes, nslbAdjInfo));
4937 0 : HCCL_INFO("[NSLBDP-WEN]-nslbAdjInfosize[%u]-algName[%s]-rankSize[%u]-commDesc[%s]..",
4938 : nslbAdjInfo.dstRankNum, algName.c_str(), userRankSize_, identifier_.c_str());
4939 : // 填充表3
4940 0 : hcclNslbDp::GetInstance().GetAlgAdjacencyTable(opType, srcLocalRankId, rootRank, nslbAlg, nslb_identifier, nslbAdjInfo);
4941 : /*发送流程*/
4942 0 : hcclNslbDp::GetInstance().SendAlgorithmInfoTable();
4943 0 : }
4944 : // 算法执行
4945 5 : if (opParam.isNpuDirectRoce) {
4946 : // AIV直驱roce多机场景,需要生成RMAInfo并拷贝至Device
4947 0 : CHK_PTR_NULL(combinOparaMem_);
4948 0 : HcclCombinOpParam *combinOparaPtr = reinterpret_cast<HcclCombinOpParam*>(combinOparaMem_->ptr());
4949 0 : CHK_PTR_NULL(combinOparaPtr);
4950 0 : CHK_RET(algOperator->SetRmaInfo(combinOparaPtr->aiRMAInfo));
4951 : }
4952 5 : if (selectAivAlg) {
4953 5 : CHK_RET(HandleAclGraphFirstOpAivBuff(opParam.stream.ptr()));
4954 5 : if (aivClearEnable_) {
4955 : // 用于判断图模式是否清零
4956 2 : CHK_RET(algOperator->SetAivClearEnable(aivClearEnable_));
4957 2 : aivOffloadTag_ = 1;
4958 : }
4959 5 : GetAivTag(algDesc.aivTagNum, opParam.isCapture, opParam.aivTag);
4960 5 : HCCL_INFO("[HcclCommunicator][ExecOpAlltoAll] tag[%s] userRank[%u] cur aiv tag [%d].",
4961 : identifier_.c_str(), userRank_, opParam.aivTag);
4962 5 : opParam.aicpuUnfoldMode = false;
4963 5 : opParam.aicpuCacheEnable = 0;
4964 5 : CHK_RET(algOperator->SetNumBlocks(aivCoreLimit));
4965 : }
4966 :
4967 5 : auto algType = algOperator->GetAlgType();
4968 5 : CHK_RET(RegisterDfxInfo(opParam, algType, algRes.slaveStreams, selectAivAlg, tag));
4969 : // 头计数
4970 5 : CHK_RET(StarsCounter(dispatcher_, opParam.stream, HEAD, opParam.aicpuUnfoldMode, retryEnable_, selectAivAlg));
4971 : // 算法执行
4972 0 : auto isSupportAicpuAlg = [](const std::string &algName) {
4973 : static const std::set<std::string> aicpuAlgs = {
4974 : "RunAlltoAllVFullMesh",
4975 : "RunAlltoAllDirectFullmesh",
4976 : "RunAlltoAllVTwoLevelPipeline",
4977 : "RunAlltoAllFullMeshSymmetricMemory",
4978 : "RunAlltoAllVContinuousPipeline",
4979 : "RunAlltoAllVPipelineFor91093"
4980 0 : };
4981 0 : return aicpuAlgs.count(algName) > 0;
4982 : };
4983 5 : if (opParam.aicpuUnfoldMode && (isSupportAicpuAlg(algName) || aicpuUnfoldModeFor910B)) {
4984 0 : isInplaceStatus_ = 0;
4985 0 : inPlaceSupportRetryStatus_ = InplaceSupportRetryStatus::INPLACE_STATUS_END;
4986 : // algOperator->SupportRetryWithInplaceCheck 依赖 algOperator->SetRetryEnable 才能正确返回是否支持inplace
4987 :
4988 0 : inplaceSupportRetry_ = algOperator->SupportRetryWithInplaceCheck(
4989 0 : opType, opParam, algName, isInplaceStatus_, inPlaceSupportRetryStatus_);
4990 0 : HCCL_INFO("[HcclCommunicator][ExecOp] aicpu Unfold mode algType[%s], inplaceSupportRetry_[%d], opType[%d], "
4991 : "isInplaceStatus_[%d], inPlaceSupportRetryStatus_[%d].",
4992 : AlgTypeToStr(algType).c_str(), inplaceSupportRetry_, opType, isInplaceStatus_, inPlaceSupportRetryStatus_);
4993 0 : CHK_RET(OrchestrateAicpu(opType, algName, opParam, algRes, newTag, algType, isCustom, false, needRecreateAlltoallComm));
4994 : } else {
4995 : // HOST展开aclgraph场景,capture从流
4996 5 : if (!selectAivAlg) {
4997 0 : CHK_RET(CaptureSlaveStreams(opParam.stream.ptr(), algRes.slaveStreams));
4998 : }
4999 5 : OpCounterInfo opCounter;
5000 5 : CHK_RET(GetOpCountInfo(opCounter));
5001 5 : CHK_RET(algOperator->SetOpCounter(opCounter));
5002 5 : CHK_RET(algOperator->Orchestrate(algName, opParam, algRes));
5003 : // for profiling, numBlocks upload
5004 5 : CHK_RET(algOperator->GetNumBlocks(numBlocks_));
5005 5 : if (implAlg_->GetAivModeConfig() && GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE && !opParam.isCapture) {
5006 2 : CHK_RET(GetCacheMap(algOperator, opParam, algType, selectAivAlg, newTag));
5007 : }
5008 : }
5009 5 : lock.unlock();
5010 : // 尾计数
5011 5 : CHK_RET(StarsCounter(dispatcher_, opParam.stream, TAIL, opParam.aicpuUnfoldMode, retryEnable_, selectAivAlg));
5012 5 : CHK_RET(UnRegisterDfxInfo(opParam, algRes.slaveStreams));
5013 5 : if (selectAivAlg) {
5014 5 : CHK_RET(algOperator->SetAivClearEnable(false));
5015 5 : aivClearEnable_ = false;
5016 : }
5017 :
5018 5 : if (isInGraphCaptureZeroCopy) {
5019 0 : SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
5020 : }
5021 5 : return HCCL_SUCCESS;
5022 5 : }
5023 :
5024 77 : HcclResult HcclCommunicator::RecordOpPara(HcclCMDType opType, const OpParam &opParam)
5025 : {
5026 77 : u32 aivCoreLimit = (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB) ? numBlocks_ : 0;
5027 77 : u8 deterministic = implAlg_->GetDeterministicConfig();
5028 77 : switch (opType) {
5029 65 : case HcclCMDType::HCCL_CMD_ALLGATHER:
5030 : case HcclCMDType::HCCL_CMD_ALLREDUCE:
5031 : case HcclCMDType::HCCL_CMD_REDUCE_SCATTER:
5032 : case HcclCMDType::HCCL_CMD_BROADCAST:
5033 65 : CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(opType,
5034 : opParam.tag, opParam.DataDes.count, opParam.DataDes.dataType, opParam.reduceType, opParam.root,
5035 : cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(),
5036 : identifier_.c_str(), ranktableCrc_, deterministic, aivCoreLimit));
5037 65 : break;
5038 8 : case HcclCMDType::HCCL_CMD_SCATTER:
5039 : case HcclCMDType::HCCL_CMD_REDUCE:
5040 8 : CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(opType,
5041 : opParam.tag, opParam.DataDes.count, opParam.DataDes.dataType, opParam.reduceType, opParam.root,
5042 : cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(),
5043 : identifier_.c_str(), ranktableCrc_, deterministic));
5044 8 : break;
5045 0 : case HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V:
5046 : case HcclCMDType::HCCL_CMD_ALLGATHER_V:
5047 0 : CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(opType,
5048 : opParam.tag, opParam.VDataDes.counts, opParam.VDataDes.displs, userRankSize_, opParam.VDataDes.dataType, opParam.reduceType,
5049 : cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(),
5050 : identifier_.c_str(), ranktableCrc_, deterministic, aivCoreLimit));
5051 0 : break;
5052 0 : case HcclCMDType::HCCL_CMD_BATCH_SEND_RECV:
5053 0 : CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(opType,
5054 : opParam.tag, cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(),
5055 : identifier_.c_str(), ranktableCrc_));
5056 0 : break;
5057 0 : case HcclCMDType::HCCL_CMD_SEND:
5058 0 : CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(opType,
5059 : opParam.tag, opParam.DataDes.count, opParam.DataDes.dataType, opParam.dstRank, opParam.srTag, opParam.localGroupRank,
5060 : cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(),
5061 : identifier_.c_str(), ranktableCrc_));
5062 0 : break;
5063 0 : case HcclCMDType::HCCL_CMD_RECEIVE:
5064 0 : CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(opType,
5065 : opParam.tag, opParam.DataDes.count, opParam.DataDes.dataType, opParam.srcRank, opParam.srTag, opParam.localGroupRank,
5066 : cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(),
5067 : identifier_.c_str(), ranktableCrc_));
5068 0 : break;
5069 0 : case HcclCMDType::HCCL_CMD_ALLTOALL:
5070 0 : CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(opType,
5071 : opParam.tag, opParam.All2AllDataDes.sendCount, opParam.All2AllDataDes.sendType, opParam.reduceType, opParam.root,
5072 : cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(),
5073 : identifier_.c_str(), ranktableCrc_, aivCoreLimit));
5074 0 : break;
5075 4 : case HcclCMDType::HCCL_CMD_ALLTOALLV:
5076 : case HcclCMDType::HCCL_CMD_ALLTOALLVC:
5077 4 : CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(opType,
5078 : opParam.tag, 0, HCCL_DATA_TYPE_RESERVED, opParam.reduceType, opParam.root,
5079 : cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(),
5080 : identifier_.c_str(), ranktableCrc_, aivCoreLimit));
5081 4 : break;
5082 0 : default:
5083 0 : break;
5084 : }
5085 77 : return HCCL_SUCCESS;
5086 : }
5087 5 : HcclResult HcclCommunicator::HandleAclGraphFirstOpAivBuff(rtStream_t mainStream)
5088 : {
5089 5 : aclmdlRI rtModel = nullptr;
5090 5 : bool isCapture = false;
5091 5 : u64 modelId = 0;
5092 5 : CHK_RET(GetStreamCaptureInfo(mainStream, rtModel, isCapture));
5093 5 : if (isCapture) {
5094 3 : CHK_PTR_NULL(rtModel);
5095 : // 获取不到modelId会报错
5096 3 : CHK_RET(GetModelId(rtModel, modelId));
5097 3 : if (captureModelIds_.find(modelId) == captureModelIds_.end()) {
5098 : // aclgraph场景,首算子清理AIV buff
5099 2 : aivClearEnable_ = true;
5100 2 : captureModelIds_.insert(modelId);
5101 2 : HCCL_INFO("[HcclCommunicator][%s] modelId[%u] is inserted to captureModelIds_", __func__, modelId);
5102 : }
5103 : }
5104 5 : return HCCL_SUCCESS;
5105 : }
5106 :
5107 88 : bool HcclCommunicator::StreamIsCapture(rtStream_t mainStream)
5108 : {
5109 88 : bool isCapture = false;
5110 88 : aclmdlRI rtModel = nullptr;
5111 88 : CHK_RET(GetStreamCaptureInfo(mainStream, rtModel, isCapture));
5112 93 : return isCapture;
5113 : }
5114 :
5115 19 : HcclResult HcclCommunicator::CaptureSlaveStreams(rtStream_t mainStream, vector<Stream> &slaveStreams)
5116 : {
5117 19 : if ((deviceType_ != DevType::DEV_TYPE_910_93) && (deviceType_ != DevType::DEV_TYPE_310P3) && (deviceType_ != DevType::DEV_TYPE_910B || GetExternalInputHcclEnableFfts())) {
5118 0 : HCCL_INFO("[HcclCommunicator][%s]Only 310P3 or A2 or A3 device in host expand mode need to capture slave streams.", __func__);
5119 0 : return HCCL_SUCCESS;
5120 : }
5121 19 : aclmdlRI rtModel = nullptr;
5122 19 : bool isCapture = false;
5123 19 : u64 modelId = 0;
5124 19 : CHK_RET(GetStreamCaptureInfo(mainStream, rtModel, isCapture));
5125 19 : if (isCapture) {
5126 0 : CHK_PTR_NULL(rtModel);
5127 0 : CHK_RET(GetModelId(rtModel, modelId));
5128 0 : for (auto slaveStream : slaveStreams) {
5129 0 : CHK_RET(AddStreamToModel(slaveStream.ptr(), rtModel));
5130 0 : HCCL_DEBUG("[HcclCommunicator][%s]Add stream[%d] to model[%u] success.", __func__, slaveStream.id(),
5131 : modelId);
5132 0 : }
5133 : }
5134 19 : return HCCL_SUCCESS;
5135 : }
5136 :
5137 0 : HcclResult HcclCommunicator::BuildOpLocalScratchMemResParam(
5138 : const AlgResourceResponse &algResource, const std::string &newTag, LocalResInfoV2 *localResHostPtr)
5139 : {
5140 0 : if (algResource.scratchMem.size() > 0) {
5141 0 : hostMemVec_.resize(hostMemVec_.size() + 1);
5142 0 : CHK_RET(AllocAndClearHostMem(sizeof(HccltagLocalResV2), hostMemVec_.back()));
5143 0 : HccltagLocalResV2 *tagLocalResHostPtr = static_cast<HccltagLocalResV2 *>(hostMemVec_.back().get()->ptr());
5144 :
5145 0 : deviceMemVec_.resize(deviceMemVec_.size() + 1);
5146 0 : CHK_RET(AllocAndClearDeviceMem(sizeof(HccltagLocalResV2), deviceMemVec_.back()));
5147 0 : HccltagLocalResV2 *tagLocalResDevicePtr = static_cast<HccltagLocalResV2 *>(deviceMemVec_.back().get()->ptr());
5148 :
5149 : // 初始化HcclRankRelationResV2中的tagRes链表
5150 0 : ListCommonInit(&tagLocalResDevicePtr->nextTagRes, &tagLocalResHostPtr->nextTagRes);
5151 : // 刷新host空间内容
5152 0 : CHK_SAFETY_FUNC_RET(
5153 : memcpy_s(tagLocalResHostPtr->tag, sizeof(tagLocalResHostPtr->tag), newTag.c_str(), newTag.length() + 1));
5154 0 : tagLocalResHostPtr->ScratchmemSize = algResource.scratchMem.size();
5155 0 : tagLocalResHostPtr->Scratchmem = reinterpret_cast<u64>(algResource.scratchMem.ptr());
5156 :
5157 : // 3、将节点插入链表头
5158 0 : ListCommonAddHead(&tagLocalResDevicePtr->nextTagRes,
5159 : &tagLocalResHostPtr->nextTagRes,
5160 : &localResHostPtr->nextTagRes,
5161 0 : &opResDeviceParaPtr_->localRes.nextTagRes);
5162 0 : HCCL_RUN_INFO("[HcclCommunicator][BuildOpLocalScratchMemResParam] LocalResHostPtr head addr[%p], nextHost[%p], "
5163 : "preHost[%p], tag LocalResHostPtr head addr[%p], nextHost[%p],"
5164 : "preHost[%p], tag[%s]",
5165 : &localResHostPtr->nextTagRes, localResHostPtr->nextTagRes.nextHost,
5166 : localResHostPtr->nextTagRes.preHost, &tagLocalResHostPtr->nextTagRes,
5167 : tagLocalResHostPtr->nextTagRes.nextHost, tagLocalResHostPtr->nextTagRes.preHost,
5168 : tagLocalResHostPtr->tag);
5169 : }
5170 0 : return HCCL_SUCCESS;
5171 : }
5172 :
5173 0 : HcclResult HcclCommunicator::CheckSetRetryStateToWaitResume()
5174 : {
5175 0 : if (retryEnable_ && opRetryManager_ != nullptr) {
5176 0 : HcclResult ret = opRetryManager_->SetRetryStateToWaitResume(identifier_, commConnections_.isRoot);
5177 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
5178 : HCCL_ERROR("[NsRecovery]set opretry state to wait resume timeout."), HCCL_E_INTERNAL);
5179 : }
5180 0 : return HCCL_SUCCESS;
5181 : }
5182 :
5183 0 : HcclResult HcclCommunicator::BuildOpLocalResParam(const AlgResourceResponse &algResource, const std::string &newTag)
5184 : {
5185 0 : LocalResInfoV2 *localResHostPtr = &opResPara_.localRes;
5186 0 : ListCommonInit(&opResDeviceParaPtr_->localRes.nextTagRes, &opResPara_.localRes.nextTagRes);
5187 0 : if (algResource.slaveDevStreams.size() > LOCAL_STREAM_MAX_NUM) {
5188 0 : HCCL_ERROR("[HcclCommunicator][BuildOpLocalResParam]Fail to assign stream for tag[%s]", newTag.c_str());
5189 0 : return HCCL_E_PARA;
5190 : }
5191 0 : auto signalM2SNum = algResource.notifiesDevMain.size();
5192 0 : auto signalS2MNum = algResource.notifiesDevAux.size();
5193 0 : auto signalNum = signalM2SNum + signalS2MNum;
5194 0 : if (signalNum > LOCAL_NOTIFY_MAX_NUM) {
5195 0 : HCCL_ERROR("[HcclCommunicator][BuildOpLocalResParam]Fail to assign local notify for tag[%s]", newTag.c_str());
5196 0 : return HCCL_E_PARA;
5197 : }
5198 :
5199 0 : localResHostPtr->streamNum = algResource.slaveDevStreams.size();
5200 0 : for (u32 i = 0; i < algResource.slaveDevStreams.size(); i++) {
5201 0 : localResHostPtr->streamParam[i].streamInfo.streamIds = algResource.slaveDevStreams[i].id();
5202 0 : localResHostPtr->streamParam[i].streamInfo.sqIds = algResource.slaveDevStreams[i].sqId();
5203 0 : localResHostPtr->streamParam[i].streamInfo.cqIds = algResource.slaveDevStreams[i].cqId();
5204 0 : localResHostPtr->streamParam[i].streamInfo.logicCqids = algResource.slaveDevStreams[i].logicCqId();
5205 0 : CHK_RET(AllocAndGetStreamContextBuff(algResource.slaveDevStreams[i].id(),
5206 : localResHostPtr->streamParam[i].sqCqContextAddr, localResHostPtr->streamParam[i].sqCqContextSize));
5207 : }
5208 :
5209 0 : localResHostPtr->signalNum = signalNum;
5210 :
5211 0 : for (u32 i = 0; i < signalM2SNum; i++) {
5212 0 : algResource.notifiesDevMain[i]->GetNotifyData(localResHostPtr->localSignals[i << 1]);
5213 0 : algResource.notifiesDevAux[i]->GetNotifyData(localResHostPtr->localSignals[(i << 1) + 1]);
5214 : }
5215 0 : HcclResult ret = HCCL_SUCCESS;
5216 0 : ret = CreateAndGetAiCpuNotify(localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_0)],
5217 0 : localResHostPtr->aicpuOpNotify[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_0)]);
5218 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
5219 : HCCL_ERROR("[HcclCommunicator][BuildOpLocalResParam]get aicpu notify 0 error,"
5220 : "errNo[0x%016llx]",
5221 : HCCL_ERROR_CODE(ret)),
5222 : ret);
5223 0 : ret = CreateAndGetAiCpuNotify(localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_1)],
5224 0 : localResHostPtr->aicpuOpNotify[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_1)]);
5225 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
5226 : HCCL_ERROR(
5227 : "[HcclCommunicator][BuildOpLocalResParam]get aicpu notify 1 error,errNo[0x%016llx]", HCCL_ERROR_CODE(ret)),
5228 : ret);
5229 :
5230 0 : if (opMainStream_.ptr() == nullptr) {
5231 0 : opMainStream_ = Stream(StreamType::STREAM_TYPE_DEVICE);
5232 : }
5233 0 : localResHostPtr->mainStreamParam.streamInfo.streamIds = opMainStream_.id();
5234 0 : localResHostPtr->mainStreamParam.streamInfo.sqIds = opMainStream_.sqId();
5235 0 : localResHostPtr->mainStreamParam.streamInfo.cqIds = opMainStream_.cqId();
5236 0 : localResHostPtr->mainStreamParam.streamInfo.logicCqids = opMainStream_.logicCqId();
5237 0 : CHK_RET(AllocAndGetStreamContextBuff(opMainStream_.id(),
5238 : localResHostPtr->mainStreamParam.sqCqContextAddr, localResHostPtr->mainStreamParam.sqCqContextSize));
5239 :
5240 : // 按序下发的aicpu控制流
5241 0 : if (aicpuOrderStream_.ptr() == nullptr) {
5242 0 : aicpuOrderStream_ = Stream(StreamType::STREAM_TYPE_DEVICE);
5243 : }
5244 0 : opResPara_.aicpuOrderStreamParam.streamInfo.streamIds = aicpuOrderStream_.id();
5245 0 : opResPara_.aicpuOrderStreamParam.streamInfo.sqIds = aicpuOrderStream_.sqId();
5246 0 : opResPara_.aicpuOrderStreamParam.streamInfo.cqIds = aicpuOrderStream_.cqId();
5247 0 : opResPara_.aicpuOrderStreamParam.streamInfo.logicCqids = aicpuOrderStream_.logicCqId();
5248 0 : CHK_RET(AllocAndGetStreamContextBuff(opResPara_.aicpuOrderStreamParam.streamInfo.streamIds,
5249 : opResPara_.aicpuOrderStreamParam.sqCqContextAddr,
5250 : opResPara_.aicpuOrderStreamParam.sqCqContextSize));
5251 :
5252 : #ifndef CCL_KERNEL_AICPU
5253 0 : for (u32 i = 0; i < AICPU_LOCAL_EVENT_SIZE; ++i) {
5254 0 : aclError ret = aclrtCreateEventExWithFlag(&localAicpuOpEvent_[i], ACL_EVENT_SYNC);
5255 0 : CHK_PRT_RET(ret != ACL_SUCCESS, HCCL_ERROR("[%s]aclrtCreateEventExWithFlag failed, ret[%d] event[%p].",
5256 : __func__, ret, localAicpuOpEvent_[i]), HCCL_E_RUNTIME);
5257 : }
5258 : #endif
5259 :
5260 0 : CHK_RET(BuildOpLocalScratchMemResParam(algResource, newTag, localResHostPtr));
5261 0 : return HCCL_SUCCESS;
5262 : }
5263 :
5264 0 : HcclResult HcclCommunicator::AllocAndGetStreamContextBuff(u32 streamId, u64 &addr, u64 &size)
5265 : {
5266 0 : if (streamIdToStreamContext_.find(streamId) == streamIdToStreamContext_.end()) {
5267 0 : DeviceMem streamContext;
5268 0 : CHK_RET(CreateWorkSpace(sizeof(SqCqeContext), streamContext));
5269 0 : streamIdToStreamContext_.insert({streamId, std::move(streamContext)});
5270 0 : }
5271 0 : addr = reinterpret_cast<u64>(streamIdToStreamContext_.at(streamId).ptr());
5272 0 : size = streamIdToStreamContext_.at(streamId).size();
5273 0 : HCCL_INFO("%s success, streamId:%u, addr:0x%llx, size:%llu", __func__, streamId, addr, size);
5274 0 : return HCCL_SUCCESS;
5275 : }
5276 :
5277 0 : u32 HcclCommunicator::UpdateOpIndex(const OpParam &opParam)
5278 : {
5279 0 : u32 opIndex = 0;
5280 0 : u32 commIndex = 0;
5281 : // 用于重执行和taskException打印的算子计数,bsr/sendrecv/其他算子分别计数
5282 0 : if (opParam.opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV) {
5283 0 : constexpr s32 batSendRecvIndex = -1; // batchSendRecv使用 key = -1
5284 0 : commIndex = batSendRecvIndex;
5285 0 : } else if (opParam.opType == HcclCMDType::HCCL_CMD_SEND) {
5286 0 : commIndex = opParam.dstRank;
5287 0 : } else if (opParam.opType == HcclCMDType::HCCL_CMD_RECEIVE) {
5288 0 : commIndex = opParam.srcRank;
5289 : } else {
5290 0 : commIndex = userRank_;
5291 : }
5292 :
5293 0 : auto it = opIndexMap_.find(commIndex);
5294 0 : if (it != opIndexMap_.end()) {
5295 0 : opIndex = ++(it->second);
5296 : } else {
5297 0 : opIndexMap_.insert({commIndex, 1});
5298 0 : opIndex = 1;
5299 : }
5300 :
5301 0 : HCCL_DEBUG("%s tag:%s opType:%u commIndex:%u opIndex:%u",
5302 : __func__, opParam.tag.c_str(), opParam.opType, commIndex, opIndex);
5303 0 : return opIndex;
5304 : }
5305 :
5306 0 : HcclResult HcclCommunicator::BuildAicpuCustomParam()
5307 : {
5308 0 : if (aicpuCustomDev_.ptr() == nullptr) {
5309 0 : CHK_RET(CreateWorkSpace(sizeof(AicpuCustomParam), aicpuCustomDev_));
5310 : }
5311 :
5312 0 : opResPara_.aicpuCustomParamAddr = reinterpret_cast<u64>(aicpuCustomDev_.ptr());
5313 0 : opResPara_.aicpuCustomParamSize = aicpuCustomDev_.size();
5314 0 : HCCL_INFO("%s success, aicpuCustomParamAddr:0x%llx, aicpuCustomParamSize:%llu",
5315 : __func__, opResPara_.aicpuCustomParamAddr, opResPara_.aicpuCustomParamSize);
5316 0 : return HCCL_SUCCESS;
5317 : }
5318 :
5319 0 : HcclResult HcclCommunicator::BuildAicpuOrderLaunchNotify()
5320 : {
5321 0 : if (aicpuOrderNotifyAddr_.ptr() == nullptr) {
5322 0 : CHK_RET(CreateWorkSpace(sizeof(HcclSignalInfo) * AICPU_ORDER_NOTIFY_MAX_NUM, aicpuOrderNotifyAddr_));
5323 : }
5324 :
5325 0 : opResPara_.aicpuOrderNotifyAddr = reinterpret_cast<u64>(aicpuOrderNotifyAddr_.ptr());
5326 0 : opResPara_.aicpuOrderNotifySize = aicpuOrderNotifyAddr_.size();
5327 0 : HCCL_INFO("%s success, aicpuOrderNotifyAddr:0x%llx, aicpuOrderNotifySize:%llu",
5328 : __func__, opResPara_.aicpuOrderNotifyAddr, opResPara_.aicpuOrderNotifySize);
5329 0 : return HCCL_SUCCESS;
5330 : }
5331 :
5332 0 : HcclResult HcclCommunicator::BuildAiRmaInfoParam(const std::string &newTag, const std::string &algName, const HcclCMDType opType)
5333 : {
5334 0 : HCCL_DEBUG("[HcclCommunicator][%s] Start prepare.", __func__);
5335 0 : CHK_PTR_NULL(aiRMAInfoMem_);
5336 0 : HcclAiRMAInfo *aiRMAInfoPtr = reinterpret_cast<HcclAiRMAInfo*>(aiRMAInfoMem_->ptr());
5337 0 : CHK_PTR_NULL(aiRMAInfoPtr);
5338 0 : aiRMAInfoPtr->curRankId = userRank_;
5339 0 : aiRMAInfoPtr->rankNum = userRankSize_;
5340 0 : u32 localRankSize = meshAggregationRankSize_;
5341 0 : LevelNSubCommTransport& commTransport = resMap_[newTag].opTransportResponse[COMM_LEVEL0];
5342 0 : CHK_PRT_RET(commTransport.size() <= 0, HCCL_ERROR("[%s] no LevelComm resource, please create comm first. "
5343 : "tag[%s], curRankId[%u] rankNum[%u]", __func__, newTag.c_str(), aiRMAInfoPtr->curRankId,
5344 : aiRMAInfoPtr->rankNum), HCCL_E_INTERNAL);
5345 0 : std::vector<LINK>& links = commTransport[0].links;
5346 0 : CHK_PRT_RET(links.size() <= 0, HCCL_ERROR("[%s] no transport resource, please create links first. "
5347 : "tag[%s], curRankId[%u] rankNum[%u]", __func__, newTag.c_str(), aiRMAInfoPtr->curRankId,
5348 : aiRMAInfoPtr->rankNum), HCCL_E_INTERNAL);
5349 :
5350 0 : LevelNSubCommTransport& tmpCommTransport = resMap_[newTag].opTransportResponse[COMM_MESH_L1];
5351 0 : CHK_PRT_RET(tmpCommTransport.size() <= 0, HCCL_ERROR("[%s] no LevelComm resource, please create comm first. "
5352 : "tag[%s], curRankId[%u] rankNum[%u]", __func__, newTag.c_str(), aiRMAInfoPtr->curRankId,
5353 : aiRMAInfoPtr->rankNum), HCCL_E_INTERNAL);
5354 0 : std::vector<LINK>& tmpLinks = tmpCommTransport[0].links;
5355 0 : CHK_PRT_RET(tmpLinks.size() <= 0, HCCL_ERROR("[%s] no transport resource, please create links first. "
5356 : "tag[%s], curRankId[%u] rankNum[%u]", __func__, newTag.c_str(), aiRMAInfoPtr->curRankId,
5357 : aiRMAInfoPtr->rankNum), HCCL_E_INTERNAL);
5358 :
5359 0 : CHK_RET(GetAivQPInfoV2(tmpLinks, newTag));
5360 0 : u32 tmpQueueSize = aiRMAInfoPtr->rankNum * aiRMAInfoPtr->qpNum;
5361 0 : u32 tmpMemSize = aiRMAInfoPtr->rankNum;
5362 0 : u32 tmpMemDetailSize = aiRMAInfoPtr->rankNum * AiMemMaxNum;
5363 :
5364 0 : CHK_RET(AllocAndClearHostMem(sizeof(HcclAiRMAWQ) * tmpQueueSize, aiSqMem_));
5365 0 : CHK_RET(AllocAndClearHostMem(sizeof(HcclAiRMACQ) * tmpQueueSize, aiScqMem_));
5366 0 : CHK_RET(AllocAndClearHostMem(sizeof(HcclAiRMAWQ) * tmpQueueSize, aiRqMem_));
5367 0 : CHK_RET(AllocAndClearHostMem(sizeof(HcclAiRMACQ) * tmpQueueSize, aiRcqMem_));
5368 0 : CHK_RET(AllocAndClearHostMem(sizeof(HcclAiRMAMemInfo) * tmpMemSize, aiMemMem_));
5369 0 : HcclAiRMAMemInfo *aiMemHost = reinterpret_cast<HcclAiRMAMemInfo*>(aiMemMem_->ptr());
5370 :
5371 0 : CHK_RET(AllocAndClearHostMem(sizeof(MemDetails) * tmpMemDetailSize, aiMemDetailsMem_));
5372 0 : MemDetails *aiMemDetailsHost = reinterpret_cast<MemDetails*>(aiMemDetailsMem_->ptr());
5373 :
5374 0 : CHK_RET(DeviceMem::alloc(aiMemDetailsDev_, aiMemDetailsMem_->size()));
5375 0 : u64 memBase = reinterpret_cast<uint64_t>(aiMemDetailsDev_.ptr());
5376 :
5377 0 : for (u32 i = 0; i < aiRMAInfoPtr->rankNum; i++)
5378 : {
5379 0 : MemDetails &remoteIn = aiMemDetailsHost[i * AiMemMaxNum +
5380 0 : GetAiMemTypeVal(HcclAiRMAMemType::REMOTE_INPUT)];
5381 0 : MemDetails &remoteOut = aiMemDetailsHost[i * AiMemMaxNum +
5382 0 : GetAiMemTypeVal(HcclAiRMAMemType::REMOTE_OUTPUT)];
5383 0 : MemDetails &localIn = aiMemDetailsHost[i * AiMemMaxNum +
5384 0 : GetAiMemTypeVal(HcclAiRMAMemType::LOCAL_INPUT)];
5385 0 : MemDetails &localOut = aiMemDetailsHost[i * AiMemMaxNum +
5386 0 : GetAiMemTypeVal(HcclAiRMAMemType::LOCAL_OUTPUT)];
5387 0 : if (i != aiRMAInfoPtr->curRankId && ((i % localRankSize) == (aiRMAInfoPtr->curRankId % localRankSize)
5388 0 : || (i / localRankSize) == (aiRMAInfoPtr->curRankId / localRankSize))) {
5389 0 : auto transport = links[i % localRankSize]; // localranksize个
5390 0 : if ((i % localRankSize) == (aiRMAInfoPtr->curRankId % localRankSize)){
5391 0 : transport = tmpLinks[i / localRankSize];// servernum个
5392 : }
5393 : // link rank info
5394 0 : CHK_RET(GetTransportRemoteMem(transport, UserMemType::INPUT_MEM, remoteIn));
5395 0 : CHK_RET(GetTransportRemoteMem(transport, UserMemType::OUTPUT_MEM, remoteOut));
5396 0 : CHK_RET(GetTransportLocalMem(transport, UserMemType::INPUT_MEM, localIn));
5397 0 : CHK_RET(GetTransportLocalMem(transport, UserMemType::OUTPUT_MEM, localOut));
5398 :
5399 0 : if (transport->GetTransportType() == TransportType::TRANS_TYPE_IBV_EXP) {
5400 0 : CHK_RET(GenIbvAiRMAInfo(i, transport, newTag, aiRMAInfoPtr));
5401 : }
5402 0 : }
5403 0 : else if (i == aiRMAInfoPtr->curRankId)
5404 : {
5405 0 : void *commInPtr = nullptr;
5406 0 : void *commOutPtr = nullptr;
5407 : u64 commInSize;
5408 : u64 commOutSize;
5409 0 : CHK_RET(cclBufferManager_.GetInCCLbuffer(commInPtr, commInSize));
5410 0 : CHK_RET(cclBufferManager_.GetOutCCLbuffer(commOutPtr, commOutSize));
5411 0 : localIn.addr = reinterpret_cast<uint64_t>(commInPtr);
5412 0 : localIn.size = commInSize;
5413 0 : localOut.addr = reinterpret_cast<uint64_t>(commOutPtr);
5414 0 : localOut.size = commOutSize;
5415 : }
5416 :
5417 0 : aiMemHost[i].memMaxNum = AiMemMaxNum;
5418 0 : aiMemHost[i].sizeOfMemDetails = static_cast<u32>(sizeof(MemDetails));
5419 0 : aiMemHost[i].memDetailPtr = memBase + i * AiMemMaxNum * aiMemHost[i].sizeOfMemDetails;
5420 :
5421 0 : HCCL_DEBUG("[%s] tag[%s] curRankId[%u] dstRankId[%u] rankNum[%u] qpNum[%u] memMaxNum[%u] sizeOfMemDetails[%u] "
5422 : "memDetailPtr[%p] remoteInAddr[%p] remoteInSize[%llu] remoteOutAddr[%p] "
5423 : "remoteOutSize[%llu] localInAddr[%p] localInSize[%llu] "
5424 : "localOutAddr[%p] localOutSize[%llu] ",
5425 : __func__, newTag.c_str(), aiRMAInfoPtr->curRankId, i, aiRMAInfoPtr->rankNum, aiRMAInfoPtr->qpNum,
5426 : aiMemHost[i].memMaxNum, aiMemHost[i].sizeOfMemDetails, aiMemHost[i].memDetailPtr,
5427 : remoteIn.addr, remoteIn.size, remoteOut.addr, remoteOut.size,
5428 : localIn.addr, localIn.size, localOut.addr, localOut.size);
5429 : }
5430 0 : return HCCL_SUCCESS;
5431 : }
5432 :
5433 : template <typename T>
5434 1 : HcclResult HcclCommunicator::CopyVectorToDeviceMem(const u64 len, DeviceMem &dstDeviceMem, const std::vector<T> &srcVec)
5435 : {
5436 1 : CHK_PRT_RET(!len,
5437 : HCCL_INFO("[HcclCommunicator][CopyVectorToDeviceMem] space size is zero. not need to malloc memory"),
5438 : HCCL_SUCCESS);
5439 :
5440 : CHK_PRT_RET((len > ULONG_MAX),
5441 : HCCL_ERROR("[HcclCommunicator][CopyVectorToDeviceMem] space size is greater than %llu", ULONG_MAX),
5442 : HCCL_E_PARA);
5443 :
5444 0 : CHK_RET(CreateWorkSpace(len, dstDeviceMem));
5445 0 : std::shared_ptr<HostMem> srcHostMem;
5446 0 : CHK_RET(AllocAndClearHostMem(len, srcHostMem));
5447 0 : std::copy(srcVec.begin(), srcVec.end(), static_cast<T *>(srcHostMem.get()->ptr()));
5448 0 : CHK_RET(hrtMemSyncCopy(
5449 : dstDeviceMem.ptr(), len, srcHostMem.get()->ptr(), len, HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
5450 0 : return HCCL_SUCCESS;
5451 0 : }
5452 :
5453 0 : HcclResult HcclCommunicator::BuildOpTopoResTlvParam(const std::string &algName,
5454 : const std::vector<std::vector<std::vector<u32>>> &inputVectorInfo, DeviceMem &dstTlvDeviceMem, u64 &tlvLen)
5455 : {
5456 0 : vector<u32> tlv;
5457 : CommonTlv commonTlv;
5458 0 : HCCL_DEBUG("[HcclCommunicator][BuildOpTopoResTlvParam] input vector size[%lu], group[%s].",
5459 : inputVectorInfo.size(), identifier_.c_str());
5460 0 : for (u16 level0Idx = 0; level0Idx < inputVectorInfo.size(); level0Idx++) {
5461 0 : for (u16 level1Idx = 0; level1Idx < inputVectorInfo[level0Idx].size(); level1Idx++) {
5462 0 : commonTlv.type = ((level0Idx << TOP_COMM_LEVEL0_SHIFT) | level1Idx);
5463 0 : commonTlv.length = (sizeof(LENGTH_TYPE) + sizeof(TAG_TYPE)) +
5464 0 : inputVectorInfo[level0Idx][level1Idx].size() * sizeof(RANK_TYPE);
5465 0 : tlv.push_back(commonTlv.type);
5466 0 : tlv.push_back(commonTlv.length);
5467 0 : tlv.insert(tlv.end(), inputVectorInfo[level0Idx][level1Idx].begin(),
5468 0 : inputVectorInfo[level0Idx][level1Idx].end());
5469 : }
5470 : }
5471 0 : for (u64 idx = 0; idx < tlv.size(); idx++) {
5472 0 : HCCL_DEBUG("[HcclCommunicator][BuildOpTopoResTlvParam] idx[%lu] tlv[%lu].", idx, tlv[idx]);
5473 : }
5474 0 : tlvLen = tlv.size() * sizeof(u32);
5475 0 : CHK_RET(CopyVectorToDeviceMem(tlvLen, dstTlvDeviceMem, tlv));
5476 0 : return HCCL_SUCCESS;
5477 0 : }
5478 :
5479 0 : HcclResult HcclCommunicator::BuildOpTopoResVectorTlvParam(const std::string &algName,
5480 : const std::vector<std::vector<std::vector<std::vector<u32>>>> &inputVectorInfo, DeviceMem &dstTlvDeviceMem, u64 &tlvLen)
5481 : {
5482 0 : vector<u32> tlv;
5483 : CommonTlv commonTlv;
5484 0 : HCCL_DEBUG("[HcclCommunicator][BuildOpTopoResVectorTlvParam] input vector size[%lu], group[%s]",
5485 : inputVectorInfo.size(), identifier_.c_str());
5486 0 : for (u16 level0Idx = 0; level0Idx < inputVectorInfo.size(); level0Idx++) {
5487 0 : for (u16 level1Idx = 0; level1Idx < inputVectorInfo[level0Idx].size(); level1Idx++) {
5488 0 : for (u16 level2Idx = 0; level2Idx < inputVectorInfo[level0Idx][level1Idx].size(); level2Idx++) {
5489 0 : commonTlv.type = (((level0Idx << TOP_HIERARCHICAL_COMM_LEVEL0_SHIFT) | level1Idx) << TOP_HIERARCHICAL_COMM_LEVEL1_SHIFT) | level2Idx;
5490 0 : commonTlv.length = (sizeof(LENGTH_TYPE) + sizeof(TAG_TYPE)) +
5491 0 : inputVectorInfo[level0Idx][level1Idx][level2Idx].size() * sizeof(RANK_TYPE);
5492 0 : tlv.push_back(commonTlv.type);
5493 0 : tlv.push_back(commonTlv.length);
5494 0 : tlv.insert(tlv.end(), inputVectorInfo[level0Idx][level1Idx][level2Idx].begin(),
5495 0 : inputVectorInfo[level0Idx][level1Idx][level2Idx].end());
5496 : }
5497 : }
5498 : }
5499 0 : for (u64 idx = 0; idx < tlv.size(); idx++) {
5500 0 : HCCL_DEBUG("[HcclCommunicator][BuildOpTopoResVectorTlvParam] idx[%lu] tlv[%lu]", idx, tlv[idx]);
5501 : }
5502 0 : tlvLen = tlv.size() * sizeof(u32);
5503 0 : CHK_RET(CopyVectorToDeviceMem(tlvLen, dstTlvDeviceMem, tlv));
5504 0 : return HCCL_SUCCESS;
5505 0 : }
5506 :
5507 0 : HcclResult HcclCommunicator::BuildPairLinkCounter(const std::string &algName)
5508 : {
5509 0 : constexpr u32 KEY_VALUE_TO_VECTOR_MODULUS = 2;
5510 0 : if (pairLinkCounterDevice_.ptr() == nullptr) {
5511 0 : u64 pairLinkCounterSize = pairLinkCounter_.size();
5512 0 : HCCL_DEBUG("[HcclCommunicator][BuildPairLinkCounter] pairLinkCounter size[%lu], group[%s]",
5513 : pairLinkCounterSize, identifier_.c_str());
5514 0 : std::vector<u32> pairLinkCounterVec(pairLinkCounterSize * KEY_VALUE_TO_VECTOR_MODULUS);
5515 0 : u64 index = 0;
5516 0 : for (auto &kt : pairLinkCounter_) {
5517 0 : pairLinkCounterVec[index] = kt.first;
5518 0 : pairLinkCounterVec[index + 1] = kt.second;
5519 0 : index += KEY_VALUE_TO_VECTOR_MODULUS; // 每次根据
5520 : }
5521 0 : u64 len = pairLinkCounterSize * sizeof(u32) * KEY_VALUE_TO_VECTOR_MODULUS; // key-value,都为u32
5522 0 : CHK_RET(CopyVectorToDeviceMem(len, pairLinkCounterDevice_, pairLinkCounterVec));
5523 0 : opResPara_.topoInfo.pairLinkCounter = reinterpret_cast<u64>(pairLinkCounterDevice_.ptr());
5524 0 : opResPara_.topoInfo.pairLinkCounterNum = pairLinkCounterSize * KEY_VALUE_TO_VECTOR_MODULUS;
5525 0 : }
5526 0 : return HCCL_SUCCESS;
5527 : }
5528 :
5529 0 : HcclResult HcclCommunicator::BuildIsUsedRdmaRank(const std::string &algName)
5530 : {
5531 0 : constexpr u32 KEY_VALUE_TO_VECTOR_MODULUS = 2;
5532 0 : if (isUsedRdmaRankPairDevice_.ptr() == nullptr) {
5533 0 : std::unordered_map<u32, bool> isUsedRdmaMap;
5534 0 : CHK_RET(implAlg_->GetIsUsedRdmaMap(isUsedRdmaMap));
5535 0 : u64 isUsedRdmaMapSize = isUsedRdmaMap.size();
5536 0 : HCCL_DEBUG("[HcclCommunicator][BuildIsUsedRdmaRank] is used Rdma rank size[%lu], group[%s]",
5537 : isUsedRdmaMapSize, identifier_.c_str());
5538 0 : std::vector<u32> isUsedRdmaPairVec(isUsedRdmaMapSize * KEY_VALUE_TO_VECTOR_MODULUS);
5539 0 : u64 index = 0;
5540 0 : for (auto &kt : isUsedRdmaMap) {
5541 0 : isUsedRdmaPairVec[index] = kt.first;
5542 0 : isUsedRdmaPairVec[index + 1] = static_cast<u32>(kt.second);
5543 0 : index += KEY_VALUE_TO_VECTOR_MODULUS;
5544 : }
5545 0 : u64 len = isUsedRdmaMapSize * sizeof(u32) * KEY_VALUE_TO_VECTOR_MODULUS; // key-value,都为u32
5546 0 : CHK_RET(CopyVectorToDeviceMem(len, isUsedRdmaRankPairDevice_, isUsedRdmaPairVec));
5547 0 : opResPara_.topoInfo.isUsedRdmaRankPair = reinterpret_cast<u64>(isUsedRdmaRankPairDevice_.ptr());
5548 0 : opResPara_.topoInfo.isUsedRdmaRankPairNum = isUsedRdmaMapSize * KEY_VALUE_TO_VECTOR_MODULUS;
5549 0 : }
5550 0 : return HCCL_SUCCESS;
5551 : }
5552 :
5553 0 : HcclResult HcclCommunicator::BuildNicList(const std::string &algName)
5554 : {
5555 0 : if (nicListDevice_.ptr() == nullptr) {
5556 0 : u64 len = nicList_.size() * sizeof(u32);
5557 0 : HCCL_DEBUG("[HcclCommunicator][BuildNicList] niclist size[%lu], group[%s]",
5558 : nicList_.size(), identifier_.c_str());
5559 0 : CHK_RET(CopyVectorToDeviceMem(len, nicListDevice_, nicList_));
5560 0 : opResPara_.topoInfo.nicList = reinterpret_cast<u64>(nicListDevice_.ptr());
5561 0 : opResPara_.topoInfo.nicNum = nicList_.size();
5562 : }
5563 0 : return HCCL_SUCCESS;
5564 : }
5565 :
5566 0 : HcclResult HcclCommunicator::BuildBridgeRank(const std::string &algName)
5567 : {
5568 0 : if (bridgeRankDevice_.ptr() == nullptr) {
5569 0 : std::vector<bool> isBridgeVector;
5570 0 : CHK_RET(implAlg_->GetIsBridgeVector(isBridgeVector));
5571 0 : u64 len = isBridgeVector.size() * sizeof(bool);
5572 0 : HCCL_DEBUG("[HcclCommunicator][BuildBridgeRank] Bridge size[%lu], group[%s]",
5573 : isBridgeVector.size(), identifier_.c_str());
5574 0 : CHK_RET(CopyVectorToDeviceMem(len, bridgeRankDevice_, isBridgeVector));
5575 0 : opResPara_.topoInfo.bridgeRank = reinterpret_cast<u64>(bridgeRankDevice_.ptr());
5576 0 : opResPara_.topoInfo.bridgeRankNum = isBridgeVector.size();
5577 0 : }
5578 0 : return HCCL_SUCCESS;
5579 : }
5580 :
5581 0 : HcclResult HcclCommunicator::BuildCommPlanRank(const std::string &algName)
5582 : {
5583 0 : opResPara_.topoInfo.complanRank = 0;
5584 0 : opResPara_.topoInfo.complanRankLength = 0;
5585 0 : if (complanRankDevice_.ptr() == nullptr) {
5586 0 : std::vector<std::vector<std::vector<u32>>> commPlaneRanks;
5587 0 : CHK_RET(implAlg_->GetCommPlaneRanks(commPlaneRanks));
5588 0 : u64 tlvLen = 0;
5589 0 : CHK_RET(BuildOpTopoResTlvParam(algName, commPlaneRanks, complanRankDevice_, tlvLen));
5590 0 : opResPara_.topoInfo.complanRank = reinterpret_cast<u64>(complanRankDevice_.ptr());
5591 0 : opResPara_.topoInfo.complanRankLength = tlvLen;
5592 0 : HCCL_DEBUG("[HcclCommunicator][BuildCommPlanRank] comm plane ranks tlv length[%lu], ptr[%p], group[%s], "
5593 : "local user rankId[%u] ",
5594 : tlvLen, complanRankDevice_.ptr(), identifier_.c_str(), userRank_);
5595 0 : }
5596 0 : return HCCL_SUCCESS;
5597 : }
5598 :
5599 0 : HcclResult HcclCommunicator::BuildServerAndsuperPodRank(const std::string &algName)
5600 : {
5601 0 : opResPara_.topoInfo.serverAndsuperPodRank = 0;
5602 0 : opResPara_.topoInfo.serverAndsuperPodRankLength = 0;
5603 0 : if (serverAndsuperPodToRankDevice_.ptr() == nullptr) {
5604 0 : std::vector<std::vector<std::vector<u32>>> serverAndsuperPodToRank;
5605 0 : CHK_RET(implAlg_->GetRankVecInfo(serverAndsuperPodToRank));
5606 0 : u64 tlvLen = 0;
5607 0 : CHK_RET(BuildOpTopoResTlvParam(algName, serverAndsuperPodToRank, serverAndsuperPodToRankDevice_, tlvLen));
5608 0 : opResPara_.topoInfo.serverAndsuperPodRank = reinterpret_cast<u64>(serverAndsuperPodToRankDevice_.ptr());
5609 0 : opResPara_.topoInfo.serverAndsuperPodRankLength = tlvLen;
5610 0 : HCCL_DEBUG("[HcclCommunicator][BuildServerAndsuperPodRank] server and super pod ranks tlv length[%lu], ptr[%p], "
5611 : "group[%s], local user rankId[%u] ",
5612 : tlvLen, serverAndsuperPodToRankDevice_.ptr(),
5613 : identifier_.c_str(), userRank_);
5614 0 : }
5615 0 : return HCCL_SUCCESS;
5616 : }
5617 :
5618 0 : HcclResult HcclCommunicator::BuildOpRetryParam(const AlgResourceResponse &algResource, const std::string &newTag)
5619 : {
5620 0 : opResPara_.config.retryEnable = static_cast<u8>(retryEnable_);
5621 0 : opResPara_.config.retryHoldTime = commConfig_.GetConfigRetryHoldTime();
5622 0 : opResPara_.config.retryIntervalTime = commConfig_.GetConfigRetryIntervalTime();
5623 : // aicpu和custom共用同一个opResPara_,aicpu初始化完成后,会修改h2d/d2h的指针,然后重新传给custom
5624 0 : opResPara_.kfcControlTransferH2DParams = kfcControlTransferH2D_->GetCommunicateParams();
5625 0 : opResPara_.kfcStatusTransferD2HParams = kfcStatusTransferD2H_->GetCommunicateParams();
5626 0 : opResPara_.debugConfig = GetDebugConfig();
5627 :
5628 0 : CHK_SMART_PTR_NULL(opRetryStreamPtr_);
5629 0 : if (opRetryStreamPtr_->find(newTag) == opRetryStreamPtr_->end()) {
5630 0 : std::vector<Stream> retryStreams(algResource.slaveDevStreams.begin(), algResource.slaveDevStreams.end());
5631 0 : retryStreams.push_back(opMainStream_);
5632 0 : opRetryStreamPtr_->insert(std::make_pair(newTag, retryStreams));
5633 0 : }
5634 0 : return HCCL_SUCCESS;
5635 : }
5636 :
5637 0 : HcclResult HcclCommunicator::BuildCommPlaneSubGroupRank(const std::string &algName)
5638 : {
5639 0 : opResPara_.hierarchicalAlgInfo.commplaneSubGroupRank = 0;
5640 0 : opResPara_.hierarchicalAlgInfo.commplaneSubGroupRankLength = 0;
5641 0 : if (commplaneSubGroupRankDevice_.ptr() == nullptr) {
5642 0 : std::vector<std::vector<std::vector<std::vector<u32>>>> commplaneSubGroupVector;
5643 0 : CHK_RET(implAlg_->GetCommPlaneSubGroupVector(commplaneSubGroupVector));
5644 0 : u64 tlvLen = 0;
5645 0 : CHK_RET(BuildOpTopoResVectorTlvParam(algName, commplaneSubGroupVector, commplaneSubGroupRankDevice_, tlvLen));
5646 0 : opResPara_.hierarchicalAlgInfo.commplaneSubGroupRank = reinterpret_cast<u64>(commplaneSubGroupRankDevice_.ptr());
5647 0 : opResPara_.hierarchicalAlgInfo.commplaneSubGroupRankLength = tlvLen;
5648 0 : HCCL_DEBUG("[HcclCommunicator][BuildCommPlaneSubGroupRank] comm plane subGroups ranks tlv length[%lu], ptr[%p], "
5649 : "group[%s], local user rankId[%u] ",
5650 : tlvLen, commplaneSubGroupRankDevice_.ptr(),
5651 : identifier_.c_str(), userRank_);
5652 0 : }
5653 0 : return HCCL_SUCCESS;
5654 : }
5655 :
5656 0 : HcclResult HcclCommunicator::BuildHierarchicalAlgOption(u32 *ahcConfInfo)
5657 : {
5658 0 : std::map<AHCConcOpType, TemplateType> hierarchicalAlgOption;
5659 0 : CHK_RET(implAlg_->GetAHCAlgOption(hierarchicalAlgOption));
5660 0 : ahcConfInfo[TOP_HIERARCHICAL_CONF_lENGTH_INDEX] = hierarchicalAlgOption.size();
5661 :
5662 0 : if (hierarchicalAlgOption.size() >= (TOP_HIERARCHICAL_CONF_SIZE-1)) {
5663 0 : HCCL_ERROR("[HcclCommunicator][BuildHierarchicalAlgOption] host hierarchicalAlgOption size[%u] exceed maxsize[%u]",
5664 : hierarchicalAlgOption.size(), (TOP_HIERARCHICAL_CONF_SIZE-1));
5665 0 : return HCCL_E_INTERNAL;
5666 : }
5667 :
5668 0 : HCCL_DEBUG("[HcclCommunicator][BuildHierarchicalAlgOption] host hierarchicalAlgOption.size() [%u]", hierarchicalAlgOption.size());
5669 :
5670 : //默认清空内存
5671 0 : for (u32 i = TOP_HIERARCHICAL_CONF_INFO_INDEX ; i < TOP_HIERARCHICAL_CONF_SIZE; i++) {
5672 0 : ahcConfInfo[i] = 0;
5673 : }
5674 :
5675 0 : u32 confDataStartIndex = TOP_HIERARCHICAL_CONF_INFO_INDEX;
5676 0 : for (auto it = hierarchicalAlgOption.begin(); it != hierarchicalAlgOption.end(); ++it) {
5677 0 : HCCL_DEBUG("[HcclCommunicator][BuildHierarchicalAlgOption] host Level [%u], ConcType[%u] AHCOpType[%u], TemplateType [%u]",
5678 : it->first.ahcLevel, it->first.concType, it->first.ahcOpType, it->second);
5679 :
5680 0 : u32 confData = (static_cast<u32>(it->first.ahcLevel) << TOP_HIERARCHICAL_CONF_LEVEL_SHIFT) |
5681 0 : (static_cast<u32>(it->first.concType) << TOP_HIERARCHICAL_CONF_CONC_TYPE_SHIFT) |
5682 0 : (static_cast<u32>(it->first.ahcOpType) << TOP_HIERARCHICAL_CONF_OP_TYPE_SHIFT) |
5683 0 : (static_cast<u32>(it->second) << TOP_HIERARCHICAL_CONF_TEMPLATE_TYPE_SHIFT);
5684 0 : ahcConfInfo[confDataStartIndex] = confData;
5685 0 : confDataStartIndex = confDataStartIndex + 1;
5686 : }
5687 0 : return HCCL_SUCCESS;
5688 0 : }
5689 :
5690 :
5691 0 : HcclResult HcclCommunicator::BuildOpTopoResParam(const std::string &algName, const AlgResourceResponse &algResource)
5692 : {
5693 0 : opResPara_.topoInfo.userRank = userRank_;
5694 0 : opResPara_.topoInfo.userRankSize = userRankSize_;
5695 0 : opResPara_.topoInfo.deviceLogicId = deviceLogicId_;
5696 0 : opResPara_.topoInfo.isSingleMeshAggregation = isSingleMeshAggregation_;
5697 0 : opResPara_.topoInfo.deviceNumPerAggregation = deviceNumPerAggregation_;
5698 0 : opResPara_.topoInfo.superPodNum = superPodNum_;
5699 0 : opResPara_.topoInfo.devicePhyId = devicePhyId_;
5700 0 : opResPara_.topoInfo.deviceType = static_cast<u32>(deviceType_);
5701 : TopoType topoType;
5702 0 : CHK_RET(implAlg_->GetTopoType(topoType));
5703 0 : opResPara_.topoInfo.topoType = static_cast<u32>(topoType);
5704 0 : opResPara_.topoInfo.serverNum = serverNum_;
5705 0 : opResPara_.topoInfo.meshAggregationRankSize = meshAggregationRankSize_;
5706 0 : opResPara_.topoInfo.multiModuleDiffDeviceNumMode = multiModuleDiffDeviceNumMode_;
5707 0 : opResPara_.topoInfo.multiSuperPodDiffServerNumMode = multiSuperPodDiffServerNumMode_;
5708 0 : opResPara_.topoInfo.realUserRank = realUserRank_;
5709 0 : opResPara_.topoInfo.isDiffDeviceModule = isDiffDeviceModule_;
5710 0 : opResPara_.topoInfo.isDiffDeviceType = isDiffDeviceType_;
5711 0 : opResPara_.topoInfo.gcdDeviceNumPerAggregation = gcdDeviceNumPerAggregation_;
5712 0 : opResPara_.topoInfo.moduleNum = moduleNum_;
5713 0 : opResPara_.isARSDoubleRing = isARSDoubleRing_;
5714 0 : opResPara_.multiSuperPodDiffDeviceNumMode = multiSuperPodDiffDeviceNumMode_;
5715 0 : CHK_RET(BuildPairLinkCounter(algName));
5716 0 : CHK_RET(BuildIsUsedRdmaRank(algName));
5717 0 : CHK_RET(BuildNicList(algName));
5718 0 : CHK_RET(BuildBridgeRank(algName));
5719 0 : CHK_RET(BuildCommPlanRank(algName));
5720 0 : CHK_RET(BuildServerAndsuperPodRank(algName));
5721 0 : CHK_RET(BuildCommPlaneSubGroupRank(algName));
5722 0 : return HCCL_SUCCESS;
5723 : }
5724 :
5725 0 : HcclResult HcclCommunicator::BuildOpRemoteLinkP2pResParam(const LINK &link, HccltagRemoteResV3 &tagRemoteRes,
5726 : TransportLinkType linkType)
5727 : {
5728 : // hccs sio并发场景,sio链路(linkTyp为SIO)打包到linkP2pSio, hccs链路(linkTyp为HCCS)打包到linkP2p;
5729 : // 其他场景打包到linkP2p
5730 0 : HcclLinkP2pV2 *linkp2p = &(tagRemoteRes.tagRemoteResPtr->linkP2p);
5731 0 : if (linkType == TransportLinkType::SIO) {
5732 0 : linkp2p = &(tagRemoteRes.tagRemoteResPtr->linkP2pSio);
5733 : }
5734 0 : if (linkp2p->localIpcSignal[0].resId != INVALID_U64) {
5735 0 : HCCL_INFO("[%s]the linkP2p is existed, no need to refresh transport resource, resId[%llu]",
5736 : __func__, linkp2p->localIpcSignal[0].resId);
5737 0 : return HCCL_SUCCESS;
5738 : }
5739 : // localMem & remoteMem
5740 0 : void *inbufferPtr = nullptr;
5741 0 : void *outbufferPtr = nullptr;
5742 0 : CHK_RET(link->GetRemoteMem(UserMemType::INPUT_MEM, &inbufferPtr));
5743 0 : CHK_RET(link->GetRemoteMem(UserMemType::OUTPUT_MEM, &outbufferPtr));
5744 0 : (linkp2p->remoteMem)[INPUT].addr = reinterpret_cast<u64>(inbufferPtr);
5745 0 : (linkp2p->remoteMem)[OUTPUT].addr = reinterpret_cast<u64>(outbufferPtr);
5746 0 : CHK_RET(link->GetRemoteMemSize(UserMemType::INPUT_MEM, (linkp2p->remoteMem)[INPUT].size));
5747 0 : CHK_RET(link->GetRemoteMemSize(UserMemType::OUTPUT_MEM, (linkp2p->remoteMem)[OUTPUT].size));
5748 0 : MemDetails localMem; // 暂时预留,赋值为空
5749 0 : (linkp2p->localMem)[0] = localMem;
5750 0 : (linkp2p->localMem)[1] = localMem;
5751 0 : HCCL_DEBUG("[%s] finish set localMem & remoteMem info", __func__);
5752 : // localnotify & remotenotify
5753 0 : u64 notifyNum = 0;
5754 0 : std::vector<HcclSignalInfo> locIpcSignals;
5755 0 : std::vector<HcclSignalInfo> rmtIpcSignals;
5756 0 : CHK_RET(link->GetLocalNotify(locIpcSignals));
5757 0 : CHK_RET(link->GetRemoteNotify(rmtIpcSignals));
5758 :
5759 0 : for (size_t i = 0; i < locIpcSignals.size(); i++) {
5760 0 : CHK_RET(CheckNotifyOrQPMaxNum(notifyNum, LINK_P2P_MAX_NUM, true));
5761 0 : linkp2p->localIpcSignal[notifyNum] = locIpcSignals[i];
5762 0 : linkp2p->remoteIpcSignal[notifyNum] = rmtIpcSignals[i];
5763 0 : notifyNum++;
5764 : }
5765 0 : tagRemoteRes.p2pNotifyNum = notifyNum;
5766 0 : HCCL_DEBUG("[%s] finish set localnotify & remotenotify info, notifyNum[%llu]", __func__, notifyNum);
5767 : // transportAttr
5768 0 : CHK_RET(link->GetTransportAttr(linkp2p->transportAttr));
5769 0 : HCCL_DEBUG("[%s] finish set RemoteLinkP2pResParam info", __func__);
5770 0 : return HCCL_SUCCESS;
5771 0 : }
5772 :
5773 0 : HcclResult HcclCommunicator::BuildOpRemoteLinkRoceResParam(const LINK &link, HccltagRemoteResV3 &tagRemoteRes,
5774 : bool isBackup, bool isRetry, bool isSecondBuild)
5775 : {
5776 0 : u32 iter = isSecondBuild ? 2 : 0;
5777 0 : HcclLinkRoceV2 *linkRoce = isBackup ? &(tagRemoteRes.tagRemoteResPtr->linkRoce[AICPU_RETRY_LINKROCE_BACKUP + iter])
5778 0 : : &(tagRemoteRes.tagRemoteResPtr->linkRoce[AICPU_RETRY_LINKROCE_DEFAULT + iter]);
5779 0 : if (!isRetry && linkRoce->localNotifyList != 0) {
5780 0 : HCCL_INFO("[%s]the linkRoce is existed, no need to refresh transport resource, localNotifyListPtr[%p], iter[%u]",
5781 : __func__, reinterpret_cast<void *>(linkRoce->localNotifyList), iter);
5782 0 : return HCCL_SUCCESS;
5783 : }
5784 : // localMem & remoteMem
5785 0 : CHK_RET(link->GetLocalMemDetails(UserMemType::INPUT_MEM, (linkRoce->localMem)[INPUT]));
5786 0 : CHK_RET(link->GetLocalMemDetails(UserMemType::OUTPUT_MEM, (linkRoce->localMem)[OUTPUT]));
5787 0 : void *inbufferPtr = nullptr;
5788 0 : void *outbufferPtr = nullptr;
5789 0 : CHK_RET(link->GetRemoteMem(UserMemType::INPUT_MEM, &inbufferPtr));
5790 0 : CHK_RET(link->GetRemoteMem(UserMemType::OUTPUT_MEM, &outbufferPtr));
5791 0 : HCCL_DEBUG("[%s]inbufferPtr[%p], outbufferPtr[%p]", __func__, inbufferPtr, outbufferPtr);
5792 0 : if (inbufferPtr == nullptr || outbufferPtr == nullptr) {
5793 0 : HCCL_ERROR("[%s]inbufferPtr[%p], outbufferPtr[%p]", __func__, inbufferPtr, outbufferPtr);
5794 0 : return HCCL_E_INTERNAL;
5795 : }
5796 0 : (linkRoce->remoteMem)[INPUT].addr = reinterpret_cast<u64>(inbufferPtr);
5797 0 : (linkRoce->remoteMem)[OUTPUT].addr = reinterpret_cast<u64>(outbufferPtr);
5798 0 : CHK_RET(link->GetRemoteMemKey(UserMemType::INPUT_MEM, &((linkRoce->remoteMem)[INPUT].key)));
5799 0 : CHK_RET(link->GetRemoteMemKey(UserMemType::OUTPUT_MEM, &((linkRoce->remoteMem)[OUTPUT].key)));
5800 0 : CHK_RET(link->GetRemoteMemSize(UserMemType::INPUT_MEM, (linkRoce->remoteMem)[INPUT].size));
5801 0 : CHK_RET(link->GetRemoteMemSize(UserMemType::OUTPUT_MEM, (linkRoce->remoteMem)[OUTPUT].size));
5802 0 : HCCL_DEBUG("[%s] finish set localMem & remoteMem info", __func__);
5803 : // notifyValue & Key
5804 0 : std::vector<AddrKey> notifyValueAddrKey;
5805 0 : CHK_RET(link->GetLocalNotifyValueAddrKey(notifyValueAddrKey));
5806 0 : linkRoce->notifyValue = notifyValueAddrKey[0].addr;
5807 0 : linkRoce->notifyValueKey = notifyValueAddrKey[0].key;
5808 : // QPInfo
5809 0 : std::vector<HcclQpInfoV2> aiQpInfos;
5810 0 : CHK_RET(link->GetAiQpInfo(aiQpInfos));
5811 0 : u32 qpNum = aiQpInfos.size();
5812 0 : if (qpNum > RDMA_QP_MAX_NUM || qpNum < 1) {
5813 0 : return HCCL_E_INTERNAL;
5814 : }
5815 0 : std::copy_n(aiQpInfos.begin(), qpNum, linkRoce->QpInfo);
5816 0 : linkRoce->qpsPerConnection = qpNum - static_cast<u32>(qpNum > 1); // 多QP数量或单QP模式
5817 :
5818 : // localnotify & remotenotify
5819 0 : std::vector<AddrKey> notifyAddrKey;
5820 0 : std::vector<HcclSignalInfo> signalInfos;
5821 0 : CHK_RET(link->GetLocalRdmaNotify(signalInfos));
5822 0 : CHK_RET(link->GetRemoteRdmaNotifyAddrKey(notifyAddrKey));
5823 0 : if ((signalInfos.size() != notifyAddrKey.size()) || (signalInfos.size() < RDMA_NOTIFY_MIN_NUM) ||
5824 0 : (signalInfos.size() > RDMA_NOTIFY_MAX_NUM) || (notifyAddrKey.size() < RDMA_NOTIFY_MIN_NUM) ||
5825 0 : (notifyAddrKey.size() > RDMA_NOTIFY_MAX_NUM) ||
5826 0 : ((signalInfos.size() - RDMA_NOTIFY_MIN_NUM) % linkRoce->qpsPerConnection) ||
5827 0 : ((notifyAddrKey.size() - RDMA_NOTIFY_MIN_NUM) % linkRoce->qpsPerConnection)) {
5828 0 : HCCL_ERROR("[HcclCommunicator][BuildOpRemoteLinkRoceResParam] signalInfos %zu notifyAddrKey %zu "
5829 : "qpsPerConnection %u", signalInfos.size(), notifyAddrKey.size(), linkRoce->qpsPerConnection);
5830 0 : return HCCL_E_INTERNAL;
5831 : }
5832 0 : u64 notifyNum = (notifyAddrKey.size() - RDMA_NOTIFY_MIN_NUM) / linkRoce->qpsPerConnection - static_cast<u32>(linkRoce->qpsPerConnection > 1);
5833 0 : linkRoce->singleQPNotifyNum = notifyNum;
5834 :
5835 0 : u64 len = signalInfos.size() * sizeof(HcclSignalInfo);
5836 0 : DeviceMem localNotifyListMem;
5837 0 : CHK_RET(CopyVectorToDeviceMem(len, localNotifyListMem, signalInfos));
5838 0 : linkRoce->localNotifyList = reinterpret_cast<u64>(localNotifyListMem.ptr());
5839 0 : ibverbsLocalNotify_[tagRemoteRes.tagRemoteResPtr->tag].emplace_back(std::move(localNotifyListMem));
5840 :
5841 0 : len = notifyAddrKey.size() * sizeof(AddrKey);
5842 0 : DeviceMem remoteNotifyListMem;
5843 0 : CHK_RET(CopyVectorToDeviceMem(len, remoteNotifyListMem, notifyAddrKey));
5844 0 : linkRoce->remoteNotifyList = reinterpret_cast<u64>(remoteNotifyListMem.ptr());
5845 0 : ibverbsRemoteNotify_[tagRemoteRes.tagRemoteResPtr->tag].emplace_back(std::move(remoteNotifyListMem));
5846 :
5847 0 : HCCL_DEBUG("[%s] finish set localnotify & remotenotify info, notifyNum[%llu], linkNotifyNum[%llu]",
5848 : __func__, notifyNum, signalInfos.size());
5849 :
5850 0 : if (isBackup) {
5851 0 : tagRemoteRes.roceNotifyNumBackup = linkRoce->singleQPNotifyNum;
5852 0 : tagRemoteRes.qpNumBackup = linkRoce->qpsPerConnection;
5853 : } else {
5854 0 : tagRemoteRes.roceNotifyNum = linkRoce->singleQPNotifyNum;
5855 0 : tagRemoteRes.qpNum = linkRoce->qpsPerConnection;
5856 : }
5857 :
5858 0 : linkRoce->useAtomicWrite = link->GetIsUseAtomicWrite();
5859 0 : HCCL_DEBUG("[%s] finish set Qp info qpNum[%u], linkRoce->localNotifyList[0].resId[%llu], "
5860 : "notifyNum[%u], isBackup[%d], isSecond[%d], qpPtr[%llu], useAtomicWrite[%d]",
5861 : __func__, linkRoce->qpsPerConnection,
5862 : signalInfos[0].resId, linkRoce->singleQPNotifyNum, isBackup, isSecondBuild,
5863 : linkRoce->QpInfo[0].qpPtr, linkRoce->useAtomicWrite);
5864 0 : return HCCL_SUCCESS;
5865 0 : }
5866 :
5867 : template <typename T>
5868 0 : HcclResult HcclCommunicator::CreateListNode(T **resHostPtr, T **resDevicePtr)
5869 : {
5870 0 : hostMemVec_.resize(hostMemVec_.size() + 1);
5871 0 : CHK_RET(AllocAndClearHostMem(sizeof(T), hostMemVec_.back()));
5872 0 : *resHostPtr = static_cast<T *>(hostMemVec_.back().get()->ptr());
5873 :
5874 0 : deviceMemVec_.resize(deviceMemVec_.size() + 1);
5875 0 : CHK_RET(AllocAndClearDeviceMem(sizeof(T), deviceMemVec_.back()));
5876 :
5877 0 : *resDevicePtr = static_cast<T *>(deviceMemVec_.back().get()->ptr());
5878 : // 初始化HcclRankRelationResV2中的tagRes链表
5879 0 : ListCommonInit(&((*resDevicePtr)->nextTagRes), &((*resHostPtr)->nextTagRes));
5880 0 : return HCCL_SUCCESS;
5881 : }
5882 :
5883 0 : HcclResult HcclCommunicator::BuildRemoteResByTag(const std::string &newTag, const u32 &usrRankId,
5884 : HcclRankRelationResV2 *&rankRelationResHostPtr, HcclRankRelationResV2 *&rankRelationResDevicePtr, bool isBackup,
5885 : bool isRetry)
5886 : {
5887 0 : HCCL_DEBUG("[%s]start to add RemoteRes with newtag[%s] and remoteRankId[%u] to list",
5888 : __func__, newTag.c_str(), usrRankId);
5889 0 : if (rankTagRemoteRes_.find(usrRankId) == rankTagRemoteRes_.end() ||
5890 0 : rankTagRemoteRes_[usrRankId].find(newTag) == rankTagRemoteRes_[usrRankId].end()) {
5891 0 : HccltagRemoteResV2 *tagRemoteResHostPtr = nullptr;
5892 0 : HccltagRemoteResV2 *tagRemoteResDevicePtr = nullptr;
5893 0 : CHK_RET(CreateListNode(&tagRemoteResHostPtr, &tagRemoteResDevicePtr));
5894 0 : CHK_SAFETY_FUNC_RET(memcpy_s(tagRemoteResHostPtr->tag, sizeof(tagRemoteResHostPtr->tag),
5895 : newTag.c_str(), newTag.length() + 1));
5896 0 : tagRemoteResHostPtr->linkP2p.localIpcSignal[0].resId = INVALID_U64;
5897 0 : tagRemoteResHostPtr->linkP2pSio.localIpcSignal[0].resId = INVALID_U64;
5898 0 : tagRemoteResHostPtr->linkRoce[0].localNotifyList = 0;
5899 0 : tagRemoteResHostPtr->linkRoce[1].localNotifyList = 0;
5900 0 : tagRemoteResHostPtr->linkRoce[2].localNotifyList = 0;
5901 0 : tagRemoteResHostPtr->linkRoce[3].localNotifyList = 0;
5902 0 : ListCommonAddHead(&tagRemoteResDevicePtr->nextTagRes, &tagRemoteResHostPtr->nextTagRes,
5903 0 : &rankRelationResHostPtr->nextTagRes, &rankRelationResDevicePtr->nextTagRes);
5904 0 : HccltagRemoteResV3 tempTagRemoteRes;
5905 0 : tempTagRemoteRes.tagRemoteResPtr = tagRemoteResHostPtr;
5906 0 : rankTagRemoteRes_[usrRankId][newTag] = tempTagRemoteRes;
5907 0 : HCCL_RUN_INFO("[%s] successfully add RemoteRes to list with newtag[%s], remoteRankId[%u]"
5908 : "rankRelationResHostPtr head addr[%p], nextHost[%p], preHost[%p], nextDevice[%p], preDevice[%p], "
5909 : "tagRemoteResDevicePtr head addr[%p]",
5910 : __func__, newTag.c_str(), usrRankId,
5911 : &rankRelationResHostPtr->nextTagRes, rankRelationResHostPtr->nextTagRes.nextHost,
5912 : rankRelationResHostPtr->nextTagRes.preHost, rankRelationResHostPtr->nextTagRes.nextDevice,
5913 : rankRelationResHostPtr->nextTagRes.preDevice, &tagRemoteResDevicePtr->nextTagRes);
5914 : } else {
5915 0 : HCCL_DEBUG("[%s] the RemoteRes with usr rankid[%u] tag[%s] has been added list",
5916 : __func__, usrRankId, newTag.c_str());
5917 : }
5918 0 : return HCCL_SUCCESS;
5919 : }
5920 :
5921 0 : HcclResult HcclCommunicator::BuildRelationResByRemoteRankId(const TransportRequest &transportRequest, const LINK &link,
5922 : HcclRankRelationResV2 *&rankRelationResHostPtr, HcclRankRelationResV2 *&rankRelationResDevicePtr)
5923 : {
5924 0 : const u32 usrRankId = transportRequest.remoteUserRank;
5925 0 : HCCL_INFO("[%s]start to add RelationRes with remote usr rankid[%u] to list", __func__, usrRankId);
5926 0 : if (opResPara_.remoteRes[usrRankId].nextHostPtr != 0 && opResPara_.remoteRes[usrRankId].nextDevicePtr != 0) {
5927 0 : rankRelationResHostPtr =
5928 0 : reinterpret_cast<HcclRankRelationResV2 *>(opResPara_.remoteRes[usrRankId].nextHostPtr);
5929 0 : rankRelationResDevicePtr =
5930 0 : reinterpret_cast<HcclRankRelationResV2 *>(opResPara_.remoteRes[usrRankId].nextDevicePtr);
5931 0 : HCCL_DEBUG("[%s] RelationRes with remote usr rankid[%u] has been added to list, "
5932 : "rankRelationResHostPtr[%p], rankRelationResDevicePtr[%p]",
5933 : __func__, usrRankId, rankRelationResHostPtr, rankRelationResDevicePtr);
5934 : } else {
5935 0 : CHK_RET(CreateListNode(&rankRelationResHostPtr, &rankRelationResDevicePtr));
5936 0 : opResPara_.remoteRes[usrRankId].nextHostPtr = reinterpret_cast<u64>(rankRelationResHostPtr);
5937 0 : opResPara_.remoteRes[usrRankId].nextDevicePtr = reinterpret_cast<u64>(rankRelationResDevicePtr);
5938 0 : rankRelationResHostPtr->remoteUsrRankId = usrRankId;
5939 0 : rankRelationResHostPtr->remoteWorldRank = rankInfoList_[usrRankId].worldRank;
5940 0 : HCCL_DEBUG("[%s]successfully add RelationRes with remote usr rankid[%u] to list, rankRelationResHostPtr[%p],"
5941 : "rankRelationResDevicePtr[%p]",
5942 : __func__, usrRankId, rankRelationResHostPtr, rankRelationResDevicePtr);
5943 : }
5944 : // 刷新远端对应的cclbuffer
5945 0 : std::vector<void *> extraMemVector;
5946 0 : if (transportRequest.inputMemType == TransportMemType::CCL_INPUT && rankRelationResHostPtr->windowsIn == 0) {
5947 0 : void *inbufferPtr = nullptr;
5948 0 : CHK_RET(link->GetRemoteMem(UserMemType::INPUT_MEM, &inbufferPtr));
5949 0 : rankRelationResHostPtr->windowsIn = reinterpret_cast<u64>(inbufferPtr);
5950 : }
5951 0 : if (transportRequest.outputMemType == TransportMemType::CCL_OUTPUT && rankRelationResHostPtr->windowsOut == 0) {
5952 0 : void *outbufferPtr = nullptr;
5953 0 : CHK_RET(link->GetRemoteMem(UserMemType::OUTPUT_MEM, &outbufferPtr));
5954 0 : rankRelationResHostPtr->windowsOut = reinterpret_cast<u64>(outbufferPtr);
5955 : }
5956 0 : if (rankRelationResHostPtr->windowsExp == 0) {
5957 0 : std::vector<void *> memPtrVec = {};
5958 0 : CHK_RET(link->GetRemoteMem(&memPtrVec));
5959 0 : if (memPtrVec.size() != 0) {
5960 0 : rankRelationResHostPtr->windowsExp = reinterpret_cast<u64>(memPtrVec[0]);
5961 0 : if (link->GetTransportType() == TransportType::TRANS_TYPE_P2P) {
5962 0 : p2pCclBuf_[usrRankId] = memPtrVec[0];
5963 : } else {
5964 0 : cclBuf_[usrRankId] = memPtrVec[0];
5965 : }
5966 0 : rankRelationResHostPtr->windowsExp += cclBufferManager_.GetInCCLbufferSize() + cclBufferManager_.GetOutCCLbufferSize();
5967 : }
5968 0 : }
5969 0 : HCCL_INFO("group[%s] successfully set windowsIn & windowsOut & windowsExp info: userRank[%u], groupRank[%u], "
5970 : "remoteRank[%u], windowsIn[0x%llx], InSize[0x%llx], windowOut[0x%llx], OutSize[0x%llx], "
5971 : "windowExp[0x%llx], ExpSize[0x%llx]",
5972 : identifier_.c_str(), GetUserRank(), GetGroupRank(), transportRequest.remoteUserRank,
5973 : rankRelationResHostPtr->windowsIn, cclBufferManager_.GetInCCLbufferSize(),
5974 : rankRelationResHostPtr->windowsOut, cclBufferManager_.GetOutCCLbufferSize(),
5975 : rankRelationResHostPtr->windowsExp, cclBufferManager_.GetExpBufferSize());
5976 0 : return HCCL_SUCCESS;
5977 0 : }
5978 :
5979 0 : HcclResult HcclCommunicator::ParseRemoteDataToMem(const OpCommTransport &opTransportResponse, const std::string &newTag,
5980 : const HcclCMDType opType, bool isBackup, bool isRetry)
5981 : {
5982 0 : HCCL_INFO("[%s] entry process newtag[%s], isBackup[%d]", __func__, newTag.c_str(), isBackup);
5983 0 : std::set<u32> bsrTansportRank;
5984 0 : for (auto &levelNSubCommTransport : opTransportResponse) {
5985 0 : for (auto &singleSubCommTransport : levelNSubCommTransport) {
5986 0 : u32 linkIdx = 0;
5987 0 : for (auto &transportRequest : singleSubCommTransport.transportRequests) {
5988 0 : if (transportRequest.isValid) {
5989 0 : auto tempLink = singleSubCommTransport.links[linkIdx];
5990 0 : HCCL_INFO("[%s]transportRequest.isUsedRdma[%d], isBackup[%d]", __func__,
5991 : transportRequest.isUsedRdma, isBackup);
5992 0 : if ((!transportRequest.isUsedRdma || tempLink->GetLinkType() == LinkType::LINK_SIO) &&
5993 0 : (isBackup || isRetry)) {
5994 0 : HCCL_INFO("[%s]no need to add p2p backup Link resource, transportRequest.isUsedRdma[%d], "
5995 : "isBackup[%d]",
5996 : __func__, transportRequest.isUsedRdma, isBackup);
5997 0 : linkIdx++;
5998 0 : continue;
5999 : }
6000 0 : HcclRankRelationResV2 *rankRelationResHostPtr = nullptr;
6001 0 : HcclRankRelationResV2 *rankRelationResDevicePtr = nullptr;
6002 0 : CHK_RET(BuildRelationResByRemoteRankId(transportRequest, tempLink, rankRelationResHostPtr,
6003 : rankRelationResDevicePtr));
6004 0 : const u32 usrRankId = transportRequest.remoteUserRank;
6005 0 : HCCL_INFO("[%s]successfully BuildRelationResByRemoteRankId with remote usr rankid[%u], "
6006 : "rankRelationResHostPtr[%p], rankRelationResDevicePtr[%p], newTage[%s]",
6007 : __func__, usrRankId, rankRelationResHostPtr, rankRelationResDevicePtr, newTag.c_str());
6008 0 : CHK_RET(BuildRemoteResByTag(newTag, usrRankId, rankRelationResHostPtr,
6009 : rankRelationResDevicePtr, isBackup, isRetry));
6010 : // transport信息保存(notify、qp)
6011 0 : if (!transportRequest.isUsedRdma || tempLink->GetLinkType() == LinkType::LINK_SIO) {
6012 : // sdma -> P2P
6013 0 : CHK_RET(BuildOpRemoteLinkP2pResParam(tempLink, rankTagRemoteRes_[usrRankId][newTag],
6014 : transportRequest.linkType));
6015 : } else {
6016 : // rdma -> roce
6017 0 : bool isSecondBuild = false;
6018 0 : if (opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV &&
6019 0 : bsrTansportRank.find(transportRequest.remoteUserRank) != bsrTansportRank.end()) {
6020 0 : isSecondBuild = true;
6021 : }
6022 0 : bsrTansportRank.insert(transportRequest.remoteUserRank);
6023 0 : CHK_RET(BuildOpRemoteLinkRoceResParam(tempLink, rankTagRemoteRes_[usrRankId][newTag],
6024 : isBackup, isRetry, isSecondBuild));
6025 : }
6026 0 : HCCL_INFO("[%s] successfully add RemoteRes to list with newtag[%s] rankRelationResHostPtr "
6027 : "head addr[%p], nextHost[%p], preHost[%p], nextDevice[%p], preDevice[%p], "
6028 : "rankRelationResDevicePtr head addr[%p]",
6029 : __func__, newTag.c_str(),
6030 : &rankRelationResHostPtr->nextTagRes, rankRelationResHostPtr->nextTagRes.nextHost,
6031 : rankRelationResHostPtr->nextTagRes.preHost, rankRelationResHostPtr->nextTagRes.nextDevice,
6032 : rankRelationResHostPtr->nextTagRes.preDevice, &rankRelationResDevicePtr->nextTagRes);
6033 0 : HCCL_INFO("[%s] create link success with newtag[%s], linkIdx[%u], isBackup[%d], usrRankId[%u]",
6034 : __func__, newTag.c_str(), linkIdx, isBackup, usrRankId);
6035 0 : }
6036 0 : linkIdx++;
6037 : }
6038 : }
6039 : }
6040 0 : HCCL_DEBUG("[%s] process success newtag[%s]", __func__, newTag.c_str());
6041 0 : return HCCL_SUCCESS;
6042 0 : }
6043 :
6044 0 : HcclResult HcclCommunicator::BuildOpRemoteResParam(const AlgResourceResponse &algResource, const std::string &newTag,
6045 : const HcclCMDType opType, bool isRetry)
6046 : {
6047 0 : HCCL_DEBUG("[%s]start ParseRemoteDataToMem, IsEnableBackupLink[%d]", __func__, IsEnableBackupLink());
6048 0 : CHK_RET(ParseRemoteDataToMem(algResource.opTransportResponse, newTag, opType, false, isRetry));
6049 0 : if (IsEnableBackupLink()) {
6050 0 : HCCL_DEBUG("[%s]start Parse backupRemoteDataToMem, IsEnableBackupLink[%d]", __func__, IsEnableBackupLink());
6051 0 : CHK_RET(ParseRemoteDataToMem(algResource.opTransportResponseBackUp, newTag, opType, true, isRetry));
6052 : }
6053 0 : if (deviceType_ == DevType::DEV_TYPE_910_93 || deviceType_ == DevType::DEV_TYPE_910B) {
6054 0 : opResPara_.notifysize = 4; // 910B & 910_93 每个notify占4个字节
6055 : } else {
6056 0 : opResPara_.notifysize = 8; // 其他芯片类型每个notify占8个字节
6057 : }
6058 0 : return HCCL_SUCCESS;
6059 : }
6060 :
6061 5 : HcclResult HcclCommunicator::CopyHostListResToDeviceParam(const std::string &newTag, const ListCommon *headHostList, const u64 size)
6062 : {
6063 5 : ListCommon *nextHostList = reinterpret_cast<ListCommon *>(headHostList->nextHost);
6064 5 : ListCommon *nextDeviceList = reinterpret_cast<ListCommon *>(headHostList->nextDevice);
6065 : // 该tag已分配过资源,只需刷新单节点(精确匹配);否则为首次分配,批量拷贝前N个节点
6066 5 : bool isRefreshSingleNode = (newTagResAlloced_.find(newTag) != newTagResAlloced_.end());
6067 : // ListCommonAddHead把新节点头插,原头节点的preHost/preDevice被改写,两节点都需要刷新到device
6068 5 : constexpr uint32_t UPDATE_NODE_NUM = 2;
6069 5 : uint32_t updateNodeCnt = 0;
6070 :
6071 12 : while (nextHostList != headHostList && updateNodeCnt < UPDATE_NODE_NUM) {
6072 9 : HCCL_INFO(
6073 : "[HcclCommunicator][CopyHostListResToDeviceParam] remote resource, tag[%s], head Host List[%p], next "
6074 : "Host List[%p],next Device List[%p]",
6075 : newTag.c_str(), headHostList, nextHostList, nextDeviceList);
6076 9 : if (isRefreshSingleNode) {
6077 : // 刷新已有资源:遍历链表找到匹配的tag,只拷贝该节点,避免全量刷新
6078 7 : std::string curTag = (size == sizeof(HccltagLocalResV2)) ? reinterpret_cast<HccltagLocalResV2 *>(nextHostList)->tag :
6079 7 : reinterpret_cast<HccltagRemoteResV2 *>(nextHostList)->tag;
6080 7 : if (curTag == newTag) {
6081 2 : CHK_RET(hrtMemSyncCopy(reinterpret_cast<void *>(nextDeviceList), size, reinterpret_cast<void *>(nextHostList),
6082 : size, HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
6083 2 : break;
6084 : }
6085 7 : } else {
6086 : // 首分配置:拷贝前UPDATE_NODE_NUM个节点到device,减少H2D次数
6087 2 : CHK_RET(hrtMemSyncCopy(reinterpret_cast<void *>(nextDeviceList), size, reinterpret_cast<void *>(nextHostList),
6088 : size, HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
6089 2 : updateNodeCnt++;
6090 : }
6091 7 : nextDeviceList = reinterpret_cast<ListCommon *>(nextHostList->nextDevice);
6092 7 : nextHostList = reinterpret_cast<ListCommon *>(nextHostList->nextHost);
6093 : }
6094 5 : return HCCL_SUCCESS;
6095 : }
6096 :
6097 0 : HcclResult HcclCommunicator::CopyHostOpResToDeviceParam(const std::string &newTag)
6098 : {
6099 : // 1、将opResPara_,H2D到device
6100 0 : CHK_RET(hrtMemSyncCopy(opResDevicePara_.ptr(), sizeof(HcclOpResParam), reinterpret_cast<void *>(&opResPara_),
6101 : sizeof(HcclOpResParam), HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
6102 0 : HCCL_DEBUG("[HcclCommunicator][CopyHostOpResToDeviceParam] tag[%s] local rankId[%u] workspace[%p] "
6103 : "workspacesize[%lu] ranksize[%u], cclbuffersize[%lu], cclinbuffer[%p], ccloutbuffer[%p], "
6104 : "remote winStart[%u], remote rWinOffset[%u], hostStateInfo[%p], aicpuStateInfo[%p], notifysize[%u], "
6105 : "sizeOfAiRMAInfo[%u],aiRMAInfo[%u]",
6106 : newTag.c_str(), userRank_, opResPara_.mc2WorkSpace.workSpace, opResPara_.mc2WorkSpace.workSpaceSize,
6107 : opResPara_.rankSize, opResPara_.winSize, opResPara_.localWindowsIn, opResPara_.localWindowsOut,
6108 : opResPara_.rWinStart, opResPara_.rWinOffset, opResPara_.hostStateInfo, opResPara_.aicpuStateInfo,
6109 : opResPara_.notifysize, opResPara_.sizeOfAiRMAInfo,opResPara_.aiRMAInfo);
6110 : // 2、将opResPara_中localres的tagRes,H2D到device
6111 0 : HCCL_DEBUG("[HcclCommunicator][CopyHostOpResToDeviceParam] local resource, tag[%s] streamNum[%u] signalNum[%u]",
6112 : newTag.c_str(), opResPara_.localRes.streamNum, opResPara_.localRes.signalNum);
6113 0 : CHK_RET(CopyHostListResToDeviceParam(
6114 : newTag, reinterpret_cast<ListCommon *>(&opResPara_.localRes.nextTagRes), sizeof(HccltagLocalResV2)));
6115 : // 3、遍历rank中tag资源,H2D到device
6116 0 : CHK_RET(CopyHostOpRemoteResToDeviceParam(newTag));
6117 0 : HCCL_DEBUG("[HcclCommunicator][CopyHostOpResToDeviceParam] copy host resource success!, tag[%s]", newTag.c_str());
6118 0 : return HCCL_SUCCESS;
6119 : }
6120 :
6121 0 : HcclResult HcclCommunicator::CopyHostAirmaInfoToDeviceParam(const std::string &newTag, const HcclCMDType opType, const rtStream_t aiCpuStream)
6122 : {
6123 0 : HCCL_INFO("[HcclCommunicator][%s] Start prepare.", __func__);
6124 0 : CHK_PTR_NULL(aiRMAInfoMem_);
6125 0 : HcclAiRMAInfo *aiRMAInfoPtr = reinterpret_cast<HcclAiRMAInfo*>(aiRMAInfoMem_->ptr());
6126 0 : CHK_PTR_NULL(aiRMAInfoPtr);
6127 :
6128 0 : aiRMAInfoPtr->sizeOfAiRMAWQ = static_cast<u32>(sizeof(HcclAiRMAWQ));
6129 0 : aiRMAInfoPtr->sizeOfAiRMACQ = static_cast<u32>(sizeof(HcclAiRMACQ));
6130 0 : aiRMAInfoPtr->sizeOfAiRMAMem = static_cast<u32>(sizeof(HcclAiRMAMemInfo));
6131 :
6132 0 : CHK_RET(DeviceMem::alloc(aiSqDev_, aiSqMem_->size()));
6133 0 : aiRMAInfoPtr->sqPtr = aiSqDev_.ptr();
6134 0 : CHK_RET(hrtMemAsyncCopy(aiSqDev_.ptr(), aiSqDev_.size(), aiSqMem_->ptr(), aiSqDev_.size(),
6135 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
6136 :
6137 0 : CHK_RET(DeviceMem::alloc(aiScqDev_, aiScqMem_->size()));
6138 0 : aiRMAInfoPtr->scqPtr = aiScqDev_.ptr();
6139 0 : CHK_RET(hrtMemAsyncCopy(aiScqDev_.ptr(), aiScqDev_.size(), aiScqMem_->ptr(), aiScqDev_.size(),
6140 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
6141 :
6142 0 : CHK_RET(DeviceMem::alloc(aiRqDev_, aiRqMem_->size()));
6143 0 : aiRMAInfoPtr->rqPtr = aiRqDev_.ptr();
6144 0 : CHK_RET(hrtMemAsyncCopy(aiRqDev_.ptr(), aiRqDev_.size(), aiRqMem_->ptr(), aiRqDev_.size(),
6145 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
6146 :
6147 0 : CHK_RET(DeviceMem::alloc(aiRcqDev_, aiRcqMem_->size()));
6148 0 : aiRMAInfoPtr->rcqPtr = aiRcqDev_.ptr();
6149 0 : CHK_RET(hrtMemAsyncCopy(aiRcqDev_.ptr(), aiRcqDev_.size(), aiRcqMem_->ptr(), aiRcqDev_.size(),
6150 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
6151 :
6152 0 : CHK_RET(hrtMemAsyncCopy(aiMemDetailsDev_.ptr(), aiMemDetailsDev_.size(), aiMemDetailsMem_->ptr(),
6153 : aiMemDetailsDev_.size(), HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
6154 :
6155 0 : CHK_RET(DeviceMem::alloc(aiMemDev_, aiMemMem_->size()));
6156 0 : aiRMAInfoPtr->memPtr = aiMemDev_.ptr();
6157 0 : CHK_RET(hrtMemAsyncCopy(aiMemDev_.ptr(), aiMemDev_.size(), aiMemMem_->ptr(), aiMemDev_.size(),
6158 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
6159 :
6160 0 : opResPara_.sizeOfAiRMAInfo = static_cast<u64>(sizeof(HcclAiRMAInfo));
6161 0 : CHK_RET(DeviceMem::alloc(aiRMAInfoDev_, opResPara_.sizeOfAiRMAInfo));
6162 0 : opResPara_.aiRMAInfo = reinterpret_cast<u64>(aiRMAInfoDev_.ptr());
6163 :
6164 0 : CHK_RET(hrtMemAsyncCopy(aiRMAInfoDev_.ptr(), aiRMAInfoDev_.size(), aiRMAInfoMem_->ptr(), aiRMAInfoDev_.size(),
6165 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
6166 0 : HCCL_INFO("[%s] tag[%s] curRankId[%u] rankNum[%u] qpNum[%u] aiRMAInfo[%p] sizeOfAiRMAInfo[%llu] "
6167 : "sizeOfAiRMAWQ[%u] sizeOfAiRMACQ[%u] sizeOfAiRMAMem[%u] sqPtr[%p] sqSize[%llu] sqCount[%zu] "
6168 : "scqPtr[%p] scqSize[%llu] scqCount[%zu] rqPtr[%p] rqSize[%llu] rqCount[%zu] rcqPtr[%p] "
6169 : "rcqSize[%llu] rcqCount[%zu] memPtr[%p] memSize[%llu] memCount[%zu] memDetailCount[%zu],opResPara_.aiRMAInfo",
6170 : __func__, newTag.c_str(), aiRMAInfoPtr->curRankId, aiRMAInfoPtr->rankNum, aiRMAInfoPtr->qpNum,
6171 : opResPara_.aiRMAInfo, opResPara_.sizeOfAiRMAInfo, aiRMAInfoPtr->sizeOfAiRMAWQ,
6172 : aiRMAInfoPtr->sizeOfAiRMACQ, aiRMAInfoPtr->sizeOfAiRMAMem, aiRMAInfoPtr->sqPtr,
6173 : aiSqDev_.size(), aiSqMem_->size(), aiRMAInfoPtr->scqPtr, aiScqDev_.size(), aiScqMem_->size(),
6174 : aiRMAInfoPtr->rqPtr, aiRqDev_.size(), aiRqMem_->size(), aiRMAInfoPtr->rcqPtr, aiRcqDev_.size(),
6175 : aiRcqMem_->size(), aiRMAInfoPtr->memPtr, aiMemDev_.size(), aiMemMem_->size(), aiMemDetailsMem_->size());
6176 0 : return HCCL_SUCCESS;
6177 : }
6178 :
6179 0 : HcclResult HcclCommunicator::BuildOpResParam(
6180 : const std::string &algName, const AlgResourceResponse &algResource, const std::string &newTag,
6181 : const HcclCMDType opType, const rtStream_t aicpuStream)
6182 : {
6183 0 : opResPara_.localUsrRankId = userRank_;
6184 0 : opResPara_.rankSize = userRankSize_;
6185 :
6186 0 : bool isUseUserMem = isUserMemRegisted_ && !userMemMap_.empty();
6187 0 : if (!isUseUserMem) {
6188 0 : opResPara_.winSize = algResource.cclInputMem.size();
6189 0 : opResPara_.localWindowsIn = reinterpret_cast<u64>(algResource.cclInputMem.ptr());
6190 0 : opResPara_.localWindowsOut = reinterpret_cast<u64>(algResource.cclOutputMem.ptr());
6191 : } else {
6192 0 : opResPara_.winSize = userMemMap_.begin()->second->size();
6193 0 : opResPara_.localWindowsIn = reinterpret_cast<u64>(userMemMap_.begin()->second->ptr());
6194 0 : opResPara_.localWindowsOut = reinterpret_cast<u64>(userMemMap_.begin()->second->ptr());
6195 : }
6196 : // 填充Exp相关信息 当前该块内存大小恒为1M
6197 0 : opResPara_.winExpSize = EXP_BUFFER_SIZE;
6198 0 : opResPara_.localWindowsExp = reinterpret_cast<u64>(cclBufferManager_.GetCommExpBuffer().ptr());
6199 0 : HCCL_INFO("[HcclCommunicator][%s] isUseUserMem[%d], winSize[%llu], localWindowsIn[%llu],"
6200 : "localWindowsOut[%llu], localWindowsExp[%llu]", __func__, isUseUserMem, opResPara_.winSize,
6201 : opResPara_.localWindowsIn, opResPara_.localWindowsOut, opResPara_.localWindowsExp);
6202 :
6203 0 : CHK_SAFETY_FUNC_RET(
6204 : memcpy_s(opResPara_.hcomId, sizeof(opResPara_.hcomId), identifier_.c_str(), identifier_.length() + 1));
6205 :
6206 0 : opResPara_.config.deterministic = GetDeterministicConfig();
6207 0 : opResPara_.config.highPerfEnable = 0;
6208 0 : aclrtFloatOverflowMode floatOverflowMode = ACL_RT_OVERFLOW_MODE_UNDEF;
6209 0 : CHK_RET(hrtGetDeviceSatMode(&floatOverflowMode));
6210 0 : opResPara_.config.floatOverflowMode = floatOverflowMode;
6211 0 : opResPara_.config.taskMonitorInterval = GetExternalInputDfsTaskMonitorInterval();
6212 0 : bool isSupportAtomicWrite = false; // 涉及到任务编排,当前不能只判断本机驱动版本是否支持
6213 0 : opResPara_.config.isSupportAtomicWrite = static_cast<u8>(isSupportAtomicWrite);
6214 0 : opResPara_.config.notifyWaitTime =
6215 0 : (GetExternalInputHcclExecTimeoutSet() != HcclExecTimeoutSet::HCCL_EXEC_TIMEOUT_NOT_SET ||
6216 0 : commConfig_.GetConfigExecTimeOutSet())
6217 0 : ? commConfig_.GetConfigExecTimeOut()
6218 : : NOTIFY_DEFAULT_WAIT_TIME;
6219 0 : opResPara_.config.linkTimeOut = std::chrono::seconds(GetExternalInputHcclLinkTimeOut());
6220 0 : opResPara_.config.retryEnable = static_cast<u8>(retryEnable_);
6221 0 : opResPara_.config.interHccsDisable = GetExternalInputInterHccsDisable();
6222 0 : opResPara_.config.multiQpThreshold = GetExternalInputMultiQpThreshold();
6223 0 : opResPara_.rWinStart = offsetof(HcclOpResParam, remoteRes);
6224 0 : opResPara_.rWinOffset = sizeof(RemoteResPtr);
6225 0 : opResPara_.notifysize = 0;
6226 0 : opResPara_.lockAddr = hostDeviceLock_->GetDevMemAddr();
6227 0 : opResPara_.utraceStatusFlag = GetExternalInputHcclEnableEntryLog();
6228 0 : DeviceMem tinySendRecvMem;
6229 0 : CHK_RET(implAlg_->GetTinyMem(tinySendRecvMem));
6230 0 : opResPara_.tinyMem = reinterpret_cast<u64>(tinySendRecvMem.ptr());
6231 0 : opResPara_.tinyMemSize = reinterpret_cast<u64>(tinySendRecvMem.size());
6232 0 : opResPara_.opEntry = GetExternalInputHcclEnableEntryLog();
6233 0 : opResPara_.hcclSdmaQos = GetHcclQos();
6234 :
6235 0 : CHK_RET(BuildOpLocalResParam(algResource, newTag));
6236 0 : CHK_RET(BuildOpRemoteResParam(algResource, newTag, opType));
6237 0 : CHK_RET(BuildOpTopoResParam(algName, algResource));
6238 0 : CHK_RET(BuildOpRetryParam(algResource, newTag));
6239 0 : CHK_RET(BuildZeroCopyParam());
6240 0 : CHK_RET(BuildAicpuCustomParam());
6241 0 : CHK_RET(BuildAicpuOrderLaunchNotify()); // 先申请device侧的关于按序下发的Notify内存
6242 0 : if (algName == "RunAlltoAllAivDirect") {
6243 : // AIV直驱ROCE
6244 0 : CHK_RET(BuildAiRmaInfoParam(newTag, algName, opType));
6245 0 : CHK_RET(CopyHostAirmaInfoToDeviceParam(newTag, opType, aicpuStream));
6246 : }
6247 0 : CHK_RET(CopyHostOpResToDeviceParam(newTag));
6248 0 : HCCL_RUN_INFO("[%s]build aicpu unfold resource success, tag[%s] rWinStart[%u] rWinOffset[%u] opEntry[%d]",
6249 : __func__, newTag.c_str(), opResPara_.rWinStart, opResPara_.rWinOffset, opResPara_.opEntry);
6250 0 : return HCCL_SUCCESS;
6251 0 : }
6252 :
6253 0 : HcclResult HcclCommunicator::BuildCustomOpResParam()
6254 : {
6255 : // custom进程需要刷新h2d/d2h内存
6256 0 : opResPara_.kfcControlTransferH2DParams = customControlTransferH2D_->GetCommunicateParams();
6257 0 : opResPara_.kfcStatusTransferD2HParams = customStatusTransferD2H_->GetCommunicateParams();
6258 0 : CHK_RET(hrtMemSyncCopy(opResDevicePara_.ptr(), sizeof(HcclOpResParam), reinterpret_cast<void *>(&opResPara_),
6259 : sizeof(HcclOpResParam), HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
6260 0 : return HCCL_SUCCESS;
6261 : }
6262 :
6263 24 : HcclResult HcclCommunicator::RegisterDfxInfo(const OpParam ¶m, AlgType algType,
6264 : const std::vector<Stream> &slaveStreams, bool isAiv, const std::string &tag)
6265 : {
6266 24 : u64 count = 0;
6267 24 : HcclDataType dataType = HcclDataType::HCCL_DATA_TYPE_RESERVED;
6268 24 : switch (param.opType) {
6269 0 : case HcclCMDType::HCCL_CMD_SEND:
6270 : case HcclCMDType::HCCL_CMD_RECEIVE:
6271 : case HcclCMDType::HCCL_CMD_BATCH_SEND_RECV:
6272 0 : count = param.GetDataCount(userRank_);
6273 0 : dataType = param.GetDataType();
6274 0 : HCCL_PROFILER_ADD_TAG_SENDRECV(param.tag, identifier_, GetWorkflowMode());
6275 0 : HCCL_PROFILER_ADD_GROUPRANK_SENDRECV(identifier_, userRankSize_, userRank_, param.dstRank);
6276 0 : break;
6277 5 : case HcclCMDType::HCCL_CMD_ALLTOALL:
6278 : case HcclCMDType::HCCL_CMD_ALLTOALLV:
6279 : case HcclCMDType::HCCL_CMD_ALLTOALLVC:
6280 5 : CHK_RET(AddGroupTagInfo(param.tag, isAiv));
6281 5 : count = param.All2AllDataDes.sendCount;
6282 5 : dataType = param.All2AllDataDes.sendType;
6283 5 : break;
6284 19 : default:
6285 19 : CHK_RET(AddGroupTagInfo(param.tag, isAiv));
6286 19 : count = param.GetDataCount(userRank_);
6287 19 : dataType = param.GetDataType();
6288 : }
6289 :
6290 24 : if(GetExternalInconsistentCheckSwitch() == InconsistentCheckMode::ON){
6291 0 : if (param.opType != HcclCMDType::HCCL_CMD_BATCH_SEND_RECV) {
6292 0 : OpInfoDesc opInfo;
6293 0 : opInfo.opType = param.opType;
6294 0 : opInfo.dataType = dataType;
6295 0 : opInfo.reduceOp = param.reduceType;
6296 0 : opInfo.count = count;
6297 0 : opInfo.root = param.root;
6298 0 : opInfo.isValid = true;
6299 0 : AddOpInfoToHeartBeat(opInfo, tag);
6300 : }
6301 : }
6302 :
6303 : // task exception使用: 算子计数,算子入参信息(src/dst/datatype/reducetype)
6304 24 : HCCL_PROFILER_ADD_OPDATA_OP(param.tag, count, param.inputPtr, param.outputPtr, dataType, param.root, identifier_,
6305 : param.reduceType);
6306 : // 记录主流相关信息, 给profiling和task exception使用
6307 24 : HCCL_PROFILER_ADD_STREAM_BY_STREAMID(param.stream.id(), param.tag, 0, algType);
6308 24 : if (((GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) &&
6309 14 : !hccl::ProfilingManagerPub::GetAdditionInfoState() &&
6310 52 : !hccl::ProfilingManagerPub::GetTaskApiState()) &&
6311 14 : !param.isCapture) {
6312 11 : return HCCL_SUCCESS;
6313 : }
6314 : // 从流信息profiling开关打开的话再注册
6315 13 : for (u32 streamIndex = 0; streamIndex < slaveStreams.size(); streamIndex++) {
6316 0 : HCCL_PROFILER_ADD_STREAM_BY_STREAMID(slaveStreams[streamIndex].id(), param.tag, streamIndex + 1, algType);
6317 : }
6318 13 : return HCCL_SUCCESS;
6319 : }
6320 :
6321 0 : HcclResult HcclCommunicator::GetReportHcclMC2Info(const Stream &kfcStream, const std::vector<Stream> &aicpuStreams)
6322 : {
6323 0 : hcclMc2Info_.groupName = hrtMsprofGetHashId(identifier_.c_str(), identifier_.length());
6324 0 : hcclMc2Info_.rankSize = userRankSize_;
6325 0 : hcclMc2Info_.rankId = userRank_;
6326 0 : hcclMc2Info_.usrRankId = realUserRank_;
6327 0 : hcclMc2Info_.aicpuKfcStreamId = static_cast<uint32_t>(kfcStream.id());
6328 0 : hcclMc2Info_.reserve = 0;
6329 0 : const uint32_t ONCE_REPORT_STREAM_NUM_MAX = 8;
6330 0 : for (uint32_t streamIndex = 0, reportId = 0; streamIndex < aicpuStreams.size(); streamIndex++) {
6331 0 : HCCL_INFO("streamIndex:%u, reportId:%u, streamId:%d, ", streamIndex, reportId, aicpuStreams[streamIndex].id());
6332 0 : hcclMc2Info_.commStreamIds[reportId++] = aicpuStreams[streamIndex].id();
6333 0 : if (reportId == ONCE_REPORT_STREAM_NUM_MAX) {
6334 0 : hcclMc2Info_.commStreamSize = reportId;
6335 0 : CHK_RET(ProfilingManagerPub::CallMsprofReportMc2CommInfo(hrtMsprofSysCycleTime(), &hcclMc2Info_,
6336 : sizeof(hcclMc2Info_)));
6337 0 : reportId = 0;
6338 : }
6339 0 : if (streamIndex == (aicpuStreams.size() - 1)) {
6340 0 : HCCL_INFO("streamIndex:%u, reportId:%u, streamId:%d", streamIndex, reportId, opMainStream_.id());
6341 0 : hcclMc2Info_.commStreamIds[reportId++] = opMainStream_.id();
6342 0 : hcclMc2Info_.commStreamSize = reportId;
6343 0 : CHK_RET(ProfilingManagerPub::CallMsprofReportMc2CommInfo(hrtMsprofSysCycleTime(), &hcclMc2Info_,
6344 : sizeof(hcclMc2Info_)));
6345 0 : reportId = 0;
6346 : }
6347 : }
6348 0 : if (aicpuStreams.empty()) {
6349 0 : HCCL_INFO("only exist main stream, streamId:%d", opMainStream_.id());
6350 0 : hcclMc2Info_.commStreamIds[0] = opMainStream_.id();
6351 0 : hcclMc2Info_.commStreamSize = 1; // 只有主流1条
6352 0 : CHK_RET(ProfilingManagerPub::CallMsprofReportMc2CommInfo(hrtMsprofSysCycleTime(), &hcclMc2Info_,
6353 : sizeof(hcclMc2Info_)));
6354 : }
6355 0 : return HCCL_SUCCESS;
6356 : }
6357 :
6358 0 : HcclResult HcclCommunicator::OrchestrateAicpu(const HcclCMDType &opType, const std::string &algName,
6359 : const OpParam ¶m, const AlgResourceResponse &algResource, const std::string &newTag, AlgType algType,
6360 : bool isCustom, bool needIncreLink, bool needRecreateAlltoallComm)
6361 : {
6362 0 : uint64_t streamMode = 0;
6363 0 : CHK_RET(hrtStreamGetMode(param.stream.ptr(), &streamMode));
6364 : rtStream_t aicpuStream;
6365 0 : Mc2AiCpuStreamAllocAndGet(streamMode, aicpuStream); // aicpuStream需要在首次下发时申请
6366 0 : if (!isContextLaunched_) {
6367 : // 1、通信域内首次下发,从algResource中获取资源,H2D刷新资源,launch init
6368 : rtStream_t aicpuInitStream;
6369 0 : Mc2AiCpuInitStreamAllocAndGet(streamMode, aicpuInitStream); // 使用aicpuInitStream_下初始化kernel
6370 0 : Stream tmpStream(aicpuInitStream);
6371 0 : HCCL_DEBUG("%s ContextLaunched, aicpuInitStream:%p, aicpuStream:%p", __func__, aicpuInitStream, aicpuStream);
6372 0 : CHK_RET(AicpuResourceInit(algName, algResource, newTag, aicpuInitStream, opType, isCustom));
6373 0 : CHK_RET(GetReportHcclMC2Info(tmpStream, algResource.slaveDevStreams));
6374 0 : CHK_RET(SetAicpuUnfoldFlag());
6375 0 : } else if (newTagResAlloced_.find(newTag) == newTagResAlloced_.end() ||
6376 0 : (opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV && needIncreLink) || needRecreateAlltoallComm) {
6377 : // 2、通信域内非首次,但是有新的newTag,查看是否需要补充资源。
6378 0 : PetersonLockGuard guard(hostDeviceLock_.get());
6379 0 : CHK_PRT_RET(guard.IsLockFailed(),
6380 : HCCL_ERROR("[HcclCommunicator][OrchestrateAicp] hostDeviceLock lock failed"), HCCL_E_INTERNAL);
6381 0 : CHK_RET(AicpuResourceRefresh(algResource, newTag, opType));
6382 0 : }
6383 0 : HCCL_DEBUG("%s isContextLaunched[%u], needRecreateAlltoallComm[%u]", __func__, isContextLaunched_, needRecreateAlltoallComm);
6384 0 : bool isUsedMainStream = (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB);
6385 : // inplace支持重执行的stream资源处理逻辑
6386 0 : bool isHcclOpInplace = IsHcclOpInplace(opType, param, userRank_, userRankSize_, isInplaceStatus_);
6387 0 : if ((retryOrigWorkflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB) &&
6388 0 : retryEnable_ && isHcclOpInplace &&
6389 0 : (opType == HcclCMDType::HCCL_CMD_ALLREDUCE || opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER)) {
6390 0 : isUsedMainStream = true;
6391 : }
6392 0 : AicpuOpTiling opTilingInfo;
6393 0 : opTilingInfo.algName = algName;
6394 0 : opTilingInfo.newTag = newTag;
6395 0 : opTilingInfo.algType = algType;
6396 0 : opTilingInfo.isUsedMainStream = isUsedMainStream;
6397 0 : opTilingInfo.dumpDebug = GetExternalInputHcclDumpDebug();
6398 0 : aclrtFloatOverflowMode floatOverflowMode = ACL_RT_OVERFLOW_MODE_UNDEF;
6399 0 : CHK_RET(hrtGetDeviceSatMode(&floatOverflowMode));
6400 0 : opTilingInfo.floatOverflowMode = floatOverflowMode;
6401 0 : HcclResult ret = HCCL_SUCCESS;
6402 : // 根据算子类型,获取 Aicpu Kernel 名称
6403 0 : auto iter = HCOM_CMD_TYPE_STR_MAP.find(opType);
6404 0 : CHK_PRT_RET((iter == HCOM_CMD_TYPE_STR_MAP.end()),
6405 : HCCL_ERROR("[%s] RunAicpuRpcSrvLaunchV2 kernel not found, opType=[%d]", __func__, static_cast<int>(opType)),
6406 : HCCL_E_INTERNAL);
6407 0 : std::string kernelName = std::string("RunAicpuRpcSrvLaunchV2") + "_" + iter->second;
6408 0 : ret = AicpuKfcTilingDataLaunchExt(param, opType, opResDevicePara_, kernelName, opTilingInfo, isCustom);
6409 0 : if (ret != HCCL_SUCCESS) {
6410 0 : HCCL_ERROR("[HcclCommunicator][OrchestrateAicpu]aicpu unfold launch kernel[%s] failed. ret[%d] inputPtr[%p]"
6411 : "outputPtr[%p] count[%llu] dataType[%s] op[%s]",
6412 : kernelName.c_str(), ret, param.inputPtr, param.outputPtr,
6413 : param.DataDes.count, GetDataTypeEnumStr(param.DataDes.dataType).c_str(),
6414 : GetReduceOpEnumStr(param.reduceType).c_str());
6415 0 : return ret;
6416 : }
6417 0 : return HCCL_SUCCESS;
6418 0 : }
6419 :
6420 5 : HcclResult HcclCommunicator::CalcTinySendRecvMem(const OpParam &opParam, AlgResourceResponse &algResResponse,
6421 : DeviceMem &tinySendRecvMem)
6422 : {
6423 5 : u64 sendCount = 0;
6424 5 : u64 recvCount = 0;
6425 5 : if (opParam.opType == HcclCMDType::HCCL_CMD_ALLTOALLV) {
6426 12 : for (u32 i = 0; i < userRankSize_; i++) {
6427 8 : u64 curSendCount = *(static_cast<const u64 *>(opParam.All2AllDataDes.sendCounts) + i) +
6428 8 : *(static_cast<const u64 *>(opParam.All2AllDataDes.sdispls) + i);
6429 8 : sendCount = std::max(sendCount, curSendCount);
6430 8 : u64 curRecvCount = *(static_cast<const u64 *>(opParam.All2AllDataDes.recvCounts) + i) +
6431 8 : *(static_cast<const u64 *>(opParam.All2AllDataDes.rdispls) + i);
6432 8 : recvCount = std::max(recvCount, curRecvCount);
6433 : }
6434 : } else {
6435 3 : for (u32 i = 0; i < userRankSize_; i++) {
6436 2 : sendCount += *(static_cast<const u64 *>(opParam.All2AllDataDes.sendCountMatrix) +
6437 2 : userRank_ * userRankSize_ + i);
6438 2 : recvCount += *(static_cast<const u64 *>(opParam.All2AllDataDes.sendCountMatrix) +
6439 2 : userRank_ + userRankSize_ * i);
6440 : }
6441 : }
6442 :
6443 5 : u32 sendTypeSize = 0, recvTypeSize = 0;
6444 5 : CHK_RET(SalGetDataTypeSize(opParam.All2AllDataDes.sendType, sendTypeSize));
6445 5 : CHK_RET(SalGetDataTypeSize(opParam.All2AllDataDes.recvType, recvTypeSize));
6446 :
6447 : // 在sendCount/recvCount全0时, 使用tinySendRecvMem, 避免使用空deviceMem
6448 5 : algResResponse.paramInputMem = sendCount == 0 ? DeviceMem::create(tinySendRecvMem.ptr(), tinySendRecvMem.size()) : DeviceMem::create(opParam.inputPtr, sendCount * sendTypeSize);
6449 5 : algResResponse.paramOutputMem = recvCount == 0 ? DeviceMem::create(tinySendRecvMem.ptr(), tinySendRecvMem.size()) : DeviceMem::create(opParam.outputPtr, recvCount * recvTypeSize);
6450 :
6451 5 : HCCL_INFO("[HcclCommunicator][CalcTinySendRecvMem] senMem addr[%p], sendSize[%llu], "
6452 : "RecvMem addr[%p], RecvSize[%llu],",
6453 : algResResponse.paramInputMem.ptr(),
6454 : algResResponse.paramInputMem.size(), algResResponse.paramOutputMem.ptr(),
6455 : algResResponse.paramOutputMem.size());
6456 5 : return HCCL_SUCCESS;
6457 : }
6458 :
6459 3 : bool HcclCommunicator::HasRoceTransportLinks(OpCommTransport &opTransportReq)
6460 : {
6461 20 : for (u32 levelIndex = 0; levelIndex < opTransportReq.size(); levelIndex++) {
6462 20 : for (u32 ringIndex = 0; ringIndex < opTransportReq[levelIndex].size(); ringIndex++) {
6463 3 : SingleSubCommTransport &reqSingleSubComm = opTransportReq[levelIndex][ringIndex];
6464 5 : for (u32 rankIndex = 0; rankIndex < reqSingleSubComm.transportRequests.size(); rankIndex++) {
6465 3 : TransportRequest &transportRequest = reqSingleSubComm.transportRequests[rankIndex];
6466 3 : if (transportRequest.isUsedRdma) {
6467 1 : return true;
6468 : }
6469 : }
6470 : }
6471 : }
6472 2 : return false;
6473 : }
6474 :
6475 0 : HcclResult HcclCommunicator::CleanTransportLinks(OpCommTransport &opTransportReq, OpCommTransport &opTransportResponse)
6476 : {
6477 0 : for (u32 levelIndex = 0; levelIndex < opTransportReq.size(); levelIndex++) {
6478 0 : for (u32 ringIndex = 0; ringIndex < opTransportReq[levelIndex].size(); ringIndex++) {
6479 0 : SingleSubCommTransport &reqSingleSubComm = opTransportReq[levelIndex][ringIndex];
6480 0 : SingleSubCommTransport &respSingleSubComm = opTransportResponse[levelIndex][ringIndex];
6481 0 : for (u32 rankIndex = 0; rankIndex < reqSingleSubComm.transportRequests.size(); rankIndex++) {
6482 0 : TransportRequest &transportRequest = reqSingleSubComm.transportRequests[rankIndex];
6483 0 : CHK_PRT_RET(rankIndex >= respSingleSubComm.links.size(),
6484 : HCCL_ERROR("[CleanTransportLinks] The remote rank_id[%u] is larger than the existent respSingleSubComm map "\
6485 : "size[%u]", rankIndex, respSingleSubComm.links.size()), HCCL_E_PARA);
6486 0 : if (respSingleSubComm.links[rankIndex] != nullptr &&
6487 0 : respSingleSubComm.links[rankIndex]->GetLinkType() != hccl::LinkType::LINK_RESERVED && !transportRequest.isUsedRdma) {
6488 0 : HCCL_INFO("[CleanTransportLinks] The link to remote userRank[%u] has existed", transportRequest.remoteUserRank);
6489 0 : continue;
6490 : }
6491 0 : respSingleSubComm.links[rankIndex] = nullptr;
6492 : }
6493 : }
6494 : }
6495 0 : return HCCL_SUCCESS;
6496 : }
6497 :
6498 116 : HcclResult HcclCommunicator::AllocAlgNotifys(const std::string &tag, const NotifyLoadType notifyLoadType, const u32 notifyNum,
6499 : std::vector<std::shared_ptr<LocalNotify>> ¬ifiesMain, std::vector<std::shared_ptr<LocalNotify>> ¬ifiesAux)
6500 : {
6501 116 : std::vector<std::shared_ptr<LocalNotify>> notifys(notifyNum, nullptr);
6502 115 : CHK_RET(queueNotifyManagerRefac_->Alloc(tag, notifyNum, notifys, notifyLoadType));
6503 :
6504 116 : u32 signalNum = notifyNum >> 1;
6505 116 : notifiesMain.resize(signalNum);
6506 115 : notifiesAux.resize(signalNum);
6507 373 : for (u32 i = 0; i < signalNum; i++) {
6508 257 : notifiesMain[i] = notifys[i << 1];
6509 258 : notifiesAux[i] = notifys[(i << 1) + 1];
6510 : }
6511 116 : return HCCL_SUCCESS;
6512 116 : }
6513 :
6514 111 : HcclResult HcclCommunicator::AllocAlgResource(const std::string &newTag, HcclCMDType opType, const OpParam &opParam,
6515 : AlgResourceRequest &resRequest, AlgResourceResponse &algResResponse, bool selectAivAlg)
6516 : {
6517 111 : HcclResult ret = HCCL_SUCCESS;
6518 111 : bool isGraphZeroCopyAlgAlloc = false;
6519 : // 只有aicpu模式下才需要申请从流和相关的notify资源,isNeedSlaveStream为true就代表算子下发是aicpu模式
6520 111 : bool isNeedSlaveStream = !selectAivAlg && opParam.aicpuUnfoldMode;
6521 171 : if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB &&
6522 61 : !IsForceAicpuOpBaseMode(opParam, opType)) {
6523 60 : isGraphZeroCopyAlgAlloc = resRequest.isInGraphCaptureZeroCopy;
6524 60 : if (isGraphZeroCopyAlgAlloc) {
6525 0 : if (resRequest.scratchMemSize > 0) {
6526 0 : algResResponse.scratchMem =
6527 0 : DeviceMem::create(cclBufferManager_.GetOutCCLbuffer().ptr(), resRequest.scratchMemSize);
6528 : }
6529 60 : } else if (resRequest.scratchMemSize > 0) {
6530 24 : algResResponse.scratchMem = GetWorkspaceScracthMem(opParam.tag, resRequest.scratchMemSize);
6531 : }
6532 :
6533 58 : if (resRequest.streamNum > 0) {
6534 17 : if (isGraphZeroCopyAlgAlloc) {
6535 0 : CHK_RET(opStreamManager_->RegisterMaster(opParam.stream));
6536 : algResResponse.slaveStreams =
6537 0 : opStreamManager_->AllocSlaves(StreamType::STREAM_TYPE_ONLINE, resRequest.streamNum);
6538 0 : CHK_PRT_RET(algResResponse.slaveStreams.empty(),
6539 : HCCL_ERROR("[AllocAlgResource]tag[%s] get slave stream failed, "
6540 : "expect to get size [%u], but only alloc 0.",
6541 : newTag.c_str(), resRequest.streamNum),
6542 : HCCL_E_INTERNAL);
6543 : } else {
6544 : // aicpu模式下不申请host从流
6545 17 : if (!opParam.aicpuUnfoldMode) {
6546 14 : algResResponse.slaveStreams = GetWorkspaceSubStreams(opParam.tag, resRequest.streamNum);
6547 : }
6548 : }
6549 : }
6550 50 : } else if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE ||
6551 0 : IsForceAicpuOpBaseMode(opParam, opType)) {
6552 50 : CHK_RET(AllocOpBaseModeScratchMem(opType, opParam, resRequest, algResResponse));
6553 51 : if ((resRequest.streamNum > 0) && !selectAivAlg) {
6554 35 : CHK_RET(opStreamManager_->RegisterMaster(opParam.stream));
6555 : algResResponse.slaveStreams =
6556 35 : opStreamManager_->AllocSlaves(StreamType::STREAM_TYPE_ONLINE, resRequest.streamNum);
6557 36 : CHK_PRT_RET(algResResponse.slaveStreams.empty(),
6558 : HCCL_ERROR("[AllocAlgResource]tag[%s] get slave stream failed, "
6559 : "expect to get size [%u], but only alloc 0.",
6560 : newTag.c_str(), resRequest.streamNum),
6561 : HCCL_E_INTERNAL);
6562 : }
6563 : } else {
6564 0 : HCCL_ERROR("[AllocAlgResource]WorkflowMode is not set.");
6565 0 : return HCCL_E_PARA;
6566 : }
6567 :
6568 111 : if (isNeedSlaveStream && ((userRankSize_ != 1) || IsForceAicpuOpBaseMode(opParam, opType))) {
6569 3 : CHK_RET(opStreamManager_->RegisterMaster(opParam.stream));
6570 : algResResponse.slaveDevStreams =
6571 4 : opStreamManager_->AllocSlaves(StreamType::STREAM_TYPE_DEVICE, LOCAL_STREAM_MAX_NUM);
6572 4 : CHK_PRT_RET(algResResponse.slaveDevStreams.empty(),
6573 : HCCL_ERROR("[AllocAlgResource]tag[%s] get slave device stream failed, "
6574 : "expect to get size [%u], but only alloc 0.",
6575 : newTag.c_str(), LOCAL_STREAM_MAX_NUM),
6576 : HCCL_E_INTERNAL);
6577 4 : CHK_RET(AllocAlgNotifys(opParam.tag, NotifyLoadType::DEVICE_NOTIFY, LOCAL_NOTIFY_MAX_NUM,
6578 : algResResponse.notifiesDevMain, algResResponse.notifiesDevAux));
6579 : }
6580 112 : uint8_t devNotifyNum = algResResponse.notifiesDevMain.size() + algResResponse.notifiesDevAux.size();
6581 112 : HCCL_INFO("[AllocAlgResource] tag[%s] alloc host slaveStreamNum[%u],"
6582 : "device slaveStreamNum[%u], devNotifyNum[%u], hostNotifyNum[%u]",
6583 : newTag.c_str(), algResResponse.slaveStreams.size(),
6584 : algResResponse.slaveDevStreams.size(), devNotifyNum, resRequest.notifyNum);
6585 112 : CHK_RET(AllocAlgNotifys(opParam.tag, NotifyLoadType::HOST_NOTIFY, resRequest.notifyNum, algResResponse.notifiesMain,
6586 : algResResponse.notifiesAux));
6587 :
6588 110 : algResResponse.cclInputMem = cclBufferManager_.GetInCCLbuffer();
6589 111 : algResResponse.cclOutputMem = cclBufferManager_.GetOutCCLbuffer();
6590 110 : DeviceMem expMem = cclBufferManager_.GetCommCCLBuffer(); // 获取拓展内存
6591 111 : if (opParam.opType == HcclCMDType::HCCL_CMD_ALLTOALLV || opParam.opType == HcclCMDType::HCCL_CMD_ALLTOALLVC || opParam.opType == HcclCMDType::HCCL_CMD_ALLTOALL) {
6592 4 : DeviceMem tinySendRecvMem;
6593 4 : CHK_RET(implAlg_->GetTinyMem(tinySendRecvMem));
6594 4 : CHK_RET(CalcTinySendRecvMem(opParam, algResResponse, tinySendRecvMem));
6595 8 : } else {
6596 107 : algResResponse.paramInputMem = DeviceMem::create(opParam.inputPtr, opParam.inputSize);
6597 106 : algResResponse.paramOutputMem = DeviceMem::create(opParam.outputPtr, opParam.outputSize);
6598 : }
6599 :
6600 111 : bool useOpbaseFlag = (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE && !opParam.isCapture);
6601 111 : if (AIV_COMM_BUFFER_BITMASK & resRequest.aivBufferRequest) {
6602 4 : ret = cclBufferManager_.CreateCommAIVbuffer(useOpbaseFlag);
6603 4 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Alloc][AlgResource]Create CommAIVbuffer failed"), ret);
6604 4 : if (useOpbaseFlag) { // 单算子非Capture模式,对应aivOpbaseTag_
6605 2 : algResResponse.aivInputMem = cclBufferManager_.GetInAivOpbaseBuffer();
6606 2 : algResResponse.aivOutputMem = cclBufferManager_.GetOutAivOpbaseBuffer();
6607 : } else { // 静态图或者Capture模式,对应aivOffloadTag_
6608 2 : algResResponse.aivInputMem = cclBufferManager_.GetInAivOffloadbuffer();
6609 2 : algResResponse.aivOutputMem = cclBufferManager_.GetOutAivOffloadbuffer();
6610 : }
6611 4 : HCCL_INFO("[AllocAlgResource] tag[%s] alloc aiv buffer", newTag.c_str());
6612 : }
6613 111 : if ((AIV_COMM_INFO_BUFFER_BITMASK & resRequest.aivBufferRequest) || opParam.isNpuDirectRoce) {
6614 0 : if (!useOpbaseFlag) {
6615 0 : DeviceMem aivCommInfoMem; // 图模式每个算子单独一块内存
6616 0 : CHK_RET(DeviceMem::alloc(aivCommInfoMem, AIV_COMM_INFO_SIZE));
6617 0 : algResResponse.aivCommInfoMem = aivCommInfoMem;
6618 0 : aivOffloadCommInfoMem_.emplace_back(std::move(aivCommInfoMem));
6619 0 : } else {
6620 0 : ret = cclBufferManager_.CreateCommInfoAIVbuffer();
6621 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Alloc][AlgResource]Create CommInfoAIVbuffer failed"), ret);
6622 0 : algResResponse.aivCommInfoMem = cclBufferManager_.GetAivCommInfoBuffer(); // 单算子每个通信域只用一块内存
6623 : }
6624 0 : HCCL_INFO("[AllocAlgResource] tag[%s] alloc aiv comm info buffer", newTag.c_str());
6625 : }
6626 :
6627 222 : TransportIOMem transMem{algResResponse.cclInputMem, algResResponse.cclOutputMem,
6628 333 : algResResponse.paramInputMem, algResResponse.paramOutputMem, algResResponse.scratchMem,
6629 111 : algResResponse.aivInputMem, algResResponse.aivOutputMem, expMem, DeviceMem()};
6630 112 : HCCL_DEBUG("algResResponse.cclInputMem[%p], size[%llu]; algResResponse.cclOutputMem[%p], "
6631 : "size[%llu]; algResResponse.paramInputMem[%p], size[%llu]; algResResponse.paramOutputMem[%p], size[%llu].",
6632 : algResResponse.cclInputMem.ptr(), algResResponse.cclInputMem.size(),
6633 : algResResponse.cclOutputMem.ptr(), algResResponse.cclOutputMem.size(),
6634 : algResResponse.paramInputMem.ptr(), algResResponse.paramInputMem.size(),
6635 : algResResponse.paramOutputMem.ptr(), algResResponse.paramOutputMem.size());
6636 112 : algResResponse.opTransportResponse = resRequest.opTransport;
6637 :
6638 : // 零拷贝场景这里只借助P2p的openIpc能力交换控制面zeroCopyLocalBuffer_,不交换实际用户的输出输出
6639 112 : if (opParam.isZeroCopy) {
6640 0 : HCCL_INFO("[AllocAlgResource] zero copy change paramInput[%p] paramOutput[%p] scratchMem[%p] to localBuffer[%p]",
6641 : transMem.paramInputMem.ptr(), transMem.paramOutputMem.ptr(), transMem.scratchMem.ptr(), zeroCopyLocalBuffer_.ptr());
6642 0 : transMem.scratchMem = zeroCopyLocalBuffer_;
6643 0 : transMem.paramInputMem = zeroCopyLocalBuffer_;
6644 0 : transMem.paramOutputMem = zeroCopyLocalBuffer_;
6645 : } else {
6646 112 : if (isGraphZeroCopyAlgAlloc) {
6647 0 : transMem.scratchMem =
6648 0 : DeviceMem::create(cclBufferManager_.GetOutCCLbuffer().ptr(), resRequest.scratchMemSize);
6649 0 : HCCL_INFO("[AllocAlgResource] acl graph set transMem.scratchMem =%ul", transMem.scratchMem.size());
6650 : }
6651 : }
6652 :
6653 112 : ClearOpTransportResponseLinks(algResResponse.opTransportResponse);
6654 110 : if (IsEnableBackupLink()) {
6655 0 : algResResponse.opTransportResponseBackUp = resRequest.opTransport;
6656 0 : ClearOpTransportResponseLinks(algResResponse.opTransportResponseBackUp);
6657 0 : HCCL_DEBUG("[%s]IsEnableBackupLink[%d] init backup & default opTransportResponse", __func__,
6658 : IsEnableBackupLink());
6659 : }
6660 :
6661 110 : if (!GetExternalInputHcclEnableFfts() && GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
6662 42 : u32 slaveNum = algResResponse.slaveStreams.size();
6663 42 : algResResponse.threadManage.resize(slaveNum);
6664 112 : for (u32 ringIndex = 0; ringIndex < slaveNum; ringIndex++) {
6665 140 : algResResponse.threadManage[ringIndex].reset(new (std::nothrow) ThreadManage(deviceLogicId_,
6666 : userRank_,
6667 70 : dispatcher_));
6668 70 : CHK_SMART_PTR_NULL(algResResponse.threadManage[ringIndex]);
6669 70 : HcclResult ret = algResResponse.threadManage[ringIndex]->Init();
6670 70 : CHK_PRT_RET(ret != HCCL_SUCCESS,
6671 : HCCL_ERROR("[Init][MultiRingResource]ringIndex[%u] ThreadManage failed,return[%d]",
6672 : ringIndex, ret),
6673 : ret);
6674 70 : HCCL_INFO("ringThreadsManage Init success[%u]", ringIndex);
6675 : }
6676 : }
6677 110 : transportManager_->SetOpType(opParam.opType);
6678 111 : if (isUserMemRegisted_) {
6679 : // user win模式,用exchange接口建链的transport
6680 0 : algResResponse.opTransportResponse = userMemTransport_;
6681 0 : CHK_RET(GetRemoteUserMemResource());
6682 : } else {
6683 111 : StateGuard<HcclCommunicator, HcclCommState> guard(this, HcclCommState::BUILDING);
6684 111 : ret = transportManager_->Alloc(opParam.tag, transMem, algResResponse.opTransportResponse,
6685 111 : opParam.aicpuUnfoldMode, false, opParam.isZeroCopy, opParam.opType,
6686 111 : opParam.isCapture, false, opParam.isNpuDirectRoce, &opParam);
6687 112 : CHK_PRT_RET(ret != HCCL_SUCCESS,
6688 : HCCL_ERROR("[%s]Alloc transports failed, tag[%s]", __func__, newTag.c_str()), ret);
6689 112 : }
6690 59 : CHK_PRT_RET(ret != HCCL_SUCCESS,
6691 : HCCL_ERROR("[%s]Alloc transports failed, tag[%s]", __func__, newTag.c_str()), ret);
6692 :
6693 59 : if (retryEnable_) {
6694 : // 获取当前rdma相连的所有对端rankList
6695 0 : std::vector<u32> rankList;
6696 0 : CHK_RET(transportManager_->GetRemoteRankList(algResResponse.opTransportResponse, rankList,
6697 : TransportType::TRANS_TYPE_IBV_EXP));
6698 0 : std::string rankListStr = "";
6699 0 : for (auto remoteRank : rankList) {
6700 0 : rankListStr += (std::to_string(remoteRank) + ";");
6701 : }
6702 0 : HCCL_DEBUG("identifier[%s] newTag[%s] rankList[%s]", identifier_.c_str(), newTag.c_str(), rankListStr.c_str());
6703 0 : CHK_RET(OpRetryManager::AddLinkInfoByIdentifier(deviceLogicId_, identifier_, newTag, rankList));
6704 0 : }
6705 :
6706 59 : if (IsEnableBackupLink()) {
6707 : // 超节点 && level2支持重执行 && Aicpu:创建备用Transport资源
6708 0 : StateGuard<HcclCommunicator, HcclCommState> guard(this, HcclCommState::BUILDING);
6709 0 : ret = transportManager_->Alloc(opParam.tag, transMem, algResResponse.opTransportResponseBackUp,
6710 0 : opParam.aicpuUnfoldMode, true, opParam.isCapture);
6711 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
6712 : HCCL_ERROR("[%s]Alloc backup transports failed, tag[%s]", __func__, newTag.c_str()), ret);
6713 0 : }
6714 59 : SaveLinkRes(algResResponse.opTransportResponse);
6715 59 : SaveLinkRes(algResResponse.opTransportResponseBackUp);
6716 59 : remoteTransportMap_ = transportManager_->GetRemoteTransportMap();
6717 59 : HCCL_DEBUG("[%s] process success newtag[%s]", __func__, newTag.c_str());
6718 59 : return HCCL_SUCCESS;
6719 112 : }
6720 :
6721 0 : HcclResult HcclCommunicator::GetRemoteUserMemResource()
6722 : {
6723 0 : for (auto &levelNSubCommTransport : userMemTransport_) {
6724 0 : for (auto &singleSubCommTransport : levelNSubCommTransport) {
6725 0 : u32 linkIdx = 0;
6726 0 : for (auto &transportRequest : singleSubCommTransport.transportRequests) {
6727 0 : if (!transportRequest.isValid) {
6728 0 : continue;
6729 : }
6730 0 : auto tempLink = singleSubCommTransport.links[linkIdx];
6731 0 : MemDetails remoteMem;
6732 0 : u32 remoteId = tempLink->GetRemoteRank();
6733 0 : CHK_PRT_RET((remoteId >= MAX_RANK_NUM_A3),
6734 : HCCL_ERROR("[%s]Invalid remoteId, valid range is [0, %u), remoteId[%u]", __func__,
6735 : MAX_RANK_NUM_A3, remoteId), HCCL_E_PARA);
6736 0 : void *userMemPtr = nullptr;
6737 0 : CHK_RET(tempLink->GetRemoteMem(UserMemType::INPUT_MEM, &userMemPtr));
6738 0 : CHK_PTR_NULL(userMemPtr);
6739 0 : remoteMem.addr = reinterpret_cast<u64>(userMemPtr);
6740 0 : CHK_RET(tempLink->GetRemoteMemSize(UserMemType::INPUT_MEM, remoteMem.size));
6741 0 : opResPara_.userMemRes[remoteId] = remoteMem;
6742 0 : HCCL_INFO("[%s]add userMem res success, remoteId[%u], "
6743 : "remote addr[%llu], linkIdx[%u]", __func__, remoteId, remoteMem.addr, linkIdx);
6744 0 : linkIdx++;
6745 0 : }
6746 : }
6747 : }
6748 0 : opResPara_.userMemType = TYPE_USER_MEM;
6749 0 : return HCCL_SUCCESS;
6750 : }
6751 :
6752 0 : HcclResult HcclCommunicator::IncreAllocLink(const std::string &newTag, const OpParam &opParam,
6753 : AlgResourceRequest &resRequest, AlgResourceResponse &algResResponse)
6754 : {
6755 0 : algResResponse.cclInputMem = cclBufferManager_.GetInCCLbuffer();
6756 0 : algResResponse.cclOutputMem = cclBufferManager_.GetOutCCLbuffer();
6757 0 : DeviceMem expMem = cclBufferManager_.GetCommCCLBuffer();
6758 0 : transportManager_->SetOpType(opParam.opType);
6759 :
6760 0 : TransportIOMem transMem{algResResponse.cclInputMem, algResResponse.cclOutputMem,
6761 0 : algResResponse.paramInputMem, algResResponse.paramOutputMem, algResResponse.scratchMem,
6762 0 : algResResponse.aivInputMem, algResResponse.aivOutputMem, expMem, DeviceMem()};
6763 : {
6764 0 : StateGuard<HcclCommunicator, HcclCommState> guard(this, HcclCommState::BUILDING);
6765 0 : CHK_RET(transportManager_->IncreAlloc(opParam.tag, transMem, resRequest.opTransport,
6766 : algResResponse.opTransportResponse, opParam.aicpuUnfoldMode, false,
6767 : opParam.isCapture, opParam.opType));
6768 0 : }
6769 0 : if (retryEnable_) {
6770 : // 获取当前rdma相连的所有对端rankList
6771 0 : std::vector<u32> rankList;
6772 0 : CHK_RET(transportManager_->GetIncreRemoteRankList(resRequest.opTransport,
6773 : rankList, TransportType::TRANS_TYPE_IBV_EXP));
6774 0 : std::string rankListStr = "";
6775 0 : for (auto remoteRank : rankList)
6776 : {
6777 0 : rankListStr += (std::to_string(remoteRank) + ";");
6778 : }
6779 0 : HCCL_DEBUG("identifier[%s] newTag[%s] rankList[%s]", identifier_.c_str(), newTag.c_str(), rankListStr.c_str());
6780 0 : CHK_RET(OpRetryManager::AddLinkInfoByIdentifier(deviceLogicId_, identifier_, newTag, rankList, true));
6781 0 : }
6782 0 : if (IsEnableBackupLink()) {
6783 0 : StateGuard<HcclCommunicator, HcclCommState> guard(this, HcclCommState::BUILDING);
6784 0 : CHK_RET(transportManager_->IncreAlloc(opParam.tag, transMem, resRequest.opTransport,
6785 : algResResponse.opTransportResponseBackUp, opParam.aicpuUnfoldMode, true,
6786 : opParam.isCapture, opParam.opType));
6787 0 : }
6788 0 : remoteTransportMap_ = transportManager_->GetRemoteTransportMap();
6789 0 : SaveLinkRes(algResResponse.opTransportResponse);
6790 0 : SaveLinkRes(algResResponse.opTransportResponseBackUp);
6791 0 : return HCCL_SUCCESS;
6792 0 : }
6793 :
6794 0 : HcclResult HcclCommunicator::SetDevicePid(s32 devicePid)
6795 : {
6796 0 : devicePid_ = devicePid;
6797 0 : return HCCL_SUCCESS;
6798 : }
6799 :
6800 803 : void HcclCommunicator::ReleaseWorkSpacebuffer()
6801 : {
6802 803 : workSpace_.free();
6803 804 : }
6804 :
6805 0 : HcclResult HcclCommunicator::AllocAndClearDeviceMem(u64 size, std::shared_ptr<DeviceMem> &bufferPtr) const
6806 : {
6807 0 : CHK_PRT_RET(!size,
6808 : HCCL_INFO("[HcclCommunicator][AllocAndClearDeviceMem]device memory size is zero. not need to malloc memory"),
6809 : HCCL_SUCCESS);
6810 :
6811 : CHK_PRT_RET((size > ULONG_MAX),
6812 : HCCL_ERROR("[HcclCommunicator][AllocAndClearDeviceMem]device memory size is greater than %llu", ULONG_MAX),
6813 : HCCL_E_PARA);
6814 :
6815 0 : DeviceMem tmpBuffer;
6816 0 : CHK_RET(DeviceMem::alloc(tmpBuffer, size));
6817 0 : EXCEPTION_CATCH((bufferPtr = std::make_shared<DeviceMem>(std::move(tmpBuffer))), return HCCL_E_PTR);
6818 :
6819 0 : CHK_PRT_RET(size && !bufferPtr.get()->ptr(),
6820 : HCCL_ERROR("[HcclCommunicator][AllocAndClearDeviceMem]Create DeviceMem size[%llu] fail,"
6821 : "please check workspace size.",
6822 : size),
6823 : HCCL_E_PTR);
6824 0 : CHK_RET(hrtMemSet(bufferPtr.get()->ptr(), size, size));
6825 0 : return HCCL_SUCCESS;
6826 0 : }
6827 :
6828 1490 : HcclResult HcclCommunicator::AllocAndClearHostMem(u64 size, std::shared_ptr<HostMem> &bufferPtr) const
6829 : {
6830 1490 : CHK_PRT_RET(!size,
6831 : HCCL_INFO("[HcclCommunicator][AllocAndClearHostMem] host memory size is zero. not need to malloc memory"),
6832 : HCCL_SUCCESS);
6833 :
6834 : CHK_PRT_RET((size > ULONG_MAX),
6835 : HCCL_ERROR("[HcclCommunicator][AllocAndClearHostMem] host memory size is greater than %llu", ULONG_MAX),
6836 : HCCL_E_PARA);
6837 :
6838 1490 : HostMem tmpBuffer = HostMem::alloc(size);
6839 1496 : EXCEPTION_CATCH((bufferPtr = std::make_shared<HostMem>(std::move(tmpBuffer))), return HCCL_E_PTR);
6840 :
6841 1491 : CHK_PRT_RET(size && !bufferPtr.get()->ptr(),
6842 : HCCL_ERROR("[HcclCommunicator][AllocAndClearHostMem]host memory space size[%llu] fail,"
6843 : "please check workspace size.",
6844 : size),
6845 : HCCL_E_PTR);
6846 1492 : CHK_SAFETY_FUNC_RET(memset_s(bufferPtr.get()->ptr(), size, 0, size));
6847 1493 : return HCCL_SUCCESS;
6848 1493 : }
6849 :
6850 520 : HcclResult HcclCommunicator::CreateWorkSpace(u64 size, DeviceMem &buffer) const
6851 : {
6852 520 : CHK_PRT_RET(!size, HCCL_INFO("[Create][WorkSpace]work space size is zero. not need to malloc memory"),
6853 : HCCL_SUCCESS);
6854 :
6855 : CHK_PRT_RET((size > ULONG_MAX),
6856 : HCCL_ERROR("[Create][WorkSpace]work space size is greater than %llu",
6857 : ULONG_MAX),
6858 : HCCL_E_PARA);
6859 :
6860 520 : u64 memSize = size;
6861 520 : CHK_RET(DeviceMem::alloc(buffer, memSize));
6862 520 : CHK_RET(hrtMemSet(buffer.ptr(), size, size));
6863 520 : return HCCL_SUCCESS;
6864 : }
6865 :
6866 0 : HcclResult HcclCommunicator::GetWorkSpace(u64 *workSpaceSize, u64 *workSpace) const
6867 : {
6868 0 : *workSpaceSize = workSpaceSize_;
6869 0 : *workSpace = reinterpret_cast<u64>(workSpace_.ptr());
6870 0 : return HCCL_SUCCESS;
6871 : }
6872 :
6873 0 : HcclResult HcclCommunicator::InitWorkSpace()
6874 : {
6875 0 : if (workSpace_.ptr() == nullptr) {
6876 0 : workSpaceSize_ = COMM_MAX_WORK_SPACE_SIZE;
6877 0 : CHK_RET(CreateWorkSpace(workSpaceSize_, workSpace_));
6878 : }
6879 0 : return HCCL_SUCCESS;
6880 : }
6881 :
6882 0 : HcclResult HcclCommunicator::FillOpParam(const HcclCMDType commType, OpParam &opParam,
6883 : const uint64_t count, void *pCount, void *pDispls)
6884 : {
6885 0 : if (commType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER ||
6886 0 : commType == HcclCMDType::HCCL_CMD_ALLGATHER ||
6887 : commType == HcclCMDType::HCCL_CMD_ALLREDUCE) {
6888 0 : opParam.DataDes.count = count;
6889 0 : opParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_FP16; // 按照fp16配置
6890 0 : } else if (commType == HcclCMDType::HCCL_CMD_ALLTOALLV ||
6891 0 : commType == HcclCMDType::HCCL_CMD_ALLTOALL ||
6892 : commType == HcclCMDType::HCCL_CMD_ALLTOALLVC) {
6893 0 : opParam.All2AllDataDes.sendType = HcclDataType::HCCL_DATA_TYPE_FP16;
6894 0 : opParam.All2AllDataDes.recvType = HcclDataType::HCCL_DATA_TYPE_FP16;
6895 0 : opParam.All2AllDataDes.sendCounts = pCount;
6896 0 : opParam.All2AllDataDes.recvCounts = pCount;
6897 0 : opParam.All2AllDataDes.sdispls = pDispls;
6898 0 : opParam.All2AllDataDes.rdispls = pDispls;
6899 0 : opParam.All2AllDataDes.sendCountMatrix = pCount;
6900 0 : } else if (commType == HcclCMDType::HCCL_CMD_BATCH_WRITE) {
6901 : } else {
6902 0 : HCCL_ERROR("[%s] invalid commType=[%u]",
6903 : __func__, static_cast<uint32_t>(commType));
6904 0 : return HCCL_E_PARA;
6905 : }
6906 0 : return HCCL_SUCCESS;
6907 : }
6908 :
6909 0 : HcclResult HcclCommunicator::AllocComResource(const string &newTag, const string &algName,
6910 : const HcclCMDType commType, const OpParam &opParam, rtStream_t stream, bool isNeedHostSlaveStream)
6911 : {
6912 0 : if (resMap_.find(newTag) == resMap_.end()) { // 计算&申请通信资源
6913 0 : unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(commType);
6914 0 : CHK_PRT_RET(algOperator == nullptr,
6915 : HCCL_ERROR("[%s] algOperator is nullptr", __func__), HCCL_E_INTERNAL);
6916 0 : AlgResourceRequest resRequest;
6917 0 : CHK_RET(algOperator->CalcResRequest(algName, opParam, resRequest));
6918 0 : AlgResourceResponse algResResponse;
6919 0 : CHK_RET(AllocAlgResource(newTag, commType, opParam, resRequest, algResResponse, isNeedHostSlaveStream));
6920 0 : resMap_[newTag] = algResResponse;
6921 0 : CHK_RET(RegisterToHeartBeat());
6922 0 : }
6923 :
6924 0 : CHK_RET(InitWorkSpace());
6925 0 : HcclResult ret = GetWorkSpace(&(opResPara_.mc2WorkSpace.workSpaceSize), &(opResPara_.mc2WorkSpace.workSpace));
6926 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
6927 : HCCL_ERROR("%s GetWorkSpace fail, size[%llu] space[%llu]", __func__,
6928 : opResPara_.mc2WorkSpace.workSpaceSize, opResPara_.mc2WorkSpace.workSpace),
6929 : ret);
6930 :
6931 0 : if (!isContextLaunched_) { // 通信域内首次下发
6932 0 : uint64_t streamMode = 0;
6933 0 : CHK_RET(hrtStreamGetMode(opParam.stream.ptr(), &streamMode));
6934 : rtStream_t aicpuStream;
6935 0 : Mc2AiCpuStreamAllocAndGet(streamMode, aicpuStream); // aicpuStream需要在首次下发时申请
6936 :
6937 : rtStream_t aicpuInitStream;
6938 0 : Mc2AiCpuInitStreamAllocAndGet(streamMode, aicpuInitStream);
6939 0 : Stream tmpStream(aicpuInitStream);
6940 0 : HCCL_DEBUG("%s ContextLaunched, aicpuInitStream:%p, aicpuStream:%p", __func__, aicpuInitStream, aicpuStream);
6941 0 : CHK_RET(AicpuResourceInit(algName, resMap_[newTag], newTag, stream, commType));
6942 0 : CHK_RET(GetReportHcclMC2Info(tmpStream, resMap_[newTag].slaveDevStreams));
6943 0 : } else if (newTagResAlloced_.find(newTag) == newTagResAlloced_.end()) {
6944 : // 通信域内非首次,但是有新的newTag
6945 0 : PetersonLockGuard guard(hostDeviceLock_.get());
6946 0 : CHK_PRT_RET(guard.IsLockFailed(),
6947 : HCCL_ERROR("[%s] hostDeviceLock lock failed", __func__), HCCL_E_INTERNAL);
6948 0 : CHK_RET(AicpuResourceRefresh(resMap_[newTag], newTag, commType));
6949 0 : }
6950 0 : return HCCL_SUCCESS;
6951 : }
6952 :
6953 2 : HcclResult HcclCommunicator::AllocComResourceByTiling(const string &algConfig, void *param)
6954 : {
6955 2 : CHK_PTR_NULL(combinOparaMem_);
6956 2 : HcclCombinOpParam *combinOparaPtr = reinterpret_cast<HcclCombinOpParam*>(combinOparaMem_->ptr());
6957 2 : CHK_PTR_NULL(combinOparaPtr);
6958 :
6959 2 : string algName, newTag;
6960 2 : OpParam &opParam = *static_cast<OpParam *>(param);
6961 2 : CHK_RET(GetAlgInfo(algConfig, opParam.tag, opParam.opType, algName, newTag));
6962 2 : if (algName == "RunAlltoAllAivDirect") {
6963 1 : opParam.isNpuDirectRoce = true;
6964 : }
6965 2 : CHK_RET(CreateAndGetAiCpuNotifyWithNotifyRes(combinOparaPtr->signalInfo.aicpuNotify));
6966 0 : HCCL_INFO("Create aicpu notify %p.", localAiCpuNotifyRes_[0]->ptr());
6967 :
6968 : // 只有第一次创建,此处通过CCL Buffer地址有效来防止通信域内非首次重新申请内存
6969 : // 已注册user mem情况下,不创建ccl buffer,使用user mem通信
6970 0 : if (userMemMap_.empty()) {
6971 0 : CHK_RET(CreateCommCCLbuffer());
6972 0 : CHK_RET(cclBufferManager_.GetInCCLbuffer(opParam.inputPtr, opParam.inputSize));
6973 0 : CHK_RET(cclBufferManager_.GetOutCCLbuffer(opParam.outputPtr, opParam.outputSize));
6974 : } else {
6975 0 : auto it = userMemMap_.begin();
6976 0 : opParam.outputSize = it->second->size();
6977 0 : opParam.inputSize = it->second->size();
6978 : }
6979 :
6980 : // 按照 ccl buffer size 折算,不同算子折算方式不同, allreduce和cclbuffer size相同
6981 : // allgather、reducescatter、alltoall需除以rank size
6982 0 : uint64_t count = opParam.outputSize / SIZE_TABLE[HcclDataType::HCCL_DATA_TYPE_FP16];
6983 0 : if (opParam.opType != HcclCMDType::HCCL_CMD_ALLREDUCE) {
6984 0 : count = (count + userRankSize_ - 1) / userRankSize_;
6985 : }
6986 0 : HCCL_INFO("[%s] userRankSize=[%u], count=[%u]", __func__, userRankSize_, count);
6987 0 : vector<uint64_t> countList(userRankSize_ * userRankSize_, count);
6988 0 : vector<uint64_t> displsList(userRankSize_, 0);
6989 0 : void *pCount = reinterpret_cast<void *>(&countList[0]);
6990 0 : void *pDispls = reinterpret_cast<void *>(&displsList[0]);
6991 0 : CHK_RET(FillOpParam(opParam.opType, opParam, count, pCount, pDispls));
6992 : // MC2算子不需要申请host侧的从流
6993 0 : bool isNeedHostSlaveStream = false;
6994 0 : CHK_RET(AllocComResource(newTag, algName, opParam.opType, opParam, opParam.stream.ptr(), isNeedHostSlaveStream));
6995 0 : return HCCL_SUCCESS;
6996 2 : }
6997 :
6998 1 : HcclResult HcclCommunicator::CreateCommResource(const std::string &tag, rtStream_t aiCpuStream, bool isOpbaseMode,
6999 : void **commContext, const std::string &algConfig)
7000 : {
7001 1 : const std::string &suffix = HCCL_MC2_MULTISERVER_SUFFIX;
7002 1 : string algName = "";
7003 1 : string newTag = tag;
7004 1 : if (tag.size() > suffix.size() && tag.compare(tag.size() - suffix.size(), suffix.size(), suffix) == 0)
7005 : {
7006 0 : HCCL_INFO("[HcclCommunicator][CreateCommResource] Set isA2MC2MultiServer_ to [true]");
7007 0 : isA2MC2MultiServer_ = true;
7008 0 : char* mmSysGetEnvValue = nullptr;
7009 0 : MM_SYS_GET_ENV(MM_ENV_HCCL_INTRA_PCIE_ENABLE, mmSysGetEnvValue);
7010 0 : std::string intraPcieEnableEnv = (mmSysGetEnvValue != nullptr) ? mmSysGetEnvValue : "EmptyString";
7011 0 : bool envA2MC2Hie = (intraPcieEnableEnv == "1") && (GetExternalInputIntraRoceSwitch() == 0);
7012 0 : if (!algConfig.empty()) {
7013 0 : CHK_RET(GetAlgInfo(algConfig, tag, algName));
7014 0 : if (algName == "DispatchCombineHierarchy" || (algName == "BatchWriteBySdma" && envA2MC2Hie)) {
7015 0 : isA2MC2IntraHie_ = true;
7016 0 : newTag.insert(newTag.size() - suffix.size(), "_HIE");
7017 : }
7018 : }
7019 0 : }
7020 1 : if (isA2MC2MultiServer_ && !isNeedInitNic_) {
7021 0 : InitNic(true);
7022 : }
7023 :
7024 1 : if ((deviceType_ != DevType::DEV_TYPE_910_93 && moduleNum_ > 1 && !isA2MC2MultiServer_) ||
7025 1 : (deviceType_ == DevType::DEV_TYPE_910_93 && superPodNum_ > 1)) {
7026 0 : HCCL_ERROR("[HcclCommunicator][CommResource]MC2 does not support in the current scenario, "
7027 : "device type[%d] moduleNum[%d] serverNum[%d] superPodNum[%d], isMC2MultiServer[%d].",
7028 : deviceType_, moduleNum_, serverNum_, superPodNum_, isA2MC2MultiServer_);
7029 0 : return HCCL_E_NOT_SUPPORT;
7030 : }
7031 :
7032 1 : HCCL_INFO("[HcclCommunicator][CommResource]newTag[%s] aicpu stream[%p] isOpbaseMode[%u]", newTag.c_str(), aiCpuStream,
7033 : isOpbaseMode);
7034 :
7035 1 : Stream stream(aiCpuStream);
7036 1 : CHK_RET(CreateCommAndStreamRes(newTag, stream));
7037 :
7038 1 : CHK_RET(Mc2CreateAndLaunchContext(aiCpuStream, isOpbaseMode, commContext, newTag));
7039 1 : return HCCL_SUCCESS;
7040 1 : }
7041 :
7042 0 : HcclResult HcclCommunicator::Mc2CreateAndLaunchContext(rtStream_t aiCpuStream, bool isOpbaseMode, void **commContext, const string &tag)
7043 : {
7044 0 : CHK_PTR_NULL(combinOparaMem_);
7045 0 : HcclCombinOpParam *combinOparaPtr = reinterpret_cast<HcclCombinOpParam*>(combinOparaMem_->ptr());
7046 0 : CHK_PTR_NULL(combinOparaPtr);
7047 0 : CHK_RET(InitWorkSpace());
7048 :
7049 0 : HcclResult result = GetWorkSpace(&(combinOparaPtr->mc2WorkSpace.workSpaceSize), &(combinOparaPtr->mc2WorkSpace.workSpace));
7050 0 : CHK_PRT_RET(result != HCCL_SUCCESS,
7051 : HCCL_ERROR("[HcclCommunicator][CommResource]errNo[0x%016llx] size[%llu] space[%llu]",
7052 : HCCL_ERROR_CODE(result), combinOparaPtr->mc2WorkSpace.workSpaceSize, combinOparaPtr->mc2WorkSpace.workSpace),
7053 : result);
7054 :
7055 0 : CHK_SAFETY_FUNC_RET(memcpy_s(combinOparaPtr->hcomId, sizeof(combinOparaPtr->hcomId),
7056 : identifier_.c_str(), identifier_.length() + 1));
7057 :
7058 0 : Stream tmpStream(aiCpuStream);
7059 0 : CHK_RET(CreateAndGetAiCpuNotifyWithNotifyRes(combinOparaPtr->signalInfo.aicpuNotify));
7060 0 : CHK_RET(CreateAndGetAiCpuNotify(localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_0)],
7061 : combinOparaPtr->signalInfo.aicpuOpNotify[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_0)]));
7062 0 : CHK_RET(CreateAndGetAiCpuNotify(localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_1)],
7063 : combinOparaPtr->signalInfo.aicpuOpNotify[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_1)]));
7064 : // 申请集合通信域存储context的device空间
7065 0 : CHK_RET(CreateDeviceCommContext(sizeof(HcclCombinOpParam), commContext_));
7066 0 : combinOparaPtr->config.deterministic = GetDeterministicConfig();
7067 : // retryEnable 写入aicpu_ctx
7068 0 : combinOparaPtr->config.retryEnable = static_cast<u8>(retryEnable_);
7069 0 : combinOparaPtr->config.retryHoldTime = commConfig_.GetConfigRetryHoldTime();
7070 0 : combinOparaPtr->config.retryIntervalTime = commConfig_.GetConfigRetryIntervalTime();
7071 0 : combinOparaPtr->config.notifyWaitTime =
7072 0 : (GetExternalInputHcclExecTimeoutSet() != HcclExecTimeoutSet::HCCL_EXEC_TIMEOUT_NOT_SET ||
7073 0 : commConfig_.GetConfigExecTimeOutSet()) ? commConfig_.GetConfigExecTimeOut() : NOTIFY_DEFAULT_WAIT_TIME;
7074 0 : combinOparaPtr->config.linkTimeOut = std::chrono::seconds(GetExternalInputHcclLinkTimeOut());
7075 :
7076 0 : combinOparaPtr->kfcControlTransferH2DParams = kfcControlTransferH2D_->GetCommunicateParams();
7077 0 : combinOparaPtr->kfcStatusTransferD2HParams = kfcStatusTransferD2H_->GetCommunicateParams();
7078 :
7079 0 : void *overflowAddr = nullptr;
7080 0 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
7081 0 : CHK_RET(hrtCtxGetOverflowAddr(&overflowAddr));
7082 0 : combinOparaPtr->overFlowAddr = reinterpret_cast<u64>(overflowAddr);
7083 0 : HCCL_INFO("[HcclImplBase][Mc2CreateAndLaunchContext]get combinOparaPtr->overFlowAddr %llx",
7084 : combinOparaPtr->overFlowAddr);
7085 : // 非整卡 (2DUO卡各取1芯的场景) 因为受到PCIE限制,不可以使用读操作进行数据拷贝
7086 0 : if (pairLinkInfo_[static_cast<u32>(LinkTypeInServer::HCCS_TYPE)].size() != userRankSize_) {
7087 0 : combinOparaPtr->onlyRead = 1;
7088 : }
7089 : }
7090 0 : HCCL_INFO("read only is set to %u", combinOparaPtr->onlyRead);
7091 :
7092 0 : if (isA2MC2MultiServer_) {
7093 : // 拷贝normal transport信息到device侧
7094 0 : bool isSupportAIVNormalQP = false;
7095 0 : CHK_RET(IsSupportAIVNormalQP(devicePhyId_, isSupportAIVNormalQP));
7096 0 : CHK_PTR_NULL(transDevIbverbsDataMem_);
7097 0 : const u64 ibverbsDataSize = transDevIbverbsDataMem_->size();
7098 0 : CHK_RET(DeviceMem::alloc(ibverbsDataBuffer_, ibverbsDataSize));
7099 0 : CHK_RET(hrtMemAsyncCopy(ibverbsDataBuffer_.ptr(),
7100 : ibverbsDataBuffer_.size(),
7101 : transDevIbverbsDataMem_->ptr(),
7102 : ibverbsDataSize,
7103 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE,
7104 : aiCpuStream));
7105 :
7106 0 : combinOparaPtr->ibverbsData = reinterpret_cast<u64>(ibverbsDataBuffer_.ptr());
7107 0 : combinOparaPtr->ibverbsDataSize = ibverbsDataSize;
7108 0 : combinOparaPtr->multiServerFlag = static_cast<u8>(true);
7109 :
7110 0 : CHK_PTR_NULL(combinedCapabilityMem_);
7111 0 : const u64 capabilitySize = sizeof(CombinedCapability);
7112 0 : CHK_RET(DeviceMem::alloc(combinedCapabilityBuffer_, capabilitySize));
7113 0 : CHK_RET(hrtMemAsyncCopy(combinedCapabilityBuffer_.ptr(),
7114 : combinedCapabilityBuffer_.size(),
7115 : combinedCapabilityMem_->ptr(),
7116 : capabilitySize,
7117 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE,
7118 : aiCpuStream));
7119 :
7120 0 : combinOparaPtr->capabilityPtr = reinterpret_cast<u64>(combinedCapabilityBuffer_.ptr());
7121 0 : combinOparaPtr->capabilitySize = capabilitySize;
7122 :
7123 0 : HCCL_INFO("[HcclImplBase][Mc2CreateAndLaunchContext] set ibverbsData to [%llu], "
7124 : "multiServerFlag to [%u]",
7125 : combinOparaPtr->ibverbsData, combinOparaPtr->multiServerFlag);
7126 0 : if (isSupportAIVNormalQP && isA2MC2IntraHie_) {
7127 0 : CHK_RET(H2DAiRMAInfo(tag, aiCpuStream));
7128 : }
7129 : }
7130 :
7131 : // 将通信数据拷贝到device侧,供AICPU算法编排使用
7132 0 : CHK_RET(hrtMemAsyncCopy(commContext_.ptr(), commContext_.size(), combinOparaMem_->ptr(), combinOparaMem_->size(),
7133 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
7134 :
7135 0 : std::string kernelName = "RunAicpuKfcResInit";
7136 0 : CHK_RET(AiCpuKernelLaunch(tmpStream.ptr(), reinterpret_cast<u64>(commContext_.ptr()), kernelName));
7137 0 : SetMC2EnvFlag();
7138 0 : if (isOpbaseMode == true) {
7139 0 : CHK_RET(hcclStreamSynchronize(tmpStream.ptr(), commConfig_.GetConfigExecTimeOut()));
7140 : }
7141 :
7142 0 : *commContext = commContext_.ptr();
7143 0 : return HCCL_SUCCESS;
7144 0 : }
7145 :
7146 0 : HcclResult HcclCommunicator::GetAiCpuNotifyData(const std::shared_ptr<LocalNotify> &localNotify,
7147 : HcclSignalInfo ¬ifyInfo)
7148 : {
7149 0 : if (localNotify == nullptr) {
7150 0 : HCCL_INFO("[HcclCommunicator][GetAiCpuNotifyData]notifyHandle is null");
7151 0 : notifyInfo.resId = INVALID_U64;
7152 0 : return HCCL_SUCCESS;
7153 : }
7154 :
7155 0 : CHK_RET(localNotify->GetNotifyData(notifyInfo));
7156 0 : HCCL_INFO("[HcclCommunicator][GetAiCpuNotifyData]resId[%lld], addr[%lld], devId[%u], tsId[%u].",
7157 : notifyInfo.resId, notifyInfo.addr, notifyInfo.devId, notifyInfo.tsId);
7158 0 : return HCCL_SUCCESS;
7159 : }
7160 :
7161 0 : HcclResult HcclCommunicator::CreateAndGetAiCpuNotify(std::shared_ptr<LocalNotify> &localNotify,
7162 : HcclSignalInfo ¬ifyInfo)
7163 : {
7164 0 : if (localNotify != nullptr) {
7165 0 : CHK_RET(GetAiCpuNotifyData(localNotify, notifyInfo));
7166 0 : HCCL_INFO("[HcclCommunicator][CreateAndGetAiCpuNotify]aicpu notify already create ptr[%p]",
7167 : localNotify->ptr());
7168 0 : return HCCL_SUCCESS;
7169 : }
7170 :
7171 0 : EXCEPTION_CATCH((localNotify = std::make_shared<LocalNotify>()), return HCCL_E_PTR);
7172 0 : CHK_RET(localNotify->Init(NotifyLoadType::DEVICE_NOTIFY));
7173 0 : CHK_RET(localNotify->SetIpc());
7174 :
7175 0 : CHK_RET(GetAiCpuNotifyData(localNotify, notifyInfo));
7176 0 : return HCCL_SUCCESS;
7177 : }
7178 :
7179 2 : HcclResult HcclCommunicator::Mc2AiCpuStreamAllocAndGet(u32 streamMode, rtStream_t &aiCpuStream)
7180 : {
7181 2 : if (opStream_.ptr() != nullptr) {
7182 0 : HCCL_INFO("%s already alloc, group:%s, stream id:%u", __func__, identifier_.c_str(), opStream_.id());
7183 0 : aiCpuStream = opStream_.ptr();
7184 0 : return HCCL_SUCCESS;
7185 : }
7186 :
7187 2 : constexpr u32 aicpuStreamMode = 1; // 单独申请的kernel流,使能遇错即停,避免出错后流卡住不退
7188 2 : opStream_ = Stream(StreamType::STREAM_TYPE_ONLINE);
7189 2 : CHK_RET(hrtStreamSetMode(opStream_.ptr(), aicpuStreamMode));
7190 2 : aiCpuStream = opStream_.ptr();
7191 2 : HCCL_RUN_INFO("%s alloc success, group:%s, stream id:%u, mainStreamMode:%u, aicpuStreamMode:%u",
7192 : __func__, identifier_.c_str(), opStream_.id(), streamMode, aicpuStreamMode);
7193 2 : return HCCL_SUCCESS;
7194 : }
7195 :
7196 2 : HcclResult HcclCommunicator::Mc2AiCpuInitStreamAllocAndGet(u32 streamMode, rtStream_t &aiCpuStream)
7197 : {
7198 2 : if (aicpuInitStream_.ptr() != nullptr) {
7199 1 : HCCL_INFO("%s already alloc, group:%s, stream id:%u", __func__, identifier_.c_str(), aicpuInitStream_.id());
7200 1 : aiCpuStream = aicpuInitStream_.ptr();
7201 1 : return HCCL_SUCCESS;
7202 : }
7203 :
7204 1 : constexpr u32 aicpuStreamMode = 1; // 单独申请的kernel流,使能遇错即停,避免出错后流卡住不退
7205 1 : aicpuInitStream_ = Stream(StreamType::STREAM_TYPE_ONLINE);
7206 1 : CHK_RET(hrtStreamSetMode(aicpuInitStream_.ptr(), aicpuStreamMode));
7207 1 : aiCpuStream = aicpuInitStream_.ptr();
7208 1 : HCCL_RUN_INFO("%s alloc success, group:%s, stream id:%u, mainStreamMode:%u, aicpuStreamMode:%u",
7209 : __func__, identifier_.c_str(), aicpuInitStream_.id(), streamMode, aicpuStreamMode);
7210 1 : return HCCL_SUCCESS;
7211 : }
7212 :
7213 0 : HcclResult HcclCommunicator::AicpuResourceInit(const std::string &algName,
7214 : const AlgResourceResponse &algResource, const std::string &newTag, const rtStream_t &aicpuStream,
7215 : const HcclCMDType opType, bool isCustom)
7216 : {
7217 0 : HCCL_RUN_INFO("[%s] start to init group[%s] aicpu resources newTag[%s] local rankId[%u]",
7218 : __func__, identifier_.c_str(), newTag.c_str(), userRank_);
7219 0 : isContextLaunched_ = true;
7220 0 : CHK_RET(BuildOpResParam(algName, algResource, newTag, opType, aicpuStream)); // 构建context结构体
7221 0 : std::string kernelName = "RunAicpuKfcResInitV2";
7222 : // 在这里构建suspending状态码的HDC通道初始化,并且在host侧进行init
7223 : // (这个主要是针对hcomId;对算子通信域的复用;也就是多个算子复用(tag+Identifier)这个通信域的情况)
7224 0 : CHK_RET(AiCpuKernelLaunch(aicpuStream, reinterpret_cast<u64>(opResDevicePara_.ptr()), kernelName));
7225 0 : SetMC2EnvFlag();
7226 0 : newTagResAlloced_.insert(newTag);
7227 : // 图模多档位场景,需要保证执行序上优先下资源初始化的kernel
7228 0 : CHK_RET(hcclStreamSynchronize(aicpuStream, commConfig_.GetConfigExecTimeOut()));
7229 :
7230 0 : if (IsEnableCustom()) {
7231 : struct InitTask
7232 : {
7233 : u64 context; // A矩阵地址,通信在前时为sendbuffer
7234 : bool isCustom;
7235 : };
7236 0 : InitTask customInitTask = {0};
7237 0 : customInitTask.context = reinterpret_cast<u64>(opResDevicePara_.ptr());
7238 0 : customInitTask.isCustom = true;
7239 0 : CHK_RET(BuildCustomOpResParam());
7240 0 : uint64_t customBeginTime = hrtMsprofSysCycleTime();
7241 0 : const std::string customProfName = "hcomAicpuCustomInit";
7242 :
7243 0 : u16 timeOut = 0;
7244 0 : if (opResPara_.config.notifyWaitTime == 0) {
7245 0 : timeOut = opResPara_.config.notifyWaitTime;
7246 0 : } else if (opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC >= MAX_VALUE_U16) {
7247 0 : timeOut = MAX_VALUE_U16;
7248 : } else {
7249 0 : timeOut = opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC;
7250 : }
7251 :
7252 0 : CHK_RET(AicpuAclKernelLaunch(aicpuStream, reinterpret_cast<void *>(&customInitTask),
7253 : sizeof(customInitTask), binCustomHandle_, kernelName, true, timeOut));
7254 0 : uint64_t customEndTime = hrtMsprofSysCycleTime();
7255 0 : s32 customthreadId = SalGetTid();
7256 0 : CHK_RET(ProfilingManagerPub::CallMsprofReportNodeInfo(customBeginTime, customEndTime, customProfName,
7257 : customthreadId));
7258 0 : CHK_RET(hcclStreamSynchronize(aicpuStream, commConfig_.GetConfigExecTimeOut()));
7259 0 : }
7260 :
7261 0 : return HCCL_SUCCESS;
7262 0 : }
7263 :
7264 0 : HcclResult HcclCommunicator::AiCpuKernelLaunch(const rtStream_t stm, u64 addr, const std::string &kernelName)
7265 : {
7266 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
7267 0 : const std::string profName = "hcomAicpuInit";
7268 : struct InitTask
7269 : {
7270 : u64 context; // A矩阵地址,通信在前时为sendbuffer
7271 : bool isCustom;
7272 : };
7273 0 : InitTask initTask = {0};
7274 0 : initTask.context = addr;
7275 0 : initTask.isCustom = false;
7276 :
7277 0 : u16 timeOut = 0;
7278 0 : if (opResPara_.config.notifyWaitTime == 0) {
7279 0 : timeOut = opResPara_.config.notifyWaitTime;
7280 0 : } else if (opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC >= MAX_VALUE_U16) {
7281 0 : timeOut = MAX_VALUE_U16;
7282 : } else {
7283 0 : timeOut = opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC;
7284 : }
7285 0 : CHK_RET(AicpuAclKernelLaunch(stm, reinterpret_cast<void *>(&initTask), sizeof(initTask),
7286 : binHandle_, kernelName, true, timeOut));
7287 0 : uint64_t endTime = hrtMsprofSysCycleTime();
7288 0 : s32 threadId = SalGetTid();
7289 0 : CHK_RET(ProfilingManagerPub::CallMsprofReportNodeInfo(beginTime, endTime, profName, threadId));
7290 0 : return HCCL_SUCCESS;
7291 0 : }
7292 :
7293 0 : HcclResult HcclCommunicator::AicpuKfcTilingDataLaunch(const OpParam &opParam, const HcclCMDType &opType,
7294 : const DeviceMem &deviceContext, const std::string &kernelName, const AicpuOpTiling opTilingInfo)
7295 : {
7296 0 : HCCL_DEBUG("AicpuKfcTilingDataLaunch count %llu dataType %s op %s opType %u", opParam.GetDataCount(userRank_),
7297 : GetDataTypeEnumStr(opParam.GetDataType()).c_str(), GetReduceOpEnumStr(opParam.reduceType).c_str(), opType);
7298 0 : struct HcclKFCTilingData tilingDate = {0};
7299 0 : tilingDate.sendCnt = opParam.DataDes.count;
7300 0 : tilingDate.dataType = opParam.DataDes.dataType;
7301 0 : tilingDate.commType = static_cast<uint8_t>(opType);
7302 0 : tilingDate.reduceOp = opParam.reduceType;
7303 0 : tilingDate.taskType = HCCL_KFC_TASK_HCCL_ONLY_EXE;
7304 0 : tilingDate.totalCnt = 1;
7305 0 : tilingDate.turnNum = 1;
7306 0 : tilingDate.hasCommOut = 1;
7307 0 : tilingDate.debugMode = 0;
7308 0 : CHK_RET(SetNormalMode(dispatcher_));
7309 0 : HcclWorkflowMode mode = GetWorkflowMode();
7310 0 : Stream mainStream(opParam.stream.ptr());
7311 0 : CHK_RET(LocalNotify::Post(mainStream, dispatcher_,
7312 : localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_0)], INVALID_VALUE_STAGE));
7313 0 : rtStream_t kfcOpStream = opStream_.ptr();
7314 0 : if (opTilingInfo.isUsedMainStream) {
7315 0 : kfcOpStream = opParam.stream.ptr();
7316 : }
7317 0 : CHK_RET(AicpuUnfoldKernelLaunch(opParam.inputPtr, opParam.outputPtr, kfcOpStream,
7318 : reinterpret_cast<u64>(deviceContext.ptr()), &tilingDate, sizeof(HcclKFCTilingData),
7319 : kernelName, mode, opParam.tag));
7320 0 : CHK_RET(LocalNotify::Wait(mainStream, dispatcher_,
7321 : localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_1)], INVALID_VALUE_STAGE));
7322 0 : return HCCL_SUCCESS;
7323 0 : }
7324 :
7325 6 : HcclResult HcclCommunicator::AicpuKfcClearOpResLaunch(const std::unordered_set<std::string> &tags)
7326 : {
7327 6 : if (tags.empty()) {
7328 1 : return HCCL_SUCCESS;
7329 : }
7330 : // 仅 aicpu unfold 模式有 aicpu 端 resMap_/linkRes_ 需要清理;host 模式下没有 binHandle_
7331 5 : if (binHandle_ == nullptr) {
7332 2 : HCCL_DEBUG("[AicpuKfcClearOpResLaunch] binHandle_ null (host-mode communicator), skip; tagCount[%zu]",
7333 : tags.size());
7334 2 : return HCCL_SUCCESS;
7335 : }
7336 3 : if (opStream_.ptr() == nullptr) {
7337 1 : HCCL_WARNING("[AicpuKfcClearOpResLaunch] opStream_ null, skip aicpu cleanup; tagCount[%zu]", tags.size());
7338 1 : return HCCL_SUCCESS;
7339 : }
7340 : // host args 通道有 size 上限,大 payload 走 args/tiling 会被拒绝。沿用 RunAicpuKfcResInit 模式:HBM buffer 持载 payload
7341 2 : if (!aicpuCleanupBuf_) {
7342 0 : CHK_RET(DeviceMem::alloc(aicpuCleanupBuf_, sizeof(HcclKfcClearOpResTilingData)));
7343 : }
7344 2 : if (!aicpuCleanupHostBuf_) {
7345 0 : aicpuCleanupHostBuf_.reset(new (std::nothrow) HcclKfcClearOpResTilingData());
7346 0 : CHK_SMART_PTR_NULL(aicpuCleanupHostBuf_);
7347 : }
7348 2 : HcclKfcClearOpResTilingData &payload = *aicpuCleanupHostBuf_;
7349 :
7350 : // 必须与 aicpu_kfc_def.h 中 KFCResInitTask 布局一致,aicpu 端按此解包
7351 : struct KFCResInitTask { u64 context; bool isCustom; };
7352 2 : KFCResInitTask initTask = { reinterpret_cast<u64>(aicpuCleanupBuf_.ptr()), false };
7353 2 : const u16 timeOut = MAX_VALUE_U16;
7354 2 : const size_t groupCopyLen = std::min(identifier_.length() + 1, sizeof(payload.group));
7355 2 : size_t totalBatches = 0;
7356 :
7357 : // 分批 launch:同 buffer 复用,每批最多 MAX_BATCH 个 tag;launch 后 sync 保证 aicpu 完成才覆盖 buffer 下一批
7358 2 : auto it = tags.begin();
7359 5 : while (it != tags.end()) {
7360 3 : payload.magic = HCCL_KFC_CLEAR_OP_RES_MAGIC;
7361 3 : CHK_SAFETY_FUNC_RET(memcpy_s(payload.group, sizeof(payload.group), identifier_.c_str(), groupCopyLen));
7362 3 : payload.group[sizeof(payload.group) - 1] = '\0';
7363 :
7364 3 : u32 idx = 0;
7365 10255 : while (it != tags.end() && idx < HCCL_KFC_CLEAR_OP_RES_MAX_BATCH) {
7366 10252 : const std::string &t = *it;
7367 10252 : const size_t tagCopyLen = std::min(t.length() + 1, sizeof(payload.tags[idx]));
7368 10252 : CHK_SAFETY_FUNC_RET(memcpy_s(payload.tags[idx], sizeof(payload.tags[idx]), t.c_str(), tagCopyLen));
7369 10252 : payload.tags[idx][sizeof(payload.tags[idx]) - 1] = '\0';
7370 10252 : ++idx;
7371 10252 : ++it;
7372 : }
7373 3 : payload.tagCount = idx;
7374 :
7375 3 : CHK_RET(hrtMemSyncCopy(aicpuCleanupBuf_.ptr(), sizeof(payload), reinterpret_cast<void *>(&payload),
7376 : sizeof(payload), HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
7377 :
7378 3 : HcclResult ret = AicpuAclKernelLaunchV2(opStream_.ptr(), reinterpret_cast<void *>(&initTask),
7379 3 : sizeof(initTask), binHandle_, "RunAicpuKfcClearOpRes", true, timeOut, nullptr, 0, identifier_);
7380 3 : if (ret != HCCL_SUCCESS) {
7381 0 : HCCL_ERROR("[AicpuKfcClearOpResLaunch] launch fail, group[%s] batch[%zu] tagCount[%u] ret[%d]",
7382 : identifier_.c_str(), totalBatches, idx, ret);
7383 0 : return ret;
7384 : }
7385 3 : CHK_RET(hcclStreamSynchronize(opStream_.ptr(), commConfig_.GetConfigExecTimeOut()));
7386 3 : ++totalBatches;
7387 : }
7388 :
7389 2 : HCCL_INFO("[AicpuKfcClearOpResLaunch] dispatched aicpu cleanup, group[%s] totalTags[%zu] batches[%zu]",
7390 : identifier_.c_str(), tags.size(), totalBatches);
7391 2 : return HCCL_SUCCESS;
7392 : }
7393 :
7394 8 : HcclResult HcclCommunicator::AicpuInitOpTilingDataAicpuCache(const OpParam &opParam, const HcclCMDType &opType, struct OpTilingData *opTilingData)
7395 : {
7396 8 : opTilingData->aicpuCacheEnable = opParam.aicpuCacheEnable;
7397 : // 开启aicpu cache, 且原来是图模式建链但强制走单算子模式展开
7398 : // 开启aicpu cache,isCapture为true,且是图模式,证明选择了aclgraph零拷贝算法,需要强制刷新cache
7399 21 : if (opParam.aicpuCacheEnable != 0 &&
7400 9 : GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB &&
7401 1 : ((IsForceAicpuOpBaseMode(opParam, opType) && !opParam.isZeroCopy) || opParam.isCapture)) {
7402 : // 环境变量传入的aicpuCacheEnable一定 < 10
7403 1 : constexpr uint8_t FORCE_OP_BASE_DELTA = 10;
7404 1 : CHK_PRT_RET(opParam.aicpuCacheEnable >= FORCE_OP_BASE_DELTA,
7405 : HCCL_ERROR("[HcclCommunicator][AicpuInitOpTilingDataBuf] enforce opbase mode: opParam.aicpuCacheEnable >= %u",
7406 : opParam.aicpuCacheEnable, FORCE_OP_BASE_DELTA),
7407 : HCCL_E_INTERNAL);
7408 :
7409 : // 1 -> 11: 开启aicpu cache且存在强制单算子模式转换
7410 1 : opTilingData->aicpuCacheEnable += FORCE_OP_BASE_DELTA;
7411 1 : HCCL_WARNING("[HcclCommunicator][AicpuInitOpTilingDataBuf] enforce opbase mode: opParam.aicpuCacheEnable[%u]"\
7412 : "opTilingData->aicpuCacheEnable[%u]", opParam.aicpuCacheEnable, opTilingData->aicpuCacheEnable);
7413 :
7414 : // 注意: 开启aicpu cache且存在强制单算子模式转换, 传入device的aicpuCacheEnable一定 > 10
7415 1 : CHK_PRT_RET(opTilingData->aicpuCacheEnable <= FORCE_OP_BASE_DELTA,
7416 : HCCL_ERROR("[HcclCommunicator][AicpuInitOpTilingDataBuf] enforce opbase mode: opTilingData->aicpuCacheEnable[%u] <= %u",
7417 : opTilingData->aicpuCacheEnable, FORCE_OP_BASE_DELTA),
7418 : HCCL_E_INTERNAL);
7419 : }
7420 :
7421 8 : return HCCL_SUCCESS;
7422 : }
7423 :
7424 1 : HcclResult HcclCommunicator::AicpuInitOpTilingDataBuf(const OpParam &opParam, const HcclCMDType &opType,
7425 : const std::string &kernelName, const AicpuOpTiling opTilingInfo, u64 dynamicDataSize)
7426 : {
7427 1 : u32 opTilingDataSize = sizeof(struct OpTilingData) + dynamicDataSize;
7428 :
7429 1 : if (opTilingDataBuf_.ptr() == nullptr) {
7430 1 : opTilingDataBuf_ = HostMem::alloc(TILINGDATA_BUF_SIZE);
7431 1 : CHK_PRT_RET(opTilingDataBuf_.ptr() == nullptr,
7432 : HCCL_ERROR("[HcclCommunicator][AicpuInitOpTilingDataBuf] Alloc opTilingDataBuf failed!"),
7433 : HCCL_E_INTERNAL);
7434 : }
7435 :
7436 1 : if (opTilingDataBuf_.ptr() != nullptr && opTilingDataSize > opTilingDataBuf_.size()) {
7437 0 : opTilingDataBuf_.free();
7438 0 : opTilingDataBuf_ = HostMem::alloc(opTilingDataSize);
7439 0 : CHK_PRT_RET(opTilingDataBuf_.ptr() == nullptr,
7440 : HCCL_ERROR("[HcclCommunicator][AicpuInitOpTilingDataBuf] in create opTilingDataBuf len[%llu] failed!",
7441 : opTilingDataSize),
7442 : HCCL_E_INTERNAL);
7443 : }
7444 :
7445 : // 填充固定内容
7446 1 : HostMem opTilingDataMem = opTilingDataBuf_.range(0, opTilingDataSize);
7447 1 : struct OpTilingData *opTilingData = static_cast<struct OpTilingData *>(opTilingDataMem.ptr());
7448 1 : u32 algTypeTranfer = (static_cast<u32>(opTilingInfo.algType.algoLevel2) << (HCCL_LEVEL_ALGO_WIDTH + HCCL_LEVEL_ALGO_WIDTH)) +
7449 1 : (static_cast<u32>(opTilingInfo.algType.algoLevel1) << HCCL_LEVEL_ALGO_WIDTH) +
7450 1 : static_cast<u32>(opTilingInfo.algType.algoLevel0);
7451 1 : opTilingData->algType = static_cast<u64>(algTypeTranfer);
7452 1 : opTilingData->floatOverflowMode = opTilingInfo.floatOverflowMode;
7453 1 : opTilingData->dumpDebug = opTilingInfo.dumpDebug;
7454 1 : CHK_RET(AicpuInitOpTilingDataFromOpParam(opParam, opType, opTilingData));
7455 1 : opTilingData->length = dynamicDataSize;
7456 1 : opTilingData->customDataLength = 0;
7457 1 : opTilingData->index = UpdateOpIndex(opParam);
7458 1 : opTilingData->debugMode = 0;
7459 1 : opTilingData->isZeroCopy = opParam.isZeroCopy;
7460 1 : opTilingData->isCapture = opParam.isCapture;
7461 1 : opTilingData->orderLaunchMode = GetOrderLaunchMode(opParam.isCapture);
7462 1 : opTilingData->isSymmetricMemory = opParam.supportSymmetricMemory;
7463 1 : opTilingData->needIncreLink = opParam.needIncreLink;
7464 : // 有没有存在对应的Notify
7465 1 : CHK_RET(InitAndCheckAicpuOrderNotify(opTilingData->orderLaunchMode));
7466 1 : CHK_RET(BuildHierarchicalAlgOption(opTilingData->ahcConfInfo));
7467 1 : CHK_RET(AicpuInitOpTilingDataAicpuCache(opParam, opType, opTilingData));
7468 :
7469 : // 填充动态内容
7470 1 : HostMem dynamicDataMem = opTilingDataBuf_.range(sizeof(struct OpTilingData), dynamicDataSize);
7471 1 : CHK_PTR_NULL(dynamicDataMem.ptr());
7472 1 : if (opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV) {
7473 : struct OpTilingBatchSendRecvDataDes *batchSendRecvDataPtr =
7474 1 : reinterpret_cast<struct OpTilingBatchSendRecvDataDes *>(dynamicDataMem.ptr());
7475 1 : batchSendRecvDataPtr->itemNum = opParam.BatchSendRecvDataDes.itemNum;
7476 2 : for (u32 i = 0; i < opParam.BatchSendRecvDataDes.itemNum; i++) {
7477 1 : CHK_PTR_NULL(opParam.BatchSendRecvDataDes.sendRecvItemsPtr + i);
7478 1 : batchSendRecvDataPtr->batchSendRecvItem[i] = *(opParam.BatchSendRecvDataDes.sendRecvItemsPtr + i);
7479 : }
7480 :
7481 1 : u8 *isDirectRemoteRankPtr = reinterpret_cast<u8*>(batchSendRecvDataPtr->batchSendRecvItem + opParam.BatchSendRecvDataDes.itemNum);
7482 3 : for (u32 i = 0; i < userRankSize_; i++) {
7483 2 : CHK_PTR_NULL(isDirectRemoteRankPtr + i);
7484 2 : isDirectRemoteRankPtr[i] = *(opParam.BatchSendRecvDataDes.isDirectRemoteRank + i);
7485 : }
7486 0 : } else if (opType == HcclCMDType::HCCL_CMD_ALLTOALL) {
7487 0 : CHK_RET(SetDynamicTilingDataAlltoall(opParam, dynamicDataMem));
7488 0 : } else if (opType == HcclCMDType::HCCL_CMD_ALLTOALLV) {
7489 0 : CHK_RET(SetDynamicTilingDataAlltoallv(opParam, dynamicDataMem, opTilingInfo.algName));
7490 0 : } else if (opType == HcclCMDType::HCCL_CMD_ALLTOALLVC) {
7491 0 : CHK_RET(SetDynamicTilingDataAlltoallvc(opParam, dynamicDataMem));
7492 0 : } else if (opType == HcclCMDType::HCCL_CMD_ALLGATHER_V || opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V) {
7493 0 : CHK_RET(SetDynamicTilingDataV(opParam, dynamicDataMem));
7494 0 : } else {
7495 0 : struct OpTilingDataDes *opDataDesPtr = reinterpret_cast<struct OpTilingDataDes *>(dynamicDataMem.ptr());
7496 0 : opDataDesPtr->count = opParam.DataDes.count;
7497 0 : opDataDesPtr->dataType = static_cast<u8>(opParam.DataDes.dataType);
7498 : }
7499 :
7500 1 : HCCL_INFO("[HcclCommunicator][AicpuInitOpTilingDataBuf]algType[%lu]", opTilingData->algType);
7501 1 : CHK_SAFETY_FUNC_RET(memcpy_s(opTilingData->algName, sizeof(opTilingData->algName), opTilingInfo.algName.c_str(),
7502 : opTilingInfo.algName.length() + 1));
7503 1 : CHK_SAFETY_FUNC_RET(memcpy_s(opTilingData->newTag, sizeof(opTilingData->newTag),
7504 : opTilingInfo.newTag.c_str(), opTilingInfo.newTag.length() + 1));
7505 1 : CHK_SAFETY_FUNC_RET(memcpy_s(opTilingData->tag, sizeof(opTilingData->tag), opParam.tag.c_str(),
7506 : opParam.tag.length() + 1));
7507 1 : return HCCL_SUCCESS;
7508 1 : }
7509 :
7510 0 : u8 HcclCommunicator::GetOrderLaunchMode (bool isCapture)
7511 : {
7512 0 : bool isSupportHcomAttachedStream = !(attachedStreams_.empty() || attachedStreams_[0].ptr() == nullptr); // true 表示图模式下成功申请附属从流
7513 0 : u8 orderLaunchMode = 0;
7514 0 : HcclWorkflowMode mode = GetWorkflowMode();
7515 0 : if (isCapture) {
7516 0 : orderLaunchMode = static_cast<u8>(AicpuNotifyMode::ACLGRAPH_MODE);
7517 0 : } else if (mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
7518 0 : orderLaunchMode = static_cast<u8>(AicpuNotifyMode::OPBASE_MODE);
7519 0 : } else if (mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB && isSupportHcomAttachedStream) {
7520 0 : orderLaunchMode = static_cast<u8>(AicpuNotifyMode::HCOM_MODE);
7521 : } else {
7522 0 : orderLaunchMode = AICPU_ORDERLAUNCH_INVALID_HCOM_MODE;
7523 : }
7524 :
7525 0 : return orderLaunchMode;
7526 : }
7527 :
7528 0 : HcclResult HcclCommunicator::InitAndCheckAicpuOrderNotify(u8 &orderLaunchMode)
7529 : {
7530 0 : if (orderLaunchMode == AICPU_ORDERLAUNCH_INVALID_HCOM_MODE) {
7531 0 : HCCL_INFO("[HcclCommunicator][InitAndCheckAicpuOrderNotify] orderLaunchMode is invalid in hcom "
7532 : "for there is no attached stream included in this operator!");
7533 0 : return HCCL_SUCCESS;
7534 : }
7535 : u32 idx0;
7536 : u32 idx1;
7537 0 : if (orderLaunchMode == 0) {
7538 0 : idx0 = static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_OPBASE_0);
7539 0 : idx1 = static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_OPBASE_1);
7540 0 : } else if (orderLaunchMode == 1) {
7541 0 : idx0 = static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_ACLGRAPH_0);
7542 0 : idx1 = static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_ACLGRAPH_1);
7543 : } else {
7544 0 : idx0 = static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_HCOM_0);
7545 0 : idx1 = static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_HCOM_1);
7546 : }
7547 :
7548 0 : if (localAiCpuOpNotify_[idx0] != nullptr) {
7549 0 : HCCL_INFO("[%s], the orderNotify of orderLaunchMode [%u] is available", __func__, orderLaunchMode);
7550 0 : return HCCL_SUCCESS;
7551 : }
7552 : HcclSignalInfo orderSignalInfo0;
7553 0 : HcclResult ret = CreateAndGetAiCpuNotify(localAiCpuOpNotify_[idx0],
7554 : orderSignalInfo0);
7555 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
7556 : HCCL_ERROR("[HcclCommunicator][InitAndCheckAicpuOrderNotify]get aicpu notify [%u] errorCode[%u]", idx0,
7557 : HCCL_ERROR_CODE(ret)), ret);
7558 :
7559 : // 按序下发(aicpu控制流 record host控制流) 使用的notify信息
7560 : HcclSignalInfo orderSignalInfo1;
7561 0 : ret = CreateAndGetAiCpuNotify(localAiCpuOpNotify_[idx1], orderSignalInfo1);
7562 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
7563 : HCCL_ERROR("[HcclCommunicator][InitAndCheckAicpuOrderNotify]get aicpu notify [%u] errorCode[%u]", idx1,
7564 : HCCL_ERROR_CODE(ret)), ret);
7565 0 : HCCL_INFO("[HcclCommunicator][InitAndCheckAicpuOrderNotify] ORDER INDEX 0: resId[%u], ORDER INDEX 1: resId[%u]",
7566 : orderSignalInfo0.resId, orderSignalInfo1.resId);
7567 :
7568 0 : CHK_RET(hrtMemSyncCopy(
7569 : static_cast<char*>(aicpuOrderNotifyAddr_.ptr()) + (sizeof(HcclSignalInfo) * orderLaunchMode),
7570 : sizeof(HcclSignalInfo), &orderSignalInfo1, sizeof(HcclSignalInfo),
7571 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
7572 :
7573 0 : return HCCL_SUCCESS;
7574 : }
7575 :
7576 2 : HcclResult HcclCommunicator::AicpuKfcTilingDataLaunchIn(const OpParam &opParam, const DeviceMem &deviceContext,
7577 : const std::string &kernelName, const AicpuOpTiling opTilingInfo, u64 opTilingDataSize, bool isCustom)
7578 : {
7579 2 : HostMem opTilingDataMem = opTilingDataBuf_.range(0, opTilingDataSize);
7580 2 : CHK_RET(SetNormalMode(dispatcher_));
7581 0 : Stream &mainStream = const_cast<Stream &>(opParam.stream);
7582 0 : CHK_RET(LocalNotify::Post(mainStream, dispatcher_,
7583 : localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_0)], INVALID_VALUE_STAGE));
7584 :
7585 0 : Stream kfcOpStream;
7586 0 : HcclWorkflowMode mode = GetWorkflowMode();
7587 0 : bool isSupportHcomAttachedStream = !(attachedStreams_.empty() || attachedStreams_[0].ptr() == nullptr); // true 表示图模式下成功申请附属从流
7588 0 : if (opParam.isCapture || mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
7589 0 : kfcOpStream = opStream_;
7590 : } else {
7591 : // 如果是图模式,则尝试从附属从流中获取一下stream,如果能拿到则使用,否则退化
7592 0 : if (isSupportHcomAttachedStream) {
7593 0 : HCCL_INFO("[HcclCommunicator][AicpuKfcTilingDataLaunchIn] attachedStreams_ is valid in graph mode");
7594 0 : kfcOpStream = attachedStreams_[0];
7595 : } else {
7596 0 : HCCL_INFO("[HcclCommunicator][AicpuKfcTilingDataLaunchIn] attachedStreams_ is invalid in graph mode");
7597 0 : kfcOpStream = opParam.stream;
7598 : }
7599 : }
7600 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
7601 0 : std::string profName = GetCMDTypeEnumStr(opParam.opType);
7602 0 : if (profName == "Invalid HcclCMDType" || profName == "invalid") {
7603 0 : profName = "HcclOpAicpuKernel";
7604 : } else {
7605 0 : profName += "AicpuKernel";
7606 : }
7607 0 : s32 streamId = kfcOpStream.id();
7608 0 : auto getAicpuTaskExceptionCallBack = [this]() {
7609 0 : return this->GetAicpuTaskException();
7610 0 : };
7611 0 : RegisterGetAicpuTaskExceptionCallBack(streamId, deviceLogicId_, getAicpuTaskExceptionCallBack);
7612 0 : aicpuStreamIds_.insert(streamId);
7613 0 : if (streamId != opParam.stream.id()) {
7614 0 : RegisterGetAicpuTaskExceptionCallBack(opParam.stream.id(), deviceLogicId_, getAicpuTaskExceptionCallBack);
7615 0 : aicpuStreamIds_.insert(opParam.stream.id());
7616 : }
7617 :
7618 0 : HCCL_INFO("%s profName[%s] tag[%s] kfcOpStreamId[%d] mainStreamId[%u] kfcStreamId[%d] isCapture[%d] mode[%d] ",
7619 : __func__, profName.c_str(), opParam.tag.c_str(), streamId, opParam.stream.id(), opStream_.id(),
7620 : opParam.isCapture, mode);
7621 :
7622 0 : if (opParam.isCapture) { // 非主流下发时,acl graph场景,capture从流
7623 0 : u64 modelId = UINT64_MAX;
7624 0 : rtModel_t rtModel = nullptr;
7625 0 : bool isCapture = false;
7626 0 : CHK_RET(GetStreamCaptureInfo(opParam.stream.ptr(), rtModel, isCapture));
7627 0 : CHK_PTR_NULL(rtModel);
7628 0 : CHK_RET(AddStreamToModel(kfcOpStream.ptr(), rtModel));
7629 :
7630 0 : CHK_RET(GetModelId(rtModel, modelId));
7631 0 : HCCL_INFO("[HcclCommunicator][%s]tag[%s], add stream[%d] to modelId[%llu] success.",
7632 : __func__, opParam.tag.c_str(), streamId, modelId);
7633 : }
7634 :
7635 0 : u32 timeOut = (opResPara_.config.notifyWaitTime == 0) ? opResPara_.config.notifyWaitTime :
7636 0 : (opResPara_.config.notifyWaitTime + AICPU_H2D_TIMEOUT_INC);
7637 0 : OrderLaunch& orderLaunch = OrderLaunch::GetInstance(deviceLogicId_);
7638 0 : std::shared_ptr<LocalNotify> notify0;
7639 0 : std::shared_ptr<LocalNotify> notify1;
7640 0 : if (opParam.isCapture) {
7641 0 : notify0 = localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_ACLGRAPH_0)];
7642 0 : notify1 = localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_ACLGRAPH_1)];
7643 0 : HcclRtEvent event0 = localAicpuOpEvent_[static_cast<u32>(AicpuLocalEventIdx::ORDER_INDEX_ACLGRAPH_EVENT_0)];
7644 0 : CHK_RET(orderLaunch.AclgraphLaunchInOrderToOrderStream(
7645 : identifier_, kfcOpStream, notify0, notify1, timeOut, event0));
7646 0 : } else if (mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
7647 0 : notify0 = localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_OPBASE_0)];
7648 0 : notify1 = localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_OPBASE_1)];
7649 0 : CHK_RET(orderLaunch.OpbaseLaunchInOrder(identifier_, kfcOpStream, notify0, notify1, timeOut));
7650 0 : } else if (mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB && isSupportHcomAttachedStream) {
7651 0 : notify0 = localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_HCOM_0)];
7652 0 : notify1 = localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_HCOM_1)];
7653 0 : CHK_RET(orderLaunch.HcomLaunchInOrder(identifier_, kfcOpStream, graphId_, notify0,
7654 : notify1, timeOut));
7655 : }
7656 0 : CHK_RET(KernelLaunchChooseAicpuOrCustom(opParam.inputPtr, opParam.outputPtr, kfcOpStream.ptr(),
7657 : reinterpret_cast<u64>(deviceContext.ptr()), opTilingDataMem.ptr(), opTilingDataSize,
7658 : kernelName, mode, opParam.tag, isCustom));
7659 0 : if (opParam.isCapture) {
7660 0 : HcclRtEvent event1 = localAicpuOpEvent_[static_cast<u32>(AicpuLocalEventIdx::ORDER_INDEX_ACLGRAPH_EVENT_1)];
7661 0 : CHK_RET(orderLaunch.AclgraphLaunchInOrderToKernelStream(identifier_, kfcOpStream, event1));
7662 : }
7663 :
7664 0 : uint64_t endTime = hrtMsprofSysCycleTime();
7665 0 : s32 threadId = SalGetTid();
7666 0 : CHK_RET(ProfilingManagerPub::CallMsprofReportNodeInfo(beginTime, endTime, profName, threadId));
7667 0 : CHK_RET(LocalNotify::Wait(mainStream, dispatcher_,
7668 : localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_1)], INVALID_VALUE_STAGE, timeOut));
7669 0 : return HCCL_SUCCESS;
7670 2 : }
7671 :
7672 :
7673 0 : HcclResult HcclCommunicator::SetAttachedStream(u32 graphId, const std::vector<rtStream_t> &streams)
7674 : {
7675 0 : constexpr u32 GRAPH_ATTACHED_STREAM_INDEX = 0; // 图粒度的附属从流
7676 0 : constexpr u32 GROUP_ATTACHED_STREAM_INDEX = 1; // 通信域粒度的附属从流
7677 :
7678 : // 在图模式下,通信使用的附属从流可能不同,所以这里直接刷新所有
7679 0 : attachedStreams_.clear();
7680 :
7681 0 : bool isValid = !streams.empty() && (streams.size() > GROUP_ATTACHED_STREAM_INDEX) &&
7682 0 : streams[GRAPH_ATTACHED_STREAM_INDEX] != nullptr && streams[GROUP_ATTACHED_STREAM_INDEX] != nullptr;
7683 0 : if (!isValid) {
7684 0 : HCCL_ERROR("%s Invalid stream configuration, streams vector is null or invalid", __func__);
7685 0 : return HCCL_E_NOT_FOUND;
7686 : }
7687 :
7688 : // 向GE申请流的时候,图粒度的流排在第一个,所以在streams列表中,第一条流是图粒度的附属从流
7689 0 : s32 graphAttachedStreamId = 0;
7690 0 : OrderLaunch& orderLaunch = OrderLaunch::GetInstance(deviceLogicId_);
7691 0 : auto& graphStream = streams[GRAPH_ATTACHED_STREAM_INDEX];
7692 0 : CHK_RET(hrtGetStreamId(graphStream, graphAttachedStreamId));
7693 0 : orderLaunch.SetHcomStream(graphId, Stream(graphStream, false));
7694 0 : graphId_ = graphId;
7695 :
7696 : // 设置通信域粒度流
7697 0 : auto& groupStream = streams[GROUP_ATTACHED_STREAM_INDEX];
7698 0 : attachedStreams_.emplace_back(Stream(groupStream, false));
7699 :
7700 0 : HCCL_INFO("%s Streams configured graph[%u], graphAttachedStreamId[%d], group[%u],"
7701 : "groupStreamId[%u], graphId[%u], groupId[%s]", __func__,
7702 : GRAPH_ATTACHED_STREAM_INDEX, graphAttachedStreamId, GROUP_ATTACHED_STREAM_INDEX,
7703 : attachedStreams_.back().id(), graphId, identifier_.c_str());
7704 :
7705 0 : return HCCL_SUCCESS;
7706 : }
7707 :
7708 4 : HcclResult HcclCommunicator::AicpuKfcTilingDataLaunchExt(const OpParam &opParam, const HcclCMDType &opType,
7709 : const DeviceMem &deviceContext, const std::string &kernelName, const AicpuOpTiling opTilingInfo,
7710 : bool isCustom)
7711 : {
7712 4 : const u64 dataCount = opParam.GetDataCount(userRank_);
7713 4 : const HcclDataType dataType = opParam.GetDataType();
7714 4 : HCCL_DEBUG("AicpuKfcTilingDataLaunchExt count %llu dataType %s op %s opType %u retryEnable_ %d, "
7715 : "inPlaceSupportRetryStatus_ %d",
7716 : dataCount, GetDataTypeEnumStr(dataType).c_str(),
7717 : GetReduceOpEnumStr(opParam.reduceType).c_str(), opType, retryEnable_, inPlaceSupportRetryStatus_);
7718 :
7719 4 : bool postSyncEnable = false;
7720 4 : u32 severNum4PostSync = 4;
7721 4 : bool needPostSync = (superPodNum_ > 1 || serverNum_ >= severNum4PostSync) && postSyncEnable; // reduce/reduce scatter算子是否需要PostSync
7722 4 : if (opType == HcclCMDType::HCCL_CMD_ALLREDUCE &&
7723 1 : retryEnable_ && (inPlaceSupportRetryStatus_ == InplaceSupportRetryStatus::USER_LARGER_THAN_CCL) &&
7724 1 : (!opParam.isZeroCopy)) {
7725 1 : u32 itemNum = 2;
7726 3 : for (u32 i = 0; i < itemNum; i++) {
7727 2 : if (i == 0) {
7728 1 : isInplacePreSync_ = true;
7729 : } else {
7730 1 : isInplacePreSync_ = false;
7731 : }
7732 2 : HCCL_DEBUG("[AicpuKfcTilingDataLaunchExt][PreSync]The op with isInplacePreSync_[%d].",
7733 : isInplacePreSync_);
7734 2 : u64 dynamicDataSize = CalcOpTilingDynamicDataSize(opParam, opType, GetRankSize(), opTilingInfo.algName);
7735 2 : CHK_RET(AicpuInitOpTilingDataBuf(opParam, opType, kernelName, opTilingInfo, dynamicDataSize));
7736 2 : CHK_RET(AicpuKfcTilingDataLaunchIn(opParam, deviceContext, kernelName, opTilingInfo,
7737 : sizeof(struct OpTilingData) + dynamicDataSize, isCustom));
7738 2 : isInplacePreSync_ = false;
7739 : }
7740 4 : } else if (opType == HcclCMDType::HCCL_CMD_REDUCE && retryEnable_ && needPostSync && (!opParam.isZeroCopy)) {
7741 0 : isPostSync_ = true;
7742 0 : HCCL_DEBUG("[AicpuKfcTilingDataLaunchExt][PreSync]The op with isPostSync_[%d].",
7743 : isPostSync_);
7744 0 : u64 dynamicDataSize = CalcOpTilingDynamicDataSize(opParam, opType, GetRankSize(), opTilingInfo.algName);
7745 0 : CHK_RET(AicpuInitOpTilingDataBuf(opParam, opType, kernelName, opTilingInfo, dynamicDataSize));
7746 0 : CHK_RET(AicpuKfcTilingDataLaunchIn(opParam, deviceContext, kernelName, opTilingInfo,
7747 : sizeof(struct OpTilingData) + dynamicDataSize, isCustom));
7748 0 : isPostSync_ = false;
7749 3 : } else if (retryEnable_ && opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER && (!opParam.isZeroCopy)) {
7750 1 : if (inPlaceSupportRetryStatus_ == InplaceSupportRetryStatus::USER_LARGER_THAN_CCL) {
7751 1 : isInplacePreSync_ = true;
7752 1 : HCCL_DEBUG("[AicpuKfcTilingDataLaunchExt][PreSync]The op with isInplacePreSync_[%d].",
7753 : isInplacePreSync_);
7754 1 : u64 dynamicDataSize = CalcOpTilingDynamicDataSize(opParam, opType, GetRankSize(), opTilingInfo.algName);
7755 1 : CHK_RET(AicpuInitOpTilingDataBuf(opParam, opType, kernelName, opTilingInfo, dynamicDataSize));
7756 1 : CHK_RET(AicpuKfcTilingDataLaunchIn(opParam, deviceContext, kernelName, opTilingInfo,
7757 : sizeof(struct OpTilingData) + dynamicDataSize, isCustom));
7758 1 : isInplacePreSync_ = false;
7759 : }
7760 1 : isInplacePreSync_ = false;
7761 1 : if (needPostSync) {
7762 0 : isPostSync_ = true;
7763 : }
7764 1 : HCCL_DEBUG("[AicpuKfcTilingDataLaunchExt][PreSync]The op with "
7765 : "isInplacePreSync_[%d], isPostSync_[%d].",
7766 : isInplacePreSync_, isPostSync_);
7767 1 : u64 dynamicDataSize = CalcOpTilingDynamicDataSize(opParam, opType, GetRankSize(), opTilingInfo.algName);
7768 1 : CHK_RET(AicpuInitOpTilingDataBuf(opParam, opType, kernelName, opTilingInfo, dynamicDataSize));
7769 1 : CHK_RET(AicpuKfcTilingDataLaunchIn(opParam, deviceContext, kernelName, opTilingInfo,
7770 : sizeof(struct OpTilingData) + dynamicDataSize, isCustom));
7771 1 : isPostSync_ = false;
7772 3 : } else if (retryEnable_ &&
7773 2 : (opType == HcclCMDType::HCCL_CMD_ALLTOALL ||
7774 2 : opType == HcclCMDType::HCCL_CMD_ALLTOALLV ||
7775 2 : opType == HcclCMDType::HCCL_CMD_ALLTOALLVC) && (!opParam.isZeroCopy)) {
7776 1 : isPostSync_ = postSyncEnable;
7777 1 : HCCL_DEBUG("[AicpuKfcTilingDataLaunchExt][PreSync]The op with "
7778 : "isInplacePreSync_[%d], isPostSync_[%d].",
7779 : isInplacePreSync_, isPostSync_);
7780 1 : u64 dynamicDataSize = CalcOpTilingDynamicDataSize(opParam, opType, GetRankSize(), opTilingInfo.algName);
7781 1 : CHK_RET(AicpuInitOpTilingDataBuf(opParam, opType, kernelName, opTilingInfo, dynamicDataSize));
7782 1 : CHK_RET(AicpuKfcTilingDataLaunchIn(opParam, deviceContext, kernelName, opTilingInfo,
7783 : sizeof(struct OpTilingData) + dynamicDataSize, isCustom));
7784 1 : isPostSync_ = false;
7785 1 : } else {
7786 1 : u64 dynamicDataSize = CalcOpTilingDynamicDataSize(opParam, opType, GetRankSize(), opTilingInfo.algName);
7787 1 : HCCL_DEBUG("[AicpuKfcTilingDataLaunchExt]dynamicDataSize[%u]", dynamicDataSize);
7788 1 : CHK_RET(AicpuInitOpTilingDataBuf(opParam, opType, kernelName, opTilingInfo, dynamicDataSize));
7789 1 : CHK_RET(AicpuKfcTilingDataLaunchIn(opParam, deviceContext, kernelName, opTilingInfo,
7790 : sizeof(struct OpTilingData) + dynamicDataSize, isCustom));
7791 : }
7792 :
7793 4 : return HCCL_SUCCESS;
7794 : }
7795 :
7796 0 : HcclResult HcclCommunicator::AicpuUnfoldKernelLaunch(void *inputPtr, void *outputPtr, const rtStream_t stm, u64 addr,
7797 : void *tilingDataPtr, u32 tilingDataSize, const std::string &kernelName, HcclWorkflowMode mode, const std::string &tag)
7798 : {
7799 : struct ApiParamDef
7800 : {
7801 : uint64_t x1; // 算子sendbuffer地址
7802 : uint64_t y = 0;
7803 : uint64_t gatherOut; // 算子recvbuffer地址
7804 : uint64_t context; // 通信资源准备的地址
7805 : uint64_t workspace; // 消息区地址
7806 : };
7807 :
7808 0 : struct ApiParamDef apiParam;
7809 0 : apiParam.x1 = reinterpret_cast<uint64_t>(inputPtr);
7810 0 : apiParam.gatherOut = reinterpret_cast<uint64_t>(outputPtr);
7811 0 : apiParam.context = addr;
7812 0 : apiParam.workspace = reinterpret_cast<uint64_t>(workSpace_.ptr());
7813 0 : u16 timeOut = 0;
7814 0 : if (opResPara_.config.notifyWaitTime == 0) {
7815 0 : timeOut = opResPara_.config.notifyWaitTime;
7816 0 : } else if (opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC >= MAX_VALUE_U16) {
7817 0 : timeOut = MAX_VALUE_U16;
7818 : } else {
7819 0 : timeOut = opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC;
7820 : }
7821 0 : CHK_PRT(AicpuAclKernelLaunch(stm, reinterpret_cast<void *>(&apiParam), sizeof(apiParam),
7822 : binHandle_, kernelName, false, timeOut, tilingDataPtr, tilingDataSize));
7823 0 : HCCL_INFO("[HcclCommunicator][AicpuUnfoldKernelLaunch] exec succ.");
7824 0 : return HCCL_SUCCESS;
7825 : }
7826 :
7827 0 : HcclResult HcclCommunicator::AicpuUnfoldKernelLaunchV2(void *inputPtr, void *outputPtr, const rtStream_t stm,
7828 : u64 addr, void *tilingDataPtr, u32 tilingDataSize, const std::string &kernelName, HcclWorkflowMode mode,
7829 : const std::string &tag, bool isCustom)
7830 : {
7831 0 : u64 context = addr;
7832 0 : HCCL_INFO("[HcclCommunicator]context[%p] tilingDataPtr[%p] tilingData[%p]", context,
7833 : tilingDataPtr, tilingDataSize);
7834 :
7835 0 : aclrtBinHandle binHandle = isCustom ? binCustomHandle_ : binHandle_;
7836 0 : if (binHandle == nullptr) {
7837 0 : HCCL_ERROR("[AicpuUnfoldKernelLaunchV2]isCustom[%d] binHandle is nullptr, please check.", isCustom);
7838 0 : return HCCL_E_NOT_SUPPORT;
7839 : }
7840 0 : u16 timeOut = 0;
7841 0 : if (opResPara_.config.notifyWaitTime == 0) {
7842 0 : timeOut = opResPara_.config.notifyWaitTime;
7843 0 : } else if (opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC >= MAX_VALUE_U16) {
7844 0 : timeOut = MAX_VALUE_U16;
7845 : } else {
7846 0 : timeOut = opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC;
7847 : }
7848 0 : HcclResult ret = AicpuAclKernelLaunchV2(stm, reinterpret_cast<void *>(&context), sizeof(context),
7849 0 : binHandle, kernelName, false, timeOut, tilingDataPtr, tilingDataSize, identifier_);
7850 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
7851 : HCCL_ERROR("[HcclCommunicator][AicpuUnfoldKernelLaunchV2]isCustom[%d] binHandle[%p]",
7852 : isCustom, binHandle), ret);
7853 0 : HCCL_INFO("[HcclCommunicator][AicpuUnfoldKernelLaunchV2] exec succ, isCustom[%d].", isCustom);
7854 0 : return HCCL_SUCCESS;
7855 : }
7856 :
7857 487 : HcclResult HcclCommunicator::InitCombinOpara()
7858 : {
7859 487 : if (combinOparaMem_ == nullptr) {
7860 488 : CHK_RET(AllocAndClearHostMem(sizeof(HcclCombinOpParam), combinOparaMem_));
7861 : }
7862 488 : CHK_PTR_NULL(combinOparaMem_);
7863 488 : HcclCombinOpParam *combinOparaPtr = reinterpret_cast<HcclCombinOpParam*>(combinOparaMem_->ptr());
7864 488 : CHK_PTR_NULL(combinOparaPtr);
7865 :
7866 488 : if (aiRMAInfoMem_ == nullptr) {
7867 487 : CHK_RET(AllocAndClearHostMem(sizeof(HcclAiRMAInfo), aiRMAInfoMem_));
7868 : }
7869 485 : if (rmaInfoMem_ == nullptr) {
7870 485 : CHK_RET(AllocAndClearHostMem(sizeof(HcclRMAInfo), rmaInfoMem_));
7871 : }
7872 488 : CHK_PTR_NULL(aiRMAInfoMem_);
7873 488 : CHK_PTR_NULL(aiRMAInfoMem_->ptr());
7874 488 : CHK_PTR_NULL(rmaInfoMem_);
7875 488 : CHK_PTR_NULL(rmaInfoMem_->ptr());
7876 :
7877 488 : CHK_SAFETY_FUNC_RET(memset_s(combinOparaPtr, sizeof(HcclCombinOpParam), 0, sizeof(HcclCombinOpParam)));
7878 :
7879 488 : combinOparaPtr->rankId = INVALID_UINT;
7880 488 : combinOparaPtr->signalInfo.aicpuNotify.rankId = INVALID_UINT;
7881 :
7882 31668 : for (u32 i = 0; i < sizeof(combinOparaPtr->signalInfo.noIpcNotifys) / sizeof(combinOparaPtr->signalInfo.noIpcNotifys[0]);
7883 : i++) {
7884 31180 : combinOparaPtr->signalInfo.noIpcNotifys[i].rankId = INVALID_UINT;
7885 : }
7886 :
7887 62866 : for (u32 i = 0; i < sizeof(combinOparaPtr->signalInfo.ipcNotifys) / sizeof(combinOparaPtr->signalInfo.ipcNotifys[0]);
7888 : i++) {
7889 62378 : combinOparaPtr->signalInfo.ipcNotifys[i].rankId = INVALID_UINT;
7890 : }
7891 :
7892 16104 : for (u32 i = 0; i < sizeof(combinOparaPtr->signalInfo.noIpcEvents) / sizeof(combinOparaPtr->signalInfo.noIpcEvents[0]);
7893 : i++) {
7894 15616 : combinOparaPtr->signalInfo.noIpcEvents[i].rankId = INVALID_UINT;
7895 : }
7896 488 : return HCCL_SUCCESS;
7897 : }
7898 :
7899 7 : bool HcclCommunicator::GetCommResource(const std::string &tag, void **commContext)
7900 : {
7901 7 : if (LIKELY(IsExistCommRes(tag))) {
7902 0 : *commContext = commContext_.ptr();
7903 0 : return true;
7904 : }
7905 7 : return false;
7906 : }
7907 :
7908 7 : bool HcclCommunicator::GetCommResource(void *&commContext)
7909 : {
7910 7 : commContext = opResDevicePara_.ptr();
7911 7 : return true;
7912 : }
7913 :
7914 0 : HcclResult HcclCommunicator::GetAicpuOpStreamNotify(HcclRtStream *opStream, u8 aicpuNotifyNum, void **aicpuNotify)
7915 : {
7916 0 : CHK_RET(GetAicpuOpStreamAndNotify(opStream, aicpuNotifyNum, aicpuNotify));
7917 0 : HCCL_INFO("[HcclCommunicator][GetAicpuOpStreamNotify]opStream %p aicpuNotify %p.", *opStream, *aicpuNotify);
7918 0 : return HCCL_SUCCESS;
7919 : }
7920 :
7921 0 : HcclResult HcclCommunicator::GetAicpuOpStreamAndNotify(HcclRtStream *opStream, u8 aicpuNotifyNum, void **aicpuNotify)
7922 : {
7923 0 : *opStream = opStream_.ptr();
7924 0 : if (localAiCpuNotifyRes_.size() < aicpuNotifyNum) {
7925 0 : for (u16 i = localAiCpuNotifyRes_.size(); i < aicpuNotifyNum; i++) {
7926 0 : std::shared_ptr<LocalNotify> localNotify = {nullptr};
7927 : HcclSignalInfo aicpuNotify;
7928 0 : CHK_RET(CreateAndGetAiCpuNotify(localNotify, aicpuNotify));
7929 0 : localAiCpuNotifyRes_.push_back(localNotify);
7930 0 : }
7931 : }
7932 :
7933 0 : for (u16 i = 0; i < aicpuNotifyNum; i++) {
7934 0 : *(aicpuNotify + i) = localAiCpuNotifyRes_[i]->ptr();
7935 : }
7936 0 : return HCCL_SUCCESS;
7937 : }
7938 :
7939 0 : HcclResult HcclCommunicator::SetAicpuNotifyInvalid()
7940 : {
7941 0 : CHK_PTR_NULL(combinOparaMem_);
7942 0 : HcclCombinOpParam *combinOparaPtr = reinterpret_cast<HcclCombinOpParam*>(combinOparaMem_->ptr());
7943 0 : CHK_PTR_NULL(combinOparaPtr);
7944 0 : combinOparaPtr->signalInfo.aicpuNotify.resId = INVALID_U64;
7945 0 : return HCCL_SUCCESS;
7946 : }
7947 :
7948 1 : HcclResult HcclCommunicator::ReplaceCommInfoByTag(const std::string &tag, std::unique_ptr<CommInfo> &commInfo)
7949 : {
7950 1 : std::unique_lock<std::mutex> replLock(commLock_);
7951 1 : tagCommInfo_.erase(tag);
7952 1 : tagCommInfo_.insert(std::pair<std::string, CommInfo>(tag, std::move(*commInfo)));
7953 1 : return HCCL_SUCCESS;
7954 1 : }
7955 :
7956 0 : HcclResult HcclCommunicator::CreateMutiStreamResFor310P(const std::string &tag, level1StreamInfo_t &streamInfo)
7957 : {
7958 0 : u32 rankSize = GetRankSize();
7959 : s32 pid;
7960 0 : if (SalGetBareTgid(&pid) != HCCL_SUCCESS) {
7961 0 : HCCL_DEBUG("get pid fail");
7962 : }
7963 0 : HCCL_INFO("[HcclCommunicator][CreateMutiStreamRes]tag[%s] ranksize[%u] comminfo ranksize[%u] "
7964 : "auxRingCommStreamsDev_ size[%u] ringDeviceSignalAux size[%u] ringDeviceSignal size[%u] "
7965 : "ringDeviceStreams size[%u]",
7966 : tag.c_str(), rankSize, tagCommInfo_[tag].commIntraServer->RankSize(),
7967 : auxRingCommStreamsDev_.size(), streamInfo.ringDeviceSignalAux.size(),
7968 : streamInfo.ringDeviceSignal.size(), streamInfo.ringDeviceStreams.size());
7969 0 : if (auxRingCommStreamsDev_.empty() || auxRingCommStreamsDev_.size() < rankSize) {
7970 0 : auxRingCommStreamsDev_.resize(rankSize);
7971 0 : u32 resNum = rankSize - 1;
7972 0 : streamInfo.ringDeviceSignalAux.resize(resNum);
7973 0 : streamInfo.ringDeviceSignal.resize(resNum);
7974 0 : for (u32 ringIndex = 0; ringIndex < rankSize; ringIndex++) {
7975 0 : auxRingCommStreamsDev_[ringIndex] = Stream(StreamType::STREAM_TYPE_DEVICE);
7976 : // 给device侧申请的流不需要setmode,否则rts会捕获流成员Flags为1024的异常
7977 : }
7978 0 : for (auto &signal : streamInfo.ringDeviceSignal) {
7979 0 : signal = nullptr;
7980 : }
7981 0 : for (auto &signal : streamInfo.ringDeviceSignalAux) {
7982 0 : signal = nullptr;
7983 : }
7984 :
7985 0 : u32 notifyNum = resNum * 2; // 2:Signal + SignalAux
7986 0 : std::vector<std::shared_ptr<LocalNotify>> notifys(notifyNum, nullptr);
7987 0 : CHK_RET(queueNotifyManager_->Alloc(tag, notifyNum, notifys, NotifyLoadType::DEVICE_NOTIFY));
7988 0 : for (u32 i = 0; i < resNum; i++) {
7989 0 : streamInfo.ringDeviceSignal[i] = notifys[2 * i];
7990 0 : streamInfo.ringDeviceSignalAux[i] = notifys[2 * i + 1];
7991 : }
7992 0 : }
7993 :
7994 0 : if (streamInfo.ringDeviceStreams.empty() || streamInfo.ringDeviceStreams.size() < rankSize) {
7995 0 : streamInfo.ringDeviceStreams.resize(rankSize);
7996 0 : for (u32 ringIndex = 0; ringIndex < rankSize; ringIndex++) {
7997 0 : streamInfo.ringDeviceStreams[ringIndex] = auxRingCommStreamsDev_[ringIndex];
7998 0 : CHK_SMART_PTR_NULL(streamInfo.ringDeviceStreams[ringIndex]);
7999 : }
8000 : }
8001 0 : return HCCL_SUCCESS;
8002 : }
8003 :
8004 1 : HcclResult HcclCommunicator::CreateCommAndStreamRes(const std::string &tag, Stream &stream)
8005 : {
8006 1 : CHK_SMART_PTR_NULL(implAlg_);
8007 1 : void *commInputPtr = nullptr;
8008 1 : void *commOutputPtr = nullptr;
8009 : u64 commInputSize, commOutputSize;
8010 :
8011 1 : HcclResult ret = CreateCommCCLbuffer();
8012 1 : CHK_PRT_RET(ret != HCCL_SUCCESS,
8013 : HCCL_ERROR("[HcclImplBase][CreateCommAndStreamRes]errNo[0x%016llx],create cclbuff failed",
8014 : HCCL_ERROR_CODE(ret)),
8015 : ret);
8016 :
8017 1 : if (isA2MC2MultiServer_) {
8018 : // 该场景下ccl buffer有一块区域在上层会被用作flag区,因此需要先清理一下
8019 0 : CHK_RET(cclBufferManager_.CleanCCLbuffer());
8020 : }
8021 :
8022 1 : CHK_RET(cclBufferManager_.GetInCCLbuffer(commInputPtr, commInputSize));
8023 1 : CHK_RET(cclBufferManager_.GetOutCCLbuffer(commOutputPtr, commOutputSize));
8024 1 : DeviceMem expMem = cclBufferManager_.GetCommExpBuffer();
8025 1 : DeviceMem inputMem = DeviceMem::create(commInputPtr, commInputSize);
8026 1 : DeviceMem outputMem = DeviceMem::create(commOutputPtr, commOutputSize);
8027 1 : AlgType algType;
8028 1 : AlgType algTypeTmp;
8029 :
8030 1 : CHK_RET(GetAlgType(algType, HcclCMDType::HCCL_CMD_ALL));
8031 1 : algTypeTmp = algType;
8032 :
8033 1 : CHK_RET(notifyPool_->RegisterOp(tag));
8034 :
8035 : // 根据tag创建comm和流资源
8036 1 : if (!(IsExistCommRes(tag))) {
8037 1 : std::unique_ptr<CommInfo> commInfo = nullptr;
8038 1 : std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
8039 1 : std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
8040 1 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_,
8041 1 : isUseRankPort_);
8042 1 : HcclResult ret = implAlg_->CreateComm(tag, inputMem, outputMem, algType, commInfo,
8043 : INVALID_VALUE_RANKID, false, true);
8044 :
8045 1 : CHK_PRT_RET(ret != HCCL_SUCCESS,
8046 : HCCL_ERROR(
8047 : "[HcclCommunicator][CreateCommAndStreamRes]errNo[0x%016llx]tag[%s],comm resource create comm failed",
8048 : HCCL_ERROR_CODE(ret),
8049 : tag.c_str()),
8050 : ret);
8051 :
8052 1 : CHK_RET(ReplaceCommInfoByTag(tag, commInfo));
8053 1 : if (isA2MC2MultiServer_ && isA2MC2IntraHie_) {
8054 0 : std::string hieSuffix = "_HIE";
8055 0 : size_t pos = tag.find(hieSuffix);
8056 0 : std::string oldtag = tag;
8057 0 : oldtag.erase(pos, hieSuffix.size());
8058 0 : CHK_RET(ReplaceCommInfoByTag(oldtag, commInfo));
8059 0 : }
8060 1 : }
8061 :
8062 1 : if (!(IsExistMutiStreamRes(tag))) {
8063 1 : level1StreamInfo_t streamInfo;
8064 1 : std::unique_lock<std::mutex> mutiStreamLock(tagStreamInfoLock_);
8065 : // 2p场景下,mc2当前algType为518,streamInfo.ringNum走默认流程值为1导致资源申请不足,910_93 mc2固定在节点内默认用mesh
8066 1 : constexpr u32 RANK_SIZE_TWO = 2;
8067 1 : if ((GetRankSize() == RANK_SIZE_TWO && !isA2MC2MultiServer_) || (deviceType_ == DevType::DEV_TYPE_910_93)) {
8068 0 : algTypeTmp.algoLevel0 = AlgTypeLevel0::ALG_LEVEL0_NP_MESH;
8069 0 : algTypeTmp.algoLevel1 = AlgTypeLevel1::ALG_LEVEL1_RING;
8070 : }
8071 1 : HcclResult ret = HCCL_SUCCESS;
8072 1 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
8073 0 : ret = CreateMutiStreamResFor310P(tag, streamInfo);
8074 : } else {
8075 1 : ret = implAlg_->CreateMutiStreamRes(tag, stream, streamInfo, algTypeTmp, true);
8076 : }
8077 1 : CHK_PRT_RET(ret != HCCL_SUCCESS,
8078 : HCCL_ERROR("[HcclCommunicator][CreateCommAndStreamRes]errNo[0x%016llx]tag[%s],comm resource create stream "
8079 : "resource",
8080 : HCCL_ERROR_CODE(ret),
8081 : tag.c_str()),
8082 : ret);
8083 1 : tagStreamInfo_.insert(std::pair<std::string, Level1StreamInfo>(tag, std::move(streamInfo)));
8084 1 : opRetryStreamPtr_->insert(std::make_pair(tag, tagStreamInfo_[tag].ringDeviceStreams));
8085 1 : mutiStreamLock.unlock();
8086 1 : }
8087 :
8088 1 : HCCL_INFO("resource creation (AllReduce) success, tag[%s]", tag.c_str());
8089 1 : CHK_RET(notifyPool_->UnregisterOp(tag));
8090 1 : if (commPortConfig_.devPortSwitchOn) {
8091 0 : std::vector<u32> &nicPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
8092 0 : std::vector<u32> &vnicPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
8093 0 : Heartbeat::GetInstance(deviceLogicId_).SetRankPortInfo(isUseRankPort_, nicPorts, vnicPorts,
8094 0 : commPortConfig_.devPortSwitchOn);
8095 : }
8096 1 : CHK_RET(RegisterToHeartBeat());
8097 :
8098 1 : CommBase *comm = nullptr;
8099 1 : CHK_RET(GetComm(tag, &comm));
8100 1 : if (comm == nullptr) {
8101 0 : HCCL_ERROR("comm get err, comm %p", comm);
8102 0 : return HCCL_E_PTR;
8103 : }
8104 1 : CHK_RET(SetCommResource(commInputSize, commInputPtr, commOutputPtr, expMem.ptr(),
8105 : comm, tagStreamInfo_[tag], stream));
8106 :
8107 1 : return HCCL_SUCCESS;
8108 1 : }
8109 :
8110 1 : HcclResult HcclCommunicator::GetComm(const std::string &tag, CommBase **comm)
8111 : {
8112 1 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
8113 0 : *comm = tagCommInfo_[tag].commIntraServer.get();
8114 1 : } else if (isA2MC2MultiServer_) {
8115 : // 使用打平RDMA Mesh子通信域
8116 0 : *comm = tagCommInfo_[tag].commLevel1Rdma[0].get();
8117 : } else {
8118 1 : *comm = tagCommInfo_[tag].commLevel0[0].get();
8119 : }
8120 1 : return HCCL_SUCCESS;
8121 : }
8122 :
8123 1 : HcclResult HcclCommunicator::SetCommResource(u64 commBufferSize, void *commInPtr, void *commOutPtr, void *commExpPtr,
8124 : CommBase *comm, level1StreamInfo_t &streamInfo, Stream &stream)
8125 : {
8126 1 : CHK_PTR_NULL(combinOparaMem_);
8127 1 : HcclCombinOpParam *combinOparaPtr = reinterpret_cast<HcclCombinOpParam*>(combinOparaMem_->ptr());
8128 1 : CHK_PTR_NULL(combinOparaPtr);
8129 :
8130 1 : u32 rankSize = comm->RankSize();
8131 1 : u32 curRankId = comm->Rank();
8132 1 : u32 usrRankId = comm->UserRank();
8133 1 : combinOparaPtr->rankId = curRankId;
8134 1 : combinOparaPtr->signalInfo.aicpuNotify.rankId = curRankId;
8135 1 : combinOparaPtr->rankNum = rankSize;
8136 1 : combinOparaPtr->winSize = commBufferSize;
8137 1 : combinOparaPtr->winExpSize = EXP_BUFFER_SIZE;
8138 1 : combinOparaPtr->config.deterministic = GetDeterministicConfig();
8139 1 : combinOparaPtr->config.notifyWaitTime =
8140 1 : (GetExternalInputHcclExecTimeoutSet() != HcclExecTimeoutSet::HCCL_EXEC_TIMEOUT_NOT_SET ||
8141 1 : commConfig_.GetConfigExecTimeOutSet()) ? commConfig_.GetConfigExecTimeOut() : NOTIFY_DEFAULT_WAIT_TIME;
8142 1 : hcclMc2Info_.groupName = hrtMsprofGetHashId(identifier_.c_str(), identifier_.length());
8143 1 : combinOparaPtr->config.linkTimeOut = std::chrono::seconds(GetExternalInputHcclLinkTimeOut());
8144 1 : hcclMc2Info_.rankSize = rankSize;
8145 1 : hcclMc2Info_.rankId = curRankId;
8146 1 : hcclMc2Info_.usrRankId = usrRankId;
8147 1 : hcclMc2Info_.aicpuKfcStreamId = static_cast<uint32_t>(stream.id());
8148 1 : hcclMc2Info_.commStreamSize = rankSize;
8149 1 : hcclMc2Info_.reserve = 0;
8150 1 : rtEvent_t event = nullptr;
8151 1 : u32 eventId = 0;
8152 1 : u32 idx = 0;
8153 1 : u32 txSigleBase = 2;
8154 1 : u32 rxSigleBase = 3;
8155 :
8156 1 : if (isA2MC2MultiServer_) {
8157 : // MoE融合算子优化,MC2多机场景
8158 : // 判断是否支持NormalQP创建,若不支持,需要额外下发敲Doorbell任务
8159 0 : bool isSupportNormalQP = false;
8160 0 : CHK_RET(IsSupportAicpuNormalQP(devicePhyId_, isSupportNormalQP));
8161 0 : CHK_RET(SetDevIbverbsData(comm, isSupportNormalQP, commBufferSize, commInPtr, commOutPtr));
8162 :
8163 0 : bool isSupportAIVNormalQP = false;
8164 0 : CHK_RET(IsSupportAIVNormalQP(devicePhyId_, isSupportAIVNormalQP));
8165 0 : if (isSupportAIVNormalQP && isA2MC2IntraHie_) {
8166 0 : CHK_RET(GenAiRMAInfo(comm));
8167 0 : } else {
8168 0 : HCCL_WARNING("[%s] db transfer normal qp not support. tag[%s] curRankId[%u] rankNum[%u] isSupportAIVNormalQP[%u]",
8169 : __func__, comm->Tag().c_str(), curRankId, rankSize, isSupportAIVNormalQP);
8170 : }
8171 :
8172 0 : if (combinedCapabilityMem_ == nullptr) {
8173 0 : CHK_RET(AllocAndClearHostMem(sizeof(CombinedCapability), combinedCapabilityMem_));
8174 : }
8175 0 : CHK_PTR_NULL(combinedCapabilityMem_);
8176 0 : CombinedCapability *combinedCapabilityPtr = reinterpret_cast<CombinedCapability*>(combinedCapabilityMem_->ptr());
8177 0 : CHK_PTR_NULL(combinedCapabilityPtr);
8178 0 : SalSetBitOne(combinedCapabilityPtr->dataplaneModeBitmap, POS_DATA_PLANE_MODE_HOST);
8179 0 : if (isSupportAIVNormalQP && isA2MC2IntraHie_) {
8180 0 : SalSetBitOne(combinedCapabilityPtr->dataplaneModeBitmap, POS_DATA_PLANE_MODE_AIV);
8181 : }
8182 0 : SalSetBitOne(combinedCapabilityPtr->dataplaneModeBitmap, POS_DATA_PLANE_MODE_AICPU);
8183 :
8184 0 : HCCL_INFO("[SetCommResource] Set dataplaneModeBitmap to [%llu]", combinedCapabilityPtr->dataplaneModeBitmap);
8185 :
8186 : // 非NormalQP场景需要传一条流,用于敲Doorbell
8187 0 : combinOparaPtr->streamInfo[0].streamIds = streamInfo.ringDeviceStreams[0].id();
8188 0 : combinOparaPtr->streamInfo[0].sqIds = streamInfo.ringDeviceStreams[0].sqId();
8189 0 : combinOparaPtr->streamInfo[0].cqIds = streamInfo.ringDeviceStreams[0].cqId();
8190 0 : combinOparaPtr->streamInfo[0].logicCqids = streamInfo.ringDeviceStreams[0].logicCqId();
8191 0 : HCCL_DEBUG("[SetCommResource] Set streamInfo[0].streamIds[%u].sqIds[%u].cqIds[%u].logicCqids[%u]",
8192 : combinOparaPtr->streamInfo[0].streamIds,
8193 : combinOparaPtr->streamInfo[0].sqIds,
8194 : combinOparaPtr->streamInfo[0].cqIds,
8195 : combinOparaPtr->streamInfo[0].logicCqids);
8196 : } else {
8197 2 : for (u32 i = 0; i < rankSize; i++) {
8198 1 : if (i != curRankId) {
8199 : void *bufferIn;
8200 : void *bufferOut;
8201 0 : std::vector<void *> remotePtrVec;
8202 0 : CHK_RET(comm->GetTransportByRank(i)->GetRemoteMem(UserMemType::INPUT_MEM, &bufferIn));
8203 0 : combinOparaPtr->windowsIn[i] = reinterpret_cast<u64>(bufferIn);
8204 :
8205 0 : CHK_RET(comm->GetTransportByRank(i)->GetRemoteMem(UserMemType::OUTPUT_MEM, &bufferOut));
8206 0 : combinOparaPtr->windowsOut[i] = reinterpret_cast<u64>(bufferOut);
8207 :
8208 0 : CHK_RET(comm->GetTransportByRank(i)->GetRemoteMem(&remotePtrVec));
8209 0 : if (remotePtrVec.size() != 0) {
8210 0 : combinOparaPtr->windowsExp[i] = reinterpret_cast<u64>(remotePtrVec[0]);
8211 0 : if (comm->GetTransportByRank(i)->GetTransportType() == TransportType::TRANS_TYPE_P2P) {
8212 0 : p2pCclBuf_[i] = remotePtrVec[0];
8213 : } else {
8214 0 : cclBuf_[i] = remotePtrVec[0];
8215 : }
8216 0 : combinOparaPtr->windowsExp[i] += cclBufferManager_.GetInCCLbufferSize() + cclBufferManager_.GetOutCCLbufferSize();
8217 : }
8218 0 : CHK_RET(comm->GetTransportByRank(i)->GetTxAckDevNotifyInfo(combinOparaPtr->signalInfo.ipcNotifys[i]));
8219 0 : CHK_RET(comm->GetTransportByRank(i)->GetRxAckDevNotifyInfo(combinOparaPtr->signalInfo.ipcNotifys[i + rankSize]));
8220 0 : CHK_RET(comm->GetTransportByRank(i)->GetTxDataSigleDevNotifyInfo(combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase]));
8221 0 : CHK_RET(comm->GetTransportByRank(i)->GetRxDataSigleDevNotifyInfo(combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase]));
8222 0 : CHK_RET(GetAiCpuNotifyData(streamInfo.ringDeviceSignalAux[idx],
8223 : combinOparaPtr->signalInfo.noIpcNotifys[i]));
8224 :
8225 0 : CHK_RET(GetAiCpuNotifyData(streamInfo.ringDeviceSignal[idx],
8226 : combinOparaPtr->signalInfo.noIpcNotifys[i + rankSize]));
8227 0 : idx++;
8228 0 : } else {
8229 1 : combinOparaPtr->windowsIn[i] = reinterpret_cast<u64>(commInPtr);
8230 1 : combinOparaPtr->windowsOut[i] = reinterpret_cast<u64>(commOutPtr);
8231 1 : combinOparaPtr->windowsExp[i] = reinterpret_cast<u64>(commExpPtr);
8232 : // 在与aicpu商议后,本卡不再防止无效值。后续代码要删掉
8233 1 : combinOparaPtr->signalInfo.ipcNotifys[i].resId = INVALID_U64;
8234 1 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize].resId = INVALID_U64;
8235 1 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase].resId = INVALID_U64;
8236 1 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase].resId = INVALID_U64;
8237 : }
8238 1 : HCCL_INFO("group[%s] successfully set windowsIn & windowsOut & windowsExp info: userRank[%u], groupRank[%u], "
8239 : "windowsIn[0x%llx], InSize[0x%llx], windowOut[0x%llx], OutSize[0x%llx], windowExp[0x%llx], ExpSize[0x%llu]",
8240 : identifier_.c_str(), GetUserRank(), GetGroupRank(),
8241 : combinOparaPtr->windowsIn[i], cclBufferManager_.GetInCCLbufferSize(),
8242 : combinOparaPtr->windowsOut[i], cclBufferManager_.GetOutCCLbufferSize(),
8243 : combinOparaPtr->windowsExp[i], cclBufferManager_.GetExpBufferSize());
8244 :
8245 1 : combinOparaPtr->signalInfo.ipcNotifys[i].rankId = i;
8246 1 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize].rankId = i;
8247 1 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase].rankId = i;
8248 1 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase].rankId = i;
8249 1 : combinOparaPtr->signalInfo.noIpcNotifys[i].rankId = i;
8250 :
8251 1 : hcclMc2Info_.commStreamIds[i] = streamInfo.ringDeviceStreams[i].id();
8252 1 : combinOparaPtr->streamInfo[i].streamIds = streamInfo.ringDeviceStreams[i].id();
8253 1 : combinOparaPtr->streamInfo[i].sqIds = streamInfo.ringDeviceStreams[i].sqId();
8254 1 : combinOparaPtr->streamInfo[i].cqIds = streamInfo.ringDeviceStreams[i].cqId();
8255 1 : combinOparaPtr->streamInfo[i].logicCqids = streamInfo.ringDeviceStreams[i].logicCqId();
8256 1 : HCCL_DEBUG("[hccl_Mc2_Info] commStreamIds[%u]:[%u]", i, streamInfo.ringDeviceStreams[i].id());
8257 :
8258 1 : CHK_RET(hrtEventCreateWithFlag(&event));
8259 :
8260 1 : CHK_RET(hrtGetEventID(event, &eventId));
8261 1 : aiCpuNoIpcEvnet_.push_back(event);
8262 1 : combinOparaPtr->signalInfo.noIpcEvents[i].resId = eventId;
8263 1 : HCCL_DEBUG("SetCommResource ipc notify info pre record local rankid: %u: remote rankid:%u, resId:%llu, "
8264 : "devId:%u, tsId:%u, addr:%llu.",
8265 : curRankId, combinOparaPtr->signalInfo.ipcNotifys[i].rankId, combinOparaPtr->signalInfo.ipcNotifys[i].resId,
8266 : combinOparaPtr->signalInfo.ipcNotifys[i].devId, combinOparaPtr->signalInfo.ipcNotifys[i].tsId,
8267 : combinOparaPtr->signalInfo.ipcNotifys[i].addr);
8268 1 : HCCL_DEBUG("SetCommResource ipc notify info pre wait local rankid: %u: remote rankid:%u, resId:%llu, "
8269 : "devId:%u, tsId:%u, addr:%llu.",
8270 : curRankId, combinOparaPtr->signalInfo.ipcNotifys[i + rankSize].rankId,
8271 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize].resId,
8272 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize].devId,
8273 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize].tsId,
8274 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize].addr);
8275 1 : HCCL_DEBUG("SetCommResource ipc notify info post record local rankid: %u: remote rankid:%u, resId:%llu, "
8276 : "devId:%u, tsId:%u, addr:%llu.",
8277 : curRankId,
8278 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase].rankId,
8279 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase].resId,
8280 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase].devId,
8281 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase].tsId,
8282 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase].addr);
8283 1 : HCCL_DEBUG("SetCommResource ipc notify info post wait local rankid: %u: remote rankid:%u, resId:%llu, "
8284 : "devId:%u, tsId:%u, addr:%llu.",
8285 : curRankId,
8286 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase].rankId,
8287 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase].resId,
8288 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase].devId,
8289 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase].tsId,
8290 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase].addr);
8291 : }
8292 : }
8293 1 : HCCL_DEBUG("[hccl_Mc2_Info] groupname:[%s][%llu], rankSize[%u], rankId[%u], usrRankId[%u], aicpuKfcStreamId[%u], "
8294 : "commStreamSize[%u]",
8295 : identifier_.c_str(), hcclMc2Info_.groupName, rankSize, curRankId, usrRankId,
8296 : static_cast<uint32_t>(stream.id()), rankSize);
8297 1 : CHK_RET(ProfilingManagerPub::CallMsprofReportMc2CommInfo(hrtMsprofSysCycleTime(), &hcclMc2Info_,
8298 : sizeof(hcclMc2Info_)));
8299 1 : return HCCL_SUCCESS;
8300 : }
8301 :
8302 802 : void HcclCommunicator::ReleaseCommContextbuffer()
8303 : {
8304 802 : commContext_.free();
8305 804 : }
8306 :
8307 0 : HcclResult HcclCommunicator::CreateDeviceCommContext(u64 size, DeviceMem &buffer) const
8308 : {
8309 0 : CHK_PRT_RET(!size, HCCL_INFO("[Create][DeviceCommContext]device commContext size is zero. "
8310 : "not need to malloc memory"),
8311 : HCCL_SUCCESS);
8312 :
8313 : CHK_PRT_RET((size > ULONG_MAX),
8314 : HCCL_ERROR("[Create][DeviceCommContext]device commContext size %llu is large than ULONG_MAX",
8315 : size),
8316 : HCCL_E_PARA);
8317 :
8318 0 : if (!buffer.ptr()) {
8319 0 : u64 memSize = size;
8320 0 : CHK_RET(DeviceMem::alloc(buffer, memSize));
8321 : }
8322 0 : return HCCL_SUCCESS;
8323 : }
8324 :
8325 0 : void HcclCommunicator::Break()
8326 : {
8327 0 : if (implAlg_ != nullptr) {
8328 0 : implAlg_->Break();
8329 : }
8330 0 : return;
8331 : }
8332 :
8333 3 : HcclResult HcclCommunicator::GetAlltoAllStagedWorkSpaceMemSize(u64 *sendCounts, u64 *sdispls, HcclDataType sendType,
8334 : u64 *recvCounts, u64 *rdispls, HcclDataType recvType, u64 &memSize)
8335 : {
8336 3 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
8337 0 : HCCL_ERROR("[%s][%s]GetAlltoAllStagedWorkSpaceMemSize Not Supported!",
8338 : LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_NOT_SUPPORTED.c_str());
8339 0 : return HCCL_E_NOT_SUPPORT;
8340 : }
8341 3 : CHK_SMART_PTR_NULL(implAlg_);
8342 3 : std::unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(HcclCMDType::HCCL_CMD_ALLTOALLV);
8343 3 : AlltoAllOperator *alltoAllOperator = dynamic_cast<AlltoAllOperator *>(algOperator.get());
8344 3 : CHK_PTR_NULL(alltoAllOperator);
8345 :
8346 3 : OpParam opParam;
8347 3 : opParam.All2AllDataDes.sendType = sendType;
8348 3 : opParam.All2AllDataDes.recvType = recvType;
8349 3 : opParam.All2AllDataDes.sendCounts = static_cast<void *>(sendCounts);
8350 3 : opParam.All2AllDataDes.recvCounts = static_cast<void *>(recvCounts);
8351 3 : opParam.All2AllDataDes.sdispls = static_cast<void *>(sdispls);
8352 3 : opParam.All2AllDataDes.rdispls = static_cast<void *>(rdispls);
8353 3 : opParam.opType = HcclCMDType::HCCL_CMD_ALLTOALLV;
8354 3 : opParam.aicpuUnfoldMode = false;
8355 3 : opParam.aicpuCacheEnable = 0;
8356 :
8357 3 : if (alltoAllOperator->IsSatisfyAlltoAllAivCondition(opParam) ||
8358 0 : alltoAllOperator->IsSatisfy91093OffloadCondition()) {
8359 3 : memSize = 0;
8360 3 : HCCL_INFO("Calculate workSpace MemSize for aiv AllToAll done, memSize[%llu]", memSize);
8361 3 : return HCCL_SUCCESS;
8362 : }
8363 :
8364 0 : std::unique_ptr<PreProcessMetaInfo> preMetaInfo = std::make_unique<PreProcessMetaInfo>();
8365 0 : CHK_SMART_PTR_NULL(preMetaInfo);
8366 :
8367 0 : CHK_RET(alltoAllOperator->PrepareAlltoAllAddrInfo(opParam.All2AllDataDes.sendCounts, opParam.All2AllDataDes.sdispls,
8368 : opParam.All2AllDataDes.sendType, opParam.All2AllDataDes.recvCounts, opParam.All2AllDataDes.rdispls,
8369 : opParam.All2AllDataDes.recvType, preMetaInfo));
8370 :
8371 0 : preMetaInfo->opType = HcclCMDType::HCCL_CMD_ALLGATHER;
8372 :
8373 0 : CHK_RET(RegressCalPreOp(alltoAllOperator, opParam, preMetaInfo));
8374 :
8375 0 : return alltoAllOperator->GetAlltoAllStagedWorkSpaceMemSize(opParam, memSize);
8376 3 : }
8377 :
8378 1 : HcclResult HcclCommunicator::GetAlltoAllStagedWorkSpaceMemSize(
8379 : std::vector<SendRecvInfo> &allMeshAggregationSendRecvInfo, u64 &memSize)
8380 : {
8381 1 : CHK_PRT_RET(Is310P3Common(isHaveCpuRank_, deviceType_),
8382 : HCCL_ERROR("[HcclCommunicator][GetAlltoAllStagedWorkSpaceMemSize]Not Supported!"), HCCL_E_NOT_SUPPORT);
8383 :
8384 1 : CHK_SMART_PTR_NULL(implAlg_);
8385 0 : return implAlg_->GetAlltoAllStagedWorkSpaceMemSize(allMeshAggregationSendRecvInfo, memSize);
8386 : }
8387 :
8388 0 : HcclResult HcclCommunicator::GetAllReduceScratchSize(
8389 : const u64 count, const HcclDataType dataType, u64 &scratchSize) const
8390 : {
8391 0 : CHK_SMART_PTR_NULL(implAlg_);
8392 0 : return implAlg_->GetAllReduceScratchSize(count, dataType, scratchSize);
8393 : }
8394 :
8395 33 : HcclResult HcclCommunicator::SetWorldGroupInfo(
8396 : std::unordered_map<std::string, std::map<u32, HcclIpAddress>> phyIdNicInfoMap,
8397 : vector<RankInfo> worldRankInfoList, vector<u32> &nicRanksPort, vector<u32> &vnicRanksPort)
8398 : {
8399 33 : for (auto &ipInfo : phyIdNicInfoMap) {
8400 0 : for (auto &devInfo : ipInfo.second) {
8401 0 : rankDevicePhyIdNicInfoMap_[ipInfo.first][devInfo.first] = devInfo.second;
8402 0 : HCCL_DEBUG("phyIdNicInfoMap print hostIp[%s] devId[%u] devIp[%s]",
8403 : ipInfo.first.c_str(), devInfo.first, devInfo.second.GetReadableAddress());
8404 : }
8405 : }
8406 :
8407 33 : for (auto &rankInfo : worldRankInfoList) {
8408 0 : worldRankInfoList_.push_back(rankInfo);
8409 : }
8410 :
8411 33 : for (auto &port : nicRanksPort) {
8412 0 : nicRanksPort_.push_back(port);
8413 0 : HCCL_DEBUG("nicRanksPort port[%u]", port);
8414 : }
8415 33 : for (auto &port : vnicRanksPort) {
8416 0 : vnicRanksPort_.push_back(port);
8417 0 : HCCL_DEBUG("vnicRanksPort port[%u]", port);
8418 : }
8419 33 : return HCCL_SUCCESS;
8420 : }
8421 :
8422 0 : HcclResult HcclCommunicator::GetTopoDesc(HcclTopoDescs *topoDescs, uint32_t topoSize)
8423 : {
8424 0 : if (topoSize < static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_MAX)) {
8425 0 : HCCL_ERROR("topoDescs size is not enough, please check topoSize[%u]", topoSize);
8426 0 : return HCCL_E_PARA;
8427 : }
8428 :
8429 0 : if (deviceType_ == DevType::DEV_TYPE_910_93) {
8430 0 : topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)].algSets = HCCL_ALG_SWITCH | HCCL_ALG_RING;
8431 0 : topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L1)].algSets = HCCL_ALG_RING;
8432 0 : } else if (deviceType_ == DevType::DEV_TYPE_910B) {
8433 0 : topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)].algSets = HCCL_ALG_MESH;
8434 0 : topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L1)].algSets = 0;
8435 0 : } else if (deviceType_ == DevType::DEV_TYPE_310P3) {
8436 0 : topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)].algSets = HCCL_ALG_RING;
8437 0 : topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L1)].algSets = 0;
8438 : }
8439 :
8440 0 : topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)].rankSize = userRankSize_;
8441 0 : topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L1)].rankSize = 0;
8442 0 : return HCCL_SUCCESS;
8443 : }
8444 :
8445 234 : HcclResult HcclCommunicator::SetAivModeConfig(const bool aivMode)
8446 : {
8447 234 : CHK_SMART_PTR_NULL(implAlg_);
8448 234 : CHK_RET(implAlg_->SetAivModeConfig(aivMode));
8449 234 : return HCCL_SUCCESS;
8450 : }
8451 :
8452 234 : HcclResult HcclCommunicator::SetOnlyAivModeConfig(const bool isOnlyAiv)
8453 : {
8454 234 : CHK_SMART_PTR_NULL(implAlg_);
8455 234 : CHK_RET(implAlg_->SetOnlyAivModeConfig(isOnlyAiv));
8456 234 : isOnlyAiv_ = isOnlyAiv;
8457 234 : return HCCL_SUCCESS;
8458 : }
8459 :
8460 234 : HcclResult HcclCommunicator::SetAicpuUnfoldConfig(const bool aicpuUnfold)
8461 : {
8462 234 : CHK_SMART_PTR_NULL(implAlg_);
8463 234 : CHK_RET(implAlg_->SetAicpuUnfoldConfig(aicpuUnfold));
8464 234 : return HCCL_SUCCESS;
8465 : }
8466 :
8467 234 : HcclResult HcclCommunicator::SetExecTimeOutConfig(const s32 execTimeOut)
8468 : {
8469 234 : CHK_SMART_PTR_NULL(implAlg_);
8470 234 : CHK_RET(implAlg_->SetExecTimeOutConfig(execTimeOut));
8471 234 : return HCCL_SUCCESS;
8472 : }
8473 :
8474 234 : HcclResult HcclCommunicator::SetAlgoConfig(const std::map<HcclCMDType, std::vector<HcclAlgoType>>& algoMap)
8475 : {
8476 234 : CHK_SMART_PTR_NULL(implAlg_);
8477 234 : CHK_RET(implAlg_->SetAlgoConfig(algoMap));
8478 234 : return HCCL_SUCCESS;
8479 : }
8480 :
8481 527 : bool HcclCommunicator::GetAivModeConfig()
8482 : {
8483 527 : return commConfig_.GetConfigAivMode();
8484 : }
8485 :
8486 529 : bool HcclCommunicator::GetConfigIsOnlyAivMode()
8487 : {
8488 529 : return commConfig_.GetConfigIsOnlyAivMode();
8489 : }
8490 :
8491 1969 : bool HcclCommunicator::GetAicpuUnfoldConfig()
8492 : {
8493 1969 : return commConfig_.GetConfigAicpuUnfold();
8494 : }
8495 :
8496 0 : void HcclCommunicator::SetQpQosAttr(u32 trafficClass, u32 serviceLevel)
8497 : {
8498 0 : if (oneSideService_) {
8499 0 : oneSideService_->SetTCAndSL(trafficClass, serviceLevel);
8500 0 : HCCL_INFO("[%s]Set TC[%u] and SL[%u] for oneSidedService success.", __func__, trafficClass, serviceLevel);
8501 : }
8502 0 : transportManager_->SetQpQosAttr(trafficClass, serviceLevel);
8503 0 : indptOpTransportManager_->SetQpQosAttr(trafficClass, serviceLevel);
8504 0 : }
8505 :
8506 0 : HcclResult HcclCommunicator::CheckExitWaitResumeState(bool &isChangedLink)
8507 : {
8508 0 : if (retryEnable_ && opRetryManager_ != nullptr) {
8509 0 : bool haveCommEnableBackupLink = false;
8510 0 : if (g_enableBackupLinkCommCount.load() > 0) {
8511 0 : haveCommEnableBackupLink = true;
8512 : }
8513 0 : HcclResult ret = opRetryManager_->ExitWaitResumeState(identifier_, commConnections_.isRoot, haveCommEnableBackupLink, isChangedLink);
8514 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
8515 : HCCL_ERROR("[HcclCommunicator][Resume]opretry exit wait resume state failed."), ret);
8516 : }
8517 0 : return HCCL_SUCCESS;
8518 : }
8519 :
8520 0 : HcclResult HcclCommunicator::SetMemoryRange(void *baseVirPtr, size_t size, size_t alignment, uint64_t flags)
8521 : {
8522 0 : CHK_PRT_RET(deviceType_ != DevType::DEV_TYPE_910_93,
8523 : HCCL_ERROR("[HcclCommunicator][SetMemoryRange] deviceType[%d] not support zero copy", deviceType_), HCCL_E_NOT_SUPPORT);
8524 0 : if (zeroCopyMemoryAgent_ == nullptr) {
8525 0 : CHK_RET(InitZeroCopyMemoryAgent());
8526 : }
8527 0 : CHK_RET(zeroCopyMemoryAgent_->SetMemoryRange(baseVirPtr, size, alignment, flags));
8528 0 : return HCCL_SUCCESS;
8529 : }
8530 :
8531 0 : HcclResult HcclCommunicator::UnsetMemoryRange(void *baseVirPtr)
8532 : {
8533 0 : CHK_PRT_RET(zeroCopyMemoryAgent_ == nullptr,
8534 : HCCL_ERROR("[HcclCommunicator][UnsetMemoryRange] not call HcclCommSetMemoryRange()"), HCCL_E_PARA);
8535 0 : CHK_RET(zeroCopyMemoryAgent_->UnsetMemoryRange(baseVirPtr));
8536 0 : return HCCL_SUCCESS;
8537 : }
8538 :
8539 0 : HcclResult HcclCommunicator::ActivateCommMemory(void *virPtr, size_t size, size_t offset, void *handle, uint64_t flags)
8540 : {
8541 0 : CHK_PRT_RET(zeroCopyMemoryAgent_ == nullptr,
8542 : HCCL_ERROR("[HcclCommunicator][ActivateCommMemory] not call HcclCommSetMemoryRange()"), HCCL_E_PARA);
8543 0 : CHK_RET(zeroCopyMemoryAgent_->ActivateCommMemory(virPtr, size, offset, handle, flags));
8544 0 : return HCCL_SUCCESS;
8545 : }
8546 :
8547 0 : HcclResult HcclCommunicator::DeactivateCommMemory(void *virPtr)
8548 : {
8549 0 : CHK_PRT_RET(zeroCopyMemoryAgent_ == nullptr,
8550 : HCCL_ERROR("[HcclCommunicator][DeactivateCommMemory] not call HcclCommSetMemoryRange()"), HCCL_E_PARA);
8551 0 : CHK_RET(zeroCopyMemoryAgent_->DeactivateCommMemory(virPtr));
8552 0 : return HCCL_SUCCESS;
8553 : }
8554 :
8555 0 : HcclResult HcclCommunicator::SetSingleLinkInfo(std::unordered_map<u32, bool> &switchRanks, u32 remoteRankId,
8556 : ChangeLinkInfo &changeLinkInfo)
8557 : {
8558 0 : auto iterLocal = switchRanks.find(userRank_);
8559 0 : auto iterRemote = switchRanks.find(remoteRankId);
8560 :
8561 0 : bool useBackupLink = false;
8562 0 : if (iterLocal != switchRanks.end() && iterRemote != switchRanks.end()) {
8563 : // 本端卡和对端卡都切,如果两者的目标网卡冲突,则切换失败;否则使用一致的目标网卡的的对应链路
8564 0 : CHK_PRT_RET(iterLocal->second ^ iterRemote->second,
8565 : HCCL_ERROR("[HcclCommunicator][SetSingleLinkInfo] local rank[%u] plan to switch to nic[%u], "
8566 : "which is conflict with remote rank[%u] planning to switch to nic[%u].",
8567 : userRank_, iterLocal->second, remoteRankId, iterRemote->second),
8568 : HCCL_E_PARA);
8569 0 : useBackupLink = iterLocal->second;
8570 0 : } else if (iterLocal != switchRanks.end()) {
8571 : // 仅切换本端卡,根据本端卡的目标网卡,刷新对应链路
8572 0 : useBackupLink = iterLocal->second;
8573 0 : } else if (iterRemote != switchRanks.end()) {
8574 : // 仅切换对端卡,根据对端卡的目标网卡,刷新对应链路
8575 0 : useBackupLink = iterRemote->second;
8576 : } else {
8577 0 : HCCL_INFO("[HcclCommunicator][SetSingleLinkInfo] comm identifier[%s], local rank[%u], "
8578 : "remote rank[%u], neither the rank need switch, link will not be refreshed.",
8579 : identifier_.c_str(), userRank_, remoteRankId);
8580 0 : return HCCL_SUCCESS;
8581 : }
8582 :
8583 0 : changeLinkInfo.remoteRankList[changeLinkInfo.remoteRankNum] = remoteRankId;
8584 0 : changeLinkInfo.isUseDefaultPort[changeLinkInfo.remoteRankNum] = !(useBackupLink);
8585 0 : changeLinkInfo.remoteRankNum++;
8586 0 : remoteRankNicStatus_[remoteRankId] = useBackupLink ? CONNECT_REMOTE_BACKUP : CONNECT_REMOTE_DEFAULT;
8587 0 : needCheckBackupNic_ |= useBackupLink;
8588 0 : needCheckDefaultNic_ |= !useBackupLink;
8589 :
8590 0 : HCCL_RUN_INFO("[HcclCommunicator][SetSingleLinkInfo] comm identifier[%s], local rank[%u], "
8591 : "remote rank[%u], useBackupLink[%u], link info refreshed.",
8592 : identifier_.c_str(), userRank_, remoteRankId, useBackupLink);
8593 0 : return HCCL_SUCCESS;
8594 : }
8595 :
8596 0 : HcclResult HcclCommunicator::SetRemoteRankLinkInfo(std::unordered_map<u32, bool> &switchRanks,
8597 : ChangeLinkInfo &changeLinkInfo)
8598 : {
8599 : // 初始化重置changeLinkInfo
8600 0 : changeLinkInfo.remoteRankNum = 0;
8601 0 : needCheckBackupNic_ = false;
8602 0 : needCheckDefaultNic_ = false;
8603 : // 初始化重置remoteRankNicStatus_
8604 0 : (void)memset_s(remoteRankNicStatus_, sizeof(remoteRankNicStatus_), 0, sizeof(remoteRankNicStatus_));
8605 :
8606 0 : for (auto resIt : resMap_) {
8607 0 : for (auto &levelNSubCommTransport : resIt.second.opTransportResponse) {
8608 0 : for (auto &singleSubCommTransport : levelNSubCommTransport) {
8609 0 : for (auto &transportRequest : singleSubCommTransport.transportRequests) {
8610 0 : if (transportRequest.isValid && transportRequest.isUsedRdma) { // 仅RDMA链路需要刷新
8611 0 : CHK_RET(SetSingleLinkInfo(switchRanks, transportRequest.remoteUserRank, changeLinkInfo));
8612 : }
8613 : }
8614 : }
8615 : }
8616 0 : }
8617 0 : return HCCL_SUCCESS;
8618 : }
8619 :
8620 0 : HcclResult HcclCommunicator::ActiveStoppedLink(std::map<u32, bool> &remoteRankPortMap,
8621 : OpCommTransport &opTransportResponse, bool isBackup)
8622 : {
8623 0 : for (auto &levelNSubCommTransport : opTransportResponse) {
8624 0 : for (auto &singleSubCommTransport : levelNSubCommTransport) {
8625 0 : if (singleSubCommTransport.status.size() == 0) {
8626 0 : continue;
8627 : }
8628 0 : if (singleSubCommTransport.status.size() != singleSubCommTransport.transportRequests.size()
8629 0 : || singleSubCommTransport.links.size() != singleSubCommTransport.transportRequests.size()) {
8630 0 : HCCL_ERROR("[HcclCommunicator][ActiveStoppedLink] comm identifier[%s], local rank[%u], "
8631 : "status num[%u] or links num[%u] is inconsistent with transport request num[%u]. "
8632 : "Please check whether the resources are allocated correctly.",
8633 : identifier_.c_str(), userRank_, singleSubCommTransport.status.size(),
8634 : singleSubCommTransport.links.size(), singleSubCommTransport.transportRequests.size());
8635 0 : return HCCL_E_INTERNAL;
8636 : }
8637 :
8638 0 : for (size_t i = 0; i < singleSubCommTransport.transportRequests.size(); i++) {
8639 0 : auto &transportRequest = singleSubCommTransport.transportRequests[i];
8640 0 : auto remoteRankIter = remoteRankPortMap.find(transportRequest.remoteUserRank);
8641 0 : bool needLink = transportRequest.isValid && transportRequest.isUsedRdma && remoteRankIter != remoteRankPortMap.end()
8642 0 : && (remoteRankIter->second ^ isBackup);
8643 : // STOP状态的Transport需要唤醒,重置位到READY
8644 0 : if (needLink && singleSubCommTransport.status[i] == TransportStatus::STOP) {
8645 0 : HCCL_INFO("[HcclCommunicator][ActiveStoppedLink] comm identifier[%s], local rank[%u], "
8646 : "resuming link of remote rank[%u]",
8647 : identifier_.c_str(), userRank_,
8648 : transportRequest.remoteUserRank);
8649 0 : CHK_RET(singleSubCommTransport.links[i]->Resume());
8650 0 : singleSubCommTransport.status[i] = TransportStatus::READY;
8651 : }
8652 : }
8653 : }
8654 : }
8655 0 : return HCCL_SUCCESS;
8656 : }
8657 :
8658 0 : HcclResult HcclCommunicator::PrepareLinkForSwitchNic(std::unordered_map<u32, bool> &switchRanks,
8659 : ChangeLinkInfo &changeLinkInfo)
8660 : {
8661 0 : CHK_RET(SetRemoteRankLinkInfo(switchRanks, changeLinkInfo));
8662 :
8663 0 : std::map<u32, bool> remoteRankPortMap;
8664 0 : for (u32 i = 0; i < changeLinkInfo.remoteRankNum; i++) {
8665 0 : remoteRankPortMap.emplace(changeLinkInfo.remoteRankList[i], changeLinkInfo.isUseDefaultPort[i]);
8666 : }
8667 0 : for (auto resIt : resMap_) {
8668 0 : CHK_RET(ActiveStoppedLink(remoteRankPortMap, resIt.second.opTransportResponse, false));
8669 0 : CHK_RET(ActiveStoppedLink(remoteRankPortMap, resIt.second.opTransportResponseBackUp, true));
8670 0 : }
8671 0 : return HCCL_SUCCESS;
8672 0 : }
8673 :
8674 0 : HcclResult HcclCommunicator::ParseSwitchRanks(uint32_t nRanks, uint32_t *ranks, bool *useBackup,
8675 : std::unordered_map<u32, bool> &switchRanks)
8676 : {
8677 0 : CHK_PTR_NULL(ranks);
8678 0 : CHK_PTR_NULL(useBackup);
8679 0 : switchRanksNum_ = nRanks;
8680 0 : (void)memset_s(switchRankList_, sizeof(switchRankList_), 0, sizeof(switchRankList_));
8681 0 : (void)memset_s(switchUseBackup_, sizeof(switchUseBackup_), 0, sizeof(switchUseBackup_));
8682 0 : s32 ret = memcpy_s(switchRankList_, sizeof(switchRankList_), ranks, sizeof(u32) * nRanks);
8683 0 : CHK_PRT_RET(ret != EOK, HCCL_ERROR("[HcclCommunicator][ParseSwitchRanks] mem copy switch ranks fail."),
8684 : HCCL_E_INTERNAL);
8685 0 : ret = memcpy_s(switchUseBackup_, sizeof(switchUseBackup_), useBackup, sizeof(bool) * nRanks);
8686 0 : CHK_PRT_RET(ret != EOK, HCCL_ERROR("[HcclCommunicator][ParseSwitchRanks] mem copy switch use backup fail."),
8687 : HCCL_E_INTERNAL);
8688 :
8689 0 : std::string switchRankStr{};
8690 0 : for (uint32_t i = 0; i < nRanks; i++) {
8691 0 : CHK_PTR_NULL(ranks + i);
8692 0 : CHK_PTR_NULL(useBackup + i);
8693 0 : uint32_t switchRankId = ranks[i];
8694 0 : bool backup = useBackup[i];
8695 0 : CHK_PRT_RET(switchRankId >= userRankSize_,
8696 : HCCL_ERROR("[HcclCommunicator][ParseSwitchRanks] invalid switchRankId[%u], "
8697 : "which should not be greater than rankSize[%u]",
8698 : switchRankId, userRankSize_),
8699 : HCCL_E_PARA);
8700 0 : CHK_PRT_RET(switchRanks.find(switchRankId) != switchRanks.end(),
8701 : HCCL_ERROR("[HcclCommunicator][ParseSwitchRanks] duplicated switchRankId[%u]", switchRankId), HCCL_E_PARA);
8702 0 : switchRanks.emplace(switchRankId, backup);
8703 0 : switchRankStr += std::to_string(switchRankId) + ":" + std::to_string(backup) + ";";
8704 : }
8705 0 : HCCL_RUN_INFO("[HcclCommunicator][ParseSwitchRanks] comm identifier[%s], userRank[%u], load switchRanks:%s.",
8706 : identifier_.c_str(), userRank_, switchRankStr.c_str());
8707 0 : return HCCL_SUCCESS;
8708 0 : }
8709 :
8710 0 : HcclResult HcclCommunicator::SwitchNic(uint32_t nRanks, uint32_t *ranks, bool *useBackup,
8711 : std::shared_ptr<HDCommunicate> &controlH2D, std::shared_ptr<HDCommunicate> &statusD2H)
8712 : {
8713 0 : HcclResult ret = HCCL_SUCCESS;
8714 0 : CHK_PRT_RET(!IsEnableBackupLink(), HCCL_RUN_WARNING("[HcclCommunicator][%s]Backup link is not enabled, "
8715 : "switch nic will not be prorocessed, comm identifier[%s], rank[%u], devType[%u], opretry enable[%u], "
8716 : "backup ip valid[%u], roce enable[%u].", __func__, identifier_.c_str(), userRank_, deviceType_,
8717 : GetAicpuUnfoldConfig() && commConfig_.GetConfigInterSuperPodRetryEnable(),
8718 : !devBackupIpAddr_[0].IsInvalid(), IsEnableRoce()), HCCL_SUCCESS);
8719 0 : CHK_PRT_RET(resMap_.empty(), HCCL_ERROR("[HcclCommunicator][%s] "
8720 : "no collective operation has been executed in this communication[%s] on rank[%u], "
8721 : "which does not support to set working device nic.", __func__, identifier_.c_str(), userRank_),
8722 : HCCL_E_PARA);
8723 0 : std::unordered_map<u32, bool> switchRanks;
8724 0 : ChangeLinkInfo changeLinkInfo;
8725 0 : ret = ParseSwitchRanks(nRanks, ranks, useBackup, switchRanks);
8726 0 : if (ret == HCCL_SUCCESS) {
8727 0 : ret = PrepareLinkForSwitchNic(switchRanks, changeLinkInfo);
8728 : }
8729 0 : changeLinkInfo.isChangeLinkFlag = ret == HCCL_SUCCESS; // 如果入参校验失败,则无需刷新链路;通知aicpu侧,防止其他卡超时等待
8730 :
8731 0 : switchNicWaitingResult_ = false;
8732 :
8733 0 : u32 changeLinkInfoStart = sizeof(KfcCommand) + sizeof(BackgroundCommand) + sizeof(HcclComSuspendingFlag) +
8734 : sizeof(HcclOpIdentifier);
8735 0 : CHK_RET(controlH2D->Put(changeLinkInfoStart, sizeof(ChangeLinkInfo),
8736 : reinterpret_cast<uint8_t *>(&changeLinkInfo)));
8737 :
8738 0 : KfcCommand switchNicCommand = KfcCommand::kSwitchNic;
8739 0 : CHK_RET(controlH2D->Put(0, sizeof(KfcCommand), reinterpret_cast<uint8_t *>(&switchNicCommand)));
8740 :
8741 0 : KfcExecStatus switchStatus;
8742 0 : switchStatus.execStatus.kfcStatus = KfcStatus::kNull;
8743 0 : u32 waitSwitchExecCmdTimeout = static_cast<u32>(GetExternalInputHcclLinkTimeOut() * 1000 * 2.5f);
8744 0 : auto waitSwitchExecCmdTimeoutMs = std::chrono::milliseconds(waitSwitchExecCmdTimeout); // 等待2.5倍的建链超时时间,给快慢卡场景提供冗余
8745 0 : auto startTime = std::chrono::steady_clock::now();
8746 : while (true) {
8747 0 : if (switchNicWaitingResult_) {
8748 0 : CHK_RET(statusD2H->Get(0, sizeof(KfcExecStatus), reinterpret_cast<uint8_t *>(&switchStatus)));
8749 : }
8750 0 : if (switchStatus.execStatus.kfcStatus == KfcStatus::kSwitchSuccess) {
8751 0 : HCCL_INFO("[HcclCommunicator][%s] comm identifier[%s], devicePhyId[%u], userRank[%u] switch nic success.",
8752 : __func__, identifier_.c_str(), devicePhyId_, userRank_);
8753 0 : ret = HCCL_SUCCESS;
8754 0 : break;
8755 0 : } else if (switchStatus.execStatus.kfcStatus == KfcStatus::kSwitchFail) {
8756 0 : HCCL_ERROR("[HcclCommunicator][%s] comm identifier[%s], devicePhyId[%u], userRank[%u] switch nic fail.",
8757 : __func__, identifier_.c_str(), devicePhyId_, userRank_);
8758 0 : ret = HCCL_E_INTERNAL;
8759 0 : break;
8760 0 : } else if ((std::chrono::steady_clock::now() - startTime) >= waitSwitchExecCmdTimeoutMs) {
8761 0 : HCCL_ERROR("[HcclCommunicator][%s] comm identifier[%s], devicePhyId[%u], "
8762 : "userRank[%u] switch nic timeout[%u ms], the transport status is undefined. "
8763 : "Please search log with keyword [ErrToWarn] for detail.",
8764 : __func__, identifier_.c_str(), devicePhyId_, userRank_, waitSwitchExecCmdTimeout);
8765 0 : ret = HCCL_E_TIMEOUT;
8766 0 : break;
8767 : } else {
8768 0 : SaluSleep(ONE_MILLISECOND_OF_USLEEP);
8769 : }
8770 0 : }
8771 0 : KfcExecControl clearCommand{};
8772 0 : CHK_RET(controlH2D->Put(0, sizeof(KfcExecControl), reinterpret_cast<uint8_t *>(&clearCommand)));
8773 0 : KfcExecStatus clearStatus{};
8774 0 : CHK_RET(controlH2D->Put(0, sizeof(KfcExecStatus), reinterpret_cast<uint8_t *>(&clearStatus)));
8775 0 : switchRanksNum_ = 0;
8776 0 : return ret;
8777 0 : }
8778 :
8779 0 : HcclResult HcclCommunicator::GetSwitchRanks(u32 *distSwitchRankList, bool *distSwitchUseBackup, u32 &distSwitchRankNum,
8780 : u8 *distRemoteRankNicStatus, u32 &distNicStatusNum, bool &needCheckDefaultNic, bool &needCheckBackupNic)
8781 : {
8782 0 : s32 ret = memcpy_s(distSwitchRankList, sizeof(u32) * AICPU_MAX_RANK_NUM, switchRankList_,
8783 0 : sizeof(u32) * switchRanksNum_);
8784 0 : CHK_PRT_RET(ret != EOK, HCCL_ERROR("[HcclCommunicator][GetSwitchRanks] mem copy switch rank list fail, ret[%u].", ret), HCCL_E_INTERNAL);
8785 0 : ret = memcpy_s(distSwitchUseBackup, sizeof(bool) * AICPU_MAX_RANK_NUM, switchUseBackup_,
8786 0 : sizeof(bool) * switchRanksNum_);
8787 0 : CHK_PRT_RET(ret != EOK, HCCL_ERROR("[HcclCommunicator][GetSwitchRanks] mem copy switch use backup fail, ret[%u].", ret), HCCL_E_INTERNAL);
8788 0 : distSwitchRankNum = switchRanksNum_;
8789 0 : ret = memcpy_s(distRemoteRankNicStatus, sizeof(u8) * AICPU_MAX_RANK_NUM, remoteRankNicStatus_,
8790 0 : sizeof(u8) * userRankSize_);
8791 0 : CHK_PRT_RET(ret != EOK, HCCL_ERROR("[HcclCommunicator][GetSwitchRanks] mem copy remote rank nic status fail, "
8792 : "ret[%u].",
8793 : ret),
8794 : HCCL_E_INTERNAL);
8795 0 : distNicStatusNum = userRankSize_;
8796 0 : needCheckDefaultNic = needCheckDefaultNic_;
8797 0 : needCheckBackupNic = needCheckBackupNic_;
8798 0 : switchNicWaitingResult_ = true;
8799 0 : return HCCL_SUCCESS;
8800 : }
8801 :
8802 0 : HcclResult GetCannPath(const char *binPath, std::string &cannPath)
8803 : {
8804 0 : CHK_PRT_RET(binPath == nullptr,
8805 : HCCL_ERROR("[HcclCommunicator][GetCannPath]binary path is nullptr"),
8806 : HCCL_E_PTR);
8807 :
8808 0 : std::string tmpPath(binPath); // 存放cann安装路径
8809 0 : std::string libraryPath;
8810 0 : HcclResult ret = ParseLibraryPath(libraryPath);
8811 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
8812 : HCCL_ERROR("[GetCannPath]errNo[0x%016llx]parse path fail.", ret), ret);
8813 :
8814 0 : ret = GetKeyWordPath(libraryPath, "/hccl", tmpPath);
8815 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
8816 : HCCL_ERROR("[GetCannPath]cannot found version file in %s.", libraryPath.c_str()),
8817 : HCCL_E_PARA);
8818 0 : tmpPath += binPath;
8819 0 : cannPath = tmpPath;
8820 :
8821 0 : return HCCL_SUCCESS;
8822 0 : }
8823 :
8824 520 : HcclResult HcclCommunicator::LoadCustomFile(const char *binPath, aclrtBinaryLoadOptionType optionType, uint32_t cpuKernelMode,
8825 : aclrtBinHandle &binHandle)
8826 : {
8827 520 : binHandle = nullptr;
8828 520 : HCCL_INFO("[LoadCustomFile]binPath[%s], optionType[%u], cpuKernelMode[%u]", binPath, optionType, cpuKernelMode);
8829 520 : return HCCL_SUCCESS;
8830 : }
8831 :
8832 0 : void HcclCommunicator::UnloadBinary(aclrtBinHandle &binHandle)
8833 : {
8834 0 : if (binHandle != nullptr) {
8835 0 : aclError ret = aclrtBinaryUnLoad(binHandle);
8836 0 : if (ret != ACL_SUCCESS) {
8837 0 : HCCL_ERROR("[UnloadBinary]errNo[0x%016llx] unload binary from file error.", ret);
8838 : }
8839 0 : binHandle = nullptr;
8840 : }
8841 0 : return;
8842 : }
8843 :
8844 0 : HcclResult HcclCommunicator::RegisterCommUserMem(void* addr, u64 size, void **handle)
8845 : {
8846 : // user mem和ccl buffer互斥,不支持同时创建
8847 0 : if (deviceType_ != DevType::DEV_TYPE_910_93 || superPodNum_ > 1 || isUserMemRegisted_ ||
8848 0 : cclBufferManager_.GetInCCLbuffer().ptr() != nullptr) {
8849 0 : HCCL_ERROR("[HcclCommunicator][%s]Registration user mem is not supported with the params. "
8850 : "Device type[%d], superPodNum[%u]; Or user mem/CCL buffer has already registered, addr[%p], "
8851 : "isUserMemRegisted[%d]", __func__, deviceType_, superPodNum_, addr, isUserMemRegisted_);
8852 0 : return HCCL_E_NOT_SUPPORT;
8853 : }
8854 : // DeviceMem::create创建的DeviceMem对象为拷贝构造,析构时不释放内存,内存由上层管理
8855 0 : DeviceMem userMem = DeviceMem::create(addr, size);
8856 0 : std::shared_ptr<DeviceMem> userMemPtr = nullptr;
8857 0 : EXCEPTION_CATCH((userMemPtr = std::make_shared<DeviceMem>(std::move(userMem))), return HCCL_E_PTR);
8858 0 : *handle = static_cast<void *>(userMemPtr.get());
8859 0 : userMemMap_.insert(std::make_pair(*handle, userMemPtr));
8860 0 : HCCL_INFO("[HcclCommunicator][%s]Register user mem success, group[%s], handle[%p], addr[%llu], size[%llu]",
8861 : __func__, identifier_.c_str(), *handle, reinterpret_cast<uint64_t>(addr), size);
8862 0 : isUserMemRegisted_ = true;
8863 0 : return HCCL_SUCCESS;
8864 0 : }
8865 :
8866 0 : HcclResult HcclCommunicator::DeregisterCommUserMem(void* handle)
8867 : {
8868 0 : if (deviceType_ != DevType::DEV_TYPE_910_93 || superPodNum_ > 1) {
8869 0 : HCCL_ERROR("[HcclCommunicator][%s]Unsupported on the device type[%d] or superPodNum[%u]", __func__,
8870 : deviceType_, superPodNum_);
8871 0 : return HCCL_E_NOT_SUPPORT;
8872 : }
8873 :
8874 0 : CHK_PRT_RET(!userMemMap_.erase(handle),
8875 : HCCL_RUN_WARNING("[HcclCommunicator][%s]Mem is not exist, handle[%p]", __func__, handle), HCCL_SUCCESS);
8876 :
8877 : // 重置user mem和userMemType
8878 0 : CHK_SAFETY_FUNC_RET(memset_s(opResPara_.userMemRes, sizeof(opResPara_.userMemRes), 0,
8879 : sizeof(opResPara_.userMemRes)));
8880 0 : opResPara_.userMemType = 0; // CCL Buffer
8881 0 : isUserMemRegisted_ = false;
8882 0 : HCCL_INFO("[HcclCommunicator][%s]Deregister mem success, group[%s], handle[%p]", __func__,
8883 : identifier_.c_str(), handle);
8884 0 : return HCCL_SUCCESS;
8885 : }
8886 :
8887 0 : HcclResult HcclCommunicator::ExchangeCommUserMem(void* handle, std::vector<u32>& peerRanks)
8888 : {
8889 0 : if (deviceType_ != DevType::DEV_TYPE_910_93 || superPodNum_ > 1 || GetExternalInputInterHccsDisable()) {
8890 0 : HCCL_ERROR("[HcclCommunicator][%s]Unsupported configuration: device type[%d], superPodNum[%u], "
8891 : "or RDMA usage", __func__, deviceType_, superPodNum_);
8892 0 : return HCCL_E_NOT_SUPPORT;
8893 : }
8894 :
8895 0 : if ((peerRanks.size() > rankInfoList_.size())) {
8896 0 : HCCL_ERROR("[HcclCommunicator][%s]Invalid peerRanksNum[%u], which should be less than communicator "
8897 : "rank nums[%u]", __func__, peerRanks.size(), rankInfoList_.size());
8898 0 : return HCCL_E_PARA;
8899 : }
8900 : // 获取user mem,调exchange接口前需要先调注册接口注册user mem
8901 0 : if (userMemMap_.find(handle) == userMemMap_.end()) {
8902 0 : HCCL_ERROR("[HcclCommunicator][%s]Find user mem failed, handle[%p] is not registered", __func__, handle);
8903 0 : return HCCL_E_NOT_FOUND;
8904 : }
8905 0 : DeviceMem userMem = *userMemMap_[handle].get();
8906 0 : CHK_PTR_NULL(userMem.ptr());
8907 : // 构造建链param
8908 0 : TransportIOMem transMem;
8909 0 : transMem.userMem = userMem;
8910 0 : OpCommTransport opCommTransport;
8911 0 : LevelNSubCommTransport level0Transport;
8912 0 : SingleSubCommTransport commTransport;
8913 :
8914 0 : for (u32 rankIdx = 0; rankIdx < peerRanks.size(); rankIdx++) {
8915 0 : TransportRequest tmpTransport;
8916 0 : if (userRank_ != peerRanks[rankIdx]) {
8917 0 : tmpTransport.isValid = true;
8918 0 : tmpTransport.localUserRank = userRank_;
8919 0 : tmpTransport.remoteUserRank = peerRanks[rankIdx];
8920 0 : tmpTransport.inputMemType = TransportMemType::USER_MEM;
8921 0 : tmpTransport.outputMemType = TransportMemType::USER_MEM;
8922 : } else {
8923 : // 本rank不需要创建transport
8924 0 : tmpTransport.isValid = false;
8925 : }
8926 0 : commTransport.transportRequests.push_back(tmpTransport);
8927 : }
8928 0 : level0Transport.push_back(commTransport);
8929 0 : opCommTransport.push_back(level0Transport);
8930 0 : ClearOpTransportResponseLinks(opCommTransport);
8931 : // 建链
8932 0 : constexpr char EXCHANGE_USER_MEM_TAG_PREFIX[] = "ExchangeUserMem_";
8933 0 : string tag = EXCHANGE_USER_MEM_TAG_PREFIX + identifier_;
8934 0 : HcclResult ret = HCCL_SUCCESS;
8935 : {
8936 0 : StateGuard<HcclCommunicator, HcclCommState> guard(this, HcclCommState::BUILDING);
8937 0 : HCCL_RUN_INFO("[%s]Alloc transport, level size[%u], trans request size[%u], mem ptr[%p], mem size[%llu]",
8938 : __func__, opCommTransport.size(), commTransport.transportRequests.size(), userMem.ptr(),
8939 : userMem.size());
8940 0 : CHK_PTR_NULL(transportManager_);
8941 0 : ret = transportManager_->Alloc(tag, transMem, opCommTransport, false);
8942 0 : }
8943 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[%s]Alloc transports failed, tag[%s]", __func__, tag.c_str()),
8944 : ret);
8945 0 : userMemTransport_ = opCommTransport;
8946 0 : return HCCL_SUCCESS;
8947 0 : }
8948 0 : HcclResult HcclCommunicator::GetLocalCCLBuf(void **addr, uint64_t *size)
8949 : {
8950 0 : uint64_t cclbufSize = cclBufferManager_.GetInCCLbufferSize() + cclBufferManager_.GetOutCCLbufferSize() + cclBufferManager_.GetExpBufferSize();
8951 0 : *addr = cclBufferManager_.GetCommCCLBuffer().ptr();
8952 0 : if (nullptr == cclBufferManager_.GetCommCCLBuffer().ptr()) {
8953 0 : cclbufSize = 0;
8954 : }
8955 0 : *size = cclbufSize;
8956 0 : HCCL_INFO("[%s] GetlocalCCLBuf success, addr[%p], size[%u]", identifier_.c_str(), cclBufferManager_.GetCommCCLBuffer().ptr(), cclbufSize);
8957 0 : return HCCL_SUCCESS;
8958 : }
8959 :
8960 2 : HcclResult HcclCommunicator::GetRemoteCCLBuf(uint32_t remoteRank, void **addr, uint64_t *size)
8961 : {
8962 2 : CHK_PRT_RET((remoteRank >= AICPU_MAX_RANK_NUM),
8963 : HCCL_ERROR("[%s] invalid remoteRank[%d]", __func__, remoteRank), HCCL_E_PARA);
8964 : //仅sdma场景
8965 1 : uint64_t cclbufSize = cclBufferManager_.GetInCCLbufferSize() + cclBufferManager_.GetOutCCLbufferSize() + cclBufferManager_.GetExpBufferSize();
8966 1 : *addr = p2pCclBuf_[remoteRank];
8967 :
8968 1 : if (nullptr == p2pCclBuf_[remoteRank]) {
8969 1 : cclbufSize = 0;
8970 : }
8971 1 : *size = cclbufSize;
8972 1 : HCCL_INFO("[%s] GetRemoteCCLBuf success, remoteRank[%u], addr[%p], size[%u]", identifier_.c_str(), remoteRank, p2pCclBuf_[remoteRank], cclbufSize);
8973 1 : return HCCL_SUCCESS;
8974 : }
8975 0 : HcclResult HcclCommunicator::GetKFCWorkSpace(void **addr, uint64_t *size)
8976 : {
8977 0 : *addr = workSpace_.ptr();
8978 0 : *size = workSpaceSize_;
8979 0 : HCCL_INFO("[%s] GetKFCWorkSpace success, addr[%p], size[%u]", identifier_.c_str(), workSpace_.ptr(), workSpaceSize_);
8980 0 : return HCCL_SUCCESS;
8981 : }
8982 4 : HcclResult HcclCommunicator::IndOpTransportAlloc(const std::string &tag, OpCommTransport &opCommTransport,
8983 : TransportIOMem& transMem, bool isAicpuModeEn)
8984 : {
8985 : // Aicpu侧不支持用户注册额外内存
8986 4 : if (isAicpuModeEn) {
8987 3 : if (transMem.indOpMem.userDeviceMem.size() > 0 ||
8988 1 : transMem.indOpMem.userHostMem.size() > 0) {
8989 2 : HCCL_ERROR("[%s] AICPU engine does not support user-registered memory", __func__);
8990 2 : return HCCL_E_NOT_SUPPORT;
8991 : }
8992 : }
8993 :
8994 2 : StateGuard<HcclCommunicator, HcclCommState> guard(this, HcclCommState::BUILDING);
8995 2 : CHK_PTR_NULL(indptOpTransportManager_);
8996 1 : bool isIndOp = true;
8997 1 : HcclResult ret = indptOpTransportManager_->Alloc(tag, transMem, opCommTransport, isAicpuModeEn, false, false,
8998 1 : HcclCMDType::HCCL_CMD_INVALID, false, isIndOp);
8999 1 : if (ret != HCCL_SUCCESS) {
9000 1 : HCCL_ERROR("[%s] Failed to alloc transport, tag[%s], isAicpuModeEn[%d], ret[%d]",
9001 : __func__, tag.c_str(), isAicpuModeEn, ret);
9002 1 : return ret;
9003 : }
9004 :
9005 0 : HCCL_RUN_INFO("[%s] Alloc transport success, tag[%s], isAicpuModeEn[%d], ret[%d]",
9006 : __func__, tag.c_str(), isAicpuModeEn, ret);
9007 0 : return HCCL_SUCCESS;
9008 2 : }
9009 :
9010 234 : HcclTopoAttr HcclCommunicator::GetTopoAttr()
9011 : {
9012 234 : HcclTopoAttr topoAttr;
9013 234 : attrCollector_.GetTopoAttr(topoAttr);
9014 234 : return topoAttr;
9015 0 : }
9016 :
9017 102 : HcclResult HcclCommunicator::GetHDCommunicate(HDCommunicateParams &kfcControlTransferH2DParams,
9018 : HDCommunicateParams &kfcStatusTransferD2HParams)
9019 : {
9020 102 : if (GetSupportHDCommunicate() == false) {
9021 101 : HCCL_WARNING("%s not support HDCommunicate, skip", __func__);
9022 101 : return HCCL_SUCCESS;
9023 : }
9024 1 : CHK_SMART_PTR_NULL(kfcControlTransferH2D_);
9025 1 : CHK_SMART_PTR_NULL(kfcStatusTransferD2H_);
9026 1 : kfcControlTransferH2DParams = kfcControlTransferH2D_->GetCommunicateParams();
9027 1 : kfcStatusTransferD2HParams = kfcStatusTransferD2H_->GetCommunicateParams();
9028 1 : HCCL_INFO("%s success, group[%s]", __func__, identifier_.c_str());
9029 1 : return HCCL_SUCCESS;
9030 : }
9031 :
9032 234 : HcclResult HcclCommunicator::SetGetAicpuCommState(std::function<bool()> getAicpuCommState)
9033 : {
9034 234 : getAicpuCommState_ = getAicpuCommState;
9035 234 : HCCL_DEBUG("%s success, group[%s]", __func__, identifier_.c_str());
9036 234 : return HCCL_SUCCESS;
9037 : }
9038 :
9039 0 : HcclResult HcclCommunicator::CommGetNetLayers(uint32_t **netLayers, uint32_t *netLayerNum)
9040 : {
9041 0 : if (deviceType_ == DevType::DEV_TYPE_910_93) {
9042 0 : netLayer_[0] = static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0);
9043 0 : netLayer_[1] = static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L1);
9044 0 : *netLayerNum = COMM_LAYER_NUM_MAX;
9045 0 : } else if (deviceType_ == DevType::DEV_TYPE_910B || deviceType_ == DevType::DEV_TYPE_310P3) {
9046 0 : netLayer_[0] =static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0);
9047 0 : *netLayerNum = 1;
9048 : }
9049 0 : *netLayers = netLayer_;
9050 0 : return HCCL_SUCCESS;
9051 : }
9052 :
9053 0 : HcclResult HcclCommunicator::CommGetInstSizeByNetLayer(uint32_t netLayer, uint32_t *rankNum)
9054 : {
9055 0 : if ((netLayer == static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)) ||
9056 : (netLayer == static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L1))) {
9057 0 : *rankNum = userRankSize_;
9058 : }
9059 0 : return HCCL_SUCCESS;
9060 : }
9061 :
9062 0 : HcclResult HcclCommunicator::CommGetInstTopoTypeByNetLayer(uint32_t netLayer, u32 *topoType)
9063 : {
9064 0 : if (deviceType_ == DevType::DEV_TYPE_910_93) {
9065 0 : if (netLayer == static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)) {
9066 0 : *topoType = HCCL_ALG_SWITCH | HCCL_ALG_RING;
9067 0 : } else if (netLayer == static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L1)) {
9068 0 : *topoType = HCCL_ALG_RING;
9069 : }
9070 0 : } else if (deviceType_ == DevType::DEV_TYPE_910B) {
9071 0 : if (netLayer == static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)) {
9072 0 : *topoType = HCCL_ALG_MESH;
9073 : }
9074 0 : } else if (deviceType_ == DevType::DEV_TYPE_310P3) {
9075 0 : if (netLayer == static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)) {
9076 0 : *topoType = HCCL_ALG_RING;
9077 : }
9078 : }
9079 0 : return HCCL_SUCCESS;
9080 : }
9081 :
9082 0 : HcclResult HcclCommunicator::GetNetLayers(uint32_t **netLayers, uint32_t *netLayerNum)
9083 : {
9084 0 : return rankGraph_.GetNetLayers(netLayers, netLayerNum);
9085 : }
9086 :
9087 0 : HcclResult HcclCommunicator::GetInstSizeByNetLayer(uint32_t netLayer, uint32_t *rankNum)
9088 : {
9089 0 : return rankGraph_.GetInstSizeByNetLayer(netLayer, rankNum);
9090 : }
9091 :
9092 520 : HcclResult HcclCommunicator::GetInstTopoTypeByNetLayer(uint32_t netLayer, CommTopo *topoType)
9093 : {
9094 520 : return rankGraph_.GetInstTopoTypeByNetLayer(netLayer, topoType);
9095 : }
9096 :
9097 0 : HcclResult HcclCommunicator::GetInstRanksByNetLayer(uint32_t netLayer, uint32_t **rankList, uint32_t *rankNum)
9098 : {
9099 0 : return rankGraph_.GetInstRanksByNetLayer(netLayer, rankList, rankNum);
9100 : }
9101 :
9102 0 : HcclResult HcclCommunicator::GetInstSizeListByNetLayer(uint32_t netLayer, uint32_t **instSizeList, uint32_t *listSize)
9103 : {
9104 0 : return rankGraph_.GetInstSizeListByNetLayer(netLayer, instSizeList, listSize);
9105 : }
9106 :
9107 0 : HcclResult HcclCommunicator::GetTopoInstsByLayer(uint32_t netLayer, uint32_t **topoInsts, uint32_t *topoInstNum)
9108 : {
9109 0 : return rankGraph_.GetTopoInstsByLayer(netLayer, topoInsts, topoInstNum);
9110 : }
9111 :
9112 0 : HcclResult HcclCommunicator::GetTopoType(uint32_t netLayer, uint32_t topoInstId, CommTopo *topoType)
9113 : {
9114 0 : return rankGraph_.GetTopoType(netLayer, topoType);
9115 : }
9116 :
9117 0 : HcclResult HcclCommunicator::GetRanksByTopoInst(uint32_t netLayer, uint32_t topoInstId, uint32_t **ranks, uint32_t *rankNum)
9118 : {
9119 0 : return rankGraph_.GetRanksByTopoInst(netLayer, topoInstId, ranks, rankNum);
9120 : }
9121 :
9122 0 : HcclResult HcclCommunicator::GetEndpointNum(uint32_t netLayer, uint32_t topoInstId, uint32_t *num)
9123 : {
9124 0 : return rankGraph_.GetEndpointNum(netLayer, topoInstId, num);
9125 : }
9126 :
9127 0 : HcclResult HcclCommunicator::GetEndpointDesc(uint32_t netLayer, uint32_t topoInstId, uint32_t *descNum, EndpointDesc *endpointDesc)
9128 : {
9129 0 : return rankGraph_.GetEndpointDesc(netLayer, topoInstId, descNum, endpointDesc);
9130 : }
9131 :
9132 0 : HcclResult HcclCommunicator::GetEndpointInfo(uint32_t rankId, const EndpointDesc *endPointDesc, EndpointAttr endpointAttr,
9133 : uint32_t infoLen, void *info)
9134 : {
9135 0 : return rankGraph_.GetEndpointInfo(rankId, endPointDesc, endpointAttr, infoLen, info);
9136 : }
9137 :
9138 0 : HcclResult HcclCommunicator::GetRankGraph(GraphType type, void **graph, uint32_t *len)
9139 : {
9140 0 : return rankGraph_.GetRankGraphInfo(type, graph, len);
9141 : }
9142 :
9143 0 : void* HcclCommunicator::GetRankGraphV1()
9144 : {
9145 0 : return static_cast<void*>(&rankGraph_);
9146 : }
9147 :
9148 0 : HcclResult HcclCommunicator::GetLinks(uint32_t netLayer, uint32_t srcRank, uint32_t dstRank,
9149 : CommLink **linkList, uint32_t *listSize)
9150 : {
9151 0 : return rankGraph_.GetLinks(netLayer, srcRank, dstRank, linkList, listSize);
9152 : }
9153 :
9154 0 : HcclResult HcclCommunicator::GetHeterogMode(HcclHeterogMode *mode)
9155 : {
9156 0 : return rankGraph_.GetHeterogMode(mode);
9157 : }
9158 :
9159 520 : HcclResult HcclCommunicator::RegisterToSnapshot()
9160 : {
9161 520 : if (deviceType_ != DevType::DEV_TYPE_910B && deviceType_ != DevType::DEV_TYPE_910_93) {
9162 324 : return HCCL_SUCCESS;
9163 : }
9164 196 : if (userRankSize_ <= 1) {
9165 2 : HCCL_RUN_INFO("[HcclCommunicator][RegisterToSnapshot]comm identifier[%s], deviceLogicId[%d], "
9166 : "rank size[%u] is no greater than 1, and then will not register to snapshot",
9167 : identifier_.c_str(), deviceLogicId_, userRankSize_);
9168 2 : return HCCL_SUCCESS;
9169 : }
9170 0 : auto setInvalidCommCallback = [this](bool isInvalid) {
9171 0 : return this->SetInvalidComm(isInvalid);
9172 194 : };
9173 0 : auto preProcessCallback = [this]() {
9174 0 : return this->SnapshotCheckPreProcess();
9175 194 : };
9176 0 : auto postProcessCallback = [this]() {
9177 0 : return this->SnapshotCheckPostProcess();
9178 194 : };
9179 194 : CHK_RET(SnapshotControl::GetInstance(deviceLogicId_).RegisterComm(identifier_, setInvalidCommCallback,
9180 : preProcessCallback, postProcessCallback));
9181 194 : if (IsEnableBackupLink()) {
9182 0 : CHK_RET(SnapshotControl::GetInstance(deviceLogicId_).RegisterBackup(identifier_, deviceBackUpPhyId_));
9183 : }
9184 194 : return HCCL_SUCCESS;
9185 : }
9186 :
9187 212 : HcclResult HcclCommunicator::UnRegisterFromSnapshot()
9188 : {
9189 212 : if (deviceType_ != DevType::DEV_TYPE_910B && deviceType_ != DevType::DEV_TYPE_910_93) {
9190 0 : return HCCL_SUCCESS;
9191 : }
9192 212 : if (userRankSize_ <= 1) {
9193 3 : HCCL_RUN_INFO("[HcclCommunicator][UnRegisterFromSnapshot]comm identifier[%s], deviceLogicId[%d], "
9194 : "rank size[%u] is no greater than 1, and then will not unregister from snapshot",
9195 : identifier_.c_str(), deviceLogicId_, userRankSize_);
9196 3 : return HCCL_SUCCESS;
9197 : }
9198 209 : CHK_RET(SnapshotControl::GetInstance(deviceLogicId_).UnRegisterComm(identifier_));
9199 209 : if (IsEnableBackupLink()) {
9200 0 : CHK_RET(SnapshotControl::GetInstance(deviceLogicId_).UnRegisterBackup(identifier_, deviceBackUpPhyId_));
9201 : }
9202 209 : return HCCL_SUCCESS;
9203 : }
9204 :
9205 0 : HcclResult HcclCommunicator::SetInvalidComm(bool isInvalid) {
9206 0 : isInvalidComm_ = isInvalid;
9207 0 : HCCL_INFO("[HcclCommunicator][SetInvalidComm] comm[%s] is set to invalid, rank[%u], deviceLogicId[%d]",
9208 : identifier_.c_str(), userRank_, deviceLogicId_);
9209 0 : return HCCL_SUCCESS;
9210 : }
9211 :
9212 0 : HcclResult HcclCommunicator::SnapshotCheckPreProcess()
9213 : {
9214 0 : bool errorFlag = false;
9215 0 : auto pauseTimeout = std::chrono::seconds(GetExternalInputHcclLinkTimeOut());
9216 0 : auto startTime = std::chrono::steady_clock::now();
9217 : while (true) {
9218 0 : CHK_PRT_BREAK(Heartbeat::GetInstance(deviceLogicId_).IsPaused(),
9219 : HCCL_INFO("[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9220 : "heartbeat thread has been paused.", identifier_.c_str(), userRank_, deviceLogicId_),);
9221 0 : CHK_PRT_BREAK((std::chrono::steady_clock::now() - startTime) >= pauseTimeout,
9222 : HCCL_ERROR("[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9223 : "pause heartbeat thread timeout[%u s].",
9224 : identifier_.c_str(), userRank_, deviceLogicId_, GetExternalInputHcclLinkTimeOut()), errorFlag = true);
9225 : }
9226 0 : startTime = std::chrono::steady_clock::now();
9227 0 : while (retryEnable_ && opRetryManager_) {
9228 0 : CHK_PRT_BREAK(opRetryManager_->IsPaused(identifier_),
9229 : HCCL_INFO("[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9230 : "opretry threads have been paused.", identifier_.c_str(), userRank_, deviceLogicId_),);
9231 0 : CHK_PRT_BREAK((std::chrono::steady_clock::now() - startTime) >= pauseTimeout,
9232 : HCCL_ERROR("[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9233 : "pause opretry threads timeout[%u s].",
9234 : identifier_.c_str(), userRank_, deviceLogicId_, GetExternalInputHcclLinkTimeOut()), errorFlag = true);
9235 : }
9236 0 : startTime = std::chrono::steady_clock::now();
9237 0 : while (zeroCopyMemoryAgent_) {
9238 0 : CHK_PRT_BREAK(zeroCopyMemoryAgent_->IsPaused(),
9239 : HCCL_INFO("[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9240 : "zero-copy memory agent thread has been paused.", identifier_.c_str(), userRank_, deviceLogicId_),);
9241 0 : CHK_PRT_BREAK((std::chrono::steady_clock::now() - startTime) >= pauseTimeout,
9242 : HCCL_ERROR("[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9243 : "pause zero-copy memory agent thread timeout[%u s].",
9244 : identifier_.c_str(), userRank_, deviceLogicId_, GetExternalInputHcclLinkTimeOut()), errorFlag = true);
9245 : }
9246 0 : CHK_PRT_RET(errorFlag, HCCL_ERROR("[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], "
9247 : "deviceLogicId[%d], snapshot pre-process fail due to some background threads pause timeout, please check.",
9248 : identifier_.c_str(), userRank_, deviceLogicId_), HCCL_E_INTERNAL);
9249 0 : HCCL_INFO("[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9250 : "snapshot pre-process success.", identifier_.c_str(), userRank_, deviceLogicId_);
9251 0 : return HCCL_SUCCESS;
9252 : }
9253 :
9254 0 : HcclResult HcclCommunicator::SnapshotCheckPostProcess()
9255 : {
9256 0 : bool errorFlag = false;
9257 0 : auto resumeTimeout = std::chrono::seconds(GetExternalInputHcclLinkTimeOut());
9258 0 : auto startTime = std::chrono::steady_clock::now();
9259 : while (true) {
9260 0 : CHK_PRT_BREAK(Heartbeat::GetInstance(deviceLogicId_).IsResumed(),
9261 : HCCL_INFO("[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9262 : "heartbeat thread has been resumed.", identifier_.c_str(), userRank_, deviceLogicId_),);
9263 0 : CHK_PRT_BREAK((std::chrono::steady_clock::now() - startTime) >= resumeTimeout,
9264 : HCCL_ERROR("[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9265 : "resume heartbeat thread timeout[%u s].",
9266 : identifier_.c_str(), userRank_, deviceLogicId_, GetExternalInputHcclLinkTimeOut()), errorFlag = true);
9267 : }
9268 0 : startTime = std::chrono::steady_clock::now();
9269 0 : while (retryEnable_ && opRetryManager_) {
9270 0 : CHK_PRT_BREAK(opRetryManager_->IsResumed(identifier_),
9271 : HCCL_INFO("[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9272 : "opretry threads have been resumed.", identifier_.c_str(), userRank_, deviceLogicId_),);
9273 0 : CHK_PRT_BREAK((std::chrono::steady_clock::now() - startTime) >= resumeTimeout,
9274 : HCCL_ERROR("[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9275 : "resume opretry threads timeout[%u s].",
9276 : identifier_.c_str(), userRank_, deviceLogicId_, GetExternalInputHcclLinkTimeOut()), errorFlag = true);
9277 : }
9278 0 : startTime = std::chrono::steady_clock::now();
9279 0 : while (zeroCopyMemoryAgent_) {
9280 0 : CHK_PRT_BREAK(zeroCopyMemoryAgent_->IsResumed(),
9281 : HCCL_INFO("[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9282 : "zero-copy memory agent thread has been resumed.", identifier_.c_str(), userRank_, deviceLogicId_),);
9283 0 : CHK_PRT_BREAK((std::chrono::steady_clock::now() - startTime) >= resumeTimeout,
9284 : HCCL_ERROR("[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9285 : "resume zero-copy memory agent thread timeout[%u s].",
9286 : identifier_.c_str(), userRank_, deviceLogicId_, GetExternalInputHcclLinkTimeOut()), errorFlag = true);
9287 : }
9288 0 : CHK_PRT_RET(errorFlag, HCCL_ERROR("[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], "
9289 : "deviceLogicId[%d], snapshot post-process check fail due to some background threads resume timeout, "
9290 : "please check.", identifier_.c_str(), userRank_, deviceLogicId_), HCCL_E_INTERNAL);
9291 0 : HCCL_INFO("[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9292 : "snapshot post-process check success.", identifier_.c_str(), userRank_, deviceLogicId_);
9293 0 : return HCCL_SUCCESS;
9294 : }
9295 :
9296 403 : void HcclCommunicator::SetReleaseChannel(std::function<HcclResult()> releaseChannel)
9297 : {
9298 403 : releaseChannel_ = releaseChannel;
9299 403 : return;
9300 : }
9301 :
9302 234 : CCLBufferManager& HcclCommunicator::GetCCLbufferManager()
9303 : {
9304 234 : return cclBufferManager_;
9305 : }
9306 :
9307 0 : void HcclCommunicator::SetHcclQos(u32 hcclQos)
9308 : {
9309 0 : HCCL_INFO("[HcclCommunicator][host][SetHcclQos] hcclQos[%u]", hcclQos);
9310 0 : hcclQos_ = hcclQos;
9311 0 : }
9312 :
9313 0 : u32 HcclCommunicator::GetHcclQos()
9314 : {
9315 0 : HCCL_INFO("[HcclCommunicator][host][GetHcclQos] hcclQos[%u]", hcclQos_);
9316 0 : return hcclQos_;
9317 : }
9318 :
9319 529 : HcclResult HcclCommunicator::InitSymmetricMemory()
9320 : {
9321 529 : if (superPodNum_ > 1) {
9322 13 : HCCL_DEBUG("[InitSymmetricMemory] Cross-SuperNode not support symmetric memory");
9323 13 : return HCCL_SUCCESS;
9324 : }
9325 516 : if (deviceType_ != DevType::DEV_TYPE_910_93) {
9326 488 : HCCL_DEBUG("[%s] deviceType:%d not support symmetric memory", __func__, deviceType_);
9327 488 : return HCCL_SUCCESS;
9328 : }
9329 :
9330 28 : u64 stride = commConfig_.GetConfigSymmetricMemoryStride() * GIGABYTE_TO_BYTE;
9331 28 : HCCL_RUN_INFO("InitSymmetricMemory, comm identifier[%s], userRank[%u], userRankSize[%u], stride[%llu], devicePhyId[%u].",
9332 : identifier_.c_str(), realUserRank_, userRankSize_, stride, devicePhyId_);
9333 :
9334 28 : symmetricMemoryAgent_ = std::make_shared<SymmetricMemoryAgent>(socketManager_, devicePhyId_,
9335 28 : deviceLogicId_, localVnicIp_, rankInfoList_, realUserRank_, useSuperPodMode_, identifier_);
9336 28 : CHK_SMART_PTR_NULL(symmetricMemoryAgent_);
9337 :
9338 28 : symmetricMemory_ = std::make_unique<SymmetricMemory>(realUserRank_, userRankSize_, stride, symmetricMemoryAgent_);
9339 28 : CHK_SMART_PTR_NULL(symmetricMemory_);
9340 28 : return HCCL_SUCCESS;
9341 : }
9342 :
9343 0 : HcclResult HcclCommunicator::RegisterWindow(void* ptr, size_t size, HcclCommSymWindow *winHandle)
9344 : {
9345 0 : CHK_PRT_RET(superPodNum_ > 1,
9346 : HCCL_ERROR("[RegisterWindow] Cross-SuperNode not support symmetric memory"), HCCL_E_NOT_SUPPORT);
9347 :
9348 0 : CHK_PRT_RET(deviceType_ != DevType::DEV_TYPE_910_93,
9349 : HCCL_ERROR("[%s] deviceType:%d not support symmetric memory", __func__, deviceType_), HCCL_E_NOT_SUPPORT);
9350 :
9351 0 : CHK_SMART_PTR_NULL(symmetricMemory_);
9352 0 : return symmetricMemory_->RegisterSymmetricMem(ptr, size, winHandle);
9353 : }
9354 :
9355 0 : HcclResult HcclCommunicator::DeregisterWindow(HcclCommSymWindow winHandle)
9356 : {
9357 0 : CHK_SMART_PTR_NULL(symmetricMemory_);
9358 0 : return symmetricMemory_->DeregisterSymmetricMem(winHandle);
9359 : }
9360 :
9361 0 : HcclResult HcclCommunicator::GetCommSymWin(void* ptr, size_t size, HcclCommSymWindow *winHandle, size_t *offset)
9362 : {
9363 0 : CHK_SMART_PTR_NULL(symmetricMemory_);
9364 0 : return symmetricMemory_->FindSymmetricWindow(ptr, size, winHandle, reinterpret_cast<u64*>(offset));
9365 : }
9366 :
9367 22 : bool HcclCommunicator::EnableAicpuUnfold(bool isCapture)
9368 : {
9369 22 : if (deviceType_ != DevType::DEV_TYPE_910_93 && deviceType_ != DevType::DEV_TYPE_910B) {
9370 3 : return false;
9371 : }
9372 : // 910B在acl graph场景(isCapture)不启用aicpu展开
9373 19 : if (deviceType_ == DevType::DEV_TYPE_910B && isCapture) {
9374 3 : HCCL_INFO("[%s] deviceType[%d] isCapture[1], aicpuUnfoldConfig[%u] 910B does not support aicpuUnfold in acl graph mode",
9375 : __func__, deviceType_, GetAicpuUnfoldConfig());
9376 3 : return false;
9377 : }
9378 16 : HCCL_INFO("[%s] aicpuUnfoldConfig[%u]", __func__, GetAicpuUnfoldConfig());
9379 15 : return GetAicpuUnfoldConfig();
9380 : }
9381 :
9382 234 : aclrtBinHandle HcclCommunicator::GetBinHandle() {
9383 234 : if (binHandle_ == nullptr) {
9384 234 : HCCL_ERROR("[HcclCommunicator][GetBinHandle] GetBinHandle binHandle failed.binHandle is nullptr");
9385 234 : return nullptr;
9386 : }
9387 0 : return binHandle_;
9388 : }
9389 0 : HcclResult HcclCommunicator::GetDevMemWorkSpace(const std::string &memTag, uint64_t *size, void **addr,
9390 : bool *newCreated)
9391 : {
9392 0 : return dpuManager_->GetDevMemWorkSpace(memTag, size, addr, newCreated);
9393 : }
9394 : }
|