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 :
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 : static std::mutex g_hcomInitMutex;
57 : static std::atomic<u32> g_enableBackupLinkCommCount{0}; // 开启借轨的通信域计数
58 : constexpr u32 MEMORY_CAPACITY = 256 * 1024;
59 : constexpr u32 WAIT_PREPARE_SLEEP_TIME = 5000;
60 : constexpr u32 SINGLE_SERVER_NUM = 1;
61 : constexpr u32 CONN_LIMIT = 4096;
62 : constexpr u32 COMM_DEV_TYPE_DIGIT_NUM = 8;
63 : constexpr u32 TILINGDATA_BUF_SIZE = 32 * 1024; // 单位:字节
64 : constexpr u32 ALLTOALL_INFO_MATRIX_SIZE = 4;
65 : constexpr u32 AICPU_RETRY_LINKROCE_DEFAULT = 0;
66 : constexpr u32 AICPU_RETRY_LINKROCE_BACKUP = 1;
67 : constexpr u32 SINGLE_PROCESS_MIN_PORT = 1024;
68 : constexpr u32 SINGLE_PROCESS_MAX_PORT = 65535;
69 : constexpr u32 TYPE_USER_MEM = 1;
70 : constexpr u32 NON_BATCH_WRITE_MAX_STREAM_NUM = 19U;
71 : constexpr u64 GIGABYTE_TO_BYTE = 1024ULL * 1024ULL * 1024ULL;
72 : constexpr u8 AICPU_ORDERLAUNCH_INVALID_HCOM_MODE = 255; // 图模式下无附属从流,不进行按序下发
73 : enum class TransferMemInfoIdx : u32 {
74 : TRANSFER_MEM_INFO_KEY_IDX = 0,
75 : TRANSFER_MEM_INFO_VALUE_IDX = 1,
76 : TRANSFER_MEM_INFO_RDMA_ENVELOPE_IDX = 2,
77 : TRANSFER_MEM_INFO_IDX_NUM = 3
78 : };
79 :
80 : enum class AicpuLocalNotifyIdx : u32 {
81 : // host-aicpu同步
82 : HOST_TO_AICPU_0 = 0,
83 : HOST_TO_AICPU_1 = 1,
84 :
85 : // 用于控制单算子模式各通信域kernel按序占核的notify
86 : ORDER_INDEX_OPBASE_0 = 2, // host_order流 record, kernel流 wait
87 : ORDER_INDEX_OPBASE_1 = 3, // aicpu_order流 record, host_order流 wait
88 :
89 : // 用于控制Aclgraph模式各通信域kernel按序占核的notify
90 : ORDER_INDEX_ACLGRAPH_0 = 4, // host_order流 record, kernel流 wait
91 : ORDER_INDEX_ACLGRAPH_1 = 5, // aicpu_order流 record, host_order流 wait
92 :
93 : // 用于控制图模式各通信域kernel按序占核的notify
94 : ORDER_INDEX_HCOM_0 = 6, // host_order流 record, kernel流 wait
95 : ORDER_INDEX_HCOM_1 = 7 // aicpu_order流 record, host_order流 wait
96 : };
97 :
98 : enum class AicpuLocalEventIdx : u32 {
99 : /**
100 : *@brief 用于控制Aclgraph模式按序下发控制流入图的event
101 : *@note
102 : *通信域绑定Context,而Stream是Context管理的资源,因此对应下在Stream上的event与通信域强相关,需要communicator管理
103 : **/
104 : ORDER_INDEX_ACLGRAPH_EVENT_0 = 0, // kernel流 record, host_order流 wait
105 : ORDER_INDEX_ACLGRAPH_EVENT_1 = 1, // host_order流 record, kernel流 wait
106 :
107 : // 用于Aclgraph模式kernel流入图的event
108 : KERNEL_INDEX_ACLGRAPH_EVENT_0 = 2, // kernel流 record, hccl主流 wait
109 : KERNEL_INDEX_ACLGRAPH_EVENT_1 = 3, // hccl主流 record, kernel流 wait
110 : };
111 :
112 395 : HcclCommunicator::HcclCommunicator()
113 395 : : dispatcher_(nullptr),
114 395 : vDispatcher_(nullptr),
115 395 : notifyPool_(nullptr),
116 395 : initializedFlag_(ATOMIC_FLAG_INIT),
117 395 : userRank_(INVALID_VALUE_RANKID),
118 395 : realUserRank_(INVALID_VALUE_RANKID),
119 395 : userRankSize_(INVALID_VALUE_RANKSIZE),
120 395 : drvInit_(false),
121 395 : inlineReduceSwitchOn_(true),
122 395 : nicDeployment_(NICDeployment::NIC_DEPLOYMENT_DEVICE),
123 395 : devicePhyId_(INVALID_UINT),
124 395 : deviceLogicId_(-1),
125 395 : localRank_(INVALID_VALUE_RANKID),
126 395 : hostSocketHandle_(nullptr),
127 395 : isUsedRdmaLevel0_(false),
128 395 : nicInitialized_(0),
129 395 : hcomGroupNicInit_(false),
130 395 : profilingMode_(HcomProfilingMode::PROFILING_CLOSE),
131 395 : raResourceInit_(false),
132 395 : interServer_(false),
133 395 : isSingleMeshAggregation_(false),
134 395 : cclBufferManager_(CCLBufferManager()),
135 395 : isExecuteProfilingInit_(false),
136 395 : deviceType_(DevType::DEV_TYPE_COUNT),
137 395 : commHandle_(nullptr),
138 395 : commWorkMode_(WorkMode::HCCL_MODE_NORMAL),
139 395 : meshAggregationRankSize_(0),
140 395 : isHaveCpuRank_(false),
141 395 : ranktableCrc_(0),
142 395 : multiModuleDiffDeviceNumMode_(false),
143 395 : multiSuperPodDiffServerNumMode_(false),
144 395 : multiSuperPodDiffDeviceNumMode_(false),
145 395 : isStandardCard_(false),
146 395 : is310PDuoCard_(false),
147 395 : hccsPortNum_(-1),
148 395 : loopBackIp_(HcclIpAddress(COMM_LOOPBACK_IP)),
149 395 : profilingInitiated_(false),
150 395 : callbackThreadId_(INVALID_U64),
151 395 : role_(SERVER_ROLE_SOCKET),
152 395 : isHostUseDevNic_(false),
153 395 : isAllRankSamePlane_(false),
154 395 : serverNum_(0),
155 304545 : moduleNum_(0)
156 : {
157 395 : zeroCopyAclGraph_.reset(new (std::nothrow) ZeroCopyAclGraph());
158 395 : if (zeroCopyAclGraph_ == nullptr) {
159 0 : HCCL_ERROR("new ZeroCopyAclGraph failed!");
160 : }
161 395 : commConfig_ = CommConfig();
162 395 : dpuManager_.reset(new (std::nothrow) DpuManager());
163 395 : if (dpuManager_ == nullptr) {
164 0 : HCCL_ERROR("new DpuManager failed!");
165 : }
166 395 : }
167 :
168 414 : HcclCommunicator::HcclCommunicator(const CommConfig& commConfig)
169 414 : : dispatcher_(nullptr),
170 414 : vDispatcher_(nullptr),
171 414 : notifyPool_(nullptr),
172 414 : initializedFlag_(ATOMIC_FLAG_INIT),
173 414 : userRank_(INVALID_VALUE_RANKID),
174 414 : realUserRank_(INVALID_VALUE_RANKID),
175 414 : userRankSize_(INVALID_VALUE_RANKSIZE),
176 414 : drvInit_(false),
177 414 : inlineReduceSwitchOn_(true),
178 414 : nicDeployment_(NICDeployment::NIC_DEPLOYMENT_DEVICE),
179 414 : devicePhyId_(INVALID_UINT),
180 414 : deviceLogicId_(-1),
181 414 : localRank_(INVALID_VALUE_RANKID),
182 414 : hostSocketHandle_(nullptr),
183 414 : isUsedRdmaLevel0_(false),
184 414 : nicInitialized_(0),
185 414 : hcomGroupNicInit_(false),
186 414 : profilingMode_(HcomProfilingMode::PROFILING_CLOSE),
187 414 : raResourceInit_(false),
188 414 : interServer_(false),
189 414 : isSingleMeshAggregation_(false),
190 414 : cclBufferManager_(CCLBufferManager()),
191 414 : isExecuteProfilingInit_(false),
192 414 : deviceType_(DevType::DEV_TYPE_COUNT),
193 414 : commHandle_(nullptr),
194 414 : commWorkMode_(WorkMode::HCCL_MODE_NORMAL),
195 414 : meshAggregationRankSize_(0),
196 414 : isHaveCpuRank_(false),
197 414 : ranktableCrc_(0),
198 414 : multiModuleDiffDeviceNumMode_(false),
199 414 : multiSuperPodDiffServerNumMode_(false),
200 414 : isStandardCard_(false),
201 414 : is310PDuoCard_(false),
202 414 : hccsPortNum_(-1),
203 414 : loopBackIp_(HcclIpAddress(COMM_LOOPBACK_IP)),
204 414 : profilingInitiated_(false),
205 414 : callbackThreadId_(INVALID_U64),
206 414 : role_(SERVER_ROLE_SOCKET),
207 413 : isHostUseDevNic_(false),
208 414 : isAllRankSamePlane_(false),
209 414 : serverNum_(0),
210 319194 : moduleNum_(0)
211 : {
212 414 : zeroCopyAclGraph_.reset(new (std::nothrow) ZeroCopyAclGraph());
213 414 : if (zeroCopyAclGraph_ == nullptr) {
214 0 : HCCL_ERROR("new ZeroCopyAclGraph failed!");
215 : }
216 414 : commConfig_ = commConfig;
217 413 : dpuManager_.reset(new (std::nothrow) DpuManager());
218 414 : if (dpuManager_ == nullptr) {
219 0 : HCCL_ERROR("new DpuManager failed!");
220 : }
221 414 : }
222 :
223 7815 : HcclCommunicator::~HcclCommunicator()
224 : {
225 809 : HCCL_DEBUG("Enter ~HcclCommunicator.");
226 :
227 809 : DeinitZeroCopyMemoryAgent(true);
228 809 : if (!isInvalidComm_) {
229 809 : (void)DestroyAicpuComm();
230 809 : (void)UnRegisterBackGroundThread();
231 : } else {
232 0 : HCCL_WARNING(
233 : "The comm[%s] is invalid in snapshot, rank[%u]. deviceLogicId[%u]. "
234 : "There is no aicpu comm in device, skip aicpu comm destroy in destructor.",
235 : identifier_.c_str(), userRank_, deviceLogicId_);
236 : }
237 :
238 808 : if (dpuManager_ != nullptr) {
239 808 : (void)dpuManager_->DeInitDpuKernel();
240 809 : dpuManager_ = nullptr;
241 : }
242 :
243 807 : UnRegisterToHeartBeat();
244 806 : DeleteOpInfoToHeartBeat();
245 808 : AlgWrap::GetInstance().UnregisterAlgCallBack(identifier_);
246 809 : DetectConnectionAnomalies::GetInstance(deviceLogicId_).Deinit();
247 809 : UnRegisterToCommConfiger();
248 809 : AclgraphCallback::GetInstance().CleanCaptureRes(this);
249 :
250 809 : if (zeroCopyAclGraph_ != nullptr) {
251 809 : zeroCopyAclGraph_ = nullptr;
252 : }
253 :
254 808 : if (implAlg_ != nullptr) {
255 526 : implAlg_ = nullptr;
256 : }
257 :
258 882 : for (auto& res : resMap_) {
259 74 : DestroyAlgResource(res.second);
260 : }
261 :
262 809 : if (releaseChannel_ != nullptr) {
263 404 : releaseChannel_();
264 : }
265 :
266 809 : if (opRetryManager_ != nullptr) {
267 0 : OpRetryManager::DeleteLinkInfoByIdentifier(deviceLogicId_, identifier_);
268 0 : opRetryManager_->UnRegisterOpRetryManager(identifier_);
269 0 : opRetryManager_ = nullptr;
270 : }
271 :
272 809 : if (IsEnableBackupLink()) {
273 0 : if (g_enableBackupLinkCommCount.load() == 0) {
274 0 : HCCL_ERROR("[Destroy] g_enableBackupLinkCommCount is 0");
275 : } else {
276 0 : g_enableBackupLinkCommCount--;
277 : }
278 : }
279 :
280 809 : resMap_.clear();
281 808 : deviceResOrigMem_.clear();
282 809 : hostResMap_.clear();
283 809 : tagCommInfo_.clear();
284 808 : tagWorkSpaceMem_.clear();
285 806 : tagStreamInfo_.clear();
286 :
287 807 : if (opRetryStreamPtr_ != nullptr) {
288 521 : opRetryStreamPtr_->clear();
289 520 : opRetryStreamPtr_ = nullptr;
290 : }
291 :
292 807 : OrderLaunch::GetInstance(deviceLogicId_).UnRegisterOrderLaunch(identifier_);
293 4040 : for (u32 i = 0; i < AICPU_LOCAL_EVENT_SIZE; ++i) {
294 3231 : if (localAicpuOpEvent_[i] != nullptr) {
295 0 : (void)hrtEventDestroy(localAicpuOpEvent_[i]);
296 0 : localAicpuOpEvent_[i] = nullptr;
297 : }
298 : }
299 :
300 809 : (void)UnRegistTaskExceptionHandler();
301 811 : for (auto streamId : aicpuStreamIds_) {
302 2 : UnregisterGetAicpuTaskExceptionCallBack(streamId, deviceLogicId_);
303 : }
304 808 : aicpuStreamIds_.clear();
305 808 : kfcControlTransferH2D_ = nullptr;
306 809 : kfcStatusTransferD2H_ = nullptr;
307 809 : customControlTransferH2D_ = nullptr;
308 809 : customStatusTransferD2H_ = nullptr;
309 :
310 809 : oneSideService_ = nullptr;
311 809 : if (isOneSidedServiceNetDevCtxInited) {
312 0 : DeInitOneSidedServiceNetDevCtx();
313 : }
314 :
315 : /* 网络资源销毁 */
316 809 : DestroyNetworkResources();
317 809 : notifyPool_ = nullptr;
318 809 : queueNotifyManager_ = nullptr;
319 : /* driver关联资源释放 */
320 809 : if (drvInit_) {
321 307 : if (DisablePreResource() != HCCL_SUCCESS) {
322 0 : HCCL_WARNING("driver resource is not released successfully");
323 : }
324 : }
325 :
326 808 : if (isExecuteProfilingInit_) {
327 490 : (void)DeinitProfiling();
328 : }
329 :
330 809 : if (OpExeCounter::GetInstance(deviceLogicId_).DeInitCounter() != HCCL_SUCCESS) {
331 0 : HCCL_WARNING("op exec counter resource free failed");
332 : }
333 :
334 : /* 销毁当前trace句柄 */
335 809 : if (opBaseAtraceInfo_ != nullptr) {
336 524 : opBaseAtraceInfo_->DeInit();
337 524 : opBaseAtraceInfo_ = nullptr;
338 : }
339 :
340 809 : ReleaseWorkSpacebuffer();
341 809 : ReleaseCommContextbuffer();
342 :
343 7268 : for (u32 i = 0; i < AICPU_LOCAL_NOTIFY_SIZE; i++) {
344 6461 : if (localAiCpuOpNotify_[i]) {
345 0 : HcclResult ret = localAiCpuOpNotify_[i]->Destroy();
346 0 : localAiCpuOpNotify_[i] = nullptr;
347 0 : if (ret != RT_ERROR_NONE) {
348 0 : HCCL_ERROR(
349 : "[Destroy][AicpuNotify]errNo[0x%016llx] rt notify destroy fail, "
350 : "aicpuOpNotify[%u] return[%d].",
351 : HCCL_ERROR_CODE(HCCL_E_RUNTIME), i, ret);
352 : }
353 : }
354 : }
355 :
356 808 : while (!aiCpuNoIpcEvnet_.empty()) {
357 1 : rtEvent_t eventInfo = aiCpuNoIpcEvnet_.back();
358 1 : HcclResult ret = hrtEventDestroy(eventInfo);
359 1 : if (ret != HCCL_SUCCESS) {
360 0 : HCCL_ERROR(
361 : "[Destroy][AicpuNoIpcEvnet]errNo[0x%016llx] rt event destroy fail, "
362 : "return[%d].",
363 : HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret);
364 : }
365 1 : aiCpuNoIpcEvnet_.pop_back();
366 : }
367 :
368 809 : UnloadAICPUKernel();
369 808 : UnloadCustomKernel();
370 809 : if (dispatcher_ != nullptr) {
371 524 : HcclDispatcherDestroy(dispatcher_);
372 521 : dispatcher_ = nullptr;
373 : }
374 806 : if (dispatcherCtx_ != nullptr) {
375 523 : DestroyDispatcherCtx(dispatcherCtx_, identifier_.c_str());
376 524 : dispatcherCtx_ = nullptr;
377 : }
378 807 : if (vDispatcher_ != nullptr) {
379 524 : HcclDispatcherDestroy(vDispatcher_);
380 524 : vDispatcher_ = nullptr;
381 : }
382 807 : if (deviceType_ == DevType::DEV_TYPE_910B || deviceType_ == DevType::DEV_TYPE_910_93) {
383 212 : UnRegisterFromSnapshot();
384 : }
385 809 : HCCL_DEBUG("~HcclCommunicator success.");
386 9433 : }
387 :
388 523 : HcclResult HcclCommunicator::SaveTopoDesc(std::string& identifier)
389 : {
390 523 : CommTopo topoType = CommTopo::COMM_TOPO_RESERVED;
391 523 : CHK_RET(GetInstTopoTypeByNetLayer(0, &topoType)); // layer 0
392 :
393 523 : CommTopoDesc::GetInstance().SaveRankSize(identifier, userRankSize_);
394 523 : CommTopoDesc::GetInstance().SaveL0TopoType(identifier, topoType);
395 523 : return HCCL_SUCCESS;
396 : }
397 :
398 501 : HcclResult HcclCommunicator::Init(HcclCommParams& params, const RankTable_t& rankTable)
399 : {
400 501 : CHK_RET(InitCommParams(params));
401 501 : CHK_RET(attrCollector_.Init(params, rankTable, commConfig_.GetConfigHcclAlgoMap()));
402 498 : CHK_RET(InitRankInfo(rankTable));
403 498 : CHK_RET(InitNetResource(rankTable));
404 491 : CHK_RET(InitDebug());
405 491 : CHK_RET(InitNotifyManager());
406 491 : CHK_RET(InitStreamManager());
407 491 : CHK_RET(InitProfiler());
408 491 : CHK_RET(InitDispatcher());
409 491 : CHK_RET(InitTransportManager());
410 490 : CHK_RET(InitCombinOpara());
411 491 : CHK_RET(RegisterRanksToDca());
412 : /*--------------加锁区--------------*/
413 491 : std::unique_lock<std::mutex> lock(g_hcomInitMutex);
414 491 : CHK_RET(RegistTaskExceptionHandler());
415 :
416 491 : attrCollector_.GenCollectiveId(params, rankTable);
417 491 : collectiveId_ = attrCollector_.GetCollectiveId();
418 :
419 : // 初始化参数(需要放置在ranktable解析之后)
420 491 : HcclResult ret = InitPara();
421 491 : CHK_PRT_RET(
422 : ret != HCCL_SUCCESS,
423 : HCCL_ERROR(
424 : "[HcclCommunicator][Init]errNo[0x%016llx] collectiveid[%s] parameter initialization failed",
425 : HCCL_ERROR_CODE(ret), params.id.internal),
426 : ret);
427 491 : lock.unlock();
428 : /*--------------加锁区--------------*/
429 491 : if (deviceType_ == DevType::DEV_TYPE_910B || deviceType_ == DevType::DEV_TYPE_910_93) {
430 198 : CHK_RET(RegisterKernel(deviceType_));
431 : }
432 491 : CHK_RET(LoadCustomKernel());
433 491 : CHK_RET(LoadAICPUKernel());
434 491 : CHK_RET(InitHDCommunicate());
435 491 : CHK_RET(InitOpRetry());
436 491 : CHK_RET(InitOpResPara());
437 :
438 491 : CHK_RET(InitOneSidedService(rankTable));
439 491 : CHK_RET(OrderLaunch::GetInstance(deviceLogicId_).RegisterOrderLaunch(identifier_));
440 491 : HcclTopoAttr topoAttr;
441 491 : attrCollector_.GetTopoAttr(topoAttr);
442 491 : CHK_RET(rankGraph_.Init(rankTable, topoAttr));
443 491 : CHK_RET(SaveTopoDesc(params.identifier));
444 491 : CHK_RET(RegisterToSnapshot());
445 491 : CHK_RET(InitSymmetricMemory());
446 :
447 491 : CHK_RET(InitMyRankConnectMode(params, rankTable));
448 491 : if (dpuManager_ != nullptr && myRankConnectMode_ != 0) { /* 当前只有host nic--device nic使用 */
449 0 : CHK_RET(dpuManager_->Init(identifier_, deviceLogicId_));
450 : }
451 :
452 491 : return HCCL_SUCCESS;
453 491 : }
454 :
455 : HcclResult
456 33 : HcclCommunicator::Init(HcclCommParams& params, const std::vector<RankInfo>& rankList, WorldGroupInfo& groupCommonData)
457 : {
458 33 : CHK_RET(InitCommParams(params));
459 33 : CHK_RET(attrCollector_.Init(params, rankList, groupCommonData, commConfig_.GetConfigHcclAlgoMap()));
460 33 : CHK_RET(InitRankInfoSubGroup(groupCommonData));
461 33 : CHK_RET(InitDebugSubGroup());
462 33 : CHK_RET(InitNotifyManager());
463 33 : CHK_RET(InitDispatcher());
464 33 : CHK_RET(InitStreamManager());
465 33 : CHK_RET(InitRaResource());
466 33 : CHK_RET(InitTransportManager());
467 33 : CHK_RET(InitHcclAlg());
468 32 : CHK_RET(LoadCustomKernel());
469 32 : CHK_RET(LoadAICPUKernel());
470 32 : CHK_RET(InitHDCommunicate());
471 32 : CHK_RET(InitOpRetry());
472 32 : CHK_RET(InitOpResPara());
473 32 : CHK_RET(RegisterRanksToDca());
474 32 : CHK_RET(OrderLaunch::GetInstance(deviceLogicId_).RegisterOrderLaunch(identifier_));
475 32 : HcclTopoAttr topoAttr;
476 32 : attrCollector_.GetTopoAttr(topoAttr);
477 32 : CHK_RET(rankGraph_.Init(topoAttr));
478 32 : CHK_RET(SaveTopoDesc(params.identifier));
479 32 : CHK_RET(RegisterToSnapshot());
480 32 : CHK_RET(InitSymmetricMemory());
481 32 : return HCCL_SUCCESS;
482 32 : }
483 :
484 523 : HcclResult HcclCommunicator::LoadAICPUKernel(void)
485 : {
486 523 : if (binHandle_ == nullptr) {
487 523 : std::string jsonPath;
488 523 : CHK_RET(GetKernelFilePath(jsonPath));
489 523 : jsonPath += "ccl_kernel.json";
490 523 : HcclResult ret = LoadBinaryFromFile(jsonPath.c_str(), ACL_RT_BINARY_LOAD_OPT_CPU_KERNEL_MODE, 0, binHandle_);
491 523 : CHK_PRT_RET(
492 : ret != HCCL_SUCCESS,
493 : HCCL_ERROR(
494 : "[LoadAICPUKernel]errNo[0x%016llx]load aicpu file fail, path[%s] optionType[%u]"
495 : "cpuKernelMode[%u].",
496 : ret, jsonPath.c_str(), ACL_RT_BINARY_LOAD_OPT_CPU_KERNEL_MODE, 0),
497 : ret);
498 523 : }
499 523 : return HCCL_SUCCESS;
500 : }
501 :
502 808 : void HcclCommunicator::UnloadAICPUKernel(void)
503 : {
504 808 : if (binHandle_ != nullptr) {
505 4 : aclError aclRet = aclrtBinaryUnLoad(binHandle_);
506 4 : if (aclRet != ACL_SUCCESS) {
507 0 : HCCL_ERROR(
508 : "[UnloadAICPUKernel]errNo[0x%016llx] unload binary from binHandel[%p] error.", aclRet, binHandle_);
509 : }
510 4 : binHandle_ = nullptr;
511 : }
512 808 : return;
513 : }
514 :
515 523 : HcclResult HcclCommunicator::LoadCustomKernel(void)
516 : {
517 : // 加载自定义算子
518 : // 请勿删除,该函数为用户自定义算子时使用,应加载句柄
519 : // 读取customEnable环境变量,开启了就执行
520 523 : std::string jsonPath;
521 523 : CHK_RET(GetCustomKernelFilePath(jsonPath));
522 523 : jsonPath += "libaicpu_custom.json";
523 523 : CHK_RET(LoadCustomFile(jsonPath.c_str(), ACL_RT_BINARY_LOAD_OPT_CPU_KERNEL_MODE, 1, binHandle_));
524 523 : return HCCL_SUCCESS;
525 523 : }
526 :
527 809 : void HcclCommunicator::UnloadCustomKernel(void)
528 : {
529 : // 卸载自定义算子
530 : // 请勿删除,该函数为用户自定义算子时使用,应释放句柄:UnloadBinary(binCustomHandle_);
531 809 : return;
532 : }
533 :
534 491 : HcclResult HcclCommunicator::InitOneSidedService(const RankTable_t& rankTable)
535 : {
536 491 : EXCEPTION_CATCH(
537 : (oneSideService_ = std::make_unique<HcclOneSidedService>(socketManager_, notifyPool_, commConfig_)),
538 : return HCCL_E_INTERNAL);
539 491 : hcclRankLinkInfo_.userRank = userRank_;
540 491 : hcclRankLinkInfo_.devicePhyId = devicePhyId_;
541 :
542 491 : if (devIpAddr_.empty()) {
543 0 : HCCL_ERROR("[%s] device ip is invalid, please set device ip first.", __func__);
544 0 : return HCCL_E_NOT_FOUND;
545 : }
546 491 : hcclRankLinkInfo_.ip = devIpAddr_[0];
547 491 : if (nicRanksPort_.size() <= userRank_) {
548 0 : HCCL_ERROR("[%s] userRank_[%u] port is invalid, please set port first", __func__, userRank_);
549 0 : return HCCL_E_NOT_FOUND;
550 : }
551 491 : hcclRankLinkInfo_.port = nicRanksPort_[userRank_];
552 491 : hcclRankLinkInfo_.socketsPerLink = 1;
553 491 : HCCL_DEBUG(
554 : "[%s]hcclRankLinkInfo_ userRank[%u], devicePhyId[%u], ip[%s], port[%u]", __func__, hcclRankLinkInfo_.userRank,
555 : hcclRankLinkInfo_.devicePhyId, hcclRankLinkInfo_.ip.GetReadableIP(), hcclRankLinkInfo_.port);
556 491 : CHK_RET(oneSideService_->Config(
557 : dispatcher_, hcclRankLinkInfo_, &rankTable, identifier_, isStandardCard_, enableP2PRankIds_));
558 491 : return HCCL_SUCCESS;
559 : }
560 :
561 0 : HcclResult HcclCommunicator::InitOneSidedServiceNetDevCtx(u32 remoteRankId)
562 : {
563 0 : if (nicDeployment_ != NICDeployment::NIC_DEPLOYMENT_DEVICE) {
564 : // 单边操作当前只支持Device网卡,不支持host
565 0 : HCCL_ERROR(
566 : "[%s]nicDeployment_[%d], userRankSize_[%u], do not support oneSidedService.", __func__, nicDeployment_,
567 : userRankSize_);
568 0 : return HCCL_E_INTERNAL;
569 : }
570 :
571 0 : std::string localServerId = serverId_;
572 0 : std::string localSuperPodId = superPodId_;
573 0 : std::string remoteServerId = rankInfoList_.at(remoteRankId).serverId;
574 0 : std::string remoteSuperPodId = rankInfoList_.at(remoteRankId).superPodId;
575 0 : u32 intraRoceSwitch = GetExternalInputIntraRoceSwitch();
576 0 : bool useRdma = false;
577 0 : if (intraRoceSwitch || (!useSuperPodMode_ && localServerId != remoteServerId)
578 0 : || (localSuperPodId != remoteSuperPodId)) {
579 : // 1. 初始化网口
580 0 : CHK_RET(InitNic());
581 0 : isOneSidedServiceNicInited = true;
582 :
583 : // 2. 单边操作SetNetDevCtx, RDMA
584 0 : if (netDevCtxMap_.find(devIpAddr_[0]) == netDevCtxMap_.end()) {
585 0 : HCCL_ERROR("[%s] nicDeployment_[%d], device nic init fail, please check", __func__, nicDeployment_);
586 0 : return HCCL_E_NOT_FOUND;
587 : }
588 0 : useRdma = true;
589 0 : oneSideService_->SetNetDevCtx(netDevCtxMap_[devIpAddr_[0]], useRdma);
590 0 : HCCL_INFO("[%s]init device Nic for oneSidedService success.", __func__);
591 : } else {
592 : // 单边操作SetNetDevCtx, IPC
593 0 : oneSideService_->SetNetDevCtx(netDevCtxMap_[localVnicIp_], useRdma);
594 0 : HCCL_INFO("[%s]init vNic for oneSidedService success.", __func__);
595 : }
596 0 : isOneSidedServiceNetDevCtxInited = true;
597 0 : HCCL_DEBUG("[%s]nicDeployment_[%d], intraRoceSwitch[%u]", __func__, nicDeployment_, intraRoceSwitch);
598 0 : return HCCL_SUCCESS;
599 0 : }
600 :
601 0 : HcclResult HcclCommunicator::DeInitOneSidedServiceNetDevCtx()
602 : {
603 0 : if (nicDeployment_ != NICDeployment::NIC_DEPLOYMENT_DEVICE) {
604 : // 单边操作当前只支持Device网卡,不支持host
605 0 : HCCL_ERROR(
606 : "[%s]nicDeployment_[%d], userRankSize_[%u], do not support oneSidedService.", __func__, nicDeployment_,
607 : userRankSize_);
608 0 : return HCCL_E_INTERNAL;
609 : }
610 0 : if (isOneSidedServiceNicStartListen_) {
611 0 : socketManager_->DestroySockets();
612 0 : u32 port = GetLocalNicPort(NicType::DEVICE_NIC_TYPE);
613 0 : CHK_RET(socketManager_->ServerDeInit(onesidedServiceNicIpAddr_, port));
614 0 : isOneSidedServiceNicStartListen_ = false;
615 0 : HCCL_INFO("[HcclCommunicator][%s] DeInit socket server success.tag[%s].", __func__, identifier_.c_str());
616 : }
617 0 : u32 intraRoceSwitch = GetExternalInputIntraRoceSwitch();
618 0 : if (isOneSidedServiceNicInited) {
619 : // 1. close sockets
620 0 : if (raResourceInit_) {
621 0 : socketManager_->DestroySockets();
622 : }
623 : // 2. 去初始化网口
624 0 : CHK_RET(DeinitNic());
625 0 : isOneSidedServiceNicInited = false;
626 0 : HCCL_INFO("[%s]Deinit device Nic for oneSidedService success.", __func__);
627 : }
628 0 : isOneSidedServiceNetDevCtxInited = false;
629 0 : HCCL_DEBUG("[%s]nicDeployment_[%d], intraRoceSwitch[%u]", __func__, nicDeployment_, intraRoceSwitch);
630 0 : return HCCL_SUCCESS;
631 : }
632 :
633 0 : HcclResult HcclCommunicator::GetOneSidedService(IHcclOneSidedService** service)
634 : {
635 0 : *service = oneSideService_.get();
636 0 : return HCCL_SUCCESS;
637 : }
638 :
639 0 : HcclResult HcclCommunicator::OneSidedServiceStartListen(NicType nicType, HcclNetDevCtx netDevCtx)
640 : {
641 0 : HCCL_INFO("[HcclCommunicator][%s] Start prepare netDevCtx.", __func__);
642 0 : u32 port = GetLocalNicPort(nicType);
643 0 : CHK_RET(socketManager_->ServerInit(netDevCtx, port));
644 0 : if (nicType == NicType::DEVICE_NIC_TYPE) {
645 0 : CHK_RET(HcclNetDevGetLocalIp(netDevCtx, onesidedServiceNicIpAddr_));
646 0 : isOneSidedServiceNicStartListen_ = true;
647 : }
648 0 : isOneSidedServiceNetDevCtxInited = true;
649 0 : HCCL_INFO("[HcclCommunicator][%s] netDevCtx[%p] port[%u] server init success.", __func__, netDevCtx, port);
650 0 : return HCCL_SUCCESS;
651 : }
652 :
653 0 : HcclResult HcclCommunicator::GetOneSidedServiceDevIpAndPort(NicType nicType, HcclIpAddress& ipAddress, u32& port)
654 : {
655 0 : if (nicDeployment_ != NICDeployment::NIC_DEPLOYMENT_DEVICE) {
656 : // 单边操作当前只支持Device网卡,不支持host
657 0 : HCCL_ERROR(
658 : "[%s]nicDeployment_[%d], userRankSize_[%u], do not support oneSidedService.", __func__, nicDeployment_,
659 : userRankSize_);
660 0 : return HCCL_E_INTERNAL;
661 : }
662 0 : port = GetLocalNicPort(nicType);
663 0 : if (nicType == NicType::VNIC_TYPE) {
664 0 : ipAddress = localVnicIp_;
665 0 : HCCL_INFO("[GetOneSidedServiceDevIpAddr] vnic ipAddress[%s] get success.", ipAddress.GetReadableAddress());
666 0 : return HCCL_SUCCESS;
667 0 : } else if (nicType == NicType::DEVICE_NIC_TYPE) {
668 0 : u32 nicNum = devIpAddr_.size();
669 0 : for (u32 i = 0; i < nicNum; i++) {
670 0 : if (devIpAddr_[i].IsInvalid()) {
671 0 : HCCL_INFO("[GetOneSidedServiceDevIpAddr]nic num[%u] deviceip is invalid, total nicNum[%u]", i, nicNum);
672 0 : continue;
673 : }
674 0 : ipAddress = devIpAddr_[i];
675 0 : HCCL_INFO("[GetOneSidedServiceDevIpAddr] nic ipAddress[%s] get success.", ipAddress.GetReadableAddress());
676 0 : return HCCL_SUCCESS;
677 : }
678 : }
679 0 : HCCL_ERROR("[HcclCommunicator][%s] ipAddress get fail. tag[%s]", __func__, identifier_.c_str());
680 0 : return HCCL_E_NOT_FOUND;
681 : }
682 :
683 0 : HcclResult HcclCommunicator::DeinitOneSidedService()
684 : {
685 0 : if (oneSideService_ != nullptr) {
686 0 : CHK_RET(oneSideService_->DeInit());
687 : }
688 0 : return HCCL_SUCCESS;
689 : }
690 :
691 78 : bool HcclCommunicator::IsSupportSymmetricMemory(HcclCMDType opType, OpParam& opParam)
692 : {
693 78 : CHK_PRT_RET(symmetricMemory_ == nullptr, HCCL_DEBUG("symmetricMemory_ is a nullptr"), false);
694 14 : HCCL_INFO(
695 : "[%s] aicpuUnfold[%d], workflowMode[%d], deviceType[%d], "
696 : "deviceNumPerAggregation_[%d], multiModuleDiffDeviceNumMode_[%d], tag[%s].",
697 : __func__, opParam.aicpuUnfoldMode, GetWorkflowMode(), deviceType_, deviceNumPerAggregation_,
698 : multiModuleDiffDeviceNumMode_, opParam.tag.c_str());
699 :
700 : // 目前只支持allgather, allreduce, reducescatter
701 14 : CHK_PRT_RET(
702 : opType != HcclCMDType::HCCL_CMD_ALLGATHER && opType != HcclCMDType::HCCL_CMD_ALLREDUCE
703 : && opType != HcclCMDType::HCCL_CMD_ALLTOALL && opType != HcclCMDType::HCCL_CMD_REDUCE_SCATTER,
704 : HCCL_INFO("[%s] opType[%d] not support symmetric memory", __func__, opType), false);
705 :
706 : // 只支持aicpu展开、单算子模式、910_93芯片
707 14 : CHK_PRT_RET(
708 : !opParam.aicpuUnfoldMode,
709 : HCCL_INFO("[%s] aicpuUnfold:%d not support symmetric memory", __func__, opParam.aicpuUnfoldMode), false);
710 4 : CHK_PRT_RET(
711 : GetWorkflowMode() != HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE,
712 : HCCL_INFO("[%s] workflowMode:%d not support symmetric memory", __func__, GetWorkflowMode()), false);
713 3 : CHK_PRT_RET(
714 : deviceType_ != DevType::DEV_TYPE_910_93,
715 : HCCL_INFO("[%s] deviceType:%d not support symmetric memory", __func__, deviceType_), false);
716 3 : CHK_PRT_RET(
717 : superPodNum_ == 1 && serverNum_ > 1 && GetExternalInputInterHccsDisable(),
718 : HCCL_INFO("[%s] mutilSever use roce not support symmetric memory", __func__), false);
719 :
720 : // 判断拓扑逻辑是否支持symmetric memory
721 : // 每个节点只有一张卡或节点间非对称场景不支持对称内存
722 3 : CHK_PRT_RET(
723 : deviceNumPerAggregation_ == 1 || multiModuleDiffDeviceNumMode_,
724 : HCCL_INFO(
725 : "[%s] deviceNumPerAggregation[%u], multiModuleDiffDeviceNumMode_[%d] not support symmetric memory",
726 : __func__, deviceNumPerAggregation_, multiModuleDiffDeviceNumMode_),
727 : false);
728 :
729 : // 判断输入输出地址是否都注册为对称内存
730 2 : HcclResult ret = symmetricMemory_->FindSymmetricWindow(
731 2 : opParam.inputPtr, opParam.inputSize, &opParam.inputSymWindow, &opParam.inputOffset);
732 2 : CHK_PRT_RET(
733 : ret != HCCL_SUCCESS || opParam.inputSymWindow == nullptr,
734 : HCCL_INFO(
735 : "[%s] input[%p] size[%llu] is not support symmetric memory", __func__, opParam.inputPtr, opParam.inputSize),
736 : false);
737 1 : ret = symmetricMemory_->FindSymmetricWindow(
738 1 : opParam.outputPtr, opParam.outputSize, &opParam.outputSymWindow, &opParam.outputOffset);
739 1 : CHK_PRT_RET(
740 : ret != HCCL_SUCCESS || opParam.outputSymWindow == nullptr,
741 : HCCL_INFO(
742 : "[%s] output[%p] size[%llu] is not support symmetric memory", __func__, opParam.outputPtr,
743 : opParam.outputSize),
744 : false);
745 :
746 1 : HCCL_INFO(
747 : "[HcclCommunicator][IsSupportSymmetricMemory] opParam.inputPtr[%p], inputOffset[%llu], inputSymWindow[%p]",
748 : opParam.inputPtr, opParam.inputOffset, opParam.inputSymWindow);
749 1 : HCCL_INFO(
750 : "[HcclCommunicator][IsSupportSymmetricMemory] opParam.outputPtr[%p], outputOffset[%llu], outputSymWindow[%p]",
751 : opParam.outputPtr, opParam.outputOffset, opParam.outputSymWindow);
752 :
753 1 : return true;
754 : }
755 :
756 77 : bool HcclCommunicator::IsSupportZeroCopy(const OpParam& opParam) const
757 : {
758 77 : HCCL_INFO(
759 : "[%s] aicpuUnfold[%d], workflowMode[%d], deviceType[%d], "
760 : "deviceNumPerAggregation_[%d], multiModuleDiffDeviceNumMode_[%d], tag[%s].",
761 : __func__, opParam.aicpuUnfoldMode, GetWorkflowMode(), deviceType_, deviceNumPerAggregation_,
762 : multiModuleDiffDeviceNumMode_, opParam.tag.c_str());
763 :
764 : // 只支持aicpu展开、非重执行、单算子模式、910_93芯片
765 76 : CHK_PRT_RET(
766 : !opParam.aicpuUnfoldMode,
767 : HCCL_INFO("[%s] aicpuUnfold:%d not support zero copy", __func__, opParam.aicpuUnfoldMode), false);
768 4 : CHK_PRT_RET(
769 : GetWorkflowMode() != HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE,
770 : HCCL_INFO("[%s] workflowMode:%d not support zero copy", __func__, GetWorkflowMode()), false);
771 0 : CHK_PRT_RET(
772 : deviceType_ != DevType::DEV_TYPE_910_93,
773 : HCCL_INFO("[%s] deviceType:%d not support zero copy", __func__, deviceType_), false);
774 :
775 : // 判断拓扑逻辑是否支持zero copy
776 : // 每个节点只有一张卡或节点间非对称场景不支持零拷贝
777 0 : CHK_PRT_RET(
778 : deviceNumPerAggregation_ == 1 || multiModuleDiffDeviceNumMode_,
779 : HCCL_INFO(
780 : "[%s] deviceNumPerAggregation[%u], multiModuleDiffDeviceNumMode_[%d] not support zero copy", __func__,
781 : deviceNumPerAggregation_, multiModuleDiffDeviceNumMode_),
782 : false);
783 :
784 : // 判断输入输出地址是否都是支持零Copy特性的
785 0 : CHK_PRT_RET(
786 : !ZeroCopyMemoryAgent::IsActivateCommMemoryAddr(opParam.inputPtr, opParam.inputSize),
787 : HCCL_INFO("[%s] input[%p] size[%llu] is not support zero copy", __func__, opParam.inputPtr, opParam.inputSize),
788 : false);
789 0 : CHK_PRT_RET(
790 : !ZeroCopyMemoryAgent::IsActivateCommMemoryAddr(opParam.outputPtr, opParam.outputSize),
791 : HCCL_INFO(
792 : "[%s] output[%p] size[%llu] is not support zero copy", __func__, opParam.outputPtr, opParam.outputSize),
793 : false);
794 :
795 0 : return true;
796 : }
797 :
798 75 : HcclResult HcclCommunicator::PrepareZeroCopy(const std::string& algName, const AlgDesc& algDesc, OpParam& opParam)
799 : {
800 75 : if (!algDesc.isZeroCopy) {
801 : opParam.supportSymmetricMemory
802 75 : = false; // 当前对称内存与零拷贝算法绑定,对称内存使能关闭,确保aicpu侧不走对称内存分支
803 75 : HCCL_INFO("[HcclCommunicator][PrepareZeroCopy] algName[%s] not support zerocopy.", algName.c_str());
804 78 : return HCCL_SUCCESS;
805 : }
806 :
807 0 : if (opParam.supportSymmetricMemory) {
808 0 : HCCL_INFO(
809 : "[HcclCommunicator][PrepareZeroCopy] algName[%s] symmetric memory is enabled, not use zerocopy.",
810 : algName.c_str());
811 0 : return HCCL_SUCCESS;
812 : }
813 : // ARS特性不支持零拷贝
814 0 : if ((opParam.opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER || opParam.opType == HcclCMDType::HCCL_CMD_ALLGATHER
815 0 : || opParam.opType == HcclCMDType::HCCL_CMD_ALLREDUCE)
816 0 : && deviceType_ == DevType::DEV_TYPE_910_93 && multiModuleDiffDeviceNumMode_
817 0 : && !multiSuperPodDiffDeviceNumMode_) {
818 0 : return HCCL_SUCCESS;
819 : }
820 :
821 : // 如果自己侧的共享内存没有申请,那么进行申请,并设置给transportManager,后续p2p建链时进行交换
822 0 : if (zeroCopyLocalBuffer_.ptr() == nullptr) {
823 0 : CHK_RET(DeviceMem::alloc(zeroCopyLocalBuffer_, ZERO_COPY_IPC_BUFFER_LENGTH));
824 0 : CHK_RET(hrtMemSet(zeroCopyLocalBuffer_.ptr(), zeroCopyLocalBuffer_.size(), zeroCopyLocalBuffer_.size()));
825 0 : zeroCopyIpcPtrs_[userRank_ % deviceNumPerAggregation_] = zeroCopyLocalBuffer_.ptr();
826 :
827 0 : HCCL_RUN_INFO(
828 : "[HCCL_TRACE][PrepareZeroCopy]Create ZeroCopy buffer success. buffer ptr[%p] size[%llu]",
829 : zeroCopyLocalBuffer_.ptr(), zeroCopyLocalBuffer_.size());
830 : }
831 0 : opParam.isZeroCopy = true;
832 0 : HCCL_INFO("[HcclCommunicator][PrepareZeroCopy] success to use zero copy feature");
833 0 : return HCCL_SUCCESS;
834 : }
835 :
836 23 : HcclResult HcclCommunicator::UpdateZeroCopy(const OpParam& opParam, const AlgResourceResponse& algResource)
837 : {
838 23 : if (!opParam.isZeroCopy) {
839 23 : return HCCL_SUCCESS;
840 : }
841 :
842 : // 遍历所有transport,找出里面的p2p链路对应的对端地址
843 0 : for (auto& singleSubCommTransport : algResource.opTransportResponse[COMM_LEVEL0]) {
844 0 : for (u64 i = 0; i < singleSubCommTransport.links.size(); ++i) {
845 0 : LINK link = singleSubCommTransport.links[i];
846 0 : if (link == nullptr || !singleSubCommTransport.transportRequests[i].isValid) {
847 : // 无效或者不支持的链路
848 0 : continue;
849 : }
850 :
851 : // 在使能零拷贝场景,我们使用控制面内存做OpenIpc交换,因此这里取出input即可
852 0 : u32 remoteRank = link->GetRemoteRank();
853 :
854 0 : void* remotePtr = nullptr;
855 0 : CHK_RET(link->GetRemoteMem(UserMemType::INPUT_MEM, &remotePtr));
856 0 : CHK_PRT_RET(
857 : remotePtr == nullptr, HCCL_ERROR("[BuildZeroCopyParam] invalid remotePtr[%p]", remotePtr), HCCL_E_PARA);
858 0 : CHK_PRT_RET(
859 : zeroCopyIpcPtrs_[remoteRank % deviceNumPerAggregation_] != nullptr
860 : && zeroCopyIpcPtrs_[remoteRank % deviceNumPerAggregation_] != remotePtr,
861 : HCCL_ERROR(
862 : "[BuildZeroCopyParam] zeroCopyIpcPtrs_[%u] is [%p] not equal to %p", remoteRank,
863 : zeroCopyIpcPtrs_[remoteRank % deviceNumPerAggregation_], remotePtr),
864 : HCCL_E_PARA);
865 :
866 0 : zeroCopyIpcPtrs_[remoteRank % deviceNumPerAggregation_] = remotePtr;
867 0 : }
868 : }
869 0 : return HCCL_SUCCESS;
870 : }
871 :
872 3 : HcclResult HcclCommunicator::BuildZeroCopyParam()
873 : {
874 : // 不支持ZeroCopy
875 3 : if (zeroCopyLocalBuffer_.ptr() == nullptr) {
876 1 : return HCCL_SUCCESS;
877 : }
878 :
879 66 : for (u32 i = 0; i < AICPU_ZERO_COPY_MAX_DEVICE_NUM_A3; ++i) {
880 64 : opResPara_.zeroCopyIpcPtrs[i] = reinterpret_cast<u64>(zeroCopyIpcPtrs_[i]);
881 : }
882 :
883 2 : for (u32 i = 0; i < rankInfoList_.size(); ++i) {
884 0 : opResPara_.zeroCopyDevicePhyId[i % deviceNumPerAggregation_] = rankInfoList_[i].devicePhyId;
885 : }
886 :
887 2 : CHK_RET(ZeroCopyMemoryAgent::GetRingBufferAddr(
888 : opResPara_.zeroCopyRingBuffer, opResPara_.zeroCopyHeadPtr, opResPara_.zeroCopyTailPtr));
889 1 : return HCCL_SUCCESS;
890 : }
891 :
892 534 : HcclResult HcclCommunicator::InitCommParams(HcclCommParams& params)
893 : {
894 534 : commHandle_ = params.commHandle;
895 534 : userRank_ = params.rank;
896 534 : realUserRank_ = params.userRank;
897 534 : userRankSize_ = params.totalRanks;
898 534 : deviceLogicId_ = params.logicDevId;
899 534 : profilingOption_ = params.profilingOption;
900 534 : profilingInitiated_ = params.profilingInitiated;
901 534 : deviceType_ = params.deviceType;
902 534 : commWorkMode_ = params.commWorkMode;
903 534 : hcomGroupNicInit_ = params.hcomGroupNicInit;
904 534 : identifier_ = params.identifier;
905 534 : collectiveId_ = params.id.internal;
906 534 : ranktableCrc_ = params.ranktableCrc;
907 534 : commConnections_ = params.commConnections;
908 534 : commPortConfig_ = params.commPortConfig;
909 534 : cclBuffName_ = params.cclBuffName;
910 534 : isShareComm_ = !cclBuffName_.empty();
911 :
912 534 : HCCL_DEBUG(
913 : " userRank_: %u realUserRank_: %u userRankSize_: %u deviceLogicId_: %u deviceType_: %u commWorkMode_: %u.",
914 : userRank_, realUserRank_, userRankSize_, deviceLogicId_, deviceType_, commWorkMode_);
915 :
916 534 : return HCCL_SUCCESS;
917 : }
918 :
919 0 : bool HcclCommunicator::Is310PDuoCard()
920 : {
921 : return (
922 0 : Is310P3Common(isHaveCpuRank_, deviceType_)
923 0 : && (pairLinkInfo_[static_cast<u32>(LinkTypeInServer::HCCS_TYPE)].size() == userRankSize_));
924 : }
925 :
926 : // 910B A+X 在RDMA未启用情况下,两模块间的device数目需要一致且两模块中使用的卡都在同一平面上
927 496 : HcclResult HcclCommunicator::CheckSingleServerComm(const std::vector<RankInfo_t>& rankList) const
928 : {
929 396 : if (serverNum_ == 1 && moduleNum_ == HCCL_MODULE_NUM_TWO && GetExternalInputIntraRoceSwitch() == 0
930 892 : && !isStandardCard_) {
931 0 : std::vector<u32> devIdList0;
932 0 : std::vector<u32> devIdList1;
933 0 : for (RankInfo_t rankInfo : rankList) {
934 0 : if (rankInfo.deviceInfo.devicePhyId == HOST_DEVICE_ID) {
935 0 : HCCL_ERROR("[Check][SingleServerComm]not support cpu rank");
936 0 : return HCCL_E_NOT_SUPPORT;
937 : }
938 0 : if (rankInfo.deviceInfo.devicePhyId < DEVICE_PER_MODULE) {
939 0 : devIdList0.push_back(rankInfo.deviceInfo.devicePhyId);
940 : } else {
941 0 : devIdList1.push_back(rankInfo.deviceInfo.devicePhyId);
942 : }
943 0 : }
944 0 : std::sort(devIdList0.begin(), devIdList0.end());
945 0 : std::sort(devIdList1.begin(), devIdList1.end());
946 :
947 0 : auto buildDeviceListStr = [](const std::vector<u32>& list) -> std::string {
948 0 : std::string result;
949 0 : for (const auto& id : list) {
950 0 : if (!result.empty()) {
951 0 : result += " ";
952 : }
953 0 : result += std::to_string(id);
954 : }
955 0 : return result;
956 0 : };
957 :
958 0 : std::string devList0Str = buildDeviceListStr(devIdList0);
959 0 : std::string devList1Str = buildDeviceListStr(devIdList1);
960 :
961 0 : if (devIdList0.size() != devIdList1.size()) {
962 0 : std::string errormessage = "Device ID " + devList0Str + " in module 0 and device ID " + devList1Str
963 0 : + " in module 1 are not on the same plane.";
964 0 : RPT_INPUT_ERR(
965 : true, "EI0010", std::vector<std::string>({"reason"}), std::vector<std::string>({errormessage}));
966 0 : HCCL_ERROR(
967 : "[%s][%s]%s", LOG_KEYWORDS_INIT_CHANNEL.c_str(), LOG_KEYWORDS_TIMEOUT.c_str(), errormessage.c_str());
968 0 : return HCCL_E_NOT_SUPPORT;
969 0 : }
970 0 : for (size_t i = 0; i < devIdList0.size(); i++) {
971 0 : if (devIdList0[i] % DEVICE_PER_MODULE != devIdList1[i] % DEVICE_PER_MODULE) {
972 0 : std::string errormessage = "Device ID " + std::to_string(devIdList0[i]) + " in module 0 and device ID "
973 0 : + std::to_string(devIdList1[i]) + " in module 1 are not on the same plane.";
974 0 : RPT_INPUT_ERR(
975 : true, "EI0010", std::vector<std::string>({"reason"}), std::vector<std::string>({errormessage}));
976 0 : HCCL_ERROR(
977 : "[%s][%s]%s", LOG_KEYWORDS_INIT_CHANNEL.c_str(), LOG_KEYWORDS_TIMEOUT.c_str(),
978 : errormessage.c_str());
979 0 : return HCCL_E_NOT_SUPPORT;
980 0 : }
981 : }
982 0 : }
983 496 : return HCCL_SUCCESS;
984 0 : }
985 :
986 406 : HcclResult HcclCommunicator::CheckDataType(const HcclDataType dataType, bool needReduce)
987 : {
988 3534 : const vector<string> infoTitle({"ccl_op", "value", "parameter", "expect"});
989 409 : if (needReduce) {
990 188 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
991 0 : if ((dataType == HCCL_DATA_TYPE_INT64) || (dataType == HCCL_DATA_TYPE_BFP16)) {
992 0 : RPT_INPUT_ERR(
993 : true, "EI0003", infoTitle,
994 : vector<string>(
995 : {"CheckDataType", GetDataTypeEnumStr(dataType), "dataType",
996 : "HCCL_DATA_TYPE_INT8, HCCL_DATA_TYPE_INT16, HCCL_DATA_TYPE_INT32, "
997 : "HCCL_DATA_TYPE_FP16, HCCL_DATA_TYPE_FP32"}));
998 0 : HCCL_ERROR(
999 : "[%s][%s]errNo[0x%016llx] data type[%s] not supported, support range=[%s]",
1000 : LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_INVALID_ARGUMENT.c_str(),
1001 : HCCL_ERROR_CODE(HCCL_E_NOT_SUPPORT), GetDataTypeEnumStr(dataType).c_str(),
1002 : GetSupportDataType(needReduce).c_str());
1003 0 : return HCCL_E_NOT_SUPPORT;
1004 : }
1005 : }
1006 185 : if ((dataType == HCCL_DATA_TYPE_UINT64) || (dataType == HCCL_DATA_TYPE_UINT8)
1007 184 : || (dataType == HCCL_DATA_TYPE_UINT16) || (dataType == HCCL_DATA_TYPE_UINT32)
1008 184 : || (dataType == HCCL_DATA_TYPE_FP64) || (dataType == HCCL_DATA_TYPE_RESERVED)) {
1009 12 : RPT_INPUT_ERR(
1010 : true, "EI0003", infoTitle,
1011 : vector<string>(
1012 : {"CheckDataType", GetDataTypeEnumStr(dataType), "dataType",
1013 : "HCCL_DATA_TYPE_INT8, HCCL_DATA_TYPE_INT16, HCCL_DATA_TYPE_INT32, "
1014 : "HCCL_DATA_TYPE_FP16, HCCL_DATA_TYPE_FP32"}));
1015 4 : HCCL_ERROR(
1016 : "[%s][%s]errNo[0x%016llx] data type[%s] not supported, support range=[%s]",
1017 : LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_INVALID_ARGUMENT.c_str(),
1018 : HCCL_ERROR_CODE(HCCL_E_NOT_SUPPORT), GetDataTypeEnumStr(dataType).c_str(),
1019 : GetSupportDataType(needReduce).c_str());
1020 1 : return HCCL_E_NOT_SUPPORT;
1021 : }
1022 : } else {
1023 221 : if ((dataType >= HCCL_DATA_TYPE_RESERVED) || (dataType < HCCL_DATA_TYPE_INT8)
1024 442 : || (Is310P3Common(isHaveCpuRank_, deviceType_) && dataType == HCCL_DATA_TYPE_BFP16)) {
1025 0 : RPT_INPUT_ERR(
1026 : true, "EI0003", infoTitle,
1027 : vector<string>(
1028 : {"CheckDataType", GetDataTypeEnumStr(dataType), "dataType",
1029 : "HCCL_DATA_TYPE_INT8, HCCL_DATA_TYPE_INT16, HCCL_DATA_TYPE_INT32, "
1030 : "HCCL_DATA_TYPE_FP16, HCCL_DATA_TYPE_FP32, HCCL_DATA_TYPE_UINT8, HCCL_DATA_TYPE_UINT16, "
1031 : "HCCL_DATA_TYPE_UINT32"}));
1032 0 : HCCL_ERROR(
1033 : "[%s][%s]errNo[0x%016llx] data type[%s] not supported, support range=[%s]",
1034 : LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_INVALID_ARGUMENT.c_str(),
1035 : HCCL_ERROR_CODE(HCCL_E_NOT_SUPPORT), GetDataTypeEnumStr(dataType).c_str(),
1036 : GetSupportDataType(needReduce).c_str());
1037 0 : return HCCL_E_NOT_SUPPORT;
1038 : }
1039 : }
1040 402 : return HCCL_SUCCESS;
1041 405 : }
1042 :
1043 0 : HcclResult HcclCommunicator::InitZeroCopyMemoryAgent()
1044 : {
1045 0 : CHK_PRT_RET(
1046 : zeroCopyMemoryAgent_ != nullptr,
1047 : HCCL_ERROR("[HcclCommunicator][InitZeroCopyMemoryAgent] ipc memory agent has init"), HCCL_E_INTERNAL);
1048 :
1049 : // 获取节点内的ranktable
1050 0 : std::vector<std::vector<std::vector<RankInfo>>> commPlaneVector;
1051 0 : CHK_SMART_PTR_NULL(implAlg_);
1052 0 : implAlg_->GetCommPlaneVector(commPlaneVector);
1053 0 : rankInfoListIntraServer_ = commPlaneVector[COMM_LEVEL0][COMM_INDEX_0];
1054 0 : zeroCopyMemoryAgent_.reset(static_cast<ZeroCopyMemoryAgent*>(new (std::nothrow) ZeroCopyMemoryAgent(
1055 0 : socketManager_, devicePhyId_, deviceLogicId_, localVnicIp_, rankInfoListIntraServer_, userRank_,
1056 0 : useSuperPodMode_, identifier_)));
1057 0 : CHK_PTR_NULL(zeroCopyMemoryAgent_);
1058 0 : CHK_RET(zeroCopyMemoryAgent_->Init());
1059 0 : return HCCL_SUCCESS;
1060 0 : }
1061 :
1062 1044 : HcclResult HcclCommunicator::DeinitZeroCopyMemoryAgent(bool inDestructor)
1063 : {
1064 1044 : if (zeroCopyMemoryAgent_ != nullptr) {
1065 0 : if (!inDestructor && zeroCopyMemoryAgent_->IsResumed()) {
1066 : // 析构函数释放场景不做barrier close
1067 0 : CHK_RET(zeroCopyMemoryAgent_->BarrierClose());
1068 : }
1069 0 : CHK_RET(zeroCopyMemoryAgent_->DeInit());
1070 0 : zeroCopyMemoryAgent_ = nullptr;
1071 : }
1072 1044 : return HCCL_SUCCESS;
1073 : }
1074 :
1075 77 : u8 HcclCommunicator::GetConfigAclGraphZeroCopyEnable() { return commConfig_.GetConfigAclGraphZeroCopyEnable(); }
1076 :
1077 61 : HcclResult HcclCommunicator::ClearResMap(const std::string& tag, bool& findTag, bool aclGraphDestroyCbk)
1078 : {
1079 61 : auto resIter = resMap_.find(tag);
1080 61 : if (resIter != resMap_.end()) {
1081 12 : findTag = true;
1082 12 : DestroyAlgResource(resIter->second, aclGraphDestroyCbk);
1083 12 : CHK_RET(StreamActiveManager::GetInstance(deviceLogicId_).StreamsUnactive(resIter->second.slaveStreams));
1084 12 : resMap_.erase(resIter);
1085 12 : HCCL_INFO("[%s] clear resMap[%s]", __func__, tag.c_str());
1086 : }
1087 61 : return HCCL_SUCCESS;
1088 : }
1089 :
1090 4 : HcclResult HcclCommunicator::ClearAclgraphHostLinks(const std::unordered_set<std::string>& tags)
1091 : {
1092 4 : std::lock_guard<std::mutex> lock(commResMutex_);
1093 8 : for (const auto& tag : tags) {
1094 4 : auto it = tagsRequiringHostCleanup_.find(tag);
1095 4 : if (it == tagsRequiringHostCleanup_.end()) {
1096 1 : continue;
1097 : }
1098 4 : for (auto& rankIt : rankTagRemoteRes_) {
1099 1 : auto tagIt = rankIt.second.find(tag);
1100 1 : if (tagIt == rankIt.second.end()) {
1101 0 : continue;
1102 : }
1103 1 : HccltagRemoteResV2* hostPtr = tagIt->second.tagRemoteResPtr;
1104 1 : if (hostPtr != nullptr) {
1105 : // 顺序敏感: ListCommonRemove 必须先于 erase shared_ptr,
1106 : // 否则 hostPtr 成野指针, ListCommonRemove 访问 segfault。
1107 1 : ListCommonRemove(&hostPtr->nextTagRes);
1108 1 : for (auto vIt = hostMemVec_.begin(); vIt != hostMemVec_.end(); ++vIt) {
1109 1 : if (*vIt && (*vIt)->ptr() == hostPtr) {
1110 1 : size_t idx = static_cast<size_t>(vIt - hostMemVec_.begin());
1111 1 : if (idx < deviceMemVec_.size()) {
1112 1 : deviceMemVec_.erase(deviceMemVec_.begin() + idx);
1113 : }
1114 1 : hostMemVec_.erase(vIt);
1115 1 : break;
1116 : }
1117 : }
1118 : }
1119 1 : rankIt.second.erase(tagIt);
1120 : }
1121 3 : ibverbsLocalNotify_.erase(tag);
1122 3 : ibverbsRemoteNotify_.erase(tag);
1123 3 : tagsRequiringHostCleanup_.erase(it);
1124 : }
1125 4 : return HCCL_SUCCESS;
1126 4 : }
1127 :
1128 19 : HcclResult HcclCommunicator::ClearOpResource(const std::string& tag, bool aclGraphDestroyCbk)
1129 : {
1130 19 : std::lock_guard<std::mutex> lock(commResMutex_);
1131 19 : bool findTag = false;
1132 19 : CHK_RET(ClearResMap(tag, findTag, aclGraphDestroyCbk));
1133 19 : CHK_RET(ClearResMap(tag + "_host", findTag, aclGraphDestroyCbk));
1134 19 : CHK_RET(ClearResMap(tag + "_device", findTag, aclGraphDestroyCbk));
1135 19 : if (!findTag) {
1136 13 : HCCL_WARNING("[%s] not find tag[%s] in resMap", __func__, tag.c_str());
1137 : }
1138 :
1139 19 : tagCommInfo_.erase(tag);
1140 : // stream解绑定
1141 19 : auto iterStream = tagStreamInfo_.find(tag);
1142 19 : if (iterStream != tagStreamInfo_.end()) {
1143 0 : CHK_RET(StreamActiveManager::GetInstance(deviceLogicId_).StreamsUnactive(iterStream->second.ringStreams));
1144 : }
1145 19 : tagStreamInfo_.erase(tag);
1146 18 : if (opRetryStreamPtr_ != nullptr) {
1147 15 : opRetryStreamPtr_->erase(tag);
1148 : }
1149 17 : if (implAlg_ != nullptr) {
1150 13 : CHK_RET(implAlg_->ClearOpResource(tag));
1151 : }
1152 17 : DestroyWorkspaceResource(tag);
1153 19 : return HCCL_SUCCESS;
1154 19 : }
1155 :
1156 95 : HcclResult HcclCommunicator::CreateOpBasedResources(
1157 : const HcclCMDType& opType, const std::string& tag, const HcomCollOpInfo& opInfo)
1158 : {
1159 95 : return workSpaceRes_->CreateOpBasedResources(opType, tag, opInfo);
1160 : }
1161 :
1162 0 : HcclResult HcclCommunicator::CreateRemoteOpBasedResources(u64 memSize, const std::string& tag)
1163 : {
1164 0 : return workSpaceRes_->CreateRemoteOpBasedResources(memSize, tag);
1165 : }
1166 :
1167 0 : HcclResult HcclCommunicator::DestroyRemoteOpBasedMem(const std::string& tag)
1168 : {
1169 0 : return workSpaceRes_->DestroyRemoteOpBasedMem(tag);
1170 : }
1171 :
1172 152 : bool HcclCommunicator::IsAtomicInit()
1173 : {
1174 304 : if (!initializedFlag_.test_and_set()) {
1175 0 : initializedFlag_.clear();
1176 0 : return false;
1177 : }
1178 152 : return true;
1179 : }
1180 :
1181 5 : bool HcclCommunicator::IsNeedNicInit()
1182 : {
1183 : return (
1184 10 : (nicInitialized_ == 0) && (!hcomGroupNicInit_) && (userRankSize_ > 1) && !isSingleMeshAggregation_
1185 10 : && (superPodNum_ > 1 || !isUsedInterHccsMode_));
1186 : }
1187 :
1188 0 : HcclResult HcclCommunicator::GetBandWidthPerNPU(u32 level, float& bandWidth)
1189 : {
1190 0 : return hccl::GetBandWidthPerNPU(level, userRankSize_, deviceNumPerAggregation_, bandWidth);
1191 : }
1192 :
1193 0 : HcclResult HcclCommunicator::GetDeviceNumPerAggregation(u32& deviceNumPerAggregation)
1194 : {
1195 0 : deviceNumPerAggregation = deviceNumPerAggregation_;
1196 0 : return HCCL_SUCCESS;
1197 : }
1198 :
1199 235 : HcclResult HcclCommunicator::InitHccpChannel() { return hcclH2dTlv::GetInstance().InitHccpChannel(devicePhyId_); }
1200 :
1201 404 : std::vector<RankInfo> HcclCommunicator::GetRankLists() { return rankInfoList_; }
1202 :
1203 163 : HcclResult HcclCommunicator::CheckReduceDataType(const HcclDataType dataType, const HcclReduceOp op)
1204 : {
1205 163 : if ((deviceType_ == DevType::DEV_TYPE_910B) || (deviceType_ == DevType::DEV_TYPE_910_93)) {
1206 54 : if ((op == HCCL_REDUCE_PROD) && ((dataType == HCCL_DATA_TYPE_INT16) || (dataType == HCCL_DATA_TYPE_BFP16))) {
1207 0 : RPT_INPUT_ERR(
1208 : true, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),
1209 : std::vector<std::string>(
1210 : {"CheckReduceDataType", GetDataTypeEnumStr(dataType), "dataType",
1211 : "HCCL_DATA_TYPE_INT8, HCCL_DATA_TYPE_INT32, HCCL_DATA_TYPE_FP16, HCCL_DATA_TYPE_FP32"}));
1212 0 : HCCL_ERROR(
1213 : "[%s][%s]errNo[0x%016llx] device type[%d] does not support the data type[%s] and data "
1214 : "type[%s] for Op[%s]",
1215 : LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_INVALID_ARGUMENT.c_str(),
1216 : HCCL_ERROR_CODE(HCCL_E_NOT_SUPPORT), deviceType_, GetDataTypeEnumStr(HCCL_DATA_TYPE_BFP16).c_str(),
1217 : GetDataTypeEnumStr(HCCL_DATA_TYPE_INT16).c_str(), GetReduceOpEnumStr(op).c_str());
1218 0 : return HCCL_E_NOT_SUPPORT;
1219 : }
1220 109 : } else if (deviceType_ == DevType::DEV_TYPE_910) {
1221 109 : if (dataType == HCCL_DATA_TYPE_INT16) {
1222 0 : RPT_INPUT_ERR(
1223 : true, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),
1224 : std::vector<std::string>(
1225 : {"CheckReduceDataType", GetDataTypeEnumStr(dataType), "dataType",
1226 : "HCCL_DATA_TYPE_INT8, HCCL_DATA_TYPE_INT32, HCCL_DATA_TYPE_FP16, HCCL_DATA_TYPE_FP32"}));
1227 0 : HCCL_ERROR(
1228 : "[%s][%s]errNo[0x%016llx] device type[%d] does not support the data type[%s]",
1229 : LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_INVALID_ARGUMENT.c_str(),
1230 : HCCL_ERROR_CODE(HCCL_E_NOT_SUPPORT), deviceType_, GetDataTypeEnumStr(dataType).c_str());
1231 0 : return HCCL_E_NOT_SUPPORT;
1232 : }
1233 0 : } else if (deviceType_ == DevType::DEV_TYPE_310P3) {
1234 0 : if (dataType == HcclDataType::HCCL_DATA_TYPE_INT16 && op != HcclReduceOp::HCCL_REDUCE_SUM) {
1235 0 : RPT_INPUT_ERR(
1236 : true, "EI0003", std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),
1237 : std::vector<std::string>({"CheckReduceDataType", GetReduceOpEnumStr(op), "op", "sum"}));
1238 0 : HCCL_ERROR(
1239 : "[%s][%s]errNo[0x%016llx] device type[%d] does not support the data type[%s] for Op[%s]",
1240 : LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_INVALID_ARGUMENT.c_str(),
1241 : HCCL_ERROR_CODE(HCCL_E_NOT_SUPPORT), deviceType_,
1242 : GetDataTypeEnumStr(HcclDataType::HCCL_DATA_TYPE_INT16).c_str(), GetReduceOpEnumStr(op).c_str());
1243 0 : return HCCL_E_NOT_SUPPORT;
1244 : }
1245 : }
1246 163 : return HCCL_SUCCESS;
1247 0 : }
1248 :
1249 235 : HcclResult HcclCommunicator::GetAlgType(AlgType& algType, HcclCMDType opType)
1250 : {
1251 235 : CHK_SMART_PTR_NULL(implAlg_);
1252 234 : return implAlg_->GetAlgType(algType, opType);
1253 : }
1254 :
1255 1 : HcclResult HcclCommunicator::GetCommParams(HcclCommParams& params)
1256 : {
1257 1 : params.commHandle = commHandle_;
1258 1 : params.rank = userRank_;
1259 1 : params.userRank = realUserRank_;
1260 1 : params.totalRanks = userRankSize_;
1261 1 : params.logicDevId = deviceLogicId_;
1262 1 : params.deviceType = deviceType_;
1263 1 : params.hcomGroupNicInit = hcomGroupNicInit_;
1264 1 : params.identifier = identifier_;
1265 1 : params.ranktableCrc = ranktableCrc_;
1266 1 : params.commConnections = commConnections_;
1267 1 : params.commPortConfig.devPortSwitchOn = commPortConfig_.devPortSwitchOn;
1268 1 : return HCCL_SUCCESS;
1269 : }
1270 :
1271 1 : HcclResult HcclCommunicator::GetCommRankTable(RankTable_t& rankTable)
1272 : {
1273 2 : for (auto& server : servRankInfo_) {
1274 2 : for (auto& rank : server.second) {
1275 1 : rankTable.rankList.emplace_back(rank);
1276 : }
1277 : }
1278 1 : rankTable.serverNum = serverNum_;
1279 1 : rankTable.superPodNum = superPodNum_;
1280 1 : rankTable.nicDeploy = nicDeployment_;
1281 1 : rankTable.version = attrCollector_.GetRankTableVersion();
1282 1 : return HCCL_SUCCESS;
1283 : }
1284 :
1285 491 : HcclResult HcclCommunicator::InitPara()
1286 : {
1287 : // 检查当前user_rank 对应的devid和rt查到的一致
1288 491 : CHK_RET(attrCollector_.CheckLocalRankInfo());
1289 491 : CHK_RET(attrCollector_.CalAndSetMeshAggRankSize());
1290 491 : meshAggregationRankSize_ = attrCollector_.GetMeshAggregationRankSize();
1291 :
1292 : // 初始化计数任务
1293 491 : CHK_RET(OpExeCounter::GetInstance(deviceLogicId_).InitCounter());
1294 :
1295 491 : notifyPool_.reset(new (std::nothrow) NotifyPool());
1296 491 : CHK_SMART_PTR_NULL(notifyPool_);
1297 491 : CHK_RET(notifyPool_->Init(devicePhyId_));
1298 :
1299 491 : callbackTask_.reset(new (std::nothrow) HcclCallbackTask(devicePhyId_, deviceLogicId_, dispatcher_, nicDeployment_));
1300 491 : CHK_SMART_PTR_NULL(callbackTask_);
1301 :
1302 491 : workSpaceRes_.reset(new (std::nothrow) WorkspaceResource(devicePhyId_, deviceLogicId_, &cclBufferManager_));
1303 491 : CHK_SMART_PTR_NULL(workSpaceRes_);
1304 :
1305 491 : CHK_RET(InitAlgResource());
1306 491 : return HCCL_SUCCESS;
1307 : }
1308 :
1309 0 : bool HcclCommunicator::IsStandardCard()
1310 : {
1311 0 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
1312 0 : HCCL_INFO("The current device just support this StandardCard case.");
1313 0 : return true;
1314 : }
1315 :
1316 : return (
1317 0 : (pairLinkInfo_[static_cast<u32>(LinkTypeInServer::HCCS_TYPE)].size() == 0)
1318 0 : && (pairLinkInfo_[static_cast<u32>(LinkTypeInServer::HCCS_SW_TYPE)].size() == 0)
1319 0 : && (pairLinkInfo_[static_cast<u32>(LinkTypeInServer::SIO_TYPE)].size() == 0));
1320 : }
1321 :
1322 523 : HcclResult HcclCommunicator::InitOpRetry()
1323 : {
1324 523 : EXCEPTION_CATCH((opRetryStreamPtr_ = std::make_shared<HcclOpStreamRes>()), return HCCL_E_PTR);
1325 523 : if (retryEnable_) {
1326 0 : opRetryManager_.reset(new (std::nothrow) OpRetryManager());
1327 0 : CHK_SMART_PTR_NULL(opRetryManager_);
1328 0 : HcclIpAddress hostIp = !rankInfoList_.empty() ? rankInfoList_[0].hostIp : HcclIpAddress();
1329 0 : u32 hostPort = !rankInfoList_.empty() ? rankInfoList_[0].hostPort : HCCL_INVALID_PORT;
1330 0 : s32 hostDevId = !rankInfoList_.empty() ? rankInfoList_[0].devicePhyId : 0;
1331 0 : HcclIpAddress localIp = rankInfoList_.size() > userRank_ ? rankInfoList_[userRank_].hostIp : HcclIpAddress();
1332 0 : auto notifyResetCallback = [this](bool isSendRecv, s64 destRank) {
1333 0 : return isSendRecv ? this->ResetNotifyForDestRank(destRank) : this->ResetNotify();
1334 0 : };
1335 :
1336 0 : auto setTransportStatusCallback = [this](
1337 : const HcclOpIdentifier& opId, bool statusStop,
1338 : const std::map<u32, bool>& remoteRankPortMap,
1339 : const std::map<u32, bool>& isChangeLinkMap, bool isChangeLinkFlag) {
1340 0 : return this->SetTransportStatus(opId, statusStop, remoteRankPortMap, isChangeLinkMap, isChangeLinkFlag);
1341 0 : };
1342 : auto getSwitchRanksCallback
1343 0 : = [this](
1344 : u32* distSwitchRankList, bool* distSwitchUseBackup, u32& distSwitchRankNum,
1345 : u8* distRemoteRankNicStatus, u32& distRankSize, bool& needCheckDefaultNic, bool& needCheckBackupNic) {
1346 0 : return this->GetSwitchRanks(
1347 : distSwitchRankList, distSwitchUseBackup, distSwitchRankNum, distRemoteRankNicStatus, distRankSize,
1348 0 : needCheckDefaultNic, needCheckBackupNic);
1349 0 : };
1350 0 : auto setTransportResumeStatusCallback = [this](
1351 : const std::map<u32, bool>& remoteRankPortMap,
1352 : const std::map<u32, bool>& isChangeLinkMap, bool isChangeLinkFlag,
1353 : bool statusStop) {
1354 0 : return this->SetTransportResumeStatus(remoteRankPortMap, isChangeLinkMap, isChangeLinkFlag, statusStop);
1355 0 : };
1356 0 : HcclNetDevCtx netDevCtx = netDevCtxMap_[devIpAddr_[0]];
1357 0 : HcclNetDevCtx backUpNetDevCtx = {};
1358 0 : if (IsEnableBackupLink()) {
1359 0 : g_enableBackupLinkCommCount++;
1360 : }
1361 0 : if (IsEnableBackupLink() && netDevCtxMap_.find(devBackupIpAddr_[0]) != netDevCtxMap_.end()) {
1362 0 : backUpNetDevCtx = netDevCtxMap_[devBackupIpAddr_[0]];
1363 : }
1364 0 : OpRetryServerInfo serverInfo = {hostIp, hostPort, hostDevId};
1365 0 : OpRetryAgentInfo agentInfo = {userRank_, deviceLogicId_, localIp, devIpAddr_[0], netDevCtx, backUpNetDevCtx};
1366 :
1367 0 : OpRetryAgentParam agentParam;
1368 0 : agentParam.group = identifier_;
1369 0 : agentParam.agentConnection = commConnections_.agentConnection;
1370 0 : agentParam.h2dPtr = kfcControlTransferH2D_;
1371 0 : agentParam.d2hPtr = kfcStatusTransferD2H_;
1372 0 : agentParam.opStreamPtr = opRetryStreamPtr_;
1373 0 : agentParam.notifyResetCallback = notifyResetCallback;
1374 0 : agentParam.setTransportStatusCallback = setTransportStatusCallback;
1375 0 : agentParam.setTransportResumeStatusCallback = setTransportResumeStatusCallback;
1376 0 : agentParam.getSwitchRanksCallback = getSwitchRanksCallback;
1377 0 : agentParam.isEnableBackupLink = IsEnableBackupLink();
1378 0 : agentParam.isEnableSdmaRetry = commConfig_.GetConfigInterServerRetryEnable();
1379 0 : agentParam.agentInfo = agentInfo;
1380 :
1381 0 : CHK_RET(opRetryManager_->RegisterOpRetryMachine(
1382 : agentParam, userRankSize_, commConnections_.isRoot, commConnections_.serverConnections, serverInfo));
1383 0 : HCCL_RUN_INFO(
1384 : "[InitOpRetry] group[%s], isEnableBackupLink[%d], g_enableBackupLinkCommCount[%u]", identifier_.c_str(),
1385 : IsEnableBackupLink(), g_enableBackupLinkCommCount.load());
1386 0 : }
1387 523 : return HCCL_SUCCESS;
1388 : }
1389 :
1390 1 : bool HcclCommunicator::CompareWithServerId(const ServerInfo_t& left, const ServerInfo_t& right)
1391 : {
1392 1 : return (strcmp(left.serverId.c_str(), right.serverId.c_str()) < 0);
1393 : }
1394 :
1395 0 : bool HcclCommunicator::CompareWithNicName(const NetworkInfo_t& left, const NetworkInfo_t& right)
1396 : {
1397 0 : return (strcmp(left.ethName.c_str(), right.ethName.c_str()) < 0);
1398 : }
1399 :
1400 0 : bool HcclCommunicator::CompareWithUserRank(const RankInfo& left, const RankInfo& right)
1401 : {
1402 0 : return left.userRank < right.userRank;
1403 : }
1404 :
1405 307 : HcclResult HcclCommunicator::InitPreResource(const RankTable_t& rankTable)
1406 : {
1407 307 : if (static_cast<s32>(devicePhyId_) == HOST_DEVICE_ID) {
1408 0 : HCCL_ERROR("[Init][PreResource]not support cpu rank");
1409 0 : return HCCL_E_NOT_SUPPORT;
1410 : }
1411 : (void)rankTable;
1412 : // 判断是否为A3多docker场景,该场景需要使用sdid获取到的serverId判断是否属于同一server,若属于同一server则需要enablep2p
1413 307 : if (deviceType_ == DevType::DEV_TYPE_910_93) {
1414 16 : uint32_t localRankServerId = 0;
1415 16 : uint32_t remoteRankServerId = 0;
1416 16 : rtError_t ret = rtGetServerIDBySDID(rankInfoList_[userRank_].superDeviceId, &localRankServerId);
1417 16 : CHK_PRT_RET(
1418 : ret != RT_ERROR_NONE,
1419 : HCCL_ERROR(
1420 : "[Init][PreResource]rtGetServerIDBySDID failed sdid[0x%08x], serverID[%u], ret[%u]",
1421 : rankInfoList_[userRank_].superDeviceId, localRankServerId, ret),
1422 : HCCL_E_RUNTIME);
1423 65 : for (size_t index = 0; index < rankInfoList_.size(); ++index) {
1424 49 : const RankInfo& rankInfo = rankInfoList_[index];
1425 49 : ret = rtGetServerIDBySDID(rankInfo.superDeviceId, &remoteRankServerId);
1426 49 : CHK_PRT_RET(
1427 : ret != RT_ERROR_NONE,
1428 : HCCL_ERROR(
1429 : "[Init][PreResource]rtGetServerIDBySDID failed sdid[0x%08x], serverID[%u], ret[%u]",
1430 : rankInfo.superDeviceId, remoteRankServerId, ret),
1431 : HCCL_E_RUNTIME);
1432 49 : if (serverId_ != rankInfo.serverId && localRankServerId == remoteRankServerId) {
1433 8 : enableP2PDevices_.push_back(rankInfo.devicePhyId);
1434 8 : enableP2PRankIds_.insert(rankInfo.userRank);
1435 8 : HCCL_INFO(
1436 : "[Init][PreResource]localRankID[%u]-localDevicePhyId[%u] needs to enablep2p with "
1437 : "remoteRankId[%u]-remoteDevicePhyId[%u], "
1438 : "and localServerId[%s], localServerIdBySDID[%u], remoteServerId[%s], remoteServerIdBySDID[%u]",
1439 : userRank_, rankInfoList_[userRank_].devicePhyId, rankInfo.userRank, rankInfo.devicePhyId,
1440 : serverId_.c_str(), localRankServerId, rankInfo.serverId.c_str(), remoteRankServerId);
1441 : }
1442 : }
1443 : }
1444 : // 查询本rank所在服务器
1445 307 : auto iterServ = servRankInfo_.find(serverId_);
1446 :
1447 304 : bool check = (iterServ == servRankInfo_.end());
1448 304 : CHK_PRT_RET(
1449 : check, HCCL_ERROR("[Init][PreResource]can't find serverId[%s] in server map", serverId_.c_str()),
1450 : HCCL_E_NOT_FOUND);
1451 :
1452 1246 : for (u32 i = 0; i < iterServ->second.size(); i++) {
1453 937 : if (iterServ->second[i].deviceInfo.devicePhyId != HOST_DEVICE_ID) {
1454 935 : enableP2PDevices_.push_back(iterServ->second[i].deviceInfo.devicePhyId);
1455 940 : enableP2PRankIds_.insert(iterServ->second[i].rankId);
1456 937 : HCCL_INFO(
1457 : "[Init][PreResource]In the current server[%s], localRank[%u]-localDevicePhyId[%u] needs to enableP2P "
1458 : "with remoteRankId[%u]-remoteDevicePhyId[%u]",
1459 : serverId_.c_str(), userRank_, rankInfoList_[userRank_].devicePhyId, iterServ->second[i].rankId,
1460 : iterServ->second[i].deviceInfo.devicePhyId);
1461 : }
1462 : }
1463 307 : HCCL_INFO(
1464 : "[Init][PreResource]Current deviceType[%d], isStandardCard[%s]", deviceType_,
1465 : isStandardCard_ ? "true" : "false");
1466 307 : if (deviceType_ != DevType::DEV_TYPE_310P3 && !isStandardCard_) {
1467 155 : HcclResult ret = P2PMgmtPub::EnableP2P(enableP2PDevices_);
1468 155 : CHK_PRT_RET(
1469 : ret != HCCL_SUCCESS,
1470 : HCCL_ERROR("[Init][PreResource]Enable P2P Failed, deviceLogicId[%d], ret[%u]", deviceLogicId_, ret), ret);
1471 : }
1472 :
1473 307 : drvInit_ = true;
1474 307 : return HCCL_SUCCESS;
1475 : }
1476 :
1477 497 : HcclResult HcclCommunicator::InitTcpMode(const RankTable_t& rankTable) const
1478 : {
1479 497 : bool isTcpMode = false;
1480 497 : HCCL_INFO("[TcpMode][%u] [1:TCP, 2:RDMA, 3:RESERVED]", GetExternalInputProtocolType());
1481 498 : if (GetExternalInputProtocolType() == ProtocolType::TCP) {
1482 0 : isTcpMode = true;
1483 498 : } else if (GetExternalInputProtocolType() == ProtocolType::RDMA) {
1484 : // 通信协议选择RDMA
1485 : } else {
1486 296 : isTcpMode = (rankTable.nicDeploy == NICDeployment::NIC_DEPLOYMENT_HOST);
1487 296 : HCCL_INFO("[Init][TcpMode]isTcpMode[%d] nicDeploy[%d]", isTcpMode, rankTable.nicDeploy);
1488 : }
1489 498 : SetTcpMode(isTcpMode);
1490 :
1491 : // 异构场景解析外部输入,放在SetTcpMode前防止Tcp用例走错分支,放在RecordProtocolType确保hdc模式下建链通信协议校验正确
1492 497 : CHK_RET(InitExternalInputHeterog());
1493 498 : return HCCL_SUCCESS;
1494 : }
1495 :
1496 491 : HcclResult HcclCommunicator::InitMyRankConnectMode(const HcclCommParams& params, const RankTable_t& rankTable)
1497 : {
1498 491 : if (deviceType_ != DevType::DEV_TYPE_910B) { /* 910B才支持host网卡特性 */
1499 326 : myRankConnectMode_ = 0;
1500 326 : return HCCL_SUCCESS;
1501 : }
1502 165 : uint32_t localRank = params.rank;
1503 165 : if (rankTable.nicDeploy != NICDeployment::NIC_DEPLOYMENT_HOST) {
1504 165 : myRankConnectMode_ = 0;
1505 165 : return HCCL_SUCCESS;
1506 : }
1507 0 : for (auto it : rankTable.rankList) {
1508 0 : if (it.rankId == localRank) {
1509 0 : continue;
1510 : }
1511 0 : if (it.deviceInfo.nicDeploy == NICDeployment::NIC_DEPLOYMENT_DEVICE) {
1512 0 : myRankConnectMode_ = 1;
1513 0 : return HCCL_SUCCESS;
1514 : }
1515 0 : }
1516 0 : myRankConnectMode_ = 0;
1517 0 : return HCCL_SUCCESS;
1518 : }
1519 :
1520 234 : uint32_t HcclCommunicator::GetConnectMode() const { return myRankConnectMode_; }
1521 :
1522 1800 : bool HcclCommunicator::IsEnableBackupLink()
1523 : {
1524 224 : return deviceType_ == DevType::DEV_TYPE_910_93 && IsEnableRoce() && GetAicpuUnfoldConfig() && retryEnable_
1525 0 : && commConfig_.GetConfigInterSuperPodRetryEnable() && !devBackupIpAddr_[0].IsInvalid()
1526 2022 : && rtsSupportChangeLink_ && !isDiffDeviceType_;
1527 : }
1528 :
1529 162 : HcclResult HcclCommunicator::InitRaNetResource()
1530 : {
1531 162 : CHK_RET(IsHostUseDevNic(isHostUseDevNic_));
1532 :
1533 162 : if (static_cast<s32>(devicePhyId_) != HOST_DEVICE_ID || nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_DEVICE) {
1534 162 : CHK_RET(HcclNetInit(NICDeployment::NIC_DEPLOYMENT_DEVICE, devicePhyId_, deviceLogicId_, false));
1535 161 : if (IsEnableBackupLink()) {
1536 : // 超节点 && level2支持重执行 && Aicpu -> 初始化主备hccp资源(Pid粒度)
1537 1 : CHK_RET(hrtGetPairDevicePhyId(devicePhyId_, deviceBackUpPhyId_));
1538 1 : if (hrtGetDeviceIndexByPhyId(deviceBackUpPhyId_, deviceBackUpLogicId_) != HCCL_SUCCESS) {
1539 1 : rtsSupportChangeLink_ = false;
1540 1 : HCCL_ERROR(
1541 : "[%s]Runtime does not support changelink, deviceLogicId_[%d], devicePhyId_[%u], "
1542 : "deviceBackUpPhyId_[%u], deviceBackUpLogicId_[%u], nicDeployment_[%d], IsEnableBackupLink[%d]"
1543 : "rtsSupportChangeLink_[%d]",
1544 : __func__, deviceLogicId_, devicePhyId_, deviceBackUpPhyId_, deviceBackUpLogicId_, nicDeployment_,
1545 : IsEnableBackupLink(), rtsSupportChangeLink_);
1546 1 : return HCCL_E_NOT_SUPPORT;
1547 : } else {
1548 0 : CHK_RET(HcclNetInit(
1549 : NICDeployment::NIC_DEPLOYMENT_DEVICE, deviceBackUpPhyId_, deviceBackUpLogicId_, false, true));
1550 0 : HCCL_DEBUG(
1551 : "[%s]Default & backup NetworkManager Init, deviceLogicId[%d], devicePhyId[%u], "
1552 : "deviceBackUpPhyId_[%u], deviceBackUpLogicId_[%u], nicDeployment_[%d], IsEnableBackupLink[%d]",
1553 : __func__, deviceLogicId_, devicePhyId_, deviceBackUpPhyId_, deviceBackUpLogicId_, nicDeployment_,
1554 : IsEnableBackupLink());
1555 : }
1556 : }
1557 : }
1558 :
1559 160 : if ((static_cast<s32>(devicePhyId_) != HOST_DEVICE_ID && isHaveCpuRank_)
1560 160 : || (IsEnableRoce() && nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST)
1561 320 : || (Is310PDevice() && nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST)) {
1562 0 : u32 devicePhyID = (static_cast<s32>(devicePhyId_) == HOST_DEVICE_ID) ? 0 : devicePhyId_;
1563 0 : u32 whiteListEn = (GetExternalInputHcclEnableWhitelist() == HCCL_WHITELIST_ON ? 1 : 0);
1564 0 : CHK_RET(HcclNetInit(NICDeployment::NIC_DEPLOYMENT_HOST, devicePhyID, deviceLogicId_, whiteListEn));
1565 : }
1566 160 : return HCCL_SUCCESS;
1567 : }
1568 :
1569 160 : HcclResult HcclCommunicator::InitRaNic()
1570 : {
1571 160 : CHK_RET(InitSocketManager());
1572 :
1573 160 : if (Is310PDevice()) {
1574 0 : CHK_RET(InitNic());
1575 160 : } else if (static_cast<s32>(devicePhyId_) != HOST_DEVICE_ID) {
1576 160 : std::shared_ptr<HcclSocket>& devVnicSocket = commPortConfig_.devVnicListen.first;
1577 160 : if (devVnicSocket) {
1578 1 : localVnicIp_ = devVnicSocket->GetLocalIp();
1579 1 : localVnicListenPort_ = devVnicSocket->GetLocalPort();
1580 1 : HcclNetDevCtx& devVnicCtx = commPortConfig_.devVnicListen.second;
1581 1 : CHK_PTR_NULL(devVnicCtx);
1582 1 : netDevCtxMap_.insert(std::make_pair(localVnicIp_, devVnicCtx));
1583 1 : CHK_RET(socketManager_->ServerInit(devVnicCtx, localVnicListenPort_));
1584 1 : commPortConfig_.devVnicListen.second = nullptr;
1585 1 : HCCL_INFO(
1586 : "[HcclCommunicator][InitRaResource] init vnic with listened socket success, "
1587 : "listened ip[%s] port[%u]",
1588 : localVnicIp_.GetReadableAddress(), localVnicListenPort_);
1589 : } else {
1590 159 : localVnicListenPort_ = GetLocalNicPort(NicType::VNIC_TYPE);
1591 159 : localVnicIp_ = HcclIpAddress(devicePhyId_);
1592 159 : if (useSuperPodMode_) {
1593 6 : CHK_RET(hrtRaGetSingleSocketVnicIpInfo(
1594 : devicePhyId_, DeviceIdType::DEVICE_ID_TYPE_SDID, superDeviceId_, localVnicIp_));
1595 : } else {
1596 159 : CHK_RET(hrtRaGetSingleSocketVnicIpInfo(
1597 : devicePhyId_, DeviceIdType::DEVICE_ID_TYPE_PHY_ID, devicePhyId_, localVnicIp_));
1598 : }
1599 :
1600 : HcclNetDevCtx vnicPortCtx;
1601 159 : CHK_RET(HcclNetOpenDev(&vnicPortCtx, NicType::VNIC_TYPE, devicePhyId_, deviceLogicId_, localVnicIp_));
1602 159 : CHK_PTR_NULL(vnicPortCtx);
1603 159 : netDevCtxMap_.insert(std::make_pair(localVnicIp_, vnicPortCtx));
1604 159 : CHK_RET(socketManager_->ServerInit(vnicPortCtx, localVnicListenPort_));
1605 153 : HCCL_INFO(
1606 : "[HcclCommunicator][InitRaResource] init vnic with ip[%s] port[%u] success",
1607 : localVnicIp_.GetReadableAddress(), localVnicListenPort_);
1608 : }
1609 :
1610 154 : if (IsEnableRoce()) {
1611 0 : CHK_RET(InitNic());
1612 : }
1613 : }
1614 154 : return HCCL_SUCCESS;
1615 : }
1616 :
1617 278 : HcclResult HcclCommunicator::InitRaResource()
1618 : {
1619 : /* 本通信域内只有1个device时,不需要初始化ra资源 */
1620 278 : if (userRankSize_ <= 1) {
1621 116 : HCCL_INFO("user rank size <= 1, ra is not needed for single device.");
1622 116 : return HCCL_SUCCESS;
1623 : }
1624 :
1625 162 : CHK_RET(InitRaNetResource());
1626 160 : CHK_RET(InitRaNic());
1627 :
1628 154 : HCCL_INFO(
1629 : "isUsedRdmaLevel0_[%u] nicNum[%u] hostIP[%s], nicDeployment[%d].", isUsedRdmaLevel0_, devIpAddr_.size(),
1630 : hostIp_.GetReadableAddress(), nicDeployment_);
1631 :
1632 154 : raResourceInit_ = true; // 全局通信域会初始化,子通信域不会初始化,但是析构均会进入此逻辑,需要标记
1633 154 : attrCollector_.GenSupportRdmaLite();
1634 154 : CHK_RET(attrCollector_.GenSupportHccsAndSio());
1635 154 : isSupportRdmaLite_ = attrCollector_.GetSupportRdmaLite(); // 是否支持Rdma Lite
1636 154 : isSupportHccsAndSio_ = attrCollector_.GetSupportHccsAndSio(); // 是否支持Hccs Sio并发
1637 154 : return HCCL_SUCCESS;
1638 : }
1639 :
1640 307 : HcclResult HcclCommunicator::DisablePreResource()
1641 : {
1642 : // 查询本rank所在服务器
1643 307 : auto iterServ = servRankInfo_.find(serverId_);
1644 307 : bool check = (iterServ == servRankInfo_.end());
1645 307 : CHK_PRT_RET(
1646 : check, HCCL_ERROR("[Disable][PreResource]can't find serverId[%s] in server map", serverId_.c_str()),
1647 : HCCL_E_NOT_FOUND);
1648 307 : HcclResult ret = P2PMgmtPub::DisableP2P(enableP2PDevices_);
1649 307 : CHK_PRT_RET(
1650 : ret != HCCL_SUCCESS,
1651 : HCCL_ERROR("[Disable][PreResource]Disable all P2P Failed, deviceLogicId[%d], ret[%u]", deviceLogicId_, ret),
1652 : ret);
1653 307 : enableP2PDevices_.clear();
1654 307 : enableP2PRankIds_.clear();
1655 306 : return HCCL_SUCCESS;
1656 : }
1657 :
1658 157 : HcclResult HcclCommunicator::GetWorkspaceSubStreamNum(
1659 : u64 count, HcclDataType dataType, HcclReduceOp op, const std::string& algName, u64& streamNum, u64 dataSize,
1660 : bool ifAiv, HcclCMDType opType)
1661 : {
1662 157 : AlgType algType;
1663 :
1664 157 : CHK_RET(GetAlgType(algType, opType));
1665 :
1666 : std::map<HcclCMDType, u64> gapMap
1667 : = {{HcclCMDType::HCCL_CMD_REDUCE_SCATTER, HCCL_SMALL_COUNT_512_KB + HCCL_SMALL_COUNT_512_KB},
1668 : {HcclCMDType::HCCL_CMD_ALLGATHER, HCCL_SMALL_COUNT_512_KB + HCCL_SMALL_COUNT_512_KB},
1669 311 : {HcclCMDType::HCCL_CMD_ALLREDUCE, (HCCL_SMALL_COUNT_512_KB + HCCL_SMALL_COUNT_512_KB) * userRankSize_}};
1670 :
1671 : // 图模式下AIV展开,需要重新计算streamNum
1672 158 : bool ifHcomWithAiv = ifAiv && (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB);
1673 158 : HCCL_INFO(
1674 : "[GetWorkspaceSubStreamNum] ifAiv[%d], workflowMode[%d], ifHcomWithAiv[%d]", ifAiv, GetWorkflowMode(),
1675 : ifHcomWithAiv);
1676 158 : if (ifHcomWithAiv && (deviceType_ == DevType::DEV_TYPE_910_93 || deviceType_ == DevType::DEV_TYPE_910B)) {
1677 0 : HCCL_INFO("[GetWorkspaceSubStreamNum] Hcom AIV enabled, calculating the streamNum.");
1678 : // A3 和 A2 公用以下的参数
1679 0 : std::string newTag;
1680 0 : std::unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(opType);
1681 0 : CHK_SMART_PTR_NULL(algOperator);
1682 0 : OpParam param;
1683 0 : param.reduceType = op;
1684 0 : param.opType = opType;
1685 :
1686 0 : if (opType == HcclCMDType::HCCL_CMD_ALLTOALL || opType == HcclCMDType::HCCL_CMD_ALLTOALLV) {
1687 0 : param.All2AllDataDes.sendType = dataType;
1688 0 : param.All2AllDataDes.recvType = dataType;
1689 0 : param.All2AllDataDes.sendCount = count;
1690 : } else { // 不论 A2 还是 A3,AIV场景下的AllReduce/ReduceScatter还是A2上单独支持AIV的算子都用以下参数
1691 0 : param.DataDes.count = count;
1692 0 : param.DataDes.dataType = dataType;
1693 : }
1694 0 : AlgResourceRequest resRequest;
1695 0 : CHK_RET(algOperator->CalcResRequest(algName, param, resRequest)); // 计算资源请求
1696 0 : streamNum = resRequest.streamNum;
1697 0 : HCCL_INFO(
1698 : "[GetWorkspaceSubStreamNum] Hcom AIV enabled on DeviceType[%d], the streamNum is [%llu]", deviceType_,
1699 : streamNum);
1700 0 : return HCCL_SUCCESS;
1701 0 : }
1702 :
1703 155 : if (serverNum_ == 1 && deviceType_ == DevType::DEV_TYPE_910_93 && opType == HcclCMDType::HCCL_CMD_ALLGATHER
1704 313 : && dataSize <= gapMap[opType] && deviceNumPerAggregation_ > HCCL_DEVICE_NUM_TWO) {
1705 0 : constexpr u64 streamForSmallCount = 3;
1706 0 : streamNum = streamForSmallCount;
1707 0 : HCCL_DEBUG("[GetWorkspaceSubStreamNum]DEV_TYPE_910_93 Single Server, the streamNum is %llu", streamNum);
1708 0 : return HCCL_SUCCESS;
1709 : }
1710 :
1711 155 : if (serverNum_ == 1 && deviceType_ == DevType::DEV_TYPE_910_93 && gapMap.find(opType) != gapMap.end()
1712 313 : && dataSize <= gapMap[opType] && deviceNumPerAggregation_ > HCCL_DEVICE_NUM_TWO) {
1713 0 : streamNum = deviceNumPerAggregation_ - HCCL_SUB_STREAM_NP_MESH;
1714 0 : HCCL_DEBUG("[GetWorkspaceSubStreamNum]DEV_TYPE_910_93 Single Server, the streamNum is %llu", streamNum);
1715 0 : return HCCL_SUCCESS;
1716 : }
1717 :
1718 158 : if (deviceType_ == DevType::DEV_TYPE_910_93) {
1719 8 : streamNum = HCCL_SUB_STREAM_NUM_DOUBLE_RING + RDMA_PLANE_NUM_IN_NPRING_DOUBLE;
1720 8 : if (algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_NP_DOUBLE_RING) {
1721 0 : streamNum += 1U; // semi_ring算法server内增加一条从流,需要2条从流
1722 : }
1723 8 : if (opType == HcclCMDType::HCCL_CMD_ALLTOALLV || opType == HcclCMDType::HCCL_CMD_ALLTOALL
1724 8 : || opType == HcclCMDType::HCCL_CMD_ALLTOALLVC) {
1725 0 : streamNum = MAX_RANK_SIZE;
1726 : }
1727 8 : HCCL_DEBUG("[GetWorkspaceSubStreamNum]DEV_TYPE_910_93, the streamNum is %llu", streamNum);
1728 8 : return HCCL_SUCCESS;
1729 : }
1730 :
1731 : // AR RS 在开启Strict && 静态图、RSv 在开启确定性 && 静态图时, 需要重新计算StreamNum
1732 300 : if (deviceType_ == DevType::DEV_TYPE_910B
1733 262 : && (((opType == HcclCMDType::HCCL_CMD_ALLREDUCE || opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER)
1734 0 : && GetExternalInputHcclDeterministicV2() == DETERMINISTIC_STRICT)
1735 112 : || (opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V
1736 0 : && GetExternalInputHcclDeterministicV2() != DETERMINISTIC_DISABLE)
1737 112 : || (opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V && !isSingleMeshAggregation_
1738 0 : && !multiModuleDiffDeviceNumMode_ // 多机&对称&图模式
1739 0 : && GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB))) {
1740 : // 图模式 A2规约保序场景,需要重新计算需要的streamNum
1741 0 : streamNum = CalcStreamNumForReduceOrderPreservation();
1742 0 : HCCL_DEBUG("[GetWorkspaceSubStreamNum]A2 reduce order preservation, the streamNum is %llu", streamNum);
1743 0 : return HCCL_SUCCESS;
1744 : }
1745 :
1746 150 : if (deviceType_ == DevType::DEV_TYPE_910B && opType == HcclCMDType::HCCL_CMD_ALLREDUCE
1747 0 : && algType.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_PIPELINE) {
1748 0 : streamNum = userRankSize_ / moduleNum_ - 1;
1749 0 : HCCL_DEBUG("[GetWorkspaceSubStreamNum]A2 pipeline AllReduce, the streamNum is %llu", streamNum);
1750 0 : return HCCL_SUCCESS;
1751 : }
1752 :
1753 : // 设置AG和RS的图模式pipeline算法能够申请的streamNum
1754 150 : if (deviceType_ == DevType::DEV_TYPE_910B && // 910B
1755 112 : algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_NP_MESH && // fullmesh
1756 88 : (opType == HcclCMDType::HCCL_CMD_ALLGATHER || opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER
1757 88 : || opType == HcclCMDType::HCCL_CMD_ALLGATHER_V)
1758 0 : && // AG或RS或AGV
1759 0 : moduleNum_ > 1 && deviceNumPerAggregation_ > 1 && // 多机且每机器出多卡
1760 0 : (moduleNum_ <= MODULE_NUM_FOUR || // "机器数量小于等于4"
1761 0 : dataSize > HCCL_SMALL_COUNT_1_MB || // "大数据量"
1762 0 : algType.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_PIPELINE)) { // "指定level1的算法为pipeline"
1763 0 : streamNum = userRankSize_ / moduleNum_;
1764 0 : HCCL_DEBUG("[GetWorkspaceSubStreamNum]DEV_TYPE_910B, the streamNum is %llu", streamNum);
1765 0 : return HCCL_SUCCESS;
1766 : }
1767 :
1768 : // 设置310P图模式 alltoall 的streamNum
1769 150 : if (deviceType_ == DevType::DEV_TYPE_310P3
1770 4 : && (opType == HcclCMDType::HCCL_CMD_ALLTOALL || opType == HcclCMDType::HCCL_CMD_ALLTOALLV
1771 4 : || opType == HcclCMDType::HCCL_CMD_ALLTOALLVC)) {
1772 0 : streamNum = userRankSize_ * RANK_SET_COMPUTE_CONST;
1773 0 : HCCL_DEBUG("[GetWorkspaceSubStreamNum]DEV_TYPE_310P3, the streamNum is %llu", streamNum);
1774 0 : return HCCL_SUCCESS;
1775 : }
1776 :
1777 : // 根据所用算法,选择所需的从stream数目
1778 150 : switch (algType.algoLevel0) {
1779 88 : case AlgTypeLevel0::ALG_LEVEL0_NP_MESH:
1780 88 : streamNum = userRankSize_ / moduleNum_ - HCCL_SUB_STREAM_NP_MESH;
1781 88 : break;
1782 16 : case AlgTypeLevel0::ALG_LEVEL0_8P_RING:
1783 16 : streamNum = HCCL_SUB_STREAM_NUM_8P_RING;
1784 16 : break;
1785 0 : case AlgTypeLevel0::ALG_LEVEL0_NP_DOUBLE_RING:
1786 0 : streamNum = HCCL_SUB_STREAM_NUM_DOUBLE_RING;
1787 0 : break;
1788 12 : case AlgTypeLevel0::ALG_LEVEL0_4P_MESH:
1789 12 : streamNum = HCCL_SUB_STREAM_NUM_4P_MESH;
1790 12 : break;
1791 34 : default:
1792 34 : streamNum = HCCL_SUB_STREAM_NUM_ZERO;
1793 34 : break;
1794 : }
1795 :
1796 150 : if (SatisfyIntraSuperPod(deviceType_, userRankSize_, useSuperPodMode_, superPodNum_)) {
1797 0 : streamNum = std::max(static_cast<u64>(userRankSize_ - 1u), streamNum);
1798 448 : } else if (FullmeshPairwiseSatisfyHighPerfAlltoallMeshCondition(
1799 148 : deviceType_, meshAggregationRankSize_, useSuperPodMode_,
1800 300 : commConfig_.GetConfigHcclAlgo(HcclCMDType::HCCL_CMD_ALLTOALL))) {
1801 0 : streamNum = std::max(static_cast<u64>(meshAggregationRankSize_ - 1u), streamNum);
1802 : }
1803 :
1804 150 : auto iter = HCCL_ALGO_LEVEL0_NAME_MAP.find(algType.algoLevel0);
1805 149 : CHK_PRT_RET(
1806 : iter == HCCL_ALGO_LEVEL0_NAME_MAP.end(),
1807 : HCCL_ERROR("[GetWorkspaceSubStreamNum]level0: algType[%u] is invalid.", algType.algoLevel0), HCCL_E_INTERNAL);
1808 149 : HCCL_DEBUG(
1809 : "[GetWorkspaceSubStreamNum]hccl algorithm: In level0, using %s algo, the streamNum is %llu",
1810 : iter->second.c_str(), streamNum);
1811 :
1812 150 : u64 sliceNum = CalculatePiplineSliceNum(opType, dataSize, algType, deviceType_, deviceNumPerServer_, serverNum_);
1813 : // 图模式下数据量固定, 按照当前数据量判断是否支持pipline切分并申请从流
1814 150 : if (implAlg_ != nullptr && sliceNum >= MIN_PIPLINE_SLICE_NUM) {
1815 0 : streamNum++;
1816 : }
1817 150 : return HCCL_SUCCESS;
1818 158 : }
1819 :
1820 809 : HcclResult HcclCommunicator::DestroyNetworkResources()
1821 : {
1822 809 : transportManager_ = nullptr;
1823 809 : if (raResourceInit_) {
1824 153 : socketManager_->DestroySockets();
1825 : }
1826 :
1827 : /* 本通信域内只有1个device时,不需要卸载ra资源 */
1828 809 : if (userRankSize_ <= 1) {
1829 209 : HCCL_INFO("user rank size <= 1, ra is not needed for single device");
1830 209 : return HCCL_SUCCESS;
1831 : }
1832 :
1833 : // nic的初始化独立调用,在此单独判断是否需要解初始化
1834 600 : if (nicInitialized_ > 0) {
1835 0 : CHK_RET(DeinitNic());
1836 : }
1837 :
1838 600 : if (raResourceInit_ && (static_cast<s32>(devicePhyId_) != HOST_DEVICE_ID) && !Is310PDevice()) {
1839 153 : CHK_RET(socketManager_->ServerDeInit(netDevCtxMap_[localVnicIp_], localVnicListenPort_));
1840 153 : HcclNetCloseDev(netDevCtxMap_[localVnicIp_]);
1841 153 : netDevCtxMap_.erase(localVnicIp_);
1842 : }
1843 :
1844 598 : CHK_RET(ReleasePreemptSocket());
1845 :
1846 599 : if (raResourceInit_) {
1847 153 : if (static_cast<s32>(devicePhyId_) != HOST_DEVICE_ID
1848 0 : || nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_DEVICE) {
1849 153 : if (IsEnableBackupLink()) {
1850 : // 超节点 && level2支持重执行 && Aicpu -> 释放主备hccp资源
1851 0 : CHK_RET(HcclNetDeInit(NICDeployment::NIC_DEPLOYMENT_DEVICE, devicePhyId_, deviceLogicId_));
1852 0 : CHK_RET(HcclNetDeInit(
1853 : NICDeployment::NIC_DEPLOYMENT_DEVICE, deviceBackUpPhyId_, deviceBackUpLogicId_, true));
1854 0 : HCCL_DEBUG(
1855 : "[%s]Default & backup HcclNetDeInit, deviceLogicId[%d], devicePhyId[%u], "
1856 : "deviceBackUpPhyId_[%u], deviceBackUpLogicId_[%u], nicDeployment_[%d], IsEnableBackupLink[%d]",
1857 : __func__, deviceLogicId_, devicePhyId_, deviceBackUpPhyId_, deviceBackUpLogicId_, nicDeployment_,
1858 : IsEnableBackupLink());
1859 : } else {
1860 152 : CHK_RET(HcclNetDeInit(NICDeployment::NIC_DEPLOYMENT_DEVICE, devicePhyId_, deviceLogicId_));
1861 : }
1862 : }
1863 :
1864 153 : if ((static_cast<s32>(devicePhyId_) != HOST_DEVICE_ID && isHaveCpuRank_)
1865 153 : || (IsEnableRoce() && nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST)
1866 306 : || (Is310PDevice() && nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST)) {
1867 0 : u32 devicePhyID = (static_cast<s32>(devicePhyId_) == HOST_DEVICE_ID) ? 0 : devicePhyId_;
1868 0 : CHK_RET(HcclNetDeInit(NICDeployment::NIC_DEPLOYMENT_HOST, devicePhyID, deviceLogicId_));
1869 : }
1870 :
1871 153 : socketManager_ = nullptr;
1872 : }
1873 :
1874 599 : raResourceInit_ = false;
1875 599 : return HCCL_SUCCESS;
1876 : }
1877 :
1878 155 : HcclResult HcclCommunicator::SetWorkspaceResource(
1879 : const std::string& tag, void* memPtr, u64& maxSize, std::vector<rtStream_t>& stream)
1880 : {
1881 155 : return workSpaceRes_->SetWorkspaceResource(tag, memPtr, maxSize, stream);
1882 : }
1883 :
1884 19 : void HcclCommunicator::DestroyWorkspaceResource(const std::string& tag)
1885 : {
1886 19 : if (workSpaceRes_ == nullptr) {
1887 3 : return;
1888 : }
1889 15 : workSpaceRes_->DestroyWorkspaceResource(tag);
1890 : }
1891 :
1892 439 : HcclResult HcclCommunicator::AtomicInitSet()
1893 : {
1894 878 : CHK_PRT_RET(
1895 : initializedFlag_.test_and_set(),
1896 : HCCL_ERROR(
1897 : "[HcclCommunicator][AtomicInitSet]errNo[0x%016llx] instance "
1898 : "already been initialized",
1899 : HCCL_ERROR_CODE(HCCL_E_INTERNAL)),
1900 : HCCL_E_INTERNAL);
1901 438 : return HCCL_SUCCESS;
1902 : }
1903 :
1904 5 : void HcclCommunicator::AtomicInitClear() { initializedFlag_.clear(); }
1905 :
1906 347 : u32 HcclCommunicator::GetUserRank() const { return realUserRank_; }
1907 :
1908 72 : u32 HcclCommunicator::GetGroupRank() const { return userRank_; }
1909 :
1910 338 : u32 HcclCommunicator::GetRankSize() const { return userRankSize_; }
1911 :
1912 0 : u32 HcclCommunicator::GetRankInParentComm() const { return rankInParentComm_; }
1913 :
1914 0 : bool HcclCommunicator::GetNicInitialized() { return nicInitialized_ > 0; }
1915 :
1916 : /*
1917 : 1. 选择算法
1918 : 2. 计算resource,存到request内
1919 : 3. 创建和分配资源
1920 : */
1921 0 : HcclResult HcclCommunicator::HcclSelectAlg(
1922 : HcclCMDType opType, u64 count, void* counts, HcclDataType dataType, HcclReduceOp op, int32_t aivCoreLimit,
1923 : bool& ifAiv, std::string& algName)
1924 : {
1925 0 : HCCL_INFO(
1926 : "[HcclCommunicator][HcclSelectAlg] start to run with opType[%d], count[%llu], dataType[%d], reduceOp[%d], "
1927 : "aivCoreLimit[%d]",
1928 : opType, count, dataType, op, aivCoreLimit);
1929 0 : ifAiv = false;
1930 0 : if (opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V || opType == HcclCMDType::HCCL_CMD_ALLGATHER_V
1931 0 : || opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV || opType == HcclCMDType::HCCL_CMD_BATCH_WRITE) {
1932 0 : HCCL_INFO("[HcclCommunicator][HcclSelectAlg] opType[%d] no need select AIV algorithm", opType);
1933 0 : return HCCL_SUCCESS;
1934 : }
1935 : /* 选择算法前,先更新成图模式 */
1936 0 : auto originWorkflowMode = GetWorkflowMode();
1937 0 : SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB);
1938 :
1939 0 : std::unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(opType);
1940 0 : CHK_SMART_PTR_NULL(algOperator);
1941 :
1942 0 : OpParam param;
1943 0 : param.reduceType = op;
1944 0 : param.opType = opType;
1945 0 : if (opType == HcclCMDType::HCCL_CMD_ALLTOALL || opType == HcclCMDType::HCCL_CMD_ALLTOALLV
1946 0 : || opType == HcclCMDType::HCCL_CMD_ALLTOALLVC) {
1947 0 : param.All2AllDataDes.sendType = dataType;
1948 0 : param.All2AllDataDes.recvType = dataType;
1949 0 : param.All2AllDataDes.sendCount = count;
1950 0 : } else if (opType == HcclCMDType::HCCL_CMD_ALLGATHER_V || opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V) {
1951 0 : param.VDataDes.counts = counts;
1952 0 : param.VDataDes.dataType = dataType;
1953 : } else {
1954 0 : param.DataDes.count = count;
1955 0 : param.DataDes.dataType = dataType;
1956 : }
1957 :
1958 0 : AlgDesc algDesc;
1959 0 : std::string newTag;
1960 0 : ResourceLimit limit{true, true, 0};
1961 0 : limit.aivCoreLimit = aivCoreLimit;
1962 0 : CHK_RET(algOperator->SelectAlg("", param, limit, algName, algDesc, newTag));
1963 :
1964 : /* 非AIV算法直接返回 */
1965 0 : if (!algDesc.isAivMode) {
1966 0 : HCCL_INFO("[HcclCommunicator][HcclSelectAlg] select non-Aiv alg, early return");
1967 0 : return HCCL_SUCCESS;
1968 : }
1969 :
1970 : /* 完成算法选择和记录后,恢复成原来的模式 */
1971 0 : SetWorkflowMode(originWorkflowMode);
1972 0 : ifAiv = true;
1973 0 : HCCL_INFO("[HcclCommunicator][HcclSelectAlg] compile for aiv, select algName is [%s]", algName.c_str());
1974 0 : return HCCL_SUCCESS;
1975 0 : }
1976 :
1977 0 : HcclResult HcclCommunicator::HcclCalcNumBlocks(
1978 : HcclCMDType opType, u64 count, void* counts, HcclDataType dataType, int32_t aivCoreLimit, std::string& algName,
1979 : u32& numBlocks)
1980 : {
1981 0 : auto originWorkflowMode = GetWorkflowMode();
1982 0 : SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB);
1983 0 : std::unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(opType);
1984 0 : CHK_SMART_PTR_NULL(algOperator);
1985 0 : OpParam param;
1986 :
1987 0 : param.opType = opType;
1988 0 : if (opType == HcclCMDType::HCCL_CMD_ALLTOALL || opType == HcclCMDType::HCCL_CMD_ALLTOALLV
1989 0 : || opType == HcclCMDType::HCCL_CMD_ALLTOALLVC) {
1990 0 : param.All2AllDataDes.sendType = dataType;
1991 0 : param.All2AllDataDes.recvType = dataType;
1992 0 : param.All2AllDataDes.sendCount = count;
1993 0 : } else if (opType == HcclCMDType::HCCL_CMD_ALLGATHER_V || opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V) {
1994 0 : param.VDataDes.counts = counts;
1995 0 : param.VDataDes.dataType = dataType;
1996 : } else {
1997 0 : param.DataDes.count = count;
1998 0 : param.DataDes.dataType = dataType;
1999 : }
2000 :
2001 0 : CHK_PRT_RET(
2002 : algOperator->CalNumBlocks(algName, param, numBlocks, aivCoreLimit) != HCCL_SUCCESS,
2003 : HCCL_ERROR("[%s] CalNumBlocks failed", __func__), HCCL_E_PARA);
2004 0 : SetWorkflowMode(originWorkflowMode);
2005 0 : return HCCL_SUCCESS;
2006 0 : }
2007 :
2008 1 : HcclResult HcclCommunicator::HcclGetAlgExecParam(
2009 : const std::string& tag, HcclCMDType opType, u64 count, void* inputPtr, void* outputPtr, bool clearEnable,
2010 : HcclDataType dataType, HcclReduceOp op, void*& commContext, u64& len, u32 aivCoreLimit)
2011 : {
2012 : /* 将Host申请和注册好的资源,传给AICPU */
2013 : // 1\ algName 从getstr里某一个名字里获取出来(要防止名字重复) commContext & len 从 response里拿
2014 : // 2\ rtmemcopy 先获取一下algoperator对象,用这个调用getalgxxx
2015 1 : AivSuperKernelArgs aivSuperKernelArgs;
2016 1 : SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB);
2017 :
2018 1 : OpParam param;
2019 1 : param.DataDes.count = count;
2020 1 : param.DataDes.dataType = dataType;
2021 1 : param.reduceType = op;
2022 1 : param.tag = tag;
2023 1 : param.inputPtr = inputPtr;
2024 1 : param.outputPtr = outputPtr;
2025 1 : param.opType = opType;
2026 : u64 totalSize;
2027 1 : std::vector<u64> sendCountMatrix(userRankSize_ * userRankSize_, count);
2028 1 : if (opType == HcclCMDType::HCCL_CMD_ALLTOALL) {
2029 0 : param.All2AllDataDes.sendType = dataType;
2030 0 : param.All2AllDataDes.recvType = dataType;
2031 0 : param.All2AllDataDes.sendCount = count;
2032 0 : param.All2AllDataDes.sendCountMatrix = static_cast<void*>(sendCountMatrix.data());
2033 : }
2034 :
2035 1 : if (opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER || opType == HcclCMDType::HCCL_CMD_ALLTOALL) {
2036 0 : totalSize = count * SIZE_TABLE[dataType] * userRankSize_;
2037 : } else {
2038 1 : totalSize = count * SIZE_TABLE[dataType]; // allreduce就是输入
2039 : }
2040 1 : param.inputSize = totalSize;
2041 1 : std::unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(opType);
2042 1 : CHK_SMART_PTR_NULL(algOperator);
2043 1 : std::string algName;
2044 1 : AlgResourceResponse algResResponse;
2045 1 : std::string newTag;
2046 1 : ResourceLimit limit;
2047 1 : limit.ifLimit = true;
2048 1 : limit.aivCoreLimit = aivCoreLimit;
2049 1 : AlgDesc algDesc;
2050 1 : algDesc.isLastSelect = true;
2051 1 : CHK_RET(algOperator->SelectAlg(param.tag, param, limit, algName, algDesc, newTag));
2052 :
2053 : // 资源创建
2054 1 : InsertNewTagToTagMap(newTag, param.tag);
2055 1 : if (resMap_.find(newTag) == resMap_.end()) {
2056 1 : HCCL_INFO("[HcclCoommunicator][HcclAllocRes] algName[%s], alloc new res", algName.c_str());
2057 1 : AlgResourceRequest resRequest;
2058 1 : CHK_RET(algOperator->CalcResRequest(algName, param, resRequest)); // [重构建议] 计算和alloc可以拆开
2059 1 : CHK_RET(AllocAlgResource(newTag, opType, param, resRequest, resMap_[newTag]));
2060 1 : CHK_RET(algOperator->PrepareCommInfoToDevice(algName, resMap_[newTag]));
2061 : // 暂不作心跳注册
2062 1 : }
2063 :
2064 1 : CHK_RET(algOperator->GetAivExecParam(algName, param, resMap_[newTag], aivSuperKernelArgs));
2065 :
2066 : // gettag
2067 1 : HCCL_INFO("SPK, rank %llu.", userRank_);
2068 : u32 numBlocks;
2069 1 : CHK_PRT_RET(
2070 : algOperator->CalNumBlocks(algName, param, numBlocks, aivCoreLimit) != HCCL_SUCCESS,
2071 : HCCL_ERROR("[%s] CalNumBlocks failed", __func__), HCCL_E_PARA);
2072 1 : if (clearEnable) {
2073 1 : aivOffloadTag_ = 1;
2074 : }
2075 1 : GetAivTag(algDesc.aivTagNum, false, aivSuperKernelArgs.tag); // workflowmode为图模式
2076 1 : aivSuperKernelArgs.numBlocks = numBlocks;
2077 :
2078 1 : HCCL_INFO(
2079 : "SPK, Tag %llu aivCoreLimit %u, numBlocks %llu.", aivSuperKernelArgs.tag, aivCoreLimit,
2080 : aivSuperKernelArgs.numBlocks);
2081 : // clearenable
2082 : // 拷贝到Device
2083 1 : SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
2084 :
2085 1 : void* sendAlgParamMemPtr = nullptr;
2086 : // alloc device 地址
2087 1 : CHK_RET(hrtMalloc(&sendAlgParamMemPtr, sizeof(AivSuperKernelArgs)));
2088 1 : HCCL_INFO("SPK sendalgparam %p.", sendAlgParamMemPtr);
2089 :
2090 1 : HcclResult hcclRet = hrtMemSyncCopy(
2091 : sendAlgParamMemPtr, sizeof(AivSuperKernelArgs), &aivSuperKernelArgs, sizeof(AivSuperKernelArgs),
2092 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE);
2093 1 : if (hcclRet != HCCL_SUCCESS) {
2094 0 : HCCL_ERROR("[HcclCommunicator][%s]hrtMemSyncCopy error, ret[%d]", __func__, hcclRet);
2095 0 : CHK_RET(hrtFree(sendAlgParamMemPtr));
2096 0 : return hcclRet;
2097 : }
2098 1 : commContext = sendAlgParamMemPtr;
2099 1 : len = sizeof(AivSuperKernelArgs);
2100 1 : return HCCL_SUCCESS;
2101 1 : }
2102 :
2103 6 : HcclResult HcclCommunicator::GetAivTag(s32 tagNum, bool isCapture, s32& aivTag)
2104 : {
2105 6 : bool useOpbaseFlag = (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE && !isCapture);
2106 6 : if (useOpbaseFlag) {
2107 2 : aivTag = aivOpbaseTag_;
2108 2 : aivOpbaseTag_ = GetNextAivTag(aivOpbaseTag_, tagNum);
2109 : } else {
2110 4 : aivTag = aivOffloadTag_;
2111 4 : aivOffloadTag_ = GetNextAivTag(aivOffloadTag_, tagNum);
2112 : }
2113 6 : return HCCL_SUCCESS;
2114 : }
2115 :
2116 2 : HcclResult HcclCommunicator::CheckDeviceType(const DevType deviceType) const
2117 : {
2118 2 : if ((deviceType >= DevType::DEV_TYPE_COUNT) || (deviceType < DevType::DEV_TYPE_910)) {
2119 2 : HCCL_ERROR(
2120 : "[Check][DeviceType]errNo[0x%016llx] device Type[%d] out of range[%d, %d]", HCCL_ERROR_CODE(HCCL_E_PARA),
2121 : deviceType, DevType::DEV_TYPE_910, DevType::DEV_TYPE_NOSOC);
2122 2 : return HCCL_E_PARA;
2123 : }
2124 0 : return HCCL_SUCCESS;
2125 : }
2126 :
2127 52 : HcclResult HcclCommunicator::CheckReductionOp(const HcclReduceOp op) const
2128 : {
2129 52 : if ((op >= HCCL_REDUCE_RESERVED) || (op < HCCL_REDUCE_SUM)) {
2130 1 : HCCL_ERROR("[Check][ReductionOp]errNo[0x%016llx] op:[%d] not supported", HCCL_ERROR_CODE(HCCL_E_PARA), op);
2131 1 : return HCCL_E_PARA;
2132 : }
2133 51 : return HCCL_SUCCESS;
2134 : }
2135 :
2136 131 : HcclResult HcclCommunicator::CheckUserRank(const u32 userRank) const
2137 : {
2138 131 : if (userRankSize_ <= userRank) {
2139 3 : HCCL_ERROR(
2140 : "[Check][UserRank]errNo[0x%016llx] userRank:[%u] is out of range[0 ~ %u]", HCCL_ERROR_CODE(HCCL_E_PARA),
2141 : userRank, userRankSize_);
2142 3 : return HCCL_E_PARA;
2143 : }
2144 128 : return HCCL_SUCCESS;
2145 : }
2146 :
2147 98 : HcclResult HcclCommunicator::CheckCount(const u64 count) const
2148 : {
2149 98 : if (count > SYS_MAX_COUNT) {
2150 1 : HCCL_ERROR(
2151 : "[Check][Count]errNo[0x%016llx] count[%llu] is invalid(bigger than MAX count[%llu])",
2152 : HCCL_ERROR_CODE(HCCL_E_PARA), count, SYS_MAX_COUNT);
2153 1 : return HCCL_E_PARA;
2154 : }
2155 97 : return HCCL_SUCCESS;
2156 : }
2157 :
2158 0 : HcclResult HcclCommunicator::GetGroupRanksInfo(const std::vector<u32>& groupRanks, std::vector<RankInfo>& ranksInfo)
2159 : {
2160 0 : ranksInfo.clear();
2161 0 : std::vector<RankInfo> tmpRankInfoList;
2162 0 : tmpRankInfoList.assign(rankInfoList_.begin(), rankInfoList_.end());
2163 :
2164 0 : for (u32 index = 0; index < groupRanks.size(); index++) {
2165 0 : if (tmpRankInfoList.size() <= groupRanks[index]) {
2166 0 : HCCL_ERROR(
2167 : "[Get][GroupRanksInfo]errNo[0x%016llx] groupRanks[%u]=[%u], >= rankinfolist size[%zu]",
2168 : HCCL_ERROR_CODE(HCCL_E_PARA), index, groupRanks[index], tmpRankInfoList.size());
2169 0 : return HCCL_E_PARA;
2170 : }
2171 0 : tmpRankInfoList[groupRanks[index]].userRank = index;
2172 0 : ranksInfo.push_back(tmpRankInfoList[groupRanks[index]]);
2173 0 : HCCL_DEBUG(
2174 : "index: %d userRank: %dhost ip: %s host port: %u dev phy id: %d serverIdx:%d", index,
2175 : tmpRankInfoList[groupRanks[index]].userRank, tmpRankInfoList[groupRanks[index]].hostIp.GetReadableAddress(),
2176 : tmpRankInfoList[groupRanks[index]].hostPort, tmpRankInfoList[groupRanks[index]].devicePhyId,
2177 : tmpRankInfoList[groupRanks[index]].serverIdx);
2178 : }
2179 :
2180 : // 按rank id从小到大的顺序返回
2181 0 : std::sort(ranksInfo.begin(), ranksInfo.end(), CompareWithUserRank);
2182 :
2183 0 : for (u32 index = 0; index < ranksInfo.size(); ++index) {
2184 0 : if (index != ranksInfo[index].userRank) {
2185 0 : HCCL_ERROR(
2186 : "[Get][GroupRanksInfo]errNo[0x%016llx] index[%u] != user rank[%u]", HCCL_ERROR_CODE(HCCL_E_PARA), index,
2187 : ranksInfo[index].userRank);
2188 0 : return HCCL_E_PARA;
2189 : }
2190 : }
2191 0 : return HCCL_SUCCESS;
2192 0 : }
2193 :
2194 0 : HcclResult HcclCommunicator::GetGroupCommonData(WorldGroupInfo& groupCommonData) const
2195 : {
2196 0 : groupCommonData.inlineReduceSwitchOn = inlineReduceSwitchOn_;
2197 0 : groupCommonData.deviceType = deviceType_;
2198 0 : groupCommonData.deviceLogicId = deviceLogicId_;
2199 0 : groupCommonData.profilingInitiated = profilingInitiated_;
2200 0 : groupCommonData.serverId = serverId_;
2201 0 : groupCommonData.phyIdNicInfoMap = rankDevicePhyIdNicInfoMap_;
2202 0 : groupCommonData.worldRankInfoList = rankInfoList_;
2203 0 : groupCommonData.ranksPort = nicRanksPort_;
2204 0 : groupCommonData.vnicRanksPort = vnicRanksPort_;
2205 0 : groupCommonData.useSuperPodMode = useSuperPodMode_;
2206 0 : groupCommonData.devPortSwitchOn = commPortConfig_.devPortSwitchOn;
2207 0 : return HCCL_SUCCESS;
2208 : }
2209 :
2210 91 : HcclResult HcclCommunicator::GetWorkspaceMemSize(
2211 : const std::string& opType, u64 count, HcclDataType dataType, u32& rankSize, u64& memSize, DevType& deviceType) const
2212 : {
2213 91 : return workSpaceRes_->GetWorkspaceMemSize(opType, count, dataType, rankSize, memSize, deviceType);
2214 : }
2215 :
2216 24 : DeviceMem HcclCommunicator::GetWorkspaceScracthMem(const std::string& tag, u64 allocMemSize)
2217 : {
2218 24 : return workSpaceRes_->AllocDeviceMem(tag, allocMemSize);
2219 : }
2220 :
2221 16 : std::vector<Stream> HcclCommunicator::GetWorkspaceSubStreams(const std::string& tag, u32 num)
2222 : {
2223 16 : return workSpaceRes_->AllocSlaveStreams(tag, num);
2224 : }
2225 :
2226 491 : HcclResult HcclCommunicator::InitProfiling()
2227 : {
2228 491 : if (static_cast<s32>(devicePhyId_) == HOST_DEVICE_ID) {
2229 0 : HCCL_ERROR("[Init][Profiling]not support cpu rank");
2230 0 : return HCCL_E_NOT_SUPPORT;
2231 : }
2232 491 : CHK_PRT_RET(profilingInitiated_, HCCL_DEBUG("Profiling plugin has already been Initiated."), HCCL_SUCCESS);
2233 :
2234 491 : if (profilingMode_ != HcomProfilingMode::PROFILING_OPEN && GetExternalInputProfilingMode()) {
2235 0 : profilingMode_ = HcomProfilingMode::PROFILING_OPEN;
2236 0 : profilingOption_ = GetExternalInputProfilingOption();
2237 : }
2238 491 : HCCL_INFO("profiling config information:options[%s], mode[%d]", profilingOption_.c_str(), profilingMode_);
2239 :
2240 : // profilingInitiated_会广播给所有子通信域,用于避免taskInfoSaver的重复初始化
2241 491 : profilingInitiated_ = true;
2242 : // isExecuteProfilingInit_用于记录本impl是否执行了taskInfoSaver的初始化,用于进行对应的释放
2243 491 : isExecuteProfilingInit_ = true;
2244 491 : return HCCL_SUCCESS;
2245 : }
2246 :
2247 490 : HcclResult HcclCommunicator::DeinitProfiling()
2248 : {
2249 490 : CHK_PRT_RET(!profilingInitiated_, HCCL_DEBUG("Profiling plugin has not been Initiated"), HCCL_SUCCESS);
2250 490 : profilingInitiated_ = false;
2251 490 : HCCL_INFO("Profiling is deinitiated.");
2252 491 : return HCCL_SUCCESS;
2253 : }
2254 :
2255 491 : HcclResult HcclCommunicator::RegistTaskExceptionHandler() const
2256 : {
2257 491 : CHK_RET(TaskExceptionHandler::Init());
2258 491 : return HCCL_SUCCESS;
2259 : }
2260 :
2261 809 : HcclResult HcclCommunicator::UnRegistTaskExceptionHandler() const
2262 : {
2263 809 : CHK_RET(TaskExceptionHandler::DeInit());
2264 809 : return HCCL_SUCCESS;
2265 : }
2266 :
2267 0 : HcclResult HcclCommunicator::GetInCCLbuffer(void*& buffer, u64& size)
2268 : {
2269 0 : return cclBufferManager_.GetInCCLbuffer(buffer, size);
2270 : }
2271 :
2272 0 : HcclResult HcclCommunicator::GetOutCCLbuffer(void*& buffer, u64& size)
2273 : {
2274 0 : return cclBufferManager_.GetOutCCLbuffer(buffer, size);
2275 : }
2276 :
2277 0 : void HcclCommunicator::ReleaseCommCCLbuffer() { cclBufferManager_.ReleaseCommCCLbuffer(); }
2278 :
2279 0 : HcclResult HcclCommunicator::ReleaseCommInfos()
2280 : {
2281 0 : if (implAlg_ != nullptr) {
2282 0 : return implAlg_->ReleaseCommInfos();
2283 : }
2284 0 : return HCCL_SUCCESS;
2285 : }
2286 :
2287 312 : HcclResult HcclCommunicator::InitProfiler()
2288 : {
2289 312 : profilerManager_.reset(new (std::nothrow)
2290 312 : ProfilerManager(devicePhyId_, deviceLogicId_, realUserRank_, userRankSize_));
2291 312 : CHK_SMART_PTR_NULL(profilerManager_);
2292 312 : HcclResult ret = profilerManager_->InitProfiler();
2293 312 : CHK_PRT_RET(
2294 : (ret != HCCL_SUCCESS), HCCL_ERROR("[BASE][InitProfiler]profilerManager_ InitProfiler failed."), HCCL_E_PARA);
2295 :
2296 312 : HCCL_INFO("[BASE][InitProfiler]Register CtrlCallBack success.");
2297 312 : return HCCL_SUCCESS;
2298 : }
2299 :
2300 73 : HcclResult HcclCommunicator::CreateCommCCLbuffer()
2301 : {
2302 : // user mem和CCL buffer互斥,不支持同时使用
2303 73 : if (isUserMemRegisted_) {
2304 0 : HCCL_ERROR(
2305 : "[HcclCommunicator][%s]tag[%s]The user mem has been registered, "
2306 : "does not support create CCL Buffer.",
2307 : __func__, identifier_.c_str());
2308 0 : return HCCL_E_NOT_SUPPORT;
2309 : }
2310 73 : return cclBufferManager_.CreateCommCCLbuffer(cclBuffName_);
2311 : }
2312 :
2313 242 : HcclResult HcclCommunicator::InitCCLbuffer(u64 inCCLbufferSize, u64 outCCLbufferSize)
2314 : {
2315 242 : return cclBufferManager_.InitCCLbuffer(inCCLbufferSize, outCCLbufferSize);
2316 : }
2317 :
2318 180 : u32 HcclCommunicator::GetLocalNicPort(NicType nicType)
2319 : {
2320 180 : u32 port = HCCL_INVALID_PORT;
2321 180 : if (nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST) {
2322 0 : return GetHostPort(devicePhyId_);
2323 : }
2324 : // isUseRankPort_在ranksPort初始化时一同配置:1. 异构场景 2. 开启device侧端口配置
2325 : // groupRanksPort_为空说明此时处于全局通信域,要从ranksPort_取监听端口;否则取groupRanksPort_
2326 180 : bool devicePortSwitchOn = commPortConfig_.devPortSwitchOn;
2327 180 : if (nicType == NicType::HOST_NIC_TYPE) {
2328 0 : port = GetHostPort(devicePhyId_);
2329 180 : } else if (devicePortSwitchOn && nicType == NicType::VNIC_TYPE) {
2330 : // vnic ports仅在开启device侧端口配置时单独配置
2331 0 : std::vector<u32>& ranksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
2332 0 : port = GetNicPort(devicePhyId_, ranksPorts, userRank_, isUseRankPort_);
2333 0 : } else {
2334 : // 1. 开启device侧端口配置时的nic port时使用ranksPorts
2335 : // 2. 异构场景使用ranksPorts
2336 : // 3. 其余场景场景isUseRankPort_应当为false,使用默认port
2337 180 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
2338 180 : port = GetNicPort(devicePhyId_, ranksPorts, userRank_, isUseRankPort_);
2339 : }
2340 180 : HCCL_INFO(
2341 : "[HcclCommunicator][GetLocalNicPort] nicType[%u], devicePortSwitchOn[%u], isUseRankPort[%u], "
2342 : "get port[%u], devId[%u]",
2343 : nicType, devicePortSwitchOn, isUseRankPort_, port, devicePhyId_);
2344 180 : return port;
2345 : }
2346 :
2347 1 : HcclResult HcclCommunicator::CheckOneSidedBackupAndSetDevId(
2348 : u32& backupDevPhyId, u32& backupDevLogicId, std::vector<HcclIpAddress>& localIpList,
2349 : bool& isOneSidedTaskAndBackupInitA3)
2350 : {
2351 1 : if (!IsOneSidedIdentifier(identifier_)) {
2352 1 : isOneSidedTaskAndBackupInitA3 = false;
2353 1 : HCCL_INFO("[%s] comm[%s] is not one sided comm.", __func__, identifier_.c_str());
2354 1 : return HCCL_SUCCESS;
2355 : }
2356 0 : DevType deviceType = DevType::DEV_TYPE_COUNT;
2357 0 : CHK_RET(hrtGetDeviceType(deviceType));
2358 0 : if (deviceType != DevType::DEV_TYPE_910_93) {
2359 0 : isOneSidedTaskAndBackupInitA3 = false;
2360 0 : HCCL_INFO(
2361 : "[HcclCommunicator::CheckOneSidedBackupAndSetDevId] DeviceType[%d] is not 910_93, one sided backup not "
2362 : "support",
2363 : static_cast<u32>(deviceType));
2364 0 : return HCCL_SUCCESS;
2365 : }
2366 0 : CHK_RET(hrtGetPairDevicePhyId(devicePhyId_, backupDevPhyId));
2367 :
2368 0 : std::vector<HcclIpAddress> backupIpList;
2369 0 : std::vector<std::vector<HcclIpAddress>> chipDeviceIPs;
2370 0 : CHK_RET(hrtRaGetDeviceAllNicIP(chipDeviceIPs));
2371 0 : u32 ipIdex = 1U - (devicePhyId_ % 2U);
2372 0 : std::copy_if(
2373 0 : chipDeviceIPs[ipIdex].begin(), chipDeviceIPs[ipIdex].end(), std::back_inserter(backupIpList),
2374 0 : [](const HcclIpAddress& ip) {
2375 0 : return !ip.IsIPv6();
2376 : });
2377 0 : HCCL_INFO(
2378 : "devicePhysicID[%u], backupDeviceId[%d], backupDeviceIP[0]:[%s], devIpAddr_[%s], ", devicePhyId_,
2379 : backupDevPhyId, backupIpList[0].GetReadableAddress(), devIpAddr_[0].GetReadableAddress());
2380 0 : CHK_RET(hrtRaGetDeviceIP(devicePhyId_, localIpList));
2381 0 : auto equalToLocal = [this](const HcclIpAddress& entry) {
2382 0 : return entry == devIpAddr_[0];
2383 0 : };
2384 0 : isOneSidedTaskAndBackupInitA3 = any_of(backupIpList.begin(), backupIpList.end(), equalToLocal)
2385 0 : && !any_of(localIpList.begin(), localIpList.end(), equalToLocal);
2386 0 : if (isOneSidedTaskAndBackupInitA3) {
2387 0 : CHK_RET(hrtGetDeviceIndexByPhyId(backupDevPhyId, backupDevLogicId));
2388 : }
2389 :
2390 0 : HCCL_INFO(
2391 : "[HcclCommunicator::CheckOneSidedBackupAndSetDevId] isOneSidedTaskAndBackupInitA3[%s]",
2392 : isOneSidedTaskAndBackupInitA3 ? "true" : "false");
2393 0 : return HCCL_SUCCESS;
2394 0 : }
2395 :
2396 0 : HcclResult HcclCommunicator::OneSidedBackupInitNetResource(
2397 : HcclNetDevCtx& nicPortBackUpCtx, u32& backupDevPhyId, u32& backupDevLogicId,
2398 : std::vector<HcclIpAddress>& localIpList)
2399 : {
2400 0 : devBackupIpAddr_[0] = devIpAddr_[0];
2401 0 : deviceBackUpPhyId_ = backupDevPhyId;
2402 0 : deviceBackUpLogicId_ = backupDevLogicId;
2403 0 : CHK_RET(HcclNetInit(NICDeployment::NIC_DEPLOYMENT_DEVICE, backupDevPhyId, backupDevLogicId, false, true));
2404 0 : HCCL_INFO(
2405 : "[HcclCommunicator::OneSidedBackupInitNetResource] OpenDev with backupDevPhyId[%d], backupDevLogicId[%d], "
2406 : "localIpList[%s], backupIp[%s]",
2407 : backupDevPhyId, backupDevLogicId, localIpList[0].GetReadableAddress(), devIpAddr_[0].GetReadableAddress());
2408 0 : CHK_RET(HcclNetOpenDev(
2409 : &nicPortBackUpCtx, NicType::DEVICE_NIC_TYPE, backupDevPhyId, backupDevLogicId, devIpAddr_[0], localIpList[0]));
2410 0 : return HCCL_SUCCESS;
2411 : }
2412 :
2413 0 : HcclResult HcclCommunicator::OneSidedBackupServerInit(HcclNetDevCtx& nicPortBackUpCtx)
2414 : {
2415 0 : u32 backupPort = HCCL_INVALID_PORT;
2416 0 : for (const auto& rankInfo : rankInfoList_) {
2417 0 : if (rankInfo.userRank == userRank_) {
2418 0 : backupPort = rankInfo.deviceNicPort;
2419 : }
2420 : }
2421 0 : CHK_RET(socketManager_->ServerInit(nicPortBackUpCtx, backupPort));
2422 0 : return HCCL_SUCCESS;
2423 : }
2424 :
2425 0 : HcclResult HcclCommunicator::InitDevicePrimaryNic([[maybe_unused]] bool isMC2ReInit, bool isOneSidedTaskAndBackupInitA3)
2426 : {
2427 0 : std::shared_ptr<HcclSocket>& devNicSocket = commPortConfig_.devNicListen.first;
2428 0 : if (devNicSocket && !isOneSidedTaskAndBackupInitA3) {
2429 0 : HcclNetDevCtx& devNicCtx = commPortConfig_.devNicListen.second;
2430 0 : CHK_PTR_NULL(devNicCtx);
2431 0 : netDevCtxMap_.insert(std::make_pair(devNicSocket->GetLocalIp(), devNicCtx));
2432 0 : CHK_RET(socketManager_->ServerInit(devNicCtx, devNicSocket->GetLocalPort()));
2433 0 : commPortConfig_.devNicListen.second = nullptr;
2434 0 : HCCL_INFO(
2435 : "[HcclCommunicator][InitNic] init nic with listened socket success, "
2436 : "listened ip[%s] port[%u]",
2437 : devNicSocket->GetLocalIp().GetReadableAddress(), devNicSocket->GetLocalPort());
2438 0 : } else if (!isOneSidedTaskAndBackupInitA3) {
2439 0 : u32 port = GetLocalNicPort(NicType::DEVICE_NIC_TYPE);
2440 0 : u32 nicNum = devIpAddr_.size();
2441 0 : for (u32 i = 0; i < nicNum; i++) {
2442 0 : if (devIpAddr_[i].IsInvalid()) {
2443 0 : HCCL_INFO("[Init][Nic]nic num[%u] deviceip is invalid, total nicNum[%u]", i, nicNum);
2444 0 : continue;
2445 : }
2446 : HcclNetDevCtx nicPortCtx;
2447 0 : CHK_RET(HcclNetOpenDev(&nicPortCtx, NicType::DEVICE_NIC_TYPE, devicePhyId_, deviceLogicId_, devIpAddr_[i]));
2448 0 : CHK_PTR_NULL(nicPortCtx);
2449 0 : netDevCtxMap_.insert(std::make_pair(devIpAddr_[i], nicPortCtx));
2450 0 : CHK_RET(socketManager_->ServerInit(nicPortCtx, port));
2451 0 : HCCL_INFO(
2452 : "[HcclCommunicator][InitNic] init nic with ip[%s] port[%u] success", devIpAddr_[i].GetReadableAddress(),
2453 : port);
2454 : }
2455 : }
2456 0 : return HCCL_SUCCESS;
2457 : }
2458 :
2459 0 : HcclResult HcclCommunicator::InitDeviceBackupNic(
2460 : u32 backupDevPhyId, u32 backupDevLogicId, std::vector<HcclIpAddress>& localIpList,
2461 : bool isOneSidedTaskAndBackupInitA3)
2462 : {
2463 0 : if (IsEnableBackupLink() || isOneSidedTaskAndBackupInitA3) {
2464 0 : std::shared_ptr<HcclSocket>& backupNicSocket = commPortConfig_.backupDevNicListen.first;
2465 0 : if (backupNicSocket) {
2466 0 : HcclNetDevCtx& backupNicCtx = commPortConfig_.backupDevNicListen.second;
2467 0 : CHK_PTR_NULL(backupNicCtx);
2468 0 : netDevCtxMap_.insert(std::make_pair(backupNicSocket->GetLocalIp(), backupNicCtx));
2469 0 : CHK_RET(socketManager_->ServerInit(backupNicCtx, backupNicSocket->GetLocalPort()));
2470 0 : commPortConfig_.backupDevNicListen.second = nullptr;
2471 0 : HCCL_INFO(
2472 : "[HcclCommunicator][InitNic] init backup nic with listened socket success, "
2473 : "listened ip[%s] port[%u]",
2474 : backupNicSocket->GetLocalIp().GetReadableAddress(), backupNicSocket->GetLocalPort());
2475 : } else {
2476 : HcclNetDevCtx nicPortBackUpCtx;
2477 0 : if (isOneSidedTaskAndBackupInitA3) {
2478 0 : CHK_RET(OneSidedBackupInitNetResource(nicPortBackUpCtx, backupDevPhyId, backupDevLogicId, localIpList));
2479 : } else {
2480 0 : CHK_RET(HcclNetOpenDev(
2481 : &nicPortBackUpCtx, NicType::DEVICE_NIC_TYPE, deviceBackUpPhyId_, deviceBackUpLogicId_,
2482 : devBackupIpAddr_[0], devIpAddr_[0]));
2483 : }
2484 0 : CHK_PTR_NULL(nicPortBackUpCtx);
2485 0 : netDevCtxMap_.insert(std::make_pair(devBackupIpAddr_[0], nicPortBackUpCtx));
2486 0 : if (isOneSidedTaskAndBackupInitA3) {
2487 0 : CHK_RET(OneSidedBackupServerInit(nicPortBackUpCtx));
2488 : } else {
2489 0 : CHK_RET(socketManager_->ServerInit(nicPortBackUpCtx, devBackupPort_));
2490 : }
2491 0 : HCCL_DEBUG(
2492 : "[%s]finish backup ServerInit, deviceBackUpPhyId_[%u], deviceBackUpLogicId_[%u], "
2493 : "devBackupIpAddr_[%s], devBackupPort_[%u], nicDeployment_[%d], IsEnableBackupLink[%d], "
2494 : "netDevCtxMap_.size[%d]",
2495 : __func__, deviceBackUpPhyId_, deviceBackUpLogicId_, devBackupIpAddr_[0].GetReadableAddress(),
2496 : devBackupPort_, nicDeployment_, IsEnableBackupLink(), netDevCtxMap_.size());
2497 0 : HCCL_INFO(
2498 : "[HcclCommunicator][InitNic] init backup nic with ip[%s] port[%u] success",
2499 : devBackupIpAddr_[0].GetReadableAddress(), devBackupPort_);
2500 : }
2501 : }
2502 0 : return HCCL_SUCCESS;
2503 : }
2504 :
2505 0 : HcclResult HcclCommunicator::InitNicDeviceDeploy(
2506 : bool isMC2ReInit, u32 backupDevPhyId, u32 backupDevLogicId, std::vector<HcclIpAddress>& localIpList,
2507 : bool isOneSidedTaskAndBackupInitA3)
2508 : {
2509 0 : CHK_RET(InitDevicePrimaryNic(isMC2ReInit, isOneSidedTaskAndBackupInitA3));
2510 0 : attrCollector_.GenUsedRdmaLevel0();
2511 0 : isUsedRdmaLevel0_ = attrCollector_.GetUsedRdmaLevel0();
2512 0 : CHK_RET(InitDeviceBackupNic(backupDevPhyId, backupDevLogicId, localIpList, isOneSidedTaskAndBackupInitA3));
2513 0 : return HCCL_SUCCESS;
2514 : }
2515 :
2516 0 : HcclResult HcclCommunicator::InitNicHostDeploy()
2517 : {
2518 0 : u32 port = GetLocalNicPort(NicType::HOST_NIC_TYPE);
2519 0 : CHK_PRT_RET(
2520 : (hostIp_.IsInvalid()),
2521 : HCCL_ERROR("[Init][Nic] host ip is invalid when NIC "
2522 : "deployment is host. "),
2523 : HCCL_E_PARA);
2524 0 : attrCollector_.GenUsedRdmaLevel0();
2525 0 : isUsedRdmaLevel0_ = attrCollector_.GetUsedRdmaLevel0();
2526 0 : u32 devicePhyID = (static_cast<s32>(devicePhyId_) == HOST_DEVICE_ID) ? 0 : devicePhyId_;
2527 :
2528 0 : u32 i = 0;
2529 : HcclNetDevCtx nicPortCtx;
2530 0 : for (i = 0; i < devIpAddr_.size(); i++) {
2531 0 : if (devIpAddr_[i].IsInvalid()) {
2532 0 : HCCL_INFO("[Init][Nic]nic num[%u] deviceip is invalid, total nicNum[%u]", i, devIpAddr_.size());
2533 0 : continue;
2534 : }
2535 0 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
2536 0 : port = GetNicPort(devicePhyId_, ranksPorts, userRank_, isUseRankPort_);
2537 0 : CHK_RET(HcclNetOpenDev(&nicPortCtx, NicType::HOST_NIC_TYPE, devicePhyId_, deviceLogicId_, devIpAddr_[i]));
2538 0 : CHK_PTR_NULL(nicPortCtx);
2539 0 : netDevCtxMap_.insert(std::make_pair(devIpAddr_[i], nicPortCtx));
2540 0 : HcclNetDevSetProtoType(nicPortCtx, HCCL_PROTO_TYPE_ROCE);
2541 0 : break;
2542 : }
2543 :
2544 0 : if (i == devIpAddr_.size()) {
2545 0 : port = GetLocalNicPort(NicType::HOST_NIC_TYPE);
2546 0 : CHK_RET(HcclNetOpenDev(&nicPortCtx, NicType::HOST_NIC_TYPE, devicePhyId_, deviceLogicId_, hostIp_));
2547 0 : CHK_PTR_NULL(nicPortCtx);
2548 0 : netDevCtxMap_.insert(std::make_pair(hostIp_, nicPortCtx));
2549 : }
2550 0 : HCCL_INFO("[Init][Nic], hostPort[%u], devicePhyID[%u]", port, devicePhyID);
2551 0 : CHK_RET(socketManager_->ServerInit(nicPortCtx, port));
2552 0 : return HCCL_SUCCESS;
2553 : }
2554 :
2555 1 : HcclResult HcclCommunicator::InitNic(bool isMC2ReInit)
2556 : {
2557 1 : if (GetExternalInputIntraRoceSwitch() == 0 && servRankInfo_.size() == 1 && isDiffDeviceModule_ && !isMC2ReInit) {
2558 0 : return HCCL_SUCCESS;
2559 : }
2560 1 : u32 backupDevPhyId = INVALID_INT;
2561 1 : u32 backupDevLogicId = INVALID_INT;
2562 1 : bool isOneSidedTaskAndBackupInitA3 = false;
2563 1 : vector<HcclIpAddress> localIpList;
2564 1 : CHK_RET(
2565 : CheckOneSidedBackupAndSetDevId(backupDevPhyId, backupDevLogicId, localIpList, isOneSidedTaskAndBackupInitA3));
2566 :
2567 1 : if (nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_DEVICE) {
2568 0 : CHK_RET(InitNicDeviceDeploy(
2569 : isMC2ReInit, backupDevPhyId, backupDevLogicId, localIpList, isOneSidedTaskAndBackupInitA3));
2570 1 : } else if (nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST) {
2571 0 : CHK_RET(InitNicHostDeploy());
2572 : } else {
2573 1 : HCCL_ERROR("[Init][Nic]nic deployment[%d] is not supported", nicDeployment_);
2574 1 : return HCCL_E_PARA;
2575 : }
2576 0 : isNeedInitNic_ = true;
2577 0 : attrCollector_.SetNeedInitNicFlag(isNeedInitNic_);
2578 0 : nicInitialized_++;
2579 0 : return HCCL_SUCCESS;
2580 1 : }
2581 :
2582 0 : HcclResult HcclCommunicator::DeinitNicHostDeploy()
2583 : {
2584 0 : u32 port = GetLocalNicPort(NicType::HOST_NIC_TYPE);
2585 0 : CHK_PRT_RET(
2586 : (hostIp_.IsInvalid()),
2587 : HCCL_ERROR("[DeInit][Nic] host ip is invalid when NIC "
2588 : "deployment is host. "),
2589 : HCCL_E_PARA);
2590 :
2591 0 : u32 i = 0;
2592 0 : for (i = 0; i < devIpAddr_.size(); i++) {
2593 0 : if (devIpAddr_[i].IsInvalid()) {
2594 0 : HCCL_INFO("[Init][Nic]nic num[%u] deviceip is invalid, total nicNum[%u]", i, devIpAddr_.size());
2595 0 : continue;
2596 : }
2597 0 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
2598 0 : port = GetNicPort(devicePhyId_, ranksPorts, userRank_, isUseRankPort_);
2599 :
2600 0 : CHK_RET(socketManager_->ServerDeInit(netDevCtxMap_[devIpAddr_[i]], port));
2601 0 : HcclNetCloseDev(netDevCtxMap_[devIpAddr_[i]]);
2602 0 : netDevCtxMap_.erase(devIpAddr_[i]);
2603 :
2604 0 : break;
2605 : }
2606 :
2607 0 : if (i == devIpAddr_.size()) {
2608 0 : CHK_RET(socketManager_->ServerDeInit(netDevCtxMap_[hostIp_], port));
2609 0 : HcclNetCloseDev(netDevCtxMap_[hostIp_]);
2610 0 : netDevCtxMap_.erase(hostIp_);
2611 : }
2612 0 : return HCCL_SUCCESS;
2613 : }
2614 :
2615 0 : HcclResult HcclCommunicator::DeinitNic()
2616 : {
2617 0 : if (nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_DEVICE) {
2618 0 : u32 port = GetLocalNicPort(NicType::DEVICE_NIC_TYPE);
2619 0 : u32 nicNum = devIpAddr_.size();
2620 0 : for (u32 i = 0; i < nicNum; i++) {
2621 0 : if (devIpAddr_[i].IsInvalid()) {
2622 0 : HCCL_INFO("continue invalid devIp %s", devIpAddr_[i].GetReadableAddress());
2623 0 : continue;
2624 : }
2625 0 : if (netDevCtxMap_.find(devIpAddr_[i]) == netDevCtxMap_.end()) {
2626 0 : HCCL_INFO("devIp[%s] not found in netDevCtxMap_", devIpAddr_[i].GetReadableAddress());
2627 0 : continue;
2628 : }
2629 0 : CHK_RET(socketManager_->ServerDeInit(netDevCtxMap_[devIpAddr_[i]], port));
2630 : // 最后一次调用才删除netCtx
2631 0 : if (nicInitialized_ - 1 <= 0) {
2632 0 : HcclNetCloseDev(netDevCtxMap_[devIpAddr_[i]]);
2633 0 : netDevCtxMap_.erase(devIpAddr_[i]);
2634 : }
2635 : }
2636 0 : if (IsEnableBackupLink() && netDevCtxMap_.find(devBackupIpAddr_[0]) != netDevCtxMap_.end()) {
2637 : // 超节点 && level2支持重执行 && Aicpu -> 备用网卡 deinit
2638 0 : CHK_RET(socketManager_->ServerDeInit(netDevCtxMap_[devBackupIpAddr_[0]], devBackupPort_));
2639 0 : if (nicInitialized_ - 1 <= 0) {
2640 0 : HcclNetCloseDev(netDevCtxMap_[devBackupIpAddr_[0]]);
2641 0 : netDevCtxMap_.erase(devBackupIpAddr_[0]);
2642 0 : HCCL_DEBUG(
2643 : "[%s]finish backup ServerDeInit devBackupIpAddr_[%s], port[%u], IsEnableBackupLink[%d]", __func__,
2644 : devBackupIpAddr_[0].GetReadableAddress(), devBackupPort_, IsEnableBackupLink());
2645 : }
2646 : }
2647 0 : } else if (nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST) {
2648 0 : CHK_RET(DeinitNicHostDeploy());
2649 : } else {
2650 0 : HCCL_ERROR("[Deinit][Nic]nic deployment[%d] is not supported", nicDeployment_);
2651 0 : return HCCL_E_PARA;
2652 : }
2653 0 : nicInitialized_--;
2654 0 : return HCCL_SUCCESS;
2655 : }
2656 :
2657 520 : HcclResult HcclCommunicator::RegisterRanksToDca()
2658 : {
2659 520 : if (deviceType_ != DevType::DEV_TYPE_910_93 && deviceType_ != DevType::DEV_TYPE_910B) {
2660 325 : HCCL_WARNING("[RegisterRanksToDca] not support deviceType[%d]", deviceType_);
2661 325 : return HCCL_SUCCESS;
2662 : }
2663 195 : CHK_RET(setVnicIpToRankInfoList());
2664 198 : DetectConnectionAnomalies::GetInstance(deviceLogicId_).Init(rankInfoList_, isNeedInitNic_);
2665 198 : return HCCL_SUCCESS;
2666 : }
2667 :
2668 0 : HcclResult HcclCommunicator::AddOpInfoToHeartBeat(const OpInfoDesc& opInfo, const std::string& tag)
2669 : {
2670 0 : if (Is310PDevice() || deviceType_ == DevType::DEV_TYPE_310P3
2671 0 : || GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB) {
2672 0 : return HCCL_SUCCESS;
2673 : }
2674 0 : return Heartbeat::GetInstance(deviceLogicId_).AddOpInfoToHeartBeat(identifier_, opInfo, tag);
2675 : }
2676 :
2677 809 : void HcclCommunicator::DeleteOpInfoToHeartBeat()
2678 : {
2679 1618 : if (Is310PDevice() || deviceType_ == DevType::DEV_TYPE_310P3
2680 1618 : || GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB) {
2681 162 : return;
2682 : }
2683 647 : for (const auto& tag : hbSendRecvTags_) {
2684 0 : Heartbeat::GetInstance(deviceLogicId_).DeleteOpInfoToHeartBeat(identifier_, tag);
2685 : }
2686 1941 : Heartbeat::GetInstance(deviceLogicId_).DeleteOpInfoToHeartBeat(identifier_, "");
2687 : }
2688 :
2689 21 : HcclResult HcclCommunicator::RegisterToHeartBeat()
2690 : {
2691 21 : if (Is310PDevice() || deviceType_ == DevType::DEV_TYPE_310P3) {
2692 0 : return HCCL_SUCCESS;
2693 : }
2694 21 : u32 localPort = commPortConfig_.devPortSwitchOn ? HCCL_INVALID_PORT : GetLocalNicPort(NicType::DEVICE_NIC_TYPE);
2695 21 : return Heartbeat::GetInstance(deviceLogicId_)
2696 42 : .RegisterToHeartBeat(
2697 21 : userRank_, deviceType_, rankInfoList_, localPort, isNeedInitNic_, identifier_, useSuperPodMode_,
2698 21 : isUsedRdmaLevel0_, retryEnable_, IsEnableBackupLink());
2699 : }
2700 :
2701 0 : HcclResult HcclCommunicator::RegisterToHeartBeat(u32 peerRankId, string& tag)
2702 : {
2703 0 : u32 localPort = commPortConfig_.devPortSwitchOn ? HCCL_INVALID_PORT : GetLocalNicPort(NicType::DEVICE_NIC_TYPE);
2704 0 : return Heartbeat::GetInstance(deviceLogicId_)
2705 0 : .RegisterToHeartBeat(
2706 0 : userRank_, deviceType_, rankInfoList_, localPort, isNeedInitNic_, peerRankId, identifier_, tag,
2707 0 : useSuperPodMode_, isUsedRdmaLevel0_, retryEnable_, IsEnableBackupLink());
2708 : }
2709 :
2710 809 : void HcclCommunicator::UnRegisterToHeartBeat()
2711 : {
2712 809 : for (auto tag : hbSendRecvTags_) {
2713 0 : Heartbeat::GetInstance(deviceLogicId_).UnRegisterToHeartBeat(deviceType_, identifier_, tag);
2714 0 : }
2715 809 : Heartbeat::GetInstance(deviceLogicId_).UnRegisterToHeartBeat(deviceType_, identifier_);
2716 806 : }
2717 :
2718 809 : void HcclCommunicator::UnRegisterToCommConfiger() { CommConfiger::GetInstance().UnRegisterToCommConfiger(identifier_); }
2719 :
2720 92 : HcclResult HcclCommunicator::SetGlobalWorkSpace(std::vector<void*>& globalWorkSpaceAddr)
2721 : {
2722 92 : CHK_RET(HcclSetGlobalWorkSpace(dispatcher_, globalWorkSpaceAddr));
2723 92 : return HCCL_SUCCESS;
2724 : }
2725 :
2726 0 : HcclResult HcclCommunicator::GetandClearOverFlowTasks(std::vector<HcclDumpInfo>& hcclDumpInfo)
2727 : {
2728 0 : if (profilerManager_ != nullptr) {
2729 0 : CHK_RET(profilerManager_->GetandClearOverFlowTasks(hcclDumpInfo));
2730 : } else {
2731 0 : HCCL_WARNING("[impl][GetDumpTask] profilerManager_ not set");
2732 : }
2733 0 : return HCCL_SUCCESS;
2734 : }
2735 :
2736 236 : HcclResult HcclCommunicator::GetDeviceId(s32& deviceId) const
2737 : {
2738 236 : deviceId = deviceLogicId_;
2739 236 : return HCCL_SUCCESS;
2740 : }
2741 :
2742 1 : HcclResult HcclCommunicator::GetCqeError(HcclResult& result)
2743 : {
2744 1 : CHK_RET(Heartbeat::GetInstance(deviceLogicId_).CheckErrorCqe(identifier_, result));
2745 1 : return HCCL_SUCCESS;
2746 : }
2747 :
2748 0 : HcclResult HcclCommunicator::GetOpInconsistentError(HcclResult& result)
2749 : {
2750 0 : CHK_RET(Heartbeat::GetInstance(deviceLogicId_).CheckOpInconsistentError(identifier_, result));
2751 0 : return HCCL_SUCCESS;
2752 : }
2753 :
2754 0 : HcclResult HcclCommunicator::SupportDeterministicOptim(bool& isDeterministicOptim)
2755 : {
2756 0 : CHK_SMART_PTR_NULL(implAlg_);
2757 0 : CHK_RET(implAlg_->SupportDeterministicOptim(isDeterministicOptim));
2758 0 : return HCCL_SUCCESS;
2759 : }
2760 :
2761 0 : HcclResult HcclCommunicator::GetHccsLinkNum(u32& numHccsLink)
2762 : {
2763 0 : auto iter = pairLinkInfo_.find(static_cast<u32>(LinkTypeInServer::HCCS_TYPE));
2764 0 : if (iter == pairLinkInfo_.end()) {
2765 0 : HCCL_ERROR("[HcclCommunicator][GetHccsLinkNum]HCCS_TYPE is not found");
2766 0 : return HCCL_E_PARA;
2767 : }
2768 0 : numHccsLink = iter->second.size();
2769 0 : return HCCL_SUCCESS;
2770 : }
2771 :
2772 4 : HcclResult HcclCommunicator::AllGather(
2773 : const std::string& tag, void* inputPtr, void* outputPtr, u64 inputCount, HcclDataType dataType, HcclRtStream stream,
2774 : [[maybe_unused]] HcomCollOpInfo* opInfo)
2775 : {
2776 4 : bool isCapture = StreamIsCapture(stream);
2777 :
2778 11 : bool aicpuUnfoldMode = false;
2779 11 : if (EnableAicpuUnfold(isCapture) && (userRankSize_ != 1)) {
2780 0 : aicpuUnfoldMode = true;
2781 : }
2782 :
2783 8 : if (!IsAtomicInit()) {
2784 0 : HCCL_ERROR(
2785 : "[HcclCommunicator][AllGather]errNo[0x%016llx] hccl init must be called before call this function",
2786 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
2787 0 : return HCCL_E_UNAVAIL;
2788 : }
2789 :
2790 11 : Stream streamObj(stream);
2791 10 : CHK_RET(callbackTask_->CallbackRegStream(stream));
2792 :
2793 9 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
2794 9 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
2795 7 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
2796 :
2797 5 : u32 perDataSize = SIZE_TABLE[dataType];
2798 5 : u64 totalSize = inputCount * perDataSize;
2799 :
2800 5 : OpParam opParam;
2801 5 : opParam.tag = tag;
2802 10 : opParam.inputPtr = inputPtr;
2803 10 : opParam.inputSize = totalSize;
2804 10 : opParam.outputPtr = outputPtr;
2805 10 : opParam.outputSize = totalSize * userRankSize_;
2806 10 : opParam.DataDes.count = inputCount;
2807 10 : opParam.DataDes.dataType = dataType;
2808 10 : opParam.reduceType = HcclReduceOp::HCCL_REDUCE_RESERVED;
2809 10 : opParam.stream = streamObj;
2810 9 : opParam.syncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
2811 9 : opParam.isCapture = isCapture;
2812 9 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
2813 9 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
2814 9 : opParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
2815 :
2816 9 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_ALLGATHER, opParam));
2817 :
2818 8 : return HCCL_SUCCESS;
2819 8 : }
2820 :
2821 1 : HcclResult HcclCommunicator::AllGatherV(
2822 : const std::string& tag, const void* sendBuf, u64 sendCount, const void* recvBuf, const void* recvCounts,
2823 : const void* rdispls, HcclDataType dataType, HcclRtStream stream)
2824 : {
2825 1 : bool aicpuUnfoldMode = false;
2826 :
2827 1 : if (GetAicpuUnfoldConfig() && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
2828 0 : aicpuUnfoldMode = true;
2829 : }
2830 :
2831 1 : if (!IsAtomicInit()) {
2832 0 : HCCL_ERROR(
2833 : "[HcclCommunicator][AllGatherV]errNo[0x%016llx] hccl init must be called before call this function",
2834 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
2835 0 : return HCCL_E_UNAVAIL;
2836 : }
2837 :
2838 1 : Stream streamObj(stream);
2839 1 : CHK_RET(callbackTask_->CallbackRegStream(stream));
2840 :
2841 1 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
2842 1 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
2843 1 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
2844 :
2845 1 : u32 perDataSize = SIZE_TABLE[dataType];
2846 1 : u64 totalSize = sendCount * perDataSize;
2847 :
2848 1 : u64 outputSize = 0;
2849 1 : const u64* counts = static_cast<const u64*>(recvCounts);
2850 3 : for (u32 i = 0; i < userRankSize_; i++) {
2851 2 : outputSize += counts[i] * perDataSize;
2852 : }
2853 :
2854 1 : bool isCapture = StreamIsCapture(stream);
2855 :
2856 1 : OpParam opParam;
2857 1 : opParam.tag = tag;
2858 1 : opParam.inputPtr = const_cast<void*>(sendBuf);
2859 1 : opParam.inputSize = totalSize;
2860 1 : opParam.outputPtr = const_cast<void*>(recvBuf);
2861 1 : opParam.outputSize = outputSize;
2862 1 : opParam.VDataDes.dataType = dataType;
2863 1 : opParam.VDataDes.counts = const_cast<void*>(recvCounts);
2864 1 : opParam.VDataDes.displs = const_cast<void*>(rdispls);
2865 1 : opParam.reduceType = HcclReduceOp::HCCL_REDUCE_RESERVED;
2866 1 : opParam.stream = streamObj;
2867 1 : opParam.syncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
2868 1 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
2869 1 : opParam.isCapture = isCapture;
2870 1 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
2871 1 : opParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
2872 :
2873 1 : if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
2874 0 : for (u32 i = 0; i < userRankSize_; i++) {
2875 0 : HCCL_CONFIG_DEBUG(
2876 : HCCL_ALG, "[HcclCommunicator][AllGatherV]userRank_[%u], rankIdx[%u], recvCounts[%llu], rdispls[%llu]",
2877 : userRank_, i, counts[i], static_cast<const u64*>(rdispls)[i]);
2878 : }
2879 : }
2880 :
2881 1 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_ALLGATHER_V, opParam));
2882 :
2883 1 : return HCCL_SUCCESS;
2884 1 : }
2885 :
2886 0 : HcclResult HcclCommunicator::AicpuUnfold(
2887 : const std::string& tag, void* inputPtr, void* outputPtr, u64 count, HcclDataType dataType, HcclReduceOp op,
2888 : HcclRtStream stream, HcclCMDType cmdType)
2889 : {
2890 0 : Stream streamObj(stream);
2891 0 : u32 perDataSize = SIZE_TABLE[dataType];
2892 0 : u64 totalSize = count * perDataSize;
2893 0 : bool isCapture = StreamIsCapture(stream);
2894 0 : OpParam opParam;
2895 0 : opParam.tag = tag;
2896 0 : opParam.inputPtr = inputPtr;
2897 0 : opParam.inputSize = totalSize;
2898 0 : opParam.outputPtr = outputPtr;
2899 0 : opParam.outputSize = totalSize;
2900 0 : opParam.DataDes.count = count;
2901 0 : opParam.DataDes.dataType = dataType;
2902 0 : opParam.reduceType = op;
2903 0 : opParam.stream = streamObj;
2904 0 : opParam.isCapture = isCapture;
2905 0 : opParam.syncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
2906 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
2907 0 : AlgType algType;
2908 0 : algType.algoLevel0 = AlgTypeLevel0::ALG_LEVEL0_NP_MESH;
2909 0 : algType.algoLevel1 = AlgTypeLevel1::ALG_LEVEL1_RING;
2910 :
2911 : // 构造空vector用于入参,无实际意义
2912 0 : const std::vector<Stream> slaveStreams;
2913 0 : CHK_RET(RegisterDfxInfo(opParam, algType, slaveStreams));
2914 0 : HcclResult ret = HCCL_SUCCESS;
2915 0 : if (!IsExistCommRes(identifier_)) {
2916 0 : HCCL_INFO(
2917 : "[AicpuUnfold] tag[%s] count[%llu] dataType[%s] op[%s].", identifier_.c_str(), count,
2918 : GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str());
2919 0 : uint64_t streamMode = 0;
2920 0 : CHK_RET(hrtStreamGetMode(stream, &streamMode));
2921 :
2922 : rtStream_t aicpuStream;
2923 0 : ret = Mc2AiCpuStreamAllocAndGet(streamMode, aicpuStream);
2924 0 : void* commContext = nullptr;
2925 0 : ret = CreateCommResource(identifier_, stream, true, &commContext);
2926 0 : if (ret != HCCL_SUCCESS) {
2927 0 : HCCL_ERROR(
2928 : "[hcclImpl][CreateComm]create aicpu unfold comminfo by tag[%s] failed. return[%d]", identifier_.c_str(),
2929 : ret);
2930 0 : return ret;
2931 : }
2932 : }
2933 :
2934 0 : std::string kernelName = "RunAicpuRpcSrvLaunch";
2935 0 : AicpuOpTiling opTilingInfo;
2936 0 : ret = AicpuKfcTilingDataLaunch(opParam, cmdType, commContext_, kernelName, opTilingInfo);
2937 0 : if (ret != HCCL_SUCCESS) {
2938 0 : HCCL_ERROR(
2939 : "[hcclImpl][TilingData]aicpu unfold tiling data launch failed. return[%d] inputPtr[%p]"
2940 : "outputPtr[%p] count[%llu] dataType[%s] op[%s]",
2941 : ret, inputPtr, outputPtr, count, GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str());
2942 0 : return ret;
2943 : }
2944 0 : CHK_RET(UnRegisterDfxInfo(opParam, slaveStreams));
2945 0 : return HCCL_SUCCESS;
2946 0 : }
2947 :
2948 4 : HcclResult HcclCommunicator::AllGatherOutPlace(
2949 : const std::string& tag, void* inputPtr, void* outputPtr, u64 inputCount, HcclDataType dataType, HcclRtStream stream)
2950 : {
2951 4 : CHK_RET(CheckSuspendingStatus());
2952 4 : if (!IsAtomicInit()) {
2953 0 : HCCL_ERROR(
2954 : "[HcclCommunicator][AllGatherOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
2955 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
2956 0 : return HCCL_E_UNAVAIL;
2957 : }
2958 :
2959 7 : bool isCapture = StreamIsCapture(stream);
2960 :
2961 8 : bool aicpuUnfoldMode = false;
2962 8 : if (EnableAicpuUnfold(isCapture) && (userRankSize_ != 1)) {
2963 3 : aicpuUnfoldMode = true;
2964 : }
2965 :
2966 7 : Stream streamObj(stream);
2967 8 : CHK_RET(callbackTask_->CallbackRegStream(stream));
2968 :
2969 6 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
2970 7 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
2971 4 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
2972 :
2973 2 : u32 perDataSize = SIZE_TABLE[dataType];
2974 2 : u64 totalSize = inputCount * perDataSize * userRankSize_;
2975 :
2976 2 : OpParam opParam;
2977 4 : opParam.tag = tag;
2978 7 : opParam.inputPtr = inputPtr;
2979 7 : opParam.inputSize = inputCount * perDataSize;
2980 7 : opParam.outputPtr = outputPtr;
2981 7 : opParam.outputSize = totalSize;
2982 7 : opParam.DataDes.count = inputCount;
2983 7 : opParam.DataDes.dataType = dataType;
2984 7 : opParam.reduceType = HcclReduceOp::HCCL_REDUCE_RESERVED;
2985 7 : opParam.stream = streamObj;
2986 8 : opParam.syncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
2987 8 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
2988 8 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
2989 8 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
2990 8 : opParam.isCapture = isCapture;
2991 8 : opParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
2992 :
2993 8 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_ALLGATHER, opParam));
2994 :
2995 0 : return HCCL_SUCCESS;
2996 8 : }
2997 :
2998 1 : HcclResult HcclCommunicator::AllGatherVOutPlace(
2999 : const std::string& tag, void* inputPtr, void* outputPtr, u64 inputCount, const void* outputCounts,
3000 : const void* outputDispls, HcclDataType dataType, HcclRtStream stream)
3001 : {
3002 1 : CHK_RET(CheckSuspendingStatus());
3003 1 : if (userRankSize_ == 1) {
3004 : // rankSize为1时,退化为AllGather
3005 0 : return AllGatherOutPlace(tag, inputPtr, outputPtr, inputCount, dataType, stream);
3006 : }
3007 :
3008 1 : if (!IsAtomicInit()) {
3009 0 : HCCL_ERROR(
3010 : "[HcclCommunicator][AllGatherVOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
3011 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3012 0 : return HCCL_E_UNAVAIL;
3013 : }
3014 :
3015 1 : bool aicpuUnfoldMode = false;
3016 1 : if (GetAicpuUnfoldConfig() && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
3017 0 : aicpuUnfoldMode = true;
3018 : }
3019 :
3020 1 : bool isCapture = StreamIsCapture(stream);
3021 :
3022 1 : Stream streamObj(stream);
3023 1 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3024 :
3025 1 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3026 1 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3027 1 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3028 :
3029 1 : u32 perDataSize = SIZE_TABLE[dataType];
3030 1 : u64 outputSize = 0;
3031 1 : const u64* counts = static_cast<const u64*>(outputCounts);
3032 3 : for (u32 i = 0; i < userRankSize_; i++) {
3033 2 : outputSize += counts[i] * perDataSize;
3034 : }
3035 :
3036 1 : OpParam opParam;
3037 1 : opParam.tag = tag;
3038 1 : opParam.inputPtr = inputPtr;
3039 1 : opParam.inputSize = inputCount * perDataSize;
3040 1 : opParam.outputPtr = outputPtr;
3041 1 : opParam.outputSize = outputSize;
3042 1 : opParam.VDataDes.counts = const_cast<void*>(outputCounts);
3043 1 : opParam.VDataDes.displs = const_cast<void*>(outputDispls);
3044 1 : opParam.VDataDes.dataType = dataType;
3045 1 : opParam.reduceType = HcclReduceOp::HCCL_REDUCE_RESERVED;
3046 1 : opParam.stream = streamObj;
3047 1 : opParam.syncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
3048 1 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
3049 1 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3050 1 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3051 1 : opParam.isCapture = isCapture;
3052 1 : opParam.rankSize = userRankSize_;
3053 1 : opParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
3054 :
3055 1 : if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
3056 0 : for (u32 i = 0; i < userRankSize_; i++) {
3057 0 : HCCL_CONFIG_DEBUG(
3058 : HCCL_ALG,
3059 : "[HcclCommunicator][AllGatherVOutPlace]userRank_[%u], rankIdx[%u],"
3060 : "outputCounts[%llu], outputDispls[%llu]",
3061 : userRank_, i, counts[i], static_cast<const u64*>(outputDispls)[i]);
3062 : }
3063 : }
3064 :
3065 1 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_ALLGATHER_V, opParam));
3066 :
3067 1 : return HCCL_SUCCESS;
3068 1 : }
3069 :
3070 79 : void HcclCommunicator::GetAndSetSyncMode(SyncMode& preSyncMode, SyncMode newSyncMode)
3071 : {
3072 79 : if (newSyncMode == SyncMode::UNLIMITED_TIMEWAITSYNCMODE) {
3073 0 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
3074 0 : HCCL_WARNING("310P don't support unlimited notify wait mode");
3075 : } else {
3076 0 : HcclGetNotifyWaitMode(dispatcher_, &preSyncMode);
3077 0 : HcclSetNotifyWaitMode(dispatcher_, newSyncMode);
3078 : }
3079 : }
3080 79 : }
3081 :
3082 68 : void HcclCommunicator::RestorePreSyncMode(SyncMode preSyncMode, SyncMode newSyncMode)
3083 : {
3084 68 : if (newSyncMode == SyncMode::UNLIMITED_TIMEWAITSYNCMODE && !Is310P3Common(isHaveCpuRank_, deviceType_)) {
3085 0 : HcclSetNotifyWaitMode(dispatcher_, preSyncMode);
3086 : }
3087 68 : }
3088 :
3089 43 : HcclResult HcclCommunicator::AllReduce(
3090 : const std::string& tag, void* inputPtr, void* outputPtr, u64 count, HcclDataType dataType, HcclReduceOp op,
3091 : HcclRtStream stream, SyncMode syncMode, [[maybe_unused]] const HcomCollOpInfo* opInfo)
3092 : {
3093 43 : CHK_RET(CheckSuspendingStatus());
3094 44 : bool aicpuUnfoldMode = false;
3095 56 : if (GetAicpuUnfoldConfig() == true && IsSupportSDMAReduce(inputPtr, outputPtr, dataType, op)
3096 56 : && deviceType_ == DevType::DEV_TYPE_910_93 && (userRankSize_ != 1)) {
3097 0 : aicpuUnfoldMode = true;
3098 : }
3099 :
3100 44 : if (!IsAtomicInit()) {
3101 0 : HCCL_ERROR(
3102 : "[HcclCommunicator][AllReduce]errNo[0x%016llx] hccl init must be called before call this function",
3103 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3104 0 : return HCCL_E_UNAVAIL;
3105 : }
3106 :
3107 : // 设置notify wait模式
3108 60 : SyncMode preSyncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
3109 60 : GetAndSetSyncMode(preSyncMode, syncMode);
3110 :
3111 60 : Stream streamObj(stream);
3112 57 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3113 :
3114 55 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3115 53 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3116 49 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3117 :
3118 46 : u32 perDataSize = SIZE_TABLE[dataType];
3119 46 : u64 totalSize = count * perDataSize;
3120 :
3121 46 : OpParam opParam;
3122 54 : opParam.tag = tag;
3123 60 : opParam.inputPtr = inputPtr;
3124 60 : opParam.inputSize = totalSize;
3125 60 : opParam.outputPtr = outputPtr;
3126 60 : opParam.outputSize = totalSize;
3127 60 : opParam.DataDes.count = count;
3128 60 : opParam.DataDes.dataType = dataType;
3129 60 : opParam.reduceType = op;
3130 60 : opParam.stream = streamObj;
3131 56 : opParam.syncMode = syncMode;
3132 56 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3133 56 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3134 56 : opParam.opType = HcclCMDType::HCCL_CMD_ALLREDUCE;
3135 : // 用于inplace支持重执行场景的图模式归一至单算子模式
3136 56 : retryOrigWorkflowMode_ = GetWorkflowMode();
3137 : bool isHcclOpInplace
3138 56 : = IsHcclOpInplace(HcclCMDType::HCCL_CMD_ALLREDUCE, opParam, userRank_, userRankSize_, isInplaceStatus_);
3139 58 : if (aicpuUnfoldMode && retryEnable_ && isHcclOpInplace) {
3140 0 : HCCL_DEBUG(
3141 : "The retry with inplace case is expected to be supported, "
3142 : "aicpuUnfoldMode[%d], retryEnable_[%d], isHcclOpInplace[%d], "
3143 : "therefore HcclWorkflowMode is converted from [%d] to HCCL_WORKFLOW_MODE_OP_BASE",
3144 : aicpuUnfoldMode, retryEnable_, isHcclOpInplace, static_cast<u8>(retryOrigWorkflowMode_));
3145 0 : CHK_RET(SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE));
3146 : }
3147 :
3148 58 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_ALLREDUCE, opParam));
3149 :
3150 50 : RestorePreSyncMode(preSyncMode, syncMode);
3151 50 : CHK_RET(SetWorkflowMode(retryOrigWorkflowMode_));
3152 50 : return HCCL_SUCCESS;
3153 55 : }
3154 :
3155 0 : HcclResult HcclCommunicator::AllReduceAicpuUnfold(
3156 : const std::string& tag, void* inputPtr, void* outputPtr, u64 count, HcclDataType dataType, HcclReduceOp op,
3157 : HcclRtStream stream)
3158 : {
3159 0 : Stream streamObj(stream);
3160 0 : u32 perDataSize = SIZE_TABLE[dataType];
3161 0 : u64 totalSize = count * perDataSize;
3162 0 : OpParam opParam;
3163 0 : opParam.tag = tag;
3164 0 : opParam.inputPtr = inputPtr;
3165 0 : opParam.inputSize = totalSize;
3166 0 : opParam.outputPtr = outputPtr;
3167 0 : opParam.outputSize = totalSize;
3168 0 : opParam.DataDes.count = count;
3169 0 : opParam.DataDes.dataType = dataType;
3170 0 : opParam.reduceType = op;
3171 0 : opParam.stream = streamObj;
3172 0 : opParam.isCapture = StreamIsCapture(stream);
3173 0 : opParam.syncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
3174 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3175 0 : AlgType algType;
3176 0 : algType.algoLevel0 = AlgTypeLevel0::ALG_LEVEL0_NP_SINGLE_RING;
3177 0 : algType.algoLevel1 = AlgTypeLevel1::ALG_LEVEL1_RING;
3178 0 : auto slaveStreams = opParam.isCapture ? std::vector<Stream>{opStream_} : std::vector<Stream>{};
3179 0 : CaptureSlaveStreams(streamObj.ptr(), slaveStreams);
3180 0 : CHK_RET(RegisterDfxInfo(opParam, algType, slaveStreams));
3181 : HcclResult ret;
3182 0 : if (!IsExistCommRes(tag)) {
3183 0 : uint64_t streamMode = 0;
3184 0 : CHK_RET(hrtStreamGetMode(stream, &streamMode));
3185 : rtStream_t aicpuStream;
3186 0 : ret = Mc2AiCpuStreamAllocAndGet(streamMode, aicpuStream);
3187 0 : void* commContext = nullptr;
3188 0 : ret = CreateCommResource(tag, aicpuStream, true, &commContext);
3189 0 : if (ret != HCCL_SUCCESS) {
3190 0 : HCCL_ERROR(
3191 : "[hcclImpl][CreateComm]create aicpu unfold comminfo by tag[%s] failed. return[%d]", tag.c_str(), ret);
3192 0 : return ret;
3193 : }
3194 : }
3195 0 : AicpuOpTiling opTilingInfo;
3196 0 : std::string kernelName = "RunAicpuRpcSrvLaunch";
3197 0 : ret = AicpuKfcTilingDataLaunch(opParam, HcclCMDType::HCCL_CMD_ALLREDUCE, commContext_, kernelName, opTilingInfo);
3198 0 : if (ret != HCCL_SUCCESS) {
3199 0 : HCCL_ERROR(
3200 : "[hcclImpl][TilingData]aicpu unfold tiling data launch failed. return[%d] inputPtr[%p]"
3201 : "outputPtr[%p] count[%llu] dataType[%s] op[%s]",
3202 : ret, inputPtr, outputPtr, count, GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str());
3203 0 : return ret;
3204 : }
3205 0 : CHK_RET(UnRegisterDfxInfo(opParam, slaveStreams));
3206 0 : return HCCL_SUCCESS;
3207 0 : }
3208 :
3209 14 : HcclResult HcclCommunicator::AllReduceOutPlace(
3210 : const std::string& tag, void* inputPtr, void* outputPtr, u64 count, HcclDataType dataType, HcclReduceOp op,
3211 : HcclRtStream stream, SyncMode syncMode)
3212 : {
3213 14 : CHK_RET(CheckSuspendingStatus());
3214 14 : const u32 RANK_SIZE_TWO = 2;
3215 14 : bool aicpuUnfoldMode = false;
3216 14 : if (GetAicpuUnfoldConfig() == true && IsSupportSDMAReduce(inputPtr, outputPtr, dataType, op)) {
3217 0 : if (userRankSize_ >= RANK_SIZE_TWO && Is310P3Common(isHaveCpuRank_, deviceType_)) {
3218 0 : HcclResult ret = AllReduceAicpuUnfold(tag, inputPtr, outputPtr, count, dataType, op, stream);
3219 0 : CHK_PRT_RET(
3220 : (ret != HCCL_SUCCESS),
3221 : HCCL_ERROR(
3222 : "[HcclCommunicator][AllReduce]errNo[0x%016llx] tag[%s], AllReduce aicpu unfold failed",
3223 : HCCL_ERROR_CODE(ret), tag.c_str()),
3224 : ret);
3225 :
3226 0 : return HCCL_SUCCESS;
3227 : }
3228 0 : if ((deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
3229 0 : aicpuUnfoldMode = true;
3230 : }
3231 : }
3232 :
3233 14 : bool isCapture = StreamIsCapture(stream);
3234 :
3235 19 : if (!IsAtomicInit()) {
3236 0 : HCCL_ERROR(
3237 : "[HcclCommunicator][AllReduceOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
3238 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3239 0 : return HCCL_E_UNAVAIL;
3240 : }
3241 :
3242 : // 设置notify wait模式
3243 21 : SyncMode preSyncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
3244 21 : GetAndSetSyncMode(preSyncMode, syncMode);
3245 :
3246 21 : Stream streamObj(stream);
3247 21 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3248 :
3249 21 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3250 21 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3251 18 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3252 :
3253 12 : u32 perDataSize = SIZE_TABLE[dataType];
3254 12 : u64 totalSize = count * perDataSize;
3255 :
3256 12 : OpParam opParam;
3257 14 : opParam.tag = tag;
3258 20 : opParam.inputPtr = inputPtr;
3259 20 : opParam.inputSize = totalSize;
3260 20 : opParam.outputPtr = outputPtr;
3261 20 : opParam.outputSize = totalSize;
3262 20 : opParam.DataDes.count = count;
3263 20 : opParam.DataDes.dataType = dataType;
3264 20 : opParam.reduceType = op;
3265 20 : opParam.stream = streamObj;
3266 16 : opParam.syncMode = syncMode;
3267 16 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3268 16 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3269 16 : opParam.isCapture = isCapture;
3270 16 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
3271 15 : opParam.opType = HcclCMDType::HCCL_CMD_ALLREDUCE;
3272 :
3273 15 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_ALLREDUCE, opParam));
3274 :
3275 18 : RestorePreSyncMode(preSyncMode, syncMode);
3276 18 : return HCCL_SUCCESS;
3277 19 : }
3278 :
3279 4 : HcclResult HcclCommunicator::AlltoAllV(
3280 : const void* sendBuf, const void* sendCounts, const void* sdispls, HcclDataType sendType, const void* recvBuf,
3281 : const void* recvCounts, const void* rdispls, HcclDataType recvType, rtStream_t stream, const std::string& tag)
3282 : {
3283 4 : CHK_RET(CheckSuspendingStatus());
3284 :
3285 4 : if (!IsAtomicInit()) {
3286 0 : HCCL_ERROR(
3287 : "[HcclCommunicator][AlltoAllV]errNo[0x%016llx] hccl init must be called before call this function",
3288 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3289 0 : return HCCL_E_UNAVAIL;
3290 : }
3291 :
3292 4 : if (IsNeedNicInit()) {
3293 4 : HCCL_INFO("InitNic.");
3294 4 : CHK_RET(InitNic());
3295 : }
3296 :
3297 4 : bool isCapture = StreamIsCapture(stream);
3298 :
3299 4 : Stream streamObj(stream);
3300 4 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3301 :
3302 4 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3303 4 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3304 4 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3305 :
3306 4 : OpParam opParam;
3307 4 : opParam.tag = tag;
3308 4 : opParam.inputPtr = const_cast<void*>(sendBuf);
3309 4 : opParam.outputPtr = const_cast<void*>(recvBuf);
3310 4 : opParam.All2AllDataDes.sendType = sendType;
3311 4 : opParam.All2AllDataDes.recvType = recvType;
3312 4 : opParam.All2AllDataDes.sendCounts = const_cast<void*>(sendCounts);
3313 4 : opParam.All2AllDataDes.recvCounts = const_cast<void*>(recvCounts);
3314 4 : opParam.All2AllDataDes.sdispls = const_cast<void*>(sdispls);
3315 4 : opParam.All2AllDataDes.rdispls = const_cast<void*>(rdispls);
3316 4 : opParam.stream = streamObj;
3317 4 : opParam.opType = HcclCMDType::HCCL_CMD_ALLTOALLV;
3318 4 : opParam.aicpuUnfoldMode = EnableAicpuUnfold(isCapture);
3319 4 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3320 4 : opParam.isCapture = isCapture;
3321 :
3322 4 : if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
3323 0 : for (u32 i = 0; i < userRankSize_; i++) {
3324 0 : HCCL_CONFIG_INFO(
3325 : HCCL_ALG,
3326 : "[HcclCommunicator][AlltoAllV] rank[%u], sendCounts[%llu], sendDispls[%llu] "
3327 : "recvCounts[%llu], recvDispls[%llu]",
3328 : userRank_, *(static_cast<const u64*>(opParam.All2AllDataDes.sendCounts) + i),
3329 : *(static_cast<const u64*>(opParam.All2AllDataDes.sdispls) + i),
3330 : *(static_cast<const u64*>(opParam.All2AllDataDes.recvCounts) + i),
3331 : *(static_cast<const u64*>(opParam.All2AllDataDes.rdispls) + i));
3332 : }
3333 : }
3334 :
3335 4 : CHK_RET(ExecOpAlltoAll(HcclCMDType::HCCL_CMD_ALLTOALLV, opParam));
3336 4 : return HCCL_SUCCESS;
3337 4 : }
3338 :
3339 0 : HcclResult HcclCommunicator::AlltoAllVOutPlace(
3340 : const void* sendBuf, const void* sendCounts, const void* sdispls, HcclDataType sendType, const void* recvBuf,
3341 : const void* recvCounts, const void* rdispls, HcclDataType recvType, rtStream_t stream, const std::string& tag)
3342 : {
3343 0 : CHK_RET(CheckSuspendingStatus());
3344 0 : CHK_PRT_RET(
3345 : Is310P3Common(isHaveCpuRank_, deviceType_),
3346 : HCCL_RUN_INFO("[AlltoAllVOutPlace]This method cannot be invoked in the current scenario."), HCCL_SUCCESS);
3347 0 : if (!IsAtomicInit()) {
3348 0 : HCCL_ERROR(
3349 : "[HcclCommunicator][AlltoAllVOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
3350 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3351 0 : return HCCL_E_UNAVAIL;
3352 : }
3353 :
3354 0 : if (IsNeedNicInit()) {
3355 0 : HCCL_INFO("InitNic.");
3356 0 : CHK_RET(InitNic());
3357 : }
3358 :
3359 0 : bool isCapture = StreamIsCapture(stream);
3360 :
3361 0 : Stream streamObj(stream);
3362 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3363 :
3364 0 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3365 0 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3366 0 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3367 :
3368 0 : OpParam opParam;
3369 0 : opParam.tag = tag;
3370 0 : opParam.inputPtr = const_cast<void*>(sendBuf);
3371 0 : opParam.outputPtr = const_cast<void*>(recvBuf);
3372 0 : opParam.All2AllDataDes.sendType = sendType;
3373 0 : opParam.All2AllDataDes.recvType = recvType;
3374 0 : opParam.All2AllDataDes.sendCounts = const_cast<void*>(sendCounts);
3375 0 : opParam.All2AllDataDes.recvCounts = const_cast<void*>(recvCounts);
3376 0 : opParam.All2AllDataDes.sdispls = const_cast<void*>(sdispls);
3377 0 : opParam.All2AllDataDes.rdispls = const_cast<void*>(rdispls);
3378 0 : opParam.stream = streamObj;
3379 0 : opParam.opType = HcclCMDType::HCCL_CMD_ALLTOALLV;
3380 0 : opParam.aicpuUnfoldMode = EnableAicpuUnfold(isCapture);
3381 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3382 0 : opParam.isCapture = isCapture;
3383 :
3384 0 : if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
3385 0 : for (u32 i = 0; i < userRankSize_; i++) {
3386 0 : HCCL_CONFIG_INFO(
3387 : HCCL_ALG,
3388 : "[HcclCommunicator][AlltoAllVOutPlace] rank[%u], sendCounts[%llu],"
3389 : "sendDispls[%llu], recvCounts[%llu], recvDispls[%llu]",
3390 : userRank_, *(static_cast<const u64*>(opParam.All2AllDataDes.sendCounts) + i),
3391 : *(static_cast<const u64*>(opParam.All2AllDataDes.sdispls) + i),
3392 : *(static_cast<const u64*>(opParam.All2AllDataDes.recvCounts) + i),
3393 : *(static_cast<const u64*>(opParam.All2AllDataDes.rdispls) + i));
3394 : }
3395 : }
3396 :
3397 0 : CHK_RET(ExecOpAlltoAll(HcclCMDType::HCCL_CMD_ALLTOALLV, opParam));
3398 0 : return HCCL_SUCCESS;
3399 0 : }
3400 :
3401 1 : HcclResult HcclCommunicator::AlltoAllVC(
3402 : const void* sendBuf, const void* sendCountMatrix, HcclDataType sendType, const void* recvBuf, HcclDataType recvType,
3403 : rtStream_t stream, const std::string& tag)
3404 : {
3405 1 : CHK_RET(CheckSuspendingStatus());
3406 1 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
3407 0 : HCCL_ERROR(
3408 : "[%s][%s]AlltoAllVC is not supported", LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_NOT_SUPPORTED.c_str());
3409 0 : return HCCL_E_NOT_SUPPORT;
3410 : }
3411 1 : if (!IsAtomicInit()) {
3412 0 : HCCL_ERROR(
3413 : "[HcclCommunicator][AlltoAllVC]errNo[0x%016llx] hccl init must be called before call this function",
3414 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3415 0 : return HCCL_E_UNAVAIL;
3416 : }
3417 :
3418 1 : if (IsNeedNicInit()) {
3419 1 : HCCL_INFO("InitNic.");
3420 1 : CHK_RET(InitNic());
3421 : }
3422 :
3423 1 : bool isCapture = StreamIsCapture(stream);
3424 :
3425 1 : Stream streamObj(stream);
3426 1 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3427 :
3428 1 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3429 1 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3430 1 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3431 :
3432 1 : OpParam opParam;
3433 1 : opParam.tag = tag;
3434 1 : opParam.inputPtr = const_cast<void*>(sendBuf);
3435 1 : opParam.outputPtr = const_cast<void*>(recvBuf);
3436 1 : opParam.All2AllDataDes.sendType = sendType;
3437 1 : opParam.All2AllDataDes.recvType = recvType;
3438 1 : opParam.All2AllDataDes.sendCountMatrix = const_cast<void*>(sendCountMatrix);
3439 1 : opParam.stream = streamObj;
3440 1 : opParam.opType = HcclCMDType::HCCL_CMD_ALLTOALLVC;
3441 1 : opParam.aicpuUnfoldMode = EnableAicpuUnfold(isCapture);
3442 1 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3443 1 : opParam.isCapture = isCapture;
3444 :
3445 1 : if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
3446 0 : for (u32 i = 0; i < userRankSize_; i++) {
3447 0 : for (u32 j = 0; j < userRankSize_; j++) {
3448 0 : HCCL_CONFIG_DEBUG(
3449 : HCCL_ALG,
3450 : "[HcclCommunicator][AlltoAllVC] usrRank[%u] rank[%u] to remoteRank[%u], "
3451 : "sendCounts[%llu]",
3452 : userRank_, i, j,
3453 : *(static_cast<const u64*>(opParam.All2AllDataDes.sendCountMatrix) + i * userRankSize_ + j));
3454 : }
3455 : }
3456 : }
3457 :
3458 1 : CHK_RET(ExecOpAlltoAll(HcclCMDType::HCCL_CMD_ALLTOALLVC, opParam));
3459 1 : return HCCL_SUCCESS;
3460 1 : }
3461 :
3462 0 : HcclResult HcclCommunicator::AlltoAllVCOutPlace(
3463 : const void* sendBuf, const void* sendCountMatrix, HcclDataType sendType, const void* recvBuf, HcclDataType recvType,
3464 : rtStream_t stream, const std::string& tag)
3465 : {
3466 0 : CHK_RET(CheckSuspendingStatus());
3467 0 : CHK_PRT_RET(
3468 : Is310P3Common(isHaveCpuRank_, deviceType_),
3469 : HCCL_RUN_INFO("[AlltoAllVCOutPlace]This method cannot be invoked in the current scenario."), HCCL_SUCCESS);
3470 :
3471 0 : if (!IsAtomicInit()) {
3472 0 : HCCL_ERROR(
3473 : "[HcclCommunicator][AlltoAllVCOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
3474 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3475 0 : return HCCL_E_UNAVAIL;
3476 : }
3477 :
3478 0 : if (IsNeedNicInit()) {
3479 0 : HCCL_INFO("InitNic");
3480 0 : CHK_RET(InitNic());
3481 : }
3482 :
3483 0 : bool isCapture = StreamIsCapture(stream);
3484 :
3485 0 : Stream streamObj(stream);
3486 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3487 :
3488 0 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3489 0 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3490 0 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3491 :
3492 0 : OpParam opParam;
3493 0 : opParam.tag = tag;
3494 0 : opParam.inputPtr = const_cast<void*>(sendBuf);
3495 0 : opParam.outputPtr = const_cast<void*>(recvBuf);
3496 0 : opParam.All2AllDataDes.sendType = sendType;
3497 0 : opParam.All2AllDataDes.recvType = recvType;
3498 0 : opParam.All2AllDataDes.sendCountMatrix = const_cast<void*>(sendCountMatrix);
3499 0 : opParam.stream = streamObj;
3500 0 : opParam.opType = HcclCMDType::HCCL_CMD_ALLTOALLVC;
3501 0 : opParam.aicpuUnfoldMode = EnableAicpuUnfold(isCapture);
3502 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3503 0 : opParam.isCapture = isCapture;
3504 :
3505 0 : if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
3506 0 : for (u32 i = 0; i < userRankSize_; i++) {
3507 0 : for (u32 j = 0; j < userRankSize_; j++) {
3508 0 : HCCL_CONFIG_DEBUG(
3509 : HCCL_ALG,
3510 : "[HcclCommunicator][AlltoAllVCOutPlace] usrRank[%u] rank[%u]"
3511 : "to remoteRank[%u], sendCounts[%llu]",
3512 : userRank_, i, j,
3513 : *(static_cast<const u64*>(opParam.All2AllDataDes.sendCountMatrix) + i * userRankSize_ + j));
3514 : }
3515 : }
3516 : }
3517 :
3518 0 : CHK_RET(ExecOpAlltoAll(HcclCMDType::HCCL_CMD_ALLTOALLVC, opParam));
3519 0 : return HCCL_SUCCESS;
3520 0 : }
3521 :
3522 0 : HcclResult HcclCommunicator::AlltoAll(
3523 : const void* sendBuf, u64 sendCount, HcclDataType sendType, const void* recvBuf, u64 recvCount,
3524 : HcclDataType recvType, rtStream_t stream, const std::string& tag)
3525 : {
3526 0 : CHK_RET(CheckSuspendingStatus());
3527 0 : if (!IsAtomicInit()) {
3528 0 : HCCL_ERROR(
3529 : "[HcclCommunicator][AlltoAll]errNo[0x%016llx] hccl init must be called before call this function",
3530 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3531 0 : return HCCL_E_UNAVAIL;
3532 : }
3533 :
3534 0 : if (IsNeedNicInit()) {
3535 0 : HCCL_INFO("InitNic.");
3536 0 : CHK_RET(InitNic());
3537 : }
3538 :
3539 0 : bool isCapture = StreamIsCapture(stream);
3540 :
3541 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3542 :
3543 : // 生成sendCountMatrix矩阵,alltoall的底层实现走alltoallvc
3544 0 : std::vector<u64> sendCountMatrix(userRankSize_ * userRankSize_, sendCount);
3545 :
3546 0 : OpParam opParam;
3547 0 : opParam.tag = tag;
3548 0 : opParam.inputPtr = const_cast<void*>(sendBuf);
3549 0 : opParam.outputPtr = const_cast<void*>(recvBuf);
3550 0 : opParam.All2AllDataDes.sendType = sendType;
3551 0 : opParam.All2AllDataDes.recvType = recvType;
3552 0 : opParam.All2AllDataDes.sendCount = sendCount;
3553 0 : opParam.All2AllDataDes.recvCount = recvCount;
3554 0 : opParam.All2AllDataDes.sendCountMatrix = static_cast<void*>(sendCountMatrix.data());
3555 0 : opParam.stream = Stream(stream);
3556 0 : opParam.opType = HcclCMDType::HCCL_CMD_ALLTOALL;
3557 0 : opParam.aicpuUnfoldMode = false;
3558 0 : opParam.aicpuCacheEnable = 0;
3559 0 : opParam.isCapture = isCapture;
3560 0 : opParam.inputSize = sendCount * SIZE_TABLE[sendType] * userRankSize_;
3561 0 : opParam.outputSize = recvCount * SIZE_TABLE[recvType] * userRankSize_;
3562 0 : if (EnableAicpuUnfold(isCapture)) {
3563 0 : opParam.aicpuUnfoldMode = true;
3564 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3565 : }
3566 :
3567 0 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3568 0 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3569 0 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3570 0 : CHK_RET(ExecOpAlltoAll(HcclCMDType::HCCL_CMD_ALLTOALL, opParam));
3571 0 : return HCCL_SUCCESS;
3572 0 : }
3573 :
3574 2 : HcclResult HcclCommunicator::Broadcast(
3575 : const std::string& tag, void* ptr, u64 count, HcclDataType dataType, u32 root, HcclRtStream stream)
3576 : {
3577 2 : CHK_RET(CheckSuspendingStatus());
3578 2 : bool aicpuUnfoldMode = false;
3579 2 : if (GetAicpuUnfoldConfig() == true && deviceType_ == DevType::DEV_TYPE_910_93 && (userRankSize_ != 1)) {
3580 0 : aicpuUnfoldMode = true;
3581 : }
3582 :
3583 2 : if (!IsAtomicInit()) {
3584 0 : HCCL_ERROR(
3585 : "[HcclCommunicator][Broadcast]errNo[0x%016llx] hccl init must be called before call this function",
3586 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3587 0 : return HCCL_E_UNAVAIL;
3588 : }
3589 :
3590 2 : Stream streamObj(stream);
3591 2 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3592 :
3593 2 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3594 2 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3595 2 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3596 2 : u32 perDataSize = SIZE_TABLE[dataType];
3597 2 : u64 totalSize = count * perDataSize;
3598 :
3599 2 : OpParam opParam;
3600 2 : opParam.tag = tag;
3601 2 : opParam.inputPtr = ptr;
3602 2 : opParam.outputPtr = ptr;
3603 2 : opParam.inputSize = totalSize;
3604 2 : opParam.outputSize = totalSize;
3605 2 : opParam.DataDes.count = count;
3606 2 : opParam.DataDes.dataType = dataType;
3607 2 : opParam.root = root;
3608 2 : opParam.stream = streamObj;
3609 2 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3610 2 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3611 2 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
3612 2 : opParam.opType = HcclCMDType::HCCL_CMD_BROADCAST;
3613 :
3614 2 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_BROADCAST, opParam));
3615 :
3616 2 : return HCCL_SUCCESS;
3617 2 : }
3618 :
3619 0 : HcclResult HcclCommunicator::BroadcastOutPlace(
3620 : const std::string& tag, void* ptr, u64 count, HcclDataType dataType, u32 root, HcclRtStream stream)
3621 : {
3622 0 : CHK_RET(CheckSuspendingStatus());
3623 0 : bool aicpuUnfoldMode = false;
3624 0 : if (GetAicpuUnfoldConfig() == true && deviceType_ == DevType::DEV_TYPE_910_93 && (userRankSize_ != 1)) {
3625 0 : aicpuUnfoldMode = true;
3626 : }
3627 :
3628 0 : CHK_PRT_RET(
3629 : Is310P3Common(isHaveCpuRank_, deviceType_),
3630 : HCCL_RUN_INFO("[BroadcastOutPlace]This method cannot be invoked in the current scenario."), HCCL_SUCCESS);
3631 :
3632 0 : if (!IsAtomicInit()) {
3633 0 : HCCL_ERROR(
3634 : "[HcclCommunicator][BroadcastOutPlace]errNo[0x%016llx] hccl init must be called before"
3635 : " call this function",
3636 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3637 0 : return HCCL_E_UNAVAIL;
3638 : }
3639 :
3640 0 : bool isCapture = StreamIsCapture(stream);
3641 :
3642 0 : Stream streamObj(stream);
3643 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3644 :
3645 0 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3646 0 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3647 0 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3648 :
3649 0 : u32 perDataSize = SIZE_TABLE[dataType];
3650 0 : u64 totalSize = count * perDataSize;
3651 :
3652 0 : OpParam opParam;
3653 0 : opParam.tag = tag;
3654 0 : opParam.inputPtr = ptr;
3655 0 : opParam.outputPtr = ptr;
3656 0 : opParam.inputSize = totalSize;
3657 0 : opParam.outputSize = totalSize;
3658 0 : opParam.DataDes.count = count;
3659 0 : opParam.DataDes.dataType = dataType;
3660 0 : opParam.root = root;
3661 0 : opParam.stream = streamObj;
3662 0 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3663 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3664 0 : opParam.isCapture = isCapture;
3665 0 : opParam.opType = HcclCMDType::HCCL_CMD_BROADCAST;
3666 :
3667 0 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_BROADCAST, opParam));
3668 :
3669 0 : return HCCL_SUCCESS;
3670 0 : }
3671 :
3672 0 : HcclResult HcclCommunicator::Scatter(
3673 : const std::string& tag, void* inputPtr, void* outputPtr, u64 recvCount, HcclDataType dataType, u32 root,
3674 : HcclRtStream stream)
3675 : {
3676 0 : CHK_RET(CheckSuspendingStatus());
3677 0 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
3678 0 : HCCL_ERROR(
3679 : "[%s][%s]Scatter Not Supported Yet", LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_NOT_SUPPORTED.c_str());
3680 0 : return HCCL_E_NOT_SUPPORT;
3681 : }
3682 0 : bool aicpuUnfoldMode = false;
3683 0 : if (GetAicpuUnfoldConfig() == true && deviceType_ == DevType::DEV_TYPE_910_93 && (userRankSize_ != 1)) {
3684 0 : aicpuUnfoldMode = true;
3685 : }
3686 :
3687 0 : if (!IsAtomicInit()) {
3688 0 : HCCL_ERROR(
3689 : "[HcclCommunicator][Scatter]errNo[0x%016llx] hccl init must be called before call this function",
3690 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3691 0 : return HCCL_E_UNAVAIL;
3692 : }
3693 :
3694 0 : Stream streamObj(stream);
3695 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3696 :
3697 0 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3698 0 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3699 0 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3700 :
3701 0 : u32 perDataSize = SIZE_TABLE[dataType];
3702 0 : u64 outputSize = recvCount * perDataSize;
3703 0 : u64 totalSize = outputSize * userRankSize_;
3704 :
3705 0 : OpParam opParam;
3706 0 : opParam.tag = tag;
3707 0 : opParam.inputPtr = inputPtr;
3708 0 : opParam.inputSize = totalSize;
3709 0 : opParam.outputPtr = outputPtr;
3710 0 : opParam.outputSize = totalSize;
3711 0 : opParam.DataDes.count = recvCount;
3712 0 : opParam.DataDes.dataType = dataType;
3713 0 : opParam.stream = streamObj;
3714 0 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3715 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3716 0 : opParam.root = root;
3717 0 : opParam.opType = HcclCMDType::HCCL_CMD_SCATTER;
3718 0 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_SCATTER, opParam));
3719 0 : return HCCL_SUCCESS;
3720 0 : }
3721 :
3722 9 : HcclResult HcclCommunicator::ScatterOutPlace(
3723 : const std::string& tag, void* inputPtr, void* outputPtr, u64 recvCount, HcclDataType dataType, u32 root,
3724 : HcclRtStream stream)
3725 : {
3726 9 : CHK_RET(CheckSuspendingStatus());
3727 12 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
3728 0 : HCCL_ERROR(
3729 : "[%s][%s]ScatterOutPlace Not Supported Yet", LOG_KEYWORDS_TASK_EXEC.c_str(),
3730 : LOG_KEYWORDS_NOT_SUPPORTED.c_str());
3731 0 : return HCCL_E_NOT_SUPPORT;
3732 : }
3733 :
3734 12 : bool aicpuUnfoldMode = false;
3735 12 : if (GetAicpuUnfoldConfig() == true && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
3736 0 : aicpuUnfoldMode = true;
3737 : }
3738 :
3739 12 : bool isCapture = StreamIsCapture(stream);
3740 :
3741 9 : if (!IsAtomicInit()) {
3742 0 : HCCL_ERROR(
3743 : "[HcclCommunicator][ScatterOutPlace]errNo[0x%016llx] hccl init must be called before"
3744 : " call this function",
3745 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3746 0 : return HCCL_E_UNAVAIL;
3747 : }
3748 :
3749 12 : Stream streamObj(stream);
3750 12 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3751 :
3752 12 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3753 12 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3754 10 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3755 :
3756 5 : u32 perDataSize = SIZE_TABLE[dataType];
3757 5 : u64 outputSize = recvCount * perDataSize;
3758 5 : u64 totalSize = outputSize * userRankSize_;
3759 :
3760 5 : OpParam opParam;
3761 7 : opParam.tag = tag;
3762 12 : opParam.inputPtr = inputPtr;
3763 12 : opParam.inputSize = totalSize;
3764 12 : opParam.outputPtr = outputPtr;
3765 12 : opParam.outputSize = totalSize;
3766 12 : opParam.DataDes.count = recvCount;
3767 12 : opParam.DataDes.dataType = dataType;
3768 12 : opParam.stream = streamObj;
3769 9 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3770 9 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3771 9 : opParam.isCapture = isCapture;
3772 9 : opParam.root = root;
3773 9 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
3774 9 : opParam.opType = HcclCMDType::HCCL_CMD_SCATTER;
3775 :
3776 9 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_SCATTER, opParam));
3777 :
3778 7 : return HCCL_SUCCESS;
3779 11 : }
3780 :
3781 5 : HcclResult HcclCommunicator::Reduce(
3782 : const std::string& tag, void* inputPtr, void* outputPtr, u64 count, HcclDataType dataType, HcclReduceOp op,
3783 : u32 root, HcclRtStream stream)
3784 : {
3785 5 : CHK_RET(CheckSuspendingStatus());
3786 5 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
3787 0 : HCCL_ERROR(
3788 : "[%s][%s]Reduce Not Supported Yet", LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_NOT_SUPPORTED.c_str());
3789 0 : return HCCL_E_NOT_SUPPORT;
3790 : }
3791 5 : bool aicpuUnfoldMode = false;
3792 10 : if (GetAicpuUnfoldConfig() == true && IsSupportSDMAReduce(inputPtr, outputPtr, dataType, op)
3793 10 : && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
3794 0 : aicpuUnfoldMode = true;
3795 : }
3796 :
3797 5 : if (!IsAtomicInit()) {
3798 0 : HCCL_ERROR(
3799 : "[HcclCommunicator][Reduce]errNo[0x%016llx] hccl init must be called before call this function",
3800 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3801 0 : return HCCL_E_UNAVAIL;
3802 : }
3803 :
3804 5 : Stream streamObj(stream);
3805 5 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3806 :
3807 5 : u32 perDataSize = SIZE_TABLE[dataType];
3808 5 : u64 totalSize = count * perDataSize;
3809 5 : OpParam opParam;
3810 5 : opParam.tag = tag;
3811 5 : opParam.inputPtr = inputPtr;
3812 5 : opParam.inputSize = totalSize;
3813 5 : opParam.outputPtr = outputPtr;
3814 5 : opParam.outputSize = totalSize;
3815 5 : opParam.DataDes.count = count;
3816 5 : opParam.DataDes.dataType = dataType;
3817 5 : opParam.reduceType = op;
3818 5 : opParam.root = root;
3819 5 : opParam.stream = streamObj;
3820 5 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3821 5 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3822 5 : opParam.opType = HcclCMDType::HCCL_CMD_REDUCE;
3823 :
3824 5 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_REDUCE, opParam));
3825 :
3826 5 : return HCCL_SUCCESS;
3827 5 : }
3828 :
3829 1 : HcclResult HcclCommunicator::ReduceOutPlace(
3830 : const std::string& tag, void* inputPtr, void* outputPtr, u64 count, HcclDataType dataType, HcclReduceOp op,
3831 : u32 root, HcclRtStream stream)
3832 : {
3833 1 : CHK_RET(CheckSuspendingStatus());
3834 1 : CHK_PRT_RET(
3835 : Is310P3Common(isHaveCpuRank_, deviceType_),
3836 : HCCL_RUN_INFO("[ReduceOutPlace]This method cannot be invoked in the current scenario."), HCCL_SUCCESS);
3837 :
3838 1 : bool aicpuUnfoldMode = false;
3839 1 : if (GetAicpuUnfoldConfig() == true && IsSupportSDMAReduce(inputPtr, outputPtr, dataType, op)
3840 1 : && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
3841 0 : aicpuUnfoldMode = true;
3842 : }
3843 :
3844 1 : bool isCapture = StreamIsCapture(stream);
3845 :
3846 4 : if (!IsAtomicInit()) {
3847 0 : HCCL_ERROR(
3848 : "[HcclCommunicator][ReduceOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
3849 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3850 0 : return HCCL_E_UNAVAIL;
3851 : }
3852 :
3853 3 : Stream streamObj(stream);
3854 4 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3855 :
3856 1 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3857 1 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3858 1 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3859 :
3860 1 : u32 perDataSize = SIZE_TABLE[dataType];
3861 1 : u64 totalSize = count * perDataSize;
3862 1 : OpParam opParam;
3863 1 : opParam.tag = tag;
3864 4 : opParam.inputPtr = inputPtr;
3865 4 : opParam.inputSize = totalSize;
3866 4 : opParam.outputPtr = outputPtr;
3867 4 : opParam.outputSize = totalSize;
3868 4 : opParam.DataDes.count = count;
3869 4 : opParam.DataDes.dataType = dataType;
3870 4 : opParam.reduceType = op;
3871 4 : opParam.root = root;
3872 4 : opParam.stream = streamObj;
3873 2 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
3874 2 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3875 2 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3876 2 : opParam.isCapture = isCapture;
3877 2 : opParam.opType = HcclCMDType::HCCL_CMD_REDUCE;
3878 :
3879 2 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_REDUCE, opParam));
3880 :
3881 0 : return HCCL_SUCCESS;
3882 4 : }
3883 :
3884 13 : HcclResult HcclCommunicator::ReduceScatter(
3885 : const std::string& tag, void* inputPtr, void* outputPtr, u64 count, HcclDataType dataType, HcclReduceOp op,
3886 : HcclRtStream stream, [[maybe_unused]] HcomCollOpInfo* opInfo)
3887 : {
3888 13 : CHK_RET(CheckSuspendingStatus());
3889 13 : bool aicpuUnfoldMode = false;
3890 16 : if (GetAicpuUnfoldConfig() == true && IsSupportSDMAReduce(inputPtr, outputPtr, dataType, op)
3891 16 : && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
3892 0 : aicpuUnfoldMode = true;
3893 : }
3894 :
3895 13 : if (!IsAtomicInit()) {
3896 0 : HCCL_ERROR(
3897 : "[HcclCommunicator][ReduceScatter]errNo[0x%016llx] hccl init must be called before call this function",
3898 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3899 0 : return HCCL_E_UNAVAIL;
3900 : }
3901 :
3902 21 : Stream streamObj(stream);
3903 21 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3904 :
3905 21 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3906 20 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3907 19 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3908 :
3909 16 : u32 perDataSize = SIZE_TABLE[dataType];
3910 :
3911 16 : OpParam opParam;
3912 20 : opParam.tag = tag;
3913 21 : opParam.inputPtr = inputPtr;
3914 21 : opParam.inputSize = userRankSize_ * count * perDataSize;
3915 21 : opParam.outputPtr = outputPtr;
3916 21 : opParam.outputSize = count * perDataSize;
3917 21 : opParam.DataDes.count = count;
3918 21 : opParam.DataDes.dataType = dataType;
3919 21 : opParam.reduceType = op;
3920 21 : opParam.stream = streamObj;
3921 16 : opParam.opType = HcclCMDType::HCCL_CMD_REDUCE_SCATTER;
3922 16 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3923 16 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3924 : // 用于inplace支持重执行场景的图模式归一至单算子模式
3925 17 : retryOrigWorkflowMode_ = GetWorkflowMode();
3926 : bool isHcclOpInplace
3927 17 : = IsHcclOpInplace(HcclCMDType::HCCL_CMD_REDUCE_SCATTER, opParam, userRank_, userRankSize_, isInplaceStatus_);
3928 21 : if (aicpuUnfoldMode && retryEnable_ && isHcclOpInplace) {
3929 0 : HCCL_DEBUG(
3930 : "The retry with inplace case is expected to be supported, "
3931 : "aicpuUnfoldMode[%d], retryEnable_[%d], isHcclOpInplace[%d], "
3932 : "therefore HcclWorkflowMode is converted from [%d] to HCCL_WORKFLOW_MODE_OP_BASE",
3933 : aicpuUnfoldMode, retryEnable_, isHcclOpInplace, static_cast<u8>(retryOrigWorkflowMode_));
3934 0 : CHK_RET(SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE));
3935 : }
3936 :
3937 21 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_REDUCE_SCATTER, opParam));
3938 :
3939 10 : CHK_RET(SetWorkflowMode(retryOrigWorkflowMode_));
3940 10 : return HCCL_SUCCESS;
3941 21 : }
3942 :
3943 21 : HcclResult HcclCommunicator::ReduceScatterOutPlace(
3944 : const std::string& tag, void* inputPtr, void* outputPtr, u64 count, HcclDataType dataType, HcclReduceOp op,
3945 : HcclRtStream stream)
3946 : {
3947 21 : CHK_RET(CheckSuspendingStatus());
3948 21 : if (userRankSize_ > 1) {
3949 20 : CHK_RET(CreateCommCCLbuffer());
3950 : }
3951 :
3952 38 : bool aicpuUnfoldMode = false;
3953 38 : if (GetAicpuUnfoldConfig() == true
3954 16 : && IsSupportSDMAReduce(
3955 16 : cclBufferManager_.GetInCCLbuffer().ptr(), cclBufferManager_.GetOutCCLbuffer().ptr(), dataType, op)
3956 53 : && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
3957 4 : aicpuUnfoldMode = true;
3958 : }
3959 :
3960 37 : bool isCapture = StreamIsCapture(stream);
3961 :
3962 37 : if (!IsAtomicInit()) {
3963 0 : HCCL_ERROR(
3964 : "[HcclCommunicator][ReduceScatterOutPlace]errNo[0x%016llx] hccl init must be called before"
3965 : " call this function",
3966 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
3967 0 : return HCCL_E_UNAVAIL;
3968 : }
3969 :
3970 37 : Stream streamObj(stream);
3971 37 : CHK_RET(callbackTask_->CallbackRegStream(stream));
3972 :
3973 37 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
3974 37 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
3975 37 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
3976 :
3977 37 : u32 perDataSize = SIZE_TABLE[dataType];
3978 :
3979 37 : OpParam opParam;
3980 36 : opParam.tag = tag;
3981 37 : opParam.inputPtr = inputPtr;
3982 37 : opParam.inputSize = userRankSize_ * count * perDataSize;
3983 37 : opParam.outputPtr = outputPtr;
3984 37 : opParam.outputSize = count * perDataSize;
3985 37 : opParam.DataDes.count = count;
3986 37 : opParam.DataDes.dataType = dataType;
3987 37 : opParam.reduceType = op;
3988 37 : opParam.stream = streamObj;
3989 37 : opParam.opType = HcclCMDType::HCCL_CMD_REDUCE_SCATTER;
3990 37 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
3991 37 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
3992 37 : opParam.isCapture = isCapture;
3993 37 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
3994 :
3995 37 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_REDUCE_SCATTER, opParam));
3996 :
3997 13 : return HCCL_SUCCESS;
3998 37 : }
3999 :
4000 0 : HcclResult HcclCommunicator::ReduceScatterV(
4001 : const std::string& tag, void* inputPtr, const void* inputCounts, const void* inputDispls, void* outputPtr,
4002 : u64 outputCount, HcclDataType dataType, HcclReduceOp op, HcclRtStream stream,
4003 : [[maybe_unused]] HcomCollOpInfo* opInfo)
4004 : {
4005 0 : CHK_RET(CheckSuspendingStatus());
4006 0 : if (userRankSize_ == 1) {
4007 : // rankSize为1时,退化为ReduceScatter
4008 0 : return ReduceScatter(tag, inputPtr, outputPtr, outputCount, dataType, op, stream);
4009 : }
4010 :
4011 0 : if (!IsAtomicInit()) {
4012 0 : HCCL_ERROR(
4013 : "[HcclCommunicator][ReduceScatterV]errNo[0x%016llx] hccl init must be called before call this function",
4014 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
4015 0 : return HCCL_E_UNAVAIL;
4016 : }
4017 :
4018 0 : Stream streamObj(stream);
4019 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
4020 :
4021 0 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
4022 0 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
4023 0 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
4024 :
4025 0 : const bool aicpuUnfoldMode = GetAicpuUnfoldConfig() && IsSupportSDMAReduce(inputPtr, outputPtr, dataType, op)
4026 0 : && (deviceType_ == DevType::DEV_TYPE_910_93);
4027 :
4028 0 : u32 perDataSize = SIZE_TABLE[dataType];
4029 0 : u64 inputSize = 0;
4030 0 : const u64* counts = static_cast<const u64*>(inputCounts);
4031 0 : for (u32 i = 0; i < userRankSize_; i++) {
4032 0 : inputSize += counts[i] * perDataSize;
4033 : }
4034 0 : CHK_PRT_RET(inputSize == 0, HCCL_WARNING("inputSize is 0, return ReduceScatterV success"), HCCL_SUCCESS);
4035 :
4036 0 : OpParam opParam;
4037 0 : opParam.tag = tag;
4038 0 : opParam.inputPtr = inputPtr;
4039 0 : opParam.inputSize = inputSize;
4040 0 : opParam.outputPtr = outputPtr;
4041 0 : opParam.outputSize = outputCount * perDataSize;
4042 0 : opParam.srcRank = userRank_; // rankId for access counts
4043 0 : opParam.VDataDes.counts = const_cast<void*>(inputCounts);
4044 0 : opParam.VDataDes.displs = const_cast<void*>(inputDispls);
4045 0 : opParam.VDataDes.dataType = dataType;
4046 0 : opParam.reduceType = op;
4047 0 : opParam.stream = streamObj;
4048 0 : opParam.opType = HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V;
4049 0 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
4050 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
4051 :
4052 0 : if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
4053 0 : for (u32 i = 0; i < userRankSize_; i++) {
4054 0 : HCCL_CONFIG_DEBUG(
4055 : HCCL_ALG,
4056 : "[HcclCommunicator][ReduceScatterV]userRank_[%u], rankIdx[%u], inputCounts[%llu], inputDispls[%llu]",
4057 : userRank_, i, counts[i], static_cast<const u64*>(inputDispls)[i]);
4058 : }
4059 : }
4060 :
4061 0 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V, opParam));
4062 :
4063 0 : return HCCL_SUCCESS;
4064 0 : }
4065 :
4066 0 : HcclResult HcclCommunicator::ReduceScatterVOutPlace(
4067 : const std::string& tag, void* inputPtr, void* outputPtr, const void* inputCounts, const void* inputDispls,
4068 : u64 outputCount, HcclDataType dataType, HcclReduceOp op, HcclRtStream stream)
4069 : {
4070 0 : CHK_RET(CheckSuspendingStatus());
4071 0 : if (userRankSize_ == 1) {
4072 : // rankSize为1时,退化为ReduceScatter
4073 0 : return ReduceScatterOutPlace(tag, inputPtr, outputPtr, outputCount, dataType, op, stream);
4074 : }
4075 :
4076 0 : CHK_RET(CreateCommCCLbuffer());
4077 0 : if (!IsAtomicInit()) {
4078 0 : HCCL_ERROR(
4079 : "[HcclCommunicator][ReduceScatterVOutPlace]errNo[0x%016llx] hccl init must be called before"
4080 : " call this function",
4081 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
4082 0 : return HCCL_E_UNAVAIL;
4083 : }
4084 :
4085 0 : bool isCapture = StreamIsCapture(stream);
4086 :
4087 0 : Stream streamObj(stream);
4088 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
4089 :
4090 0 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
4091 0 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
4092 0 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
4093 :
4094 0 : const bool aicpuUnfoldMode = GetAicpuUnfoldConfig() && IsSupportSDMAReduce(inputPtr, outputPtr, dataType, op)
4095 0 : && (deviceType_ == DevType::DEV_TYPE_910_93);
4096 :
4097 0 : u32 perDataSize = SIZE_TABLE[dataType];
4098 0 : u64 inputSize = 0;
4099 0 : const u64* counts = static_cast<const u64*>(inputCounts);
4100 0 : for (u32 i = 0; i < userRankSize_; i++) {
4101 0 : inputSize += counts[i] * perDataSize;
4102 : }
4103 :
4104 0 : OpParam opParam;
4105 0 : opParam.tag = tag;
4106 0 : opParam.inputPtr = inputPtr;
4107 0 : opParam.inputSize = inputSize;
4108 0 : opParam.outputPtr = outputPtr;
4109 0 : opParam.outputSize = outputCount * perDataSize;
4110 0 : opParam.srcRank = userRank_; // rankId for access counts
4111 0 : opParam.VDataDes.counts = const_cast<void*>(inputCounts);
4112 0 : opParam.VDataDes.displs = const_cast<void*>(inputDispls);
4113 0 : opParam.VDataDes.dataType = dataType;
4114 0 : opParam.reduceType = op;
4115 0 : opParam.stream = streamObj;
4116 0 : opParam.opType = HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V;
4117 0 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
4118 0 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
4119 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
4120 0 : opParam.isCapture = isCapture;
4121 0 : opParam.rankSize = userRankSize_;
4122 :
4123 0 : if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
4124 0 : for (u32 i = 0; i < userRankSize_; i++) {
4125 0 : HCCL_CONFIG_DEBUG(
4126 : HCCL_ALG,
4127 : "[HcclCommunicator][ReduceScatterVOutPlace]userRank_[%u],"
4128 : "rankIdx[%u], inputCounts[%llu], inputDispls[%llu]",
4129 : userRank_, i, counts[i], static_cast<const u64*>(inputDispls)[i]);
4130 : }
4131 : }
4132 :
4133 0 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V, opParam));
4134 :
4135 0 : return HCCL_SUCCESS;
4136 0 : }
4137 :
4138 0 : HcclResult HcclCommunicator::BatchSendRecv(
4139 : const std::string& tag, HcclSendRecvItem* sendRecvItemsPtr, u32 itemNum, rtStream_t stream)
4140 : {
4141 0 : if (!IsAtomicInit()) {
4142 0 : HCCL_ERROR(
4143 : "[HcclCommunicator][BatchSendRecv]errNo[0x%016llx] hccl init must be called before call this function",
4144 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
4145 0 : return HCCL_E_UNAVAIL;
4146 : }
4147 :
4148 0 : bool aicpuUnfoldMode = false;
4149 0 : if (GetAicpuUnfoldConfig() == true && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
4150 0 : aicpuUnfoldMode = true;
4151 : }
4152 :
4153 0 : bool isCapture = StreamIsCapture(stream);
4154 :
4155 0 : if (!IsAtomicInit()) {
4156 0 : HCCL_ERROR(
4157 : "[HcclCommunicator][BatchSendRecv]errNo[0x%016llx] hccl init must be called before call this function",
4158 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
4159 0 : return HCCL_E_UNAVAIL;
4160 : }
4161 0 : Stream streamObj(stream);
4162 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
4163 :
4164 0 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
4165 0 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
4166 0 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
4167 0 : OpParam opParam;
4168 0 : opParam.tag = tag;
4169 0 : opParam.stream = streamObj;
4170 0 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
4171 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
4172 0 : opParam.isCapture = isCapture;
4173 0 : opParam.BatchSendRecvDataDes.sendRecvItemsPtr = sendRecvItemsPtr;
4174 0 : opParam.BatchSendRecvDataDes.itemNum = itemNum;
4175 0 : opParam.opType = HcclCMDType::HCCL_CMD_BATCH_SEND_RECV;
4176 0 : opParam.isGroupMode = isGroupMode_;
4177 0 : if (isGroupMode_) {
4178 0 : opParam.aicpuUnfoldMode = true; // A2的GroupSendRecv也走aicpu模式
4179 : }
4180 :
4181 0 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_BATCH_SEND_RECV, opParam));
4182 :
4183 0 : return HCCL_SUCCESS;
4184 0 : }
4185 :
4186 0 : HcclResult HcclCommunicator::Send(
4187 : const std::string& tag, void* inputPtr, u64 count, HcclDataType dataType, u32 destRank, rtStream_t stream,
4188 : u32 srTag, u32 localGroupRank)
4189 : {
4190 0 : CHK_RET(CheckSuspendingStatus());
4191 0 : bool aicpuUnfoldMode = false;
4192 0 : if (GetAicpuUnfoldConfig() == true && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
4193 0 : aicpuUnfoldMode = true;
4194 : }
4195 :
4196 0 : if (!IsAtomicInit()) {
4197 0 : HCCL_ERROR(
4198 : "[HcclCommunicator][Send]errNo[0x%016llx] hccl init must be called before call this function",
4199 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
4200 0 : return HCCL_E_UNAVAIL;
4201 : }
4202 :
4203 0 : Stream streamObj(stream);
4204 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
4205 :
4206 0 : u32 perDataSize = SIZE_TABLE[dataType];
4207 0 : u64 totalSize = count * perDataSize;
4208 :
4209 0 : OpParam opParam;
4210 0 : opParam.tag = tag;
4211 0 : opParam.inputPtr = inputPtr;
4212 0 : opParam.inputSize = totalSize;
4213 0 : opParam.outputPtr = inputPtr;
4214 0 : opParam.outputSize = totalSize;
4215 0 : opParam.DataDes.count = count;
4216 0 : opParam.DataDes.dataType = dataType;
4217 0 : opParam.stream = streamObj;
4218 0 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
4219 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
4220 0 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
4221 0 : opParam.dstRank = destRank;
4222 0 : opParam.opType = HcclCMDType::HCCL_CMD_SEND;
4223 0 : opParam.srTag = srTag;
4224 0 : opParam.localGroupRank = localGroupRank;
4225 0 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_SEND, opParam));
4226 :
4227 0 : return HCCL_SUCCESS;
4228 0 : }
4229 :
4230 0 : HcclResult HcclCommunicator::SendOutPlace(
4231 : const std::string& tag, void* inputPtr, u64 count, HcclDataType dataType, u32 destRank, rtStream_t stream)
4232 : {
4233 0 : CHK_RET(CheckSuspendingStatus());
4234 0 : bool aicpuUnfoldMode = false;
4235 0 : if (GetAicpuUnfoldConfig() == true && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
4236 0 : aicpuUnfoldMode = true;
4237 : }
4238 :
4239 0 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
4240 0 : HCCL_ERROR(
4241 : "[%s][%s]SendOutPlace is not supported", LOG_KEYWORDS_TASK_EXEC.c_str(),
4242 : LOG_KEYWORDS_NOT_SUPPORTED.c_str());
4243 0 : return HCCL_E_NOT_SUPPORT;
4244 : }
4245 0 : if (!IsAtomicInit()) {
4246 0 : HCCL_ERROR(
4247 : "[HcclCommunicator][SendOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
4248 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
4249 0 : return HCCL_E_UNAVAIL;
4250 : }
4251 :
4252 0 : bool isCapture = StreamIsCapture(stream);
4253 :
4254 0 : Stream streamObj(stream);
4255 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
4256 :
4257 0 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
4258 0 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
4259 0 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
4260 :
4261 0 : u32 perDataSize = SIZE_TABLE[dataType];
4262 0 : u64 totalSize = count * perDataSize;
4263 :
4264 0 : OpParam opParam;
4265 0 : opParam.tag = tag;
4266 0 : opParam.inputPtr = inputPtr;
4267 0 : opParam.inputSize = totalSize;
4268 0 : opParam.outputPtr = inputPtr;
4269 0 : opParam.outputSize = totalSize;
4270 0 : opParam.DataDes.count = count;
4271 0 : opParam.DataDes.dataType = dataType;
4272 0 : opParam.stream = streamObj;
4273 0 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
4274 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
4275 0 : opParam.isCapture = isCapture;
4276 0 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
4277 0 : opParam.dstRank = destRank;
4278 0 : opParam.opType = HcclCMDType::HCCL_CMD_SEND;
4279 0 : opParam.localGroupRank = userRank_;
4280 0 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_SEND, opParam));
4281 :
4282 0 : return HCCL_SUCCESS;
4283 0 : }
4284 :
4285 0 : HcclResult HcclCommunicator::Receive(
4286 : const std::string& tag, void* outputPtr, u64 count, HcclDataType dataType, u32 srcRank, rtStream_t stream,
4287 : u32 srTag, u32 localGroupRank)
4288 : {
4289 0 : CHK_RET(CheckSuspendingStatus());
4290 0 : bool aicpuUnfoldMode = false;
4291 0 : if (GetAicpuUnfoldConfig() == true && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
4292 0 : aicpuUnfoldMode = true;
4293 : }
4294 :
4295 0 : if (!IsAtomicInit()) {
4296 0 : HCCL_ERROR(
4297 : "[HcclCommunicator][Receive]errNo[0x%016llx] hccl init must be called before call this function",
4298 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
4299 0 : return HCCL_E_UNAVAIL;
4300 : }
4301 :
4302 0 : Stream streamObj(stream);
4303 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
4304 :
4305 0 : u32 perDataSize = SIZE_TABLE[dataType];
4306 0 : u64 totalSize = count * perDataSize;
4307 :
4308 0 : OpParam opParam;
4309 0 : opParam.tag = tag;
4310 0 : opParam.inputPtr = outputPtr;
4311 0 : opParam.inputSize = totalSize;
4312 0 : opParam.outputPtr = outputPtr;
4313 0 : opParam.outputSize = totalSize;
4314 0 : opParam.DataDes.count = count;
4315 0 : opParam.DataDes.dataType = dataType;
4316 0 : opParam.stream = streamObj;
4317 0 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
4318 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
4319 0 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
4320 0 : opParam.srcRank = srcRank;
4321 0 : opParam.opType = HcclCMDType::HCCL_CMD_RECEIVE;
4322 0 : opParam.srTag = srTag;
4323 0 : opParam.localGroupRank = localGroupRank;
4324 0 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_RECEIVE, opParam));
4325 :
4326 0 : return HCCL_SUCCESS;
4327 0 : }
4328 :
4329 0 : HcclResult HcclCommunicator::ReceiveOutPlace(
4330 : const std::string& tag, void* outputPtr, u64 count, HcclDataType dataType, u32 srcRank, rtStream_t stream)
4331 : {
4332 0 : CHK_RET(CheckSuspendingStatus());
4333 0 : bool aicpuUnfoldMode = false;
4334 0 : if (GetAicpuUnfoldConfig() == true && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
4335 0 : aicpuUnfoldMode = true;
4336 : }
4337 :
4338 0 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
4339 0 : HCCL_ERROR(
4340 : "[%s][%s]ReceiveOutPlace is not supported", LOG_KEYWORDS_TASK_EXEC.c_str(),
4341 : LOG_KEYWORDS_NOT_SUPPORTED.c_str());
4342 0 : return HCCL_E_NOT_SUPPORT;
4343 : }
4344 0 : if (!IsAtomicInit()) {
4345 0 : HCCL_ERROR(
4346 : "[HcclCommunicator][ReceiveOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
4347 : HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
4348 0 : return HCCL_E_UNAVAIL;
4349 : }
4350 :
4351 0 : bool isCapture = StreamIsCapture(stream);
4352 :
4353 0 : Stream streamObj(stream);
4354 0 : CHK_RET(callbackTask_->CallbackRegStream(stream));
4355 :
4356 0 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
4357 0 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
4358 0 : implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
4359 :
4360 0 : u32 perDataSize = SIZE_TABLE[dataType];
4361 0 : u64 totalSize = count * perDataSize;
4362 :
4363 0 : OpParam opParam;
4364 0 : opParam.tag = tag;
4365 0 : opParam.inputPtr = outputPtr;
4366 0 : opParam.inputSize = totalSize;
4367 0 : opParam.outputPtr = outputPtr;
4368 0 : opParam.outputSize = totalSize;
4369 0 : opParam.DataDes.count = count;
4370 0 : opParam.DataDes.dataType = dataType;
4371 0 : opParam.stream = streamObj;
4372 0 : opParam.aicpuUnfoldMode = aicpuUnfoldMode;
4373 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
4374 0 : opParam.isCapture = isCapture;
4375 0 : opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
4376 0 : opParam.srcRank = srcRank;
4377 0 : opParam.opType = HcclCMDType::HCCL_CMD_RECEIVE;
4378 0 : opParam.localGroupRank = userRank_;
4379 0 : CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_RECEIVE, opParam));
4380 :
4381 0 : return HCCL_SUCCESS;
4382 0 : }
4383 :
4384 0 : HcclResult HcclCommunicator::RegressCalPreOp(
4385 : AlltoAllOperator*& alltoAllOperator, const OpParam& opParam, std::unique_ptr<PreProcessMetaInfo>& preMetaInfo)
4386 : {
4387 0 : HCCL_INFO("Run with Graph, alloc new stream");
4388 0 : Stream stream(StreamType::STREAM_TYPE_ONLINE);
4389 0 : return RegressCalPreOp(alltoAllOperator, opParam, preMetaInfo, stream);
4390 0 : }
4391 :
4392 0 : HcclResult HcclCommunicator::RegressCalPreOp(
4393 : AlltoAllOperator*& alltoAllOperator, [[maybe_unused]] const OpParam& opParam,
4394 : std::unique_ptr<PreProcessMetaInfo>& preMetaInfo, Stream& preProcessStream)
4395 : {
4396 0 : OpParam preProcessOpParam;
4397 0 : HcclWorkflowMode mode = GetWorkflowMode();
4398 0 : CHK_PRT_RET(
4399 : mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_RESERVED, HCCL_ERROR("Invalid Workflow Mode[%d]", mode),
4400 : HCCL_E_INTERNAL);
4401 :
4402 : // h to d
4403 0 : CHK_RET(SetInfoToDevice(preMetaInfo, preProcessStream));
4404 : // opParam准备
4405 0 : CHK_RET(alltoAllOperator->PreparePreOpParam(preProcessOpParam, preMetaInfo, preProcessStream));
4406 :
4407 : // 回归调用其它算子
4408 0 : HCCL_INFO(
4409 : "[HcclCommunicator][RegressCalPreOp] Regression calls other operators and opType[%u]", preMetaInfo->opType);
4410 0 : CHK_RET(ExecOp(preMetaInfo->opType, preProcessOpParam));
4411 0 : CHK_RET(hcclStreamSynchronize(preProcessStream.ptr(), commConfig_.GetConfigExecTimeOut()));
4412 0 : HCCL_DEBUG("[HcclCommunicator][RegressCalPreOp] preProcess tag[%s].", preProcessOpParam.tag.c_str());
4413 0 : SetWorkflowMode(mode);
4414 :
4415 : // d to h
4416 0 : HostMem hostCollectBuffer = HostMem::alloc(preMetaInfo->outputSize);
4417 0 : CHK_PTR_NULL(hostCollectBuffer.ptr());
4418 0 : CHK_RET(GetInfoFromDevice(preMetaInfo, mode, hostCollectBuffer));
4419 :
4420 0 : hostCollectBuffer_ = hostCollectBuffer;
4421 0 : alltoAllOperator->SetPreProcessResult(std::move(hostCollectBuffer));
4422 0 : HCCL_INFO("[HcclCommunicator][RegressCalPreOp] run success!");
4423 0 : return HCCL_SUCCESS;
4424 0 : }
4425 :
4426 0 : HcclResult HcclCommunicator::SaveRankInfoHasLinked(const AlgResourceRequest& resRequest)
4427 : {
4428 0 : for (auto& levelNSubCommTransport : resRequest.opTransport) {
4429 0 : for (auto& singleSubCommTransport : levelNSubCommTransport) {
4430 0 : for (auto& transportRequest : singleSubCommTransport.transportRequests) {
4431 0 : if (transportRequest.isValid) {
4432 0 : ranksLinked_.insert(transportRequest.remoteUserRank);
4433 0 : HCCL_INFO(
4434 : "[HcclCommunicator][SaveRankInfoHasLinked]Insert remote Rank[%u] to ranksLinked Set.",
4435 : transportRequest.remoteUserRank);
4436 : }
4437 : }
4438 : }
4439 : }
4440 :
4441 0 : return HCCL_SUCCESS;
4442 : }
4443 :
4444 2 : HcclResult HcclCommunicator::GetCacheMap(
4445 : std::unique_ptr<CollAlgOperator>& algOperator, OpParam& opParam, AlgType& algType, bool selectAivAlg,
4446 : std::string& newTag)
4447 : {
4448 2 : HcclCacheInfo cacheInfo;
4449 2 : CHK_RET(algOperator->GetCache(cacheInfo));
4450 2 : if (cacheInfo.isUseCache == false) {
4451 2 : return HCCL_SUCCESS;
4452 : }
4453 0 : cacheInfo.algType = algType;
4454 0 : cacheInfo.selectAivAlg = selectAivAlg;
4455 0 : cacheInfo.newTag = newTag;
4456 :
4457 0 : if (hcclCacheMap_.size() > CACHEMAP_MAXSIZE) {
4458 0 : size_t clearCount = static_cast<size_t>(CACHEMAP_MAXSIZE * CACHEMAP_CLEARPERCENT);
4459 0 : for (auto it = hcclCacheMap_.begin(); clearCount > 0 && it != hcclCacheMap_.end(); clearCount--) {
4460 0 : it = hcclCacheMap_.erase(it);
4461 : }
4462 : }
4463 :
4464 0 : hcclCacheMap_.emplace(std::make_pair(opParam, std::move(cacheInfo)));
4465 :
4466 0 : HCCL_INFO(
4467 : "[HcclCommunicator][GetCacheMap] algType %s, selectAivAlg %d, newTag %s", AlgTypeToStr(algType).c_str(),
4468 : selectAivAlg, newTag.c_str());
4469 0 : return HCCL_SUCCESS;
4470 2 : }
4471 :
4472 0 : HcclResult HcclCommunicator::ExecOpCache(HcclCMDType opType, OpParam& opParam, HcclCacheInfo& cacheInfo)
4473 : {
4474 : // 可用核数也需要作为key的一部分,防止cache中拿出来的和计算出来的实际核数不一致
4475 : // cache目前仅支持executor的kernel为1的情况
4476 0 : cacheInfo.resourceArgs.buffersIn = cacheInfo.buffersIn;
4477 0 : cacheInfo.resourceArgs.buffersOut = cacheInfo.buffersOut;
4478 0 : cacheInfo.resourceArgs.stream = opParam.stream.ptr(); // 刷新cache下发的stream
4479 0 : cacheInfo.opArgs.input = opParam.inputPtr;
4480 0 : cacheInfo.opArgs.output = opParam.outputPtr;
4481 0 : AlgType& algType = cacheInfo.algType;
4482 0 : bool selectAivAlg = cacheInfo.selectAivAlg;
4483 0 : std::string newTag = cacheInfo.newTag;
4484 0 : HcclResult ret = HCCL_SUCCESS;
4485 : // 更新aivtag
4486 0 : GetAivTag(1, opParam.isCapture, cacheInfo.resourceArgs.aivTag);
4487 0 : HCCL_INFO(
4488 : "[HcclCommunicator][ExecOpCache]buffersIn[%p] buffersOut[%p] tag[%s] opType[%d] "
4489 : "deterministic [%u] count[%llu] op[%d] userRank[%u] aiv tag [%d] stream [%d]",
4490 : cacheInfo.buffersIn, cacheInfo.buffersOut, identifier_.c_str(), opType, opParam.deterministic,
4491 : cacheInfo.opArgs.count, cacheInfo.opArgs.op, userRank_, cacheInfo.resourceArgs.aivTag, opParam.stream.id());
4492 0 : CHK_RET(HandleAclGraphFirstOpAivBuff(opParam.stream.ptr()));
4493 : // 保留dfx
4494 0 : CHK_RET(RegisterDfxInfo(opParam, algType, resMap_[newTag].slaveStreams, selectAivAlg));
4495 : // 头计数
4496 0 : CHK_RET(StarsCounter(dispatcher_, opParam.stream, HEAD, opParam.aicpuUnfoldMode, retryEnable_, selectAivAlg));
4497 0 : u64 dataSize
4498 0 : = (opParam.opType == HcclCMDType::HCCL_CMD_ALLTOALL ?
4499 0 : opParam.All2AllDataDes.sendCount * SIZE_TABLE[opParam.All2AllDataDes.sendType] :
4500 : 0);
4501 0 : if (opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V || opType == HcclCMDType::HCCL_CMD_ALLGATHER_V
4502 0 : || (opType == HcclCMDType::HCCL_CMD_ALLTOALL && dataSize >= AIV_ALL_TO_ALL_BIG_SIZE)) {
4503 0 : ret = ExecuteKernelLaunch(
4504 0 : cacheInfo.opArgs, cacheInfo.topoArgs, cacheInfo.resourceArgs, cacheInfo.algArgs, cacheInfo.extraArgs,
4505 0 : cacheInfo.profilingInfo);
4506 : } else {
4507 0 : ret = ExecuteKernelLaunch(
4508 0 : cacheInfo.opArgs, cacheInfo.topoArgs, cacheInfo.resourceArgs, cacheInfo.algArgs, cacheInfo.profilingInfo);
4509 : }
4510 : // 刷新核数
4511 0 : numBlocks_ = cacheInfo.resourceArgs.numBlocks;
4512 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[ExecOpCache]launch aiv failed, return[%d]", ret), ret);
4513 0 : CHK_RET(StarsCounter(dispatcher_, opParam.stream, TAIL, opParam.aicpuUnfoldMode, retryEnable_, selectAivAlg));
4514 0 : CHK_RET(UnRegisterDfxInfo(opParam, resMap_[newTag].slaveStreams));
4515 0 : if (selectAivAlg) {
4516 0 : aivClearEnable_ = false;
4517 : }
4518 0 : return HCCL_SUCCESS;
4519 0 : }
4520 :
4521 1 : void HcclCommunicator::SplitBsrData(
4522 : OpParam& opParam, std::vector<u8>& isDirectRemoteRank, std::vector<HcclSendRecvItem>& hostSendRecvInfo,
4523 : std::vector<HcclSendRecvItem>& aicpuSendRecvInfo)
4524 : {
4525 1 : u32 itemNum = opParam.BatchSendRecvDataDes.itemNum;
4526 1 : isDirectRemoteRank.resize(userRankSize_);
4527 1 : HCCL_INFO("[HcclCommunicator][SplitBsrData] rankSize %u", userRankSize_);
4528 1 : HcclSendRecvItem* sendRecvInfo = opParam.BatchSendRecvDataDes.sendRecvItemsPtr;
4529 3 : for (u32 i = 0; i < itemNum; i++) {
4530 2 : if (sendRecvInfo->buf == nullptr) {
4531 2 : sendRecvInfo++;
4532 2 : continue;
4533 : }
4534 0 : if (remoteTransportMap_[sendRecvInfo->remoteRank] == TransportType::TRANS_TYPE_DEVICE_DIRECT) {
4535 : // host 侧需要下发的数据
4536 0 : HCCL_INFO(
4537 : "[HcclCommunicator][SplitBsrData]host localRank %u remoteRank %u type %d sendRecvType %d count %llu",
4538 : userRank_, sendRecvInfo->remoteRank, remoteTransportMap_[sendRecvInfo->remoteRank],
4539 : sendRecvInfo->sendRecvType, sendRecvInfo->count);
4540 0 : isDirectRemoteRank[sendRecvInfo->remoteRank] = true;
4541 0 : hostSendRecvInfo.push_back(*sendRecvInfo);
4542 : } else {
4543 : // aicpu侧需要下发的数据
4544 0 : HCCL_INFO(
4545 : "[HcclCommunicator][SplitBsrData]aicpu localRank %u remoteRank %u type %d sendRecvType %d count %llu",
4546 : userRank_, sendRecvInfo->remoteRank, remoteTransportMap_[sendRecvInfo->remoteRank],
4547 : sendRecvInfo->sendRecvType, sendRecvInfo->count);
4548 0 : isDirectRemoteRank[sendRecvInfo->remoteRank] = false;
4549 0 : aicpuSendRecvInfo.push_back(*sendRecvInfo);
4550 : }
4551 0 : sendRecvInfo++;
4552 : }
4553 1 : HCCL_INFO(
4554 : "[HcclCommunicator][SplitBsrData] itemNum %u hostItemNum %zu aicpuItemNum %zu", itemNum,
4555 : hostSendRecvInfo.size(), aicpuSendRecvInfo.size());
4556 1 : return;
4557 : }
4558 :
4559 0 : bool HcclCommunicator::IsReduceWithInt64OrProd(HcclCMDType opType, const OpParam& opParam) const
4560 : {
4561 0 : if (opType == HcclCMDType::HCCL_CMD_ALLREDUCE || opType == HcclCMDType::HCCL_CMD_REDUCE
4562 0 : || opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER) {
4563 0 : if (opParam.reduceType == HcclReduceOp::HCCL_REDUCE_PROD
4564 0 : || opParam.DataDes.dataType == HcclDataType::HCCL_DATA_TYPE_INT64) {
4565 0 : return true;
4566 : }
4567 : }
4568 :
4569 0 : if (opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V) {
4570 0 : if (opParam.reduceType == HcclReduceOp::HCCL_REDUCE_PROD
4571 0 : || opParam.VDataDes.dataType == HcclDataType::HCCL_DATA_TYPE_INT64) {
4572 0 : return true;
4573 : }
4574 : }
4575 0 : return false;
4576 : }
4577 :
4578 71 : HcclResult HcclCommunicator::ExecOp(HcclCMDType opType, OpParam& opParam, bool isCustom)
4579 : {
4580 71 : CHK_PRT_RET(
4581 : isInvalidComm_,
4582 : HCCL_ERROR(
4583 : "[HcclCommunicator][%s] comm[%s], rank[%u], devId[%d], snapshot recoverying, "
4584 : "this comm is invalid, no operator is allowed to execute.",
4585 : __func__, identifier_.c_str(), userRank_, deviceLogicId_),
4586 : HCCL_E_UNAVAIL);
4587 :
4588 71 : if (retryEnable_ && needWarnAboutReduceProdInt64_ && IsReduceWithInt64OrProd(opType, opParam)) {
4589 0 : HCCL_RUN_WARNING(
4590 : "[HcclCommunicator][%s]comm[%s], opType[%d], reduceType[%d]. Reduce operators with prod operation or int64 "
4591 : "data type. This operator type unsupportd for AICPU mode, retry disabled",
4592 : __func__, identifier_.c_str(), opType, opParam.reduceType);
4593 0 : needWarnAboutReduceProdInt64_ = false;
4594 : }
4595 71 : std::string tag = opParam.tag;
4596 72 : u32 aivCoreLimit = numBlocks_;
4597 : // 单机AIV场景下cache复用,提升下发性能
4598 72 : if (implAlg_->GetAivModeConfig() && GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
4599 0 : if (aivCoreLimit == 0) {
4600 0 : aclError acl_ret = aclrtGetResInCurrentThread(ACL_RT_DEV_RES_VECTOR_CORE, &aivCoreLimit);
4601 0 : CHK_PRT_RET(
4602 : acl_ret != ACL_SUCCESS,
4603 : HCCL_ERROR("[HcclCommunicator][ExecOp] aclrtGetResInCurrentThread failed, ret=[%d]", acl_ret),
4604 : HCCL_E_PARA);
4605 : }
4606 0 : opParam.deterministic = implAlg_->GetDeterministicConfig();
4607 0 : opParam.aivCoreLimit = aivCoreLimit;
4608 0 : auto it = hcclCacheMap_.find(opParam);
4609 0 : if (it != hcclCacheMap_.end()) {
4610 0 : CHK_RET(ExecOpCache(opType, opParam, it->second));
4611 0 : return HCCL_SUCCESS;
4612 : }
4613 : }
4614 :
4615 67 : ForceProf(opParam.isCapture);
4616 66 : opParam.supportSymmetricMemory = IsSupportSymmetricMemory(opType, opParam);
4617 71 : opParam.supportZeroCopy = !opParam.supportSymmetricMemory && IsSupportZeroCopy(opParam);
4618 70 : opParam.aclGraphZeroCopyEnable = GetConfigAclGraphZeroCopyEnable();
4619 71 : bool isInGraphCaptureZeroCopy = false;
4620 71 : zeroCopyAclGraph_->SetRetryEnable(retryEnable_);
4621 66 : isInGraphCaptureZeroCopy = zeroCopyAclGraph_->SetAclGraphZeroCopyMode(
4622 : deviceType_, opType, opParam, implAlg_.get(), cclBufferManager_.GetOutCCLbufferSize());
4623 72 : if (isInGraphCaptureZeroCopy && userRankSize_ > 1) {
4624 0 : CHK_RET(CreateCommCCLbuffer());
4625 : }
4626 72 : if (isShareComm_) {
4627 0 : CHK_RET(ShareCCLbufferMgr::GetInstance().CheckCCLbuffConflict(cclBuffName_, opParam.stream.id()));
4628 : }
4629 72 : std::unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(opType);
4630 69 : CHK_SMART_PTR_NULL(algOperator);
4631 : // 算法选择
4632 68 : std::string algName;
4633 68 : std::string newTag;
4634 68 : if (opParam.aicpuUnfoldMode) {
4635 : // 用于inplace支持重执行判断
4636 4 : CHK_RET(algOperator->SetRetryEnable(retryEnable_));
4637 : }
4638 68 : if (GetExternalInputHcclAivMode()) {
4639 : // 用于判断图模式是否清零
4640 0 : CHK_RET(algOperator->SetAivClearEnable(aivClearEnable_));
4641 : }
4642 :
4643 68 : std::unique_lock<std::mutex> lock(commResMutex_);
4644 73 : ResourceLimit limit;
4645 73 : limit.ifLimit = true;
4646 73 : limit.aivCoreLimit = aivCoreLimit;
4647 73 : AlgDesc algDesc;
4648 69 : algDesc.isLastSelect = true;
4649 69 : CHK_RET(algOperator->SelectAlg(opParam.tag, opParam, limit, algName, algDesc, newTag));
4650 70 : if (isOnlyAiv_ && !algDesc.isAivMode) {
4651 0 : std::string opTypeName = GetCMDTypeEnumStr(opType);
4652 0 : HCCL_ERROR(
4653 : "[HcclCommunicator][ExecOp] opType[%s] currently do not select aiv mode, aiv only not support.",
4654 : opTypeName.c_str());
4655 0 : return HCCL_E_NOT_SUPPORT;
4656 0 : }
4657 70 : CHK_RET(PrepareZeroCopy(algName, algDesc, opParam));
4658 :
4659 70 : if (opParam.isCapture) {
4660 : // aclgraph使用新的Tag,避免影响其他操作
4661 0 : newTag += "_Capture";
4662 : // aclgraph零拷贝场景下,每个算子都有单独的tag,需要记录,在graph销毁时清理相关资源
4663 0 : if (isInGraphCaptureZeroCopy) {
4664 0 : CHK_RET(AclgraphCallback::GetInstance().InsertNewTagToCaptureResMap(this, newTag, opParam));
4665 0 : tagsRequiringHostCleanup_.insert(newTag);
4666 : }
4667 : }
4668 :
4669 70 : if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE && userRankSize_ > 1) {
4670 41 : CHK_RET(CreateCommCCLbuffer());
4671 : }
4672 73 : if (hcclNslbDp::GetInstance().GetGlobalCommTaskId() != 0) {
4673 0 : NslbDp_CollectOperTable(opType, opParam, algOperator->GetAlgType(), algName);
4674 : }
4675 :
4676 : // 资源创建
4677 73 : if ((resMap_.find(newTag) != resMap_.end()) && opParam.isCapture) {
4678 0 : AlgResourceRequest resRequest;
4679 0 : CHK_RET(algOperator->CalcResRequest(algName, opParam, resRequest));
4680 0 : if (HasRoceTransportLinks(resRequest.opTransport)) {
4681 0 : auto resTmp = resMap_[newTag];
4682 0 : ++captureCnt_;
4683 0 : newTag += std::to_string(captureCnt_);
4684 0 : resMap_[newTag] = resTmp;
4685 0 : resRequest.isInGraphCaptureZeroCopy = isInGraphCaptureZeroCopy;
4686 0 : CHK_RET(CleanTransportLinks(resRequest.opTransport, resMap_[newTag].opTransportResponse));
4687 0 : if (IsEnableBackupLink()) {
4688 0 : CHK_RET(CleanTransportLinks(resRequest.opTransport, resMap_[newTag].opTransportResponseBackUp));
4689 : }
4690 : // 记录指令信息用于一致性校验
4691 0 : CHK_RET(RecordOpPara(opType, opParam));
4692 0 : CHK_RET(IncreAllocLink(newTag, opParam, resRequest, resMap_[newTag]));
4693 : // 移除tag对应的指令信息
4694 0 : CHK_RET(RankConsistentcyChecker::GetInstance().DelOpPara(opParam.tag));
4695 : // aclgraph零拷贝场景下,除第一个capture外,需要记录,在graph销毁时清理相关资源
4696 0 : CHK_RET(AclgraphCallback::GetInstance().InsertNewTagToCaptureResMap(this, newTag, opParam));
4697 0 : tagsRequiringHostCleanup_.insert(newTag);
4698 0 : }
4699 0 : }
4700 72 : InsertNewTagToTagMap(newTag, opParam.tag);
4701 72 : bool needIncreLink = false;
4702 : // aiv算法不需要申请host和device侧的从流
4703 72 : bool selectAivAlg = algDesc.isAivMode;
4704 72 : if (resMap_.find(newTag) == resMap_.end()) {
4705 70 : AlgResourceRequest resRequest;
4706 71 : CHK_RET(algOperator->CalcResRequest(algName, opParam, resRequest));
4707 72 : if (opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV) {
4708 0 : CHK_RET(SaveRankInfoHasLinked(resRequest));
4709 : }
4710 72 : resRequest.isInGraphCaptureZeroCopy = isInGraphCaptureZeroCopy;
4711 72 : CHK_RET(RecordOpPara(opType, opParam));
4712 71 : HcclResult ret = AllocAlgResource(newTag, opType, opParam, resRequest, resMap_[newTag], selectAivAlg);
4713 73 : CHK_PRT_RET(
4714 : ret != HCCL_SUCCESS,
4715 : HCCL_ERROR("[HcclCommunicator][ExecOp] AllocAlgResource failed, algName=[%s]", algName.c_str()), ret);
4716 20 : CHK_RET(RankConsistentcyChecker::GetInstance().DelOpPara(opParam.tag));
4717 :
4718 : // 对于91093超节点内aiv跨机通信算子,将不同机的CCLbuffer地址存在约定好的aiv将读取的HBM位置
4719 20 : CHK_RET(algOperator->PrepareCommInfoToDevice(algName, resMap_[newTag]));
4720 :
4721 20 : if (!isHaveCpuRank_) {
4722 20 : if (isUseRankPort_) {
4723 20 : std::vector<u32>& nicPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
4724 20 : std::vector<u32>& vnicPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
4725 20 : Heartbeat::GetInstance(deviceLogicId_)
4726 20 : .SetRankPortInfo(isUseRankPort_, nicPorts, vnicPorts, commPortConfig_.devPortSwitchOn);
4727 : }
4728 : // 开始注册心跳
4729 20 : if (opType == HcclCMDType::HCCL_CMD_SEND) {
4730 0 : CHK_RET(RegisterToHeartBeat(opParam.dstRank, tag));
4731 0 : hbSendRecvTags_.emplace(tag);
4732 20 : } else if (opType == HcclCMDType::HCCL_CMD_RECEIVE) {
4733 0 : CHK_RET(RegisterToHeartBeat(opParam.srcRank, tag));
4734 0 : hbSendRecvTags_.emplace(tag);
4735 : } else {
4736 20 : CHK_RET(RegisterToHeartBeat());
4737 : }
4738 : }
4739 19 : CHK_RET(UpdateZeroCopy(opParam, resMap_[newTag]));
4740 73 : } else if (opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV) {
4741 : // batchsendrecv需要根据任务来确定和哪些卡建链,因此复用tag,并在此基础上实现增量建链
4742 0 : AlgResourceRequest resRequest;
4743 0 : CHK_RET(algOperator->CalcIncreLinkRequest(algName, opParam, ranksLinked_, resRequest, needIncreLink));
4744 0 : if (needIncreLink) {
4745 0 : CHK_RET(RecordOpPara(opType, opParam));
4746 0 : CHK_RET(IncreAllocLink(newTag, opParam, resRequest, resMap_[newTag]));
4747 0 : CHK_RET(RankConsistentcyChecker::GetInstance().DelOpPara(opParam.tag));
4748 0 : opParam.needIncreLink = true;
4749 : }
4750 0 : }
4751 :
4752 : // 算法执行
4753 19 : if (selectAivAlg) {
4754 0 : CHK_RET(HandleAclGraphFirstOpAivBuff(opParam.stream.ptr()));
4755 0 : if (aivClearEnable_) {
4756 : // 用于判断图模式是否清零
4757 0 : CHK_RET(algOperator->SetAivClearEnable(aivClearEnable_));
4758 0 : aivOffloadTag_ = 1;
4759 : }
4760 0 : GetAivTag(algDesc.aivTagNum, opParam.isCapture, opParam.aivTag);
4761 0 : HCCL_INFO(
4762 : "[HcclCommunicator][ExecOp] tag[%s] userRank[%u] cur aiv tag [%d]", identifier_.c_str(), userRank_,
4763 : opParam.aivTag);
4764 0 : opParam.aicpuUnfoldMode = false;
4765 0 : opParam.aicpuCacheEnable = 0;
4766 0 : CHK_RET(algOperator->SetNumBlocks(aivCoreLimit));
4767 : }
4768 19 : std::vector<HcclSendRecvItem> hostSendRecvInfo;
4769 19 : std::vector<HcclSendRecvItem> aicpuSendRecvInfo;
4770 19 : std::vector<u8> isDirectRemoteRank;
4771 19 : if (opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV && deviceType_ == DevType::DEV_TYPE_910_93) {
4772 0 : SplitBsrData(opParam, isDirectRemoteRank, hostSendRecvInfo, aicpuSendRecvInfo);
4773 : // A3 bsr记录Direct下发方式数据
4774 0 : opParam.BatchSendRecvDataDes.isDirectRemoteRank = isDirectRemoteRank.data();
4775 0 : if (!retryEnable_) {
4776 0 : opParam.BatchSendRecvDataDes.sendRecvItemsPtr = aicpuSendRecvInfo.data();
4777 0 : opParam.BatchSendRecvDataDes.itemNum = aicpuSendRecvInfo.size();
4778 : }
4779 : }
4780 : // A2 Group SendRecv 将isDirectRemoteRank全部置为false
4781 19 : if (opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV && deviceType_ == DevType::DEV_TYPE_910B && isGroupMode_) {
4782 0 : isDirectRemoteRank.resize(userRankSize_, 0);
4783 0 : opParam.BatchSendRecvDataDes.isDirectRemoteRank = isDirectRemoteRank.data();
4784 : }
4785 19 : auto algType = algOperator->GetAlgType();
4786 19 : CHK_RET(RegisterDfxInfo(opParam, algType, resMap_[newTag].slaveStreams, selectAivAlg, tag));
4787 : // 头计数
4788 19 : CHK_RET(StarsCounter(dispatcher_, opParam.stream, HEAD, opParam.aicpuUnfoldMode, retryEnable_, selectAivAlg));
4789 19 : if (opParam.aicpuUnfoldMode) {
4790 0 : isInplaceStatus_ = 0;
4791 0 : inPlaceSupportRetryStatus_ = InplaceSupportRetryStatus::INPLACE_STATUS_END;
4792 : // algOperator->SupportRetryWithInplaceCheck 依赖 algOperator->SetRetryEnable 才能正确返回是否支持inplace
4793 :
4794 0 : inplaceSupportRetry_ = algOperator->SupportRetryWithInplaceCheck(
4795 0 : opType, opParam, algName, isInplaceStatus_, inPlaceSupportRetryStatus_);
4796 0 : HCCL_INFO(
4797 : "[HcclCommunicator][ExecOp] aicpu Unfold mode algType[%s], inplaceSupportRetry_[%d], opType[%d], "
4798 : "isInplaceStatus_[%d], inPlaceSupportRetryStatus_[%d].",
4799 : AlgTypeToStr(algType).c_str(), inplaceSupportRetry_, opType, isInplaceStatus_, inPlaceSupportRetryStatus_);
4800 0 : CHK_RET(OrchestrateAicpu(opType, algName, opParam, resMap_[newTag], newTag, algType, isCustom, needIncreLink));
4801 : } else {
4802 : // HOST展开aclgraph场景,capture从流
4803 19 : if (!selectAivAlg) {
4804 22 : CHK_RET(CaptureSlaveStreams(opParam.stream.ptr(), resMap_[newTag].slaveStreams));
4805 : }
4806 19 : OpCounterInfo opCounter;
4807 19 : CHK_RET(GetOpCountInfo(opCounter));
4808 19 : CHK_RET(algOperator->SetOpCounter(opCounter));
4809 19 : CHK_RET(algOperator->Orchestrate(algName, opParam, resMap_[newTag]));
4810 16 : if (hostResMap_.find(newTag) == hostResMap_.end()) {
4811 16 : hostResMap_.insert(newTag);
4812 : }
4813 16 : CHK_RET(algOperator->GetNumBlocks(numBlocks_));
4814 16 : if (implAlg_->GetAivModeConfig() && GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE
4815 16 : && !opParam.isCapture) {
4816 0 : CHK_RET(GetCacheMap(algOperator, opParam, algType, selectAivAlg, newTag));
4817 : }
4818 : }
4819 : // A3 bsr 只有走NPU直驱的时候hostSendRecvInfo才有内容
4820 16 : if (!hostSendRecvInfo.empty()) {
4821 : // A3 bsr获取到host侧需要下发的数据
4822 0 : HCCL_INFO("[HcclCommunicator][ExecOp] hostSendRecvInfo size %zu", hostSendRecvInfo.size());
4823 0 : opParam.BatchSendRecvDataDes.sendRecvItemsPtr = hostSendRecvInfo.data();
4824 0 : opParam.BatchSendRecvDataDes.itemNum = hostSendRecvInfo.size();
4825 0 : opParam.aicpuUnfoldMode = false;
4826 0 : opParam.aicpuCacheEnable = 0;
4827 0 : std::string tempTag;
4828 0 : std::unique_ptr<CollAlgOperator> newalgOperator = implAlg_->GetAlgOperator(opType);
4829 0 : CHK_SMART_PTR_NULL(newalgOperator);
4830 0 : CHK_RET(newalgOperator->SelectAlg(opParam.tag, opParam, limit, algName, algDesc, tempTag));
4831 0 : CHK_RET(newalgOperator->Orchestrate(algName, opParam, resMap_[newTag]));
4832 0 : }
4833 16 : lock.unlock();
4834 : // 尾计数
4835 16 : CHK_RET(StarsCounter(dispatcher_, opParam.stream, TAIL, opParam.aicpuUnfoldMode, retryEnable_, selectAivAlg));
4836 16 : CHK_RET(UnRegisterDfxInfo(opParam, resMap_[newTag].slaveStreams));
4837 16 : if (selectAivAlg) {
4838 0 : CHK_RET(algOperator->SetAivClearEnable(false));
4839 0 : aivClearEnable_ = false;
4840 : }
4841 16 : if (hcclNslbDp::GetInstance().GetGlobalCommTaskId() != 0 && hcclNslbDp::GetInstance().GetInitNetCoFlag() == true) {
4842 0 : AdjInfo nslbAdjInfo = {};
4843 0 : CHK_RET(algOperator->GetAdjInfo(algName, opParam, resMap_[newTag], nslbAdjInfo));
4844 0 : NslbDp_CollectSendAdjTable(opType, opParam, algOperator->GetAlgType(), nslbAdjInfo);
4845 0 : }
4846 16 : if (isInGraphCaptureZeroCopy) {
4847 0 : SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
4848 : }
4849 16 : return HCCL_SUCCESS;
4850 73 : }
4851 :
4852 : HcclResult
4853 0 : HcclCommunicator::FreeScratchMemOnOpBaseMode(DeviceMem& scratchMem, const OpParam& opParam, const HcclCMDType& opType)
4854 : {
4855 : // 当前单算子模式下scratch内存为手动申请,需要手动进行释放
4856 0 : if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE || IsForceAicpuOpBaseMode(opParam, opType)) {
4857 0 : scratchMem.free();
4858 : }
4859 0 : return HCCL_SUCCESS;
4860 : }
4861 :
4862 0 : HcclResult HcclCommunicator::ReAllocScratchMemForAlltoall(
4863 : HcclCMDType opType, const OpParam& opParam, AlgResourceRequest& resRequest, AlgResourceResponse& algResResponse)
4864 : {
4865 0 : if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB
4866 0 : && !IsForceAicpuOpBaseMode(opParam, opType)) {
4867 0 : if (resRequest.scratchMemSize > 0) {
4868 0 : algResResponse.scratchMem = GetWorkspaceScracthMem(opParam.tag, resRequest.scratchMemSize);
4869 : }
4870 0 : HCCL_DEBUG("[%s] WorkflowMode set for workspace opType[%u] tag[%s]", __func__, opType, opParam.tag.c_str());
4871 0 : } else if (
4872 0 : GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE || IsForceAicpuOpBaseMode(opParam, opType)) {
4873 0 : CHK_RET(AllocOpBaseModeScratchMem(opType, opParam, resRequest, algResResponse));
4874 0 : HCCL_DEBUG("[%s] WorkflowMode set for opType[%u] tag[%s]", __func__, opType, opParam.tag.c_str());
4875 : } else {
4876 0 : HCCL_ERROR("[%s] WorkflowMode is not set for opType[%u] tag[%s]", __func__, opType, opParam.tag.c_str());
4877 0 : return HCCL_E_PARA;
4878 : }
4879 0 : return HCCL_SUCCESS;
4880 : }
4881 :
4882 1 : HcclResult HcclCommunicator::HandleExistAlgResource(
4883 : const std::string& newTag, const std::string& algName, HcclCMDType opType, const OpParam& opParam,
4884 : std::unique_ptr<CollAlgOperator>& algOperator, bool selectAivAlg, bool aicpuUnfoldModeFor910B,
4885 : bool needRecreateAlltoallComm)
4886 : {
4887 1 : if (needRecreateAlltoallComm) {
4888 0 : CHK_RET(hcclStreamSynchronize(opParam.stream.ptr(), commConfig_.GetConfigExecTimeOut()));
4889 :
4890 0 : AlgResourceRequest resRequest;
4891 0 : CHK_RET(algOperator->CalcResRequest(algName, opParam, resRequest));
4892 :
4893 : // 释放旧内存防止泄漏
4894 0 : CHK_RET(FreeScratchMemOnOpBaseMode(resMap_[newTag].scratchMem, opParam, opType));
4895 :
4896 0 : if (aicpuUnfoldModeFor910B) {
4897 0 : CHK_RET(ReAllocScratchMemForAlltoall(opType, opParam, resRequest, resMap_[newTag]));
4898 0 : isContextLaunched_ = true;
4899 : } else {
4900 0 : CHK_RET(RecordOpPara(opType, opParam));
4901 0 : CHK_RET(AllocAlgResource(newTag, opType, opParam, resRequest, resMap_[newTag], selectAivAlg));
4902 0 : CHK_RET(RankConsistentcyChecker::GetInstance().DelOpPara(opParam.tag));
4903 :
4904 0 : if (!isHaveCpuRank_) {
4905 0 : if (isUseRankPort_) {
4906 0 : std::vector<u32>& nicPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
4907 0 : std::vector<u32>& vnicPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
4908 0 : Heartbeat::GetInstance(deviceLogicId_)
4909 0 : .SetRankPortInfo(isUseRankPort_, nicPorts, vnicPorts, commPortConfig_.devPortSwitchOn);
4910 : }
4911 0 : CHK_RET(RegisterToHeartBeat());
4912 : }
4913 : }
4914 0 : } else {
4915 1 : DeviceMem tinySendRecvMem;
4916 1 : CHK_RET(implAlg_->GetTinyMem(tinySendRecvMem));
4917 1 : CHK_RET(CalcTinySendRecvMem(opParam, resMap_[newTag], tinySendRecvMem));
4918 1 : }
4919 1 : return HCCL_SUCCESS;
4920 : }
4921 :
4922 5 : HcclResult HcclCommunicator::ExecOpAlltoAll(HcclCMDType opType, OpParam& opParam, bool isCustom)
4923 : {
4924 5 : CHK_PRT_RET(
4925 : isInvalidComm_,
4926 : HCCL_ERROR(
4927 : "[HcclCommunicator][%s] comm[%s], rank[%u], devId[%d], snapshot recoverying, "
4928 : "this comm is invalid, no operator is allowed to execute.",
4929 : __func__, identifier_.c_str(), userRank_, deviceLogicId_),
4930 : HCCL_E_UNAVAIL);
4931 :
4932 5 : std::string& tag = opParam.tag;
4933 5 : u32 aivCoreLimit = numBlocks_;
4934 : // 单机AIV场景下cache复用,提升下发性能
4935 5 : if (implAlg_->GetAivModeConfig() && GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
4936 5 : if (aivCoreLimit == 0) {
4937 4 : aclError acl_ret = aclrtGetResInCurrentThread(ACL_RT_DEV_RES_VECTOR_CORE, &aivCoreLimit);
4938 4 : CHK_PRT_RET(
4939 : acl_ret != ACL_SUCCESS,
4940 : HCCL_ERROR("[HcclCommunicator][ExecOpAlltoAll] aclrtGetResInCurrentThread failed, ret=[%d]", acl_ret),
4941 : HCCL_E_PARA);
4942 : }
4943 5 : opParam.deterministic = implAlg_->GetDeterministicConfig();
4944 5 : opParam.aivCoreLimit = aivCoreLimit;
4945 5 : auto it = hcclCacheMap_.find(opParam);
4946 5 : if (it != hcclCacheMap_.end()) {
4947 0 : CHK_RET(ExecOpCache(opType, opParam, it->second));
4948 0 : return HCCL_SUCCESS;
4949 : }
4950 : }
4951 :
4952 5 : ForceProf(opParam.isCapture);
4953 5 : bool isInGraphCaptureZeroCopy = false;
4954 5 : zeroCopyAclGraph_->SetRetryEnable(retryEnable_);
4955 5 : opParam.supportSymmetricMemory = IsSupportSymmetricMemory(opType, opParam);
4956 5 : opParam.supportZeroCopy = !opParam.supportSymmetricMemory && IsSupportZeroCopy(opParam);
4957 5 : opParam.aclGraphZeroCopyEnable = GetConfigAclGraphZeroCopyEnable();
4958 5 : isInGraphCaptureZeroCopy = zeroCopyAclGraph_->SetAclGraphZeroCopyMode(
4959 : deviceType_, opType, opParam, implAlg_.get(), cclBufferManager_.GetOutCCLbufferSize());
4960 5 : if (isInGraphCaptureZeroCopy && userRankSize_ > 1) {
4961 0 : CHK_RET(CreateCommCCLbuffer());
4962 : }
4963 5 : if (isShareComm_) {
4964 0 : CHK_RET(ShareCCLbufferMgr::GetInstance().CheckCCLbuffConflict(cclBuffName_, opParam.stream.id()));
4965 : }
4966 5 : std::unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(opType);
4967 5 : AlltoAllOperator* alltoAllOperator = dynamic_cast<AlltoAllOperator*>(algOperator.get());
4968 5 : CHK_PTR_NULL(alltoAllOperator);
4969 :
4970 5 : bool isSatisfyA2ACPForA3Condition = alltoAllOperator->IsSatisfyA2AContinuousPipelineFor91093Condition(opParam);
4971 5 : bool IsSatisfyA2ACPForA2Condition = alltoAllOperator->IsSatisfyAlltoallContinuousPipelineCondition(opParam);
4972 5 : if (IsSatisfyA2ACPForA2Condition || isSatisfyA2ACPForA3Condition) {
4973 0 : opParam.aicpuUnfoldMode = true;
4974 0 : opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
4975 : }
4976 :
4977 : // 算法选择
4978 5 : std::string algName;
4979 5 : std::string newTag;
4980 5 : if (opParam.aicpuUnfoldMode) {
4981 : // 用于inplace支持重执行判断
4982 0 : CHK_RET(algOperator->SetRetryEnable(retryEnable_));
4983 : }
4984 5 : std::unique_ptr<PreProcessMetaInfo> preMetaInfo = std::make_unique<PreProcessMetaInfo>();
4985 5 : CHK_SMART_PTR_NULL(preMetaInfo);
4986 :
4987 5 : bool preProcessFlag = alltoAllOperator->JudgeIfNeedPreProcessAndGetParam(opParam, preMetaInfo);
4988 5 : if (preProcessFlag) {
4989 0 : if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
4990 0 : CHK_RET(RegressCalPreOp(alltoAllOperator, opParam, preMetaInfo, const_cast<Stream&>(opParam.stream)));
4991 : } else {
4992 0 : CHK_RET(RegressCalPreOp(alltoAllOperator, opParam, preMetaInfo));
4993 : }
4994 : }
4995 :
4996 5 : if (deviceType_ == DevType::DEV_TYPE_910B && userRankSize_ > 1) {
4997 : // 用于AIV支持Roce直驱判断
4998 5 : CHK_RET(IsSupportAIVNormalQP(devicePhyId_, opParam.supportRoceDirect));
4999 : }
5000 :
5001 5 : std::unique_lock<std::mutex> lock(commResMutex_);
5002 5 : ResourceLimit limit;
5003 5 : limit.ifLimit = true;
5004 5 : limit.aivCoreLimit = aivCoreLimit;
5005 5 : AlgDesc algDesc;
5006 5 : algDesc.isLastSelect = true;
5007 5 : CHK_RET(algOperator->SelectAlg(opParam.tag, opParam, limit, algName, algDesc, newTag));
5008 : // 是否是AIV直驱Roce场景
5009 5 : opParam.isNpuDirectRoce = algName == "AlltoAllDirectFullmeshAIVExecutor";
5010 5 : if (isOnlyAiv_ && !algDesc.isAivMode) {
5011 0 : std::string opTypeName = GetCMDTypeEnumStr(opType);
5012 0 : HCCL_ERROR(
5013 : "[HcclCommunicator][ExecOp] opType[%s] currently do not select aiv mode, aiv only not support.",
5014 : opTypeName.c_str());
5015 0 : return HCCL_E_NOT_SUPPORT;
5016 0 : }
5017 5 : CHK_RET(PrepareZeroCopy(algName, algDesc, opParam));
5018 :
5019 5 : if (opParam.isCapture) {
5020 : // aclgraph使用新的Tag,避免影响其他操作
5021 3 : newTag += "_Capture";
5022 : // aclgraph零拷贝场景下,每个算子都有单独的tag,需要记录,在graph销毁时清理相关资源
5023 3 : if (isInGraphCaptureZeroCopy) {
5024 0 : CHK_RET(AclgraphCallback::GetInstance().InsertNewTagToCaptureResMap(this, newTag, opParam));
5025 0 : tagsRequiringHostCleanup_.insert(newTag);
5026 : }
5027 : }
5028 :
5029 0 : auto isSupportAlg = [](const std::string& algName, bool aicpuUnfoldMode) -> bool {
5030 0 : return ((algName == "RunAlltoAllVFullMesh" || algName == "RunAlltoAllVTwoLevelPipeline") && aicpuUnfoldMode)
5031 0 : || (algName == "RunAlltoAllDirectFullmesh" || algName == "RunAlltoAllFullMeshSymmetricMemory");
5032 : };
5033 5 : bool isOpbaseMode = GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE;
5034 5 : if ((isOpbaseMode && userRankSize_ > 1) || (isSupportAlg(algName, opParam.aicpuUnfoldMode))) {
5035 5 : CHK_RET(CreateCommCCLbuffer());
5036 : }
5037 : // 资源创建
5038 5 : bool selectAivAlg = algDesc.isAivMode;
5039 5 : if ((resMap_.find(newTag) != resMap_.end()) && opParam.isCapture) {
5040 1 : AlgResourceRequest resRequest;
5041 1 : CHK_RET(algOperator->CalcResRequest(algName, opParam, resRequest));
5042 1 : if (HasRoceTransportLinks(resRequest.opTransport)) {
5043 0 : auto resTmp = resMap_[newTag];
5044 0 : ++captureCnt_;
5045 0 : newTag += std::to_string(captureCnt_);
5046 0 : resMap_[newTag] = resTmp;
5047 0 : resRequest.isInGraphCaptureZeroCopy = isInGraphCaptureZeroCopy;
5048 0 : CHK_RET(CleanTransportLinks(resRequest.opTransport, resMap_[newTag].opTransportResponse));
5049 0 : if (IsEnableBackupLink()) {
5050 0 : CHK_RET(CleanTransportLinks(resRequest.opTransport, resMap_[newTag].opTransportResponseBackUp));
5051 : }
5052 : // 记录指令信息用于一致性校验
5053 0 : CHK_RET(RecordOpPara(opType, opParam));
5054 0 : CHK_RET(IncreAllocLink(newTag, opParam, resRequest, resMap_[newTag]));
5055 : // 移除tag对应的指令信息
5056 0 : CHK_RET(RankConsistentcyChecker::GetInstance().DelOpPara(opParam.tag));
5057 : // aclgraph零拷贝场景下,除第一个capture外,需要记录,在graph销毁时清理相关资源
5058 0 : CHK_RET(AclgraphCallback::GetInstance().InsertNewTagToCaptureResMap(this, newTag, opParam));
5059 0 : tagsRequiringHostCleanup_.insert(newTag);
5060 0 : }
5061 1 : }
5062 5 : InsertNewTagToTagMap(newTag, opParam.tag);
5063 5 : bool aicpuUnfoldModeFor910B = deviceType_ == DevType::DEV_TYPE_910B && opParam.aicpuUnfoldMode
5064 10 : && (algName == "RunAlltoAllVStaged" || algName == "RunAlltoAllVFullMesh");
5065 5 : bool needRecreateAlltoallComm = false;
5066 5 : if (resMap_.find(newTag) == resMap_.end()) {
5067 4 : AlgResourceRequest resRequest;
5068 4 : CHK_RET(algOperator->CalcResRequest(algName, opParam, resRequest));
5069 4 : resRequest.isInGraphCaptureZeroCopy = isInGraphCaptureZeroCopy;
5070 4 : CHK_RET(RecordOpPara(opType, opParam));
5071 4 : CHK_RET(AllocAlgResource(newTag, opType, opParam, resRequest, resMap_[newTag], selectAivAlg));
5072 4 : CHK_RET(RankConsistentcyChecker::GetInstance().DelOpPara(opParam.tag));
5073 4 : if (opParam.isNpuDirectRoce) {
5074 : // AIV直驱roce多机场景,需要生成RMAInfo并拷贝至Device
5075 0 : CHK_RET(GenAiRMAInfoV2(newTag));
5076 0 : CHK_RET(H2DAiRMAInfoV2(newTag, opParam.stream.ptr()));
5077 : }
5078 : // 对于91093超节点内aiv跨机通信算子,将不同机的CCLbuffer地址存在约定好的aiv将读取的HBM位置
5079 4 : CHK_RET(algOperator->PrepareCommInfoToDevice(algName, resMap_[newTag]));
5080 :
5081 4 : if (!isHaveCpuRank_) {
5082 4 : if (isUseRankPort_) {
5083 4 : std::vector<u32>& nicPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
5084 4 : std::vector<u32>& vnicPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
5085 4 : Heartbeat::GetInstance(deviceLogicId_)
5086 4 : .SetRankPortInfo(isUseRankPort_, nicPorts, vnicPorts, commPortConfig_.devPortSwitchOn);
5087 : }
5088 4 : CHK_RET(RegisterToHeartBeat());
5089 : }
5090 4 : CHK_RET(UpdateZeroCopy(opParam, resMap_[newTag]));
5091 4 : } else {
5092 1 : CHK_RET(alltoAllOperator->CheckNeedRecreateComm(
5093 : algName, opParam, resMap_[newTag].scratchMem.size(), needRecreateAlltoallComm));
5094 1 : HCCL_INFO(
5095 : "resMap_ find this newTag[%s], and need to judge whether recreate comm [%d]", newTag.c_str(),
5096 : needRecreateAlltoallComm);
5097 1 : CHK_RET(HandleExistAlgResource(
5098 : newTag, algName, opType, opParam, algOperator, selectAivAlg, aicpuUnfoldModeFor910B,
5099 : needRecreateAlltoallComm));
5100 : }
5101 5 : auto& algRes = resMap_[newTag];
5102 :
5103 5 : if (hcclNslbDp::GetInstance().GetGlobalCommTaskId() != 0 && hcclNslbDp::GetInstance().GetInitNetCoFlag() == true) {
5104 : /* NSLB 填充 表 */
5105 0 : u32 srcLocalRankId = userRank_;
5106 0 : u32 rootRank = (opParam.root == INVALID_VALUE_RANKID) ? 0 : opParam.root;
5107 0 : AlgType nslbAlgType = algOperator->GetAlgType();
5108 0 : AlgTypeLevel1 algValue = nslbAlgType.algoLevel1;
5109 0 : uint8_t nslbAlg = hcclNslbDp::GetInstance().GetNslbLevel1AlgType(algValue);
5110 :
5111 0 : if (algName == "RunAlltoAllVFullMesh" || algName == "RunAlltoAllDirectFullmesh") {
5112 0 : nslbAlg = NSLBDP_PAIRWISE;
5113 0 : if (deviceType_ == DevType::DEV_TYPE_910_93) {
5114 0 : nslbAlg = NSLB_ALGO_TYPE_FULLMESH;
5115 : }
5116 : }
5117 :
5118 0 : std::string nslb_identifier = identifier_;
5119 0 : HCCL_INFO("NSLBDP-SWK NslbDp_CollectOperTable nslb_identifier[%s] .", nslb_identifier.c_str());
5120 0 : u32 rankSize = userRankSize_;
5121 0 : u64 count = opParam.All2AllDataDes.sendCount * SIZE_TABLE[opParam.All2AllDataDes.sendType];
5122 : // 填充表2
5123 0 : hcclNslbDp::GetInstance().GenerateOpAndAdjTable(
5124 : opType, rootRank, srcLocalRankId, nslbAlg, nslb_identifier, count, rankSize);
5125 0 : AdjInfo nslbAdjInfo = {};
5126 0 : CHK_RET(algOperator->GetAdjInfo(algName, opParam, algRes, nslbAdjInfo));
5127 0 : HCCL_INFO(
5128 : "[NSLBDP-WEN]-nslbAdjInfosize[%u]-algName[%s]-rankSize[%u]-commDesc[%s]..", nslbAdjInfo.dstRankNum,
5129 : algName.c_str(), userRankSize_, identifier_.c_str());
5130 : // 填充表3
5131 0 : hcclNslbDp::GetInstance().GetAlgAdjacencyTable(
5132 : opType, srcLocalRankId, rootRank, nslbAlg, nslb_identifier, nslbAdjInfo);
5133 : /*发送流程*/
5134 0 : hcclNslbDp::GetInstance().SendAlgorithmInfoTable();
5135 0 : }
5136 : // 算法执行
5137 5 : if (opParam.isNpuDirectRoce) {
5138 : // AIV直驱roce多机场景,需要生成RMAInfo并拷贝至Device
5139 0 : CHK_PTR_NULL(combinOparaMem_);
5140 0 : HcclCombinOpParam* combinOparaPtr = reinterpret_cast<HcclCombinOpParam*>(combinOparaMem_->ptr());
5141 0 : CHK_PTR_NULL(combinOparaPtr);
5142 0 : CHK_RET(algOperator->SetRmaInfo(combinOparaPtr->aiRMAInfo));
5143 : }
5144 5 : if (selectAivAlg) {
5145 5 : CHK_RET(HandleAclGraphFirstOpAivBuff(opParam.stream.ptr()));
5146 5 : if (aivClearEnable_) {
5147 : // 用于判断图模式是否清零
5148 2 : CHK_RET(algOperator->SetAivClearEnable(aivClearEnable_));
5149 2 : aivOffloadTag_ = 1;
5150 : }
5151 5 : GetAivTag(algDesc.aivTagNum, opParam.isCapture, opParam.aivTag);
5152 5 : HCCL_INFO(
5153 : "[HcclCommunicator][ExecOpAlltoAll] tag[%s] userRank[%u] cur aiv tag [%d].", identifier_.c_str(), userRank_,
5154 : opParam.aivTag);
5155 5 : opParam.aicpuUnfoldMode = false;
5156 5 : opParam.aicpuCacheEnable = 0;
5157 5 : CHK_RET(algOperator->SetNumBlocks(aivCoreLimit));
5158 : }
5159 :
5160 5 : auto algType = algOperator->GetAlgType();
5161 5 : CHK_RET(RegisterDfxInfo(opParam, algType, algRes.slaveStreams, selectAivAlg, tag));
5162 : // 头计数
5163 5 : CHK_RET(StarsCounter(dispatcher_, opParam.stream, HEAD, opParam.aicpuUnfoldMode, retryEnable_, selectAivAlg));
5164 : // 算法执行
5165 0 : auto isSupportAicpuAlg = [](const std::string& algName) {
5166 : static const std::set<std::string> aicpuAlgs
5167 : = {"RunAlltoAllVFullMesh", "RunAlltoAllDirectFullmesh",
5168 : "RunAlltoAllVTwoLevelPipeline", "RunAlltoAllFullMeshSymmetricMemory",
5169 0 : "RunAlltoAllVContinuousPipeline", "RunAlltoAllVPipelineFor91093"};
5170 0 : return aicpuAlgs.count(algName) > 0;
5171 : };
5172 5 : if (opParam.aicpuUnfoldMode && (isSupportAicpuAlg(algName) || aicpuUnfoldModeFor910B)) {
5173 0 : isInplaceStatus_ = 0;
5174 0 : inPlaceSupportRetryStatus_ = InplaceSupportRetryStatus::INPLACE_STATUS_END;
5175 : // algOperator->SupportRetryWithInplaceCheck 依赖 algOperator->SetRetryEnable 才能正确返回是否支持inplace
5176 :
5177 0 : inplaceSupportRetry_ = algOperator->SupportRetryWithInplaceCheck(
5178 0 : opType, opParam, algName, isInplaceStatus_, inPlaceSupportRetryStatus_);
5179 0 : HCCL_INFO(
5180 : "[HcclCommunicator][ExecOp] aicpu Unfold mode algType[%s], inplaceSupportRetry_[%d], opType[%d], "
5181 : "isInplaceStatus_[%d], inPlaceSupportRetryStatus_[%d].",
5182 : AlgTypeToStr(algType).c_str(), inplaceSupportRetry_, opType, isInplaceStatus_, inPlaceSupportRetryStatus_);
5183 0 : CHK_RET(OrchestrateAicpu(
5184 : opType, algName, opParam, algRes, newTag, algType, isCustom, false, needRecreateAlltoallComm));
5185 : } else {
5186 : // HOST展开aclgraph场景,capture从流
5187 5 : if (!selectAivAlg) {
5188 0 : CHK_RET(CaptureSlaveStreams(opParam.stream.ptr(), algRes.slaveStreams));
5189 : }
5190 5 : OpCounterInfo opCounter;
5191 5 : CHK_RET(GetOpCountInfo(opCounter));
5192 5 : CHK_RET(algOperator->SetOpCounter(opCounter));
5193 5 : CHK_RET(algOperator->Orchestrate(algName, opParam, algRes));
5194 : // for profiling, numBlocks upload
5195 5 : CHK_RET(algOperator->GetNumBlocks(numBlocks_));
5196 10 : if (implAlg_->GetAivModeConfig() && GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE
5197 10 : && !opParam.isCapture) {
5198 2 : CHK_RET(GetCacheMap(algOperator, opParam, algType, selectAivAlg, newTag));
5199 : }
5200 : }
5201 5 : lock.unlock();
5202 : // 尾计数
5203 5 : CHK_RET(StarsCounter(dispatcher_, opParam.stream, TAIL, opParam.aicpuUnfoldMode, retryEnable_, selectAivAlg));
5204 5 : CHK_RET(UnRegisterDfxInfo(opParam, algRes.slaveStreams));
5205 5 : if (selectAivAlg) {
5206 5 : CHK_RET(algOperator->SetAivClearEnable(false));
5207 5 : aivClearEnable_ = false;
5208 : }
5209 :
5210 5 : if (isInGraphCaptureZeroCopy) {
5211 0 : SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
5212 : }
5213 5 : return HCCL_SUCCESS;
5214 5 : }
5215 :
5216 76 : HcclResult HcclCommunicator::RecordOpPara(HcclCMDType opType, const OpParam& opParam)
5217 : {
5218 76 : u32 aivCoreLimit = (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB) ? numBlocks_ : 0;
5219 74 : u8 deterministic = implAlg_->GetDeterministicConfig();
5220 73 : switch (opType) {
5221 61 : case HcclCMDType::HCCL_CMD_ALLGATHER:
5222 : case HcclCMDType::HCCL_CMD_ALLREDUCE:
5223 : case HcclCMDType::HCCL_CMD_REDUCE_SCATTER:
5224 : case HcclCMDType::HCCL_CMD_BROADCAST:
5225 61 : CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(
5226 : opType, opParam.tag, opParam.DataDes.count, opParam.DataDes.dataType, opParam.reduceType, opParam.root,
5227 : cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(), identifier_.c_str(),
5228 : ranktableCrc_, deterministic, aivCoreLimit));
5229 64 : break;
5230 8 : case HcclCMDType::HCCL_CMD_SCATTER:
5231 : case HcclCMDType::HCCL_CMD_REDUCE:
5232 8 : CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(
5233 : opType, opParam.tag, opParam.DataDes.count, opParam.DataDes.dataType, opParam.reduceType, opParam.root,
5234 : cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(), identifier_.c_str(),
5235 : ranktableCrc_, deterministic));
5236 8 : break;
5237 0 : case HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V:
5238 : case HcclCMDType::HCCL_CMD_ALLGATHER_V:
5239 0 : CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(
5240 : opType, opParam.tag, opParam.VDataDes.counts, opParam.VDataDes.displs, userRankSize_,
5241 : opParam.VDataDes.dataType, opParam.reduceType, cclBufferManager_.GetInCCLbufferSize(),
5242 : cclBufferManager_.GetOutCCLbufferSize(), identifier_.c_str(), ranktableCrc_, deterministic,
5243 : aivCoreLimit));
5244 0 : break;
5245 0 : case HcclCMDType::HCCL_CMD_BATCH_SEND_RECV:
5246 0 : CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(
5247 : opType, opParam.tag, cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(),
5248 : identifier_.c_str(), ranktableCrc_));
5249 0 : break;
5250 0 : case HcclCMDType::HCCL_CMD_SEND:
5251 0 : CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(
5252 : opType, opParam.tag, opParam.DataDes.count, opParam.DataDes.dataType, opParam.dstRank, opParam.srTag,
5253 : opParam.localGroupRank, cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(),
5254 : identifier_.c_str(), ranktableCrc_));
5255 0 : break;
5256 0 : case HcclCMDType::HCCL_CMD_RECEIVE:
5257 0 : CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(
5258 : opType, opParam.tag, opParam.DataDes.count, opParam.DataDes.dataType, opParam.srcRank, opParam.srTag,
5259 : opParam.localGroupRank, cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(),
5260 : identifier_.c_str(), ranktableCrc_));
5261 0 : break;
5262 0 : case HcclCMDType::HCCL_CMD_ALLTOALL:
5263 0 : CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(
5264 : opType, opParam.tag, opParam.All2AllDataDes.sendCount, opParam.All2AllDataDes.sendType,
5265 : opParam.reduceType, opParam.root, cclBufferManager_.GetInCCLbufferSize(),
5266 : cclBufferManager_.GetOutCCLbufferSize(), identifier_.c_str(), ranktableCrc_, aivCoreLimit));
5267 0 : break;
5268 4 : case HcclCMDType::HCCL_CMD_ALLTOALLV:
5269 : case HcclCMDType::HCCL_CMD_ALLTOALLVC:
5270 4 : CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(
5271 : opType, opParam.tag, 0, HCCL_DATA_TYPE_RESERVED, opParam.reduceType, opParam.root,
5272 : cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(), identifier_.c_str(),
5273 : ranktableCrc_, aivCoreLimit));
5274 4 : break;
5275 0 : default:
5276 0 : break;
5277 : }
5278 76 : return HCCL_SUCCESS;
5279 : }
5280 5 : HcclResult HcclCommunicator::HandleAclGraphFirstOpAivBuff(rtStream_t mainStream)
5281 : {
5282 5 : aclmdlRI rtModel = nullptr;
5283 5 : bool isCapture = false;
5284 5 : u64 modelId = 0;
5285 5 : CHK_RET(GetStreamCaptureInfo(mainStream, rtModel, isCapture));
5286 5 : if (isCapture) {
5287 3 : CHK_PTR_NULL(rtModel);
5288 : // 获取不到modelId会报错
5289 3 : CHK_RET(GetModelId(rtModel, modelId));
5290 3 : if (captureModelIds_.find(modelId) == captureModelIds_.end()) {
5291 2 : CHK_RET(AclgraphCallback::GetInstance().RegisterModelId(this, rtModel, modelId));
5292 : // aclgraph场景,首算子清理AIV buff
5293 2 : aivClearEnable_ = true;
5294 2 : captureModelIds_.insert(modelId);
5295 2 : HCCL_INFO("[HcclCommunicator][%s] modelId[%llu] is inserted to captureModelIds_", __func__, modelId);
5296 : }
5297 : }
5298 5 : return HCCL_SUCCESS;
5299 : }
5300 :
5301 1 : void HcclCommunicator::EraseCaptureModelId(u64 modelId)
5302 : {
5303 1 : auto it = captureModelIds_.find(modelId);
5304 1 : if (it != captureModelIds_.end()) {
5305 0 : captureModelIds_.erase(it);
5306 0 : HCCL_INFO("[HcclCommunicator][%s] modelId[%llu] is erased from captureModelIds_", __func__, modelId);
5307 : }
5308 2 : return;
5309 : }
5310 :
5311 81 : bool HcclCommunicator::StreamIsCapture(rtStream_t mainStream) const
5312 : {
5313 81 : bool isCapture = false;
5314 81 : aclmdlRI rtModel = nullptr;
5315 81 : CHK_RET(GetStreamCaptureInfo(mainStream, rtModel, isCapture));
5316 93 : return isCapture;
5317 : }
5318 :
5319 19 : HcclResult HcclCommunicator::CaptureSlaveStreams(rtStream_t mainStream, vector<Stream>& slaveStreams)
5320 : {
5321 0 : if ((deviceType_ != DevType::DEV_TYPE_910_93) && (deviceType_ != DevType::DEV_TYPE_310P3)
5322 19 : && (deviceType_ != DevType::DEV_TYPE_910B || GetExternalInputHcclEnableFfts())) {
5323 0 : HCCL_INFO(
5324 : "[HcclCommunicator][%s]Only 310P3 or A2 or A3 device in host expand mode need to capture slave streams.",
5325 : __func__);
5326 0 : return HCCL_SUCCESS;
5327 : }
5328 19 : aclmdlRI rtModel = nullptr;
5329 19 : bool isCapture = false;
5330 19 : u64 modelId = 0;
5331 19 : CHK_RET(GetStreamCaptureInfo(mainStream, rtModel, isCapture));
5332 19 : if (isCapture) {
5333 0 : CHK_PTR_NULL(rtModel);
5334 0 : CHK_RET(GetModelId(rtModel, modelId));
5335 0 : for (auto slaveStream : slaveStreams) {
5336 0 : CHK_RET(AddStreamToModel(slaveStream.ptr(), rtModel));
5337 0 : HCCL_DEBUG(
5338 : "[HcclCommunicator][%s]Add stream[%d] to model[%u] success.", __func__, slaveStream.id(), modelId);
5339 0 : }
5340 : }
5341 19 : return HCCL_SUCCESS;
5342 : }
5343 :
5344 0 : HcclResult HcclCommunicator::BuildOpLocalScratchMemResParam(
5345 : const AlgResourceResponse& algResource, const std::string& newTag, LocalResInfoV2* localResHostPtr)
5346 : {
5347 0 : if (algResource.scratchMem.size() > 0) {
5348 0 : hostMemVec_.resize(hostMemVec_.size() + 1);
5349 0 : CHK_RET(AllocAndClearHostMem(sizeof(HccltagLocalResV2), hostMemVec_.back()));
5350 0 : HccltagLocalResV2* tagLocalResHostPtr = static_cast<HccltagLocalResV2*>(hostMemVec_.back().get()->ptr());
5351 :
5352 0 : deviceMemVec_.resize(deviceMemVec_.size() + 1);
5353 0 : CHK_RET(AllocAndClearDeviceMem(sizeof(HccltagLocalResV2), deviceMemVec_.back()));
5354 0 : HccltagLocalResV2* tagLocalResDevicePtr = static_cast<HccltagLocalResV2*>(deviceMemVec_.back().get()->ptr());
5355 :
5356 : // 初始化HcclRankRelationResV2中的tagRes链表
5357 0 : ListCommonInit(&tagLocalResDevicePtr->nextTagRes, &tagLocalResHostPtr->nextTagRes);
5358 : // 刷新host空间内容
5359 0 : CHK_SAFETY_FUNC_RET(
5360 : memcpy_s(tagLocalResHostPtr->tag, sizeof(tagLocalResHostPtr->tag), newTag.c_str(), newTag.length() + 1));
5361 0 : tagLocalResHostPtr->ScratchmemSize = algResource.scratchMem.size();
5362 0 : tagLocalResHostPtr->Scratchmem = reinterpret_cast<u64>(algResource.scratchMem.ptr());
5363 :
5364 : // 3、将节点插入链表头
5365 0 : ListCommonAddHead(
5366 : &tagLocalResDevicePtr->nextTagRes, &tagLocalResHostPtr->nextTagRes, &localResHostPtr->nextTagRes,
5367 0 : &opResDeviceParaPtr_->localRes.nextTagRes);
5368 0 : HCCL_RUN_INFO(
5369 : "[HcclCommunicator][BuildOpLocalScratchMemResParam] LocalResHostPtr head addr[%p], nextHost[%p], "
5370 : "preHost[%p], tag LocalResHostPtr head addr[%p], nextHost[%p],"
5371 : "preHost[%p], tag[%s]",
5372 : &localResHostPtr->nextTagRes, localResHostPtr->nextTagRes.nextHost, localResHostPtr->nextTagRes.preHost,
5373 : &tagLocalResHostPtr->nextTagRes, tagLocalResHostPtr->nextTagRes.nextHost,
5374 : tagLocalResHostPtr->nextTagRes.preHost, tagLocalResHostPtr->tag);
5375 : }
5376 0 : return HCCL_SUCCESS;
5377 : }
5378 :
5379 0 : HcclResult HcclCommunicator::CheckSetRetryStateToWaitResume()
5380 : {
5381 0 : if (retryEnable_ && opRetryManager_ != nullptr) {
5382 0 : HcclResult ret = opRetryManager_->SetRetryStateToWaitResume(identifier_, commConnections_.isRoot);
5383 0 : CHK_PRT_RET(
5384 : ret != HCCL_SUCCESS, HCCL_ERROR("[NsRecovery]set opretry state to wait resume timeout."), HCCL_E_INTERNAL);
5385 : }
5386 0 : return HCCL_SUCCESS;
5387 : }
5388 :
5389 0 : HcclResult HcclCommunicator::BuildOpLocalResParam(const AlgResourceResponse& algResource, const std::string& newTag)
5390 : {
5391 0 : LocalResInfoV2* localResHostPtr = &opResPara_.localRes;
5392 0 : ListCommonInit(&opResDeviceParaPtr_->localRes.nextTagRes, &opResPara_.localRes.nextTagRes);
5393 0 : if (algResource.slaveDevStreams.size() > LOCAL_STREAM_MAX_NUM) {
5394 0 : HCCL_ERROR("[HcclCommunicator][BuildOpLocalResParam]Fail to assign stream for tag[%s]", newTag.c_str());
5395 0 : return HCCL_E_PARA;
5396 : }
5397 0 : auto signalM2SNum = algResource.notifiesDevMain.size();
5398 0 : auto signalS2MNum = algResource.notifiesDevAux.size();
5399 0 : auto signalNum = signalM2SNum + signalS2MNum;
5400 0 : if (signalNum > LOCAL_NOTIFY_MAX_NUM) {
5401 0 : HCCL_ERROR("[HcclCommunicator][BuildOpLocalResParam]Fail to assign local notify for tag[%s]", newTag.c_str());
5402 0 : return HCCL_E_PARA;
5403 : }
5404 :
5405 0 : localResHostPtr->streamNum = algResource.slaveDevStreams.size();
5406 0 : for (u32 i = 0; i < algResource.slaveDevStreams.size(); i++) {
5407 0 : localResHostPtr->streamParam[i].streamInfo.streamIds = algResource.slaveDevStreams[i].id();
5408 0 : localResHostPtr->streamParam[i].streamInfo.sqIds = algResource.slaveDevStreams[i].sqId();
5409 0 : localResHostPtr->streamParam[i].streamInfo.cqIds = algResource.slaveDevStreams[i].cqId();
5410 0 : localResHostPtr->streamParam[i].streamInfo.logicCqids = algResource.slaveDevStreams[i].logicCqId();
5411 0 : CHK_RET(AllocAndGetStreamContextBuff(
5412 : algResource.slaveDevStreams[i].id(), localResHostPtr->streamParam[i].sqCqContextAddr,
5413 : localResHostPtr->streamParam[i].sqCqContextSize));
5414 : }
5415 :
5416 0 : localResHostPtr->signalNum = signalNum;
5417 :
5418 0 : for (u32 i = 0; i < signalM2SNum; i++) {
5419 0 : algResource.notifiesDevMain[i]->GetNotifyData(localResHostPtr->localSignals[i << 1]);
5420 0 : algResource.notifiesDevAux[i]->GetNotifyData(localResHostPtr->localSignals[(i << 1) + 1]);
5421 : }
5422 0 : HcclResult ret = HCCL_SUCCESS;
5423 0 : ret = CreateAndGetAiCpuNotify(
5424 0 : localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_0)],
5425 0 : localResHostPtr->aicpuOpNotify[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_0)]);
5426 0 : CHK_PRT_RET(
5427 : ret != HCCL_SUCCESS,
5428 : HCCL_ERROR(
5429 : "[HcclCommunicator][BuildOpLocalResParam]get aicpu notify 0 error,"
5430 : "errNo[0x%016llx]",
5431 : HCCL_ERROR_CODE(ret)),
5432 : ret);
5433 0 : ret = CreateAndGetAiCpuNotify(
5434 0 : localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_1)],
5435 0 : localResHostPtr->aicpuOpNotify[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_1)]);
5436 0 : CHK_PRT_RET(
5437 : ret != HCCL_SUCCESS,
5438 : HCCL_ERROR(
5439 : "[HcclCommunicator][BuildOpLocalResParam]get aicpu notify 1 error,errNo[0x%016llx]", HCCL_ERROR_CODE(ret)),
5440 : ret);
5441 :
5442 0 : if (opMainStream_.ptr() == nullptr) {
5443 0 : opMainStream_ = Stream(StreamType::STREAM_TYPE_DEVICE);
5444 : }
5445 0 : localResHostPtr->mainStreamParam.streamInfo.streamIds = opMainStream_.id();
5446 0 : localResHostPtr->mainStreamParam.streamInfo.sqIds = opMainStream_.sqId();
5447 0 : localResHostPtr->mainStreamParam.streamInfo.cqIds = opMainStream_.cqId();
5448 0 : localResHostPtr->mainStreamParam.streamInfo.logicCqids = opMainStream_.logicCqId();
5449 0 : CHK_RET(AllocAndGetStreamContextBuff(
5450 : opMainStream_.id(), localResHostPtr->mainStreamParam.sqCqContextAddr,
5451 : localResHostPtr->mainStreamParam.sqCqContextSize));
5452 :
5453 : // 按序下发的aicpu控制流
5454 0 : if (aicpuOrderStream_.ptr() == nullptr) {
5455 0 : aicpuOrderStream_ = Stream(StreamType::STREAM_TYPE_DEVICE);
5456 : }
5457 0 : opResPara_.aicpuOrderStreamParam.streamInfo.streamIds = aicpuOrderStream_.id();
5458 0 : opResPara_.aicpuOrderStreamParam.streamInfo.sqIds = aicpuOrderStream_.sqId();
5459 0 : opResPara_.aicpuOrderStreamParam.streamInfo.cqIds = aicpuOrderStream_.cqId();
5460 0 : opResPara_.aicpuOrderStreamParam.streamInfo.logicCqids = aicpuOrderStream_.logicCqId();
5461 0 : CHK_RET(AllocAndGetStreamContextBuff(
5462 : opResPara_.aicpuOrderStreamParam.streamInfo.streamIds, opResPara_.aicpuOrderStreamParam.sqCqContextAddr,
5463 : opResPara_.aicpuOrderStreamParam.sqCqContextSize));
5464 :
5465 : #ifndef CCL_KERNEL_AICPU
5466 0 : for (u32 i = 0; i < AICPU_LOCAL_EVENT_SIZE; ++i) {
5467 0 : aclError ret = aclrtCreateEventExWithFlag(&localAicpuOpEvent_[i], ACL_EVENT_SYNC);
5468 0 : CHK_PRT_RET(
5469 : ret != ACL_SUCCESS,
5470 : HCCL_ERROR(
5471 : "[%s]aclrtCreateEventExWithFlag failed, ret[%d] event[%p].", __func__, ret, localAicpuOpEvent_[i]),
5472 : HCCL_E_RUNTIME);
5473 : }
5474 : #endif
5475 :
5476 0 : CHK_RET(BuildOpLocalScratchMemResParam(algResource, newTag, localResHostPtr));
5477 0 : return HCCL_SUCCESS;
5478 : }
5479 :
5480 0 : HcclResult HcclCommunicator::AllocAndGetStreamContextBuff(u32 streamId, u64& addr, u64& size)
5481 : {
5482 0 : if (streamIdToStreamContext_.find(streamId) == streamIdToStreamContext_.end()) {
5483 0 : DeviceMem streamContext;
5484 0 : CHK_RET(CreateWorkSpace(sizeof(SqCqeContext), streamContext));
5485 0 : streamIdToStreamContext_.insert({streamId, std::move(streamContext)});
5486 0 : }
5487 0 : addr = reinterpret_cast<u64>(streamIdToStreamContext_.at(streamId).ptr());
5488 0 : size = streamIdToStreamContext_.at(streamId).size();
5489 0 : HCCL_INFO("%s success, streamId:%u, addr:0x%llx, size:%llu", __func__, streamId, addr, size);
5490 0 : return HCCL_SUCCESS;
5491 : }
5492 :
5493 0 : u32 HcclCommunicator::UpdateOpIndex(const OpParam& opParam)
5494 : {
5495 0 : u32 opIndex = 0;
5496 0 : u32 commIndex = 0;
5497 : // 用于重执行和taskException打印的算子计数,bsr/sendrecv/其他算子分别计数
5498 0 : if (opParam.opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV) {
5499 0 : constexpr s32 batSendRecvIndex = -1; // batchSendRecv使用 key = -1
5500 0 : commIndex = batSendRecvIndex;
5501 0 : } else if (opParam.opType == HcclCMDType::HCCL_CMD_SEND) {
5502 0 : commIndex = opParam.dstRank;
5503 0 : } else if (opParam.opType == HcclCMDType::HCCL_CMD_RECEIVE) {
5504 0 : commIndex = opParam.srcRank;
5505 : } else {
5506 0 : commIndex = userRank_;
5507 : }
5508 :
5509 0 : auto it = opIndexMap_.find(commIndex);
5510 0 : if (it != opIndexMap_.end()) {
5511 0 : opIndex = ++(it->second);
5512 : } else {
5513 0 : opIndexMap_.insert({commIndex, 1});
5514 0 : opIndex = 1;
5515 : }
5516 :
5517 0 : HCCL_DEBUG(
5518 : "%s tag:%s opType:%u commIndex:%u opIndex:%u", __func__, opParam.tag.c_str(), opParam.opType, commIndex,
5519 : opIndex);
5520 0 : return opIndex;
5521 : }
5522 :
5523 0 : HcclResult HcclCommunicator::BuildAicpuCustomParam()
5524 : {
5525 0 : if (aicpuCustomDev_.ptr() == nullptr) {
5526 0 : CHK_RET(CreateWorkSpace(sizeof(AicpuCustomParam), aicpuCustomDev_));
5527 : }
5528 :
5529 0 : opResPara_.aicpuCustomParamAddr = reinterpret_cast<u64>(aicpuCustomDev_.ptr());
5530 0 : opResPara_.aicpuCustomParamSize = aicpuCustomDev_.size();
5531 0 : HCCL_INFO(
5532 : "%s success, aicpuCustomParamAddr:0x%llx, aicpuCustomParamSize:%llu", __func__, opResPara_.aicpuCustomParamAddr,
5533 : opResPara_.aicpuCustomParamSize);
5534 0 : return HCCL_SUCCESS;
5535 : }
5536 :
5537 0 : HcclResult HcclCommunicator::BuildAicpuOrderLaunchNotify()
5538 : {
5539 0 : if (aicpuOrderNotifyAddr_.ptr() == nullptr) {
5540 0 : CHK_RET(CreateWorkSpace(sizeof(HcclSignalInfo) * AICPU_ORDER_NOTIFY_MAX_NUM, aicpuOrderNotifyAddr_));
5541 : }
5542 :
5543 0 : opResPara_.aicpuOrderNotifyAddr = reinterpret_cast<u64>(aicpuOrderNotifyAddr_.ptr());
5544 0 : opResPara_.aicpuOrderNotifySize = aicpuOrderNotifyAddr_.size();
5545 0 : HCCL_INFO(
5546 : "%s success, aicpuOrderNotifyAddr:0x%llx, aicpuOrderNotifySize:%llu", __func__, opResPara_.aicpuOrderNotifyAddr,
5547 : opResPara_.aicpuOrderNotifySize);
5548 0 : return HCCL_SUCCESS;
5549 : }
5550 :
5551 0 : HcclResult HcclCommunicator::BuildAiRmaInfoParam(
5552 : const std::string& newTag, [[maybe_unused]] const std::string& algName, [[maybe_unused]] const HcclCMDType opType)
5553 : {
5554 0 : HCCL_DEBUG("[HcclCommunicator][%s] Start prepare.", __func__);
5555 0 : CHK_PTR_NULL(aiRMAInfoMem_);
5556 0 : HcclAiRMAInfo* aiRMAInfoPtr = reinterpret_cast<HcclAiRMAInfo*>(aiRMAInfoMem_->ptr());
5557 0 : CHK_PTR_NULL(aiRMAInfoPtr);
5558 0 : aiRMAInfoPtr->curRankId = userRank_;
5559 0 : aiRMAInfoPtr->rankNum = userRankSize_;
5560 0 : u32 localRankSize = meshAggregationRankSize_;
5561 0 : LevelNSubCommTransport& commTransport = resMap_[newTag].opTransportResponse[COMM_LEVEL0];
5562 0 : CHK_PRT_RET(
5563 : commTransport.size() <= 0,
5564 : HCCL_ERROR(
5565 : "[%s] no LevelComm resource, please create comm first. "
5566 : "tag[%s], curRankId[%u] rankNum[%u]",
5567 : __func__, newTag.c_str(), aiRMAInfoPtr->curRankId, aiRMAInfoPtr->rankNum),
5568 : HCCL_E_INTERNAL);
5569 0 : std::vector<LINK>& links = commTransport[0].links;
5570 0 : CHK_PRT_RET(
5571 : links.size() <= 0,
5572 : HCCL_ERROR(
5573 : "[%s] no transport resource, please create links first. "
5574 : "tag[%s], curRankId[%u] rankNum[%u]",
5575 : __func__, newTag.c_str(), aiRMAInfoPtr->curRankId, aiRMAInfoPtr->rankNum),
5576 : HCCL_E_INTERNAL);
5577 :
5578 0 : LevelNSubCommTransport& tmpCommTransport = resMap_[newTag].opTransportResponse[COMM_MESH_L1];
5579 0 : CHK_PRT_RET(
5580 : tmpCommTransport.size() <= 0,
5581 : HCCL_ERROR(
5582 : "[%s] no LevelComm resource, please create comm first. "
5583 : "tag[%s], curRankId[%u] rankNum[%u]",
5584 : __func__, newTag.c_str(), aiRMAInfoPtr->curRankId, aiRMAInfoPtr->rankNum),
5585 : HCCL_E_INTERNAL);
5586 0 : std::vector<LINK>& tmpLinks = tmpCommTransport[0].links;
5587 0 : CHK_PRT_RET(
5588 : tmpLinks.size() <= 0,
5589 : HCCL_ERROR(
5590 : "[%s] no transport resource, please create links first. "
5591 : "tag[%s], curRankId[%u] rankNum[%u]",
5592 : __func__, newTag.c_str(), aiRMAInfoPtr->curRankId, aiRMAInfoPtr->rankNum),
5593 : HCCL_E_INTERNAL);
5594 :
5595 0 : CHK_RET(GetAivQPInfoV2(tmpLinks, newTag));
5596 0 : u32 tmpQueueSize = aiRMAInfoPtr->rankNum * aiRMAInfoPtr->qpNum;
5597 0 : u32 tmpMemSize = aiRMAInfoPtr->rankNum;
5598 0 : u32 tmpMemDetailSize = aiRMAInfoPtr->rankNum * AiMemMaxNum;
5599 :
5600 0 : CHK_RET(AllocAndClearHostMem(sizeof(HcclAiRMAWQ) * tmpQueueSize, aiSqMem_));
5601 0 : CHK_RET(AllocAndClearHostMem(sizeof(HcclAiRMACQ) * tmpQueueSize, aiScqMem_));
5602 0 : CHK_RET(AllocAndClearHostMem(sizeof(HcclAiRMAWQ) * tmpQueueSize, aiRqMem_));
5603 0 : CHK_RET(AllocAndClearHostMem(sizeof(HcclAiRMACQ) * tmpQueueSize, aiRcqMem_));
5604 0 : CHK_RET(AllocAndClearHostMem(sizeof(HcclAiRMAMemInfo) * tmpMemSize, aiMemMem_));
5605 0 : HcclAiRMAMemInfo* aiMemHost = reinterpret_cast<HcclAiRMAMemInfo*>(aiMemMem_->ptr());
5606 :
5607 0 : CHK_RET(AllocAndClearHostMem(sizeof(MemDetails) * tmpMemDetailSize, aiMemDetailsMem_));
5608 0 : MemDetails* aiMemDetailsHost = reinterpret_cast<MemDetails*>(aiMemDetailsMem_->ptr());
5609 :
5610 0 : CHK_RET(DeviceMem::alloc(aiMemDetailsDev_, aiMemDetailsMem_->size()));
5611 0 : u64 memBase = reinterpret_cast<uint64_t>(aiMemDetailsDev_.ptr());
5612 :
5613 0 : for (u32 i = 0; i < aiRMAInfoPtr->rankNum; i++) {
5614 0 : MemDetails& remoteIn = aiMemDetailsHost[i * AiMemMaxNum + GetAiMemTypeVal(HcclAiRMAMemType::REMOTE_INPUT)];
5615 0 : MemDetails& remoteOut = aiMemDetailsHost[i * AiMemMaxNum + GetAiMemTypeVal(HcclAiRMAMemType::REMOTE_OUTPUT)];
5616 0 : MemDetails& localIn = aiMemDetailsHost[i * AiMemMaxNum + GetAiMemTypeVal(HcclAiRMAMemType::LOCAL_INPUT)];
5617 0 : MemDetails& localOut = aiMemDetailsHost[i * AiMemMaxNum + GetAiMemTypeVal(HcclAiRMAMemType::LOCAL_OUTPUT)];
5618 0 : if (i != aiRMAInfoPtr->curRankId
5619 0 : && ((i % localRankSize) == (aiRMAInfoPtr->curRankId % localRankSize)
5620 0 : || (i / localRankSize) == (aiRMAInfoPtr->curRankId / localRankSize))) {
5621 0 : auto transport = links[i % localRankSize]; // localranksize个
5622 0 : if ((i % localRankSize) == (aiRMAInfoPtr->curRankId % localRankSize)) {
5623 0 : transport = tmpLinks[i / localRankSize]; // servernum个
5624 : }
5625 : // link rank info
5626 0 : CHK_RET(GetTransportRemoteMem(transport, UserMemType::INPUT_MEM, remoteIn));
5627 0 : CHK_RET(GetTransportRemoteMem(transport, UserMemType::OUTPUT_MEM, remoteOut));
5628 0 : CHK_RET(GetTransportLocalMem(transport, UserMemType::INPUT_MEM, localIn));
5629 0 : CHK_RET(GetTransportLocalMem(transport, UserMemType::OUTPUT_MEM, localOut));
5630 :
5631 0 : if (transport->GetTransportType() == TransportType::TRANS_TYPE_IBV_EXP) {
5632 0 : CHK_RET(GenIbvAiRMAInfo(i, transport, newTag, aiRMAInfoPtr));
5633 : }
5634 0 : } else if (i == aiRMAInfoPtr->curRankId) {
5635 0 : void* commInPtr = nullptr;
5636 0 : void* commOutPtr = nullptr;
5637 : u64 commInSize;
5638 : u64 commOutSize;
5639 0 : CHK_RET(cclBufferManager_.GetInCCLbuffer(commInPtr, commInSize));
5640 0 : CHK_RET(cclBufferManager_.GetOutCCLbuffer(commOutPtr, commOutSize));
5641 0 : localIn.addr = reinterpret_cast<uint64_t>(commInPtr);
5642 0 : localIn.size = commInSize;
5643 0 : localOut.addr = reinterpret_cast<uint64_t>(commOutPtr);
5644 0 : localOut.size = commOutSize;
5645 : }
5646 :
5647 0 : aiMemHost[i].memMaxNum = AiMemMaxNum;
5648 0 : aiMemHost[i].sizeOfMemDetails = static_cast<u32>(sizeof(MemDetails));
5649 0 : aiMemHost[i].memDetailPtr = memBase + i * AiMemMaxNum * aiMemHost[i].sizeOfMemDetails;
5650 :
5651 0 : HCCL_DEBUG(
5652 : "[%s] tag[%s] curRankId[%u] dstRankId[%u] rankNum[%u] qpNum[%u] memMaxNum[%u] sizeOfMemDetails[%u] "
5653 : "memDetailPtr[%p] remoteInAddr[%p] remoteInSize[%llu] remoteOutAddr[%p] "
5654 : "remoteOutSize[%llu] localInAddr[%p] localInSize[%llu] "
5655 : "localOutAddr[%p] localOutSize[%llu] ",
5656 : __func__, newTag.c_str(), aiRMAInfoPtr->curRankId, i, aiRMAInfoPtr->rankNum, aiRMAInfoPtr->qpNum,
5657 : aiMemHost[i].memMaxNum, aiMemHost[i].sizeOfMemDetails, aiMemHost[i].memDetailPtr, remoteIn.addr,
5658 : remoteIn.size, remoteOut.addr, remoteOut.size, localIn.addr, localIn.size, localOut.addr, localOut.size);
5659 : }
5660 0 : return HCCL_SUCCESS;
5661 : }
5662 :
5663 : template <typename T>
5664 1 : HcclResult HcclCommunicator::CopyVectorToDeviceMem(const u64 len, DeviceMem& dstDeviceMem, const std::vector<T>& srcVec)
5665 : {
5666 1 : CHK_PRT_RET(
5667 : len == 0, HCCL_INFO("[HcclCommunicator][CopyVectorToDeviceMem] space size is zero. not need to malloc memory"),
5668 : HCCL_SUCCESS);
5669 :
5670 : CHK_PRT_RET(
5671 : (len > ULONG_MAX),
5672 : HCCL_ERROR("[HcclCommunicator][CopyVectorToDeviceMem] space size is greater than %llu", ULONG_MAX),
5673 : HCCL_E_PARA);
5674 :
5675 0 : CHK_RET(CreateWorkSpace(len, dstDeviceMem));
5676 0 : std::shared_ptr<HostMem> srcHostMem;
5677 0 : CHK_RET(AllocAndClearHostMem(len, srcHostMem));
5678 0 : std::copy(srcVec.begin(), srcVec.end(), static_cast<T*>(srcHostMem.get()->ptr()));
5679 0 : CHK_RET(hrtMemSyncCopy(
5680 : dstDeviceMem.ptr(), len, srcHostMem.get()->ptr(), len, HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
5681 0 : return HCCL_SUCCESS;
5682 0 : }
5683 :
5684 0 : HcclResult HcclCommunicator::BuildOpTopoResTlvParam(
5685 : [[maybe_unused]] const std::string& algName, const std::vector<std::vector<std::vector<u32>>>& inputVectorInfo,
5686 : DeviceMem& dstTlvDeviceMem, u64& tlvLen)
5687 : {
5688 0 : vector<u32> tlv;
5689 : CommonTlv commonTlv;
5690 0 : HCCL_DEBUG(
5691 : "[HcclCommunicator][BuildOpTopoResTlvParam] input vector size[%lu], group[%s].", inputVectorInfo.size(),
5692 : identifier_.c_str());
5693 0 : for (u16 level0Idx = 0; level0Idx < inputVectorInfo.size(); level0Idx++) {
5694 0 : for (u16 level1Idx = 0; level1Idx < inputVectorInfo[level0Idx].size(); level1Idx++) {
5695 0 : commonTlv.type = ((level0Idx << TOP_COMM_LEVEL0_SHIFT) | level1Idx);
5696 0 : commonTlv.length = (sizeof(LENGTH_TYPE) + sizeof(TAG_TYPE))
5697 0 : + inputVectorInfo[level0Idx][level1Idx].size() * sizeof(RANK_TYPE);
5698 0 : tlv.push_back(commonTlv.type);
5699 0 : tlv.push_back(commonTlv.length);
5700 0 : tlv.insert(
5701 0 : tlv.end(), inputVectorInfo[level0Idx][level1Idx].begin(), inputVectorInfo[level0Idx][level1Idx].end());
5702 : }
5703 : }
5704 0 : for (u64 idx = 0; idx < tlv.size(); idx++) {
5705 0 : HCCL_DEBUG("[HcclCommunicator][BuildOpTopoResTlvParam] idx[%lu] tlv[%lu].", idx, tlv[idx]);
5706 : }
5707 0 : tlvLen = tlv.size() * sizeof(u32);
5708 0 : CHK_RET(CopyVectorToDeviceMem(tlvLen, dstTlvDeviceMem, tlv));
5709 0 : return HCCL_SUCCESS;
5710 0 : }
5711 :
5712 0 : HcclResult HcclCommunicator::BuildOpTopoResVectorTlvParam(
5713 : [[maybe_unused]] const std::string& algName,
5714 : const std::vector<std::vector<std::vector<std::vector<u32>>>>& inputVectorInfo, DeviceMem& dstTlvDeviceMem,
5715 : u64& tlvLen)
5716 : {
5717 0 : vector<u32> tlv;
5718 : CommonTlv commonTlv;
5719 0 : HCCL_DEBUG(
5720 : "[HcclCommunicator][BuildOpTopoResVectorTlvParam] input vector size[%lu], group[%s]", inputVectorInfo.size(),
5721 : identifier_.c_str());
5722 0 : for (u16 level0Idx = 0; level0Idx < inputVectorInfo.size(); level0Idx++) {
5723 0 : for (u16 level1Idx = 0; level1Idx < inputVectorInfo[level0Idx].size(); level1Idx++) {
5724 0 : for (u16 level2Idx = 0; level2Idx < inputVectorInfo[level0Idx][level1Idx].size(); level2Idx++) {
5725 0 : commonTlv.type = (((level0Idx << TOP_HIERARCHICAL_COMM_LEVEL0_SHIFT) | level1Idx)
5726 0 : << TOP_HIERARCHICAL_COMM_LEVEL1_SHIFT)
5727 0 : | level2Idx;
5728 0 : commonTlv.length = (sizeof(LENGTH_TYPE) + sizeof(TAG_TYPE))
5729 0 : + inputVectorInfo[level0Idx][level1Idx][level2Idx].size() * sizeof(RANK_TYPE);
5730 0 : tlv.push_back(commonTlv.type);
5731 0 : tlv.push_back(commonTlv.length);
5732 0 : tlv.insert(
5733 0 : tlv.end(), inputVectorInfo[level0Idx][level1Idx][level2Idx].begin(),
5734 0 : inputVectorInfo[level0Idx][level1Idx][level2Idx].end());
5735 : }
5736 : }
5737 : }
5738 0 : for (u64 idx = 0; idx < tlv.size(); idx++) {
5739 0 : HCCL_DEBUG("[HcclCommunicator][BuildOpTopoResVectorTlvParam] idx[%lu] tlv[%lu]", idx, tlv[idx]);
5740 : }
5741 0 : tlvLen = tlv.size() * sizeof(u32);
5742 0 : CHK_RET(CopyVectorToDeviceMem(tlvLen, dstTlvDeviceMem, tlv));
5743 0 : return HCCL_SUCCESS;
5744 0 : }
5745 :
5746 0 : HcclResult HcclCommunicator::BuildPairLinkCounter([[maybe_unused]] const std::string& algName)
5747 : {
5748 0 : constexpr u32 KEY_VALUE_TO_VECTOR_MODULUS = 2;
5749 0 : if (pairLinkCounterDevice_.ptr() == nullptr) {
5750 0 : u64 pairLinkCounterSize = pairLinkCounter_.size();
5751 0 : HCCL_DEBUG(
5752 : "[HcclCommunicator][BuildPairLinkCounter] pairLinkCounter size[%lu], group[%s]", pairLinkCounterSize,
5753 : identifier_.c_str());
5754 0 : std::vector<u32> pairLinkCounterVec(pairLinkCounterSize * KEY_VALUE_TO_VECTOR_MODULUS);
5755 0 : u64 index = 0;
5756 0 : for (auto& kt : pairLinkCounter_) {
5757 0 : pairLinkCounterVec[index] = kt.first;
5758 0 : pairLinkCounterVec[index + 1] = kt.second;
5759 0 : index += KEY_VALUE_TO_VECTOR_MODULUS; // 每次根据
5760 : }
5761 0 : u64 len = pairLinkCounterSize * sizeof(u32) * KEY_VALUE_TO_VECTOR_MODULUS; // key-value,都为u32
5762 0 : CHK_RET(CopyVectorToDeviceMem(len, pairLinkCounterDevice_, pairLinkCounterVec));
5763 0 : opResPara_.topoInfo.pairLinkCounter = reinterpret_cast<u64>(pairLinkCounterDevice_.ptr());
5764 0 : opResPara_.topoInfo.pairLinkCounterNum = pairLinkCounterSize * KEY_VALUE_TO_VECTOR_MODULUS;
5765 0 : }
5766 0 : return HCCL_SUCCESS;
5767 : }
5768 :
5769 0 : HcclResult HcclCommunicator::BuildIsUsedRdmaRank([[maybe_unused]] const std::string& algName)
5770 : {
5771 0 : constexpr u32 KEY_VALUE_TO_VECTOR_MODULUS = 2;
5772 0 : if (isUsedRdmaRankPairDevice_.ptr() == nullptr) {
5773 0 : std::unordered_map<u32, bool> isUsedRdmaMap;
5774 0 : CHK_RET(implAlg_->GetIsUsedRdmaMap(isUsedRdmaMap));
5775 0 : u64 isUsedRdmaMapSize = isUsedRdmaMap.size();
5776 0 : HCCL_DEBUG(
5777 : "[HcclCommunicator][BuildIsUsedRdmaRank] is used Rdma rank size[%lu], group[%s]", isUsedRdmaMapSize,
5778 : identifier_.c_str());
5779 0 : std::vector<u32> isUsedRdmaPairVec(isUsedRdmaMapSize * KEY_VALUE_TO_VECTOR_MODULUS);
5780 0 : u64 index = 0;
5781 0 : for (auto& kt : isUsedRdmaMap) {
5782 0 : isUsedRdmaPairVec[index] = kt.first;
5783 0 : isUsedRdmaPairVec[index + 1] = static_cast<u32>(kt.second);
5784 0 : index += KEY_VALUE_TO_VECTOR_MODULUS;
5785 : }
5786 0 : u64 len = isUsedRdmaMapSize * sizeof(u32) * KEY_VALUE_TO_VECTOR_MODULUS; // key-value,都为u32
5787 0 : CHK_RET(CopyVectorToDeviceMem(len, isUsedRdmaRankPairDevice_, isUsedRdmaPairVec));
5788 0 : opResPara_.topoInfo.isUsedRdmaRankPair = reinterpret_cast<u64>(isUsedRdmaRankPairDevice_.ptr());
5789 0 : opResPara_.topoInfo.isUsedRdmaRankPairNum = isUsedRdmaMapSize * KEY_VALUE_TO_VECTOR_MODULUS;
5790 0 : }
5791 0 : return HCCL_SUCCESS;
5792 : }
5793 :
5794 0 : HcclResult HcclCommunicator::BuildNicList([[maybe_unused]] const std::string& algName)
5795 : {
5796 0 : if (nicListDevice_.ptr() == nullptr) {
5797 0 : u64 len = nicList_.size() * sizeof(u32);
5798 0 : HCCL_DEBUG(
5799 : "[HcclCommunicator][BuildNicList] niclist size[%lu], group[%s]", nicList_.size(), identifier_.c_str());
5800 0 : CHK_RET(CopyVectorToDeviceMem(len, nicListDevice_, nicList_));
5801 0 : opResPara_.topoInfo.nicList = reinterpret_cast<u64>(nicListDevice_.ptr());
5802 0 : opResPara_.topoInfo.nicNum = nicList_.size();
5803 : }
5804 0 : return HCCL_SUCCESS;
5805 : }
5806 :
5807 0 : HcclResult HcclCommunicator::BuildBridgeRank([[maybe_unused]] const std::string& algName)
5808 : {
5809 0 : if (bridgeRankDevice_.ptr() == nullptr) {
5810 0 : std::vector<bool> isBridgeVector;
5811 0 : CHK_RET(implAlg_->GetIsBridgeVector(isBridgeVector));
5812 0 : u64 len = isBridgeVector.size() * sizeof(bool);
5813 0 : HCCL_DEBUG(
5814 : "[HcclCommunicator][BuildBridgeRank] Bridge size[%lu], group[%s]", isBridgeVector.size(),
5815 : identifier_.c_str());
5816 0 : CHK_RET(CopyVectorToDeviceMem(len, bridgeRankDevice_, isBridgeVector));
5817 0 : opResPara_.topoInfo.bridgeRank = reinterpret_cast<u64>(bridgeRankDevice_.ptr());
5818 0 : opResPara_.topoInfo.bridgeRankNum = isBridgeVector.size();
5819 0 : }
5820 0 : return HCCL_SUCCESS;
5821 : }
5822 :
5823 0 : HcclResult HcclCommunicator::BuildCommPlanRank(const std::string& algName)
5824 : {
5825 0 : opResPara_.topoInfo.complanRank = 0;
5826 0 : opResPara_.topoInfo.complanRankLength = 0;
5827 0 : if (complanRankDevice_.ptr() == nullptr) {
5828 0 : std::vector<std::vector<std::vector<u32>>> commPlaneRanks;
5829 0 : CHK_RET(implAlg_->GetCommPlaneRanks(commPlaneRanks));
5830 0 : u64 tlvLen = 0;
5831 0 : CHK_RET(BuildOpTopoResTlvParam(algName, commPlaneRanks, complanRankDevice_, tlvLen));
5832 0 : opResPara_.topoInfo.complanRank = reinterpret_cast<u64>(complanRankDevice_.ptr());
5833 0 : opResPara_.topoInfo.complanRankLength = tlvLen;
5834 0 : HCCL_DEBUG(
5835 : "[HcclCommunicator][BuildCommPlanRank] comm plane ranks tlv length[%lu], ptr[%p], group[%s], "
5836 : "local user rankId[%u] ",
5837 : tlvLen, complanRankDevice_.ptr(), identifier_.c_str(), userRank_);
5838 0 : }
5839 0 : return HCCL_SUCCESS;
5840 : }
5841 :
5842 0 : HcclResult HcclCommunicator::BuildServerAndsuperPodRank(const std::string& algName)
5843 : {
5844 0 : opResPara_.topoInfo.serverAndsuperPodRank = 0;
5845 0 : opResPara_.topoInfo.serverAndsuperPodRankLength = 0;
5846 0 : if (serverAndsuperPodToRankDevice_.ptr() == nullptr) {
5847 0 : std::vector<std::vector<std::vector<u32>>> serverAndsuperPodToRank;
5848 0 : CHK_RET(implAlg_->GetRankVecInfo(serverAndsuperPodToRank));
5849 0 : u64 tlvLen = 0;
5850 0 : CHK_RET(BuildOpTopoResTlvParam(algName, serverAndsuperPodToRank, serverAndsuperPodToRankDevice_, tlvLen));
5851 0 : opResPara_.topoInfo.serverAndsuperPodRank = reinterpret_cast<u64>(serverAndsuperPodToRankDevice_.ptr());
5852 0 : opResPara_.topoInfo.serverAndsuperPodRankLength = tlvLen;
5853 0 : HCCL_DEBUG(
5854 : "[HcclCommunicator][BuildServerAndsuperPodRank] server and super pod ranks tlv length[%lu], ptr[%p], "
5855 : "group[%s], local user rankId[%u] ",
5856 : tlvLen, serverAndsuperPodToRankDevice_.ptr(), identifier_.c_str(), userRank_);
5857 0 : }
5858 0 : return HCCL_SUCCESS;
5859 : }
5860 :
5861 0 : HcclResult HcclCommunicator::BuildOpRetryParam(const AlgResourceResponse& algResource, const std::string& newTag)
5862 : {
5863 0 : opResPara_.config.retryEnable = static_cast<u8>(retryEnable_);
5864 0 : opResPara_.config.retryHoldTime = commConfig_.GetConfigRetryHoldTime();
5865 0 : opResPara_.config.retryIntervalTime = commConfig_.GetConfigRetryIntervalTime();
5866 : // aicpu和custom共用同一个opResPara_,aicpu初始化完成后,会修改h2d/d2h的指针,然后重新传给custom
5867 0 : opResPara_.kfcControlTransferH2DParams = kfcControlTransferH2D_->GetCommunicateParams();
5868 0 : opResPara_.kfcStatusTransferD2HParams = kfcStatusTransferD2H_->GetCommunicateParams();
5869 0 : opResPara_.debugConfig = GetDebugConfig();
5870 :
5871 0 : CHK_SMART_PTR_NULL(opRetryStreamPtr_);
5872 0 : if (opRetryStreamPtr_->find(newTag) == opRetryStreamPtr_->end()) {
5873 0 : std::vector<Stream> retryStreams(algResource.slaveDevStreams.begin(), algResource.slaveDevStreams.end());
5874 0 : retryStreams.push_back(opMainStream_);
5875 0 : opRetryStreamPtr_->insert(std::make_pair(newTag, retryStreams));
5876 0 : }
5877 0 : return HCCL_SUCCESS;
5878 : }
5879 :
5880 0 : HcclResult HcclCommunicator::BuildCommPlaneSubGroupRank(const std::string& algName)
5881 : {
5882 0 : opResPara_.hierarchicalAlgInfo.commplaneSubGroupRank = 0;
5883 0 : opResPara_.hierarchicalAlgInfo.commplaneSubGroupRankLength = 0;
5884 0 : if (commplaneSubGroupRankDevice_.ptr() == nullptr) {
5885 0 : std::vector<std::vector<std::vector<std::vector<u32>>>> commplaneSubGroupVector;
5886 0 : CHK_RET(implAlg_->GetCommPlaneSubGroupVector(commplaneSubGroupVector));
5887 0 : u64 tlvLen = 0;
5888 0 : CHK_RET(BuildOpTopoResVectorTlvParam(algName, commplaneSubGroupVector, commplaneSubGroupRankDevice_, tlvLen));
5889 : opResPara_.hierarchicalAlgInfo.commplaneSubGroupRank
5890 0 : = reinterpret_cast<u64>(commplaneSubGroupRankDevice_.ptr());
5891 0 : opResPara_.hierarchicalAlgInfo.commplaneSubGroupRankLength = tlvLen;
5892 0 : HCCL_DEBUG(
5893 : "[HcclCommunicator][BuildCommPlaneSubGroupRank] comm plane subGroups ranks tlv length[%lu], ptr[%p], "
5894 : "group[%s], local user rankId[%u] ",
5895 : tlvLen, commplaneSubGroupRankDevice_.ptr(), identifier_.c_str(), userRank_);
5896 0 : }
5897 0 : return HCCL_SUCCESS;
5898 : }
5899 :
5900 0 : HcclResult HcclCommunicator::BuildHierarchicalAlgOption(u32* ahcConfInfo)
5901 : {
5902 0 : std::map<AHCConcOpType, TemplateType> hierarchicalAlgOption;
5903 0 : CHK_RET(implAlg_->GetAHCAlgOption(hierarchicalAlgOption));
5904 0 : ahcConfInfo[TOP_HIERARCHICAL_CONF_lENGTH_INDEX] = hierarchicalAlgOption.size();
5905 :
5906 0 : if (hierarchicalAlgOption.size() >= (TOP_HIERARCHICAL_CONF_SIZE - 1)) {
5907 0 : HCCL_ERROR(
5908 : "[HcclCommunicator][BuildHierarchicalAlgOption] host hierarchicalAlgOption size[%u] exceed maxsize[%u]",
5909 : hierarchicalAlgOption.size(), (TOP_HIERARCHICAL_CONF_SIZE - 1));
5910 0 : return HCCL_E_INTERNAL;
5911 : }
5912 :
5913 0 : HCCL_DEBUG(
5914 : "[HcclCommunicator][BuildHierarchicalAlgOption] host hierarchicalAlgOption.size() [%u]",
5915 : hierarchicalAlgOption.size());
5916 :
5917 : // 默认清空内存
5918 0 : for (u32 i = TOP_HIERARCHICAL_CONF_INFO_INDEX; i < TOP_HIERARCHICAL_CONF_SIZE; i++) {
5919 0 : ahcConfInfo[i] = 0;
5920 : }
5921 :
5922 0 : u32 confDataStartIndex = TOP_HIERARCHICAL_CONF_INFO_INDEX;
5923 0 : for (auto it = hierarchicalAlgOption.begin(); it != hierarchicalAlgOption.end(); ++it) {
5924 0 : HCCL_DEBUG(
5925 : "[HcclCommunicator][BuildHierarchicalAlgOption] host Level [%u], ConcType[%u] AHCOpType[%u], TemplateType "
5926 : "[%u]",
5927 : it->first.ahcLevel, it->first.concType, it->first.ahcOpType, it->second);
5928 :
5929 0 : u32 confData = (static_cast<u32>(it->first.ahcLevel) << TOP_HIERARCHICAL_CONF_LEVEL_SHIFT)
5930 0 : | (static_cast<u32>(it->first.concType) << TOP_HIERARCHICAL_CONF_CONC_TYPE_SHIFT)
5931 0 : | (static_cast<u32>(it->first.ahcOpType) << TOP_HIERARCHICAL_CONF_OP_TYPE_SHIFT)
5932 0 : | (static_cast<u32>(it->second) << TOP_HIERARCHICAL_CONF_TEMPLATE_TYPE_SHIFT);
5933 0 : ahcConfInfo[confDataStartIndex] = confData;
5934 0 : confDataStartIndex = confDataStartIndex + 1;
5935 : }
5936 0 : return HCCL_SUCCESS;
5937 0 : }
5938 :
5939 0 : HcclResult HcclCommunicator::BuildOpTopoResParam(
5940 : const std::string& algName, [[maybe_unused]] const AlgResourceResponse& algResource)
5941 : {
5942 0 : opResPara_.topoInfo.userRank = userRank_;
5943 0 : opResPara_.topoInfo.userRankSize = userRankSize_;
5944 0 : opResPara_.topoInfo.deviceLogicId = deviceLogicId_;
5945 0 : opResPara_.topoInfo.isSingleMeshAggregation = isSingleMeshAggregation_;
5946 0 : opResPara_.topoInfo.deviceNumPerAggregation = deviceNumPerAggregation_;
5947 0 : opResPara_.topoInfo.superPodNum = superPodNum_;
5948 0 : opResPara_.topoInfo.devicePhyId = devicePhyId_;
5949 0 : opResPara_.topoInfo.deviceType = static_cast<u32>(deviceType_);
5950 : TopoType topoType;
5951 0 : CHK_RET(implAlg_->GetTopoType(topoType));
5952 0 : opResPara_.topoInfo.topoType = static_cast<u32>(topoType);
5953 0 : opResPara_.topoInfo.serverNum = serverNum_;
5954 0 : opResPara_.topoInfo.meshAggregationRankSize = meshAggregationRankSize_;
5955 0 : opResPara_.topoInfo.multiModuleDiffDeviceNumMode = multiModuleDiffDeviceNumMode_;
5956 0 : opResPara_.topoInfo.multiSuperPodDiffServerNumMode = multiSuperPodDiffServerNumMode_;
5957 0 : opResPara_.topoInfo.realUserRank = realUserRank_;
5958 0 : opResPara_.topoInfo.isDiffDeviceModule = isDiffDeviceModule_;
5959 0 : opResPara_.topoInfo.isDiffDeviceType = isDiffDeviceType_;
5960 0 : opResPara_.topoInfo.gcdDeviceNumPerAggregation = gcdDeviceNumPerAggregation_;
5961 0 : opResPara_.topoInfo.moduleNum = moduleNum_;
5962 0 : opResPara_.isARSDoubleRing = isARSDoubleRing_;
5963 0 : opResPara_.multiSuperPodDiffDeviceNumMode = multiSuperPodDiffDeviceNumMode_;
5964 0 : CHK_RET(BuildPairLinkCounter(algName));
5965 0 : CHK_RET(BuildIsUsedRdmaRank(algName));
5966 0 : CHK_RET(BuildNicList(algName));
5967 0 : CHK_RET(BuildBridgeRank(algName));
5968 0 : CHK_RET(BuildCommPlanRank(algName));
5969 0 : CHK_RET(BuildServerAndsuperPodRank(algName));
5970 0 : CHK_RET(BuildCommPlaneSubGroupRank(algName));
5971 0 : return HCCL_SUCCESS;
5972 : }
5973 :
5974 0 : HcclResult HcclCommunicator::BuildOpRemoteLinkP2pResParam(
5975 : const LINK& link, HccltagRemoteResV3& tagRemoteRes, TransportLinkType linkType)
5976 : {
5977 : // hccs sio并发场景,sio链路(linkTyp为SIO)打包到linkP2pSio, hccs链路(linkTyp为HCCS)打包到linkP2p;
5978 : // 其他场景打包到linkP2p
5979 0 : HcclLinkP2pV2* linkp2p = &(tagRemoteRes.tagRemoteResPtr->linkP2p);
5980 0 : if (linkType == TransportLinkType::SIO) {
5981 0 : linkp2p = &(tagRemoteRes.tagRemoteResPtr->linkP2pSio);
5982 : }
5983 0 : if (linkp2p->localIpcSignal[0].resId != INVALID_U64) {
5984 0 : HCCL_INFO(
5985 : "[%s]the linkP2p is existed, no need to refresh transport resource, resId[%llu]", __func__,
5986 : linkp2p->localIpcSignal[0].resId);
5987 0 : return HCCL_SUCCESS;
5988 : }
5989 : // localMem & remoteMem
5990 0 : void* inbufferPtr = nullptr;
5991 0 : void* outbufferPtr = nullptr;
5992 0 : CHK_RET(link->GetRemoteMem(UserMemType::INPUT_MEM, &inbufferPtr));
5993 0 : CHK_RET(link->GetRemoteMem(UserMemType::OUTPUT_MEM, &outbufferPtr));
5994 0 : (linkp2p->remoteMem)[INPUT].addr = reinterpret_cast<u64>(inbufferPtr);
5995 0 : (linkp2p->remoteMem)[OUTPUT].addr = reinterpret_cast<u64>(outbufferPtr);
5996 0 : CHK_RET(link->GetRemoteMemSize(UserMemType::INPUT_MEM, (linkp2p->remoteMem)[INPUT].size));
5997 0 : CHK_RET(link->GetRemoteMemSize(UserMemType::OUTPUT_MEM, (linkp2p->remoteMem)[OUTPUT].size));
5998 0 : MemDetails localMem; // 暂时预留,赋值为空
5999 0 : (linkp2p->localMem)[0] = localMem;
6000 0 : (linkp2p->localMem)[1] = localMem;
6001 0 : HCCL_DEBUG("[%s] finish set localMem & remoteMem info", __func__);
6002 : // localnotify & remotenotify
6003 0 : u64 notifyNum = 0;
6004 0 : std::vector<HcclSignalInfo> locIpcSignals;
6005 0 : std::vector<HcclSignalInfo> rmtIpcSignals;
6006 0 : CHK_RET(link->GetLocalNotify(locIpcSignals));
6007 0 : CHK_RET(link->GetRemoteNotify(rmtIpcSignals));
6008 :
6009 0 : for (size_t i = 0; i < locIpcSignals.size(); i++) {
6010 0 : CHK_RET(CheckNotifyOrQPMaxNum(notifyNum, LINK_P2P_MAX_NUM, true));
6011 0 : linkp2p->localIpcSignal[notifyNum] = locIpcSignals[i];
6012 0 : linkp2p->remoteIpcSignal[notifyNum] = rmtIpcSignals[i];
6013 0 : notifyNum++;
6014 : }
6015 0 : tagRemoteRes.p2pNotifyNum = notifyNum;
6016 0 : HCCL_DEBUG("[%s] finish set localnotify & remotenotify info, notifyNum[%llu]", __func__, notifyNum);
6017 : // transportAttr
6018 0 : CHK_RET(link->GetTransportAttr(linkp2p->transportAttr));
6019 0 : HCCL_DEBUG("[%s] finish set RemoteLinkP2pResParam info", __func__);
6020 0 : return HCCL_SUCCESS;
6021 0 : }
6022 :
6023 0 : HcclResult HcclCommunicator::BuildOpRemoteLinkRoceResParam(
6024 : const LINK& link, HccltagRemoteResV3& tagRemoteRes, bool isBackup, bool isRetry, bool isSecondBuild)
6025 : {
6026 0 : u32 iter = isSecondBuild ? 2 : 0;
6027 0 : HcclLinkRoceV2* linkRoce = isBackup ?
6028 0 : &(tagRemoteRes.tagRemoteResPtr->linkRoce[AICPU_RETRY_LINKROCE_BACKUP + iter]) :
6029 0 : &(tagRemoteRes.tagRemoteResPtr->linkRoce[AICPU_RETRY_LINKROCE_DEFAULT + iter]);
6030 0 : if (!isRetry && linkRoce->localNotifyList != 0) {
6031 0 : HCCL_INFO(
6032 : "[%s]the linkRoce is existed, no need to refresh transport resource, localNotifyListPtr[%p], iter[%u]",
6033 : __func__, reinterpret_cast<void*>(linkRoce->localNotifyList), iter);
6034 0 : return HCCL_SUCCESS;
6035 : }
6036 : // localMem & remoteMem
6037 0 : CHK_RET(link->GetLocalMemDetails(UserMemType::INPUT_MEM, (linkRoce->localMem)[INPUT]));
6038 0 : CHK_RET(link->GetLocalMemDetails(UserMemType::OUTPUT_MEM, (linkRoce->localMem)[OUTPUT]));
6039 0 : void* inbufferPtr = nullptr;
6040 0 : void* outbufferPtr = nullptr;
6041 0 : CHK_RET(link->GetRemoteMem(UserMemType::INPUT_MEM, &inbufferPtr));
6042 0 : CHK_RET(link->GetRemoteMem(UserMemType::OUTPUT_MEM, &outbufferPtr));
6043 0 : HCCL_DEBUG("[%s]inbufferPtr[%p], outbufferPtr[%p]", __func__, inbufferPtr, outbufferPtr);
6044 0 : if (inbufferPtr == nullptr || outbufferPtr == nullptr) {
6045 0 : HCCL_ERROR("[%s]inbufferPtr[%p], outbufferPtr[%p]", __func__, inbufferPtr, outbufferPtr);
6046 0 : return HCCL_E_INTERNAL;
6047 : }
6048 0 : (linkRoce->remoteMem)[INPUT].addr = reinterpret_cast<u64>(inbufferPtr);
6049 0 : (linkRoce->remoteMem)[OUTPUT].addr = reinterpret_cast<u64>(outbufferPtr);
6050 0 : CHK_RET(link->GetRemoteMemKey(UserMemType::INPUT_MEM, &((linkRoce->remoteMem)[INPUT].key)));
6051 0 : CHK_RET(link->GetRemoteMemKey(UserMemType::OUTPUT_MEM, &((linkRoce->remoteMem)[OUTPUT].key)));
6052 0 : CHK_RET(link->GetRemoteMemSize(UserMemType::INPUT_MEM, (linkRoce->remoteMem)[INPUT].size));
6053 0 : CHK_RET(link->GetRemoteMemSize(UserMemType::OUTPUT_MEM, (linkRoce->remoteMem)[OUTPUT].size));
6054 0 : HCCL_DEBUG("[%s] finish set localMem & remoteMem info", __func__);
6055 : // notifyValue & Key
6056 0 : std::vector<AddrKey> notifyValueAddrKey;
6057 0 : CHK_RET(link->GetLocalNotifyValueAddrKey(notifyValueAddrKey));
6058 0 : linkRoce->notifyValue = notifyValueAddrKey[0].addr;
6059 0 : linkRoce->notifyValueKey = notifyValueAddrKey[0].key;
6060 : // QPInfo
6061 0 : std::vector<HcclQpInfoV2> aiQpInfos;
6062 0 : CHK_RET(link->GetAiQpInfo(aiQpInfos));
6063 0 : u32 qpNum = aiQpInfos.size();
6064 0 : if (qpNum > RDMA_QP_MAX_NUM || qpNum < 1) {
6065 0 : return HCCL_E_INTERNAL;
6066 : }
6067 0 : std::copy_n(aiQpInfos.begin(), qpNum, linkRoce->QpInfo);
6068 0 : linkRoce->qpsPerConnection = qpNum - static_cast<u32>(qpNum > 1); // 多QP数量或单QP模式
6069 :
6070 : // localnotify & remotenotify
6071 0 : std::vector<AddrKey> notifyAddrKey;
6072 0 : std::vector<HcclSignalInfo> signalInfos;
6073 0 : CHK_RET(link->GetLocalRdmaNotify(signalInfos));
6074 0 : CHK_RET(link->GetRemoteRdmaNotifyAddrKey(notifyAddrKey));
6075 0 : if ((signalInfos.size() != notifyAddrKey.size()) || (signalInfos.size() < RDMA_NOTIFY_MIN_NUM)
6076 0 : || (signalInfos.size() > RDMA_NOTIFY_MAX_NUM) || (notifyAddrKey.size() < RDMA_NOTIFY_MIN_NUM)
6077 0 : || (notifyAddrKey.size() > RDMA_NOTIFY_MAX_NUM)
6078 0 : || ((signalInfos.size() - RDMA_NOTIFY_MIN_NUM) % linkRoce->qpsPerConnection != 0)
6079 0 : || ((notifyAddrKey.size() - RDMA_NOTIFY_MIN_NUM) % linkRoce->qpsPerConnection != 0)) {
6080 0 : HCCL_ERROR(
6081 : "[HcclCommunicator][BuildOpRemoteLinkRoceResParam] signalInfos %zu notifyAddrKey %zu "
6082 : "qpsPerConnection %u",
6083 : signalInfos.size(), notifyAddrKey.size(), linkRoce->qpsPerConnection);
6084 0 : return HCCL_E_INTERNAL;
6085 : }
6086 0 : u64 notifyNum = (notifyAddrKey.size() - RDMA_NOTIFY_MIN_NUM) / linkRoce->qpsPerConnection
6087 0 : - static_cast<u32>(linkRoce->qpsPerConnection > 1);
6088 0 : linkRoce->singleQPNotifyNum = notifyNum;
6089 :
6090 0 : u64 len = signalInfos.size() * sizeof(HcclSignalInfo);
6091 0 : DeviceMem localNotifyListMem;
6092 0 : CHK_RET(CopyVectorToDeviceMem(len, localNotifyListMem, signalInfos));
6093 0 : linkRoce->localNotifyList = reinterpret_cast<u64>(localNotifyListMem.ptr());
6094 0 : ibverbsLocalNotify_[tagRemoteRes.tagRemoteResPtr->tag].emplace_back(std::move(localNotifyListMem));
6095 :
6096 0 : len = notifyAddrKey.size() * sizeof(AddrKey);
6097 0 : DeviceMem remoteNotifyListMem;
6098 0 : CHK_RET(CopyVectorToDeviceMem(len, remoteNotifyListMem, notifyAddrKey));
6099 0 : linkRoce->remoteNotifyList = reinterpret_cast<u64>(remoteNotifyListMem.ptr());
6100 0 : ibverbsRemoteNotify_[tagRemoteRes.tagRemoteResPtr->tag].emplace_back(std::move(remoteNotifyListMem));
6101 :
6102 0 : HCCL_DEBUG(
6103 : "[%s] finish set localnotify & remotenotify info, notifyNum[%llu], linkNotifyNum[%llu]", __func__, notifyNum,
6104 : signalInfos.size());
6105 :
6106 0 : if (isBackup) {
6107 0 : tagRemoteRes.roceNotifyNumBackup = linkRoce->singleQPNotifyNum;
6108 0 : tagRemoteRes.qpNumBackup = linkRoce->qpsPerConnection;
6109 : } else {
6110 0 : tagRemoteRes.roceNotifyNum = linkRoce->singleQPNotifyNum;
6111 0 : tagRemoteRes.qpNum = linkRoce->qpsPerConnection;
6112 : }
6113 :
6114 0 : linkRoce->useAtomicWrite = link->GetIsUseAtomicWrite();
6115 0 : HCCL_DEBUG(
6116 : "[%s] finish set Qp info qpNum[%u], linkRoce->localNotifyList[0].resId[%llu], "
6117 : "notifyNum[%u], isBackup[%d], isSecond[%d], qpPtr[%llu], useAtomicWrite[%d]",
6118 : __func__, linkRoce->qpsPerConnection, signalInfos[0].resId, linkRoce->singleQPNotifyNum, isBackup,
6119 : isSecondBuild, linkRoce->QpInfo[0].qpPtr, linkRoce->useAtomicWrite);
6120 0 : return HCCL_SUCCESS;
6121 0 : }
6122 :
6123 : template <typename T>
6124 0 : HcclResult HcclCommunicator::CreateListNode(T** resHostPtr, T** resDevicePtr)
6125 : {
6126 0 : hostMemVec_.resize(hostMemVec_.size() + 1);
6127 0 : CHK_RET(AllocAndClearHostMem(sizeof(T), hostMemVec_.back()));
6128 0 : *resHostPtr = static_cast<T*>(hostMemVec_.back().get()->ptr());
6129 :
6130 0 : deviceMemVec_.resize(deviceMemVec_.size() + 1);
6131 0 : CHK_RET(AllocAndClearDeviceMem(sizeof(T), deviceMemVec_.back()));
6132 :
6133 0 : *resDevicePtr = static_cast<T*>(deviceMemVec_.back().get()->ptr());
6134 : // 初始化HcclRankRelationResV2中的tagRes链表
6135 0 : ListCommonInit(&((*resDevicePtr)->nextTagRes), &((*resHostPtr)->nextTagRes));
6136 0 : return HCCL_SUCCESS;
6137 : }
6138 :
6139 0 : HcclResult HcclCommunicator::BuildRemoteResByTag(
6140 : const std::string& newTag, const u32& usrRankId, HcclRankRelationResV2*& rankRelationResHostPtr,
6141 : HcclRankRelationResV2*& rankRelationResDevicePtr, [[maybe_unused]] bool isBackup, [[maybe_unused]] bool isRetry)
6142 : {
6143 0 : HCCL_DEBUG(
6144 : "[%s]start to add RemoteRes with newtag[%s] and remoteRankId[%u] to list", __func__, newTag.c_str(), usrRankId);
6145 0 : if (rankTagRemoteRes_.find(usrRankId) == rankTagRemoteRes_.end()
6146 0 : || rankTagRemoteRes_[usrRankId].find(newTag) == rankTagRemoteRes_[usrRankId].end()) {
6147 0 : HccltagRemoteResV2* tagRemoteResHostPtr = nullptr;
6148 0 : HccltagRemoteResV2* tagRemoteResDevicePtr = nullptr;
6149 0 : CHK_RET(CreateListNode(&tagRemoteResHostPtr, &tagRemoteResDevicePtr));
6150 0 : CHK_SAFETY_FUNC_RET(
6151 : memcpy_s(tagRemoteResHostPtr->tag, sizeof(tagRemoteResHostPtr->tag), newTag.c_str(), newTag.length() + 1));
6152 0 : tagRemoteResHostPtr->linkP2p.localIpcSignal[0].resId = INVALID_U64;
6153 0 : tagRemoteResHostPtr->linkP2pSio.localIpcSignal[0].resId = INVALID_U64;
6154 0 : tagRemoteResHostPtr->linkRoce[0].localNotifyList = 0;
6155 0 : tagRemoteResHostPtr->linkRoce[1].localNotifyList = 0;
6156 0 : tagRemoteResHostPtr->linkRoce[2].localNotifyList = 0;
6157 0 : tagRemoteResHostPtr->linkRoce[3].localNotifyList = 0;
6158 0 : ListCommonAddHead(
6159 0 : &tagRemoteResDevicePtr->nextTagRes, &tagRemoteResHostPtr->nextTagRes, &rankRelationResHostPtr->nextTagRes,
6160 0 : &rankRelationResDevicePtr->nextTagRes);
6161 0 : HccltagRemoteResV3 tempTagRemoteRes;
6162 0 : tempTagRemoteRes.tagRemoteResPtr = tagRemoteResHostPtr;
6163 0 : rankTagRemoteRes_[usrRankId][newTag] = tempTagRemoteRes;
6164 0 : HCCL_RUN_INFO(
6165 : "[%s] successfully add RemoteRes to list with newtag[%s], remoteRankId[%u]"
6166 : "rankRelationResHostPtr head addr[%p], nextHost[%p], preHost[%p], nextDevice[%p], preDevice[%p], "
6167 : "tagRemoteResDevicePtr head addr[%p]",
6168 : __func__, newTag.c_str(), usrRankId, &rankRelationResHostPtr->nextTagRes,
6169 : rankRelationResHostPtr->nextTagRes.nextHost, rankRelationResHostPtr->nextTagRes.preHost,
6170 : rankRelationResHostPtr->nextTagRes.nextDevice, rankRelationResHostPtr->nextTagRes.preDevice,
6171 : &tagRemoteResDevicePtr->nextTagRes);
6172 : } else {
6173 0 : HCCL_DEBUG(
6174 : "[%s] the RemoteRes with usr rankid[%u] tag[%s] has been added list", __func__, usrRankId, newTag.c_str());
6175 : }
6176 0 : return HCCL_SUCCESS;
6177 : }
6178 :
6179 0 : HcclResult HcclCommunicator::BuildRelationResByRemoteRankId(
6180 : const TransportRequest& transportRequest, const LINK& link, HcclRankRelationResV2*& rankRelationResHostPtr,
6181 : HcclRankRelationResV2*& rankRelationResDevicePtr)
6182 : {
6183 0 : const u32 usrRankId = transportRequest.remoteUserRank;
6184 0 : HCCL_INFO("[%s]start to add RelationRes with remote usr rankid[%u] to list", __func__, usrRankId);
6185 0 : if (opResPara_.remoteRes[usrRankId].nextHostPtr != 0 && opResPara_.remoteRes[usrRankId].nextDevicePtr != 0) {
6186 0 : rankRelationResHostPtr = reinterpret_cast<HcclRankRelationResV2*>(opResPara_.remoteRes[usrRankId].nextHostPtr);
6187 : rankRelationResDevicePtr
6188 0 : = reinterpret_cast<HcclRankRelationResV2*>(opResPara_.remoteRes[usrRankId].nextDevicePtr);
6189 0 : HCCL_DEBUG(
6190 : "[%s] RelationRes with remote usr rankid[%u] has been added to list, "
6191 : "rankRelationResHostPtr[%p], rankRelationResDevicePtr[%p]",
6192 : __func__, usrRankId, rankRelationResHostPtr, rankRelationResDevicePtr);
6193 : } else {
6194 0 : CHK_RET(CreateListNode(&rankRelationResHostPtr, &rankRelationResDevicePtr));
6195 0 : opResPara_.remoteRes[usrRankId].nextHostPtr = reinterpret_cast<u64>(rankRelationResHostPtr);
6196 0 : opResPara_.remoteRes[usrRankId].nextDevicePtr = reinterpret_cast<u64>(rankRelationResDevicePtr);
6197 0 : rankRelationResHostPtr->remoteUsrRankId = usrRankId;
6198 0 : rankRelationResHostPtr->remoteWorldRank = rankInfoList_[usrRankId].worldRank;
6199 0 : HCCL_DEBUG(
6200 : "[%s]successfully add RelationRes with remote usr rankid[%u] to list, rankRelationResHostPtr[%p],"
6201 : "rankRelationResDevicePtr[%p]",
6202 : __func__, usrRankId, rankRelationResHostPtr, rankRelationResDevicePtr);
6203 : }
6204 : // 刷新远端对应的cclbuffer
6205 0 : std::vector<void*> extraMemVector;
6206 0 : if (transportRequest.inputMemType == TransportMemType::CCL_INPUT && rankRelationResHostPtr->windowsIn == 0) {
6207 0 : void* inbufferPtr = nullptr;
6208 0 : CHK_RET(link->GetRemoteMem(UserMemType::INPUT_MEM, &inbufferPtr));
6209 0 : rankRelationResHostPtr->windowsIn = reinterpret_cast<u64>(inbufferPtr);
6210 : }
6211 0 : if (transportRequest.outputMemType == TransportMemType::CCL_OUTPUT && rankRelationResHostPtr->windowsOut == 0) {
6212 0 : void* outbufferPtr = nullptr;
6213 0 : CHK_RET(link->GetRemoteMem(UserMemType::OUTPUT_MEM, &outbufferPtr));
6214 0 : rankRelationResHostPtr->windowsOut = reinterpret_cast<u64>(outbufferPtr);
6215 : }
6216 0 : if (rankRelationResHostPtr->windowsExp == 0) {
6217 0 : std::vector<void*> memPtrVec = {};
6218 0 : CHK_RET(link->GetRemoteMem(&memPtrVec));
6219 0 : if (memPtrVec.size() != 0) {
6220 0 : rankRelationResHostPtr->windowsExp = reinterpret_cast<u64>(memPtrVec[0]);
6221 0 : if (link->GetTransportType() == TransportType::TRANS_TYPE_P2P) {
6222 0 : p2pCclBuf_[usrRankId] = memPtrVec[0];
6223 : } else {
6224 0 : cclBuf_[usrRankId] = memPtrVec[0];
6225 : }
6226 0 : rankRelationResHostPtr->windowsExp
6227 0 : += cclBufferManager_.GetInCCLbufferSize() + cclBufferManager_.GetOutCCLbufferSize();
6228 : }
6229 0 : }
6230 0 : HCCL_INFO(
6231 : "group[%s] successfully set windowsIn & windowsOut & windowsExp info: userRank[%u], groupRank[%u], "
6232 : "remoteRank[%u], windowsIn[0x%llx], InSize[0x%llx], windowOut[0x%llx], OutSize[0x%llx], "
6233 : "windowExp[0x%llx], ExpSize[0x%llx]",
6234 : identifier_.c_str(), GetUserRank(), GetGroupRank(), transportRequest.remoteUserRank,
6235 : rankRelationResHostPtr->windowsIn, cclBufferManager_.GetInCCLbufferSize(), rankRelationResHostPtr->windowsOut,
6236 : cclBufferManager_.GetOutCCLbufferSize(), rankRelationResHostPtr->windowsExp,
6237 : cclBufferManager_.GetExpBufferSize());
6238 0 : return HCCL_SUCCESS;
6239 0 : }
6240 :
6241 0 : HcclResult HcclCommunicator::ParseRemoteDataToMem(
6242 : const OpCommTransport& opTransportResponse, const std::string& newTag, const HcclCMDType opType, bool isBackup,
6243 : bool isRetry)
6244 : {
6245 0 : HCCL_INFO("[%s] entry process newtag[%s], isBackup[%d]", __func__, newTag.c_str(), isBackup);
6246 0 : std::set<u32> bsrTansportRank;
6247 0 : for (auto& levelNSubCommTransport : opTransportResponse) {
6248 0 : for (auto& singleSubCommTransport : levelNSubCommTransport) {
6249 0 : u32 linkIdx = 0;
6250 0 : for (auto& transportRequest : singleSubCommTransport.transportRequests) {
6251 0 : if (transportRequest.isValid) {
6252 0 : auto tempLink = singleSubCommTransport.links[linkIdx];
6253 0 : HCCL_INFO(
6254 : "[%s]transportRequest.isUsedRdma[%d], isBackup[%d]", __func__, transportRequest.isUsedRdma,
6255 : isBackup);
6256 0 : if ((!transportRequest.isUsedRdma || tempLink->GetLinkType() == LinkType::LINK_SIO)
6257 0 : && (isBackup || isRetry)) {
6258 0 : HCCL_INFO(
6259 : "[%s]no need to add p2p backup Link resource, transportRequest.isUsedRdma[%d], "
6260 : "isBackup[%d]",
6261 : __func__, transportRequest.isUsedRdma, isBackup);
6262 0 : linkIdx++;
6263 0 : continue;
6264 : }
6265 0 : HcclRankRelationResV2* rankRelationResHostPtr = nullptr;
6266 0 : HcclRankRelationResV2* rankRelationResDevicePtr = nullptr;
6267 0 : CHK_RET(BuildRelationResByRemoteRankId(
6268 : transportRequest, tempLink, rankRelationResHostPtr, rankRelationResDevicePtr));
6269 0 : const u32 usrRankId = transportRequest.remoteUserRank;
6270 0 : HCCL_INFO(
6271 : "[%s]successfully BuildRelationResByRemoteRankId with remote usr rankid[%u], "
6272 : "rankRelationResHostPtr[%p], rankRelationResDevicePtr[%p], newTage[%s]",
6273 : __func__, usrRankId, rankRelationResHostPtr, rankRelationResDevicePtr, newTag.c_str());
6274 0 : CHK_RET(BuildRemoteResByTag(
6275 : newTag, usrRankId, rankRelationResHostPtr, rankRelationResDevicePtr, isBackup, isRetry));
6276 : // transport信息保存(notify、qp)
6277 0 : if (!transportRequest.isUsedRdma || tempLink->GetLinkType() == LinkType::LINK_SIO) {
6278 : // sdma -> P2P
6279 0 : CHK_RET(BuildOpRemoteLinkP2pResParam(
6280 : tempLink, rankTagRemoteRes_[usrRankId][newTag], transportRequest.linkType));
6281 : } else {
6282 : // rdma -> roce
6283 0 : bool isSecondBuild = false;
6284 0 : if (opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV
6285 0 : && bsrTansportRank.find(transportRequest.remoteUserRank) != bsrTansportRank.end()) {
6286 0 : isSecondBuild = true;
6287 : }
6288 0 : bsrTansportRank.insert(transportRequest.remoteUserRank);
6289 0 : CHK_RET(BuildOpRemoteLinkRoceResParam(
6290 : tempLink, rankTagRemoteRes_[usrRankId][newTag], isBackup, isRetry, isSecondBuild));
6291 : }
6292 0 : HCCL_INFO(
6293 : "[%s] successfully add RemoteRes to list with newtag[%s] rankRelationResHostPtr "
6294 : "head addr[%p], nextHost[%p], preHost[%p], nextDevice[%p], preDevice[%p], "
6295 : "rankRelationResDevicePtr head addr[%p]",
6296 : __func__, newTag.c_str(), &rankRelationResHostPtr->nextTagRes,
6297 : rankRelationResHostPtr->nextTagRes.nextHost, rankRelationResHostPtr->nextTagRes.preHost,
6298 : rankRelationResHostPtr->nextTagRes.nextDevice, rankRelationResHostPtr->nextTagRes.preDevice,
6299 : &rankRelationResDevicePtr->nextTagRes);
6300 0 : HCCL_INFO(
6301 : "[%s] create link success with newtag[%s], linkIdx[%u], isBackup[%d], usrRankId[%u]", __func__,
6302 : newTag.c_str(), linkIdx, isBackup, usrRankId);
6303 0 : }
6304 0 : linkIdx++;
6305 : }
6306 : }
6307 : }
6308 0 : HCCL_DEBUG("[%s] process success newtag[%s]", __func__, newTag.c_str());
6309 0 : return HCCL_SUCCESS;
6310 0 : }
6311 :
6312 0 : HcclResult HcclCommunicator::BuildOpRemoteResParam(
6313 : const AlgResourceResponse& algResource, const std::string& newTag, const HcclCMDType opType, bool isRetry)
6314 : {
6315 0 : HCCL_DEBUG("[%s]start ParseRemoteDataToMem, IsEnableBackupLink[%d]", __func__, IsEnableBackupLink());
6316 0 : CHK_RET(ParseRemoteDataToMem(algResource.opTransportResponse, newTag, opType, false, isRetry));
6317 0 : if (IsEnableBackupLink()) {
6318 0 : HCCL_DEBUG("[%s]start Parse backupRemoteDataToMem, IsEnableBackupLink[%d]", __func__, IsEnableBackupLink());
6319 0 : CHK_RET(ParseRemoteDataToMem(algResource.opTransportResponseBackUp, newTag, opType, true, isRetry));
6320 : }
6321 0 : if (deviceType_ == DevType::DEV_TYPE_910_93 || deviceType_ == DevType::DEV_TYPE_910B) {
6322 0 : opResPara_.notifysize = 4; // 910B & 910_93 每个notify占4个字节
6323 : } else {
6324 0 : opResPara_.notifysize = 8; // 其他芯片类型每个notify占8个字节
6325 : }
6326 0 : return HCCL_SUCCESS;
6327 : }
6328 :
6329 5 : HcclResult HcclCommunicator::CopyHostListResToDeviceParam(
6330 : const std::string& newTag, const ListCommon* headHostList, const u64 size)
6331 : {
6332 5 : ListCommon* nextHostList = reinterpret_cast<ListCommon*>(headHostList->nextHost);
6333 5 : ListCommon* nextDeviceList = reinterpret_cast<ListCommon*>(headHostList->nextDevice);
6334 : // 该tag已分配过资源,只需刷新单节点(精确匹配);否则为首次分配,批量拷贝前N个节点
6335 5 : bool isRefreshSingleNode = (newTagResAlloced_.find(newTag) != newTagResAlloced_.end());
6336 : // ListCommonAddHead把新节点头插,原头节点的preHost/preDevice被改写,两节点都需要刷新到device
6337 5 : constexpr uint32_t UPDATE_NODE_NUM = 2;
6338 5 : uint32_t updateNodeCnt = 0;
6339 :
6340 12 : while (nextHostList != headHostList && updateNodeCnt < UPDATE_NODE_NUM) {
6341 9 : HCCL_INFO(
6342 : "[HcclCommunicator][CopyHostListResToDeviceParam] remote resource, tag[%s], head Host List[%p], next "
6343 : "Host List[%p],next Device List[%p]",
6344 : newTag.c_str(), headHostList, nextHostList, nextDeviceList);
6345 9 : if (isRefreshSingleNode) {
6346 : // 刷新已有资源:遍历链表找到匹配的tag,只拷贝该节点,避免全量刷新
6347 : std::string curTag = (size == sizeof(HccltagLocalResV2)) ?
6348 7 : reinterpret_cast<HccltagLocalResV2*>(nextHostList)->tag :
6349 7 : reinterpret_cast<HccltagRemoteResV2*>(nextHostList)->tag;
6350 7 : if (curTag == newTag) {
6351 2 : CHK_RET(hrtMemSyncCopy(
6352 : static_cast<void*>(nextDeviceList), size, static_cast<void*>(nextHostList), size,
6353 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
6354 2 : break;
6355 : }
6356 7 : } else {
6357 : // 首分配置:拷贝前UPDATE_NODE_NUM个节点到device,减少H2D次数
6358 2 : CHK_RET(hrtMemSyncCopy(
6359 : static_cast<void*>(nextDeviceList), size, static_cast<void*>(nextHostList), size,
6360 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
6361 2 : updateNodeCnt++;
6362 : }
6363 7 : nextDeviceList = reinterpret_cast<ListCommon*>(nextHostList->nextDevice);
6364 7 : nextHostList = reinterpret_cast<ListCommon*>(nextHostList->nextHost);
6365 : }
6366 5 : return HCCL_SUCCESS;
6367 : }
6368 :
6369 0 : HcclResult HcclCommunicator::CopyHostOpResToDeviceParam(const std::string& newTag)
6370 : {
6371 : // 1、将opResPara_,H2D到device
6372 0 : CHK_RET(hrtMemSyncCopy(
6373 : opResDevicePara_.ptr(), sizeof(HcclOpResParam), static_cast<void*>(&opResPara_), sizeof(HcclOpResParam),
6374 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
6375 0 : HCCL_DEBUG(
6376 : "[HcclCommunicator][CopyHostOpResToDeviceParam] tag[%s] local rankId[%u] workspace[%p] "
6377 : "workspacesize[%lu] ranksize[%u], cclbuffersize[%lu], cclinbuffer[%p], ccloutbuffer[%p], "
6378 : "remote winStart[%u], remote rWinOffset[%u], hostStateInfo[%p], aicpuStateInfo[%p], notifysize[%u], "
6379 : "sizeOfAiRMAInfo[%u],aiRMAInfo[%u]",
6380 : newTag.c_str(), userRank_, opResPara_.mc2WorkSpace.workSpace, opResPara_.mc2WorkSpace.workSpaceSize,
6381 : opResPara_.rankSize, opResPara_.winSize, opResPara_.localWindowsIn, opResPara_.localWindowsOut,
6382 : opResPara_.rWinStart, opResPara_.rWinOffset, opResPara_.hostStateInfo, opResPara_.aicpuStateInfo,
6383 : opResPara_.notifysize, opResPara_.sizeOfAiRMAInfo, opResPara_.aiRMAInfo);
6384 : // 2、将opResPara_中localres的tagRes,H2D到device
6385 0 : HCCL_DEBUG(
6386 : "[HcclCommunicator][CopyHostOpResToDeviceParam] local resource, tag[%s] streamNum[%u] signalNum[%u]",
6387 : newTag.c_str(), opResPara_.localRes.streamNum, opResPara_.localRes.signalNum);
6388 0 : CHK_RET(CopyHostListResToDeviceParam(
6389 : newTag, reinterpret_cast<ListCommon*>(&opResPara_.localRes.nextTagRes), sizeof(HccltagLocalResV2)));
6390 : // 3、遍历rank中tag资源,H2D到device
6391 0 : CHK_RET(CopyHostOpRemoteResToDeviceParam(newTag));
6392 0 : HCCL_DEBUG("[HcclCommunicator][CopyHostOpResToDeviceParam] copy host resource success!, tag[%s]", newTag.c_str());
6393 0 : return HCCL_SUCCESS;
6394 : }
6395 :
6396 0 : HcclResult HcclCommunicator::CopyHostAirmaInfoToDeviceParam(
6397 : const std::string& newTag, [[maybe_unused]] const HcclCMDType opType, const rtStream_t aiCpuStream)
6398 : {
6399 0 : HCCL_INFO("[HcclCommunicator][%s] Start prepare.", __func__);
6400 0 : CHK_PTR_NULL(aiRMAInfoMem_);
6401 0 : HcclAiRMAInfo* aiRMAInfoPtr = reinterpret_cast<HcclAiRMAInfo*>(aiRMAInfoMem_->ptr());
6402 0 : CHK_PTR_NULL(aiRMAInfoPtr);
6403 :
6404 0 : aiRMAInfoPtr->sizeOfAiRMAWQ = static_cast<u32>(sizeof(HcclAiRMAWQ));
6405 0 : aiRMAInfoPtr->sizeOfAiRMACQ = static_cast<u32>(sizeof(HcclAiRMACQ));
6406 0 : aiRMAInfoPtr->sizeOfAiRMAMem = static_cast<u32>(sizeof(HcclAiRMAMemInfo));
6407 :
6408 0 : CHK_RET(DeviceMem::alloc(aiSqDev_, aiSqMem_->size()));
6409 0 : aiRMAInfoPtr->sqPtr = aiSqDev_.ptr();
6410 0 : CHK_RET(hrtMemAsyncCopy(
6411 : aiSqDev_.ptr(), aiSqDev_.size(), aiSqMem_->ptr(), aiSqDev_.size(),
6412 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
6413 :
6414 0 : CHK_RET(DeviceMem::alloc(aiScqDev_, aiScqMem_->size()));
6415 0 : aiRMAInfoPtr->scqPtr = aiScqDev_.ptr();
6416 0 : CHK_RET(hrtMemAsyncCopy(
6417 : aiScqDev_.ptr(), aiScqDev_.size(), aiScqMem_->ptr(), aiScqDev_.size(),
6418 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
6419 :
6420 0 : CHK_RET(DeviceMem::alloc(aiRqDev_, aiRqMem_->size()));
6421 0 : aiRMAInfoPtr->rqPtr = aiRqDev_.ptr();
6422 0 : CHK_RET(hrtMemAsyncCopy(
6423 : aiRqDev_.ptr(), aiRqDev_.size(), aiRqMem_->ptr(), aiRqDev_.size(),
6424 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
6425 :
6426 0 : CHK_RET(DeviceMem::alloc(aiRcqDev_, aiRcqMem_->size()));
6427 0 : aiRMAInfoPtr->rcqPtr = aiRcqDev_.ptr();
6428 0 : CHK_RET(hrtMemAsyncCopy(
6429 : aiRcqDev_.ptr(), aiRcqDev_.size(), aiRcqMem_->ptr(), aiRcqDev_.size(),
6430 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
6431 :
6432 0 : CHK_RET(hrtMemAsyncCopy(
6433 : aiMemDetailsDev_.ptr(), aiMemDetailsDev_.size(), aiMemDetailsMem_->ptr(), aiMemDetailsDev_.size(),
6434 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
6435 :
6436 0 : CHK_RET(DeviceMem::alloc(aiMemDev_, aiMemMem_->size()));
6437 0 : aiRMAInfoPtr->memPtr = aiMemDev_.ptr();
6438 0 : CHK_RET(hrtMemAsyncCopy(
6439 : aiMemDev_.ptr(), aiMemDev_.size(), aiMemMem_->ptr(), aiMemDev_.size(),
6440 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
6441 :
6442 0 : opResPara_.sizeOfAiRMAInfo = static_cast<u64>(sizeof(HcclAiRMAInfo));
6443 0 : CHK_RET(DeviceMem::alloc(aiRMAInfoDev_, opResPara_.sizeOfAiRMAInfo));
6444 0 : opResPara_.aiRMAInfo = reinterpret_cast<u64>(aiRMAInfoDev_.ptr());
6445 :
6446 0 : CHK_RET(hrtMemAsyncCopy(
6447 : aiRMAInfoDev_.ptr(), aiRMAInfoDev_.size(), aiRMAInfoMem_->ptr(), aiRMAInfoDev_.size(),
6448 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
6449 0 : HCCL_INFO(
6450 : "[%s] tag[%s] curRankId[%u] rankNum[%u] qpNum[%u] aiRMAInfo[%p] sizeOfAiRMAInfo[%llu] "
6451 : "sizeOfAiRMAWQ[%u] sizeOfAiRMACQ[%u] sizeOfAiRMAMem[%u] sqPtr[%p] sqSize[%llu] sqCount[%zu] "
6452 : "scqPtr[%p] scqSize[%llu] scqCount[%zu] rqPtr[%p] rqSize[%llu] rqCount[%zu] rcqPtr[%p] "
6453 : "rcqSize[%llu] rcqCount[%zu] memPtr[%p] memSize[%llu] memCount[%zu] memDetailCount[%zu],opResPara_.aiRMAInfo",
6454 : __func__, newTag.c_str(), aiRMAInfoPtr->curRankId, aiRMAInfoPtr->rankNum, aiRMAInfoPtr->qpNum,
6455 : opResPara_.aiRMAInfo, opResPara_.sizeOfAiRMAInfo, aiRMAInfoPtr->sizeOfAiRMAWQ, aiRMAInfoPtr->sizeOfAiRMACQ,
6456 : aiRMAInfoPtr->sizeOfAiRMAMem, aiRMAInfoPtr->sqPtr, aiSqDev_.size(), aiSqMem_->size(), aiRMAInfoPtr->scqPtr,
6457 : aiScqDev_.size(), aiScqMem_->size(), aiRMAInfoPtr->rqPtr, aiRqDev_.size(), aiRqMem_->size(),
6458 : aiRMAInfoPtr->rcqPtr, aiRcqDev_.size(), aiRcqMem_->size(), aiRMAInfoPtr->memPtr, aiMemDev_.size(),
6459 : aiMemMem_->size(), aiMemDetailsMem_->size());
6460 0 : return HCCL_SUCCESS;
6461 : }
6462 :
6463 0 : HcclResult HcclCommunicator::BuildOpResParam(
6464 : const std::string& algName, const AlgResourceResponse& algResource, const std::string& newTag,
6465 : const HcclCMDType opType, const rtStream_t aicpuStream)
6466 : {
6467 0 : opResPara_.localUsrRankId = userRank_;
6468 0 : opResPara_.rankSize = userRankSize_;
6469 :
6470 0 : bool isUseUserMem = isUserMemRegisted_ && !userMemMap_.empty();
6471 0 : if (!isUseUserMem) {
6472 0 : opResPara_.winSize = algResource.cclInputMem.size();
6473 0 : opResPara_.localWindowsIn = reinterpret_cast<u64>(algResource.cclInputMem.ptr());
6474 0 : opResPara_.localWindowsOut = reinterpret_cast<u64>(algResource.cclOutputMem.ptr());
6475 : } else {
6476 0 : opResPara_.winSize = userMemMap_.begin()->second->size();
6477 0 : opResPara_.localWindowsIn = reinterpret_cast<u64>(userMemMap_.begin()->second->ptr());
6478 0 : opResPara_.localWindowsOut = reinterpret_cast<u64>(userMemMap_.begin()->second->ptr());
6479 : }
6480 : // 填充Exp相关信息 当前该块内存大小恒为1M
6481 0 : opResPara_.winExpSize = EXP_BUFFER_SIZE;
6482 0 : opResPara_.localWindowsExp = reinterpret_cast<u64>(cclBufferManager_.GetCommExpBuffer().ptr());
6483 0 : HCCL_INFO(
6484 : "[HcclCommunicator][%s] isUseUserMem[%d], winSize[%llu], localWindowsIn[%llu],"
6485 : "localWindowsOut[%llu], localWindowsExp[%llu]",
6486 : __func__, isUseUserMem, opResPara_.winSize, opResPara_.localWindowsIn, opResPara_.localWindowsOut,
6487 : opResPara_.localWindowsExp);
6488 :
6489 0 : CHK_SAFETY_FUNC_RET(
6490 : memcpy_s(opResPara_.hcomId, sizeof(opResPara_.hcomId), identifier_.c_str(), identifier_.length() + 1));
6491 :
6492 0 : opResPara_.config.deterministic = GetDeterministicConfig();
6493 0 : opResPara_.config.highPerfEnable = 0;
6494 0 : aclrtFloatOverflowMode floatOverflowMode = ACL_RT_OVERFLOW_MODE_UNDEF;
6495 0 : CHK_RET(hrtGetDeviceSatMode(&floatOverflowMode));
6496 0 : opResPara_.config.floatOverflowMode = floatOverflowMode;
6497 0 : opResPara_.config.taskMonitorInterval = GetExternalInputDfsTaskMonitorInterval();
6498 0 : bool isSupportAtomicWrite = false; // 涉及到任务编排,当前不能只判断本机驱动版本是否支持
6499 0 : opResPara_.config.isSupportAtomicWrite = static_cast<u8>(isSupportAtomicWrite);
6500 : opResPara_.config.notifyWaitTime
6501 0 : = (GetExternalInputHcclExecTimeoutSet() != HcclExecTimeoutSet::HCCL_EXEC_TIMEOUT_NOT_SET
6502 0 : || commConfig_.GetConfigExecTimeOutSet()) ?
6503 0 : commConfig_.GetConfigExecTimeOut() :
6504 : NOTIFY_DEFAULT_WAIT_TIME;
6505 0 : opResPara_.config.linkTimeOut = std::chrono::seconds(GetExternalInputHcclLinkTimeOut());
6506 0 : opResPara_.config.retryEnable = static_cast<u8>(retryEnable_);
6507 0 : opResPara_.config.interHccsDisable = GetExternalInputInterHccsDisable();
6508 0 : opResPara_.config.multiQpThreshold = GetExternalInputMultiQpThreshold();
6509 0 : opResPara_.rWinStart = offsetof(HcclOpResParam, remoteRes);
6510 0 : opResPara_.rWinOffset = sizeof(RemoteResPtr);
6511 0 : opResPara_.notifysize = 0;
6512 0 : opResPara_.lockAddr = hostDeviceLock_->GetDevMemAddr();
6513 0 : opResPara_.utraceStatusFlag = GetExternalInputHcclEnableEntryLog();
6514 0 : DeviceMem tinySendRecvMem;
6515 0 : CHK_RET(implAlg_->GetTinyMem(tinySendRecvMem));
6516 0 : opResPara_.tinyMem = reinterpret_cast<u64>(tinySendRecvMem.ptr());
6517 0 : opResPara_.tinyMemSize = reinterpret_cast<u64>(tinySendRecvMem.size());
6518 0 : opResPara_.opEntry = GetExternalInputHcclEnableEntryLog();
6519 0 : opResPara_.hcclSdmaQos = GetHcclQos();
6520 :
6521 0 : CHK_RET(BuildOpLocalResParam(algResource, newTag));
6522 0 : CHK_RET(BuildOpRemoteResParam(algResource, newTag, opType));
6523 0 : CHK_RET(BuildOpTopoResParam(algName, algResource));
6524 0 : CHK_RET(BuildOpRetryParam(algResource, newTag));
6525 0 : CHK_RET(BuildZeroCopyParam());
6526 0 : CHK_RET(BuildAicpuCustomParam());
6527 0 : CHK_RET(BuildAicpuOrderLaunchNotify()); // 先申请device侧的关于按序下发的Notify内存
6528 0 : if (algName == "RunAlltoAllAivDirect") {
6529 : // AIV直驱ROCE
6530 0 : CHK_RET(BuildAiRmaInfoParam(newTag, algName, opType));
6531 0 : CHK_RET(CopyHostAirmaInfoToDeviceParam(newTag, opType, aicpuStream));
6532 : }
6533 0 : CHK_RET(CopyHostOpResToDeviceParam(newTag));
6534 0 : HCCL_RUN_INFO(
6535 : "[%s]build aicpu unfold resource success, tag[%s] rWinStart[%u] rWinOffset[%u] opEntry[%d]", __func__,
6536 : newTag.c_str(), opResPara_.rWinStart, opResPara_.rWinOffset, opResPara_.opEntry);
6537 0 : return HCCL_SUCCESS;
6538 0 : }
6539 :
6540 0 : HcclResult HcclCommunicator::BuildCustomOpResParam()
6541 : {
6542 : // custom进程需要刷新h2d/d2h内存
6543 0 : opResPara_.kfcControlTransferH2DParams = customControlTransferH2D_->GetCommunicateParams();
6544 0 : opResPara_.kfcStatusTransferD2HParams = customStatusTransferD2H_->GetCommunicateParams();
6545 0 : CHK_RET(hrtMemSyncCopy(
6546 : opResDevicePara_.ptr(), sizeof(HcclOpResParam), static_cast<void*>(&opResPara_), sizeof(HcclOpResParam),
6547 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
6548 0 : return HCCL_SUCCESS;
6549 : }
6550 :
6551 24 : HcclResult HcclCommunicator::RegisterDfxInfo(
6552 : const OpParam& param, AlgType algType, const std::vector<Stream>& slaveStreams, bool isAiv, const std::string& tag)
6553 : {
6554 24 : u64 count = 0;
6555 24 : HcclDataType dataType = HcclDataType::HCCL_DATA_TYPE_RESERVED;
6556 24 : switch (param.opType) {
6557 0 : case HcclCMDType::HCCL_CMD_SEND:
6558 : case HcclCMDType::HCCL_CMD_RECEIVE:
6559 : case HcclCMDType::HCCL_CMD_BATCH_SEND_RECV:
6560 0 : count = param.GetDataCount(userRank_);
6561 0 : dataType = param.GetDataType();
6562 0 : HCCL_PROFILER_ADD_TAG_SENDRECV(param.tag, identifier_, GetWorkflowMode());
6563 0 : HCCL_PROFILER_ADD_GROUPRANK_SENDRECV(identifier_, userRankSize_, userRank_, param.dstRank);
6564 0 : break;
6565 5 : case HcclCMDType::HCCL_CMD_ALLTOALL:
6566 : case HcclCMDType::HCCL_CMD_ALLTOALLV:
6567 : case HcclCMDType::HCCL_CMD_ALLTOALLVC:
6568 5 : CHK_RET(AddGroupTagInfo(param.tag, isAiv));
6569 5 : count = param.All2AllDataDes.sendCount;
6570 5 : dataType = param.All2AllDataDes.sendType;
6571 5 : break;
6572 19 : default:
6573 19 : CHK_RET(AddGroupTagInfo(param.tag, isAiv));
6574 19 : count = param.GetDataCount(userRank_);
6575 19 : dataType = param.GetDataType();
6576 : }
6577 :
6578 24 : if (GetExternalInconsistentCheckSwitch() == InconsistentCheckMode::ON) {
6579 0 : if (param.opType != HcclCMDType::HCCL_CMD_BATCH_SEND_RECV) {
6580 0 : OpInfoDesc opInfo;
6581 0 : opInfo.opType = param.opType;
6582 0 : opInfo.dataType = dataType;
6583 0 : opInfo.reduceOp = param.reduceType;
6584 0 : opInfo.count = count;
6585 0 : opInfo.root = param.root;
6586 0 : opInfo.isValid = true;
6587 0 : AddOpInfoToHeartBeat(opInfo, tag);
6588 : }
6589 : }
6590 :
6591 : // task exception使用: 算子计数,算子入参信息(src/dst/datatype/reducetype)
6592 24 : HCCL_PROFILER_ADD_OPDATA_OP(
6593 : param.tag, count, param.inputPtr, param.outputPtr, dataType, param.root, identifier_, param.reduceType);
6594 : // 记录主流相关信息, 给profiling和task exception使用
6595 24 : HCCL_PROFILER_ADD_STREAM_BY_STREAMID(param.stream.id(), param.tag, 0, algType);
6596 24 : if (((GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE)
6597 14 : && !hccl::ProfilingManagerPub::GetAdditionInfoState() && !hccl::ProfilingManagerPub::GetTaskApiState())
6598 38 : && !param.isCapture) {
6599 11 : return HCCL_SUCCESS;
6600 : }
6601 : // 从流信息profiling开关打开的话再注册
6602 13 : for (u32 streamIndex = 0; streamIndex < slaveStreams.size(); streamIndex++) {
6603 0 : HCCL_PROFILER_ADD_STREAM_BY_STREAMID(slaveStreams[streamIndex].id(), param.tag, streamIndex + 1, algType);
6604 : }
6605 13 : return HCCL_SUCCESS;
6606 : }
6607 :
6608 0 : HcclResult HcclCommunicator::GetReportHcclMC2Info(const Stream& kfcStream, const std::vector<Stream>& aicpuStreams)
6609 : {
6610 0 : hcclMc2Info_.groupName = hrtMsprofGetHashId(identifier_.c_str(), identifier_.length());
6611 0 : hcclMc2Info_.rankSize = userRankSize_;
6612 0 : hcclMc2Info_.rankId = userRank_;
6613 0 : hcclMc2Info_.usrRankId = realUserRank_;
6614 0 : hcclMc2Info_.aicpuKfcStreamId = static_cast<uint32_t>(kfcStream.id());
6615 0 : hcclMc2Info_.reserve = 0;
6616 0 : const uint32_t ONCE_REPORT_STREAM_NUM_MAX = 8;
6617 0 : for (uint32_t streamIndex = 0, reportId = 0; streamIndex < aicpuStreams.size(); streamIndex++) {
6618 0 : HCCL_INFO("streamIndex:%u, reportId:%u, streamId:%d, ", streamIndex, reportId, aicpuStreams[streamIndex].id());
6619 0 : hcclMc2Info_.commStreamIds[reportId++] = aicpuStreams[streamIndex].id();
6620 0 : if (reportId == ONCE_REPORT_STREAM_NUM_MAX) {
6621 0 : hcclMc2Info_.commStreamSize = reportId;
6622 0 : CHK_RET(ProfilingManagerPub::CallMsprofReportMc2CommInfo(
6623 : hrtMsprofSysCycleTime(), &hcclMc2Info_, sizeof(hcclMc2Info_)));
6624 0 : reportId = 0;
6625 : }
6626 0 : if (streamIndex == (aicpuStreams.size() - 1)) {
6627 0 : HCCL_INFO("streamIndex:%u, reportId:%u, streamId:%d", streamIndex, reportId, opMainStream_.id());
6628 0 : hcclMc2Info_.commStreamIds[reportId++] = opMainStream_.id();
6629 0 : hcclMc2Info_.commStreamSize = reportId;
6630 0 : CHK_RET(ProfilingManagerPub::CallMsprofReportMc2CommInfo(
6631 : hrtMsprofSysCycleTime(), &hcclMc2Info_, sizeof(hcclMc2Info_)));
6632 0 : reportId = 0;
6633 : }
6634 : }
6635 0 : if (aicpuStreams.empty()) {
6636 0 : HCCL_INFO("only exist main stream, streamId:%d", opMainStream_.id());
6637 0 : hcclMc2Info_.commStreamIds[0] = opMainStream_.id();
6638 0 : hcclMc2Info_.commStreamSize = 1; // 只有主流1条
6639 0 : CHK_RET(ProfilingManagerPub::CallMsprofReportMc2CommInfo(
6640 : hrtMsprofSysCycleTime(), &hcclMc2Info_, sizeof(hcclMc2Info_)));
6641 : }
6642 0 : return HCCL_SUCCESS;
6643 : }
6644 :
6645 0 : HcclResult HcclCommunicator::OrchestrateAicpu(
6646 : const HcclCMDType& opType, const std::string& algName, const OpParam& param, const AlgResourceResponse& algResource,
6647 : const std::string& newTag, AlgType algType, bool isCustom, bool needIncreLink, bool needRecreateAlltoallComm)
6648 : {
6649 0 : uint64_t streamMode = 0;
6650 0 : CHK_RET(hrtStreamGetMode(param.stream.ptr(), &streamMode));
6651 : rtStream_t aicpuStream;
6652 0 : Mc2AiCpuStreamAllocAndGet(streamMode, aicpuStream); // aicpuStream需要在首次下发时申请
6653 0 : if (!isContextLaunched_) {
6654 : // 1、通信域内首次下发,从algResource中获取资源,H2D刷新资源,launch init
6655 : rtStream_t aicpuInitStream;
6656 0 : Mc2AiCpuInitStreamAllocAndGet(streamMode, aicpuInitStream); // 使用aicpuInitStream_下初始化kernel
6657 0 : Stream tmpStream(aicpuInitStream);
6658 0 : HCCL_DEBUG("%s ContextLaunched, aicpuInitStream:%p, aicpuStream:%p", __func__, aicpuInitStream, aicpuStream);
6659 0 : CHK_RET(AicpuResourceInit(algName, algResource, newTag, aicpuInitStream, opType, isCustom));
6660 0 : CHK_RET(GetReportHcclMC2Info(tmpStream, algResource.slaveDevStreams));
6661 0 : CHK_RET(SetAicpuUnfoldFlag());
6662 0 : } else if (
6663 0 : newTagResAlloced_.find(newTag) == newTagResAlloced_.end()
6664 0 : || (opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV && needIncreLink) || needRecreateAlltoallComm) {
6665 : // 2、通信域内非首次,但是有新的newTag,查看是否需要补充资源。
6666 0 : PetersonLockGuard guard(hostDeviceLock_.get());
6667 0 : CHK_PRT_RET(
6668 : guard.IsLockFailed(), HCCL_ERROR("[HcclCommunicator][OrchestrateAicp] hostDeviceLock lock failed"),
6669 : HCCL_E_INTERNAL);
6670 0 : CHK_RET(AicpuResourceRefresh(algResource, newTag, opType));
6671 0 : }
6672 0 : HCCL_DEBUG(
6673 : "%s isContextLaunched[%u], needRecreateAlltoallComm[%u]", __func__, isContextLaunched_,
6674 : needRecreateAlltoallComm);
6675 0 : bool isUsedMainStream = (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB);
6676 : // inplace支持重执行的stream资源处理逻辑
6677 0 : bool isHcclOpInplace = IsHcclOpInplace(opType, param, userRank_, userRankSize_, isInplaceStatus_);
6678 0 : if ((retryOrigWorkflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB) && retryEnable_
6679 0 : && isHcclOpInplace
6680 0 : && (opType == HcclCMDType::HCCL_CMD_ALLREDUCE || opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER)) {
6681 0 : isUsedMainStream = true;
6682 : }
6683 0 : AicpuOpTiling opTilingInfo;
6684 0 : opTilingInfo.algName = algName;
6685 0 : opTilingInfo.newTag = newTag;
6686 0 : opTilingInfo.algType = algType;
6687 0 : opTilingInfo.isUsedMainStream = isUsedMainStream;
6688 0 : opTilingInfo.dumpDebug = GetExternalInputHcclDumpDebug();
6689 0 : aclrtFloatOverflowMode floatOverflowMode = ACL_RT_OVERFLOW_MODE_UNDEF;
6690 0 : CHK_RET(hrtGetDeviceSatMode(&floatOverflowMode));
6691 0 : opTilingInfo.floatOverflowMode = floatOverflowMode;
6692 0 : HcclResult ret = HCCL_SUCCESS;
6693 : // 根据算子类型,获取 Aicpu Kernel 名称
6694 0 : auto iter = HCOM_CMD_TYPE_STR_MAP.find(opType);
6695 0 : CHK_PRT_RET(
6696 : (iter == HCOM_CMD_TYPE_STR_MAP.end()),
6697 : HCCL_ERROR("[%s] RunAicpuRpcSrvLaunchV2 kernel not found, opType=[%d]", __func__, static_cast<int>(opType)),
6698 : HCCL_E_INTERNAL);
6699 0 : std::string kernelName = std::string("RunAicpuRpcSrvLaunchV2") + "_" + iter->second;
6700 0 : ret = AicpuKfcTilingDataLaunchExt(param, opType, opResDevicePara_, kernelName, opTilingInfo, isCustom);
6701 0 : if (ret != HCCL_SUCCESS) {
6702 0 : HCCL_ERROR(
6703 : "[HcclCommunicator][OrchestrateAicpu]aicpu unfold launch kernel[%s] failed. ret[%d] inputPtr[%p]"
6704 : "outputPtr[%p] count[%llu] dataType[%s] op[%s]",
6705 : kernelName.c_str(), ret, param.inputPtr, param.outputPtr, param.DataDes.count,
6706 : GetDataTypeEnumStr(param.DataDes.dataType).c_str(), GetReduceOpEnumStr(param.reduceType).c_str());
6707 0 : return ret;
6708 : }
6709 0 : return HCCL_SUCCESS;
6710 0 : }
6711 :
6712 5 : HcclResult HcclCommunicator::CalcTinySendRecvMem(
6713 : const OpParam& opParam, AlgResourceResponse& algResResponse, DeviceMem& tinySendRecvMem) const
6714 : {
6715 5 : u64 sendCount = 0;
6716 5 : u64 recvCount = 0;
6717 5 : if (opParam.opType == HcclCMDType::HCCL_CMD_ALLTOALLV) {
6718 12 : for (u32 i = 0; i < userRankSize_; i++) {
6719 8 : u64 curSendCount = *(static_cast<const u64*>(opParam.All2AllDataDes.sendCounts) + i)
6720 8 : + *(static_cast<const u64*>(opParam.All2AllDataDes.sdispls) + i);
6721 8 : sendCount = std::max(sendCount, curSendCount);
6722 8 : u64 curRecvCount = *(static_cast<const u64*>(opParam.All2AllDataDes.recvCounts) + i)
6723 8 : + *(static_cast<const u64*>(opParam.All2AllDataDes.rdispls) + i);
6724 8 : recvCount = std::max(recvCount, curRecvCount);
6725 : }
6726 : } else {
6727 3 : for (u32 i = 0; i < userRankSize_; i++) {
6728 : sendCount
6729 2 : += *(static_cast<const u64*>(opParam.All2AllDataDes.sendCountMatrix) + userRank_ * userRankSize_ + i);
6730 : recvCount
6731 2 : += *(static_cast<const u64*>(opParam.All2AllDataDes.sendCountMatrix) + userRank_ + userRankSize_ * i);
6732 : }
6733 : }
6734 :
6735 5 : u32 sendTypeSize = 0, recvTypeSize = 0;
6736 5 : CHK_RET(SalGetDataTypeSize(opParam.All2AllDataDes.sendType, sendTypeSize));
6737 5 : CHK_RET(SalGetDataTypeSize(opParam.All2AllDataDes.recvType, recvTypeSize));
6738 :
6739 : // 在sendCount/recvCount全0时, 使用tinySendRecvMem, 避免使用空deviceMem
6740 10 : algResResponse.paramInputMem = sendCount == 0 ? DeviceMem::create(tinySendRecvMem.ptr(), tinySendRecvMem.size()) :
6741 5 : DeviceMem::create(opParam.inputPtr, sendCount * sendTypeSize);
6742 10 : algResResponse.paramOutputMem = recvCount == 0 ? DeviceMem::create(tinySendRecvMem.ptr(), tinySendRecvMem.size()) :
6743 5 : DeviceMem::create(opParam.outputPtr, recvCount * recvTypeSize);
6744 :
6745 5 : HCCL_INFO(
6746 : "[HcclCommunicator][CalcTinySendRecvMem] senMem addr[%p], sendSize[%llu], "
6747 : "RecvMem addr[%p], RecvSize[%llu],",
6748 : algResResponse.paramInputMem.ptr(), algResResponse.paramInputMem.size(), algResResponse.paramOutputMem.ptr(),
6749 : algResResponse.paramOutputMem.size());
6750 5 : return HCCL_SUCCESS;
6751 : }
6752 :
6753 3 : bool HcclCommunicator::HasRoceTransportLinks(OpCommTransport& opTransportReq) const
6754 : {
6755 20 : for (u32 levelIndex = 0; levelIndex < opTransportReq.size(); levelIndex++) {
6756 20 : for (u32 ringIndex = 0; ringIndex < opTransportReq[levelIndex].size(); ringIndex++) {
6757 3 : SingleSubCommTransport& reqSingleSubComm = opTransportReq[levelIndex][ringIndex];
6758 5 : for (u32 rankIndex = 0; rankIndex < reqSingleSubComm.transportRequests.size(); rankIndex++) {
6759 3 : TransportRequest& transportRequest = reqSingleSubComm.transportRequests[rankIndex];
6760 3 : if (transportRequest.isUsedRdma) {
6761 1 : return true;
6762 : }
6763 : }
6764 : }
6765 : }
6766 2 : return false;
6767 : }
6768 :
6769 0 : HcclResult HcclCommunicator::CleanTransportLinks(OpCommTransport& opTransportReq, OpCommTransport& opTransportResponse)
6770 : {
6771 0 : for (u32 levelIndex = 0; levelIndex < opTransportReq.size(); levelIndex++) {
6772 0 : for (u32 ringIndex = 0; ringIndex < opTransportReq[levelIndex].size(); ringIndex++) {
6773 0 : SingleSubCommTransport& reqSingleSubComm = opTransportReq[levelIndex][ringIndex];
6774 0 : SingleSubCommTransport& respSingleSubComm = opTransportResponse[levelIndex][ringIndex];
6775 0 : for (u32 rankIndex = 0; rankIndex < reqSingleSubComm.transportRequests.size(); rankIndex++) {
6776 0 : TransportRequest& transportRequest = reqSingleSubComm.transportRequests[rankIndex];
6777 0 : CHK_PRT_RET(
6778 : rankIndex >= respSingleSubComm.links.size(),
6779 : HCCL_ERROR(
6780 : "[CleanTransportLinks] The remote rank_id[%u] is larger than the existent respSingleSubComm "
6781 : "map "
6782 : "size[%u]",
6783 : rankIndex, respSingleSubComm.links.size()),
6784 : HCCL_E_PARA);
6785 0 : if (respSingleSubComm.links[rankIndex] != nullptr
6786 0 : && respSingleSubComm.links[rankIndex]->GetLinkType() != hccl::LinkType::LINK_RESERVED
6787 0 : && !transportRequest.isUsedRdma) {
6788 0 : HCCL_INFO(
6789 : "[CleanTransportLinks] The link to remote userRank[%u] has existed",
6790 : transportRequest.remoteUserRank);
6791 0 : continue;
6792 : }
6793 0 : respSingleSubComm.links[rankIndex] = nullptr;
6794 : }
6795 : }
6796 : }
6797 0 : return HCCL_SUCCESS;
6798 : }
6799 :
6800 116 : HcclResult HcclCommunicator::AllocAlgNotifys(
6801 : const std::string& tag, const NotifyLoadType notifyLoadType, const u32 notifyNum,
6802 : std::vector<std::shared_ptr<LocalNotify>>& notifiesMain, std::vector<std::shared_ptr<LocalNotify>>& notifiesAux)
6803 : {
6804 116 : std::vector<std::shared_ptr<LocalNotify>> notifys(notifyNum, nullptr);
6805 113 : CHK_RET(queueNotifyManagerRefac_->Alloc(tag, notifyNum, notifys, notifyLoadType));
6806 :
6807 116 : u32 signalNum = notifyNum >> 1;
6808 116 : notifiesMain.resize(signalNum);
6809 115 : notifiesAux.resize(signalNum);
6810 371 : for (u32 i = 0; i < signalNum; i++) {
6811 258 : notifiesMain[i] = notifys[i << 1];
6812 258 : notifiesAux[i] = notifys[(i << 1) + 1];
6813 : }
6814 113 : return HCCL_SUCCESS;
6815 113 : }
6816 :
6817 110 : HcclResult HcclCommunicator::AllocAlgResource(
6818 : const std::string& newTag, HcclCMDType opType, const OpParam& opParam, AlgResourceRequest& resRequest,
6819 : AlgResourceResponse& algResResponse, bool selectAivAlg)
6820 : {
6821 110 : HcclResult ret = HCCL_SUCCESS;
6822 110 : bool isGraphZeroCopyAlgAlloc = false;
6823 : // 只有aicpu模式下才需要申请从流和相关的notify资源,isNeedSlaveStream为true就代表算子下发是aicpu模式
6824 110 : bool isNeedSlaveStream = !selectAivAlg && opParam.aicpuUnfoldMode;
6825 110 : if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB
6826 111 : && !IsForceAicpuOpBaseMode(opParam, opType)) {
6827 61 : isGraphZeroCopyAlgAlloc = resRequest.isInGraphCaptureZeroCopy;
6828 61 : if (isGraphZeroCopyAlgAlloc) {
6829 0 : if (resRequest.scratchMemSize > 0) {
6830 : algResResponse.scratchMem
6831 0 : = DeviceMem::create(cclBufferManager_.GetOutCCLbuffer().ptr(), resRequest.scratchMemSize);
6832 : }
6833 61 : } else if (resRequest.scratchMemSize > 0) {
6834 24 : algResResponse.scratchMem = GetWorkspaceScracthMem(opParam.tag, resRequest.scratchMemSize);
6835 : }
6836 :
6837 61 : if (resRequest.streamNum > 0) {
6838 20 : if (isGraphZeroCopyAlgAlloc) {
6839 0 : CHK_RET(opStreamManager_->RegisterMaster(opParam.stream));
6840 : algResResponse.slaveStreams
6841 0 : = opStreamManager_->AllocSlaves(StreamType::STREAM_TYPE_ONLINE, resRequest.streamNum);
6842 0 : CHK_PRT_RET(
6843 : algResResponse.slaveStreams.empty(),
6844 : HCCL_ERROR(
6845 : "[AllocAlgResource]tag[%s] get slave stream failed, "
6846 : "expect to get size [%u], but only alloc 0.",
6847 : newTag.c_str(), resRequest.streamNum),
6848 : HCCL_E_INTERNAL);
6849 : } else {
6850 : // aicpu模式下不申请host从流
6851 20 : if (!opParam.aicpuUnfoldMode) {
6852 16 : algResResponse.slaveStreams = GetWorkspaceSubStreams(opParam.tag, resRequest.streamNum);
6853 : }
6854 : }
6855 : }
6856 50 : } else if (
6857 50 : GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE || IsForceAicpuOpBaseMode(opParam, opType)) {
6858 50 : CHK_RET(AllocOpBaseModeScratchMem(opType, opParam, resRequest, algResResponse));
6859 50 : if ((resRequest.streamNum > 0) && !selectAivAlg) {
6860 35 : CHK_RET(opStreamManager_->RegisterMaster(opParam.stream));
6861 : algResResponse.slaveStreams
6862 33 : = opStreamManager_->AllocSlaves(StreamType::STREAM_TYPE_ONLINE, resRequest.streamNum);
6863 36 : CHK_PRT_RET(
6864 : algResResponse.slaveStreams.empty(),
6865 : HCCL_ERROR(
6866 : "[AllocAlgResource]tag[%s] get slave stream failed, "
6867 : "expect to get size [%u], but only alloc 0.",
6868 : newTag.c_str(), resRequest.streamNum),
6869 : HCCL_E_INTERNAL);
6870 : }
6871 : } else {
6872 0 : HCCL_ERROR("[AllocAlgResource]WorkflowMode is not set.");
6873 0 : return HCCL_E_PARA;
6874 : }
6875 :
6876 110 : if (isNeedSlaveStream && ((userRankSize_ != 1) || IsForceAicpuOpBaseMode(opParam, opType))) {
6877 4 : CHK_RET(opStreamManager_->RegisterMaster(opParam.stream));
6878 : algResResponse.slaveDevStreams
6879 4 : = opStreamManager_->AllocSlaves(StreamType::STREAM_TYPE_DEVICE, LOCAL_STREAM_MAX_NUM);
6880 4 : CHK_PRT_RET(
6881 : algResResponse.slaveDevStreams.empty(),
6882 : HCCL_ERROR(
6883 : "[AllocAlgResource]tag[%s] get slave device stream failed, "
6884 : "expect to get size [%u], but only alloc 0.",
6885 : newTag.c_str(), LOCAL_STREAM_MAX_NUM),
6886 : HCCL_E_INTERNAL);
6887 4 : CHK_RET(AllocAlgNotifys(
6888 : opParam.tag, NotifyLoadType::DEVICE_NOTIFY, LOCAL_NOTIFY_MAX_NUM, algResResponse.notifiesDevMain,
6889 : algResResponse.notifiesDevAux));
6890 : }
6891 110 : uint8_t devNotifyNum = algResResponse.notifiesDevMain.size() + algResResponse.notifiesDevAux.size();
6892 110 : HCCL_INFO(
6893 : "[AllocAlgResource] tag[%s] alloc host slaveStreamNum[%u],"
6894 : "device slaveStreamNum[%u], devNotifyNum[%u], hostNotifyNum[%u]",
6895 : newTag.c_str(), algResResponse.slaveStreams.size(), algResResponse.slaveDevStreams.size(), devNotifyNum,
6896 : resRequest.notifyNum);
6897 112 : CHK_RET(AllocAlgNotifys(
6898 : opParam.tag, NotifyLoadType::HOST_NOTIFY, resRequest.notifyNum, algResResponse.notifiesMain,
6899 : algResResponse.notifiesAux));
6900 :
6901 109 : algResResponse.cclInputMem = cclBufferManager_.GetInCCLbuffer();
6902 109 : algResResponse.cclOutputMem = cclBufferManager_.GetOutCCLbuffer();
6903 109 : DeviceMem expMem = cclBufferManager_.GetCommCCLBuffer(); // 获取拓展内存
6904 109 : if (opParam.opType == HcclCMDType::HCCL_CMD_ALLTOALLV || opParam.opType == HcclCMDType::HCCL_CMD_ALLTOALLVC
6905 105 : || opParam.opType == HcclCMDType::HCCL_CMD_ALLTOALL) {
6906 4 : DeviceMem tinySendRecvMem;
6907 4 : CHK_RET(implAlg_->GetTinyMem(tinySendRecvMem));
6908 4 : CHK_RET(CalcTinySendRecvMem(opParam, algResResponse, tinySendRecvMem));
6909 8 : } else {
6910 105 : algResResponse.paramInputMem = DeviceMem::create(opParam.inputPtr, opParam.inputSize);
6911 105 : algResResponse.paramOutputMem = DeviceMem::create(opParam.outputPtr, opParam.outputSize);
6912 : }
6913 :
6914 109 : bool useOpbaseFlag = (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE && !opParam.isCapture);
6915 109 : if ((AIV_COMM_BUFFER_BITMASK & resRequest.aivBufferRequest) != 0) {
6916 4 : ret = cclBufferManager_.CreateCommAIVbuffer(useOpbaseFlag);
6917 4 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Alloc][AlgResource]Create CommAIVbuffer failed"), ret);
6918 4 : if (useOpbaseFlag) { // 单算子非Capture模式,对应aivOpbaseTag_
6919 2 : algResResponse.aivInputMem = cclBufferManager_.GetInAivOpbaseBuffer();
6920 2 : algResResponse.aivOutputMem = cclBufferManager_.GetOutAivOpbaseBuffer();
6921 : } else { // 静态图或者Capture模式,对应aivOffloadTag_
6922 2 : algResResponse.aivInputMem = cclBufferManager_.GetInAivOffloadbuffer();
6923 2 : algResResponse.aivOutputMem = cclBufferManager_.GetOutAivOffloadbuffer();
6924 : }
6925 4 : HCCL_INFO("[AllocAlgResource] tag[%s] alloc aiv buffer", newTag.c_str());
6926 : }
6927 109 : if ((AIV_COMM_INFO_BUFFER_BITMASK & resRequest.aivBufferRequest) != 0 || opParam.isNpuDirectRoce) {
6928 0 : if (!useOpbaseFlag) {
6929 0 : DeviceMem aivCommInfoMem; // 图模式每个算子单独一块内存
6930 0 : CHK_RET(DeviceMem::alloc(aivCommInfoMem, AIV_COMM_INFO_SIZE));
6931 0 : algResResponse.aivCommInfoMem = aivCommInfoMem;
6932 0 : aivOffloadCommInfoMem_.emplace_back(std::move(aivCommInfoMem));
6933 0 : } else {
6934 0 : ret = cclBufferManager_.CreateCommInfoAIVbuffer();
6935 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Alloc][AlgResource]Create CommInfoAIVbuffer failed"), ret);
6936 0 : algResResponse.aivCommInfoMem = cclBufferManager_.GetAivCommInfoBuffer(); // 单算子每个通信域只用一块内存
6937 : }
6938 0 : HCCL_INFO("[AllocAlgResource] tag[%s] alloc aiv comm info buffer", newTag.c_str());
6939 : }
6940 :
6941 : TransportIOMem transMem{
6942 109 : algResResponse.cclInputMem,
6943 109 : algResResponse.cclOutputMem,
6944 109 : algResResponse.paramInputMem,
6945 109 : algResResponse.paramOutputMem,
6946 108 : algResResponse.scratchMem,
6947 108 : algResResponse.aivInputMem,
6948 108 : algResResponse.aivOutputMem,
6949 : expMem,
6950 : DeviceMem(),
6951 109 : {}};
6952 108 : HCCL_DEBUG(
6953 : "algResResponse.cclInputMem[%p], size[%llu]; algResResponse.cclOutputMem[%p], "
6954 : "size[%llu]; algResResponse.paramInputMem[%p], size[%llu]; algResResponse.paramOutputMem[%p], size[%llu].",
6955 : algResResponse.cclInputMem.ptr(), algResResponse.cclInputMem.size(), algResResponse.cclOutputMem.ptr(),
6956 : algResResponse.cclOutputMem.size(), algResResponse.paramInputMem.ptr(), algResResponse.paramInputMem.size(),
6957 : algResResponse.paramOutputMem.ptr(), algResResponse.paramOutputMem.size());
6958 112 : algResResponse.opTransportResponse = resRequest.opTransport;
6959 :
6960 : // 零拷贝场景这里只借助P2p的openIpc能力交换控制面zeroCopyLocalBuffer_,不交换实际用户的输出输出
6961 112 : if (opParam.isZeroCopy) {
6962 0 : HCCL_INFO(
6963 : "[AllocAlgResource] zero copy change paramInput[%p] paramOutput[%p] scratchMem[%p] to localBuffer[%p]",
6964 : transMem.paramInputMem.ptr(), transMem.paramOutputMem.ptr(), transMem.scratchMem.ptr(),
6965 : zeroCopyLocalBuffer_.ptr());
6966 0 : transMem.scratchMem = zeroCopyLocalBuffer_;
6967 0 : transMem.paramInputMem = zeroCopyLocalBuffer_;
6968 0 : transMem.paramOutputMem = zeroCopyLocalBuffer_;
6969 : } else {
6970 112 : if (isGraphZeroCopyAlgAlloc) {
6971 : transMem.scratchMem
6972 0 : = DeviceMem::create(cclBufferManager_.GetOutCCLbuffer().ptr(), resRequest.scratchMemSize);
6973 0 : HCCL_INFO("[AllocAlgResource] acl graph set transMem.scratchMem =%ul", transMem.scratchMem.size());
6974 : }
6975 : }
6976 :
6977 112 : ClearOpTransportResponseLinks(algResResponse.opTransportResponse);
6978 111 : if (IsEnableBackupLink()) {
6979 0 : algResResponse.opTransportResponseBackUp = resRequest.opTransport;
6980 0 : ClearOpTransportResponseLinks(algResResponse.opTransportResponseBackUp);
6981 0 : HCCL_DEBUG(
6982 : "[%s]IsEnableBackupLink[%d] init backup & default opTransportResponse", __func__, IsEnableBackupLink());
6983 : }
6984 :
6985 111 : if (!GetExternalInputHcclEnableFfts() && GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
6986 42 : u32 slaveNum = algResResponse.slaveStreams.size();
6987 42 : algResResponse.threadManage.resize(slaveNum);
6988 112 : for (u32 ringIndex = 0; ringIndex < slaveNum; ringIndex++) {
6989 140 : algResResponse.threadManage[ringIndex].reset(new (std::nothrow)
6990 70 : ThreadManage(deviceLogicId_, userRank_, dispatcher_));
6991 70 : CHK_SMART_PTR_NULL(algResResponse.threadManage[ringIndex]);
6992 70 : HcclResult ret = algResResponse.threadManage[ringIndex]->Init();
6993 70 : CHK_PRT_RET(
6994 : ret != HCCL_SUCCESS,
6995 : HCCL_ERROR("[Init][MultiRingResource]ringIndex[%u] ThreadManage failed,return[%d]", ringIndex, ret),
6996 : ret);
6997 70 : HCCL_INFO("ringThreadsManage Init success[%u]", ringIndex);
6998 : }
6999 : }
7000 111 : transportManager_->SetOpType(opParam.opType);
7001 111 : if (isUserMemRegisted_) {
7002 : // user win模式,用exchange接口建链的transport
7003 0 : algResResponse.opTransportResponse = userMemTransport_;
7004 0 : CHK_RET(GetRemoteUserMemResource());
7005 : } else {
7006 111 : StateGuard<HcclCommunicator, HcclCommState> guard(this, HcclCommState::BUILDING);
7007 112 : ret = transportManager_->Alloc(
7008 112 : opParam.tag, transMem, algResResponse.opTransportResponse, opParam.aicpuUnfoldMode, false,
7009 112 : opParam.isZeroCopy, opParam.opType, opParam.isCapture, false, opParam.isNpuDirectRoce, &opParam);
7010 112 : CHK_PRT_RET(
7011 : ret != HCCL_SUCCESS, HCCL_ERROR("[%s]Alloc transports failed, tag[%s]", __func__, newTag.c_str()), ret);
7012 112 : }
7013 59 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[%s]Alloc transports failed, tag[%s]", __func__, newTag.c_str()), ret);
7014 :
7015 59 : if (retryEnable_) {
7016 : // 获取当前rdma相连的所有对端rankList
7017 0 : std::vector<u32> rankList;
7018 0 : CHK_RET(transportManager_->GetRemoteRankList(
7019 : algResResponse.opTransportResponse, rankList, TransportType::TRANS_TYPE_IBV_EXP));
7020 0 : std::string rankListStr = "";
7021 0 : for (auto remoteRank : rankList) {
7022 0 : rankListStr += (std::to_string(remoteRank) + ";");
7023 : }
7024 0 : HCCL_DEBUG("identifier[%s] newTag[%s] rankList[%s]", identifier_.c_str(), newTag.c_str(), rankListStr.c_str());
7025 0 : CHK_RET(OpRetryManager::AddLinkInfoByIdentifier(deviceLogicId_, identifier_, newTag, rankList));
7026 0 : }
7027 :
7028 59 : if (IsEnableBackupLink()) {
7029 : // 超节点 && level2支持重执行 && Aicpu:创建备用Transport资源
7030 0 : StateGuard<HcclCommunicator, HcclCommState> guard(this, HcclCommState::BUILDING);
7031 0 : ret = transportManager_->Alloc(
7032 0 : opParam.tag, transMem, algResResponse.opTransportResponseBackUp, opParam.aicpuUnfoldMode, true,
7033 0 : opParam.isCapture);
7034 0 : CHK_PRT_RET(
7035 : ret != HCCL_SUCCESS, HCCL_ERROR("[%s]Alloc backup transports failed, tag[%s]", __func__, newTag.c_str()),
7036 : ret);
7037 0 : }
7038 59 : SaveLinkRes(algResResponse.opTransportResponse);
7039 59 : SaveLinkRes(algResResponse.opTransportResponseBackUp);
7040 59 : remoteTransportMap_ = transportManager_->GetRemoteTransportMap();
7041 59 : HCCL_DEBUG("[%s] process success newtag[%s]", __func__, newTag.c_str());
7042 59 : return HCCL_SUCCESS;
7043 112 : }
7044 :
7045 0 : HcclResult HcclCommunicator::GetRemoteUserMemResource()
7046 : {
7047 0 : for (auto& levelNSubCommTransport : userMemTransport_) {
7048 0 : for (auto& singleSubCommTransport : levelNSubCommTransport) {
7049 0 : u32 linkIdx = 0;
7050 0 : for (auto& transportRequest : singleSubCommTransport.transportRequests) {
7051 0 : if (!transportRequest.isValid) {
7052 0 : continue;
7053 : }
7054 0 : auto tempLink = singleSubCommTransport.links[linkIdx];
7055 0 : MemDetails remoteMem;
7056 0 : u32 remoteId = tempLink->GetRemoteRank();
7057 0 : CHK_PRT_RET(
7058 : (remoteId >= MAX_RANK_NUM_A3),
7059 : HCCL_ERROR(
7060 : "[%s]Invalid remoteId, valid range is [0, %u), remoteId[%u]", __func__, MAX_RANK_NUM_A3,
7061 : remoteId),
7062 : HCCL_E_PARA);
7063 0 : void* userMemPtr = nullptr;
7064 0 : CHK_RET(tempLink->GetRemoteMem(UserMemType::INPUT_MEM, &userMemPtr));
7065 0 : CHK_PTR_NULL(userMemPtr);
7066 0 : remoteMem.addr = reinterpret_cast<u64>(userMemPtr);
7067 0 : CHK_RET(tempLink->GetRemoteMemSize(UserMemType::INPUT_MEM, remoteMem.size));
7068 0 : opResPara_.userMemRes[remoteId] = remoteMem;
7069 0 : HCCL_INFO(
7070 : "[%s]add userMem res success, remoteId[%u], "
7071 : "remote addr[%llu], linkIdx[%u]",
7072 : __func__, remoteId, remoteMem.addr, linkIdx);
7073 0 : linkIdx++;
7074 0 : }
7075 : }
7076 : }
7077 0 : opResPara_.userMemType = TYPE_USER_MEM;
7078 0 : return HCCL_SUCCESS;
7079 : }
7080 :
7081 0 : HcclResult HcclCommunicator::IncreAllocLink(
7082 : const std::string& newTag, const OpParam& opParam, AlgResourceRequest& resRequest,
7083 : AlgResourceResponse& algResResponse)
7084 : {
7085 0 : algResResponse.cclInputMem = cclBufferManager_.GetInCCLbuffer();
7086 0 : algResResponse.cclOutputMem = cclBufferManager_.GetOutCCLbuffer();
7087 0 : DeviceMem expMem = cclBufferManager_.GetCommCCLBuffer();
7088 0 : transportManager_->SetOpType(opParam.opType);
7089 :
7090 : TransportIOMem transMem{
7091 0 : algResResponse.cclInputMem,
7092 0 : algResResponse.cclOutputMem,
7093 0 : algResResponse.paramInputMem,
7094 0 : algResResponse.paramOutputMem,
7095 0 : algResResponse.scratchMem,
7096 0 : algResResponse.aivInputMem,
7097 0 : algResResponse.aivOutputMem,
7098 : expMem,
7099 : DeviceMem(),
7100 0 : {}};
7101 : {
7102 0 : StateGuard<HcclCommunicator, HcclCommState> guard(this, HcclCommState::BUILDING);
7103 0 : CHK_RET(transportManager_->IncreAlloc(
7104 : opParam.tag, transMem, resRequest.opTransport, algResResponse.opTransportResponse, opParam.aicpuUnfoldMode,
7105 : false, opParam.isCapture, opParam.opType));
7106 0 : }
7107 0 : if (retryEnable_) {
7108 : // 获取当前rdma相连的所有对端rankList
7109 0 : std::vector<u32> rankList;
7110 0 : CHK_RET(transportManager_->GetIncreRemoteRankList(
7111 : resRequest.opTransport, rankList, TransportType::TRANS_TYPE_IBV_EXP));
7112 0 : std::string rankListStr = "";
7113 0 : for (auto remoteRank : rankList) {
7114 0 : rankListStr += (std::to_string(remoteRank) + ";");
7115 : }
7116 0 : HCCL_DEBUG("identifier[%s] newTag[%s] rankList[%s]", identifier_.c_str(), newTag.c_str(), rankListStr.c_str());
7117 0 : CHK_RET(OpRetryManager::AddLinkInfoByIdentifier(deviceLogicId_, identifier_, newTag, rankList, true));
7118 0 : }
7119 0 : if (IsEnableBackupLink()) {
7120 0 : StateGuard<HcclCommunicator, HcclCommState> guard(this, HcclCommState::BUILDING);
7121 0 : CHK_RET(transportManager_->IncreAlloc(
7122 : opParam.tag, transMem, resRequest.opTransport, algResResponse.opTransportResponseBackUp,
7123 : opParam.aicpuUnfoldMode, true, opParam.isCapture, opParam.opType));
7124 0 : }
7125 0 : remoteTransportMap_ = transportManager_->GetRemoteTransportMap();
7126 0 : SaveLinkRes(algResResponse.opTransportResponse);
7127 0 : SaveLinkRes(algResResponse.opTransportResponseBackUp);
7128 0 : return HCCL_SUCCESS;
7129 0 : }
7130 :
7131 0 : HcclResult HcclCommunicator::SetDevicePid(s32 devicePid)
7132 : {
7133 0 : devicePid_ = devicePid;
7134 0 : return HCCL_SUCCESS;
7135 : }
7136 :
7137 809 : void HcclCommunicator::ReleaseWorkSpacebuffer() { workSpace_.free(); }
7138 :
7139 0 : HcclResult HcclCommunicator::AllocAndClearDeviceMem(u64 size, std::shared_ptr<DeviceMem>& bufferPtr) const
7140 : {
7141 0 : CHK_PRT_RET(
7142 : !size,
7143 : HCCL_INFO("[HcclCommunicator][AllocAndClearDeviceMem]device memory size is zero. not need to malloc memory"),
7144 : HCCL_SUCCESS);
7145 :
7146 : CHK_PRT_RET(
7147 : (size > ULONG_MAX),
7148 : HCCL_ERROR("[HcclCommunicator][AllocAndClearDeviceMem]device memory size is greater than %llu", ULONG_MAX),
7149 : HCCL_E_PARA);
7150 :
7151 0 : DeviceMem tmpBuffer;
7152 0 : CHK_RET(DeviceMem::alloc(tmpBuffer, size));
7153 0 : EXCEPTION_CATCH((bufferPtr = std::make_shared<DeviceMem>(std::move(tmpBuffer))), return HCCL_E_PTR);
7154 :
7155 0 : CHK_PRT_RET(
7156 : size && !bufferPtr.get()->ptr(),
7157 : HCCL_ERROR(
7158 : "[HcclCommunicator][AllocAndClearDeviceMem]Create DeviceMem size[%llu] fail,"
7159 : "please check workspace size.",
7160 : size),
7161 : HCCL_E_PTR);
7162 0 : CHK_RET(hrtMemSet(bufferPtr.get()->ptr(), size, size));
7163 0 : return HCCL_SUCCESS;
7164 0 : }
7165 :
7166 1499 : HcclResult HcclCommunicator::AllocAndClearHostMem(u64 size, std::shared_ptr<HostMem>& bufferPtr) const
7167 : {
7168 1499 : CHK_PRT_RET(
7169 : !size,
7170 : HCCL_INFO("[HcclCommunicator][AllocAndClearHostMem] host memory size is zero. not need to malloc memory"),
7171 : HCCL_SUCCESS);
7172 :
7173 : CHK_PRT_RET(
7174 : (size > ULONG_MAX),
7175 : HCCL_ERROR("[HcclCommunicator][AllocAndClearHostMem] host memory size is greater than %llu", ULONG_MAX),
7176 : HCCL_E_PARA);
7177 :
7178 1499 : HostMem tmpBuffer = HostMem::alloc(size);
7179 1504 : EXCEPTION_CATCH((bufferPtr = std::make_shared<HostMem>(std::move(tmpBuffer))), return HCCL_E_PTR);
7180 :
7181 1500 : CHK_PRT_RET(
7182 : size && !bufferPtr.get()->ptr(),
7183 : HCCL_ERROR(
7184 : "[HcclCommunicator][AllocAndClearHostMem]host memory space size[%llu] fail,"
7185 : "please check workspace size.",
7186 : size),
7187 : HCCL_E_PTR);
7188 1500 : CHK_SAFETY_FUNC_RET(memset_s(bufferPtr.get()->ptr(), size, 0, size));
7189 1500 : return HCCL_SUCCESS;
7190 1500 : }
7191 :
7192 523 : HcclResult HcclCommunicator::CreateWorkSpace(u64 size, DeviceMem& buffer) const
7193 : {
7194 523 : CHK_PRT_RET(
7195 : !size, HCCL_INFO("[Create][WorkSpace]work space size is zero. not need to malloc memory"), HCCL_SUCCESS);
7196 :
7197 : CHK_PRT_RET(
7198 : (size > ULONG_MAX), HCCL_ERROR("[Create][WorkSpace]work space size is greater than %llu", ULONG_MAX),
7199 : HCCL_E_PARA);
7200 :
7201 523 : u64 memSize = size;
7202 523 : CHK_RET(DeviceMem::alloc(buffer, memSize));
7203 523 : CHK_RET(hrtMemSet(buffer.ptr(), size, size));
7204 523 : return HCCL_SUCCESS;
7205 : }
7206 :
7207 0 : HcclResult HcclCommunicator::GetWorkSpace(u64* workSpaceSize, u64* workSpace) const
7208 : {
7209 0 : *workSpaceSize = workSpaceSize_;
7210 0 : *workSpace = reinterpret_cast<u64>(workSpace_.ptr());
7211 0 : return HCCL_SUCCESS;
7212 : }
7213 :
7214 0 : HcclResult HcclCommunicator::InitWorkSpace()
7215 : {
7216 0 : if (workSpace_.ptr() == nullptr) {
7217 0 : workSpaceSize_ = COMM_MAX_WORK_SPACE_SIZE;
7218 0 : CHK_RET(CreateWorkSpace(workSpaceSize_, workSpace_));
7219 : }
7220 0 : return HCCL_SUCCESS;
7221 : }
7222 :
7223 0 : HcclResult HcclCommunicator::FillOpParam(
7224 : const HcclCMDType commType, OpParam& opParam, const uint64_t count, void* pCount, void* pDispls) const
7225 : {
7226 0 : if (commType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER || commType == HcclCMDType::HCCL_CMD_ALLGATHER
7227 0 : || commType == HcclCMDType::HCCL_CMD_ALLREDUCE) {
7228 0 : opParam.DataDes.count = count;
7229 0 : opParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_FP16; // 按照fp16配置
7230 0 : } else if (
7231 0 : commType == HcclCMDType::HCCL_CMD_ALLTOALLV || commType == HcclCMDType::HCCL_CMD_ALLTOALL
7232 0 : || commType == HcclCMDType::HCCL_CMD_ALLTOALLVC) {
7233 0 : opParam.All2AllDataDes.sendType = HcclDataType::HCCL_DATA_TYPE_FP16;
7234 0 : opParam.All2AllDataDes.recvType = HcclDataType::HCCL_DATA_TYPE_FP16;
7235 0 : opParam.All2AllDataDes.sendCounts = pCount;
7236 0 : opParam.All2AllDataDes.recvCounts = pCount;
7237 0 : opParam.All2AllDataDes.sdispls = pDispls;
7238 0 : opParam.All2AllDataDes.rdispls = pDispls;
7239 0 : opParam.All2AllDataDes.sendCountMatrix = pCount;
7240 0 : } else if (commType == HcclCMDType::HCCL_CMD_BATCH_WRITE) {
7241 : } else {
7242 0 : HCCL_ERROR("[%s] invalid commType=[%u]", __func__, static_cast<uint32_t>(commType));
7243 0 : return HCCL_E_PARA;
7244 : }
7245 0 : return HCCL_SUCCESS;
7246 : }
7247 :
7248 0 : HcclResult HcclCommunicator::AllocComResource(
7249 : const string& newTag, const string& algName, const HcclCMDType commType, const OpParam& opParam, rtStream_t stream,
7250 : bool isNeedHostSlaveStream)
7251 : {
7252 0 : if (resMap_.find(newTag) == resMap_.end()) { // 计算&申请通信资源
7253 0 : unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(commType);
7254 0 : CHK_PRT_RET(algOperator == nullptr, HCCL_ERROR("[%s] algOperator is nullptr", __func__), HCCL_E_INTERNAL);
7255 0 : AlgResourceRequest resRequest;
7256 0 : CHK_RET(algOperator->CalcResRequest(algName, opParam, resRequest));
7257 0 : AlgResourceResponse algResResponse;
7258 0 : CHK_RET(AllocAlgResource(newTag, commType, opParam, resRequest, algResResponse, isNeedHostSlaveStream));
7259 0 : resMap_[newTag] = algResResponse;
7260 0 : CHK_RET(RegisterToHeartBeat());
7261 0 : }
7262 :
7263 0 : CHK_RET(InitWorkSpace());
7264 0 : HcclResult ret = GetWorkSpace(&(opResPara_.mc2WorkSpace.workSpaceSize), &(opResPara_.mc2WorkSpace.workSpace));
7265 0 : CHK_PRT_RET(
7266 : ret != HCCL_SUCCESS,
7267 : HCCL_ERROR(
7268 : "%s GetWorkSpace fail, size[%llu] space[%llu]", __func__, opResPara_.mc2WorkSpace.workSpaceSize,
7269 : opResPara_.mc2WorkSpace.workSpace),
7270 : ret);
7271 :
7272 0 : if (!isContextLaunched_) { // 通信域内首次下发
7273 0 : uint64_t streamMode = 0;
7274 0 : CHK_RET(hrtStreamGetMode(opParam.stream.ptr(), &streamMode));
7275 : rtStream_t aicpuStream;
7276 0 : Mc2AiCpuStreamAllocAndGet(streamMode, aicpuStream); // aicpuStream需要在首次下发时申请
7277 :
7278 : rtStream_t aicpuInitStream;
7279 0 : Mc2AiCpuInitStreamAllocAndGet(streamMode, aicpuInitStream);
7280 0 : Stream tmpStream(aicpuInitStream);
7281 0 : HCCL_DEBUG("%s ContextLaunched, aicpuInitStream:%p, aicpuStream:%p", __func__, aicpuInitStream, aicpuStream);
7282 0 : CHK_RET(AicpuResourceInit(algName, resMap_[newTag], newTag, stream, commType));
7283 0 : CHK_RET(GetReportHcclMC2Info(tmpStream, resMap_[newTag].slaveDevStreams));
7284 0 : } else if (newTagResAlloced_.find(newTag) == newTagResAlloced_.end()) {
7285 : // 通信域内非首次,但是有新的newTag
7286 0 : PetersonLockGuard guard(hostDeviceLock_.get());
7287 0 : CHK_PRT_RET(guard.IsLockFailed(), HCCL_ERROR("[%s] hostDeviceLock lock failed", __func__), HCCL_E_INTERNAL);
7288 0 : CHK_RET(AicpuResourceRefresh(resMap_[newTag], newTag, commType));
7289 0 : }
7290 0 : return HCCL_SUCCESS;
7291 : }
7292 :
7293 2 : HcclResult HcclCommunicator::AllocComResourceByTiling(const string& algConfig, void* param)
7294 : {
7295 2 : CHK_PTR_NULL(combinOparaMem_);
7296 2 : HcclCombinOpParam* combinOparaPtr = reinterpret_cast<HcclCombinOpParam*>(combinOparaMem_->ptr());
7297 2 : CHK_PTR_NULL(combinOparaPtr);
7298 :
7299 2 : string algName, newTag;
7300 2 : OpParam& opParam = *static_cast<OpParam*>(param);
7301 2 : CHK_RET(GetAlgInfo(algConfig, opParam.tag, opParam.opType, algName, newTag));
7302 2 : if (algName == "RunAlltoAllAivDirect") {
7303 1 : opParam.isNpuDirectRoce = true;
7304 : }
7305 2 : CHK_RET(CreateAndGetAiCpuNotifyWithNotifyRes(combinOparaPtr->signalInfo.aicpuNotify));
7306 0 : HCCL_INFO("Create aicpu notify %p.", localAiCpuNotifyRes_[0]->ptr());
7307 :
7308 : // 只有第一次创建,此处通过CCL Buffer地址有效来防止通信域内非首次重新申请内存
7309 : // 已注册user mem情况下,不创建ccl buffer,使用user mem通信
7310 0 : if (userMemMap_.empty()) {
7311 0 : CHK_RET(CreateCommCCLbuffer());
7312 0 : CHK_RET(cclBufferManager_.GetInCCLbuffer(opParam.inputPtr, opParam.inputSize));
7313 0 : CHK_RET(cclBufferManager_.GetOutCCLbuffer(opParam.outputPtr, opParam.outputSize));
7314 : } else {
7315 0 : auto it = userMemMap_.begin();
7316 0 : opParam.outputSize = it->second->size();
7317 0 : opParam.inputSize = it->second->size();
7318 : }
7319 :
7320 : // 按照 ccl buffer size 折算,不同算子折算方式不同, allreduce和cclbuffer size相同
7321 : // allgather、reducescatter、alltoall需除以rank size
7322 0 : uint64_t count = opParam.outputSize / SIZE_TABLE[HcclDataType::HCCL_DATA_TYPE_FP16];
7323 0 : if (opParam.opType != HcclCMDType::HCCL_CMD_ALLREDUCE) {
7324 0 : count = (count + userRankSize_ - 1) / userRankSize_;
7325 : }
7326 0 : HCCL_INFO("[%s] userRankSize=[%u], count=[%u]", __func__, userRankSize_, count);
7327 0 : vector<uint64_t> countList(userRankSize_ * userRankSize_, count);
7328 0 : vector<uint64_t> displsList(userRankSize_, 0);
7329 0 : void* pCount = static_cast<void*>(&countList[0]);
7330 0 : void* pDispls = static_cast<void*>(&displsList[0]);
7331 0 : CHK_RET(FillOpParam(opParam.opType, opParam, count, pCount, pDispls));
7332 : // MC2算子不需要申请host侧的从流
7333 0 : bool isNeedHostSlaveStream = false;
7334 0 : CHK_RET(AllocComResource(newTag, algName, opParam.opType, opParam, opParam.stream.ptr(), isNeedHostSlaveStream));
7335 0 : return HCCL_SUCCESS;
7336 2 : }
7337 :
7338 1 : HcclResult HcclCommunicator::CreateCommResource(
7339 : const std::string& tag, rtStream_t aiCpuStream, bool isOpbaseMode, void** commContext, const std::string& algConfig)
7340 : {
7341 1 : const std::string& suffix = HCCL_MC2_MULTISERVER_SUFFIX;
7342 1 : string algName = "";
7343 1 : string newTag = tag;
7344 1 : if (tag.size() > suffix.size() && tag.compare(tag.size() - suffix.size(), suffix.size(), suffix) == 0) {
7345 0 : HCCL_INFO("[HcclCommunicator][CreateCommResource] Set isA2MC2MultiServer_ to [true]");
7346 0 : isA2MC2MultiServer_ = true;
7347 0 : char* mmSysGetEnvValue = nullptr;
7348 0 : MM_SYS_GET_ENV(MM_ENV_HCCL_INTRA_PCIE_ENABLE, mmSysGetEnvValue);
7349 0 : std::string intraPcieEnableEnv = (mmSysGetEnvValue != nullptr) ? mmSysGetEnvValue : "EmptyString";
7350 0 : bool envA2MC2Hie = (intraPcieEnableEnv == "1") && (GetExternalInputIntraRoceSwitch() == 0);
7351 0 : if (!algConfig.empty()) {
7352 0 : CHK_RET(GetAlgInfo(algConfig, tag, algName));
7353 0 : if (algName == "DispatchCombineHierarchy" || (algName == "BatchWriteBySdma" && envA2MC2Hie)) {
7354 0 : isA2MC2IntraHie_ = true;
7355 0 : newTag.insert(newTag.size() - suffix.size(), "_HIE");
7356 : }
7357 : }
7358 0 : }
7359 1 : if (isA2MC2MultiServer_ && !isNeedInitNic_) {
7360 0 : InitNic(true);
7361 : }
7362 :
7363 1 : if ((deviceType_ != DevType::DEV_TYPE_910_93 && moduleNum_ > 1 && !isA2MC2MultiServer_)
7364 1 : || (deviceType_ == DevType::DEV_TYPE_910_93 && superPodNum_ > 1)) {
7365 0 : HCCL_ERROR(
7366 : "[HcclCommunicator][CommResource]MC2 does not support in the current scenario, "
7367 : "device type[%d] moduleNum[%d] serverNum[%d] superPodNum[%d], isMC2MultiServer[%d].",
7368 : deviceType_, moduleNum_, serverNum_, superPodNum_, isA2MC2MultiServer_);
7369 0 : return HCCL_E_NOT_SUPPORT;
7370 : }
7371 :
7372 1 : HCCL_INFO(
7373 : "[HcclCommunicator][CommResource]newTag[%s] aicpu stream[%p] isOpbaseMode[%u]", newTag.c_str(), aiCpuStream,
7374 : isOpbaseMode);
7375 :
7376 1 : Stream stream(aiCpuStream);
7377 1 : CHK_RET(CreateCommAndStreamRes(newTag, stream));
7378 :
7379 1 : CHK_RET(Mc2CreateAndLaunchContext(aiCpuStream, isOpbaseMode, commContext, newTag));
7380 1 : return HCCL_SUCCESS;
7381 1 : }
7382 :
7383 0 : HcclResult HcclCommunicator::Mc2CreateAndLaunchContext(
7384 : rtStream_t aiCpuStream, bool isOpbaseMode, void** commContext, const string& tag)
7385 : {
7386 0 : CHK_PTR_NULL(combinOparaMem_);
7387 0 : HcclCombinOpParam* combinOparaPtr = reinterpret_cast<HcclCombinOpParam*>(combinOparaMem_->ptr());
7388 0 : CHK_PTR_NULL(combinOparaPtr);
7389 0 : CHK_RET(InitWorkSpace());
7390 :
7391 : HcclResult result
7392 0 : = GetWorkSpace(&(combinOparaPtr->mc2WorkSpace.workSpaceSize), &(combinOparaPtr->mc2WorkSpace.workSpace));
7393 0 : CHK_PRT_RET(
7394 : result != HCCL_SUCCESS,
7395 : HCCL_ERROR(
7396 : "[HcclCommunicator][CommResource]errNo[0x%016llx] size[%llu] space[%llu]", HCCL_ERROR_CODE(result),
7397 : combinOparaPtr->mc2WorkSpace.workSpaceSize, combinOparaPtr->mc2WorkSpace.workSpace),
7398 : result);
7399 :
7400 0 : CHK_SAFETY_FUNC_RET(memcpy_s(
7401 : combinOparaPtr->hcomId, sizeof(combinOparaPtr->hcomId), identifier_.c_str(), identifier_.length() + 1));
7402 :
7403 0 : Stream tmpStream(aiCpuStream);
7404 0 : CHK_RET(CreateAndGetAiCpuNotifyWithNotifyRes(combinOparaPtr->signalInfo.aicpuNotify));
7405 0 : CHK_RET(CreateAndGetAiCpuNotify(
7406 : localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_0)],
7407 : combinOparaPtr->signalInfo.aicpuOpNotify[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_0)]));
7408 0 : CHK_RET(CreateAndGetAiCpuNotify(
7409 : localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_1)],
7410 : combinOparaPtr->signalInfo.aicpuOpNotify[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_1)]));
7411 : // 申请集合通信域存储context的device空间
7412 0 : CHK_RET(CreateDeviceCommContext(sizeof(HcclCombinOpParam), commContext_));
7413 0 : combinOparaPtr->config.deterministic = GetDeterministicConfig();
7414 : // retryEnable 写入aicpu_ctx
7415 0 : combinOparaPtr->config.retryEnable = static_cast<u8>(retryEnable_);
7416 0 : combinOparaPtr->config.retryHoldTime = commConfig_.GetConfigRetryHoldTime();
7417 0 : combinOparaPtr->config.retryIntervalTime = commConfig_.GetConfigRetryIntervalTime();
7418 : combinOparaPtr->config.notifyWaitTime
7419 0 : = (GetExternalInputHcclExecTimeoutSet() != HcclExecTimeoutSet::HCCL_EXEC_TIMEOUT_NOT_SET
7420 0 : || commConfig_.GetConfigExecTimeOutSet()) ?
7421 0 : commConfig_.GetConfigExecTimeOut() :
7422 : NOTIFY_DEFAULT_WAIT_TIME;
7423 0 : combinOparaPtr->config.linkTimeOut = std::chrono::seconds(GetExternalInputHcclLinkTimeOut());
7424 :
7425 0 : combinOparaPtr->kfcControlTransferH2DParams = kfcControlTransferH2D_->GetCommunicateParams();
7426 0 : combinOparaPtr->kfcStatusTransferD2HParams = kfcStatusTransferD2H_->GetCommunicateParams();
7427 :
7428 0 : void* overflowAddr = nullptr;
7429 0 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
7430 0 : CHK_RET(hrtCtxGetOverflowAddr(&overflowAddr));
7431 0 : combinOparaPtr->overFlowAddr = reinterpret_cast<u64>(overflowAddr);
7432 0 : HCCL_INFO(
7433 : "[HcclImplBase][Mc2CreateAndLaunchContext]get combinOparaPtr->overFlowAddr %llx",
7434 : combinOparaPtr->overFlowAddr);
7435 : // 非整卡 (2DUO卡各取1芯的场景) 因为受到PCIE限制,不可以使用读操作进行数据拷贝
7436 0 : if (pairLinkInfo_[static_cast<u32>(LinkTypeInServer::HCCS_TYPE)].size() != userRankSize_) {
7437 0 : combinOparaPtr->onlyRead = 1;
7438 : }
7439 : }
7440 0 : HCCL_INFO("read only is set to %u", combinOparaPtr->onlyRead);
7441 :
7442 0 : if (isA2MC2MultiServer_) {
7443 : // 拷贝normal transport信息到device侧
7444 0 : bool isSupportAIVNormalQP = false;
7445 0 : CHK_RET(IsSupportAIVNormalQP(devicePhyId_, isSupportAIVNormalQP));
7446 0 : CHK_PTR_NULL(transDevIbverbsDataMem_);
7447 0 : const u64 ibverbsDataSize = transDevIbverbsDataMem_->size();
7448 0 : CHK_RET(DeviceMem::alloc(ibverbsDataBuffer_, ibverbsDataSize));
7449 0 : CHK_RET(hrtMemAsyncCopy(
7450 : ibverbsDataBuffer_.ptr(), ibverbsDataBuffer_.size(), transDevIbverbsDataMem_->ptr(), ibverbsDataSize,
7451 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
7452 :
7453 0 : combinOparaPtr->ibverbsData = reinterpret_cast<u64>(ibverbsDataBuffer_.ptr());
7454 0 : combinOparaPtr->ibverbsDataSize = ibverbsDataSize;
7455 0 : combinOparaPtr->multiServerFlag = static_cast<u8>(true);
7456 :
7457 0 : CHK_PTR_NULL(combinedCapabilityMem_);
7458 0 : const u64 capabilitySize = sizeof(CombinedCapability);
7459 0 : CHK_RET(DeviceMem::alloc(combinedCapabilityBuffer_, capabilitySize));
7460 0 : CHK_RET(hrtMemAsyncCopy(
7461 : combinedCapabilityBuffer_.ptr(), combinedCapabilityBuffer_.size(), combinedCapabilityMem_->ptr(),
7462 : capabilitySize, HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
7463 :
7464 0 : combinOparaPtr->capabilityPtr = reinterpret_cast<u64>(combinedCapabilityBuffer_.ptr());
7465 0 : combinOparaPtr->capabilitySize = capabilitySize;
7466 :
7467 0 : HCCL_INFO(
7468 : "[HcclImplBase][Mc2CreateAndLaunchContext] set ibverbsData to [%llu], "
7469 : "multiServerFlag to [%u]",
7470 : combinOparaPtr->ibverbsData, combinOparaPtr->multiServerFlag);
7471 0 : if (isSupportAIVNormalQP && isA2MC2IntraHie_) {
7472 0 : CHK_RET(H2DAiRMAInfo(tag, aiCpuStream));
7473 : }
7474 : }
7475 :
7476 : // 将通信数据拷贝到device侧,供AICPU算法编排使用
7477 0 : CHK_RET(hrtMemAsyncCopy(
7478 : commContext_.ptr(), commContext_.size(), combinOparaMem_->ptr(), combinOparaMem_->size(),
7479 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
7480 :
7481 0 : std::string kernelName = "RunAicpuKfcResInit";
7482 0 : CHK_RET(AiCpuKernelLaunch(tmpStream.ptr(), reinterpret_cast<u64>(commContext_.ptr()), kernelName));
7483 0 : SetMC2EnvFlag();
7484 0 : if (isOpbaseMode == true) {
7485 0 : CHK_RET(hcclStreamSynchronize(tmpStream.ptr(), commConfig_.GetConfigExecTimeOut()));
7486 : }
7487 :
7488 0 : *commContext = commContext_.ptr();
7489 0 : return HCCL_SUCCESS;
7490 0 : }
7491 :
7492 : HcclResult
7493 0 : HcclCommunicator::GetAiCpuNotifyData(const std::shared_ptr<LocalNotify>& localNotify, HcclSignalInfo& notifyInfo) const
7494 : {
7495 0 : if (localNotify == nullptr) {
7496 0 : HCCL_INFO("[HcclCommunicator][GetAiCpuNotifyData]notifyHandle is null");
7497 0 : notifyInfo.resId = INVALID_U64;
7498 0 : return HCCL_SUCCESS;
7499 : }
7500 :
7501 0 : CHK_RET(localNotify->GetNotifyData(notifyInfo));
7502 0 : HCCL_INFO(
7503 : "[HcclCommunicator][GetAiCpuNotifyData]resId[%lld], addr[%lld], devId[%u], tsId[%u].", notifyInfo.resId,
7504 : notifyInfo.addr, notifyInfo.devId, notifyInfo.tsId);
7505 0 : return HCCL_SUCCESS;
7506 : }
7507 :
7508 : HcclResult
7509 0 : HcclCommunicator::CreateAndGetAiCpuNotify(std::shared_ptr<LocalNotify>& localNotify, HcclSignalInfo& notifyInfo)
7510 : {
7511 0 : if (localNotify != nullptr) {
7512 0 : CHK_RET(GetAiCpuNotifyData(localNotify, notifyInfo));
7513 0 : HCCL_INFO("[HcclCommunicator][CreateAndGetAiCpuNotify]aicpu notify already create ptr[%p]", localNotify->ptr());
7514 0 : return HCCL_SUCCESS;
7515 : }
7516 :
7517 0 : EXCEPTION_CATCH((localNotify = std::make_shared<LocalNotify>()), return HCCL_E_PTR);
7518 0 : CHK_RET(localNotify->Init(NotifyLoadType::DEVICE_NOTIFY));
7519 0 : CHK_RET(localNotify->SetIpc());
7520 :
7521 0 : CHK_RET(GetAiCpuNotifyData(localNotify, notifyInfo));
7522 0 : return HCCL_SUCCESS;
7523 : }
7524 :
7525 2 : HcclResult HcclCommunicator::Mc2AiCpuStreamAllocAndGet(u32 streamMode, rtStream_t& aiCpuStream)
7526 : {
7527 2 : if (opStream_.ptr() != nullptr) {
7528 0 : HCCL_INFO("%s already alloc, group:%s, stream id:%u", __func__, identifier_.c_str(), opStream_.id());
7529 0 : aiCpuStream = opStream_.ptr();
7530 0 : return HCCL_SUCCESS;
7531 : }
7532 :
7533 2 : constexpr u32 aicpuStreamMode = 1; // 单独申请的kernel流,使能遇错即停,避免出错后流卡住不退
7534 2 : opStream_ = Stream(StreamType::STREAM_TYPE_ONLINE);
7535 2 : CHK_RET(hrtStreamSetMode(opStream_.ptr(), aicpuStreamMode));
7536 2 : aiCpuStream = opStream_.ptr();
7537 2 : HCCL_RUN_INFO(
7538 : "%s alloc success, group:%s, stream id:%u, mainStreamMode:%u, aicpuStreamMode:%u", __func__,
7539 : identifier_.c_str(), opStream_.id(), streamMode, aicpuStreamMode);
7540 2 : return HCCL_SUCCESS;
7541 : }
7542 :
7543 2 : HcclResult HcclCommunicator::Mc2AiCpuInitStreamAllocAndGet(u32 streamMode, rtStream_t& aiCpuStream)
7544 : {
7545 2 : if (aicpuInitStream_.ptr() != nullptr) {
7546 1 : HCCL_INFO("%s already alloc, group:%s, stream id:%u", __func__, identifier_.c_str(), aicpuInitStream_.id());
7547 1 : aiCpuStream = aicpuInitStream_.ptr();
7548 1 : return HCCL_SUCCESS;
7549 : }
7550 :
7551 1 : constexpr u32 aicpuStreamMode = 1; // 单独申请的kernel流,使能遇错即停,避免出错后流卡住不退
7552 1 : aicpuInitStream_ = Stream(StreamType::STREAM_TYPE_ONLINE);
7553 1 : CHK_RET(hrtStreamSetMode(aicpuInitStream_.ptr(), aicpuStreamMode));
7554 1 : aiCpuStream = aicpuInitStream_.ptr();
7555 1 : HCCL_RUN_INFO(
7556 : "%s alloc success, group:%s, stream id:%u, mainStreamMode:%u, aicpuStreamMode:%u", __func__,
7557 : identifier_.c_str(), aicpuInitStream_.id(), streamMode, aicpuStreamMode);
7558 1 : return HCCL_SUCCESS;
7559 : }
7560 :
7561 0 : HcclResult HcclCommunicator::AicpuResourceInit(
7562 : const std::string& algName, const AlgResourceResponse& algResource, const std::string& newTag,
7563 : const rtStream_t& aicpuStream, const HcclCMDType opType, [[maybe_unused]] bool isCustom)
7564 : {
7565 0 : HCCL_RUN_INFO(
7566 : "[%s] start to init group[%s] aicpu resources newTag[%s] local rankId[%u]", __func__, identifier_.c_str(),
7567 : newTag.c_str(), userRank_);
7568 0 : isContextLaunched_ = true;
7569 0 : CHK_RET(BuildOpResParam(algName, algResource, newTag, opType, aicpuStream)); // 构建context结构体
7570 0 : std::string kernelName = "RunAicpuKfcResInitV2";
7571 : // 在这里构建suspending状态码的HDC通道初始化,并且在host侧进行init
7572 : // (这个主要是针对hcomId;对算子通信域的复用;也就是多个算子复用(tag+Identifier)这个通信域的情况)
7573 0 : CHK_RET(AiCpuKernelLaunch(aicpuStream, reinterpret_cast<u64>(opResDevicePara_.ptr()), kernelName));
7574 0 : SetMC2EnvFlag();
7575 0 : newTagResAlloced_.insert(newTag);
7576 : // 图模多档位场景,需要保证执行序上优先下资源初始化的kernel
7577 0 : CHK_RET(hcclStreamSynchronize(aicpuStream, commConfig_.GetConfigExecTimeOut()));
7578 :
7579 0 : if (IsEnableCustom()) {
7580 : struct InitTask {
7581 : u64 context; // A矩阵地址,通信在前时为sendbuffer
7582 : bool isCustom;
7583 : };
7584 0 : InitTask customInitTask = {};
7585 0 : customInitTask.context = reinterpret_cast<u64>(opResDevicePara_.ptr());
7586 0 : customInitTask.isCustom = true;
7587 0 : CHK_RET(BuildCustomOpResParam());
7588 0 : uint64_t customBeginTime = hrtMsprofSysCycleTime();
7589 0 : const std::string customProfName = "hcomAicpuCustomInit";
7590 :
7591 0 : u16 timeOut = 0;
7592 0 : if (opResPara_.config.notifyWaitTime == 0) {
7593 0 : timeOut = opResPara_.config.notifyWaitTime;
7594 0 : } else if (opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC >= MAX_VALUE_U16) {
7595 0 : timeOut = MAX_VALUE_U16;
7596 : } else {
7597 0 : timeOut = opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC;
7598 : }
7599 :
7600 0 : CHK_RET(AicpuAclKernelLaunch(
7601 : aicpuStream, static_cast<void*>(&customInitTask), sizeof(customInitTask), binCustomHandle_, kernelName,
7602 : true, timeOut));
7603 0 : uint64_t customEndTime = hrtMsprofSysCycleTime();
7604 0 : s32 customthreadId = SalGetTid();
7605 0 : CHK_RET(ProfilingManagerPub::CallMsprofReportNodeInfo(
7606 : customBeginTime, customEndTime, customProfName, customthreadId));
7607 0 : CHK_RET(hcclStreamSynchronize(aicpuStream, commConfig_.GetConfigExecTimeOut()));
7608 0 : }
7609 :
7610 0 : return HCCL_SUCCESS;
7611 0 : }
7612 :
7613 0 : HcclResult HcclCommunicator::AiCpuKernelLaunch(const rtStream_t stm, u64 addr, const std::string& kernelName)
7614 : {
7615 0 : uint64_t beginTime = hrtMsprofSysCycleTime();
7616 0 : const std::string profName = "hcomAicpuInit";
7617 : struct InitTask {
7618 : u64 context; // A矩阵地址,通信在前时为sendbuffer
7619 : bool isCustom;
7620 : };
7621 0 : InitTask initTask = {};
7622 0 : initTask.context = addr;
7623 0 : initTask.isCustom = false;
7624 :
7625 0 : u16 timeOut = 0;
7626 0 : if (opResPara_.config.notifyWaitTime == 0) {
7627 0 : timeOut = opResPara_.config.notifyWaitTime;
7628 0 : } else if (opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC >= MAX_VALUE_U16) {
7629 0 : timeOut = MAX_VALUE_U16;
7630 : } else {
7631 0 : timeOut = opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC;
7632 : }
7633 0 : CHK_RET(AicpuAclKernelLaunch(
7634 : stm, static_cast<void*>(&initTask), sizeof(initTask), binHandle_, kernelName, true, timeOut));
7635 0 : uint64_t endTime = hrtMsprofSysCycleTime();
7636 0 : s32 threadId = SalGetTid();
7637 0 : CHK_RET(ProfilingManagerPub::CallMsprofReportNodeInfo(beginTime, endTime, profName, threadId));
7638 0 : return HCCL_SUCCESS;
7639 0 : }
7640 :
7641 0 : HcclResult HcclCommunicator::AicpuKfcTilingDataLaunch(
7642 : const OpParam& opParam, const HcclCMDType& opType, const DeviceMem& deviceContext, const std::string& kernelName,
7643 : const AicpuOpTiling opTilingInfo)
7644 : {
7645 0 : HCCL_DEBUG(
7646 : "AicpuKfcTilingDataLaunch count %llu dataType %s op %s opType %u", opParam.GetDataCount(userRank_),
7647 : GetDataTypeEnumStr(opParam.GetDataType()).c_str(), GetReduceOpEnumStr(opParam.reduceType).c_str(), opType);
7648 0 : struct HcclKFCTilingData tilingDate = {};
7649 0 : tilingDate.sendCnt = opParam.DataDes.count;
7650 0 : tilingDate.dataType = opParam.DataDes.dataType;
7651 0 : tilingDate.commType = static_cast<uint8_t>(opType);
7652 0 : tilingDate.reduceOp = opParam.reduceType;
7653 0 : tilingDate.taskType = HCCL_KFC_TASK_HCCL_ONLY_EXE;
7654 0 : tilingDate.totalCnt = 1;
7655 0 : tilingDate.turnNum = 1;
7656 0 : tilingDate.hasCommOut = 1;
7657 0 : tilingDate.debugMode = 0;
7658 0 : CHK_RET(SetNormalMode(dispatcher_));
7659 0 : HcclWorkflowMode mode = GetWorkflowMode();
7660 0 : Stream mainStream(opParam.stream.ptr());
7661 0 : CHK_RET(LocalNotify::Post(
7662 : mainStream, dispatcher_, localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_0)],
7663 : INVALID_VALUE_STAGE));
7664 0 : rtStream_t kfcOpStream = opStream_.ptr();
7665 0 : if (opTilingInfo.isUsedMainStream) {
7666 0 : kfcOpStream = opParam.stream.ptr();
7667 : }
7668 0 : CHK_RET(AicpuUnfoldKernelLaunch(
7669 : opParam.inputPtr, opParam.outputPtr, kfcOpStream, reinterpret_cast<u64>(deviceContext.ptr()), &tilingDate,
7670 : sizeof(HcclKFCTilingData), kernelName, mode, opParam.tag));
7671 0 : CHK_RET(LocalNotify::Wait(
7672 : mainStream, dispatcher_, localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_1)],
7673 : INVALID_VALUE_STAGE));
7674 0 : return HCCL_SUCCESS;
7675 0 : }
7676 :
7677 6 : HcclResult HcclCommunicator::AicpuKfcClearOpResLaunch(const std::unordered_set<std::string>& tags)
7678 : {
7679 6 : if (tags.empty()) {
7680 1 : return HCCL_SUCCESS;
7681 : }
7682 : // 仅 aicpu unfold 模式有 aicpu 端 resMap_/linkRes_ 需要清理;host 模式下没有 binHandle_
7683 5 : if (binHandle_ == nullptr) {
7684 2 : HCCL_DEBUG(
7685 : "[AicpuKfcClearOpResLaunch] binHandle_ null (host-mode communicator), skip; tagCount[%zu]", tags.size());
7686 2 : return HCCL_SUCCESS;
7687 : }
7688 3 : if (opStream_.ptr() == nullptr) {
7689 1 : HCCL_WARNING("[AicpuKfcClearOpResLaunch] opStream_ null, skip aicpu cleanup; tagCount[%zu]", tags.size());
7690 1 : return HCCL_SUCCESS;
7691 : }
7692 : // host args 通道有 size 上限,大 payload 走 args/tiling 会被拒绝。沿用 RunAicpuKfcResInit 模式:HBM buffer 持载
7693 : // payload
7694 2 : if (!aicpuCleanupBuf_) {
7695 0 : CHK_RET(DeviceMem::alloc(aicpuCleanupBuf_, sizeof(HcclKfcClearOpResTilingData)));
7696 : }
7697 2 : if (!aicpuCleanupHostBuf_) {
7698 0 : aicpuCleanupHostBuf_.reset(new (std::nothrow) HcclKfcClearOpResTilingData());
7699 0 : CHK_SMART_PTR_NULL(aicpuCleanupHostBuf_);
7700 : }
7701 2 : HcclKfcClearOpResTilingData& payload = *aicpuCleanupHostBuf_;
7702 :
7703 : // 必须与 aicpu_kfc_def.h 中 KFCResInitTask 布局一致,aicpu 端按此解包
7704 : struct KFCResInitTask {
7705 : u64 context;
7706 : bool isCustom;
7707 : };
7708 2 : KFCResInitTask initTask = {reinterpret_cast<u64>(aicpuCleanupBuf_.ptr()), false};
7709 2 : const u16 timeOut = MAX_VALUE_U16;
7710 2 : const size_t groupCopyLen = std::min(identifier_.length() + 1, sizeof(payload.group));
7711 2 : size_t totalBatches = 0;
7712 :
7713 : // 分批 launch:同 buffer 复用,每批最多 MAX_BATCH 个 tag;launch 后 sync 保证 aicpu 完成才覆盖 buffer 下一批
7714 2 : auto it = tags.begin();
7715 5 : while (it != tags.end()) {
7716 3 : payload.magic = HCCL_KFC_CLEAR_OP_RES_MAGIC;
7717 3 : CHK_SAFETY_FUNC_RET(memcpy_s(payload.group, sizeof(payload.group), identifier_.c_str(), groupCopyLen));
7718 3 : payload.group[sizeof(payload.group) - 1] = '\0';
7719 :
7720 3 : u32 idx = 0;
7721 10255 : while (it != tags.end() && idx < HCCL_KFC_CLEAR_OP_RES_MAX_BATCH) {
7722 10252 : const std::string& t = *it;
7723 10252 : const size_t tagCopyLen = std::min(t.length() + 1, sizeof(payload.tags[idx]));
7724 10252 : CHK_SAFETY_FUNC_RET(memcpy_s(payload.tags[idx], sizeof(payload.tags[idx]), t.c_str(), tagCopyLen));
7725 10252 : payload.tags[idx][sizeof(payload.tags[idx]) - 1] = '\0';
7726 10252 : ++idx;
7727 10252 : ++it;
7728 : }
7729 3 : payload.tagCount = idx;
7730 :
7731 3 : CHK_RET(hrtMemSyncCopy(
7732 : aicpuCleanupBuf_.ptr(), sizeof(payload), static_cast<void*>(&payload), sizeof(payload),
7733 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
7734 :
7735 3 : HcclResult ret = AicpuAclKernelLaunchV2(
7736 : opStream_.ptr(), static_cast<void*>(&initTask), sizeof(initTask), binHandle_, "RunAicpuKfcClearOpRes", true,
7737 3 : timeOut, nullptr, 0, identifier_);
7738 3 : if (ret != HCCL_SUCCESS) {
7739 0 : HCCL_ERROR(
7740 : "[AicpuKfcClearOpResLaunch] launch fail, group[%s] batch[%zu] tagCount[%u] ret[%d]",
7741 : identifier_.c_str(), totalBatches, idx, ret);
7742 0 : return ret;
7743 : }
7744 3 : CHK_RET(hcclStreamSynchronize(opStream_.ptr(), commConfig_.GetConfigExecTimeOut()));
7745 3 : ++totalBatches;
7746 : }
7747 :
7748 2 : HCCL_INFO(
7749 : "[AicpuKfcClearOpResLaunch] dispatched aicpu cleanup, group[%s] totalTags[%zu] batches[%zu]",
7750 : identifier_.c_str(), tags.size(), totalBatches);
7751 2 : return HCCL_SUCCESS;
7752 : }
7753 :
7754 8 : HcclResult HcclCommunicator::AicpuInitOpTilingDataAicpuCache(
7755 : const OpParam& opParam, const HcclCMDType& opType, struct OpTilingData* opTilingData)
7756 : {
7757 8 : opTilingData->aicpuCacheEnable = opParam.aicpuCacheEnable;
7758 : // 开启aicpu cache, 且原来是图模式建链但强制走单算子模式展开
7759 : // 开启aicpu cache,isCapture为true,且是图模式,证明选择了aclgraph零拷贝算法,需要强制刷新cache
7760 5 : if (opParam.aicpuCacheEnable != 0 && GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB
7761 13 : && ((IsForceAicpuOpBaseMode(opParam, opType) && !opParam.isZeroCopy) || opParam.isCapture)) {
7762 : // 环境变量传入的aicpuCacheEnable一定 < 10
7763 1 : constexpr uint8_t FORCE_OP_BASE_DELTA = 10;
7764 1 : CHK_PRT_RET(
7765 : opParam.aicpuCacheEnable >= FORCE_OP_BASE_DELTA,
7766 : HCCL_ERROR(
7767 : "[HcclCommunicator][AicpuInitOpTilingDataBuf] enforce opbase mode: opParam.aicpuCacheEnable >= %u",
7768 : opParam.aicpuCacheEnable, FORCE_OP_BASE_DELTA),
7769 : HCCL_E_INTERNAL);
7770 :
7771 : // 1 -> 11: 开启aicpu cache且存在强制单算子模式转换
7772 1 : opTilingData->aicpuCacheEnable += FORCE_OP_BASE_DELTA;
7773 1 : HCCL_WARNING(
7774 : "[HcclCommunicator][AicpuInitOpTilingDataBuf] enforce opbase mode: opParam.aicpuCacheEnable[%u]"
7775 : "opTilingData->aicpuCacheEnable[%u]",
7776 : opParam.aicpuCacheEnable, opTilingData->aicpuCacheEnable);
7777 :
7778 : // 注意: 开启aicpu cache且存在强制单算子模式转换, 传入device的aicpuCacheEnable一定 > 10
7779 1 : CHK_PRT_RET(
7780 : opTilingData->aicpuCacheEnable <= FORCE_OP_BASE_DELTA,
7781 : HCCL_ERROR(
7782 : "[HcclCommunicator][AicpuInitOpTilingDataBuf] enforce opbase mode: opTilingData->aicpuCacheEnable[%u] "
7783 : "<= %u",
7784 : opTilingData->aicpuCacheEnable, FORCE_OP_BASE_DELTA),
7785 : HCCL_E_INTERNAL);
7786 : }
7787 :
7788 8 : return HCCL_SUCCESS;
7789 : }
7790 :
7791 1 : HcclResult HcclCommunicator::AicpuInitOpTilingDataBuf(
7792 : const OpParam& opParam, const HcclCMDType& opType, [[maybe_unused]] const std::string& kernelName,
7793 : const AicpuOpTiling opTilingInfo, u64 dynamicDataSize)
7794 : {
7795 1 : u32 opTilingDataSize = sizeof(struct OpTilingData) + dynamicDataSize;
7796 :
7797 1 : if (opTilingDataBuf_.ptr() == nullptr) {
7798 1 : opTilingDataBuf_ = HostMem::alloc(TILINGDATA_BUF_SIZE);
7799 1 : CHK_PRT_RET(
7800 : opTilingDataBuf_.ptr() == nullptr,
7801 : HCCL_ERROR("[HcclCommunicator][AicpuInitOpTilingDataBuf] Alloc opTilingDataBuf failed!"), HCCL_E_INTERNAL);
7802 : }
7803 :
7804 1 : if (opTilingDataBuf_.ptr() != nullptr && opTilingDataSize > opTilingDataBuf_.size()) {
7805 0 : opTilingDataBuf_.free();
7806 0 : opTilingDataBuf_ = HostMem::alloc(opTilingDataSize);
7807 0 : CHK_PRT_RET(
7808 : opTilingDataBuf_.ptr() == nullptr,
7809 : HCCL_ERROR(
7810 : "[HcclCommunicator][AicpuInitOpTilingDataBuf] in create opTilingDataBuf len[%llu] failed!",
7811 : opTilingDataSize),
7812 : HCCL_E_INTERNAL);
7813 : }
7814 :
7815 : // 填充固定内容
7816 1 : HostMem opTilingDataMem = opTilingDataBuf_.range(0, opTilingDataSize);
7817 1 : struct OpTilingData* opTilingData = static_cast<struct OpTilingData*>(opTilingDataMem.ptr());
7818 1 : u32 algTypeTranfer
7819 1 : = (static_cast<u32>(opTilingInfo.algType.algoLevel2) << (HCCL_LEVEL_ALGO_WIDTH + HCCL_LEVEL_ALGO_WIDTH))
7820 1 : + (static_cast<u32>(opTilingInfo.algType.algoLevel1) << HCCL_LEVEL_ALGO_WIDTH)
7821 1 : + static_cast<u32>(opTilingInfo.algType.algoLevel0);
7822 1 : opTilingData->algType = static_cast<u64>(algTypeTranfer);
7823 1 : opTilingData->floatOverflowMode = opTilingInfo.floatOverflowMode;
7824 1 : opTilingData->dumpDebug = opTilingInfo.dumpDebug;
7825 1 : CHK_RET(AicpuInitOpTilingDataFromOpParam(opParam, opType, opTilingData));
7826 1 : opTilingData->length = dynamicDataSize;
7827 1 : opTilingData->customDataLength = 0;
7828 1 : opTilingData->index = UpdateOpIndex(opParam);
7829 1 : opTilingData->debugMode = 0;
7830 1 : opTilingData->isZeroCopy = opParam.isZeroCopy;
7831 1 : opTilingData->isCapture = opParam.isCapture;
7832 1 : opTilingData->orderLaunchMode = GetOrderLaunchMode(opParam.isCapture);
7833 1 : opTilingData->isSymmetricMemory = opParam.supportSymmetricMemory;
7834 1 : opTilingData->needIncreLink = opParam.needIncreLink;
7835 : // 有没有存在对应的Notify
7836 1 : CHK_RET(InitAndCheckAicpuOrderNotify(opTilingData->orderLaunchMode));
7837 1 : CHK_RET(BuildHierarchicalAlgOption(opTilingData->ahcConfInfo));
7838 1 : CHK_RET(AicpuInitOpTilingDataAicpuCache(opParam, opType, opTilingData));
7839 :
7840 : // 填充动态内容
7841 1 : HostMem dynamicDataMem = opTilingDataBuf_.range(sizeof(struct OpTilingData), dynamicDataSize);
7842 1 : CHK_PTR_NULL(dynamicDataMem.ptr());
7843 1 : if (opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV) {
7844 : struct OpTilingBatchSendRecvDataDes* batchSendRecvDataPtr
7845 1 : = reinterpret_cast<struct OpTilingBatchSendRecvDataDes*>(dynamicDataMem.ptr());
7846 1 : batchSendRecvDataPtr->itemNum = opParam.BatchSendRecvDataDes.itemNum;
7847 2 : for (u32 i = 0; i < opParam.BatchSendRecvDataDes.itemNum; i++) {
7848 1 : CHK_PTR_NULL(opParam.BatchSendRecvDataDes.sendRecvItemsPtr + i);
7849 1 : batchSendRecvDataPtr->batchSendRecvItem[i] = *(opParam.BatchSendRecvDataDes.sendRecvItemsPtr + i);
7850 : }
7851 :
7852 1 : u8* isDirectRemoteRankPtr
7853 1 : = reinterpret_cast<u8*>(batchSendRecvDataPtr->batchSendRecvItem + opParam.BatchSendRecvDataDes.itemNum);
7854 3 : for (u32 i = 0; i < userRankSize_; i++) {
7855 2 : CHK_PTR_NULL(isDirectRemoteRankPtr + i);
7856 2 : isDirectRemoteRankPtr[i] = *(opParam.BatchSendRecvDataDes.isDirectRemoteRank + i);
7857 : }
7858 0 : } else if (opType == HcclCMDType::HCCL_CMD_ALLTOALL) {
7859 0 : CHK_RET(SetDynamicTilingDataAlltoall(opParam, dynamicDataMem));
7860 0 : } else if (opType == HcclCMDType::HCCL_CMD_ALLTOALLV) {
7861 0 : CHK_RET(SetDynamicTilingDataAlltoallv(opParam, dynamicDataMem, opTilingInfo.algName));
7862 0 : } else if (opType == HcclCMDType::HCCL_CMD_ALLTOALLVC) {
7863 0 : CHK_RET(SetDynamicTilingDataAlltoallvc(opParam, dynamicDataMem));
7864 0 : } else if (opType == HcclCMDType::HCCL_CMD_ALLGATHER_V || opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V) {
7865 0 : CHK_RET(SetDynamicTilingDataV(opParam, dynamicDataMem));
7866 0 : } else {
7867 0 : struct OpTilingDataDes* opDataDesPtr = reinterpret_cast<struct OpTilingDataDes*>(dynamicDataMem.ptr());
7868 0 : opDataDesPtr->count = opParam.DataDes.count;
7869 0 : opDataDesPtr->dataType = static_cast<u8>(opParam.DataDes.dataType);
7870 : }
7871 :
7872 1 : HCCL_INFO("[HcclCommunicator][AicpuInitOpTilingDataBuf]algType[%lu]", opTilingData->algType);
7873 1 : CHK_SAFETY_FUNC_RET(memcpy_s(
7874 : opTilingData->algName, sizeof(opTilingData->algName), opTilingInfo.algName.c_str(),
7875 : opTilingInfo.algName.length() + 1));
7876 1 : CHK_SAFETY_FUNC_RET(memcpy_s(
7877 : opTilingData->newTag, sizeof(opTilingData->newTag), opTilingInfo.newTag.c_str(),
7878 : opTilingInfo.newTag.length() + 1));
7879 1 : CHK_SAFETY_FUNC_RET(
7880 : memcpy_s(opTilingData->tag, sizeof(opTilingData->tag), opParam.tag.c_str(), opParam.tag.length() + 1));
7881 1 : return HCCL_SUCCESS;
7882 1 : }
7883 :
7884 0 : u8 HcclCommunicator::GetOrderLaunchMode(bool isCapture)
7885 : {
7886 : bool isSupportHcomAttachedStream
7887 0 : = !(attachedStreams_.empty() || attachedStreams_[0].ptr() == nullptr); // true 表示图模式下成功申请附属从流
7888 0 : u8 orderLaunchMode = 0;
7889 0 : HcclWorkflowMode mode = GetWorkflowMode();
7890 0 : if (isCapture) {
7891 0 : orderLaunchMode = static_cast<u8>(AicpuNotifyMode::ACLGRAPH_MODE);
7892 0 : } else if (mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
7893 0 : orderLaunchMode = static_cast<u8>(AicpuNotifyMode::OPBASE_MODE);
7894 0 : } else if (mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB && isSupportHcomAttachedStream) {
7895 0 : orderLaunchMode = static_cast<u8>(AicpuNotifyMode::HCOM_MODE);
7896 : } else {
7897 0 : orderLaunchMode = AICPU_ORDERLAUNCH_INVALID_HCOM_MODE;
7898 : }
7899 :
7900 0 : return orderLaunchMode;
7901 : }
7902 :
7903 0 : HcclResult HcclCommunicator::InitAndCheckAicpuOrderNotify(u8& orderLaunchMode)
7904 : {
7905 0 : if (orderLaunchMode == AICPU_ORDERLAUNCH_INVALID_HCOM_MODE) {
7906 0 : HCCL_INFO("[HcclCommunicator][InitAndCheckAicpuOrderNotify] orderLaunchMode is invalid in hcom "
7907 : "for there is no attached stream included in this operator!");
7908 0 : return HCCL_SUCCESS;
7909 : }
7910 : u32 idx0;
7911 : u32 idx1;
7912 0 : if (orderLaunchMode == 0) {
7913 0 : idx0 = static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_OPBASE_0);
7914 0 : idx1 = static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_OPBASE_1);
7915 0 : } else if (orderLaunchMode == 1) {
7916 0 : idx0 = static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_ACLGRAPH_0);
7917 0 : idx1 = static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_ACLGRAPH_1);
7918 : } else {
7919 0 : idx0 = static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_HCOM_0);
7920 0 : idx1 = static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_HCOM_1);
7921 : }
7922 :
7923 0 : if (localAiCpuOpNotify_[idx0] != nullptr) {
7924 0 : HCCL_INFO("[%s], the orderNotify of orderLaunchMode [%u] is available", __func__, orderLaunchMode);
7925 0 : return HCCL_SUCCESS;
7926 : }
7927 : HcclSignalInfo orderSignalInfo0;
7928 0 : HcclResult ret = CreateAndGetAiCpuNotify(localAiCpuOpNotify_[idx0], orderSignalInfo0);
7929 0 : CHK_PRT_RET(
7930 : ret != HCCL_SUCCESS,
7931 : HCCL_ERROR(
7932 : "[HcclCommunicator][InitAndCheckAicpuOrderNotify]get aicpu notify [%u] errorCode[%u]", idx0,
7933 : HCCL_ERROR_CODE(ret)),
7934 : ret);
7935 :
7936 : // 按序下发(aicpu控制流 record host控制流) 使用的notify信息
7937 : HcclSignalInfo orderSignalInfo1;
7938 0 : ret = CreateAndGetAiCpuNotify(localAiCpuOpNotify_[idx1], orderSignalInfo1);
7939 0 : CHK_PRT_RET(
7940 : ret != HCCL_SUCCESS,
7941 : HCCL_ERROR(
7942 : "[HcclCommunicator][InitAndCheckAicpuOrderNotify]get aicpu notify [%u] errorCode[%u]", idx1,
7943 : HCCL_ERROR_CODE(ret)),
7944 : ret);
7945 0 : HCCL_INFO(
7946 : "[HcclCommunicator][InitAndCheckAicpuOrderNotify] ORDER INDEX 0: resId[%u], ORDER INDEX 1: resId[%u]",
7947 : orderSignalInfo0.resId, orderSignalInfo1.resId);
7948 :
7949 0 : CHK_RET(hrtMemSyncCopy(
7950 : static_cast<char*>(aicpuOrderNotifyAddr_.ptr()) + (sizeof(HcclSignalInfo) * orderLaunchMode),
7951 : sizeof(HcclSignalInfo), &orderSignalInfo1, sizeof(HcclSignalInfo),
7952 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
7953 :
7954 0 : return HCCL_SUCCESS;
7955 : }
7956 :
7957 4 : HcclResult HcclCommunicator::AicpuKfcTilingDataLaunchIn(
7958 : const OpParam& opParam, const DeviceMem& deviceContext, const std::string& kernelName,
7959 : [[maybe_unused]] const AicpuOpTiling opTilingInfo, u64 opTilingDataSize, bool isCustom)
7960 : {
7961 4 : HostMem opTilingDataMem = opTilingDataBuf_.range(0, opTilingDataSize);
7962 4 : CHK_RET(SetNormalMode(dispatcher_));
7963 2 : Stream& mainStream = const_cast<Stream&>(opParam.stream);
7964 2 : CHK_RET(LocalNotify::Post(
7965 : mainStream, dispatcher_, localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_0)],
7966 : INVALID_VALUE_STAGE));
7967 :
7968 2 : Stream kfcOpStream;
7969 2 : HcclWorkflowMode mode = GetWorkflowMode();
7970 : bool isSupportHcomAttachedStream
7971 2 : = !(attachedStreams_.empty() || attachedStreams_[0].ptr() == nullptr); // true 表示图模式下成功申请附属从流
7972 2 : if (opParam.isCapture || mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
7973 2 : kfcOpStream = opStream_;
7974 : } else {
7975 : // 如果是图模式,则尝试从附属从流中获取一下stream,如果能拿到则使用,否则退化
7976 0 : if (isSupportHcomAttachedStream) {
7977 0 : HCCL_INFO("[HcclCommunicator][AicpuKfcTilingDataLaunchIn] attachedStreams_ is valid in graph mode");
7978 0 : kfcOpStream = attachedStreams_[0];
7979 : } else {
7980 0 : HCCL_INFO("[HcclCommunicator][AicpuKfcTilingDataLaunchIn] attachedStreams_ is invalid in graph mode");
7981 0 : kfcOpStream = opParam.stream;
7982 : }
7983 : }
7984 2 : uint64_t beginTime = hrtMsprofSysCycleTime();
7985 2 : std::string profName = GetCMDTypeEnumStr(opParam.opType);
7986 2 : if (profName == "Invalid HcclCMDType" || profName == "invalid") {
7987 0 : profName = "HcclOpAicpuKernel";
7988 : } else {
7989 2 : profName += "AicpuKernel";
7990 : }
7991 2 : s32 streamId = kfcOpStream.id();
7992 0 : auto getAicpuTaskExceptionCallBack = [this]() {
7993 0 : return this->GetAicpuTaskException();
7994 2 : };
7995 2 : RegisterGetAicpuTaskExceptionCallBack(streamId, deviceLogicId_, getAicpuTaskExceptionCallBack);
7996 2 : aicpuStreamIds_.insert(streamId);
7997 2 : if (streamId != opParam.stream.id()) {
7998 0 : RegisterGetAicpuTaskExceptionCallBack(opParam.stream.id(), deviceLogicId_, getAicpuTaskExceptionCallBack);
7999 0 : aicpuStreamIds_.insert(opParam.stream.id());
8000 : }
8001 :
8002 2 : HCCL_INFO(
8003 : "%s profName[%s] tag[%s] kfcOpStreamId[%d] mainStreamId[%u] kfcStreamId[%d] isCapture[%d] mode[%d] ", __func__,
8004 : profName.c_str(), opParam.tag.c_str(), streamId, opParam.stream.id(), opStream_.id(), opParam.isCapture, mode);
8005 :
8006 2 : if (opParam.isCapture) { // 非主流下发时,acl graph场景,capture从流
8007 : // mainStream -> kernelStream, kernel流通过event capture入图
8008 2 : aclError ret = ACL_SUCCESS;
8009 2 : HcclRtEvent event = localAicpuOpEvent_[static_cast<u32>(AicpuLocalEventIdx::KERNEL_INDEX_ACLGRAPH_EVENT_0)];
8010 2 : ret = aclrtRecordEvent(event, opParam.stream.ptr());
8011 2 : CHK_PRT_RET(
8012 : ret != ACL_SUCCESS, HCCL_ERROR("[%s]aclrtRecordEvent failed, ret[%d]", __func__, ret), HCCL_E_RUNTIME);
8013 1 : HCCL_CONFIG_INFO(HCCL_TASK, "[%s]aclrtRecordEvent para: opParam.stream[%d]", __func__, opParam.stream.id());
8014 1 : ret = aclrtStreamWaitEvent(kfcOpStream.ptr(), event);
8015 1 : CHK_PRT_RET(
8016 : ret != ACL_SUCCESS, HCCL_ERROR("[%s]aclrtStreamWaitEvent failed, ret[%d]", __func__, ret), HCCL_E_RUNTIME);
8017 1 : HCCL_CONFIG_INFO(HCCL_TASK, "[%s]aclrtStreamWaitEvent para: kfcOpStream[%d]", __func__, kfcOpStream.id());
8018 1 : HCCL_INFO(
8019 : "[HcclCommunicator][%s]tag[%s], capture kfcOpStream, mainStream[%d] recordEvent, kfcOpStream[%d] "
8020 : "waitEvent.",
8021 : __func__, opParam.tag.c_str(), opParam.stream.id(), kfcOpStream.id());
8022 : }
8023 :
8024 1 : u32 timeOut = (opResPara_.config.notifyWaitTime == 0) ? opResPara_.config.notifyWaitTime :
8025 0 : (opResPara_.config.notifyWaitTime + AICPU_H2D_TIMEOUT_INC);
8026 1 : OrderLaunch& orderLaunch = OrderLaunch::GetInstance(deviceLogicId_);
8027 1 : std::shared_ptr<LocalNotify> notify0;
8028 1 : std::shared_ptr<LocalNotify> notify1;
8029 1 : if (opParam.isCapture) {
8030 1 : notify0 = localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_ACLGRAPH_0)];
8031 1 : notify1 = localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_ACLGRAPH_1)];
8032 1 : HcclRtEvent event0 = localAicpuOpEvent_[static_cast<u32>(AicpuLocalEventIdx::ORDER_INDEX_ACLGRAPH_EVENT_0)];
8033 1 : CHK_RET(orderLaunch.AclgraphLaunchInOrderToOrderStream(
8034 : identifier_, kfcOpStream, notify0, notify1, timeOut, event0));
8035 0 : } else if (mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
8036 0 : notify0 = localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_OPBASE_0)];
8037 0 : notify1 = localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_OPBASE_1)];
8038 0 : CHK_RET(orderLaunch.OpbaseLaunchInOrder(identifier_, kfcOpStream, notify0, notify1, timeOut));
8039 0 : } else if (mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB && isSupportHcomAttachedStream) {
8040 0 : notify0 = localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_HCOM_0)];
8041 0 : notify1 = localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_HCOM_1)];
8042 0 : CHK_RET(orderLaunch.HcomLaunchInOrder(identifier_, kfcOpStream, graphId_, notify0, notify1, timeOut));
8043 : }
8044 1 : CHK_RET(KernelLaunchChooseAicpuOrCustom(
8045 : opParam.inputPtr, opParam.outputPtr, kfcOpStream.ptr(), reinterpret_cast<u64>(deviceContext.ptr()),
8046 : opTilingDataMem.ptr(), opTilingDataSize, kernelName, mode, opParam.tag, isCustom));
8047 1 : if (opParam.isCapture) {
8048 1 : HcclRtEvent event1 = localAicpuOpEvent_[static_cast<u32>(AicpuLocalEventIdx::ORDER_INDEX_ACLGRAPH_EVENT_1)];
8049 1 : CHK_RET(orderLaunch.AclgraphLaunchInOrderToKernelStream(identifier_, kfcOpStream, event1));
8050 : // kernelStream -> mainStream
8051 1 : aclError ret = ACL_SUCCESS;
8052 1 : HcclRtEvent event = localAicpuOpEvent_[static_cast<u32>(AicpuLocalEventIdx::KERNEL_INDEX_ACLGRAPH_EVENT_1)];
8053 1 : ret = aclrtRecordEvent(event, kfcOpStream.ptr());
8054 1 : CHK_PRT_RET(
8055 : ret != ACL_SUCCESS, HCCL_ERROR("[%s]aclrtRecordEvent failed, ret[%d]", __func__, ret), HCCL_E_RUNTIME);
8056 1 : HCCL_CONFIG_INFO(HCCL_TASK, "[%s]aclrtRecordEvent para: kfcOpStream[%d]", __func__, kfcOpStream.id());
8057 1 : ret = aclrtStreamWaitEvent(opParam.stream.ptr(), event);
8058 1 : CHK_PRT_RET(
8059 : ret != ACL_SUCCESS, HCCL_ERROR("[%s]aclrtStreamWaitEvent failed, ret[%d]", __func__, ret), HCCL_E_RUNTIME);
8060 1 : HCCL_CONFIG_INFO(HCCL_TASK, "[%s]aclrtStreamWaitEvent para: opParam.stream[%d]", __func__, opParam.stream.id());
8061 1 : HCCL_INFO(
8062 : "[HcclCommunicator][%s]tag[%s], capture kfcOpStream, mainStream[%d] waitEvent, kfcOpStream[%d] "
8063 : "recordEvent.",
8064 : __func__, opParam.tag.c_str(), opParam.stream.id(), kfcOpStream.id());
8065 : }
8066 :
8067 1 : uint64_t endTime = hrtMsprofSysCycleTime();
8068 1 : s32 threadId = SalGetTid();
8069 1 : CHK_RET(ProfilingManagerPub::CallMsprofReportNodeInfo(beginTime, endTime, profName, threadId));
8070 1 : CHK_RET(LocalNotify::Wait(
8071 : mainStream, dispatcher_, localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_1)],
8072 : INVALID_VALUE_STAGE, timeOut));
8073 1 : return HCCL_SUCCESS;
8074 4 : }
8075 :
8076 0 : HcclResult HcclCommunicator::SetAttachedStream(u32 graphId, const std::vector<rtStream_t>& streams)
8077 : {
8078 0 : constexpr u32 GRAPH_ATTACHED_STREAM_INDEX = 0; // 图粒度的附属从流
8079 0 : constexpr u32 GROUP_ATTACHED_STREAM_INDEX = 1; // 通信域粒度的附属从流
8080 :
8081 : // 在图模式下,通信使用的附属从流可能不同,所以这里直接刷新所有
8082 0 : attachedStreams_.clear();
8083 :
8084 0 : bool isValid = !streams.empty() && (streams.size() > GROUP_ATTACHED_STREAM_INDEX)
8085 0 : && streams[GRAPH_ATTACHED_STREAM_INDEX] != nullptr
8086 0 : && streams[GROUP_ATTACHED_STREAM_INDEX] != nullptr;
8087 0 : if (!isValid) {
8088 0 : HCCL_ERROR("%s Invalid stream configuration, streams vector is null or invalid", __func__);
8089 0 : return HCCL_E_NOT_FOUND;
8090 : }
8091 :
8092 : // 向GE申请流的时候,图粒度的流排在第一个,所以在streams列表中,第一条流是图粒度的附属从流
8093 0 : s32 graphAttachedStreamId = 0;
8094 0 : OrderLaunch& orderLaunch = OrderLaunch::GetInstance(deviceLogicId_);
8095 0 : auto& graphStream = streams[GRAPH_ATTACHED_STREAM_INDEX];
8096 0 : CHK_RET(hrtGetStreamId(graphStream, graphAttachedStreamId));
8097 0 : orderLaunch.SetHcomStream(graphId, Stream(graphStream, false));
8098 0 : graphId_ = graphId;
8099 :
8100 : // 设置通信域粒度流
8101 0 : auto& groupStream = streams[GROUP_ATTACHED_STREAM_INDEX];
8102 0 : attachedStreams_.emplace_back(Stream(groupStream, false));
8103 :
8104 0 : HCCL_INFO(
8105 : "%s Streams configured graph[%u], graphAttachedStreamId[%d], group[%u],"
8106 : "groupStreamId[%u], graphId[%u], groupId[%s]",
8107 : __func__, GRAPH_ATTACHED_STREAM_INDEX, graphAttachedStreamId, GROUP_ATTACHED_STREAM_INDEX,
8108 : attachedStreams_.back().id(), graphId, identifier_.c_str());
8109 :
8110 0 : return HCCL_SUCCESS;
8111 : }
8112 :
8113 4 : HcclResult HcclCommunicator::AicpuKfcTilingDataLaunchExt(
8114 : const OpParam& opParam, const HcclCMDType& opType, const DeviceMem& deviceContext, const std::string& kernelName,
8115 : const AicpuOpTiling opTilingInfo, bool isCustom)
8116 : {
8117 4 : const u64 dataCount = opParam.GetDataCount(userRank_);
8118 4 : const HcclDataType dataType = opParam.GetDataType();
8119 4 : HCCL_DEBUG(
8120 : "AicpuKfcTilingDataLaunchExt count %llu dataType %s op %s opType %u retryEnable_ %d, "
8121 : "inPlaceSupportRetryStatus_ %d",
8122 : dataCount, GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(opParam.reduceType).c_str(), opType,
8123 : retryEnable_, inPlaceSupportRetryStatus_);
8124 :
8125 4 : bool postSyncEnable = false;
8126 4 : u32 severNum4PostSync = 4;
8127 1 : bool needPostSync = (superPodNum_ > 1 || serverNum_ >= severNum4PostSync)
8128 5 : && postSyncEnable; // reduce/reduce scatter算子是否需要PostSync
8129 4 : if (opType == HcclCMDType::HCCL_CMD_ALLREDUCE && retryEnable_
8130 1 : && (inPlaceSupportRetryStatus_ == InplaceSupportRetryStatus::USER_LARGER_THAN_CCL) && (!opParam.isZeroCopy)) {
8131 1 : u32 itemNum = 2;
8132 3 : for (u32 i = 0; i < itemNum; i++) {
8133 2 : if (i == 0) {
8134 1 : isInplacePreSync_ = true;
8135 : } else {
8136 1 : isInplacePreSync_ = false;
8137 : }
8138 2 : HCCL_DEBUG("[AicpuKfcTilingDataLaunchExt][PreSync]The op with isInplacePreSync_[%d].", isInplacePreSync_);
8139 2 : u64 dynamicDataSize = CalcOpTilingDynamicDataSize(opParam, opType, GetRankSize(), opTilingInfo.algName);
8140 2 : CHK_RET(AicpuInitOpTilingDataBuf(opParam, opType, kernelName, opTilingInfo, dynamicDataSize));
8141 2 : CHK_RET(AicpuKfcTilingDataLaunchIn(
8142 : opParam, deviceContext, kernelName, opTilingInfo, sizeof(struct OpTilingData) + dynamicDataSize,
8143 : isCustom));
8144 2 : isInplacePreSync_ = false;
8145 : }
8146 4 : } else if (opType == HcclCMDType::HCCL_CMD_REDUCE && retryEnable_ && needPostSync && (!opParam.isZeroCopy)) {
8147 0 : isPostSync_ = true;
8148 0 : HCCL_DEBUG("[AicpuKfcTilingDataLaunchExt][PreSync]The op with isPostSync_[%d].", isPostSync_);
8149 0 : u64 dynamicDataSize = CalcOpTilingDynamicDataSize(opParam, opType, GetRankSize(), opTilingInfo.algName);
8150 0 : CHK_RET(AicpuInitOpTilingDataBuf(opParam, opType, kernelName, opTilingInfo, dynamicDataSize));
8151 0 : CHK_RET(AicpuKfcTilingDataLaunchIn(
8152 : opParam, deviceContext, kernelName, opTilingInfo, sizeof(struct OpTilingData) + dynamicDataSize, isCustom));
8153 0 : isPostSync_ = false;
8154 3 : } else if (retryEnable_ && opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER && (!opParam.isZeroCopy)) {
8155 1 : if (inPlaceSupportRetryStatus_ == InplaceSupportRetryStatus::USER_LARGER_THAN_CCL) {
8156 1 : isInplacePreSync_ = true;
8157 1 : HCCL_DEBUG("[AicpuKfcTilingDataLaunchExt][PreSync]The op with isInplacePreSync_[%d].", isInplacePreSync_);
8158 1 : u64 dynamicDataSize = CalcOpTilingDynamicDataSize(opParam, opType, GetRankSize(), opTilingInfo.algName);
8159 1 : CHK_RET(AicpuInitOpTilingDataBuf(opParam, opType, kernelName, opTilingInfo, dynamicDataSize));
8160 1 : CHK_RET(AicpuKfcTilingDataLaunchIn(
8161 : opParam, deviceContext, kernelName, opTilingInfo, sizeof(struct OpTilingData) + dynamicDataSize,
8162 : isCustom));
8163 1 : isInplacePreSync_ = false;
8164 : }
8165 1 : isInplacePreSync_ = false;
8166 1 : if (needPostSync) {
8167 0 : isPostSync_ = true;
8168 : }
8169 1 : HCCL_DEBUG(
8170 : "[AicpuKfcTilingDataLaunchExt][PreSync]The op with "
8171 : "isInplacePreSync_[%d], isPostSync_[%d].",
8172 : isInplacePreSync_, isPostSync_);
8173 1 : u64 dynamicDataSize = CalcOpTilingDynamicDataSize(opParam, opType, GetRankSize(), opTilingInfo.algName);
8174 1 : CHK_RET(AicpuInitOpTilingDataBuf(opParam, opType, kernelName, opTilingInfo, dynamicDataSize));
8175 1 : CHK_RET(AicpuKfcTilingDataLaunchIn(
8176 : opParam, deviceContext, kernelName, opTilingInfo, sizeof(struct OpTilingData) + dynamicDataSize, isCustom));
8177 1 : isPostSync_ = false;
8178 1 : } else if (
8179 2 : retryEnable_
8180 2 : && (opType == HcclCMDType::HCCL_CMD_ALLTOALL || opType == HcclCMDType::HCCL_CMD_ALLTOALLV
8181 1 : || opType == HcclCMDType::HCCL_CMD_ALLTOALLVC)
8182 1 : && (!opParam.isZeroCopy)) {
8183 1 : isPostSync_ = postSyncEnable;
8184 1 : HCCL_DEBUG(
8185 : "[AicpuKfcTilingDataLaunchExt][PreSync]The op with "
8186 : "isInplacePreSync_[%d], isPostSync_[%d].",
8187 : isInplacePreSync_, isPostSync_);
8188 1 : u64 dynamicDataSize = CalcOpTilingDynamicDataSize(opParam, opType, GetRankSize(), opTilingInfo.algName);
8189 1 : CHK_RET(AicpuInitOpTilingDataBuf(opParam, opType, kernelName, opTilingInfo, dynamicDataSize));
8190 1 : CHK_RET(AicpuKfcTilingDataLaunchIn(
8191 : opParam, deviceContext, kernelName, opTilingInfo, sizeof(struct OpTilingData) + dynamicDataSize, isCustom));
8192 1 : isPostSync_ = false;
8193 1 : } else {
8194 1 : u64 dynamicDataSize = CalcOpTilingDynamicDataSize(opParam, opType, GetRankSize(), opTilingInfo.algName);
8195 1 : HCCL_DEBUG("[AicpuKfcTilingDataLaunchExt]dynamicDataSize[%u]", dynamicDataSize);
8196 1 : CHK_RET(AicpuInitOpTilingDataBuf(opParam, opType, kernelName, opTilingInfo, dynamicDataSize));
8197 1 : CHK_RET(AicpuKfcTilingDataLaunchIn(
8198 : opParam, deviceContext, kernelName, opTilingInfo, sizeof(struct OpTilingData) + dynamicDataSize, isCustom));
8199 : }
8200 :
8201 4 : return HCCL_SUCCESS;
8202 : }
8203 :
8204 0 : HcclResult HcclCommunicator::AicpuUnfoldKernelLaunch(
8205 : void* inputPtr, void* outputPtr, const rtStream_t stm, u64 addr, void* tilingDataPtr, u32 tilingDataSize,
8206 : const std::string& kernelName, [[maybe_unused]] HcclWorkflowMode mode, [[maybe_unused]] const std::string& tag)
8207 : {
8208 : struct ApiParamDef {
8209 : uint64_t x1; // 算子sendbuffer地址
8210 : uint64_t y = 0;
8211 : uint64_t gatherOut; // 算子recvbuffer地址
8212 : uint64_t context; // 通信资源准备的地址
8213 : uint64_t workspace; // 消息区地址
8214 : };
8215 :
8216 0 : struct ApiParamDef apiParam;
8217 0 : apiParam.x1 = reinterpret_cast<uint64_t>(inputPtr);
8218 0 : apiParam.gatherOut = reinterpret_cast<uint64_t>(outputPtr);
8219 0 : apiParam.context = addr;
8220 0 : apiParam.workspace = reinterpret_cast<uint64_t>(workSpace_.ptr());
8221 0 : u16 timeOut = 0;
8222 0 : if (opResPara_.config.notifyWaitTime == 0) {
8223 0 : timeOut = opResPara_.config.notifyWaitTime;
8224 0 : } else if (opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC >= MAX_VALUE_U16) {
8225 0 : timeOut = MAX_VALUE_U16;
8226 : } else {
8227 0 : timeOut = opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC;
8228 : }
8229 0 : CHK_PRT(AicpuAclKernelLaunch(
8230 : stm, static_cast<void*>(&apiParam), sizeof(apiParam), binHandle_, kernelName, false, timeOut, tilingDataPtr,
8231 : tilingDataSize));
8232 0 : HCCL_INFO("[HcclCommunicator][AicpuUnfoldKernelLaunch] exec succ.");
8233 0 : return HCCL_SUCCESS;
8234 : }
8235 :
8236 0 : HcclResult HcclCommunicator::AicpuUnfoldKernelLaunchV2(
8237 : [[maybe_unused]] void* inputPtr, [[maybe_unused]] void* outputPtr, const rtStream_t stm, u64 addr,
8238 : void* tilingDataPtr, u32 tilingDataSize, const std::string& kernelName, [[maybe_unused]] HcclWorkflowMode mode,
8239 : [[maybe_unused]] const std::string& tag, bool isCustom)
8240 : {
8241 0 : u64 context = addr;
8242 0 : HCCL_INFO("[HcclCommunicator]context[%p] tilingDataPtr[%p] tilingData[%p]", context, tilingDataPtr, tilingDataSize);
8243 :
8244 0 : aclrtBinHandle binHandle = isCustom ? binCustomHandle_ : binHandle_;
8245 0 : if (binHandle == nullptr) {
8246 0 : HCCL_ERROR("[AicpuUnfoldKernelLaunchV2]isCustom[%d] binHandle is nullptr, please check.", isCustom);
8247 0 : return HCCL_E_NOT_SUPPORT;
8248 : }
8249 0 : u16 timeOut = 0;
8250 0 : if (opResPara_.config.notifyWaitTime == 0) {
8251 0 : timeOut = opResPara_.config.notifyWaitTime;
8252 0 : } else if (opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC >= MAX_VALUE_U16) {
8253 0 : timeOut = MAX_VALUE_U16;
8254 : } else {
8255 0 : timeOut = opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC;
8256 : }
8257 0 : HcclResult ret = AicpuAclKernelLaunchV2(
8258 : stm, static_cast<void*>(&context), sizeof(context), binHandle, kernelName, false, timeOut, tilingDataPtr,
8259 0 : tilingDataSize, identifier_);
8260 0 : CHK_PRT_RET(
8261 : ret != HCCL_SUCCESS,
8262 : HCCL_ERROR("[HcclCommunicator][AicpuUnfoldKernelLaunchV2]isCustom[%d] binHandle[%p]", isCustom, binHandle),
8263 : ret);
8264 0 : HCCL_INFO("[HcclCommunicator][AicpuUnfoldKernelLaunchV2] exec succ, isCustom[%d].", isCustom);
8265 0 : return HCCL_SUCCESS;
8266 : }
8267 :
8268 490 : HcclResult HcclCommunicator::InitCombinOpara()
8269 : {
8270 490 : if (combinOparaMem_ == nullptr) {
8271 491 : CHK_RET(AllocAndClearHostMem(sizeof(HcclCombinOpParam), combinOparaMem_));
8272 : }
8273 491 : CHK_PTR_NULL(combinOparaMem_);
8274 491 : HcclCombinOpParam* combinOparaPtr = reinterpret_cast<HcclCombinOpParam*>(combinOparaMem_->ptr());
8275 489 : CHK_PTR_NULL(combinOparaPtr);
8276 :
8277 489 : if (aiRMAInfoMem_ == nullptr) {
8278 489 : CHK_RET(AllocAndClearHostMem(sizeof(HcclAiRMAInfo), aiRMAInfoMem_));
8279 : }
8280 488 : if (rmaInfoMem_ == nullptr) {
8281 488 : CHK_RET(AllocAndClearHostMem(sizeof(HcclRMAInfo), rmaInfoMem_));
8282 : }
8283 489 : CHK_PTR_NULL(aiRMAInfoMem_);
8284 489 : CHK_PTR_NULL(aiRMAInfoMem_->ptr());
8285 489 : CHK_PTR_NULL(rmaInfoMem_);
8286 489 : CHK_PTR_NULL(rmaInfoMem_->ptr());
8287 :
8288 489 : CHK_SAFETY_FUNC_RET(memset_s(combinOparaPtr, sizeof(HcclCombinOpParam), 0, sizeof(HcclCombinOpParam)));
8289 :
8290 491 : combinOparaPtr->rankId = INVALID_UINT;
8291 491 : combinOparaPtr->signalInfo.aicpuNotify.rankId = INVALID_UINT;
8292 :
8293 491 : for (u32 i = 0;
8294 31910 : i < sizeof(combinOparaPtr->signalInfo.noIpcNotifys) / sizeof(combinOparaPtr->signalInfo.noIpcNotifys[0]);
8295 : i++) {
8296 31419 : combinOparaPtr->signalInfo.noIpcNotifys[i].rankId = INVALID_UINT;
8297 : }
8298 :
8299 491 : for (u32 i = 0;
8300 63263 : i < sizeof(combinOparaPtr->signalInfo.ipcNotifys) / sizeof(combinOparaPtr->signalInfo.ipcNotifys[0]); i++) {
8301 62772 : combinOparaPtr->signalInfo.ipcNotifys[i].rankId = INVALID_UINT;
8302 : }
8303 :
8304 491 : for (u32 i = 0;
8305 16203 : i < sizeof(combinOparaPtr->signalInfo.noIpcEvents) / sizeof(combinOparaPtr->signalInfo.noIpcEvents[0]); i++) {
8306 15712 : combinOparaPtr->signalInfo.noIpcEvents[i].rankId = INVALID_UINT;
8307 : }
8308 491 : return HCCL_SUCCESS;
8309 : }
8310 :
8311 7 : bool HcclCommunicator::GetCommResource(const std::string& tag, void** commContext)
8312 : {
8313 7 : if (LIKELY(IsExistCommRes(tag))) {
8314 0 : *commContext = commContext_.ptr();
8315 0 : return true;
8316 : }
8317 7 : return false;
8318 : }
8319 :
8320 7 : bool HcclCommunicator::GetCommResource(void*& commContext)
8321 : {
8322 7 : commContext = opResDevicePara_.ptr();
8323 7 : return true;
8324 : }
8325 :
8326 0 : HcclResult HcclCommunicator::GetAicpuOpStreamNotify(HcclRtStream* opStream, u8 aicpuNotifyNum, void** aicpuNotify)
8327 : {
8328 0 : CHK_RET(GetAicpuOpStreamAndNotify(opStream, aicpuNotifyNum, aicpuNotify));
8329 0 : HCCL_INFO("[HcclCommunicator][GetAicpuOpStreamNotify]opStream %p aicpuNotify %p.", *opStream, *aicpuNotify);
8330 0 : return HCCL_SUCCESS;
8331 : }
8332 :
8333 0 : HcclResult HcclCommunicator::GetAicpuOpStreamAndNotify(HcclRtStream* opStream, u8 aicpuNotifyNum, void** aicpuNotify)
8334 : {
8335 0 : *opStream = opStream_.ptr();
8336 0 : if (localAiCpuNotifyRes_.size() < aicpuNotifyNum) {
8337 0 : for (u16 i = localAiCpuNotifyRes_.size(); i < aicpuNotifyNum; i++) {
8338 0 : std::shared_ptr<LocalNotify> localNotify = {nullptr};
8339 : HcclSignalInfo aicpuNotify;
8340 0 : CHK_RET(CreateAndGetAiCpuNotify(localNotify, aicpuNotify));
8341 0 : localAiCpuNotifyRes_.push_back(localNotify);
8342 0 : }
8343 : }
8344 :
8345 0 : for (u16 i = 0; i < aicpuNotifyNum; i++) {
8346 0 : *(aicpuNotify + i) = localAiCpuNotifyRes_[i]->ptr();
8347 : }
8348 0 : return HCCL_SUCCESS;
8349 : }
8350 :
8351 0 : HcclResult HcclCommunicator::SetAicpuNotifyInvalid()
8352 : {
8353 0 : CHK_PTR_NULL(combinOparaMem_);
8354 0 : HcclCombinOpParam* combinOparaPtr = reinterpret_cast<HcclCombinOpParam*>(combinOparaMem_->ptr());
8355 0 : CHK_PTR_NULL(combinOparaPtr);
8356 0 : combinOparaPtr->signalInfo.aicpuNotify.resId = INVALID_U64;
8357 0 : return HCCL_SUCCESS;
8358 : }
8359 :
8360 1 : HcclResult HcclCommunicator::ReplaceCommInfoByTag(const std::string& tag, std::unique_ptr<CommInfo>& commInfo)
8361 : {
8362 1 : std::unique_lock<std::mutex> replLock(commLock_);
8363 1 : tagCommInfo_.erase(tag);
8364 1 : tagCommInfo_.insert(std::pair<std::string, CommInfo>(tag, std::move(*commInfo)));
8365 1 : return HCCL_SUCCESS;
8366 1 : }
8367 :
8368 0 : HcclResult HcclCommunicator::CreateMutiStreamResFor310P(const std::string& tag, level1StreamInfo_t& streamInfo)
8369 : {
8370 0 : u32 rankSize = GetRankSize();
8371 : s32 pid;
8372 0 : if (SalGetBareTgid(&pid) != HCCL_SUCCESS) {
8373 0 : HCCL_DEBUG("get pid fail");
8374 : }
8375 0 : HCCL_INFO(
8376 : "[HcclCommunicator][CreateMutiStreamRes]tag[%s] ranksize[%u] comminfo ranksize[%u] "
8377 : "auxRingCommStreamsDev_ size[%u] ringDeviceSignalAux size[%u] ringDeviceSignal size[%u] "
8378 : "ringDeviceStreams size[%u]",
8379 : tag.c_str(), rankSize, tagCommInfo_[tag].commIntraServer->RankSize(), auxRingCommStreamsDev_.size(),
8380 : streamInfo.ringDeviceSignalAux.size(), streamInfo.ringDeviceSignal.size(), streamInfo.ringDeviceStreams.size());
8381 0 : if (auxRingCommStreamsDev_.empty() || auxRingCommStreamsDev_.size() < rankSize) {
8382 0 : auxRingCommStreamsDev_.resize(rankSize);
8383 0 : u32 resNum = rankSize - 1;
8384 0 : streamInfo.ringDeviceSignalAux.resize(resNum);
8385 0 : streamInfo.ringDeviceSignal.resize(resNum);
8386 0 : for (u32 ringIndex = 0; ringIndex < rankSize; ringIndex++) {
8387 0 : auxRingCommStreamsDev_[ringIndex] = Stream(StreamType::STREAM_TYPE_DEVICE);
8388 : // 给device侧申请的流不需要setmode,否则rts会捕获流成员Flags为1024的异常
8389 : }
8390 0 : for (auto& signal : streamInfo.ringDeviceSignal) {
8391 0 : signal = nullptr;
8392 : }
8393 0 : for (auto& signal : streamInfo.ringDeviceSignalAux) {
8394 0 : signal = nullptr;
8395 : }
8396 :
8397 0 : u32 notifyNum = resNum * 2; // 2:Signal + SignalAux
8398 0 : std::vector<std::shared_ptr<LocalNotify>> notifys(notifyNum, nullptr);
8399 0 : CHK_RET(queueNotifyManager_->Alloc(tag, notifyNum, notifys, NotifyLoadType::DEVICE_NOTIFY));
8400 0 : for (u32 i = 0; i < resNum; i++) {
8401 0 : streamInfo.ringDeviceSignal[i] = notifys[2 * i];
8402 0 : streamInfo.ringDeviceSignalAux[i] = notifys[2 * i + 1];
8403 : }
8404 0 : }
8405 :
8406 0 : if (streamInfo.ringDeviceStreams.empty() || streamInfo.ringDeviceStreams.size() < rankSize) {
8407 0 : streamInfo.ringDeviceStreams.resize(rankSize);
8408 0 : for (u32 ringIndex = 0; ringIndex < rankSize; ringIndex++) {
8409 0 : streamInfo.ringDeviceStreams[ringIndex] = auxRingCommStreamsDev_[ringIndex];
8410 0 : CHK_SMART_PTR_NULL(streamInfo.ringDeviceStreams[ringIndex]);
8411 : }
8412 : }
8413 0 : return HCCL_SUCCESS;
8414 : }
8415 :
8416 1 : HcclResult HcclCommunicator::CreateCommAndStreamRes(const std::string& tag, Stream& stream)
8417 : {
8418 1 : CHK_SMART_PTR_NULL(implAlg_);
8419 1 : void* commInputPtr = nullptr;
8420 1 : void* commOutputPtr = nullptr;
8421 : u64 commInputSize, commOutputSize;
8422 :
8423 1 : HcclResult ret = CreateCommCCLbuffer();
8424 1 : CHK_PRT_RET(
8425 : ret != HCCL_SUCCESS,
8426 : HCCL_ERROR(
8427 : "[HcclImplBase][CreateCommAndStreamRes]errNo[0x%016llx],create cclbuff failed", HCCL_ERROR_CODE(ret)),
8428 : ret);
8429 :
8430 1 : if (isA2MC2MultiServer_) {
8431 : // 该场景下ccl buffer有一块区域在上层会被用作flag区,因此需要先清理一下
8432 0 : CHK_RET(cclBufferManager_.CleanCCLbuffer());
8433 : }
8434 :
8435 1 : CHK_RET(cclBufferManager_.GetInCCLbuffer(commInputPtr, commInputSize));
8436 1 : CHK_RET(cclBufferManager_.GetOutCCLbuffer(commOutputPtr, commOutputSize));
8437 1 : DeviceMem expMem = cclBufferManager_.GetCommExpBuffer();
8438 1 : DeviceMem inputMem = DeviceMem::create(commInputPtr, commInputSize);
8439 1 : DeviceMem outputMem = DeviceMem::create(commOutputPtr, commOutputSize);
8440 1 : AlgType algType;
8441 1 : AlgType algTypeTmp;
8442 :
8443 1 : CHK_RET(GetAlgType(algType, HcclCMDType::HCCL_CMD_ALL));
8444 1 : algTypeTmp = algType;
8445 :
8446 1 : CHK_RET(notifyPool_->RegisterOp(tag));
8447 :
8448 : // 根据tag创建comm和流资源
8449 1 : if (!(IsExistCommRes(tag))) {
8450 1 : std::unique_ptr<CommInfo> commInfo = nullptr;
8451 1 : std::vector<u32>& ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
8452 1 : std::vector<u32>& vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
8453 1 : implAlg_->SetHDCModeInfo(
8454 1 : rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
8455 : HcclResult ret
8456 1 : = implAlg_->CreateComm(tag, inputMem, outputMem, algType, commInfo, INVALID_VALUE_RANKID, false, true);
8457 :
8458 1 : CHK_PRT_RET(
8459 : ret != HCCL_SUCCESS,
8460 : HCCL_ERROR(
8461 : "[HcclCommunicator][CreateCommAndStreamRes]errNo[0x%016llx]tag[%s],comm resource create comm failed",
8462 : HCCL_ERROR_CODE(ret), tag.c_str()),
8463 : ret);
8464 :
8465 1 : CHK_RET(ReplaceCommInfoByTag(tag, commInfo));
8466 1 : if (isA2MC2MultiServer_ && isA2MC2IntraHie_) {
8467 0 : std::string hieSuffix = "_HIE";
8468 0 : size_t pos = tag.find(hieSuffix);
8469 0 : std::string oldtag = tag;
8470 0 : oldtag.erase(pos, hieSuffix.size());
8471 0 : CHK_RET(ReplaceCommInfoByTag(oldtag, commInfo));
8472 0 : }
8473 1 : }
8474 :
8475 1 : if (!(IsExistMutiStreamRes(tag))) {
8476 1 : level1StreamInfo_t streamInfo;
8477 1 : std::unique_lock<std::mutex> mutiStreamLock(tagStreamInfoLock_);
8478 : // 2p场景下,mc2当前algType为518,streamInfo.ringNum走默认流程值为1导致资源申请不足,910_93
8479 : // mc2固定在节点内默认用mesh
8480 1 : constexpr u32 RANK_SIZE_TWO = 2;
8481 1 : if ((GetRankSize() == RANK_SIZE_TWO && !isA2MC2MultiServer_) || (deviceType_ == DevType::DEV_TYPE_910_93)) {
8482 0 : algTypeTmp.algoLevel0 = AlgTypeLevel0::ALG_LEVEL0_NP_MESH;
8483 0 : algTypeTmp.algoLevel1 = AlgTypeLevel1::ALG_LEVEL1_RING;
8484 : }
8485 1 : HcclResult ret = HCCL_SUCCESS;
8486 1 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
8487 0 : ret = CreateMutiStreamResFor310P(tag, streamInfo);
8488 : } else {
8489 1 : ret = implAlg_->CreateMutiStreamRes(tag, stream, streamInfo, algTypeTmp, true);
8490 : }
8491 1 : CHK_PRT_RET(
8492 : ret != HCCL_SUCCESS,
8493 : HCCL_ERROR(
8494 : "[HcclCommunicator][CreateCommAndStreamRes]errNo[0x%016llx]tag[%s],comm resource create stream "
8495 : "resource",
8496 : HCCL_ERROR_CODE(ret), tag.c_str()),
8497 : ret);
8498 1 : tagStreamInfo_.insert(std::pair<std::string, Level1StreamInfo>(tag, std::move(streamInfo)));
8499 1 : opRetryStreamPtr_->insert(std::make_pair(tag, tagStreamInfo_[tag].ringDeviceStreams));
8500 1 : mutiStreamLock.unlock();
8501 1 : }
8502 :
8503 1 : HCCL_INFO("resource creation (AllReduce) success, tag[%s]", tag.c_str());
8504 1 : CHK_RET(notifyPool_->UnregisterOp(tag));
8505 1 : if (commPortConfig_.devPortSwitchOn) {
8506 0 : std::vector<u32>& nicPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
8507 0 : std::vector<u32>& vnicPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
8508 0 : Heartbeat::GetInstance(deviceLogicId_)
8509 0 : .SetRankPortInfo(isUseRankPort_, nicPorts, vnicPorts, commPortConfig_.devPortSwitchOn);
8510 : }
8511 1 : CHK_RET(RegisterToHeartBeat());
8512 :
8513 1 : CommBase* comm = nullptr;
8514 1 : CHK_RET(GetComm(tag, &comm));
8515 1 : if (comm == nullptr) {
8516 0 : HCCL_ERROR("comm get err, comm %p", comm);
8517 0 : return HCCL_E_PTR;
8518 : }
8519 1 : CHK_RET(
8520 : SetCommResource(commInputSize, commInputPtr, commOutputPtr, expMem.ptr(), comm, tagStreamInfo_[tag], stream));
8521 :
8522 1 : return HCCL_SUCCESS;
8523 1 : }
8524 :
8525 1 : HcclResult HcclCommunicator::GetComm(const std::string& tag, CommBase** comm)
8526 : {
8527 1 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
8528 0 : *comm = tagCommInfo_[tag].commIntraServer.get();
8529 1 : } else if (isA2MC2MultiServer_) {
8530 : // 使用打平RDMA Mesh子通信域
8531 0 : *comm = tagCommInfo_[tag].commLevel1Rdma[0].get();
8532 : } else {
8533 1 : *comm = tagCommInfo_[tag].commLevel0[0].get();
8534 : }
8535 1 : return HCCL_SUCCESS;
8536 : }
8537 :
8538 1 : HcclResult HcclCommunicator::SetCommResource(
8539 : u64 commBufferSize, void* commInPtr, void* commOutPtr, void* commExpPtr, CommBase* comm,
8540 : level1StreamInfo_t& streamInfo, Stream& stream)
8541 : {
8542 1 : CHK_PTR_NULL(combinOparaMem_);
8543 1 : HcclCombinOpParam* combinOparaPtr = reinterpret_cast<HcclCombinOpParam*>(combinOparaMem_->ptr());
8544 1 : CHK_PTR_NULL(combinOparaPtr);
8545 :
8546 1 : u32 rankSize = comm->RankSize();
8547 1 : u32 curRankId = comm->Rank();
8548 1 : u32 usrRankId = comm->UserRank();
8549 1 : combinOparaPtr->rankId = curRankId;
8550 1 : combinOparaPtr->signalInfo.aicpuNotify.rankId = curRankId;
8551 1 : combinOparaPtr->rankNum = rankSize;
8552 1 : combinOparaPtr->winSize = commBufferSize;
8553 1 : combinOparaPtr->winExpSize = EXP_BUFFER_SIZE;
8554 1 : combinOparaPtr->config.deterministic = GetDeterministicConfig();
8555 : combinOparaPtr->config.notifyWaitTime
8556 1 : = (GetExternalInputHcclExecTimeoutSet() != HcclExecTimeoutSet::HCCL_EXEC_TIMEOUT_NOT_SET
8557 1 : || commConfig_.GetConfigExecTimeOutSet()) ?
8558 0 : commConfig_.GetConfigExecTimeOut() :
8559 : NOTIFY_DEFAULT_WAIT_TIME;
8560 1 : hcclMc2Info_.groupName = hrtMsprofGetHashId(identifier_.c_str(), identifier_.length());
8561 1 : combinOparaPtr->config.linkTimeOut = std::chrono::seconds(GetExternalInputHcclLinkTimeOut());
8562 1 : hcclMc2Info_.rankSize = rankSize;
8563 1 : hcclMc2Info_.rankId = curRankId;
8564 1 : hcclMc2Info_.usrRankId = usrRankId;
8565 1 : hcclMc2Info_.aicpuKfcStreamId = static_cast<uint32_t>(stream.id());
8566 1 : hcclMc2Info_.commStreamSize = rankSize;
8567 1 : hcclMc2Info_.reserve = 0;
8568 1 : rtEvent_t event = nullptr;
8569 1 : u32 eventId = 0;
8570 1 : u32 idx = 0;
8571 1 : u32 txSigleBase = 2;
8572 1 : u32 rxSigleBase = 3;
8573 :
8574 1 : if (isA2MC2MultiServer_) {
8575 : // MoE融合算子优化,MC2多机场景
8576 : // 判断是否支持NormalQP创建,若不支持,需要额外下发敲Doorbell任务
8577 0 : bool isSupportNormalQP = false;
8578 0 : CHK_RET(IsSupportAicpuNormalQP(devicePhyId_, isSupportNormalQP));
8579 0 : CHK_RET(SetDevIbverbsData(comm, isSupportNormalQP, commBufferSize, commInPtr, commOutPtr));
8580 :
8581 0 : bool isSupportAIVNormalQP = false;
8582 0 : CHK_RET(IsSupportAIVNormalQP(devicePhyId_, isSupportAIVNormalQP));
8583 0 : if (isSupportAIVNormalQP && isA2MC2IntraHie_) {
8584 0 : CHK_RET(GenAiRMAInfo(comm));
8585 0 : } else {
8586 0 : HCCL_WARNING(
8587 : "[%s] db transfer normal qp not support. tag[%s] curRankId[%u] rankNum[%u] isSupportAIVNormalQP[%u]",
8588 : __func__, comm->Tag().c_str(), curRankId, rankSize, isSupportAIVNormalQP);
8589 : }
8590 :
8591 0 : if (combinedCapabilityMem_ == nullptr) {
8592 0 : CHK_RET(AllocAndClearHostMem(sizeof(CombinedCapability), combinedCapabilityMem_));
8593 : }
8594 0 : CHK_PTR_NULL(combinedCapabilityMem_);
8595 0 : CombinedCapability* combinedCapabilityPtr = static_cast<CombinedCapability*>(combinedCapabilityMem_->ptr());
8596 0 : CHK_PTR_NULL(combinedCapabilityPtr);
8597 0 : SalSetBitOne(combinedCapabilityPtr->dataplaneModeBitmap, POS_DATA_PLANE_MODE_HOST);
8598 0 : if (isSupportAIVNormalQP && isA2MC2IntraHie_) {
8599 0 : SalSetBitOne(combinedCapabilityPtr->dataplaneModeBitmap, POS_DATA_PLANE_MODE_AIV);
8600 : }
8601 0 : SalSetBitOne(combinedCapabilityPtr->dataplaneModeBitmap, POS_DATA_PLANE_MODE_AICPU);
8602 :
8603 0 : HCCL_INFO("[SetCommResource] Set dataplaneModeBitmap to [%llu]", combinedCapabilityPtr->dataplaneModeBitmap);
8604 :
8605 : // 非NormalQP场景需要传一条流,用于敲Doorbell
8606 0 : combinOparaPtr->streamInfo[0].streamIds = streamInfo.ringDeviceStreams[0].id();
8607 0 : combinOparaPtr->streamInfo[0].sqIds = streamInfo.ringDeviceStreams[0].sqId();
8608 0 : combinOparaPtr->streamInfo[0].cqIds = streamInfo.ringDeviceStreams[0].cqId();
8609 0 : combinOparaPtr->streamInfo[0].logicCqids = streamInfo.ringDeviceStreams[0].logicCqId();
8610 0 : HCCL_DEBUG(
8611 : "[SetCommResource] Set streamInfo[0].streamIds[%u].sqIds[%u].cqIds[%u].logicCqids[%u]",
8612 : combinOparaPtr->streamInfo[0].streamIds, combinOparaPtr->streamInfo[0].sqIds,
8613 : combinOparaPtr->streamInfo[0].cqIds, combinOparaPtr->streamInfo[0].logicCqids);
8614 : } else {
8615 2 : for (u32 i = 0; i < rankSize; i++) {
8616 1 : if (i != curRankId) {
8617 : void* bufferIn;
8618 : void* bufferOut;
8619 0 : std::vector<void*> remotePtrVec;
8620 0 : CHK_RET(comm->GetTransportByRank(i)->GetRemoteMem(UserMemType::INPUT_MEM, &bufferIn));
8621 0 : combinOparaPtr->windowsIn[i] = reinterpret_cast<u64>(bufferIn);
8622 :
8623 0 : CHK_RET(comm->GetTransportByRank(i)->GetRemoteMem(UserMemType::OUTPUT_MEM, &bufferOut));
8624 0 : combinOparaPtr->windowsOut[i] = reinterpret_cast<u64>(bufferOut);
8625 :
8626 0 : CHK_RET(comm->GetTransportByRank(i)->GetRemoteMem(&remotePtrVec));
8627 0 : if (remotePtrVec.size() != 0) {
8628 0 : combinOparaPtr->windowsExp[i] = reinterpret_cast<u64>(remotePtrVec[0]);
8629 0 : if (comm->GetTransportByRank(i)->GetTransportType() == TransportType::TRANS_TYPE_P2P) {
8630 0 : p2pCclBuf_[i] = remotePtrVec[0];
8631 : } else {
8632 0 : cclBuf_[i] = remotePtrVec[0];
8633 : }
8634 0 : combinOparaPtr->windowsExp[i]
8635 0 : += cclBufferManager_.GetInCCLbufferSize() + cclBufferManager_.GetOutCCLbufferSize();
8636 : }
8637 0 : CHK_RET(comm->GetTransportByRank(i)->GetTxAckDevNotifyInfo(combinOparaPtr->signalInfo.ipcNotifys[i]));
8638 0 : CHK_RET(comm->GetTransportByRank(i)->GetRxAckDevNotifyInfo(
8639 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize]));
8640 0 : CHK_RET(comm->GetTransportByRank(i)->GetTxDataSigleDevNotifyInfo(
8641 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase]));
8642 0 : CHK_RET(comm->GetTransportByRank(i)->GetRxDataSigleDevNotifyInfo(
8643 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase]));
8644 0 : CHK_RET(GetAiCpuNotifyData(
8645 : streamInfo.ringDeviceSignalAux[idx], combinOparaPtr->signalInfo.noIpcNotifys[i]));
8646 :
8647 0 : CHK_RET(GetAiCpuNotifyData(
8648 : streamInfo.ringDeviceSignal[idx], combinOparaPtr->signalInfo.noIpcNotifys[i + rankSize]));
8649 0 : idx++;
8650 0 : } else {
8651 1 : combinOparaPtr->windowsIn[i] = reinterpret_cast<u64>(commInPtr);
8652 1 : combinOparaPtr->windowsOut[i] = reinterpret_cast<u64>(commOutPtr);
8653 1 : combinOparaPtr->windowsExp[i] = reinterpret_cast<u64>(commExpPtr);
8654 : // 在与aicpu商议后,本卡不再防止无效值。后续代码要删掉
8655 1 : combinOparaPtr->signalInfo.ipcNotifys[i].resId = INVALID_U64;
8656 1 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize].resId = INVALID_U64;
8657 1 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase].resId = INVALID_U64;
8658 1 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase].resId = INVALID_U64;
8659 : }
8660 1 : HCCL_INFO(
8661 : "group[%s] successfully set windowsIn & windowsOut & windowsExp info: userRank[%u], groupRank[%u], "
8662 : "windowsIn[0x%llx], InSize[0x%llx], windowOut[0x%llx], OutSize[0x%llx], windowExp[0x%llx], "
8663 : "ExpSize[0x%llu]",
8664 : identifier_.c_str(), GetUserRank(), GetGroupRank(), combinOparaPtr->windowsIn[i],
8665 : cclBufferManager_.GetInCCLbufferSize(), combinOparaPtr->windowsOut[i],
8666 : cclBufferManager_.GetOutCCLbufferSize(), combinOparaPtr->windowsExp[i],
8667 : cclBufferManager_.GetExpBufferSize());
8668 :
8669 1 : combinOparaPtr->signalInfo.ipcNotifys[i].rankId = i;
8670 1 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize].rankId = i;
8671 1 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase].rankId = i;
8672 1 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase].rankId = i;
8673 1 : combinOparaPtr->signalInfo.noIpcNotifys[i].rankId = i;
8674 :
8675 1 : hcclMc2Info_.commStreamIds[i] = streamInfo.ringDeviceStreams[i].id();
8676 1 : combinOparaPtr->streamInfo[i].streamIds = streamInfo.ringDeviceStreams[i].id();
8677 1 : combinOparaPtr->streamInfo[i].sqIds = streamInfo.ringDeviceStreams[i].sqId();
8678 1 : combinOparaPtr->streamInfo[i].cqIds = streamInfo.ringDeviceStreams[i].cqId();
8679 1 : combinOparaPtr->streamInfo[i].logicCqids = streamInfo.ringDeviceStreams[i].logicCqId();
8680 1 : HCCL_DEBUG("[hccl_Mc2_Info] commStreamIds[%u]:[%u]", i, streamInfo.ringDeviceStreams[i].id());
8681 :
8682 1 : CHK_RET(hrtEventCreateWithFlag(&event));
8683 :
8684 1 : CHK_RET(hrtGetEventID(event, &eventId));
8685 1 : aiCpuNoIpcEvnet_.push_back(event);
8686 1 : combinOparaPtr->signalInfo.noIpcEvents[i].resId = eventId;
8687 1 : HCCL_DEBUG(
8688 : "SetCommResource ipc notify info pre record local rankid: %u: remote rankid:%u, resId:%llu, "
8689 : "devId:%u, tsId:%u, addr:%llu.",
8690 : curRankId, combinOparaPtr->signalInfo.ipcNotifys[i].rankId,
8691 : combinOparaPtr->signalInfo.ipcNotifys[i].resId, combinOparaPtr->signalInfo.ipcNotifys[i].devId,
8692 : combinOparaPtr->signalInfo.ipcNotifys[i].tsId, combinOparaPtr->signalInfo.ipcNotifys[i].addr);
8693 1 : HCCL_DEBUG(
8694 : "SetCommResource ipc notify info pre wait local rankid: %u: remote rankid:%u, resId:%llu, "
8695 : "devId:%u, tsId:%u, addr:%llu.",
8696 : curRankId, combinOparaPtr->signalInfo.ipcNotifys[i + rankSize].rankId,
8697 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize].resId,
8698 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize].devId,
8699 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize].tsId,
8700 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize].addr);
8701 1 : HCCL_DEBUG(
8702 : "SetCommResource ipc notify info post record local rankid: %u: remote rankid:%u, resId:%llu, "
8703 : "devId:%u, tsId:%u, addr:%llu.",
8704 : curRankId, combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase].rankId,
8705 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase].resId,
8706 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase].devId,
8707 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase].tsId,
8708 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase].addr);
8709 1 : HCCL_DEBUG(
8710 : "SetCommResource ipc notify info post wait local rankid: %u: remote rankid:%u, resId:%llu, "
8711 : "devId:%u, tsId:%u, addr:%llu.",
8712 : curRankId, combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase].rankId,
8713 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase].resId,
8714 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase].devId,
8715 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase].tsId,
8716 : combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase].addr);
8717 : }
8718 : }
8719 1 : HCCL_DEBUG(
8720 : "[hccl_Mc2_Info] groupname:[%s][%llu], rankSize[%u], rankId[%u], usrRankId[%u], aicpuKfcStreamId[%u], "
8721 : "commStreamSize[%u]",
8722 : identifier_.c_str(), hcclMc2Info_.groupName, rankSize, curRankId, usrRankId, static_cast<uint32_t>(stream.id()),
8723 : rankSize);
8724 1 : CHK_RET(
8725 : ProfilingManagerPub::CallMsprofReportMc2CommInfo(hrtMsprofSysCycleTime(), &hcclMc2Info_, sizeof(hcclMc2Info_)));
8726 1 : return HCCL_SUCCESS;
8727 : }
8728 :
8729 807 : void HcclCommunicator::ReleaseCommContextbuffer() { commContext_.free(); }
8730 :
8731 0 : HcclResult HcclCommunicator::CreateDeviceCommContext(u64 size, DeviceMem& buffer) const
8732 : {
8733 0 : CHK_PRT_RET(
8734 : !size,
8735 : HCCL_INFO("[Create][DeviceCommContext]device commContext size is zero. "
8736 : "not need to malloc memory"),
8737 : HCCL_SUCCESS);
8738 :
8739 : CHK_PRT_RET(
8740 : (size > ULONG_MAX),
8741 : HCCL_ERROR("[Create][DeviceCommContext]device commContext size %llu is large than ULONG_MAX", size),
8742 : HCCL_E_PARA);
8743 :
8744 0 : if (!buffer.ptr()) {
8745 0 : u64 memSize = size;
8746 0 : CHK_RET(DeviceMem::alloc(buffer, memSize));
8747 : }
8748 0 : return HCCL_SUCCESS;
8749 : }
8750 :
8751 0 : void HcclCommunicator::Break()
8752 : {
8753 0 : if (implAlg_ != nullptr) {
8754 0 : implAlg_->Break();
8755 : }
8756 0 : return;
8757 : }
8758 :
8759 3 : HcclResult HcclCommunicator::GetAlltoAllStagedWorkSpaceMemSize(
8760 : u64* sendCounts, u64* sdispls, HcclDataType sendType, u64* recvCounts, u64* rdispls, HcclDataType recvType,
8761 : u64& memSize)
8762 : {
8763 3 : if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
8764 0 : HCCL_ERROR(
8765 : "[%s][%s]GetAlltoAllStagedWorkSpaceMemSize Not Supported!", LOG_KEYWORDS_TASK_EXEC.c_str(),
8766 : LOG_KEYWORDS_NOT_SUPPORTED.c_str());
8767 0 : return HCCL_E_NOT_SUPPORT;
8768 : }
8769 3 : CHK_SMART_PTR_NULL(implAlg_);
8770 3 : std::unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(HcclCMDType::HCCL_CMD_ALLTOALLV);
8771 3 : AlltoAllOperator* alltoAllOperator = dynamic_cast<AlltoAllOperator*>(algOperator.get());
8772 3 : CHK_PTR_NULL(alltoAllOperator);
8773 :
8774 3 : OpParam opParam;
8775 3 : opParam.All2AllDataDes.sendType = sendType;
8776 3 : opParam.All2AllDataDes.recvType = recvType;
8777 3 : opParam.All2AllDataDes.sendCounts = static_cast<void*>(sendCounts);
8778 3 : opParam.All2AllDataDes.recvCounts = static_cast<void*>(recvCounts);
8779 3 : opParam.All2AllDataDes.sdispls = static_cast<void*>(sdispls);
8780 3 : opParam.All2AllDataDes.rdispls = static_cast<void*>(rdispls);
8781 3 : opParam.opType = HcclCMDType::HCCL_CMD_ALLTOALLV;
8782 3 : opParam.aicpuUnfoldMode = false;
8783 3 : opParam.aicpuCacheEnable = 0;
8784 :
8785 3 : if (alltoAllOperator->IsSatisfyAlltoAllAivCondition(opParam)
8786 3 : || alltoAllOperator->IsSatisfy91093OffloadCondition()) {
8787 3 : memSize = 0;
8788 3 : HCCL_INFO("Calculate workSpace MemSize for aiv AllToAll done, memSize[%llu]", memSize);
8789 3 : return HCCL_SUCCESS;
8790 : }
8791 :
8792 0 : std::unique_ptr<PreProcessMetaInfo> preMetaInfo = std::make_unique<PreProcessMetaInfo>();
8793 0 : CHK_SMART_PTR_NULL(preMetaInfo);
8794 :
8795 0 : CHK_RET(alltoAllOperator->PrepareAlltoAllAddrInfo(
8796 : opParam.All2AllDataDes.sendCounts, opParam.All2AllDataDes.sdispls, opParam.All2AllDataDes.sendType,
8797 : opParam.All2AllDataDes.recvCounts, opParam.All2AllDataDes.rdispls, opParam.All2AllDataDes.recvType,
8798 : preMetaInfo));
8799 :
8800 0 : preMetaInfo->opType = HcclCMDType::HCCL_CMD_ALLGATHER;
8801 :
8802 0 : CHK_RET(RegressCalPreOp(alltoAllOperator, opParam, preMetaInfo));
8803 :
8804 0 : return alltoAllOperator->GetAlltoAllStagedWorkSpaceMemSize(opParam, memSize);
8805 3 : }
8806 :
8807 1 : HcclResult HcclCommunicator::GetAlltoAllStagedWorkSpaceMemSize(
8808 : std::vector<SendRecvInfo>& allMeshAggregationSendRecvInfo, u64& memSize)
8809 : {
8810 1 : CHK_PRT_RET(
8811 : Is310P3Common(isHaveCpuRank_, deviceType_),
8812 : HCCL_ERROR("[HcclCommunicator][GetAlltoAllStagedWorkSpaceMemSize]Not Supported!"), HCCL_E_NOT_SUPPORT);
8813 :
8814 1 : CHK_SMART_PTR_NULL(implAlg_);
8815 0 : return implAlg_->GetAlltoAllStagedWorkSpaceMemSize(allMeshAggregationSendRecvInfo, memSize);
8816 : }
8817 :
8818 : HcclResult
8819 0 : HcclCommunicator::GetAllReduceScratchSize(const u64 count, const HcclDataType dataType, u64& scratchSize) const
8820 : {
8821 0 : CHK_SMART_PTR_NULL(implAlg_);
8822 0 : return implAlg_->GetAllReduceScratchSize(count, dataType, scratchSize);
8823 : }
8824 :
8825 33 : HcclResult HcclCommunicator::SetWorldGroupInfo(
8826 : std::unordered_map<std::string, std::map<u32, HcclIpAddress>> phyIdNicInfoMap, vector<RankInfo> worldRankInfoList,
8827 : vector<u32>& nicRanksPort, vector<u32>& vnicRanksPort)
8828 : {
8829 33 : for (auto& ipInfo : phyIdNicInfoMap) {
8830 0 : for (auto& devInfo : ipInfo.second) {
8831 0 : rankDevicePhyIdNicInfoMap_[ipInfo.first][devInfo.first] = devInfo.second;
8832 0 : HCCL_DEBUG(
8833 : "phyIdNicInfoMap print hostIp[%s] devId[%u] devIp[%s]", ipInfo.first.c_str(), devInfo.first,
8834 : devInfo.second.GetReadableAddress());
8835 : }
8836 : }
8837 :
8838 33 : for (auto& rankInfo : worldRankInfoList) {
8839 0 : worldRankInfoList_.push_back(rankInfo);
8840 : }
8841 :
8842 33 : for (auto& port : nicRanksPort) {
8843 0 : nicRanksPort_.push_back(port);
8844 0 : HCCL_DEBUG("nicRanksPort port[%u]", port);
8845 : }
8846 33 : for (auto& port : vnicRanksPort) {
8847 0 : vnicRanksPort_.push_back(port);
8848 0 : HCCL_DEBUG("vnicRanksPort port[%u]", port);
8849 : }
8850 33 : return HCCL_SUCCESS;
8851 : }
8852 :
8853 0 : HcclResult HcclCommunicator::GetTopoDesc(HcclTopoDescs* topoDescs, uint32_t topoSize) const
8854 : {
8855 0 : if (topoSize < static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_MAX)) {
8856 0 : HCCL_ERROR("topoDescs size is not enough, please check topoSize[%u]", topoSize);
8857 0 : return HCCL_E_PARA;
8858 : }
8859 :
8860 0 : if (deviceType_ == DevType::DEV_TYPE_910_93) {
8861 0 : topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)].algSets = HCCL_ALG_SWITCH | HCCL_ALG_RING;
8862 0 : topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L1)].algSets = HCCL_ALG_RING;
8863 0 : } else if (deviceType_ == DevType::DEV_TYPE_910B) {
8864 0 : topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)].algSets = HCCL_ALG_MESH;
8865 0 : topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L1)].algSets = 0;
8866 0 : } else if (deviceType_ == DevType::DEV_TYPE_310P3) {
8867 0 : topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)].algSets = HCCL_ALG_RING;
8868 0 : topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L1)].algSets = 0;
8869 : }
8870 :
8871 0 : topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)].rankSize = userRankSize_;
8872 0 : topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L1)].rankSize = 0;
8873 0 : return HCCL_SUCCESS;
8874 : }
8875 :
8876 235 : HcclResult HcclCommunicator::SetAivModeConfig(const bool aivMode)
8877 : {
8878 235 : CHK_SMART_PTR_NULL(implAlg_);
8879 235 : CHK_RET(implAlg_->SetAivModeConfig(aivMode));
8880 235 : return HCCL_SUCCESS;
8881 : }
8882 :
8883 235 : HcclResult HcclCommunicator::SetOnlyAivModeConfig(const bool isOnlyAiv)
8884 : {
8885 235 : CHK_SMART_PTR_NULL(implAlg_);
8886 235 : CHK_RET(implAlg_->SetOnlyAivModeConfig(isOnlyAiv));
8887 235 : isOnlyAiv_ = isOnlyAiv;
8888 235 : return HCCL_SUCCESS;
8889 : }
8890 :
8891 235 : HcclResult HcclCommunicator::SetAicpuUnfoldConfig(const bool aicpuUnfold)
8892 : {
8893 235 : CHK_SMART_PTR_NULL(implAlg_);
8894 235 : CHK_RET(implAlg_->SetAicpuUnfoldConfig(aicpuUnfold));
8895 235 : return HCCL_SUCCESS;
8896 : }
8897 :
8898 235 : HcclResult HcclCommunicator::SetExecTimeOutConfig(const s32 execTimeOut)
8899 : {
8900 235 : CHK_SMART_PTR_NULL(implAlg_);
8901 235 : CHK_RET(implAlg_->SetExecTimeOutConfig(execTimeOut));
8902 235 : return HCCL_SUCCESS;
8903 : }
8904 :
8905 235 : HcclResult HcclCommunicator::SetAlgoConfig(const std::map<HcclCMDType, std::vector<HcclAlgoType>>& algoMap)
8906 : {
8907 235 : CHK_SMART_PTR_NULL(implAlg_);
8908 235 : CHK_RET(implAlg_->SetAlgoConfig(algoMap));
8909 235 : return HCCL_SUCCESS;
8910 : }
8911 :
8912 532 : bool HcclCommunicator::GetAivModeConfig() { return commConfig_.GetConfigAivMode(); }
8913 :
8914 532 : bool HcclCommunicator::GetConfigIsOnlyAivMode() { return commConfig_.GetConfigIsOnlyAivMode(); }
8915 :
8916 1979 : bool HcclCommunicator::GetAicpuUnfoldConfig() { return commConfig_.GetConfigAicpuUnfold(); }
8917 :
8918 0 : void HcclCommunicator::SetQpQosAttr(u32 trafficClass, u32 serviceLevel)
8919 : {
8920 0 : if (oneSideService_) {
8921 0 : oneSideService_->SetTCAndSL(trafficClass, serviceLevel);
8922 0 : HCCL_INFO("[%s]Set TC[%u] and SL[%u] for oneSidedService success.", __func__, trafficClass, serviceLevel);
8923 : }
8924 0 : transportManager_->SetQpQosAttr(trafficClass, serviceLevel);
8925 0 : indptOpTransportManager_->SetQpQosAttr(trafficClass, serviceLevel);
8926 0 : }
8927 :
8928 0 : HcclResult HcclCommunicator::CheckExitWaitResumeState(bool& isChangedLink)
8929 : {
8930 0 : if (retryEnable_ && opRetryManager_ != nullptr) {
8931 0 : bool haveCommEnableBackupLink = false;
8932 0 : if (g_enableBackupLinkCommCount.load() > 0) {
8933 0 : haveCommEnableBackupLink = true;
8934 : }
8935 0 : HcclResult ret = opRetryManager_->ExitWaitResumeState(
8936 0 : identifier_, commConnections_.isRoot, haveCommEnableBackupLink, isChangedLink);
8937 0 : CHK_PRT_RET(
8938 : ret != HCCL_SUCCESS, HCCL_ERROR("[HcclCommunicator][Resume]opretry exit wait resume state failed."), ret);
8939 : }
8940 0 : return HCCL_SUCCESS;
8941 : }
8942 :
8943 0 : HcclResult HcclCommunicator::SetMemoryRange(void* baseVirPtr, size_t size, size_t alignment, uint64_t flags)
8944 : {
8945 0 : CHK_PRT_RET(
8946 : deviceType_ != DevType::DEV_TYPE_910_93,
8947 : HCCL_ERROR("[HcclCommunicator][SetMemoryRange] deviceType[%d] not support zero copy", deviceType_),
8948 : HCCL_E_NOT_SUPPORT);
8949 0 : if (zeroCopyMemoryAgent_ == nullptr) {
8950 0 : CHK_RET(InitZeroCopyMemoryAgent());
8951 : }
8952 0 : CHK_RET(zeroCopyMemoryAgent_->SetMemoryRange(baseVirPtr, size, alignment, flags));
8953 0 : return HCCL_SUCCESS;
8954 : }
8955 :
8956 0 : HcclResult HcclCommunicator::UnsetMemoryRange(void* baseVirPtr)
8957 : {
8958 0 : CHK_PRT_RET(
8959 : zeroCopyMemoryAgent_ == nullptr,
8960 : HCCL_ERROR("[HcclCommunicator][UnsetMemoryRange] not call HcclCommSetMemoryRange()"), HCCL_E_PARA);
8961 0 : CHK_RET(zeroCopyMemoryAgent_->UnsetMemoryRange(baseVirPtr));
8962 0 : return HCCL_SUCCESS;
8963 : }
8964 :
8965 0 : HcclResult HcclCommunicator::ActivateCommMemory(void* virPtr, size_t size, size_t offset, void* handle, uint64_t flags)
8966 : {
8967 0 : CHK_PRT_RET(
8968 : zeroCopyMemoryAgent_ == nullptr,
8969 : HCCL_ERROR("[HcclCommunicator][ActivateCommMemory] not call HcclCommSetMemoryRange()"), HCCL_E_PARA);
8970 0 : CHK_RET(zeroCopyMemoryAgent_->ActivateCommMemory(virPtr, size, offset, handle, flags));
8971 0 : return HCCL_SUCCESS;
8972 : }
8973 :
8974 0 : HcclResult HcclCommunicator::DeactivateCommMemory(void* virPtr)
8975 : {
8976 0 : CHK_PRT_RET(
8977 : zeroCopyMemoryAgent_ == nullptr,
8978 : HCCL_ERROR("[HcclCommunicator][DeactivateCommMemory] not call HcclCommSetMemoryRange()"), HCCL_E_PARA);
8979 0 : CHK_RET(zeroCopyMemoryAgent_->DeactivateCommMemory(virPtr));
8980 0 : return HCCL_SUCCESS;
8981 : }
8982 :
8983 0 : HcclResult HcclCommunicator::SetSingleLinkInfo(
8984 : std::unordered_map<u32, bool>& switchRanks, u32 remoteRankId, ChangeLinkInfo& changeLinkInfo)
8985 : {
8986 0 : auto iterLocal = switchRanks.find(userRank_);
8987 0 : auto iterRemote = switchRanks.find(remoteRankId);
8988 :
8989 0 : bool useBackupLink = false;
8990 0 : if (iterLocal != switchRanks.end() && iterRemote != switchRanks.end()) {
8991 : // 本端卡和对端卡都切,如果两者的目标网卡冲突,则切换失败;否则使用一致的目标网卡的的对应链路
8992 0 : CHK_PRT_RET(
8993 : iterLocal->second != iterRemote->second,
8994 : HCCL_ERROR(
8995 : "[HcclCommunicator][SetSingleLinkInfo] local rank[%u] plan to switch to nic[%u], "
8996 : "which is conflict with remote rank[%u] planning to switch to nic[%u].",
8997 : userRank_, iterLocal->second, remoteRankId, iterRemote->second),
8998 : HCCL_E_PARA);
8999 0 : useBackupLink = iterLocal->second;
9000 0 : } else if (iterLocal != switchRanks.end()) {
9001 : // 仅切换本端卡,根据本端卡的目标网卡,刷新对应链路
9002 0 : useBackupLink = iterLocal->second;
9003 0 : } else if (iterRemote != switchRanks.end()) {
9004 : // 仅切换对端卡,根据对端卡的目标网卡,刷新对应链路
9005 0 : useBackupLink = iterRemote->second;
9006 : } else {
9007 0 : HCCL_INFO(
9008 : "[HcclCommunicator][SetSingleLinkInfo] comm identifier[%s], local rank[%u], "
9009 : "remote rank[%u], neither the rank need switch, link will not be refreshed.",
9010 : identifier_.c_str(), userRank_, remoteRankId);
9011 0 : return HCCL_SUCCESS;
9012 : }
9013 :
9014 0 : changeLinkInfo.remoteRankList[changeLinkInfo.remoteRankNum] = remoteRankId;
9015 0 : changeLinkInfo.isUseDefaultPort[changeLinkInfo.remoteRankNum] = !(useBackupLink);
9016 0 : changeLinkInfo.remoteRankNum++;
9017 0 : remoteRankNicStatus_[remoteRankId] = useBackupLink ? CONNECT_REMOTE_BACKUP : CONNECT_REMOTE_DEFAULT;
9018 0 : needCheckBackupNic_ = needCheckBackupNic_ || useBackupLink;
9019 0 : needCheckDefaultNic_ = needCheckDefaultNic_ || !useBackupLink;
9020 :
9021 0 : HCCL_RUN_INFO(
9022 : "[HcclCommunicator][SetSingleLinkInfo] comm identifier[%s], local rank[%u], "
9023 : "remote rank[%u], useBackupLink[%u], link info refreshed.",
9024 : identifier_.c_str(), userRank_, remoteRankId, useBackupLink);
9025 0 : return HCCL_SUCCESS;
9026 : }
9027 :
9028 : HcclResult
9029 0 : HcclCommunicator::SetRemoteRankLinkInfo(std::unordered_map<u32, bool>& switchRanks, ChangeLinkInfo& changeLinkInfo)
9030 : {
9031 : // 初始化重置changeLinkInfo
9032 0 : changeLinkInfo.remoteRankNum = 0;
9033 0 : needCheckBackupNic_ = false;
9034 0 : needCheckDefaultNic_ = false;
9035 : // 初始化重置remoteRankNicStatus_
9036 0 : (void)memset_s(remoteRankNicStatus_, sizeof(remoteRankNicStatus_), 0, sizeof(remoteRankNicStatus_));
9037 :
9038 0 : for (auto resIt : resMap_) {
9039 0 : for (auto& levelNSubCommTransport : resIt.second.opTransportResponse) {
9040 0 : for (auto& singleSubCommTransport : levelNSubCommTransport) {
9041 0 : for (auto& transportRequest : singleSubCommTransport.transportRequests) {
9042 0 : if (transportRequest.isValid && transportRequest.isUsedRdma) { // 仅RDMA链路需要刷新
9043 0 : CHK_RET(SetSingleLinkInfo(switchRanks, transportRequest.remoteUserRank, changeLinkInfo));
9044 : }
9045 : }
9046 : }
9047 : }
9048 0 : }
9049 0 : return HCCL_SUCCESS;
9050 : }
9051 :
9052 0 : HcclResult HcclCommunicator::ActiveStoppedLink(
9053 : std::map<u32, bool>& remoteRankPortMap, OpCommTransport& opTransportResponse, bool isBackup)
9054 : {
9055 0 : for (auto& levelNSubCommTransport : opTransportResponse) {
9056 0 : for (auto& singleSubCommTransport : levelNSubCommTransport) {
9057 0 : if (singleSubCommTransport.status.size() == 0) {
9058 0 : continue;
9059 : }
9060 0 : if (singleSubCommTransport.status.size() != singleSubCommTransport.transportRequests.size()
9061 0 : || singleSubCommTransport.links.size() != singleSubCommTransport.transportRequests.size()) {
9062 0 : HCCL_ERROR(
9063 : "[HcclCommunicator][ActiveStoppedLink] comm identifier[%s], local rank[%u], "
9064 : "status num[%u] or links num[%u] is inconsistent with transport request num[%u]. "
9065 : "Please check whether the resources are allocated correctly.",
9066 : identifier_.c_str(), userRank_, singleSubCommTransport.status.size(),
9067 : singleSubCommTransport.links.size(), singleSubCommTransport.transportRequests.size());
9068 0 : return HCCL_E_INTERNAL;
9069 : }
9070 :
9071 0 : for (size_t i = 0; i < singleSubCommTransport.transportRequests.size(); i++) {
9072 0 : auto& transportRequest = singleSubCommTransport.transportRequests[i];
9073 0 : auto remoteRankIter = remoteRankPortMap.find(transportRequest.remoteUserRank);
9074 0 : bool needLink = transportRequest.isValid && transportRequest.isUsedRdma
9075 0 : && remoteRankIter != remoteRankPortMap.end() && (remoteRankIter->second != isBackup);
9076 : // STOP状态的Transport需要唤醒,重置位到READY
9077 0 : if (needLink && singleSubCommTransport.status[i] == TransportStatus::STOP) {
9078 0 : HCCL_INFO(
9079 : "[HcclCommunicator][ActiveStoppedLink] comm identifier[%s], local rank[%u], "
9080 : "resuming link of remote rank[%u]",
9081 : identifier_.c_str(), userRank_, transportRequest.remoteUserRank);
9082 0 : CHK_RET(singleSubCommTransport.links[i]->Resume());
9083 0 : singleSubCommTransport.status[i] = TransportStatus::READY;
9084 : }
9085 : }
9086 : }
9087 : }
9088 0 : return HCCL_SUCCESS;
9089 : }
9090 :
9091 : HcclResult
9092 0 : HcclCommunicator::PrepareLinkForSwitchNic(std::unordered_map<u32, bool>& switchRanks, ChangeLinkInfo& changeLinkInfo)
9093 : {
9094 0 : CHK_RET(SetRemoteRankLinkInfo(switchRanks, changeLinkInfo));
9095 :
9096 0 : std::map<u32, bool> remoteRankPortMap;
9097 0 : for (u32 i = 0; i < changeLinkInfo.remoteRankNum; i++) {
9098 0 : remoteRankPortMap.emplace(changeLinkInfo.remoteRankList[i], changeLinkInfo.isUseDefaultPort[i]);
9099 : }
9100 0 : for (auto resIt : resMap_) {
9101 0 : CHK_RET(ActiveStoppedLink(remoteRankPortMap, resIt.second.opTransportResponse, false));
9102 0 : CHK_RET(ActiveStoppedLink(remoteRankPortMap, resIt.second.opTransportResponseBackUp, true));
9103 0 : }
9104 0 : return HCCL_SUCCESS;
9105 0 : }
9106 :
9107 0 : HcclResult HcclCommunicator::ParseSwitchRanks(
9108 : uint32_t nRanks, uint32_t* ranks, bool* useBackup, std::unordered_map<u32, bool>& switchRanks)
9109 : {
9110 0 : CHK_PTR_NULL(ranks);
9111 0 : CHK_PTR_NULL(useBackup);
9112 0 : switchRanksNum_ = nRanks;
9113 0 : (void)memset_s(switchRankList_, sizeof(switchRankList_), 0, sizeof(switchRankList_));
9114 0 : (void)memset_s(switchUseBackup_, sizeof(switchUseBackup_), 0, sizeof(switchUseBackup_));
9115 0 : s32 ret = memcpy_s(switchRankList_, sizeof(switchRankList_), ranks, sizeof(u32) * nRanks);
9116 0 : CHK_PRT_RET(
9117 : ret != EOK, HCCL_ERROR("[HcclCommunicator][ParseSwitchRanks] mem copy switch ranks fail."), HCCL_E_INTERNAL);
9118 0 : ret = memcpy_s(switchUseBackup_, sizeof(switchUseBackup_), useBackup, sizeof(bool) * nRanks);
9119 0 : CHK_PRT_RET(
9120 : ret != EOK, HCCL_ERROR("[HcclCommunicator][ParseSwitchRanks] mem copy switch use backup fail."),
9121 : HCCL_E_INTERNAL);
9122 :
9123 0 : std::string switchRankStr{};
9124 0 : for (uint32_t i = 0; i < nRanks; i++) {
9125 0 : CHK_PTR_NULL(ranks + i);
9126 0 : CHK_PTR_NULL(useBackup + i);
9127 0 : uint32_t switchRankId = ranks[i];
9128 0 : bool backup = useBackup[i];
9129 0 : CHK_PRT_RET(
9130 : switchRankId >= userRankSize_,
9131 : HCCL_ERROR(
9132 : "[HcclCommunicator][ParseSwitchRanks] invalid switchRankId[%u], "
9133 : "which should not be greater than rankSize[%u]",
9134 : switchRankId, userRankSize_),
9135 : HCCL_E_PARA);
9136 0 : CHK_PRT_RET(
9137 : switchRanks.find(switchRankId) != switchRanks.end(),
9138 : HCCL_ERROR("[HcclCommunicator][ParseSwitchRanks] duplicated switchRankId[%u]", switchRankId), HCCL_E_PARA);
9139 0 : switchRanks.emplace(switchRankId, backup);
9140 0 : switchRankStr += std::to_string(switchRankId) + ":" + std::to_string(backup) + ";";
9141 : }
9142 0 : HCCL_RUN_INFO(
9143 : "[HcclCommunicator][ParseSwitchRanks] comm identifier[%s], userRank[%u], load switchRanks:%s.",
9144 : identifier_.c_str(), userRank_, switchRankStr.c_str());
9145 0 : return HCCL_SUCCESS;
9146 0 : }
9147 :
9148 0 : HcclResult HcclCommunicator::SwitchNic(
9149 : uint32_t nRanks, uint32_t* ranks, bool* useBackup, std::shared_ptr<HDCommunicate>& controlH2D,
9150 : std::shared_ptr<HDCommunicate>& statusD2H)
9151 : {
9152 0 : HcclResult ret = HCCL_SUCCESS;
9153 0 : CHK_PRT_RET(
9154 : !IsEnableBackupLink(),
9155 : HCCL_RUN_WARNING(
9156 : "[HcclCommunicator][%s]Backup link is not enabled, "
9157 : "switch nic will not be prorocessed, comm identifier[%s], rank[%u], devType[%u], opretry enable[%u], "
9158 : "backup ip valid[%u], roce enable[%u].",
9159 : __func__, identifier_.c_str(), userRank_, deviceType_,
9160 : GetAicpuUnfoldConfig() && commConfig_.GetConfigInterSuperPodRetryEnable(), !devBackupIpAddr_[0].IsInvalid(),
9161 : IsEnableRoce()),
9162 : HCCL_SUCCESS);
9163 0 : CHK_PRT_RET(
9164 : resMap_.empty(),
9165 : HCCL_ERROR(
9166 : "[HcclCommunicator][%s] "
9167 : "no collective operation has been executed in this communication[%s] on rank[%u], "
9168 : "which does not support to set working device nic.",
9169 : __func__, identifier_.c_str(), userRank_),
9170 : HCCL_E_PARA);
9171 0 : std::unordered_map<u32, bool> switchRanks;
9172 0 : ChangeLinkInfo changeLinkInfo;
9173 0 : ret = ParseSwitchRanks(nRanks, ranks, useBackup, switchRanks);
9174 0 : if (ret == HCCL_SUCCESS) {
9175 0 : ret = PrepareLinkForSwitchNic(switchRanks, changeLinkInfo);
9176 : }
9177 : changeLinkInfo.isChangeLinkFlag
9178 0 : = ret == HCCL_SUCCESS; // 如果入参校验失败,则无需刷新链路;通知aicpu侧,防止其他卡超时等待
9179 :
9180 0 : switchNicWaitingResult_ = false;
9181 :
9182 0 : u32 changeLinkInfoStart
9183 : = sizeof(KfcCommand) + sizeof(BackgroundCommand) + sizeof(HcclComSuspendingFlag) + sizeof(HcclOpIdentifier);
9184 0 : CHK_RET(controlH2D->Put(changeLinkInfoStart, sizeof(ChangeLinkInfo), reinterpret_cast<uint8_t*>(&changeLinkInfo)));
9185 :
9186 0 : KfcCommand switchNicCommand = KfcCommand::kSwitchNic;
9187 0 : CHK_RET(controlH2D->Put(0, sizeof(KfcCommand), reinterpret_cast<uint8_t*>(&switchNicCommand)));
9188 :
9189 0 : KfcExecStatus switchStatus;
9190 0 : switchStatus.execStatus.kfcStatus = KfcStatus::kNull;
9191 0 : u32 waitSwitchExecCmdTimeout = static_cast<u32>(GetExternalInputHcclLinkTimeOut() * 1000 * 2.5f);
9192 : auto waitSwitchExecCmdTimeoutMs
9193 0 : = std::chrono::milliseconds(waitSwitchExecCmdTimeout); // 等待2.5倍的建链超时时间,给快慢卡场景提供冗余
9194 0 : auto startTime = std::chrono::steady_clock::now();
9195 : while (true) {
9196 0 : if (switchNicWaitingResult_) {
9197 0 : CHK_RET(statusD2H->Get(0, sizeof(KfcExecStatus), reinterpret_cast<uint8_t*>(&switchStatus)));
9198 : }
9199 0 : if (switchStatus.execStatus.kfcStatus == KfcStatus::kSwitchSuccess) {
9200 0 : HCCL_INFO(
9201 : "[HcclCommunicator][%s] comm identifier[%s], devicePhyId[%u], userRank[%u] switch nic success.",
9202 : __func__, identifier_.c_str(), devicePhyId_, userRank_);
9203 0 : ret = HCCL_SUCCESS;
9204 0 : break;
9205 0 : } else if (switchStatus.execStatus.kfcStatus == KfcStatus::kSwitchFail) {
9206 0 : HCCL_ERROR(
9207 : "[HcclCommunicator][%s] comm identifier[%s], devicePhyId[%u], userRank[%u] switch nic fail.", __func__,
9208 : identifier_.c_str(), devicePhyId_, userRank_);
9209 0 : ret = HCCL_E_INTERNAL;
9210 0 : break;
9211 0 : } else if ((std::chrono::steady_clock::now() - startTime) >= waitSwitchExecCmdTimeoutMs) {
9212 0 : HCCL_ERROR(
9213 : "[HcclCommunicator][%s] comm identifier[%s], devicePhyId[%u], "
9214 : "userRank[%u] switch nic timeout[%u ms], the transport status is undefined. "
9215 : "Please search log with keyword [ErrToWarn] for detail.",
9216 : __func__, identifier_.c_str(), devicePhyId_, userRank_, waitSwitchExecCmdTimeout);
9217 0 : ret = HCCL_E_TIMEOUT;
9218 0 : break;
9219 : } else {
9220 0 : SaluSleep(ONE_MILLISECOND_OF_USLEEP);
9221 : }
9222 0 : }
9223 0 : KfcExecControl clearCommand{};
9224 0 : CHK_RET(controlH2D->Put(0, sizeof(KfcExecControl), reinterpret_cast<uint8_t*>(&clearCommand)));
9225 0 : KfcExecStatus clearStatus{};
9226 0 : CHK_RET(controlH2D->Put(0, sizeof(KfcExecStatus), reinterpret_cast<uint8_t*>(&clearStatus)));
9227 0 : switchRanksNum_ = 0;
9228 0 : return ret;
9229 0 : }
9230 :
9231 0 : HcclResult HcclCommunicator::GetSwitchRanks(
9232 : u32* distSwitchRankList, bool* distSwitchUseBackup, u32& distSwitchRankNum, u8* distRemoteRankNicStatus,
9233 : u32& distNicStatusNum, bool& needCheckDefaultNic, bool& needCheckBackupNic)
9234 : {
9235 0 : s32 ret = memcpy_s(
9236 0 : distSwitchRankList, sizeof(u32) * AICPU_MAX_RANK_NUM, switchRankList_, sizeof(u32) * switchRanksNum_);
9237 0 : CHK_PRT_RET(
9238 : ret != EOK, HCCL_ERROR("[HcclCommunicator][GetSwitchRanks] mem copy switch rank list fail, ret[%u].", ret),
9239 : HCCL_E_INTERNAL);
9240 0 : ret = memcpy_s(
9241 0 : distSwitchUseBackup, sizeof(bool) * AICPU_MAX_RANK_NUM, switchUseBackup_, sizeof(bool) * switchRanksNum_);
9242 0 : CHK_PRT_RET(
9243 : ret != EOK, HCCL_ERROR("[HcclCommunicator][GetSwitchRanks] mem copy switch use backup fail, ret[%u].", ret),
9244 : HCCL_E_INTERNAL);
9245 0 : distSwitchRankNum = switchRanksNum_;
9246 0 : ret = memcpy_s(
9247 0 : distRemoteRankNicStatus, sizeof(u8) * AICPU_MAX_RANK_NUM, remoteRankNicStatus_, sizeof(u8) * userRankSize_);
9248 0 : CHK_PRT_RET(
9249 : ret != EOK,
9250 : HCCL_ERROR(
9251 : "[HcclCommunicator][GetSwitchRanks] mem copy remote rank nic status fail, "
9252 : "ret[%u].",
9253 : ret),
9254 : HCCL_E_INTERNAL);
9255 0 : distNicStatusNum = userRankSize_;
9256 0 : needCheckDefaultNic = needCheckDefaultNic_;
9257 0 : needCheckBackupNic = needCheckBackupNic_;
9258 0 : switchNicWaitingResult_ = true;
9259 0 : return HCCL_SUCCESS;
9260 : }
9261 :
9262 0 : HcclResult GetCannPath(const char* binPath, std::string& cannPath)
9263 : {
9264 0 : CHK_PRT_RET(binPath == nullptr, HCCL_ERROR("[HcclCommunicator][GetCannPath]binary path is nullptr"), HCCL_E_PTR);
9265 :
9266 0 : std::string tmpPath(binPath); // 存放cann安装路径
9267 0 : std::string libraryPath;
9268 0 : HcclResult ret = ParseLibraryPath(libraryPath);
9269 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[GetCannPath]errNo[0x%016llx]parse path fail.", ret), ret);
9270 :
9271 0 : ret = GetKeyWordPath(libraryPath, "/hccl", tmpPath);
9272 0 : CHK_PRT_RET(
9273 : ret != HCCL_SUCCESS, HCCL_ERROR("[GetCannPath]cannot found version file in %s.", libraryPath.c_str()),
9274 : HCCL_E_PARA);
9275 0 : tmpPath += binPath;
9276 0 : cannPath = tmpPath;
9277 :
9278 0 : return HCCL_SUCCESS;
9279 0 : }
9280 :
9281 523 : HcclResult HcclCommunicator::LoadCustomFile(
9282 : const char* binPath, aclrtBinaryLoadOptionType optionType, uint32_t cpuKernelMode, aclrtBinHandle& binHandle)
9283 : {
9284 523 : binHandle = nullptr;
9285 523 : HCCL_INFO("[LoadCustomFile]binPath[%s], optionType[%u], cpuKernelMode[%u]", binPath, optionType, cpuKernelMode);
9286 523 : return HCCL_SUCCESS;
9287 : }
9288 :
9289 0 : void HcclCommunicator::UnloadBinary(aclrtBinHandle& binHandle)
9290 : {
9291 0 : if (binHandle != nullptr) {
9292 0 : aclError ret = aclrtBinaryUnLoad(binHandle);
9293 0 : if (ret != ACL_SUCCESS) {
9294 0 : HCCL_ERROR("[UnloadBinary]errNo[0x%016llx] unload binary from file error.", ret);
9295 : }
9296 0 : binHandle = nullptr;
9297 : }
9298 0 : return;
9299 : }
9300 :
9301 0 : HcclResult HcclCommunicator::RegisterCommUserMem(void* addr, u64 size, void** handle)
9302 : {
9303 : // user mem和ccl buffer互斥,不支持同时创建
9304 0 : if (deviceType_ != DevType::DEV_TYPE_910_93 || superPodNum_ > 1 || isUserMemRegisted_
9305 0 : || cclBufferManager_.GetInCCLbuffer().ptr() != nullptr) {
9306 0 : HCCL_ERROR(
9307 : "[HcclCommunicator][%s]Registration user mem is not supported with the params. "
9308 : "Device type[%d], superPodNum[%u]; Or user mem/CCL buffer has already registered, addr[%p], "
9309 : "isUserMemRegisted[%d]",
9310 : __func__, deviceType_, superPodNum_, addr, isUserMemRegisted_);
9311 0 : return HCCL_E_NOT_SUPPORT;
9312 : }
9313 : // DeviceMem::create创建的DeviceMem对象为拷贝构造,析构时不释放内存,内存由上层管理
9314 0 : DeviceMem userMem = DeviceMem::create(addr, size);
9315 0 : std::shared_ptr<DeviceMem> userMemPtr = nullptr;
9316 0 : EXCEPTION_CATCH((userMemPtr = std::make_shared<DeviceMem>(std::move(userMem))), return HCCL_E_PTR);
9317 0 : *handle = static_cast<void*>(userMemPtr.get());
9318 0 : userMemMap_.insert(std::make_pair(*handle, userMemPtr));
9319 0 : HCCL_INFO(
9320 : "[HcclCommunicator][%s]Register user mem success, group[%s], handle[%p], addr[%llu], size[%llu]", __func__,
9321 : identifier_.c_str(), *handle, reinterpret_cast<uint64_t>(addr), size);
9322 0 : isUserMemRegisted_ = true;
9323 0 : return HCCL_SUCCESS;
9324 0 : }
9325 :
9326 0 : HcclResult HcclCommunicator::DeregisterCommUserMem(void* handle)
9327 : {
9328 0 : if (deviceType_ != DevType::DEV_TYPE_910_93 || superPodNum_ > 1) {
9329 0 : HCCL_ERROR(
9330 : "[HcclCommunicator][%s]Unsupported on the device type[%d] or superPodNum[%u]", __func__, deviceType_,
9331 : superPodNum_);
9332 0 : return HCCL_E_NOT_SUPPORT;
9333 : }
9334 :
9335 0 : CHK_PRT_RET(
9336 : !userMemMap_.erase(handle),
9337 : HCCL_RUN_WARNING("[HcclCommunicator][%s]Mem is not exist, handle[%p]", __func__, handle), HCCL_SUCCESS);
9338 :
9339 : // 重置user mem和userMemType
9340 0 : CHK_SAFETY_FUNC_RET(
9341 : memset_s(opResPara_.userMemRes, sizeof(opResPara_.userMemRes), 0, sizeof(opResPara_.userMemRes)));
9342 0 : opResPara_.userMemType = 0; // CCL Buffer
9343 0 : isUserMemRegisted_ = false;
9344 0 : HCCL_INFO(
9345 : "[HcclCommunicator][%s]Deregister mem success, group[%s], handle[%p]", __func__, identifier_.c_str(), handle);
9346 0 : return HCCL_SUCCESS;
9347 : }
9348 :
9349 0 : HcclResult HcclCommunicator::ExchangeCommUserMem(void* handle, std::vector<u32>& peerRanks)
9350 : {
9351 0 : if (deviceType_ != DevType::DEV_TYPE_910_93 || superPodNum_ > 1 || GetExternalInputInterHccsDisable()) {
9352 0 : HCCL_ERROR(
9353 : "[HcclCommunicator][%s]Unsupported configuration: device type[%d], superPodNum[%u], "
9354 : "or RDMA usage",
9355 : __func__, deviceType_, superPodNum_);
9356 0 : return HCCL_E_NOT_SUPPORT;
9357 : }
9358 :
9359 0 : if ((peerRanks.size() > rankInfoList_.size())) {
9360 0 : HCCL_ERROR(
9361 : "[HcclCommunicator][%s]Invalid peerRanksNum[%u], which should be less than communicator "
9362 : "rank nums[%u]",
9363 : __func__, peerRanks.size(), rankInfoList_.size());
9364 0 : return HCCL_E_PARA;
9365 : }
9366 : // 获取user mem,调exchange接口前需要先调注册接口注册user mem
9367 0 : if (userMemMap_.find(handle) == userMemMap_.end()) {
9368 0 : HCCL_ERROR("[HcclCommunicator][%s]Find user mem failed, handle[%p] is not registered", __func__, handle);
9369 0 : return HCCL_E_NOT_FOUND;
9370 : }
9371 0 : DeviceMem userMem = *userMemMap_[handle].get();
9372 0 : CHK_PTR_NULL(userMem.ptr());
9373 : // 构造建链param
9374 0 : TransportIOMem transMem;
9375 0 : transMem.userMem = userMem;
9376 0 : OpCommTransport opCommTransport;
9377 0 : LevelNSubCommTransport level0Transport;
9378 0 : SingleSubCommTransport commTransport;
9379 :
9380 0 : for (u32 rankIdx = 0; rankIdx < peerRanks.size(); rankIdx++) {
9381 0 : TransportRequest tmpTransport;
9382 0 : if (userRank_ != peerRanks[rankIdx]) {
9383 0 : tmpTransport.isValid = true;
9384 0 : tmpTransport.localUserRank = userRank_;
9385 0 : tmpTransport.remoteUserRank = peerRanks[rankIdx];
9386 0 : tmpTransport.inputMemType = TransportMemType::USER_MEM;
9387 0 : tmpTransport.outputMemType = TransportMemType::USER_MEM;
9388 : } else {
9389 : // 本rank不需要创建transport
9390 0 : tmpTransport.isValid = false;
9391 : }
9392 0 : commTransport.transportRequests.push_back(tmpTransport);
9393 : }
9394 0 : level0Transport.push_back(commTransport);
9395 0 : opCommTransport.push_back(level0Transport);
9396 0 : ClearOpTransportResponseLinks(opCommTransport);
9397 : // 建链
9398 0 : constexpr char EXCHANGE_USER_MEM_TAG_PREFIX[] = "ExchangeUserMem_";
9399 0 : string tag = EXCHANGE_USER_MEM_TAG_PREFIX + identifier_;
9400 0 : HcclResult ret = HCCL_SUCCESS;
9401 : {
9402 0 : StateGuard<HcclCommunicator, HcclCommState> guard(this, HcclCommState::BUILDING);
9403 0 : HCCL_RUN_INFO(
9404 : "[%s]Alloc transport, level size[%u], trans request size[%u], mem ptr[%p], mem size[%llu]", __func__,
9405 : opCommTransport.size(), commTransport.transportRequests.size(), userMem.ptr(), userMem.size());
9406 0 : CHK_PTR_NULL(transportManager_);
9407 0 : ret = transportManager_->Alloc(tag, transMem, opCommTransport, false);
9408 0 : }
9409 0 : CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[%s]Alloc transports failed, tag[%s]", __func__, tag.c_str()), ret);
9410 0 : userMemTransport_ = opCommTransport;
9411 0 : return HCCL_SUCCESS;
9412 0 : }
9413 0 : HcclResult HcclCommunicator::GetLocalCCLBuf(void** addr, uint64_t* size)
9414 : {
9415 0 : uint64_t cclbufSize = cclBufferManager_.GetInCCLbufferSize() + cclBufferManager_.GetOutCCLbufferSize()
9416 0 : + cclBufferManager_.GetExpBufferSize();
9417 0 : *addr = cclBufferManager_.GetCommCCLBuffer().ptr();
9418 0 : if (nullptr == cclBufferManager_.GetCommCCLBuffer().ptr()) {
9419 0 : cclbufSize = 0;
9420 : }
9421 0 : *size = cclbufSize;
9422 0 : HCCL_INFO(
9423 : "[%s] GetlocalCCLBuf success, addr[%p], size[%u]", identifier_.c_str(),
9424 : cclBufferManager_.GetCommCCLBuffer().ptr(), cclbufSize);
9425 0 : return HCCL_SUCCESS;
9426 : }
9427 :
9428 2 : HcclResult HcclCommunicator::GetRemoteCCLBuf(uint32_t remoteRank, void** addr, uint64_t* size)
9429 : {
9430 2 : CHK_PRT_RET(
9431 : (remoteRank >= AICPU_MAX_RANK_NUM), HCCL_ERROR("[%s] invalid remoteRank[%d]", __func__, remoteRank),
9432 : HCCL_E_PARA);
9433 : // 仅sdma场景
9434 1 : uint64_t cclbufSize = cclBufferManager_.GetInCCLbufferSize() + cclBufferManager_.GetOutCCLbufferSize()
9435 1 : + cclBufferManager_.GetExpBufferSize();
9436 1 : *addr = p2pCclBuf_[remoteRank];
9437 :
9438 1 : if (nullptr == p2pCclBuf_[remoteRank]) {
9439 1 : cclbufSize = 0;
9440 : }
9441 1 : *size = cclbufSize;
9442 1 : HCCL_INFO(
9443 : "[%s] GetRemoteCCLBuf success, remoteRank[%u], addr[%p], size[%u]", identifier_.c_str(), remoteRank,
9444 : p2pCclBuf_[remoteRank], cclbufSize);
9445 1 : return HCCL_SUCCESS;
9446 : }
9447 0 : HcclResult HcclCommunicator::GetKFCWorkSpace(void** addr, uint64_t* size)
9448 : {
9449 0 : *addr = workSpace_.ptr();
9450 0 : *size = workSpaceSize_;
9451 0 : HCCL_INFO(
9452 : "[%s] GetKFCWorkSpace success, addr[%p], size[%u]", identifier_.c_str(), workSpace_.ptr(), workSpaceSize_);
9453 0 : return HCCL_SUCCESS;
9454 : }
9455 4 : HcclResult HcclCommunicator::IndOpTransportAlloc(
9456 : const std::string& tag, OpCommTransport& opCommTransport, TransportIOMem& transMem, bool isAicpuModeEn)
9457 : {
9458 : // Aicpu侧不支持用户注册额外内存
9459 4 : if (isAicpuModeEn) {
9460 2 : if (transMem.indOpMem.userDeviceMem.size() > 0 || transMem.indOpMem.userHostMem.size() > 0) {
9461 2 : HCCL_ERROR("[%s] AICPU engine does not support user-registered memory", __func__);
9462 2 : return HCCL_E_NOT_SUPPORT;
9463 : }
9464 : }
9465 :
9466 2 : StateGuard<HcclCommunicator, HcclCommState> guard(this, HcclCommState::BUILDING);
9467 2 : CHK_PTR_NULL(indptOpTransportManager_);
9468 1 : bool isIndOp = true;
9469 1 : HcclResult ret = indptOpTransportManager_->Alloc(
9470 1 : tag, transMem, opCommTransport, isAicpuModeEn, false, false, HcclCMDType::HCCL_CMD_INVALID, false, isIndOp);
9471 1 : if (ret != HCCL_SUCCESS) {
9472 1 : HCCL_ERROR(
9473 : "[%s] Failed to alloc transport, tag[%s], isAicpuModeEn[%d], ret[%d]", __func__, tag.c_str(), isAicpuModeEn,
9474 : ret);
9475 1 : return ret;
9476 : }
9477 :
9478 0 : HCCL_RUN_INFO(
9479 : "[%s] Alloc transport success, tag[%s], isAicpuModeEn[%d], ret[%d]", __func__, tag.c_str(), isAicpuModeEn, ret);
9480 0 : return HCCL_SUCCESS;
9481 2 : }
9482 :
9483 235 : HcclTopoAttr HcclCommunicator::GetTopoAttr()
9484 : {
9485 235 : HcclTopoAttr topoAttr;
9486 235 : attrCollector_.GetTopoAttr(topoAttr);
9487 235 : return topoAttr;
9488 0 : }
9489 :
9490 102 : HcclResult HcclCommunicator::GetHDCommunicate(
9491 : HDCommunicateParams& kfcControlTransferH2DParams, HDCommunicateParams& kfcStatusTransferD2HParams)
9492 : {
9493 102 : if (GetSupportHDCommunicate() == false) {
9494 101 : HCCL_WARNING("%s not support HDCommunicate, skip", __func__);
9495 101 : return HCCL_SUCCESS;
9496 : }
9497 1 : CHK_SMART_PTR_NULL(kfcControlTransferH2D_);
9498 1 : CHK_SMART_PTR_NULL(kfcStatusTransferD2H_);
9499 1 : kfcControlTransferH2DParams = kfcControlTransferH2D_->GetCommunicateParams();
9500 1 : kfcStatusTransferD2HParams = kfcStatusTransferD2H_->GetCommunicateParams();
9501 1 : HCCL_INFO("%s success, group[%s]", __func__, identifier_.c_str());
9502 1 : return HCCL_SUCCESS;
9503 : }
9504 :
9505 235 : HcclResult HcclCommunicator::SetGetAicpuCommState(std::function<bool()> getAicpuCommState)
9506 : {
9507 235 : getAicpuCommState_ = getAicpuCommState;
9508 235 : HCCL_DEBUG("%s success, group[%s]", __func__, identifier_.c_str());
9509 235 : return HCCL_SUCCESS;
9510 : }
9511 :
9512 0 : HcclResult HcclCommunicator::CommGetNetLayers(uint32_t** netLayers, uint32_t* netLayerNum)
9513 : {
9514 0 : if (deviceType_ == DevType::DEV_TYPE_910_93) {
9515 0 : netLayer_[0] = static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0);
9516 0 : netLayer_[1] = static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L1);
9517 0 : *netLayerNum = COMM_LAYER_NUM_MAX;
9518 0 : } else if (deviceType_ == DevType::DEV_TYPE_910B || deviceType_ == DevType::DEV_TYPE_310P3) {
9519 0 : netLayer_[0] = static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0);
9520 0 : *netLayerNum = 1;
9521 : }
9522 0 : *netLayers = netLayer_;
9523 0 : return HCCL_SUCCESS;
9524 : }
9525 :
9526 0 : HcclResult HcclCommunicator::CommGetInstSizeByNetLayer(uint32_t netLayer, uint32_t* rankNum) const
9527 : {
9528 0 : if ((netLayer == static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0))
9529 0 : || (netLayer == static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L1))) {
9530 0 : *rankNum = userRankSize_;
9531 : }
9532 0 : return HCCL_SUCCESS;
9533 : }
9534 :
9535 0 : HcclResult HcclCommunicator::CommGetInstTopoTypeByNetLayer(uint32_t netLayer, u32* topoType) const
9536 : {
9537 0 : if (deviceType_ == DevType::DEV_TYPE_910_93) {
9538 0 : if (netLayer == static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)) {
9539 0 : *topoType = HCCL_ALG_SWITCH | HCCL_ALG_RING;
9540 0 : } else if (netLayer == static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L1)) {
9541 0 : *topoType = HCCL_ALG_RING;
9542 : }
9543 0 : } else if (deviceType_ == DevType::DEV_TYPE_910B) {
9544 0 : if (netLayer == static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)) {
9545 0 : *topoType = HCCL_ALG_MESH;
9546 : }
9547 0 : } else if (deviceType_ == DevType::DEV_TYPE_310P3) {
9548 0 : if (netLayer == static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)) {
9549 0 : *topoType = HCCL_ALG_RING;
9550 : }
9551 : }
9552 0 : return HCCL_SUCCESS;
9553 : }
9554 :
9555 0 : HcclResult HcclCommunicator::GetNetLayers(uint32_t** netLayers, uint32_t* netLayerNum)
9556 : {
9557 0 : return rankGraph_.GetNetLayers(netLayers, netLayerNum);
9558 : }
9559 :
9560 0 : HcclResult HcclCommunicator::GetInstSizeByNetLayer(uint32_t netLayer, uint32_t* rankNum)
9561 : {
9562 0 : return rankGraph_.GetInstSizeByNetLayer(netLayer, rankNum);
9563 : }
9564 :
9565 523 : HcclResult HcclCommunicator::GetInstTopoTypeByNetLayer(uint32_t netLayer, CommTopo* topoType)
9566 : {
9567 523 : return rankGraph_.GetInstTopoTypeByNetLayer(netLayer, topoType);
9568 : }
9569 :
9570 0 : HcclResult HcclCommunicator::GetInstRanksByNetLayer(uint32_t netLayer, uint32_t** rankList, uint32_t* rankNum)
9571 : {
9572 0 : return rankGraph_.GetInstRanksByNetLayer(netLayer, rankList, rankNum);
9573 : }
9574 :
9575 0 : HcclResult HcclCommunicator::GetInstSizeListByNetLayer(uint32_t netLayer, uint32_t** instSizeList, uint32_t* listSize)
9576 : {
9577 0 : return rankGraph_.GetInstSizeListByNetLayer(netLayer, instSizeList, listSize);
9578 : }
9579 :
9580 0 : HcclResult HcclCommunicator::GetTopoInstsByLayer(uint32_t netLayer, uint32_t** topoInsts, uint32_t* topoInstNum)
9581 : {
9582 0 : return rankGraph_.GetTopoInstsByLayer(netLayer, topoInsts, topoInstNum);
9583 : }
9584 :
9585 0 : HcclResult HcclCommunicator::GetTopoType(uint32_t netLayer, [[maybe_unused]] uint32_t topoInstId, CommTopo* topoType)
9586 : {
9587 0 : return rankGraph_.GetTopoType(netLayer, topoType);
9588 : }
9589 :
9590 : HcclResult
9591 0 : HcclCommunicator::GetRanksByTopoInst(uint32_t netLayer, uint32_t topoInstId, uint32_t** ranks, uint32_t* rankNum)
9592 : {
9593 0 : return rankGraph_.GetRanksByTopoInst(netLayer, topoInstId, ranks, rankNum);
9594 : }
9595 :
9596 0 : HcclResult HcclCommunicator::GetEndpointNum(uint32_t netLayer, uint32_t topoInstId, uint32_t* num)
9597 : {
9598 0 : return rankGraph_.GetEndpointNum(netLayer, topoInstId, num);
9599 : }
9600 :
9601 : HcclResult
9602 0 : HcclCommunicator::GetEndpointDesc(uint32_t netLayer, uint32_t topoInstId, uint32_t* descNum, EndpointDesc* endpointDesc)
9603 : {
9604 0 : return rankGraph_.GetEndpointDesc(netLayer, topoInstId, descNum, endpointDesc);
9605 : }
9606 :
9607 0 : HcclResult HcclCommunicator::GetEndpointInfo(
9608 : uint32_t rankId, const EndpointDesc* endPointDesc, EndpointAttr endpointAttr, uint32_t infoLen, void* info)
9609 : {
9610 0 : return rankGraph_.GetEndpointInfo(rankId, endPointDesc, endpointAttr, infoLen, info);
9611 : }
9612 :
9613 0 : HcclResult HcclCommunicator::GetRankGraph(GraphType type, void** graph, uint32_t* len)
9614 : {
9615 0 : return rankGraph_.GetRankGraphInfo(type, graph, len);
9616 : }
9617 :
9618 0 : void* HcclCommunicator::GetRankGraphV1() { return static_cast<void*>(&rankGraph_); }
9619 :
9620 0 : HcclResult HcclCommunicator::GetLinks(
9621 : uint32_t netLayer, uint32_t srcRank, uint32_t dstRank, CommLink** linkList, uint32_t* listSize)
9622 : {
9623 0 : return rankGraph_.GetLinks(netLayer, srcRank, dstRank, linkList, listSize);
9624 : }
9625 :
9626 0 : HcclResult HcclCommunicator::GetHeterogMode(HcclHeterogMode* mode) { return rankGraph_.GetHeterogMode(mode); }
9627 :
9628 523 : HcclResult HcclCommunicator::RegisterToSnapshot()
9629 : {
9630 523 : if (deviceType_ != DevType::DEV_TYPE_910B && deviceType_ != DevType::DEV_TYPE_910_93) {
9631 325 : return HCCL_SUCCESS;
9632 : }
9633 198 : if (userRankSize_ <= 1) {
9634 2 : HCCL_RUN_INFO(
9635 : "[HcclCommunicator][RegisterToSnapshot]comm identifier[%s], deviceLogicId[%d], "
9636 : "rank size[%u] is no greater than 1, and then will not register to snapshot",
9637 : identifier_.c_str(), deviceLogicId_, userRankSize_);
9638 2 : return HCCL_SUCCESS;
9639 : }
9640 0 : auto setInvalidCommCallback = [this](bool isInvalid) {
9641 0 : return this->SetInvalidComm(isInvalid);
9642 196 : };
9643 0 : auto preProcessCallback = [this]() {
9644 0 : return this->SnapshotCheckPreProcess();
9645 196 : };
9646 0 : auto postProcessCallback = [this]() {
9647 0 : return this->SnapshotCheckPostProcess();
9648 196 : };
9649 196 : CHK_RET(SnapshotControl::GetInstance(deviceLogicId_)
9650 : .RegisterComm(identifier_, setInvalidCommCallback, preProcessCallback, postProcessCallback));
9651 196 : if (IsEnableBackupLink()) {
9652 0 : CHK_RET(SnapshotControl::GetInstance(deviceLogicId_).RegisterBackup(identifier_, deviceBackUpPhyId_));
9653 : }
9654 196 : return HCCL_SUCCESS;
9655 : }
9656 :
9657 214 : HcclResult HcclCommunicator::UnRegisterFromSnapshot()
9658 : {
9659 214 : if (deviceType_ != DevType::DEV_TYPE_910B && deviceType_ != DevType::DEV_TYPE_910_93) {
9660 0 : return HCCL_SUCCESS;
9661 : }
9662 214 : if (userRankSize_ <= 1) {
9663 3 : HCCL_RUN_INFO(
9664 : "[HcclCommunicator][UnRegisterFromSnapshot]comm identifier[%s], deviceLogicId[%d], "
9665 : "rank size[%u] is no greater than 1, and then will not unregister from snapshot",
9666 : identifier_.c_str(), deviceLogicId_, userRankSize_);
9667 3 : return HCCL_SUCCESS;
9668 : }
9669 211 : CHK_RET(SnapshotControl::GetInstance(deviceLogicId_).UnRegisterComm(identifier_));
9670 211 : if (IsEnableBackupLink()) {
9671 0 : CHK_RET(SnapshotControl::GetInstance(deviceLogicId_).UnRegisterBackup(identifier_, deviceBackUpPhyId_));
9672 : }
9673 211 : return HCCL_SUCCESS;
9674 : }
9675 :
9676 0 : HcclResult HcclCommunicator::SetInvalidComm(bool isInvalid)
9677 : {
9678 0 : isInvalidComm_ = isInvalid;
9679 0 : HCCL_INFO(
9680 : "[HcclCommunicator][SetInvalidComm] comm[%s] is set to invalid, rank[%u], deviceLogicId[%d]",
9681 : identifier_.c_str(), userRank_, deviceLogicId_);
9682 0 : return HCCL_SUCCESS;
9683 : }
9684 :
9685 0 : HcclResult HcclCommunicator::SnapshotCheckPreProcess()
9686 : {
9687 0 : bool errorFlag = false;
9688 0 : auto pauseTimeout = std::chrono::seconds(GetExternalInputHcclLinkTimeOut());
9689 0 : auto startTime = std::chrono::steady_clock::now();
9690 : while (true) {
9691 0 : CHK_PRT_BREAK(
9692 : Heartbeat::GetInstance(deviceLogicId_).IsPaused(),
9693 : HCCL_INFO(
9694 : "[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9695 : "heartbeat thread has been paused.",
9696 : identifier_.c_str(), userRank_, deviceLogicId_), );
9697 0 : CHK_PRT_BREAK(
9698 : (std::chrono::steady_clock::now() - startTime) >= pauseTimeout,
9699 : HCCL_ERROR(
9700 : "[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9701 : "pause heartbeat thread timeout[%u s].",
9702 : identifier_.c_str(), userRank_, deviceLogicId_, GetExternalInputHcclLinkTimeOut()),
9703 : errorFlag = true);
9704 : }
9705 0 : startTime = std::chrono::steady_clock::now();
9706 0 : while (retryEnable_ && opRetryManager_) {
9707 0 : CHK_PRT_BREAK(
9708 : opRetryManager_->IsPaused(identifier_),
9709 : HCCL_INFO(
9710 : "[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9711 : "opretry threads have been paused.",
9712 : identifier_.c_str(), userRank_, deviceLogicId_), );
9713 0 : CHK_PRT_BREAK(
9714 : (std::chrono::steady_clock::now() - startTime) >= pauseTimeout,
9715 : HCCL_ERROR(
9716 : "[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9717 : "pause opretry threads timeout[%u s].",
9718 : identifier_.c_str(), userRank_, deviceLogicId_, GetExternalInputHcclLinkTimeOut()),
9719 : errorFlag = true);
9720 : }
9721 0 : startTime = std::chrono::steady_clock::now();
9722 0 : while (zeroCopyMemoryAgent_) {
9723 0 : CHK_PRT_BREAK(
9724 : zeroCopyMemoryAgent_->IsPaused(),
9725 : HCCL_INFO(
9726 : "[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9727 : "zero-copy memory agent thread has been paused.",
9728 : identifier_.c_str(), userRank_, deviceLogicId_), );
9729 0 : CHK_PRT_BREAK(
9730 : (std::chrono::steady_clock::now() - startTime) >= pauseTimeout,
9731 : HCCL_ERROR(
9732 : "[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9733 : "pause zero-copy memory agent thread timeout[%u s].",
9734 : identifier_.c_str(), userRank_, deviceLogicId_, GetExternalInputHcclLinkTimeOut()),
9735 : errorFlag = true);
9736 : }
9737 0 : CHK_PRT_RET(
9738 : errorFlag,
9739 : HCCL_ERROR(
9740 : "[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], "
9741 : "deviceLogicId[%d], snapshot pre-process fail due to some background threads pause timeout, please check.",
9742 : identifier_.c_str(), userRank_, deviceLogicId_),
9743 : HCCL_E_INTERNAL);
9744 0 : HCCL_INFO(
9745 : "[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9746 : "snapshot pre-process success.",
9747 : identifier_.c_str(), userRank_, deviceLogicId_);
9748 0 : return HCCL_SUCCESS;
9749 : }
9750 :
9751 0 : HcclResult HcclCommunicator::SnapshotCheckPostProcess()
9752 : {
9753 0 : bool errorFlag = false;
9754 0 : auto resumeTimeout = std::chrono::seconds(GetExternalInputHcclLinkTimeOut());
9755 0 : auto startTime = std::chrono::steady_clock::now();
9756 : while (true) {
9757 0 : CHK_PRT_BREAK(
9758 : Heartbeat::GetInstance(deviceLogicId_).IsResumed(),
9759 : HCCL_INFO(
9760 : "[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9761 : "heartbeat thread has been resumed.",
9762 : identifier_.c_str(), userRank_, deviceLogicId_), );
9763 0 : CHK_PRT_BREAK(
9764 : (std::chrono::steady_clock::now() - startTime) >= resumeTimeout,
9765 : HCCL_ERROR(
9766 : "[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9767 : "resume heartbeat thread timeout[%u s].",
9768 : identifier_.c_str(), userRank_, deviceLogicId_, GetExternalInputHcclLinkTimeOut()),
9769 : errorFlag = true);
9770 : }
9771 0 : startTime = std::chrono::steady_clock::now();
9772 0 : while (retryEnable_ && opRetryManager_) {
9773 0 : CHK_PRT_BREAK(
9774 : opRetryManager_->IsResumed(identifier_),
9775 : HCCL_INFO(
9776 : "[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9777 : "opretry threads have been resumed.",
9778 : identifier_.c_str(), userRank_, deviceLogicId_), );
9779 0 : CHK_PRT_BREAK(
9780 : (std::chrono::steady_clock::now() - startTime) >= resumeTimeout,
9781 : HCCL_ERROR(
9782 : "[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9783 : "resume opretry threads timeout[%u s].",
9784 : identifier_.c_str(), userRank_, deviceLogicId_, GetExternalInputHcclLinkTimeOut()),
9785 : errorFlag = true);
9786 : }
9787 0 : startTime = std::chrono::steady_clock::now();
9788 0 : while (zeroCopyMemoryAgent_) {
9789 0 : CHK_PRT_BREAK(
9790 : zeroCopyMemoryAgent_->IsResumed(),
9791 : HCCL_INFO(
9792 : "[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9793 : "zero-copy memory agent thread has been resumed.",
9794 : identifier_.c_str(), userRank_, deviceLogicId_), );
9795 0 : CHK_PRT_BREAK(
9796 : (std::chrono::steady_clock::now() - startTime) >= resumeTimeout,
9797 : HCCL_ERROR(
9798 : "[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9799 : "resume zero-copy memory agent thread timeout[%u s].",
9800 : identifier_.c_str(), userRank_, deviceLogicId_, GetExternalInputHcclLinkTimeOut()),
9801 : errorFlag = true);
9802 : }
9803 0 : CHK_PRT_RET(
9804 : errorFlag,
9805 : HCCL_ERROR(
9806 : "[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], "
9807 : "deviceLogicId[%d], snapshot post-process check fail due to some background threads resume timeout, "
9808 : "please check.",
9809 : identifier_.c_str(), userRank_, deviceLogicId_),
9810 : HCCL_E_INTERNAL);
9811 0 : HCCL_INFO(
9812 : "[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], deviceLogicId[%d], "
9813 : "snapshot post-process check success.",
9814 : identifier_.c_str(), userRank_, deviceLogicId_);
9815 0 : return HCCL_SUCCESS;
9816 : }
9817 :
9818 404 : void HcclCommunicator::SetReleaseChannel(std::function<HcclResult()> releaseChannel)
9819 : {
9820 404 : releaseChannel_ = releaseChannel;
9821 404 : return;
9822 : }
9823 :
9824 235 : CCLBufferManager& HcclCommunicator::GetCCLbufferManager() { return cclBufferManager_; }
9825 :
9826 0 : void HcclCommunicator::SetHcclQos(u32 hcclQos)
9827 : {
9828 0 : HCCL_INFO("[HcclCommunicator][host][SetHcclQos] hcclQos[%u]", hcclQos);
9829 0 : hcclQos_ = hcclQos;
9830 0 : }
9831 :
9832 0 : u32 HcclCommunicator::GetHcclQos() const
9833 : {
9834 0 : HCCL_INFO("[HcclCommunicator][host][GetHcclQos] hcclQos[%u]", hcclQos_);
9835 0 : return hcclQos_;
9836 : }
9837 :
9838 532 : HcclResult HcclCommunicator::InitSymmetricMemory()
9839 : {
9840 532 : if (superPodNum_ > 1) {
9841 13 : HCCL_DEBUG("[InitSymmetricMemory] Cross-SuperNode not support symmetric memory");
9842 13 : return HCCL_SUCCESS;
9843 : }
9844 519 : if (deviceType_ != DevType::DEV_TYPE_910_93) {
9845 491 : HCCL_DEBUG("[%s] deviceType:%d not support symmetric memory", __func__, deviceType_);
9846 491 : return HCCL_SUCCESS;
9847 : }
9848 :
9849 28 : u64 stride = commConfig_.GetConfigSymmetricMemoryStride() * GIGABYTE_TO_BYTE;
9850 28 : HCCL_RUN_INFO(
9851 : "InitSymmetricMemory, comm identifier[%s], userRank[%u], userRankSize[%u], stride[%llu], devicePhyId[%u].",
9852 : identifier_.c_str(), realUserRank_, userRankSize_, stride, devicePhyId_);
9853 :
9854 28 : symmetricMemoryAgent_ = std::make_shared<SymmetricMemoryAgent>(
9855 28 : socketManager_, devicePhyId_, deviceLogicId_, localVnicIp_, rankInfoList_, realUserRank_, useSuperPodMode_,
9856 28 : identifier_);
9857 28 : CHK_SMART_PTR_NULL(symmetricMemoryAgent_);
9858 :
9859 28 : symmetricMemory_ = std::make_unique<SymmetricMemory>(realUserRank_, userRankSize_, stride, symmetricMemoryAgent_);
9860 28 : CHK_SMART_PTR_NULL(symmetricMemory_);
9861 28 : return HCCL_SUCCESS;
9862 : }
9863 :
9864 0 : HcclResult HcclCommunicator::RegisterWindow(void* ptr, size_t size, HcclCommSymWindow* winHandle)
9865 : {
9866 0 : CHK_PRT_RET(
9867 : superPodNum_ > 1, HCCL_ERROR("[RegisterWindow] Cross-SuperNode not support symmetric memory"),
9868 : HCCL_E_NOT_SUPPORT);
9869 :
9870 0 : CHK_PRT_RET(
9871 : deviceType_ != DevType::DEV_TYPE_910_93,
9872 : HCCL_ERROR("[%s] deviceType:%d not support symmetric memory", __func__, deviceType_), HCCL_E_NOT_SUPPORT);
9873 :
9874 0 : CHK_SMART_PTR_NULL(symmetricMemory_);
9875 0 : return symmetricMemory_->RegisterSymmetricMem(ptr, size, winHandle);
9876 : }
9877 :
9878 0 : HcclResult HcclCommunicator::DeregisterWindow(HcclCommSymWindow winHandle)
9879 : {
9880 0 : CHK_SMART_PTR_NULL(symmetricMemory_);
9881 0 : return symmetricMemory_->DeregisterSymmetricMem(winHandle);
9882 : }
9883 :
9884 0 : HcclResult HcclCommunicator::GetCommSymWin(void* ptr, size_t size, HcclCommSymWindow* winHandle, size_t* offset)
9885 : {
9886 0 : CHK_SMART_PTR_NULL(symmetricMemory_);
9887 0 : return symmetricMemory_->FindSymmetricWindow(ptr, size, winHandle, reinterpret_cast<u64*>(offset));
9888 : }
9889 :
9890 24 : bool HcclCommunicator::EnableAicpuUnfold(bool isCapture)
9891 : {
9892 24 : if (deviceType_ != DevType::DEV_TYPE_910_93 && deviceType_ != DevType::DEV_TYPE_910B) {
9893 5 : return false;
9894 : }
9895 : // 910B在acl graph场景(isCapture)不启用aicpu展开
9896 19 : if (deviceType_ == DevType::DEV_TYPE_910B && isCapture) {
9897 3 : HCCL_INFO(
9898 : "[%s] deviceType[%d] isCapture[1], aicpuUnfoldConfig[%u] 910B does not support aicpuUnfold in acl graph "
9899 : "mode",
9900 : __func__, deviceType_, GetAicpuUnfoldConfig());
9901 3 : return false;
9902 : }
9903 16 : HCCL_INFO("[%s] aicpuUnfoldConfig[%u]", __func__, GetAicpuUnfoldConfig());
9904 14 : return GetAicpuUnfoldConfig();
9905 : }
9906 :
9907 235 : aclrtBinHandle HcclCommunicator::GetBinHandle()
9908 : {
9909 235 : if (binHandle_ == nullptr) {
9910 234 : HCCL_ERROR("[HcclCommunicator][GetBinHandle] GetBinHandle binHandle failed.binHandle is nullptr");
9911 234 : return nullptr;
9912 : }
9913 1 : return binHandle_;
9914 : }
9915 : HcclResult
9916 0 : HcclCommunicator::GetDevMemWorkSpace(const std::string& memTag, uint64_t* size, void** addr, bool* newCreated)
9917 : {
9918 0 : return dpuManager_->GetDevMemWorkSpace(memTag, size, addr, newCreated);
9919 : }
9920 : } // namespace hccl
|