LCOV - code coverage report
Current view: top level - legacy/ascend910/framework/communicator/impl - hccl_communicator_host.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 39.4 % 5752 2265
Test Date: 2026-07-28 12:11:00 Functions: 46.4 % 343 159

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : 
      11              : #include <memory>
      12              : #include <atomic>
      13              : #include <chrono>
      14              : #include <thread>
      15              : #include <algorithm>
      16              : #include <numeric>
      17              : #include <unordered_set>
      18              : #include <sys/time.h>
      19              : #include "externalinput_pub.h"
      20              : #include "env_config.h"
      21              : #include "p2p_mgmt_pub.h"
      22              : #include "opexecounter_pub.h"
      23              : #include "config.h"
      24              : #include "stream_active_manager.h"
      25              : #include "device_capacity.h"
      26              : #include "profiling_manager_pub.h"
      27              : #include "task_exception_handler_pub.h"
      28              : #include "rank_consistentcy_checker.h"
      29              : #include "hccl_aiv.h"
      30              : #include "adapter_rts_common.h"
      31              : #include "coll_alg_utils.h"
      32              : #include "../common/src/state_guard.h"
      33              : #include "detect_connect_anomalies.h"
      34              : #include "alg_profiling.h"
      35              : #include "mmpa_api.h"
      36              : #include "stream_utils.h"
      37              : #include "config_log.h"
      38              : #include "../nslbdp/hccl_nslbdp.h"
      39              : #include "../common/src/h2d_tlv/hccl_h2dtlv.h"
      40              : #include "hccl_one_sided_service.h"
      41              : #include "launch_device.h"
      42              : #include "hccl_communicator.h"
      43              : #include "launch_aicpu.h"
      44              : #include "order_launch/order_launch.h"
      45              : #include "comm_configer.h"
      46              : #include "snapshot_control.h"
      47              : #include "comm_topo_desc.h"
      48              : #include "hccl_net_dev_defs.h"
      49              : #include "aclgraph_callback.h"
      50              : 
      51              : using namespace std;
      52              : constexpr u32 MODULE_NUM_FOUR = 4;
      53              : constexpr u16 MAX_VALUE_U16 = 0xFFFF;
      54              : 
      55              : namespace hccl
      56              : {
      57              :     static std::mutex g_hcomInitMutex;
      58              :     static std::atomic<u32> g_enableBackupLinkCommCount{0}; // 开启借轨的通信域计数
      59              :     constexpr u32 MEMORY_CAPACITY = 256 * 1024;
      60              :     constexpr u32 WAIT_PREPARE_SLEEP_TIME = 5000;
      61              :     constexpr u32 SINGLE_SERVER_NUM = 1;
      62              :     constexpr u32 CONN_LIMIT = 4096;
      63              :     constexpr u32 COMM_DEV_TYPE_DIGIT_NUM = 8;
      64              :     constexpr u32 TILINGDATA_BUF_SIZE = 32 * 1024; // 单位:字节
      65              :     constexpr u32 ALLTOALL_INFO_MATRIX_SIZE = 4;
      66              :     constexpr u32 AICPU_RETRY_LINKROCE_DEFAULT = 0;
      67              :     constexpr u32 AICPU_RETRY_LINKROCE_BACKUP = 1;
      68              :     constexpr u32 SINGLE_PROCESS_MIN_PORT = 1024;
      69              :     constexpr u32 SINGLE_PROCESS_MAX_PORT = 65535;
      70              :     constexpr u32 TYPE_USER_MEM = 1;
      71              :     constexpr u32 NON_BATCH_WRITE_MAX_STREAM_NUM = 19U;
      72              :     constexpr u64 GIGABYTE_TO_BYTE = 1024ULL * 1024ULL * 1024ULL;
      73              :     constexpr u8 AICPU_ORDERLAUNCH_INVALID_HCOM_MODE = 255; // 图模式下无附属从流,不进行按序下发
      74              :     enum TransferMemInfoIdx
      75              :     {
      76              :         TRANSFER_MEM_INFO_KEY_IDX = 0,
      77              :         TRANSFER_MEM_INFO_VALUE_IDX = 1,
      78              :         TRANSFER_MEM_INFO_RDMA_ENVELOPE_IDX = 2,
      79              :         TRANSFER_MEM_INFO_IDX_NUM = 3
      80              :     };
      81              : 
      82              :     enum class AicpuLocalNotifyIdx : u32
      83              :     {
      84              :         // host-aicpu同步
      85              :         HOST_TO_AICPU_0 = 0,
      86              :         HOST_TO_AICPU_1 = 1,
      87              : 
      88              :         // 用于控制单算子模式各通信域kernel按序占核的notify
      89              :         ORDER_INDEX_OPBASE_0 = 2, // host_order流 record, kernel流 wait
      90              :         ORDER_INDEX_OPBASE_1 = 3, // aicpu_order流 record, host_order流 wait
      91              : 
      92              :         // 用于控制Aclgraph模式各通信域kernel按序占核的notify
      93              :         ORDER_INDEX_ACLGRAPH_0 = 4, // host_order流 record, kernel流 wait
      94              :         ORDER_INDEX_ACLGRAPH_1 = 5, // aicpu_order流 record, host_order流 wait
      95              : 
      96              :         // 用于控制图模式各通信域kernel按序占核的notify
      97              :         ORDER_INDEX_HCOM_0 = 6, // host_order流 record, kernel流 wait
      98              :         ORDER_INDEX_HCOM_1 = 7 // aicpu_order流 record, host_order流 wait
      99              :     };
     100              : 
     101              :     enum class AicpuLocalEventIdx : u32
     102              :     {
     103              :         /**
     104              :          *@brief 用于控制Aclgraph模式按序下发控制流入图的event
     105              :          *@note 通信域绑定Context,而Stream是Context管理的资源,因此对应下在Stream上的event与通信域强相关,需要communicator管理
     106              :         **/
     107              :         ORDER_INDEX_ACLGRAPH_EVENT_0 = 0, // kernel流 record, host_order流 wait
     108              :         ORDER_INDEX_ACLGRAPH_EVENT_1 = 1, // host_order流 record, kernel流 wait
     109              :     };
     110              : 
     111          391 :     HcclCommunicator::HcclCommunicator()
     112          391 :         : dispatcher_(nullptr), vDispatcher_(nullptr), notifyPool_(nullptr),
     113          391 :           initializedFlag_(ATOMIC_FLAG_INIT), userRank_(INVALID_VALUE_RANKID), realUserRank_(INVALID_VALUE_RANKID),
     114         1173 :           userRankSize_(INVALID_VALUE_RANKSIZE), drvInit_(false), inlineReduceSwitchOn_(true),
     115          391 :           nicDeployment_(NICDeployment::NIC_DEPLOYMENT_DEVICE), devicePhyId_(INVALID_UINT),
     116          782 :           deviceLogicId_(-1), localRank_(INVALID_VALUE_RANKID), hostSocketHandle_(nullptr),
     117          782 :           isUsedRdmaLevel0_(false), nicInitialized_(0), hcomGroupNicInit_(false),
     118          782 :           profilingMode_(HcomProfilingMode::PROFILING_CLOSE), raResourceInit_(false),
     119          391 :           interServer_(false), isSingleMeshAggregation_(false), cclBufferManager_(CCLBufferManager()),
     120          391 :           isExecuteProfilingInit_(false), deviceType_(DevType::DEV_TYPE_COUNT),
     121          391 :           commHandle_(nullptr),
     122         1173 :           commWorkMode_(WorkMode::HCCL_MODE_NORMAL), meshAggregationRankSize_(0), isHaveCpuRank_(false), ranktableCrc_(0),
     123          391 :           multiModuleDiffDeviceNumMode_(false), multiSuperPodDiffServerNumMode_(false), multiSuperPodDiffDeviceNumMode_(false),
     124          391 :           isStandardCard_(false), is310PDuoCard_(false), hccsPortNum_(-1),
     125          782 :           loopBackIp_(HcclIpAddress(COMM_LOOPBACK_IP)), profilingInitiated_(false), callbackThreadId_(INVALID_U64),
     126          391 :           role_(SERVER_ROLE_SOCKET),
     127          391 :           isHostUseDevNic_(false),
     128       301461 :           isAllRankSamePlane_(false), serverNum_(0), moduleNum_(0)
     129              :     {
     130          391 :         zeroCopyAclGraph_.reset(new (std::nothrow) ZeroCopyAclGraph());
     131          391 :         if (zeroCopyAclGraph_ == nullptr)
     132              :         {
     133            0 :             HCCL_ERROR("new ZeroCopyAclGraph failed!");
     134              :         }
     135          391 :         commConfig_ = CommConfig();
     136          391 :         dpuManager_.reset(new (std::nothrow) DpuManager());
     137          391 :         if (dpuManager_ == nullptr) {
     138            0 :             HCCL_ERROR("new DpuManager failed!");
     139              :         }
     140          391 :     }
     141              : 
     142          413 :     HcclCommunicator::HcclCommunicator(const CommConfig &commConfig)
     143          413 :         : dispatcher_(nullptr), vDispatcher_(nullptr), notifyPool_(nullptr),
     144          413 :           initializedFlag_(ATOMIC_FLAG_INIT), userRank_(INVALID_VALUE_RANKID), realUserRank_(INVALID_VALUE_RANKID),
     145         1239 :           userRankSize_(INVALID_VALUE_RANKSIZE), drvInit_(false), inlineReduceSwitchOn_(true),
     146          413 :           nicDeployment_(NICDeployment::NIC_DEPLOYMENT_DEVICE), devicePhyId_(INVALID_UINT),
     147          826 :           deviceLogicId_(-1), localRank_(INVALID_VALUE_RANKID), hostSocketHandle_(nullptr),
     148          826 :           isUsedRdmaLevel0_(false), nicInitialized_(0), hcomGroupNicInit_(false),
     149          826 :           profilingMode_(HcomProfilingMode::PROFILING_CLOSE), raResourceInit_(false),
     150          413 :           interServer_(false), isSingleMeshAggregation_(false), cclBufferManager_(CCLBufferManager()),
     151          413 :           isExecuteProfilingInit_(false), deviceType_(DevType::DEV_TYPE_COUNT),
     152          413 :           commHandle_(nullptr),
     153         1239 :           commWorkMode_(WorkMode::HCCL_MODE_NORMAL), meshAggregationRankSize_(0), isHaveCpuRank_(false), ranktableCrc_(0),
     154          413 :           multiModuleDiffDeviceNumMode_(false), multiSuperPodDiffServerNumMode_(false),
     155          412 :           isStandardCard_(false), is310PDuoCard_(false), hccsPortNum_(-1),
     156          826 :           loopBackIp_(HcclIpAddress(COMM_LOOPBACK_IP)), profilingInitiated_(false), callbackThreadId_(INVALID_U64),
     157          413 :           role_(SERVER_ROLE_SOCKET),
     158          412 :           isHostUseDevNic_(false),
     159       318252 :           isAllRankSamePlane_(false), serverNum_(0), moduleNum_(0)
     160              :     {
     161          413 :         zeroCopyAclGraph_.reset(new (std::nothrow) ZeroCopyAclGraph());
     162          413 :         if (zeroCopyAclGraph_ == nullptr)
     163              :         {
     164            0 :             HCCL_ERROR("new ZeroCopyAclGraph failed!");
     165              :         }
     166          413 :         commConfig_ = commConfig;
     167          413 :         dpuManager_.reset(new (std::nothrow) DpuManager());
     168          413 :         if (dpuManager_ == nullptr) {
     169            0 :             HCCL_ERROR("new DpuManager failed!");
     170              :         }
     171          413 :     }
     172              : 
     173         7768 :     HcclCommunicator::~HcclCommunicator()
     174              :     {
     175          804 :         HCCL_DEBUG("Enter ~HcclCommunicator.");
     176              : 
     177          804 :         DeinitZeroCopyMemoryAgent(true);
     178          804 :         if (!isInvalidComm_) {
     179          804 :             (void)DestroyAicpuComm();
     180          804 :             (void)UnRegisterBackGroundThread();
     181              :         } else {
     182            0 :             HCCL_WARNING("The comm[%s] is invalid in snapshot, rank[%u]. deviceLogicId[%u]. "
     183              :                 "There is no aicpu comm in device, skip aicpu comm destroy in destructor.",
     184              :                 identifier_.c_str(), userRank_, deviceLogicId_);
     185              :         }
     186              : 
     187          804 :         if (dpuManager_ != nullptr) {
     188          804 :             (void)dpuManager_->DeInitDpuKernel();
     189          803 :             dpuManager_ = nullptr;
     190              :         }
     191              : 
     192          803 :         UnRegisterToHeartBeat();
     193          801 :         DeleteOpInfoToHeartBeat();
     194          804 :         AlgWrap::GetInstance().UnregisterAlgCallBack(identifier_);
     195          804 :         DetectConnectionAnomalies::GetInstance(deviceLogicId_).Deinit();
     196          804 :         UnRegisterToCommConfiger();
     197          804 :         AclgraphCallback::GetInstance().CleanCaptureRes(this);
     198              : 
     199          804 :         if (zeroCopyAclGraph_ != nullptr) {
     200          804 :             zeroCopyAclGraph_ = nullptr;
     201              :         }
     202              : 
     203          804 :         if (implAlg_ != nullptr) {
     204          520 :             implAlg_ = nullptr;
     205              :         }
     206              : 
     207          878 :         for (auto &res : resMap_) {
     208           74 :             DestroyAlgResource(res.second);
     209              :         }
     210              : 
     211          804 :         if (releaseChannel_ != nullptr) {
     212          403 :             releaseChannel_();
     213              :         }
     214              : 
     215          804 :         if (opRetryManager_ != nullptr) {
     216            0 :             OpRetryManager::DeleteLinkInfoByIdentifier(deviceLogicId_, identifier_);
     217            0 :             opRetryManager_->UnRegisterOpRetryManager(identifier_);
     218            0 :             opRetryManager_ = nullptr;
     219              :         }
     220              : 
     221          804 :         if (IsEnableBackupLink()) {
     222            0 :             if (g_enableBackupLinkCommCount.load() == 0) {
     223            0 :                 HCCL_ERROR("[Destroy] g_enableBackupLinkCommCount is 0");
     224              :             } else {
     225            0 :                 g_enableBackupLinkCommCount--;
     226              :             }
     227              :         }
     228              : 
     229          804 :         resMap_.clear();
     230          804 :         deviceResOrigMem_.clear();
     231          804 :         hostResMap_.clear();
     232          804 :         tagCommInfo_.clear();
     233          804 :         tagWorkSpaceMem_.clear();
     234          804 :         tagStreamInfo_.clear();
     235              : 
     236          803 :         if (opRetryStreamPtr_ != nullptr) {
     237          519 :             opRetryStreamPtr_->clear();
     238          518 :             opRetryStreamPtr_ = nullptr;
     239              :         }
     240              : 
     241          803 :         OrderLaunch::GetInstance(deviceLogicId_).UnRegisterOrderLaunch(identifier_);
     242         2412 :         for (u32 i = 0; i < AICPU_LOCAL_EVENT_SIZE; ++i) {
     243         1608 :             if (localAicpuOpEvent_[i] != nullptr) {
     244            0 :                 (void)hrtEventDestroy(localAicpuOpEvent_[i]);
     245            0 :                 localAicpuOpEvent_[i] = nullptr;
     246              :             }
     247              :         }
     248              : 
     249          804 :         (void)UnRegistTaskExceptionHandler();
     250          804 :         for (auto streamId : aicpuStreamIds_) {
     251            0 :             UnregisterGetAicpuTaskExceptionCallBack(streamId, deviceLogicId_);
     252              :         }
     253          804 :         aicpuStreamIds_.clear();
     254          804 :         kfcControlTransferH2D_ = nullptr;
     255          804 :         kfcStatusTransferD2H_ = nullptr;
     256          804 :         customControlTransferH2D_ = nullptr;
     257          804 :         customStatusTransferD2H_ = nullptr;
     258              : 
     259          804 :         oneSideService_ = nullptr;
     260          804 :         if (isOneSidedServiceNetDevCtxInited) {
     261            0 :             DeInitOneSidedServiceNetDevCtx();
     262              :         }
     263              : 
     264              :         /* 网络资源销毁 */
     265          804 :         DestroyNetworkResources();
     266          804 :         notifyPool_ = nullptr;
     267          804 :         queueNotifyManager_ = nullptr;
     268              :         /* driver关联资源释放 */
     269          804 :         if (drvInit_){
     270          305 :             if (DisablePreResource() != HCCL_SUCCESS) {
     271            0 :                 HCCL_WARNING("driver resource is not released successfully");
     272              :             }
     273              :         }
     274              : 
     275          804 :         if (isExecuteProfilingInit_) {
     276          488 :             (void)DeinitProfiling();
     277              :         }
     278              : 
     279          804 :         if (OpExeCounter::GetInstance(deviceLogicId_).DeInitCounter() != HCCL_SUCCESS) {
     280            0 :             HCCL_WARNING("op exec counter resource free failed");
     281              :         }
     282              : 
     283              :         /* 销毁当前trace句柄 */
     284          804 :         if (opBaseAtraceInfo_ != nullptr) {
     285          521 :             opBaseAtraceInfo_->DeInit();
     286          521 :             opBaseAtraceInfo_ = nullptr;
     287              :         }
     288              : 
     289          802 :         ReleaseWorkSpacebuffer();
     290          803 :         ReleaseCommContextbuffer();
     291              : 
     292         7236 :         for (u32 i = 0; i < AICPU_LOCAL_NOTIFY_SIZE; i++) {
     293         6432 :             if (localAiCpuOpNotify_[i]) {
     294            0 :                 HcclResult ret = localAiCpuOpNotify_[i]->Destroy();
     295            0 :                 localAiCpuOpNotify_[i] = nullptr;
     296            0 :                 if (ret != RT_ERROR_NONE) {
     297            0 :                     HCCL_ERROR("[Destroy][AicpuNotify]errNo[0x%016llx] rt notify destroy fail, "
     298              :                                "aicpuOpNotify[%u] return[%d].",
     299              :                                HCCL_ERROR_CODE(HCCL_E_RUNTIME), i, ret);
     300              :                 }
     301              :             }
     302              :         }
     303              : 
     304          805 :         while (!aiCpuNoIpcEvnet_.empty()) {
     305            1 :             rtEvent_t eventInfo = aiCpuNoIpcEvnet_.back();
     306            1 :             HcclResult ret = hrtEventDestroy(eventInfo);
     307            1 :             if (ret != HCCL_SUCCESS) {
     308            0 :                 HCCL_ERROR("[Destroy][AicpuNoIpcEvnet]errNo[0x%016llx] rt event destroy fail, "
     309              :                            "return[%d].",
     310              :                            HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret);
     311              :             }
     312            1 :             aiCpuNoIpcEvnet_.pop_back();
     313              :         }
     314              : 
     315          801 :         UnloadAICPUKernel();
     316          803 :         UnloadCustomKernel();
     317          804 :         if (dispatcher_ != nullptr) {
     318          521 :             HcclDispatcherDestroy(dispatcher_);
     319          519 :             dispatcher_ = nullptr;
     320              :         }
     321          802 :         if (dispatcherCtx_ != nullptr) {
     322          519 :             DestroyDispatcherCtx(dispatcherCtx_, identifier_.c_str());
     323          521 :             dispatcherCtx_ = nullptr;
     324              :         }
     325          804 :         if (vDispatcher_ != nullptr) {
     326          521 :             HcclDispatcherDestroy(vDispatcher_);
     327          521 :             vDispatcher_ = nullptr;
     328              :         }
     329          804 :         if (deviceType_ == DevType::DEV_TYPE_910B || deviceType_ == DevType::DEV_TYPE_910_93){
     330          212 :             UnRegisterFromSnapshot();
     331              :         }
     332          804 :         HCCL_DEBUG("~HcclCommunicator success.");
     333         9373 :     }
     334              : 
     335          520 :     HcclResult HcclCommunicator::SaveTopoDesc(std::string &identifier)
     336              :     {
     337          520 :         CommTopo topoType = CommTopo::COMM_TOPO_RESERVED;
     338          520 :         CHK_RET(GetInstTopoTypeByNetLayer(0, &topoType)); // layer 0
     339              : 
     340          520 :         CommTopoDesc::GetInstance().SaveRankSize(identifier, userRankSize_);
     341          520 :         CommTopoDesc::GetInstance().SaveL0TopoType(identifier, topoType);
     342          520 :         return HCCL_SUCCESS;
     343              :     }
     344              : 
     345          498 :     HcclResult HcclCommunicator::Init(HcclCommParams &params, const RankTable_t &rankTable)
     346              :     {
     347          498 :         CHK_RET(InitCommParams(params));
     348          498 :         CHK_RET(attrCollector_.Init(params, rankTable, commConfig_.GetConfigHcclAlgoMap()));
     349          495 :         CHK_RET(InitRankInfo(rankTable));
     350          495 :         CHK_RET(InitNetResource(rankTable));
     351          488 :         CHK_RET(InitDebug());
     352          488 :         CHK_RET(InitNotifyManager());
     353          488 :         CHK_RET(InitStreamManager());
     354          488 :         CHK_RET(InitProfiler());
     355          488 :         CHK_RET(InitDispatcher());
     356          488 :         CHK_RET(InitTransportManager());
     357          487 :         CHK_RET(InitCombinOpara());
     358          488 :         CHK_RET(RegisterRanksToDca());
     359              :         /*--------------加锁区--------------*/
     360          486 :         std::unique_lock<std::mutex> lock(g_hcomInitMutex);
     361          488 :         CHK_RET(RegistTaskExceptionHandler());
     362              : 
     363          488 :         attrCollector_.GenCollectiveId(params, rankTable);
     364          488 :         collectiveId_ = attrCollector_.GetCollectiveId();
     365              : 
     366              :         // 初始化参数(需要放置在ranktable解析之后)
     367          488 :         HcclResult ret = InitPara();
     368          488 :         CHK_PRT_RET(ret != HCCL_SUCCESS,
     369              :                     HCCL_ERROR("[HcclCommunicator][Init]errNo[0x%016llx] collectiveid[%s] parameter initialization failed",
     370              :                                HCCL_ERROR_CODE(ret), params.id.internal),
     371              :                     ret);
     372          488 :         lock.unlock();
     373              :         /*--------------加锁区--------------*/
     374          488 :         if (deviceType_ == DevType::DEV_TYPE_910B || deviceType_ == DevType::DEV_TYPE_910_93){
     375          196 :             CHK_RET(RegisterKernel(deviceType_));
     376              :         }
     377          488 :         CHK_RET(LoadCustomKernel());
     378          488 :         CHK_RET(LoadAICPUKernel());
     379          488 :         CHK_RET(InitHDCommunicate());
     380          488 :         CHK_RET(InitOpRetry());
     381          488 :         CHK_RET(InitOpResPara());
     382              :         
     383          488 :         CHK_RET(InitOneSidedService(rankTable));
     384          488 :         CHK_RET(OrderLaunch::GetInstance(deviceLogicId_).RegisterOrderLaunch(identifier_));
     385          488 :         HcclTopoAttr topoAttr;
     386          488 :         attrCollector_.GetTopoAttr(topoAttr);
     387          488 :         CHK_RET(rankGraph_.Init(rankTable, topoAttr));
     388          488 :         CHK_RET(SaveTopoDesc(params.identifier));
     389          488 :         CHK_RET(RegisterToSnapshot());
     390          488 :         CHK_RET(InitSymmetricMemory());
     391              : 
     392          488 :         CHK_RET(InitMyRankConnectMode(params, rankTable));
     393          488 :         if (dpuManager_ != nullptr && myRankConnectMode_) { /* 当前只有host nic--device nic使用 */
     394            0 :             CHK_RET(dpuManager_->Init(identifier_, deviceLogicId_));
     395              :         }
     396              : 
     397          488 :         return HCCL_SUCCESS;
     398          488 :     }
     399              : 
     400           33 :     HcclResult HcclCommunicator::Init(HcclCommParams &params, const std::vector<RankInfo> &rankList,
     401              :                                       WorldGroupInfo &groupCommonData)
     402              :     {
     403           33 :         CHK_RET(InitCommParams(params));
     404           33 :         CHK_RET(attrCollector_.Init(params, rankList, groupCommonData, commConfig_.GetConfigHcclAlgoMap()));
     405           33 :         CHK_RET(InitRankInfoSubGroup(groupCommonData));
     406           33 :         CHK_RET(InitDebugSubGroup());
     407           33 :         CHK_RET(InitNotifyManager());
     408           33 :         CHK_RET(InitDispatcher());
     409           33 :         CHK_RET(InitStreamManager());
     410           33 :         CHK_RET(InitRaResource());
     411           33 :         CHK_RET(InitTransportManager());
     412           33 :         CHK_RET(InitHcclAlg());
     413           32 :         CHK_RET(LoadCustomKernel());
     414           32 :         CHK_RET(LoadAICPUKernel());
     415           32 :         CHK_RET(InitHDCommunicate());
     416           32 :         CHK_RET(InitOpRetry());
     417           32 :         CHK_RET(InitOpResPara());
     418           32 :         CHK_RET(RegisterRanksToDca());
     419           32 :         CHK_RET(OrderLaunch::GetInstance(deviceLogicId_).RegisterOrderLaunch(identifier_));
     420           32 :         HcclTopoAttr topoAttr;
     421           32 :         attrCollector_.GetTopoAttr(topoAttr);
     422           32 :         CHK_RET(rankGraph_.Init(topoAttr));
     423           32 :         CHK_RET(SaveTopoDesc(params.identifier));
     424           32 :         CHK_RET(RegisterToSnapshot());
     425           32 :         CHK_RET(InitSymmetricMemory());
     426           32 :         return HCCL_SUCCESS;
     427           32 :     }
     428              : 
     429          520 :     HcclResult HcclCommunicator::LoadAICPUKernel(void)
     430              :     {
     431          520 :         if (binHandle_ == nullptr) {
     432          520 :             std::string jsonPath;
     433          520 :             CHK_RET(GetKernelFilePath(jsonPath));
     434          520 :             jsonPath += "ccl_kernel.json";
     435          520 :             HcclResult ret = LoadBinaryFromFile(jsonPath.c_str(), ACL_RT_BINARY_LOAD_OPT_CPU_KERNEL_MODE, 0,
     436          520 :                 binHandle_);
     437          520 :             CHK_PRT_RET(ret != HCCL_SUCCESS,
     438              :                 HCCL_ERROR("[LoadAICPUKernel]errNo[0x%016llx]load aicpu file fail, path[%s] optionType[%u]"
     439              :                 "cpuKernelMode[%u].", ret, jsonPath.c_str(), ACL_RT_BINARY_LOAD_OPT_CPU_KERNEL_MODE, 0), ret);
     440          520 :         }
     441          520 :         return HCCL_SUCCESS;
     442              :     }
     443              : 
     444          804 :     void HcclCommunicator::UnloadAICPUKernel(void)
     445              :     {
     446          804 :         if (binHandle_ != nullptr) {
     447            3 :             aclError aclRet = aclrtBinaryUnLoad(binHandle_);
     448            3 :             if (aclRet != ACL_SUCCESS) {
     449            0 :                 HCCL_ERROR("[UnloadAICPUKernel]errNo[0x%016llx] unload binary from binHandel[%p] error.",
     450              :                 aclRet, binHandle_);
     451              :             }
     452            3 :             binHandle_ = nullptr;
     453              :         }
     454          804 :         return;
     455              :     }
     456              : 
     457          520 :     HcclResult HcclCommunicator::LoadCustomKernel(void)
     458              :     {
     459              :         // 加载自定义算子
     460              :         // 请勿删除,该函数为用户自定义算子时使用,应加载句柄
     461              :         // 读取customEnable环境变量,开启了就执行
     462          520 :         std::string jsonPath;
     463          520 :         CHK_RET(GetCustomKernelFilePath(jsonPath));
     464          520 :         jsonPath += "libaicpu_custom.json";
     465          520 :         CHK_RET(LoadCustomFile(jsonPath.c_str(), ACL_RT_BINARY_LOAD_OPT_CPU_KERNEL_MODE, 1, binHandle_));
     466          520 :         return HCCL_SUCCESS;
     467          520 :     }
     468              : 
     469          804 :     void HcclCommunicator::UnloadCustomKernel(void)
     470              :     {
     471              :         // 卸载自定义算子
     472              :         // 请勿删除,该函数为用户自定义算子时使用,应释放句柄:UnloadBinary(binCustomHandle_);
     473          804 :         return;
     474              :     }
     475              : 
     476          488 :     HcclResult HcclCommunicator::InitOneSidedService(const RankTable_t &rankTable)
     477              :     {
     478          488 :         EXCEPTION_CATCH((oneSideService_ = std::make_unique<HcclOneSidedService>(socketManager_, notifyPool_, commConfig_)),
     479              :                          return HCCL_E_INTERNAL);
     480          488 :         hcclRankLinkInfo_.userRank = userRank_;
     481          488 :         hcclRankLinkInfo_.devicePhyId = devicePhyId_;
     482              : 
     483          488 :         if (devIpAddr_.empty()) {
     484            0 :             HCCL_ERROR("[%s] device ip is invalid, please set device ip first.", __func__);
     485            0 :             return HCCL_E_NOT_FOUND;
     486              :         }
     487          488 :         hcclRankLinkInfo_.ip = devIpAddr_[0];
     488          488 :         if (nicRanksPort_.size() <= userRank_) {
     489            0 :             HCCL_ERROR("[%s] userRank_[%u] port is invalid, please set port first", __func__, userRank_);
     490            0 :             return HCCL_E_NOT_FOUND;
     491              :         }
     492          488 :         hcclRankLinkInfo_.port = nicRanksPort_[userRank_];
     493          488 :         hcclRankLinkInfo_.socketsPerLink = 1;
     494          488 :         HCCL_DEBUG("[%s]hcclRankLinkInfo_ userRank[%u], devicePhyId[%u], ip[%s], port[%u]", __func__,
     495              :                    hcclRankLinkInfo_.userRank, hcclRankLinkInfo_.devicePhyId, hcclRankLinkInfo_.ip.GetReadableIP(),
     496              :                    hcclRankLinkInfo_.port);
     497          488 :         CHK_RET(oneSideService_->Config(dispatcher_, hcclRankLinkInfo_, &rankTable, identifier_, isStandardCard_, enableP2PRankIds_));
     498          488 :         return HCCL_SUCCESS;
     499              :     }
     500              : 
     501            0 :     HcclResult HcclCommunicator::InitOneSidedServiceNetDevCtx(u32 remoteRankId)
     502              :     {
     503            0 :         if (nicDeployment_ != NICDeployment::NIC_DEPLOYMENT_DEVICE) {
     504              :             // 单边操作当前只支持Device网卡,不支持host
     505            0 :             HCCL_ERROR("[%s]nicDeployment_[%d], userRankSize_[%u], do not support oneSidedService.",
     506              :                        __func__, nicDeployment_, userRankSize_);
     507            0 :             return HCCL_E_INTERNAL;
     508              :         }
     509              : 
     510            0 :         std::string localServerId = serverId_;
     511            0 :         std::string localSuperPodId = superPodId_;
     512            0 :         std::string remoteServerId = rankInfoList_.at(remoteRankId).serverId;
     513            0 :         std::string remoteSuperPodId = rankInfoList_.at(remoteRankId).superPodId;
     514            0 :         u32 intraRoceSwitch = GetExternalInputIntraRoceSwitch();
     515            0 :         bool useRdma = false;
     516            0 :         if (intraRoceSwitch ||
     517            0 :             (!useSuperPodMode_ && localServerId != remoteServerId) ||
     518            0 :             (localSuperPodId != remoteSuperPodId)) {
     519              :             // 1. 初始化网口
     520            0 :             CHK_RET(InitNic());
     521            0 :             isOneSidedServiceNicInited = true;
     522              : 
     523              :             // 2. 单边操作SetNetDevCtx, RDMA
     524            0 :             if (netDevCtxMap_.find(devIpAddr_[0]) == netDevCtxMap_.end()) {
     525            0 :                 HCCL_ERROR("[%s] nicDeployment_[%d], device nic init fail, please check", __func__, nicDeployment_);
     526            0 :                 return HCCL_E_NOT_FOUND;
     527              :             }
     528            0 :             useRdma = true;
     529            0 :             oneSideService_->SetNetDevCtx(netDevCtxMap_[devIpAddr_[0]], useRdma);
     530            0 :             HCCL_INFO("[%s]init device Nic for oneSidedService success.", __func__);
     531              :         }else {
     532              :             // 单边操作SetNetDevCtx, IPC
     533            0 :             oneSideService_->SetNetDevCtx(netDevCtxMap_[localVnicIp_], useRdma);
     534            0 :             HCCL_INFO("[%s]init vNic for oneSidedService success.", __func__);
     535              :         }
     536            0 :         isOneSidedServiceNetDevCtxInited = true;
     537            0 :         HCCL_DEBUG("[%s]nicDeployment_[%d], intraRoceSwitch[%u]", __func__, nicDeployment_, intraRoceSwitch);
     538            0 :         return HCCL_SUCCESS;
     539            0 :     }
     540              : 
     541            0 :     HcclResult HcclCommunicator::DeInitOneSidedServiceNetDevCtx()
     542              :     {
     543            0 :         if (nicDeployment_ != NICDeployment::NIC_DEPLOYMENT_DEVICE) {
     544              :             // 单边操作当前只支持Device网卡,不支持host
     545            0 :             HCCL_ERROR("[%s]nicDeployment_[%d], userRankSize_[%u], do not support oneSidedService.",
     546              :                        __func__, nicDeployment_, userRankSize_);
     547            0 :             return HCCL_E_INTERNAL;
     548              :         }
     549            0 :         if (isOneSidedServiceNicStartListen_) {
     550            0 :             socketManager_->DestroySockets();
     551            0 :             u32 port = GetLocalNicPort(NicType::DEVICE_NIC_TYPE);
     552            0 :             CHK_RET(socketManager_->ServerDeInit(onesidedServiceNicIpAddr_, port));
     553            0 :             isOneSidedServiceNicStartListen_ = false;
     554            0 :             HCCL_INFO("[HcclCommunicator][%s] DeInit socket server success.tag[%s].", __func__, identifier_.c_str());
     555              :         }
     556            0 :         u32 intraRoceSwitch = GetExternalInputIntraRoceSwitch();
     557            0 :         if (isOneSidedServiceNicInited) {
     558              :             // 1. close sockets
     559            0 :             if (raResourceInit_) {
     560            0 :                 socketManager_->DestroySockets();
     561              :             }
     562              :             // 2. 去初始化网口
     563            0 :             CHK_RET(DeinitNic());
     564            0 :             isOneSidedServiceNicInited = false;
     565            0 :             HCCL_INFO("[%s]Deinit device Nic for oneSidedService success.", __func__);
     566              :         }
     567            0 :         isOneSidedServiceNetDevCtxInited = false;
     568            0 :         HCCL_DEBUG("[%s]nicDeployment_[%d], intraRoceSwitch[%u]", __func__, nicDeployment_, intraRoceSwitch);
     569            0 :         return HCCL_SUCCESS;
     570              :     }
     571              : 
     572            0 :     HcclResult HcclCommunicator::GetOneSidedService(IHcclOneSidedService **service)
     573              :     {
     574            0 :         *service = oneSideService_.get();
     575            0 :         return HCCL_SUCCESS;
     576              :     }
     577              : 
     578            0 :     HcclResult HcclCommunicator::OneSidedServiceStartListen(NicType nicType, HcclNetDevCtx netDevCtx)
     579              :     {
     580            0 :         HCCL_INFO("[HcclCommunicator][%s] Start prepare netDevCtx.", __func__);
     581            0 :         u32 port = GetLocalNicPort(nicType);
     582            0 :         CHK_RET(socketManager_->ServerInit(netDevCtx, port));
     583            0 :         if (nicType == NicType::DEVICE_NIC_TYPE) {
     584            0 :             CHK_RET(HcclNetDevGetLocalIp(netDevCtx, onesidedServiceNicIpAddr_));
     585            0 :             isOneSidedServiceNicStartListen_ = true;
     586              :         }
     587            0 :         isOneSidedServiceNetDevCtxInited = true;
     588            0 :         HCCL_INFO("[HcclCommunicator][%s] netDevCtx[%p] port[%u] server init success.", __func__, netDevCtx, port);
     589            0 :         return HCCL_SUCCESS;
     590              :     }
     591              : 
     592            0 :     HcclResult HcclCommunicator::GetOneSidedServiceDevIpAndPort(NicType nicType, HcclIpAddress &ipAddress, u32& port)
     593              :     {
     594            0 :         if (nicDeployment_ != NICDeployment::NIC_DEPLOYMENT_DEVICE) {
     595              :             // 单边操作当前只支持Device网卡,不支持host
     596            0 :             HCCL_ERROR("[%s]nicDeployment_[%d], userRankSize_[%u], do not support oneSidedService.",
     597              :                        __func__, nicDeployment_, userRankSize_);
     598            0 :             return HCCL_E_INTERNAL;
     599              :         }
     600            0 :         port = GetLocalNicPort(nicType);
     601            0 :         if (nicType == NicType::VNIC_TYPE) {
     602            0 :             ipAddress = localVnicIp_;
     603            0 :             HCCL_INFO("[GetOneSidedServiceDevIpAddr] vnic ipAddress[%s] get success.", ipAddress.GetReadableAddress());
     604            0 :             return HCCL_SUCCESS;
     605            0 :         }else if (nicType == NicType::DEVICE_NIC_TYPE) {
     606            0 :             u32 nicNum = devIpAddr_.size();
     607            0 :             for (u32 i = 0; i < nicNum; i++) {
     608            0 :                 if (devIpAddr_[i].IsInvalid()) {
     609            0 :                     HCCL_INFO("[GetOneSidedServiceDevIpAddr]nic num[%u] deviceip is invalid, total nicNum[%u]", i, nicNum);
     610            0 :                     continue;
     611              :                 }
     612            0 :                 ipAddress = devIpAddr_[i];
     613            0 :                 HCCL_INFO("[GetOneSidedServiceDevIpAddr] nic ipAddress[%s] get success.", ipAddress.GetReadableAddress());
     614            0 :                 return HCCL_SUCCESS;
     615              :             }
     616              :         }
     617            0 :         HCCL_ERROR("[HcclCommunicator][%s] ipAddress get fail. tag[%s]", __func__, identifier_.c_str());
     618            0 :         return HCCL_E_NOT_FOUND;
     619              :     }
     620              : 
     621            0 :     HcclResult HcclCommunicator::DeinitOneSidedService()
     622              :     {
     623            0 :         if (oneSideService_ != nullptr) {
     624            0 :             CHK_RET(oneSideService_->DeInit());
     625              :         }
     626            0 :         return HCCL_SUCCESS;
     627              :     }
     628              : 
     629           80 :     bool HcclCommunicator::IsSupportSymmetricMemory(HcclCMDType opType, OpParam &opParam)
     630              :     {
     631           80 :         CHK_PRT_RET(symmetricMemory_ == nullptr, HCCL_DEBUG("symmetricMemory_ is a nullptr"), false);
     632           14 :         HCCL_INFO("[%s] aicpuUnfold[%d], workflowMode[%d], deviceType[%d], "
     633              :             "deviceNumPerAggregation_[%d], multiModuleDiffDeviceNumMode_[%d], tag[%s].",
     634              :             __func__, opParam.aicpuUnfoldMode, GetWorkflowMode(), deviceType_,
     635              :             deviceNumPerAggregation_, multiModuleDiffDeviceNumMode_, opParam.tag.c_str());
     636              : 
     637              :         // 目前只支持allgather, allreduce, reducescatter
     638           14 :         CHK_PRT_RET(opType != HcclCMDType::HCCL_CMD_ALLGATHER && 
     639              :                     opType != HcclCMDType::HCCL_CMD_ALLREDUCE &&
     640              :                     opType != HcclCMDType::HCCL_CMD_ALLTOALL &&
     641              :                     opType != HcclCMDType::HCCL_CMD_REDUCE_SCATTER,
     642              :                     HCCL_INFO("[%s] opType[%d] not support symmetric memory", 
     643              :                             __func__, opType),
     644              :                     false);
     645              : 
     646              :         // 只支持aicpu展开、单算子模式、910_93芯片
     647           14 :         CHK_PRT_RET(!opParam.aicpuUnfoldMode,
     648              :                     HCCL_INFO("[%s] aicpuUnfold:%d not support symmetric memory", __func__, opParam.aicpuUnfoldMode), false);
     649            4 :         CHK_PRT_RET(GetWorkflowMode() != HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE,
     650              :                     HCCL_INFO("[%s] workflowMode:%d not support symmetric memory", __func__, GetWorkflowMode()), false);
     651            3 :         CHK_PRT_RET(deviceType_ != DevType::DEV_TYPE_910_93,
     652              :                     HCCL_INFO("[%s] deviceType:%d not support symmetric memory", __func__, deviceType_), false);
     653            3 :         CHK_PRT_RET(superPodNum_ == 1 && serverNum_ > 1 && GetExternalInputInterHccsDisable(),
     654              :                     HCCL_INFO("[%s] mutilSever use roce not support symmetric memory", __func__), false);
     655              : 
     656              :         // 判断拓扑逻辑是否支持symmetric memory
     657              :         // 每个节点只有一张卡或节点间非对称场景不支持对称内存
     658            3 :         CHK_PRT_RET(deviceNumPerAggregation_ == 1 || multiModuleDiffDeviceNumMode_,
     659              :                     HCCL_INFO("[%s] deviceNumPerAggregation[%u], multiModuleDiffDeviceNumMode_[%d] not support symmetric memory",
     660              :                               __func__, deviceNumPerAggregation_, multiModuleDiffDeviceNumMode_),
     661              :                     false);
     662              : 
     663              :         // 判断输入输出地址是否都注册为对称内存
     664            2 :         HcclResult ret = symmetricMemory_->FindSymmetricWindow(opParam.inputPtr, opParam.inputSize, &opParam.inputSymWindow, &opParam.inputOffset);
     665            2 :         CHK_PRT_RET(ret != HCCL_SUCCESS || opParam.inputSymWindow == nullptr,
     666              :                     HCCL_INFO("[%s] input[%p] size[%llu] is not support symmetric memory", __func__, opParam.inputPtr, opParam.inputSize), false);
     667            1 :         ret = symmetricMemory_->FindSymmetricWindow(opParam.outputPtr, opParam.outputSize, &opParam.outputSymWindow, &opParam.outputOffset);
     668            1 :         CHK_PRT_RET(ret != HCCL_SUCCESS || opParam.outputSymWindow == nullptr,
     669              :                     HCCL_INFO("[%s] output[%p] size[%llu] is not support symmetric memory", __func__, opParam.outputPtr, opParam.outputSize), false);
     670              :         
     671            1 :         HCCL_INFO("[HcclCommunicator][IsSupportSymmetricMemory] opParam.inputPtr[%p], inputOffset[%llu], inputSymWindow[%p]",
     672              :                     opParam.inputPtr, opParam.inputOffset, opParam.inputSymWindow);
     673            1 :         HCCL_INFO("[HcclCommunicator][IsSupportSymmetricMemory] opParam.outputPtr[%p], outputOffset[%llu], outputSymWindow[%p]",
     674              :                     opParam.outputPtr, opParam.outputOffset, opParam.outputSymWindow);
     675              : 
     676            1 :         return true;
     677              :     }
     678              : 
     679           78 :     bool HcclCommunicator::IsSupportZeroCopy(const OpParam &opParam)
     680              :     {
     681           78 :         HCCL_INFO("[%s] aicpuUnfold[%d], workflowMode[%d], deviceType[%d], "
     682              :             "deviceNumPerAggregation_[%d], multiModuleDiffDeviceNumMode_[%d], tag[%s].",
     683              :             __func__, opParam.aicpuUnfoldMode, GetWorkflowMode(), deviceType_,
     684              :             deviceNumPerAggregation_, multiModuleDiffDeviceNumMode_, opParam.tag.c_str());
     685              : 
     686              :         // 只支持aicpu展开、非重执行、单算子模式、910_93芯片
     687           74 :         CHK_PRT_RET(!opParam.aicpuUnfoldMode,
     688              :                     HCCL_INFO("[%s] aicpuUnfold:%d not support zero copy", __func__, opParam.aicpuUnfoldMode), false);
     689            4 :         CHK_PRT_RET(GetWorkflowMode() != HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE,
     690              :                     HCCL_INFO("[%s] workflowMode:%d not support zero copy", __func__, GetWorkflowMode()), false);
     691            0 :         CHK_PRT_RET(deviceType_ != DevType::DEV_TYPE_910_93,
     692              :                     HCCL_INFO("[%s] deviceType:%d not support zero copy", __func__, deviceType_), false);
     693              : 
     694              :         // 判断拓扑逻辑是否支持zero copy
     695              :         // 每个节点只有一张卡或节点间非对称场景不支持零拷贝
     696            0 :         CHK_PRT_RET(deviceNumPerAggregation_ == 1 || multiModuleDiffDeviceNumMode_,
     697              :                     HCCL_INFO("[%s] deviceNumPerAggregation[%u], multiModuleDiffDeviceNumMode_[%d] not support zero copy",
     698              :                               __func__, deviceNumPerAggregation_, multiModuleDiffDeviceNumMode_),
     699              :                     false);
     700              : 
     701              :         // 判断输入输出地址是否都是支持零Copy特性的
     702            0 :         CHK_PRT_RET(!ZeroCopyMemoryAgent::IsActivateCommMemoryAddr(opParam.inputPtr, opParam.inputSize),
     703              :                     HCCL_INFO("[%s] input[%p] size[%llu] is not support zero copy", __func__, opParam.inputPtr, opParam.inputSize), false);
     704            0 :         CHK_PRT_RET(!ZeroCopyMemoryAgent::IsActivateCommMemoryAddr(opParam.outputPtr, opParam.outputSize),
     705              :                     HCCL_INFO("[%s] output[%p] size[%llu] is not support zero copy", __func__, opParam.outputPtr, opParam.outputSize), false);
     706              : 
     707            0 :         return true;
     708              :     }
     709              : 
     710           73 :     HcclResult HcclCommunicator::PrepareZeroCopy(const std::string &algName, const AlgDesc &algDesc, OpParam &opParam)
     711              :     {
     712           73 :         if (!algDesc.isZeroCopy) {
     713           73 :             opParam.supportSymmetricMemory = false;     //  当前对称内存与零拷贝算法绑定,对称内存使能关闭,确保aicpu侧不走对称内存分支
     714           73 :             HCCL_INFO("[HcclCommunicator][PrepareZeroCopy] algName[%s] not support zerocopy.", algName.c_str());
     715           78 :             return HCCL_SUCCESS;
     716              :         }
     717              : 
     718            0 :         if (opParam.supportSymmetricMemory) {
     719            0 :             HCCL_INFO("[HcclCommunicator][PrepareZeroCopy] algName[%s] symmetric memory is enabled, not use zerocopy.",
     720              :                       algName.c_str());
     721            0 :             return HCCL_SUCCESS;
     722              :         }
     723              :         // ARS特性不支持零拷贝
     724            0 :         if ((opParam.opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER || opParam.opType == HcclCMDType::HCCL_CMD_ALLGATHER ||
     725            0 :                 opParam.opType == HcclCMDType::HCCL_CMD_ALLREDUCE) && deviceType_ == DevType::DEV_TYPE_910_93 && 
     726            0 :                 multiModuleDiffDeviceNumMode_ && !multiSuperPodDiffDeviceNumMode_){
     727            0 :             return HCCL_SUCCESS;
     728              :         }
     729              : 
     730              :         // 如果自己侧的共享内存没有申请,那么进行申请,并设置给transportManager,后续p2p建链时进行交换
     731            0 :         if (zeroCopyLocalBuffer_.ptr() == nullptr) {
     732            0 :             CHK_RET(DeviceMem::alloc(zeroCopyLocalBuffer_, ZERO_COPY_IPC_BUFFER_LENGTH));
     733            0 :             CHK_RET(hrtMemSet(zeroCopyLocalBuffer_.ptr(), zeroCopyLocalBuffer_.size(), zeroCopyLocalBuffer_.size()));
     734            0 :             zeroCopyIpcPtrs_[userRank_ % deviceNumPerAggregation_] = zeroCopyLocalBuffer_.ptr();
     735              : 
     736            0 :             HCCL_RUN_INFO("[HCCL_TRACE][PrepareZeroCopy]Create ZeroCopy buffer success. buffer ptr[%p] size[%llu]",
     737              :                           zeroCopyLocalBuffer_.ptr(), zeroCopyLocalBuffer_.size());
     738              :         }
     739            0 :         opParam.isZeroCopy = true;
     740            0 :         HCCL_INFO("[HcclCommunicator][PrepareZeroCopy] success to use zero copy feature");
     741            0 :         return HCCL_SUCCESS;
     742              :     }
     743              : 
     744           23 :     HcclResult HcclCommunicator::UpdateZeroCopy(const OpParam &opParam, const AlgResourceResponse &algResource)
     745              :     {
     746           23 :         if (!opParam.isZeroCopy) {
     747           23 :             return HCCL_SUCCESS;
     748              :         }
     749              : 
     750              :         // 遍历所有transport,找出里面的p2p链路对应的对端地址
     751            0 :         for (auto &singleSubCommTransport : algResource.opTransportResponse[COMM_LEVEL0]) {
     752            0 :             for (u64 i = 0; i < singleSubCommTransport.links.size(); ++i) {
     753            0 :                 LINK link = singleSubCommTransport.links[i];
     754            0 :                 if (link == nullptr || !singleSubCommTransport.transportRequests[i].isValid) {
     755              :                     // 无效或者不支持的链路
     756            0 :                     continue;
     757              :                 }
     758              : 
     759              :                 // 在使能零拷贝场景,我们使用控制面内存做OpenIpc交换,因此这里取出input即可
     760            0 :                 u32 remoteRank = link->GetRemoteRank();
     761              : 
     762            0 :                 void *remotePtr = nullptr;
     763            0 :                 CHK_RET(link->GetRemoteMem(UserMemType::INPUT_MEM, &remotePtr));
     764            0 :                 CHK_PRT_RET(remotePtr == nullptr,
     765              :                             HCCL_ERROR("[BuildZeroCopyParam] invalid remotePtr[%p]", remotePtr), HCCL_E_PARA);
     766            0 :                 CHK_PRT_RET(zeroCopyIpcPtrs_[remoteRank % deviceNumPerAggregation_] != nullptr && zeroCopyIpcPtrs_[remoteRank % deviceNumPerAggregation_] != remotePtr,
     767              :                             HCCL_ERROR("[BuildZeroCopyParam] zeroCopyIpcPtrs_[%u] is [%p] not equal to %p", remoteRank, zeroCopyIpcPtrs_[remoteRank % deviceNumPerAggregation_],
     768              :                                        remotePtr),
     769              :                             HCCL_E_PARA);
     770              : 
     771            0 :                 zeroCopyIpcPtrs_[remoteRank % deviceNumPerAggregation_] = remotePtr;
     772            0 :             }
     773              :         }
     774            0 :         return HCCL_SUCCESS;
     775              :     }
     776              : 
     777            3 :     HcclResult HcclCommunicator::BuildZeroCopyParam()
     778              :     {
     779              :         // 不支持ZeroCopy
     780            3 :         if (zeroCopyLocalBuffer_.ptr() == nullptr) {
     781            1 :             return HCCL_SUCCESS;
     782              :         }
     783              : 
     784           66 :         for (u32 i = 0; i < AICPU_ZERO_COPY_MAX_DEVICE_NUM_A3; ++i) {
     785           64 :             opResPara_.zeroCopyIpcPtrs[i] = reinterpret_cast<u64>(zeroCopyIpcPtrs_[i]);
     786              :         }
     787              : 
     788            2 :         for (u32 i = 0; i < rankInfoList_.size(); ++i) {
     789            0 :             opResPara_.zeroCopyDevicePhyId[i % deviceNumPerAggregation_] = rankInfoList_[i].devicePhyId;
     790              :         }
     791              : 
     792            2 :         CHK_RET(ZeroCopyMemoryAgent::GetRingBufferAddr(opResPara_.zeroCopyRingBuffer,
     793              :                                                        opResPara_.zeroCopyHeadPtr, opResPara_.zeroCopyTailPtr));
     794            1 :         return HCCL_SUCCESS;
     795              :     }
     796              : 
     797          531 :     HcclResult HcclCommunicator::InitCommParams(HcclCommParams &params)
     798              :     {
     799          531 :         commHandle_ = params.commHandle;
     800          531 :         userRank_ = params.rank;
     801          531 :         realUserRank_ = params.userRank;
     802          531 :         userRankSize_ = params.totalRanks;
     803          531 :         deviceLogicId_ = params.logicDevId;
     804          531 :         profilingOption_ = params.profilingOption;
     805          531 :         profilingInitiated_ = params.profilingInitiated;
     806          531 :         deviceType_ = params.deviceType;
     807          531 :         commWorkMode_ = params.commWorkMode;
     808          531 :         hcomGroupNicInit_ = params.hcomGroupNicInit;
     809          531 :         identifier_ = params.identifier;
     810          531 :         collectiveId_ = params.id.internal;
     811          531 :         ranktableCrc_ = params.ranktableCrc;
     812          531 :         commConnections_ = params.commConnections;
     813          531 :         commPortConfig_ = params.commPortConfig;
     814          531 :         cclBuffName_ = params.cclBuffName;
     815          531 :         isShareComm_ = !cclBuffName_.empty();
     816              : 
     817          531 :         HCCL_DEBUG(
     818              :             " userRank_: %u realUserRank_: %u userRankSize_: %u deviceLogicId_: %u deviceType_: %u commWorkMode_: %u.",
     819              :             userRank_,
     820              :             realUserRank_,
     821              :             userRankSize_,
     822              :             deviceLogicId_,
     823              :             deviceType_,
     824              :             commWorkMode_);
     825              : 
     826          531 :         return HCCL_SUCCESS;
     827              :     }
     828              : 
     829            0 :     bool HcclCommunicator::Is310PDuoCard()
     830              :     {
     831            0 :         return (Is310P3Common(isHaveCpuRank_, deviceType_) &&
     832            0 :                 (pairLinkInfo_[static_cast<u32>(LinkTypeInServer::HCCS_TYPE)].size() == userRankSize_));
     833              :     }
     834              : 
     835              :     // 910B A+X 在RDMA未启用情况下,两模块间的device数目需要一致且两模块中使用的卡都在同一平面上
     836          492 :     HcclResult HcclCommunicator::CheckSingleServerComm(const std::vector<RankInfo_t> &rankList) const
     837              :     {
     838          492 :         if (serverNum_ == 1 && moduleNum_ == HCCL_MODULE_NUM_TWO && GetExternalInputIntraRoceSwitch() == 0 && !isStandardCard_) {
     839            0 :             std::vector<u32> devIdList0;
     840            0 :             std::vector<u32> devIdList1;
     841            0 :             for (RankInfo_t rankInfo : rankList){
     842            0 :                 if (rankInfo.deviceInfo.devicePhyId == HOST_DEVICE_ID) {
     843            0 :                     HCCL_ERROR("[Check][SingleServerComm]not support cpu rank");
     844            0 :                     return HCCL_E_NOT_SUPPORT;
     845              :                 }
     846            0 :                 if (rankInfo.deviceInfo.devicePhyId < DEVICE_PER_MODULE) {
     847            0 :                     devIdList0.push_back(rankInfo.deviceInfo.devicePhyId);
     848              :                 } else {
     849            0 :                     devIdList1.push_back(rankInfo.deviceInfo.devicePhyId);
     850              :                 }
     851            0 :             }
     852            0 :             std::sort(devIdList0.begin(), devIdList0.end());
     853            0 :             std::sort(devIdList1.begin(), devIdList1.end());
     854              : 
     855            0 :             auto buildDeviceListStr = [](const std::vector<u32>& list) -> std::string {
     856            0 :                 std:: string result;
     857            0 :                 for(const auto& id : list) {
     858            0 :                     if (!result.empty()) {
     859            0 :                         result += " ";
     860              :                     }
     861            0 :                     result += std::to_string(id);
     862              :                 }
     863            0 :                 return result;
     864            0 :             };
     865              : 
     866            0 :             std::string devList0Str = buildDeviceListStr(devIdList0);
     867            0 :             std::string devList1Str = buildDeviceListStr(devIdList1);
     868              : 
     869            0 :             if (devIdList0.size() != devIdList1.size()) {
     870            0 :                 std::string errormessage = "Device ID " + devList0Str + " in module 0 and device ID " + devList1Str + " in module 1 are not on the same plane.";
     871            0 :                 RPT_INPUT_ERR(true, "EI0010", std::vector<std::string>({"reason"}),
     872              :                               std::vector<std::string>({ errormessage }));
     873            0 :                 HCCL_ERROR("[%s][%s]%s",
     874              :                     LOG_KEYWORDS_INIT_CHANNEL.c_str(), LOG_KEYWORDS_TIMEOUT.c_str(), errormessage.c_str());
     875            0 :                 return HCCL_E_NOT_SUPPORT;
     876            0 :             }
     877            0 :             for (size_t i = 0; i < devIdList0.size(); i++) {
     878            0 :                 if (devIdList0[i] % DEVICE_PER_MODULE != devIdList1[i] % DEVICE_PER_MODULE) {
     879            0 :                     std::string errormessage = "Device ID " + std::to_string(devIdList0[i]) + " in module 0 and device ID " + std::to_string(devIdList1[i]) + " in module 1 are not on the same plane.";
     880            0 :                     RPT_INPUT_ERR(true, "EI0010", std::vector<std::string>({"reason"}),
     881              :                                   std::vector<std::string>({ errormessage }));
     882            0 :                     HCCL_ERROR("[%s][%s]%s",
     883              :                         LOG_KEYWORDS_INIT_CHANNEL.c_str(), LOG_KEYWORDS_TIMEOUT.c_str(), errormessage.c_str());
     884            0 :                     return HCCL_E_NOT_SUPPORT;
     885            0 :                 }
     886              :             }
     887            0 :         }
     888          492 :         return HCCL_SUCCESS;
     889            0 :     }
     890              : 
     891          393 :     HcclResult HcclCommunicator::CheckDataType(const HcclDataType dataType, bool needReduce)
     892              :     {
     893         3493 :         const vector<string> infoTitle({"ccl_op", "value", "parameter", "expect"});
     894          414 :         if (needReduce) {
     895          193 :             if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
     896            0 :                 if ((dataType == HCCL_DATA_TYPE_INT64) || (dataType == HCCL_DATA_TYPE_BFP16)) {
     897            0 :                     RPT_INPUT_ERR(true,
     898              :                         "EI0003",
     899              :                         infoTitle,
     900              :                         vector<string>(
     901              :                             {"CheckDataType", GetDataTypeEnumStr(dataType), "dataType", "HCCL_DATA_TYPE_INT8, HCCL_DATA_TYPE_INT16, HCCL_DATA_TYPE_INT32, "\
     902              :                             "HCCL_DATA_TYPE_FP16, HCCL_DATA_TYPE_FP32"}));
     903            0 :                     HCCL_ERROR("[%s][%s]errNo[0x%016llx] data type[%s] not supported, support range=[%s]",
     904              :                         LOG_KEYWORDS_TASK_EXEC.c_str(),
     905              :                         LOG_KEYWORDS_INVALID_ARGUMENT.c_str(),
     906              :                         HCCL_ERROR_CODE(HCCL_E_NOT_SUPPORT),
     907              :                         GetDataTypeEnumStr(dataType).c_str(),
     908              :                         GetSupportDataType(needReduce).c_str());
     909            0 :                     return HCCL_E_NOT_SUPPORT;
     910              :                 }
     911              :             }
     912          190 :             if ((dataType == HCCL_DATA_TYPE_UINT64) ||
     913          191 :                 (dataType == HCCL_DATA_TYPE_UINT8) || (dataType == HCCL_DATA_TYPE_UINT16) ||
     914          191 :                 (dataType == HCCL_DATA_TYPE_UINT32) || (dataType == HCCL_DATA_TYPE_FP64) ||
     915              :                 (dataType == HCCL_DATA_TYPE_RESERVED)) {
     916           13 :                 RPT_INPUT_ERR(true,
     917              :                     "EI0003",
     918              :                     infoTitle,
     919              :                     vector<string>(
     920              :                         {"CheckDataType", GetDataTypeEnumStr(dataType), "dataType", "HCCL_DATA_TYPE_INT8, HCCL_DATA_TYPE_INT16, HCCL_DATA_TYPE_INT32, "\
     921              :                         "HCCL_DATA_TYPE_FP16, HCCL_DATA_TYPE_FP32"}));
     922            5 :                 HCCL_ERROR("[%s][%s]errNo[0x%016llx] data type[%s] not supported, support range=[%s]",
     923              :                     LOG_KEYWORDS_TASK_EXEC.c_str(),
     924              :                     LOG_KEYWORDS_INVALID_ARGUMENT.c_str(),
     925              :                     HCCL_ERROR_CODE(HCCL_E_NOT_SUPPORT),
     926              :                     GetDataTypeEnumStr(dataType).c_str(),
     927              :                     GetSupportDataType(needReduce).c_str());
     928            1 :                 return HCCL_E_NOT_SUPPORT;
     929              :             }
     930              :         } else {
     931          442 :             if ((dataType >= HCCL_DATA_TYPE_RESERVED) || (dataType < HCCL_DATA_TYPE_INT8) ||
     932          221 :                 (Is310P3Common(isHaveCpuRank_, deviceType_) && dataType == HCCL_DATA_TYPE_BFP16)) {
     933            0 :                 RPT_INPUT_ERR(true,
     934              :                     "EI0003",
     935              :                     infoTitle,
     936              :                     vector<string>(
     937              :                         {"CheckDataType",  GetDataTypeEnumStr(dataType), "dataType", "HCCL_DATA_TYPE_INT8, HCCL_DATA_TYPE_INT16, HCCL_DATA_TYPE_INT32, "\
     938              :                             "HCCL_DATA_TYPE_FP16, HCCL_DATA_TYPE_FP32, HCCL_DATA_TYPE_UINT8, HCCL_DATA_TYPE_UINT16, HCCL_DATA_TYPE_UINT32"}));
     939            0 :                 HCCL_ERROR("[%s][%s]errNo[0x%016llx] data type[%s] not supported, support range=[%s]",
     940              :                     LOG_KEYWORDS_TASK_EXEC.c_str(),
     941              :                     LOG_KEYWORDS_INVALID_ARGUMENT.c_str(),
     942              :                     HCCL_ERROR_CODE(HCCL_E_NOT_SUPPORT),
     943              :                     GetDataTypeEnumStr(dataType).c_str(),
     944              :                     GetSupportDataType(needReduce).c_str());
     945            0 :                 return HCCL_E_NOT_SUPPORT;
     946              :             }
     947              :         }
     948          406 :         return HCCL_SUCCESS;
     949          409 :     }
     950              : 
     951            0 :     HcclResult HcclCommunicator::InitZeroCopyMemoryAgent()
     952              :     {
     953            0 :         CHK_PRT_RET(zeroCopyMemoryAgent_ != nullptr,
     954              :                     HCCL_ERROR("[HcclCommunicator][InitZeroCopyMemoryAgent] ipc memory agent has init"), HCCL_E_INTERNAL);
     955              : 
     956              :         // 获取节点内的ranktable
     957            0 :         std::vector<std::vector<std::vector<RankInfo>>> commPlaneVector;
     958            0 :         CHK_SMART_PTR_NULL(implAlg_);
     959            0 :         implAlg_->GetCommPlaneVector(commPlaneVector);
     960            0 :         rankInfoListIntraServer_ = commPlaneVector[COMM_LEVEL0][COMM_INDEX_0];
     961            0 :         zeroCopyMemoryAgent_.reset(static_cast<ZeroCopyMemoryAgent *>(new (std::nothrow) ZeroCopyMemoryAgent(socketManager_, devicePhyId_,
     962            0 :                                                                                                              deviceLogicId_, localVnicIp_, rankInfoListIntraServer_, userRank_, useSuperPodMode_, identifier_)));
     963            0 :         CHK_PTR_NULL(zeroCopyMemoryAgent_);
     964            0 :         CHK_RET(zeroCopyMemoryAgent_->Init());
     965            0 :         return HCCL_SUCCESS;
     966            0 :     }
     967              : 
     968         1038 :     HcclResult HcclCommunicator::DeinitZeroCopyMemoryAgent(bool inDestructor)
     969              :     {
     970         1038 :         if (zeroCopyMemoryAgent_ != nullptr) {
     971            0 :             if (!inDestructor && zeroCopyMemoryAgent_->IsResumed()) {
     972              :                 // 析构函数释放场景不做barrier close
     973            0 :                 CHK_RET(zeroCopyMemoryAgent_->BarrierClose());
     974              :             }
     975            0 :             CHK_RET(zeroCopyMemoryAgent_->DeInit());
     976            0 :             zeroCopyMemoryAgent_ = nullptr;
     977              :         }
     978         1038 :         return HCCL_SUCCESS;
     979              :     }
     980              : 
     981           77 :     u8 HcclCommunicator::GetConfigAclGraphZeroCopyEnable()
     982              :     {
     983           77 :         return commConfig_.GetConfigAclGraphZeroCopyEnable();
     984              :     }
     985              : 
     986           60 :     HcclResult HcclCommunicator::ClearResMap(const std::string &tag, bool &findTag, bool aclGraphDestroyCbk)
     987              :     {
     988           60 :         auto resIter = resMap_.find(tag);
     989           61 :         if (resIter != resMap_.end()) {
     990           12 :             findTag = true;
     991           12 :             DestroyAlgResource(resIter->second, aclGraphDestroyCbk);
     992           12 :             CHK_RET(StreamActiveManager::GetInstance(deviceLogicId_).StreamsUnactive(resIter->second.slaveStreams));
     993           12 :             resMap_.erase(resIter);
     994           12 :             HCCL_INFO("[%s] clear resMap[%s]", __func__, tag.c_str());
     995              :         }
     996           61 :         return HCCL_SUCCESS;
     997              :     }
     998              : 
     999            4 :     HcclResult HcclCommunicator::ClearAclgraphHostLinks(const std::unordered_set<std::string> &tags)
    1000              :     {
    1001            4 :         std::lock_guard<std::mutex> lock(commResMutex_);
    1002            8 :         for (const auto &tag : tags) {
    1003            4 :             auto it = tagsRequiringHostCleanup_.find(tag);
    1004            4 :             if (it == tagsRequiringHostCleanup_.end()) {
    1005            1 :                 continue;
    1006              :             }
    1007            4 :             for (auto &rankIt : rankTagRemoteRes_) {
    1008            1 :                 auto tagIt = rankIt.second.find(tag);
    1009            1 :                 if (tagIt == rankIt.second.end()) {
    1010            0 :                     continue;
    1011              :                 }
    1012            1 :                 HccltagRemoteResV2 *hostPtr = tagIt->second.tagRemoteResPtr;
    1013            1 :                 if (hostPtr != nullptr) {
    1014              :                     // 顺序敏感: ListCommonRemove 必须先于 erase shared_ptr,
    1015              :                     // 否则 hostPtr 成野指针, ListCommonRemove 访问 segfault。
    1016            1 :                     ListCommonRemove(&hostPtr->nextTagRes);
    1017            1 :                     for (auto vIt = hostMemVec_.begin(); vIt != hostMemVec_.end(); ++vIt) {
    1018            1 :                         if (*vIt && (*vIt)->ptr() == hostPtr) {
    1019            1 :                             size_t idx = static_cast<size_t>(vIt - hostMemVec_.begin());
    1020            1 :                             if (idx < deviceMemVec_.size()) {
    1021            1 :                                 deviceMemVec_.erase(deviceMemVec_.begin() + idx);
    1022              :                             }
    1023            1 :                             hostMemVec_.erase(vIt);
    1024            1 :                             break;
    1025              :                         }
    1026              :                     }
    1027              :                 }
    1028            1 :                 rankIt.second.erase(tagIt);
    1029              :             }
    1030            3 :             ibverbsLocalNotify_.erase(tag);
    1031            3 :             ibverbsRemoteNotify_.erase(tag);
    1032            3 :             tagsRequiringHostCleanup_.erase(it);
    1033              :         }
    1034            4 :         return HCCL_SUCCESS;
    1035            4 :     }
    1036              : 
    1037           19 :     HcclResult HcclCommunicator::ClearOpResource(const std::string &tag, bool aclGraphDestroyCbk)
    1038              :     {
    1039           19 :         std::lock_guard<std::mutex> lock(commResMutex_);
    1040           19 :         bool findTag = false;
    1041           19 :         CHK_RET(ClearResMap(tag, findTag, aclGraphDestroyCbk));
    1042           19 :         CHK_RET(ClearResMap(tag + "_host", findTag, aclGraphDestroyCbk));
    1043           19 :         CHK_RET(ClearResMap(tag + "_device", findTag, aclGraphDestroyCbk));
    1044           19 :         if (!findTag) {
    1045           13 :             HCCL_WARNING("[%s] not find tag[%s] in resMap", __func__, tag.c_str());
    1046              :         }
    1047              : 
    1048           19 :         tagCommInfo_.erase(tag);
    1049              :         // stream解绑定
    1050           16 :         auto iterStream = tagStreamInfo_.find(tag);
    1051           17 :         if (iterStream != tagStreamInfo_.end()) {
    1052            0 :             CHK_RET(StreamActiveManager::GetInstance(deviceLogicId_).StreamsUnactive(iterStream->second.ringStreams));
    1053              :         }
    1054           17 :         tagStreamInfo_.erase(tag);
    1055           16 :         if (opRetryStreamPtr_ != nullptr) {
    1056           13 :             opRetryStreamPtr_->erase(tag);
    1057              :         }
    1058           16 :         if (implAlg_ != nullptr) {
    1059           13 :             CHK_RET(implAlg_->ClearOpResource(tag));
    1060              :         }
    1061           16 :         DestroyWorkspaceResource(tag);
    1062           19 :         return HCCL_SUCCESS;
    1063           19 :     }
    1064              : 
    1065           95 :     HcclResult HcclCommunicator::CreateOpBasedResources(const HcclCMDType &opType, const std::string &tag,
    1066              :                                                         const HcomCollOpInfo &opInfo)
    1067              :     {
    1068           95 :         return workSpaceRes_->CreateOpBasedResources(opType, tag, opInfo);
    1069              :     }
    1070              : 
    1071            0 :     HcclResult HcclCommunicator::CreateRemoteOpBasedResources(u64 memSize, const std::string &tag)
    1072              :     {
    1073            0 :         return workSpaceRes_->CreateRemoteOpBasedResources(memSize, tag);
    1074              :     }
    1075              : 
    1076            0 :     HcclResult HcclCommunicator::DestroyRemoteOpBasedMem(const std::string &tag)
    1077              :     {
    1078            0 :         return workSpaceRes_->DestroyRemoteOpBasedMem(tag);
    1079              :     }
    1080              : 
    1081          157 :     bool HcclCommunicator::IsAtomicInit()
    1082              :     {
    1083          314 :         if (!initializedFlag_.test_and_set()) {
    1084            0 :             initializedFlag_.clear();
    1085            0 :             return false;
    1086              :         }
    1087          157 :         return true;
    1088              :     }
    1089              : 
    1090            5 :     bool HcclCommunicator::IsNeedNicInit()
    1091              :     {
    1092           10 :         return ((nicInitialized_ == 0) && (!hcomGroupNicInit_) && (userRankSize_ > 1) && !isSingleMeshAggregation_ &&
    1093           10 :                 (superPodNum_ > 1 || !isUsedInterHccsMode_));
    1094              :     }
    1095              : 
    1096            0 :     HcclResult HcclCommunicator::GetBandWidthPerNPU(u32 level, float &bandWidth)
    1097              :     {
    1098            0 :         return hccl::GetBandWidthPerNPU(level, userRankSize_, deviceNumPerAggregation_, bandWidth);
    1099              :     }
    1100              : 
    1101            0 :     HcclResult HcclCommunicator::GetDeviceNumPerAggregation(u32 &deviceNumPerAggregation)
    1102              :     {
    1103            0 :         deviceNumPerAggregation = deviceNumPerAggregation_;
    1104            0 :         return HCCL_SUCCESS;
    1105              :     }
    1106              : 
    1107          234 :     HcclResult HcclCommunicator::InitHccpChannel()
    1108              :     {
    1109          234 :         return hcclH2dTlv::GetInstance().InitHccpChannel(devicePhyId_);
    1110              :     }
    1111              : 
    1112          403 :     std::vector<RankInfo> HcclCommunicator::GetRankLists()
    1113              :     {
    1114          403 :         return rankInfoList_;
    1115              :     }
    1116              : 
    1117          175 :     HcclResult HcclCommunicator::CheckReduceDataType(const HcclDataType dataType, const HcclReduceOp op)
    1118              :     {
    1119          175 :         if ((deviceType_ == DevType::DEV_TYPE_910B) || (deviceType_ == DevType::DEV_TYPE_910_93)) {
    1120           63 :             if ((op == HCCL_REDUCE_PROD) &&
    1121           12 :                 ((dataType == HCCL_DATA_TYPE_INT16) || (dataType == HCCL_DATA_TYPE_BFP16))) {
    1122            0 :                 RPT_INPUT_ERR(true,
    1123              :                     "EI0003",
    1124              :                     std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),
    1125              :                     std::vector<std::string>({"CheckReduceDataType",
    1126              :                         GetDataTypeEnumStr(dataType),
    1127              :                         "dataType",
    1128              :                         "HCCL_DATA_TYPE_INT8, HCCL_DATA_TYPE_INT32, HCCL_DATA_TYPE_FP16, HCCL_DATA_TYPE_FP32"}));
    1129            0 :                 HCCL_ERROR("[%s][%s]errNo[0x%016llx] device type[%d] does not support the data type[%s] and data "
    1130              :                            "type[%s] for Op[%s]",
    1131              :                     LOG_KEYWORDS_TASK_EXEC.c_str(),
    1132              :                     LOG_KEYWORDS_INVALID_ARGUMENT.c_str(),
    1133              :                     HCCL_ERROR_CODE(HCCL_E_NOT_SUPPORT),
    1134              :                     deviceType_,
    1135              :                     GetDataTypeEnumStr(HCCL_DATA_TYPE_BFP16).c_str(),
    1136              :                     GetDataTypeEnumStr(HCCL_DATA_TYPE_INT16).c_str(),
    1137              :                     GetReduceOpEnumStr(op).c_str());
    1138            0 :                 return HCCL_E_NOT_SUPPORT;
    1139              :             }
    1140          112 :         } else if (deviceType_ == DevType::DEV_TYPE_910) {
    1141          112 :             if (dataType == HCCL_DATA_TYPE_INT16) {
    1142            0 :                 RPT_INPUT_ERR(true,
    1143              :                     "EI0003",
    1144              :                     std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),
    1145              :                     std::vector<std::string>({"CheckReduceDataType",
    1146              :                         GetDataTypeEnumStr(dataType),
    1147              :                         "dataType",
    1148              :                         "HCCL_DATA_TYPE_INT8, HCCL_DATA_TYPE_INT32, HCCL_DATA_TYPE_FP16, HCCL_DATA_TYPE_FP32"}));
    1149            0 :                 HCCL_ERROR("[%s][%s]errNo[0x%016llx] device type[%d] does not support the data type[%s]",
    1150              :                     LOG_KEYWORDS_TASK_EXEC.c_str(),
    1151              :                     LOG_KEYWORDS_INVALID_ARGUMENT.c_str(),
    1152              :                     HCCL_ERROR_CODE(HCCL_E_NOT_SUPPORT),
    1153              :                     deviceType_,
    1154              :                     GetDataTypeEnumStr(dataType).c_str());
    1155            0 :                 return HCCL_E_NOT_SUPPORT;
    1156              :             }
    1157            0 :         } else if (deviceType_ == DevType::DEV_TYPE_310P3) {
    1158            0 :             if (dataType == HcclDataType::HCCL_DATA_TYPE_INT16 && op != HcclReduceOp::HCCL_REDUCE_SUM) {
    1159            0 :                 RPT_INPUT_ERR(true,
    1160              :                     "EI0003",
    1161              :                     std::vector<std::string>({"ccl_op", "value", "parameter", "expect"}),
    1162              :                     std::vector<std::string>({"CheckReduceDataType",
    1163              :                         GetReduceOpEnumStr(op),
    1164              :                         "op",
    1165              :                         "sum"}));
    1166            0 :                 HCCL_ERROR("[%s][%s]errNo[0x%016llx] device type[%d] does not support the data type[%s] for Op[%s]",
    1167              :                     LOG_KEYWORDS_TASK_EXEC.c_str(),
    1168              :                     LOG_KEYWORDS_INVALID_ARGUMENT.c_str(),
    1169              :                     HCCL_ERROR_CODE(HCCL_E_NOT_SUPPORT),
    1170              :                     deviceType_,
    1171              :                     GetDataTypeEnumStr(HcclDataType::HCCL_DATA_TYPE_INT16).c_str(),
    1172              :                     GetReduceOpEnumStr(op).c_str());
    1173            0 :                 return HCCL_E_NOT_SUPPORT;
    1174              :             }
    1175              :         }
    1176          176 :         return HCCL_SUCCESS;
    1177            0 :     }
    1178              : 
    1179          235 :     HcclResult HcclCommunicator::GetAlgType(AlgType &algType, HcclCMDType opType)
    1180              :     {
    1181          235 :         CHK_SMART_PTR_NULL(implAlg_);
    1182          235 :         return implAlg_->GetAlgType(algType, opType);
    1183              :     }
    1184              : 
    1185            1 :     HcclResult HcclCommunicator::GetCommParams(HcclCommParams &params)
    1186              :     {
    1187            1 :         params.commHandle = commHandle_;
    1188            1 :         params.rank = userRank_;
    1189            1 :         params.userRank = realUserRank_;
    1190            1 :         params.totalRanks = userRankSize_;
    1191            1 :         params.logicDevId = deviceLogicId_;
    1192            1 :         params.deviceType = deviceType_;
    1193            1 :         params.hcomGroupNicInit = hcomGroupNicInit_;
    1194            1 :         params.identifier = identifier_;
    1195            1 :         params.ranktableCrc = ranktableCrc_;
    1196            1 :         params.commConnections = commConnections_;
    1197            1 :         params.commPortConfig.devPortSwitchOn = commPortConfig_.devPortSwitchOn;
    1198            1 :         return HCCL_SUCCESS;
    1199              :     }
    1200              : 
    1201            1 :     HcclResult HcclCommunicator::GetCommRankTable(RankTable_t &rankTable)
    1202              :     {
    1203            2 :         for (auto &server : servRankInfo_) {
    1204            2 :             for (auto &rank : server.second) {
    1205            1 :                 rankTable.rankList.emplace_back(rank);
    1206              :             }
    1207              :         }
    1208            1 :         rankTable.serverNum = serverNum_;
    1209            1 :         rankTable.superPodNum = superPodNum_;
    1210            1 :         rankTable.nicDeploy = nicDeployment_;
    1211            1 :         rankTable.version = attrCollector_.GetRankTableVersion();
    1212            1 :         return HCCL_SUCCESS;
    1213              :     }
    1214              : 
    1215          488 :     HcclResult HcclCommunicator::InitPara()
    1216              :     {
    1217              :         // 检查当前user_rank 对应的devid和rt查到的一致
    1218          488 :         CHK_RET(attrCollector_.CheckLocalRankInfo());
    1219          488 :         CHK_RET(attrCollector_.CalAndSetMeshAggRankSize());
    1220          488 :         meshAggregationRankSize_ = attrCollector_.GetMeshAggregationRankSize();
    1221              : 
    1222              :         // 初始化计数任务
    1223          488 :         CHK_RET(OpExeCounter::GetInstance(deviceLogicId_).InitCounter());
    1224              : 
    1225          488 :         notifyPool_.reset(new (std::nothrow) NotifyPool());
    1226          488 :         CHK_SMART_PTR_NULL(notifyPool_);
    1227          488 :         CHK_RET(notifyPool_->Init(devicePhyId_));
    1228              : 
    1229          488 :         callbackTask_.reset(new (std::nothrow) HcclCallbackTask(devicePhyId_, deviceLogicId_,
    1230          488 :                                                                 dispatcher_, nicDeployment_));
    1231          488 :         CHK_SMART_PTR_NULL(callbackTask_);
    1232              : 
    1233          488 :         workSpaceRes_.reset(new (std::nothrow)
    1234          488 :                                 WorkspaceResource(devicePhyId_, deviceLogicId_, &cclBufferManager_));
    1235          488 :         CHK_SMART_PTR_NULL(workSpaceRes_);
    1236              : 
    1237          488 :         CHK_RET(InitAlgResource());
    1238          488 :         return HCCL_SUCCESS;
    1239              :     }
    1240              : 
    1241            0 :     bool HcclCommunicator::IsStandardCard()
    1242              :     {
    1243            0 :         if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
    1244            0 :             HCCL_INFO("The current device just support this StandardCard case.");
    1245            0 :             return true;
    1246              :         }
    1247              : 
    1248            0 :         return ((pairLinkInfo_[static_cast<u32>(LinkTypeInServer::HCCS_TYPE)].size() == 0) &&
    1249            0 :                 (pairLinkInfo_[static_cast<u32>(LinkTypeInServer::HCCS_SW_TYPE)].size() == 0) &&
    1250            0 :                 (pairLinkInfo_[static_cast<u32>(LinkTypeInServer::SIO_TYPE)].size() == 0));
    1251              :     }
    1252              : 
    1253          520 :     HcclResult HcclCommunicator::InitOpRetry()
    1254              :     {
    1255          520 :         EXCEPTION_CATCH((opRetryStreamPtr_ = std::make_shared<HcclOpStreamRes>()), return HCCL_E_PTR);
    1256          520 :         if (retryEnable_) {
    1257            0 :             opRetryManager_.reset(new (std::nothrow) OpRetryManager());
    1258            0 :             CHK_SMART_PTR_NULL(opRetryManager_);
    1259            0 :             HcclIpAddress hostIp = !rankInfoList_.empty() ? rankInfoList_[0].hostIp : HcclIpAddress();
    1260            0 :             u32 hostPort = !rankInfoList_.empty() ? rankInfoList_[0].hostPort : HCCL_INVALID_PORT;
    1261            0 :             s32 hostDevId = !rankInfoList_.empty() ? rankInfoList_[0].devicePhyId : 0;
    1262            0 :             HcclIpAddress localIp = rankInfoList_.size() > userRank_ ? rankInfoList_[userRank_].hostIp : HcclIpAddress();
    1263            0 :             auto notifyResetCallback = [this](bool isSendRecv, s64 destRank) {
    1264            0 :                 return isSendRecv ? this->ResetNotifyForDestRank(destRank) : this->ResetNotify();
    1265            0 :             };
    1266              : 
    1267            0 :             auto setTransportStatusCallback = [this](const HcclOpIdentifier &opId, bool statusStop,
    1268              :                 const std::map<u32, bool> &remoteRankPortMap, const std::map<u32, bool> &isChangeLinkMap, bool isChangeLinkFlag) {
    1269            0 :                     return this->SetTransportStatus(opId, statusStop, remoteRankPortMap, isChangeLinkMap, isChangeLinkFlag);
    1270            0 :             };
    1271              :             auto getSwitchRanksCallback =
    1272            0 :                 [this](u32 *distSwitchRankList, bool *distSwitchUseBackup, u32 &distSwitchRankNum,
    1273              :                        u8 *distRemoteRankNicStatus, u32 &distRankSize, bool &needCheckDefaultNic, bool &needCheckBackupNic) {
    1274            0 :                 return this->GetSwitchRanks(distSwitchRankList, distSwitchUseBackup, distSwitchRankNum,
    1275            0 :                                             distRemoteRankNicStatus, distRankSize, needCheckDefaultNic, needCheckBackupNic);
    1276            0 :             };
    1277            0 :             auto setTransportResumeStatusCallback = [this](const std::map<u32, bool> &remoteRankPortMap,
    1278              :                 const std::map<u32, bool> &isChangeLinkMap, bool isChangeLinkFlag, bool statusStop){
    1279            0 :                     return this->SetTransportResumeStatus(remoteRankPortMap, isChangeLinkMap, isChangeLinkFlag, statusStop); };
    1280            0 :             HcclNetDevCtx netDevCtx = netDevCtxMap_[devIpAddr_[0]];
    1281            0 :             HcclNetDevCtx backUpNetDevCtx = {};
    1282            0 :             if (IsEnableBackupLink()) {
    1283            0 :                 g_enableBackupLinkCommCount++;
    1284              :             }
    1285            0 :             if (IsEnableBackupLink() && netDevCtxMap_.find(devBackupIpAddr_[0]) != netDevCtxMap_.end()) {
    1286            0 :                 backUpNetDevCtx = netDevCtxMap_[devBackupIpAddr_[0]];
    1287              :             }
    1288            0 :             OpRetryServerInfo serverInfo = {hostIp, hostPort, hostDevId};
    1289            0 :             OpRetryAgentInfo agentInfo = {userRank_, deviceLogicId_, localIp, devIpAddr_[0], netDevCtx, backUpNetDevCtx};
    1290              : 
    1291            0 :             OpRetryAgentParam agentParam;
    1292            0 :             agentParam.group = identifier_;
    1293            0 :             agentParam.agentConnection = commConnections_.agentConnection;
    1294            0 :             agentParam.h2dPtr = kfcControlTransferH2D_;
    1295            0 :             agentParam.d2hPtr = kfcStatusTransferD2H_;
    1296            0 :             agentParam.opStreamPtr = opRetryStreamPtr_;
    1297            0 :             agentParam.notifyResetCallback = notifyResetCallback;
    1298            0 :             agentParam.setTransportStatusCallback = setTransportStatusCallback;
    1299            0 :             agentParam.setTransportResumeStatusCallback = setTransportResumeStatusCallback;
    1300            0 :             agentParam.getSwitchRanksCallback = getSwitchRanksCallback;
    1301            0 :             agentParam.isEnableBackupLink = IsEnableBackupLink();
    1302            0 :             agentParam.isEnableSdmaRetry = commConfig_.GetConfigInterServerRetryEnable();
    1303            0 :             agentParam.agentInfo = agentInfo;
    1304              : 
    1305            0 :             CHK_RET(opRetryManager_->RegisterOpRetryMachine(agentParam, userRankSize_, commConnections_.isRoot,
    1306              :                                                             commConnections_.serverConnections, serverInfo));
    1307            0 :             HCCL_RUN_INFO("[InitOpRetry] group[%s], isEnableBackupLink[%d], g_enableBackupLinkCommCount[%u]",
    1308              :                           identifier_.c_str(), IsEnableBackupLink(), g_enableBackupLinkCommCount.load());
    1309            0 :         }
    1310          520 :         return HCCL_SUCCESS;
    1311              :     }
    1312              : 
    1313            1 :     bool HcclCommunicator::CompareWithServerId(const ServerInfo_t &left, const ServerInfo_t &right)
    1314              :     {
    1315            1 :         return (strcmp(left.serverId.c_str(), right.serverId.c_str()) < 0);
    1316              :     }
    1317              : 
    1318            0 :     bool HcclCommunicator::CompareWithNicName(const NetworkInfo_t &left, const NetworkInfo_t &right)
    1319              :     {
    1320            0 :         return (strcmp(left.ethName.c_str(), right.ethName.c_str()) < 0);
    1321              :     }
    1322              : 
    1323            0 :     bool HcclCommunicator::CompareWithUserRank(const RankInfo &left, const RankInfo &right)
    1324              :     {
    1325            0 :         return left.userRank < right.userRank;
    1326              :     }
    1327              : 
    1328          305 :     HcclResult HcclCommunicator::InitPreResource(const RankTable_t &rankTable)
    1329              :     {
    1330          305 :         if (static_cast<s32>(devicePhyId_) == HOST_DEVICE_ID) {
    1331            0 :             HCCL_ERROR("[Init][PreResource]not support cpu rank");
    1332            0 :             return HCCL_E_NOT_SUPPORT;
    1333              :         }
    1334              :         (void)rankTable;
    1335              :         // 判断是否为A3多docker场景,该场景需要使用sdid获取到的serverId判断是否属于同一server,若属于同一server则需要enablep2p
    1336          305 :         if (deviceType_ == DevType::DEV_TYPE_910_93) {
    1337           16 :             uint32_t localRankServerId = 0;
    1338           16 :             uint32_t remoteRankServerId = 0;
    1339           16 :             rtError_t ret = rtGetServerIDBySDID(rankInfoList_[userRank_].superDeviceId, &localRankServerId);
    1340           16 :             CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Init][PreResource]rtGetServerIDBySDID failed sdid[0x%08x], serverID[%u], ret[%u]",
    1341              :                 rankInfoList_[userRank_].superDeviceId, localRankServerId, ret), HCCL_E_RUNTIME);
    1342           65 :             for (size_t index = 0; index < rankInfoList_.size(); ++index)
    1343              :             {
    1344           49 :                 const RankInfo &rankInfo = rankInfoList_[index];
    1345           49 :                 ret = rtGetServerIDBySDID(rankInfo.superDeviceId, &remoteRankServerId);
    1346           49 :                 CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Init][PreResource]rtGetServerIDBySDID failed sdid[0x%08x], serverID[%u], ret[%u]",
    1347              :                     rankInfo.superDeviceId, remoteRankServerId, ret), HCCL_E_RUNTIME);
    1348           49 :                 if (serverId_ != rankInfo.serverId && localRankServerId == remoteRankServerId) {
    1349            8 :                     enableP2PDevices_.push_back(rankInfo.devicePhyId);
    1350            8 :                     enableP2PRankIds_.insert(rankInfo.userRank);
    1351            8 :                     HCCL_INFO("[Init][PreResource]localRankID[%u]-localDevicePhyId[%u] needs to enablep2p with remoteRankId[%u]-remoteDevicePhyId[%u], " \
    1352              :                         "and localServerId[%s], localServerIdBySDID[%u], remoteServerId[%s], remoteServerIdBySDID[%u]",
    1353              :                         userRank_, rankInfoList_[userRank_].devicePhyId, rankInfo.userRank, rankInfo.devicePhyId,
    1354              :                         serverId_.c_str(), localRankServerId, rankInfo.serverId.c_str(), remoteRankServerId);
    1355              :                 }
    1356              :             }
    1357              :         }
    1358              :         // 查询本rank所在服务器
    1359          305 :         auto iterServ = servRankInfo_.find(serverId_);
    1360              : 
    1361          302 :         bool check = (iterServ == servRankInfo_.end());
    1362          302 :         CHK_PRT_RET(check, HCCL_ERROR("[Init][PreResource]can't find serverId[%s] in server map", serverId_.c_str()),
    1363              :                     HCCL_E_NOT_FOUND);
    1364              : 
    1365         1242 :         for (u32 i = 0; i < iterServ->second.size(); i++) {
    1366          935 :             if (iterServ->second[i].deviceInfo.devicePhyId != HOST_DEVICE_ID) {
    1367          934 :                 enableP2PDevices_.push_back(iterServ->second[i].deviceInfo.devicePhyId);
    1368          936 :                 enableP2PRankIds_.insert(iterServ->second[i].rankId);
    1369          934 :                 HCCL_INFO("[Init][PreResource]In the current server[%s], localRank[%u]-localDevicePhyId[%u] needs to enableP2P " \
    1370              :                     "with remoteRankId[%u]-remoteDevicePhyId[%u]",
    1371              :                     serverId_.c_str(), userRank_, rankInfoList_[userRank_].devicePhyId,
    1372              :                     iterServ->second[i].rankId, iterServ->second[i].deviceInfo.devicePhyId);
    1373              :             }
    1374              :         }
    1375          305 :         HCCL_INFO("[Init][PreResource]Current deviceType[%d], isStandardCard[%s]", deviceType_, isStandardCard_ ? "true" : "false");
    1376          305 :         if (deviceType_ != DevType::DEV_TYPE_310P3 && !isStandardCard_) {
    1377          155 :             HcclResult ret = P2PMgmtPub::EnableP2P(enableP2PDevices_);
    1378          155 :             CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Init][PreResource]Enable P2P Failed, deviceLogicId[%d], ret[%u]", deviceLogicId_, ret), ret);
    1379              :         }
    1380              : 
    1381          305 :         drvInit_ = true;
    1382          305 :         return HCCL_SUCCESS;
    1383              :     }
    1384              : 
    1385          493 :     HcclResult HcclCommunicator::InitTcpMode(const RankTable_t &rankTable) const
    1386              :     {
    1387          493 :         bool isTcpMode = false;
    1388          493 :         HCCL_INFO("[TcpMode][%u] [1:TCP, 2:RDMA, 3:RESERVED]", GetExternalInputProtocolType());
    1389          495 :         if (GetExternalInputProtocolType() == ProtocolType::TCP) {
    1390            0 :             isTcpMode = true;
    1391              :         }
    1392          495 :         else if (GetExternalInputProtocolType() == ProtocolType::RDMA) {
    1393              :             // 通信协议选择RDMA
    1394              :         } else {
    1395          296 :             isTcpMode = (rankTable.nicDeploy == NICDeployment::NIC_DEPLOYMENT_HOST);
    1396          296 :             HCCL_INFO("[Init][TcpMode]isTcpMode[%d] nicDeploy[%d]", isTcpMode, rankTable.nicDeploy);
    1397              :         }
    1398          495 :         SetTcpMode(isTcpMode);
    1399              : 
    1400              :         // 异构场景解析外部输入,放在SetTcpMode前防止Tcp用例走错分支,放在RecordProtocolType确保hdc模式下建链通信协议校验正确
    1401          495 :         CHK_RET(InitExternalInputHeterog());
    1402          493 :         return HCCL_SUCCESS;
    1403              :     }
    1404              : 
    1405          488 :     HcclResult HcclCommunicator::InitMyRankConnectMode(HcclCommParams &params, const RankTable_t &rankTable)
    1406              :     {
    1407          488 :         if (deviceType_ != DevType::DEV_TYPE_910B) {    /* 910B才支持host网卡特性 */
    1408          325 :             myRankConnectMode_ = 0;
    1409          325 :             return HCCL_SUCCESS;
    1410              :         }
    1411          163 :         uint32_t localRank = params.rank;
    1412          163 :         if (rankTable.nicDeploy != NICDeployment::NIC_DEPLOYMENT_HOST) {
    1413          163 :             myRankConnectMode_ = 0;
    1414          163 :             return HCCL_SUCCESS;
    1415              :         }
    1416            0 :         for (auto it : rankTable.rankList) {
    1417            0 :             if (it.rankId == localRank) {
    1418            0 :                 continue;
    1419              :             }
    1420            0 :             if (it.deviceInfo.nicDeploy == NICDeployment::NIC_DEPLOYMENT_DEVICE) {
    1421            0 :                 myRankConnectMode_ = 1;
    1422            0 :                 return HCCL_SUCCESS;
    1423              :             }
    1424            0 :         }
    1425            0 :         myRankConnectMode_ = 0;
    1426            0 :         return HCCL_SUCCESS;
    1427              :     }
    1428              : 
    1429          233 :     uint32_t HcclCommunicator::GetConnectMode()
    1430              :     {
    1431          233 :         return myRankConnectMode_;
    1432              :     }
    1433              : 
    1434         1790 :     bool HcclCommunicator::IsEnableBackupLink()
    1435              :     {
    1436          224 :         return deviceType_ == DevType::DEV_TYPE_910_93 && IsEnableRoce() && GetAicpuUnfoldConfig() && retryEnable_ &&
    1437         2014 :                commConfig_.GetConfigInterSuperPodRetryEnable() && !devBackupIpAddr_[0].IsInvalid() && rtsSupportChangeLink_ &&
    1438         1790 :                !isDiffDeviceType_;
    1439              :     }
    1440              : 
    1441          162 :     HcclResult HcclCommunicator::InitRaNetResource()
    1442              :     {
    1443          162 :         CHK_RET(IsHostUseDevNic(isHostUseDevNic_));
    1444              : 
    1445          162 :         if (static_cast<s32>(devicePhyId_) != HOST_DEVICE_ID ||
    1446            0 :             nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_DEVICE) {
    1447          162 :             CHK_RET(HcclNetInit(NICDeployment::NIC_DEPLOYMENT_DEVICE, devicePhyId_, deviceLogicId_, false));
    1448          161 :             if (IsEnableBackupLink()) {
    1449              :                 // 超节点 && level2支持重执行 && Aicpu -> 初始化主备hccp资源(Pid粒度)
    1450            1 :                 CHK_RET(hrtGetPairDevicePhyId(devicePhyId_, deviceBackUpPhyId_));
    1451            1 :                 if (hrtGetDeviceIndexByPhyId(deviceBackUpPhyId_, deviceBackUpLogicId_) != HCCL_SUCCESS) {
    1452            1 :                     rtsSupportChangeLink_ = false;
    1453            1 :                     HCCL_ERROR("[%s]Runtime does not support changelink, deviceLogicId_[%d], devicePhyId_[%u], "
    1454              :                                "deviceBackUpPhyId_[%u], deviceBackUpLogicId_[%u], nicDeployment_[%d], IsEnableBackupLink[%d]"
    1455              :                                "rtsSupportChangeLink_[%d]",
    1456              :                                __func__, deviceLogicId_, devicePhyId_, deviceBackUpPhyId_,
    1457              :                                deviceBackUpLogicId_, nicDeployment_, IsEnableBackupLink(), rtsSupportChangeLink_);
    1458            1 :                     return HCCL_E_NOT_SUPPORT;
    1459              :                 } else {
    1460            0 :                     CHK_RET(HcclNetInit(NICDeployment::NIC_DEPLOYMENT_DEVICE, deviceBackUpPhyId_, deviceBackUpLogicId_,
    1461              :                                         false, true));
    1462            0 :                     HCCL_DEBUG("[%s]Default & backup NetworkManager Init, deviceLogicId[%d], devicePhyId[%u], "
    1463              :                                "deviceBackUpPhyId_[%u], deviceBackUpLogicId_[%u], nicDeployment_[%d], IsEnableBackupLink[%d]",
    1464              :                                __func__, deviceLogicId_, devicePhyId_, deviceBackUpPhyId_, deviceBackUpLogicId_,
    1465              :                                nicDeployment_, IsEnableBackupLink());
    1466              :                 }
    1467              :             }
    1468              :         }
    1469              : 
    1470          160 :         if ((static_cast<s32>(devicePhyId_) != HOST_DEVICE_ID && isHaveCpuRank_) ||
    1471          480 :             (IsEnableRoce() && nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST) ||
    1472          160 :             (Is310PDevice() && nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST)) {
    1473            0 :             u32 devicePhyID = (static_cast<s32>(devicePhyId_) == HOST_DEVICE_ID) ? 0 : devicePhyId_;
    1474            0 :             u32 whiteListEn = (GetExternalInputHcclEnableWhitelist() == HCCL_WHITELIST_ON ? 1 : 0);
    1475            0 :             CHK_RET(HcclNetInit(NICDeployment::NIC_DEPLOYMENT_HOST, devicePhyID, deviceLogicId_, whiteListEn));
    1476              :         }
    1477          160 :         return HCCL_SUCCESS;
    1478              :     }
    1479              : 
    1480          160 :     HcclResult HcclCommunicator::InitRaNic()
    1481              :     {
    1482          160 :         CHK_RET(InitSocketManager());
    1483              : 
    1484          160 :         if (Is310PDevice()) {
    1485            0 :             CHK_RET(InitNic());
    1486          160 :         } else if (static_cast<s32>(devicePhyId_) != HOST_DEVICE_ID) {
    1487          160 :             std::shared_ptr<HcclSocket> &devVnicSocket = commPortConfig_.devVnicListen.first;
    1488          160 :             if (devVnicSocket) {
    1489            1 :                 localVnicIp_ = devVnicSocket->GetLocalIp();
    1490            1 :                 localVnicListenPort_ = devVnicSocket->GetLocalPort();
    1491            1 :                 HcclNetDevCtx &devVnicCtx = commPortConfig_.devVnicListen.second;
    1492            1 :                 CHK_PTR_NULL(devVnicCtx);
    1493            1 :                 netDevCtxMap_.insert(std::make_pair(localVnicIp_, devVnicCtx));
    1494            1 :                 CHK_RET(socketManager_->ServerInit(devVnicCtx, localVnicListenPort_));
    1495            1 :                 commPortConfig_.devVnicListen.second = nullptr;
    1496            1 :                 HCCL_INFO("[HcclCommunicator][InitRaResource] init vnic with listened socket success, "
    1497              :                           "listened ip[%s] port[%u]",
    1498              :                           localVnicIp_.GetReadableAddress(), localVnicListenPort_);
    1499              :             } else {
    1500          159 :                 localVnicListenPort_ = GetLocalNicPort(NicType::VNIC_TYPE);
    1501          159 :                 localVnicIp_ = HcclIpAddress(devicePhyId_);
    1502          159 :                 if (useSuperPodMode_) {
    1503            6 :                     CHK_RET(hrtRaGetSingleSocketVnicIpInfo(
    1504              :                         devicePhyId_, DeviceIdType::DEVICE_ID_TYPE_SDID, superDeviceId_, localVnicIp_));
    1505              :                 } else {
    1506          159 :                     CHK_RET(hrtRaGetSingleSocketVnicIpInfo(
    1507              :                         devicePhyId_, DeviceIdType::DEVICE_ID_TYPE_PHY_ID, devicePhyId_, localVnicIp_));
    1508              :                 }
    1509              : 
    1510              :                 HcclNetDevCtx vnicPortCtx;
    1511          159 :                 CHK_RET(HcclNetOpenDev(&vnicPortCtx, NicType::VNIC_TYPE, devicePhyId_, deviceLogicId_, localVnicIp_));
    1512          159 :                 CHK_PTR_NULL(vnicPortCtx);
    1513          159 :                 netDevCtxMap_.insert(std::make_pair(localVnicIp_, vnicPortCtx));
    1514          159 :                 CHK_RET(socketManager_->ServerInit(vnicPortCtx, localVnicListenPort_));
    1515          153 :                 HCCL_INFO("[HcclCommunicator][InitRaResource] init vnic with ip[%s] port[%u] success",
    1516              :                           localVnicIp_.GetReadableAddress(), localVnicListenPort_);
    1517              :             }
    1518              : 
    1519          154 :             if (IsEnableRoce()) {
    1520            0 :                 CHK_RET(InitNic());
    1521              :             }
    1522              :         }
    1523          154 :         return HCCL_SUCCESS;
    1524              :     }
    1525              : 
    1526          277 :     HcclResult HcclCommunicator::InitRaResource()
    1527              :     {
    1528              :         /* 本通信域内只有1个device时,不需要初始化ra资源 */
    1529          277 :         if (userRankSize_ <= 1) {
    1530          116 :             HCCL_INFO("user rank size <= 1, ra is not needed for single device.");
    1531          116 :             return HCCL_SUCCESS;
    1532              :         }
    1533              : 
    1534          161 :         CHK_RET(InitRaNetResource());
    1535          160 :         CHK_RET(InitRaNic());
    1536              : 
    1537          154 :         HCCL_INFO("isUsedRdmaLevel0_[%u] nicNum[%u] hostIP[%s], nicDeployment[%d].",
    1538              :                   isUsedRdmaLevel0_, devIpAddr_.size(), hostIp_.GetReadableAddress(), nicDeployment_);
    1539              : 
    1540          154 :         raResourceInit_ = true; // 全局通信域会初始化,子通信域不会初始化,但是析构均会进入此逻辑,需要标记
    1541          154 :         attrCollector_.GenSupportRdmaLite();
    1542          154 :         CHK_RET(attrCollector_.GenSupportHccsAndSio());
    1543          154 :         isSupportRdmaLite_ = attrCollector_.GetSupportRdmaLite();     // 是否支持Rdma Lite
    1544          154 :         isSupportHccsAndSio_ = attrCollector_.GetSupportHccsAndSio(); // 是否支持Hccs Sio并发
    1545          154 :         return HCCL_SUCCESS;
    1546              :     }
    1547              : 
    1548          305 :     HcclResult HcclCommunicator::DisablePreResource()
    1549              :     {
    1550              :         // 查询本rank所在服务器
    1551          305 :         auto iterServ = servRankInfo_.find(serverId_);
    1552          305 :         bool check = (iterServ == servRankInfo_.end());
    1553          305 :         CHK_PRT_RET(check, HCCL_ERROR("[Disable][PreResource]can't find serverId[%s] in server map", serverId_.c_str()),
    1554              :                     HCCL_E_NOT_FOUND);
    1555          305 :         HcclResult ret = P2PMgmtPub::DisableP2P(enableP2PDevices_);
    1556          304 :         CHK_PRT_RET(ret != HCCL_SUCCESS,
    1557              :                     HCCL_ERROR("[Disable][PreResource]Disable all P2P Failed, deviceLogicId[%d], ret[%u]",
    1558              :                                deviceLogicId_, ret),
    1559              :                     ret);
    1560          304 :         enableP2PDevices_.clear();
    1561          304 :         enableP2PRankIds_.clear();
    1562          305 :         return HCCL_SUCCESS;
    1563              :     }
    1564              : 
    1565          157 :     HcclResult HcclCommunicator::GetWorkspaceSubStreamNum(u64 count, HcclDataType dataType, HcclReduceOp op,
    1566              :         const std::string &algName,u64 &streamNum, u64 dataSize, bool ifAiv, HcclCMDType opType)
    1567              :     {
    1568          157 :         AlgType algType;
    1569              : 
    1570          157 :         CHK_RET(GetAlgType(algType, opType));
    1571              : 
    1572              :         std::map<HcclCMDType, u64> gapMap = {
    1573              :             {HcclCMDType::HCCL_CMD_REDUCE_SCATTER, HCCL_SMALL_COUNT_512_KB + HCCL_SMALL_COUNT_512_KB},
    1574              :             {HcclCMDType::HCCL_CMD_ALLGATHER, HCCL_SMALL_COUNT_512_KB + HCCL_SMALL_COUNT_512_KB},
    1575          315 :             {HcclCMDType::HCCL_CMD_ALLREDUCE, (HCCL_SMALL_COUNT_512_KB + HCCL_SMALL_COUNT_512_KB) * userRankSize_}};
    1576              : 
    1577              :         // 图模式下AIV展开,需要重新计算streamNum
    1578          157 :         bool ifHcomWithAiv = ifAiv && (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB);
    1579          157 :         HCCL_INFO("[GetWorkspaceSubStreamNum] ifAiv[%d], workflowMode[%d], ifHcomWithAiv[%d]",
    1580              :                   ifAiv, GetWorkflowMode(), ifHcomWithAiv);
    1581          158 :         if (ifHcomWithAiv && (deviceType_ == DevType::DEV_TYPE_910_93 || deviceType_ == DevType::DEV_TYPE_910B)) {
    1582            0 :             HCCL_INFO("[GetWorkspaceSubStreamNum] Hcom AIV enabled, calculating the streamNum.");
    1583              :             // A3 和 A2 公用以下的参数
    1584            0 :             std::string newTag;
    1585            0 :             std::unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(opType);
    1586            0 :             CHK_SMART_PTR_NULL(algOperator);
    1587            0 :             OpParam param;
    1588            0 :             param.reduceType = op;
    1589            0 :             param.opType = opType;
    1590              : 
    1591            0 :             if (opType == HcclCMDType::HCCL_CMD_ALLTOALL || opType == HcclCMDType::HCCL_CMD_ALLTOALLV) {
    1592            0 :                 param.All2AllDataDes.sendType = dataType;
    1593            0 :                 param.All2AllDataDes.recvType = dataType;
    1594            0 :                 param.All2AllDataDes.sendCount = count;
    1595              :             } else { //不论 A2 还是 A3,AIV场景下的AllReduce/ReduceScatter还是A2上单独支持AIV的算子都用以下参数
    1596            0 :                 param.DataDes.count = count;
    1597            0 :                 param.DataDes.dataType = dataType;
    1598              :             }
    1599            0 :             AlgResourceRequest resRequest;
    1600            0 :             CHK_RET(algOperator->CalcResRequest(algName, param, resRequest)); // 计算资源请求
    1601            0 :             streamNum = resRequest.streamNum;
    1602            0 :             HCCL_INFO("[GetWorkspaceSubStreamNum] Hcom AIV enabled on DeviceType[%d], the streamNum is [%llu]",
    1603              :                       deviceType_, streamNum);
    1604            0 :             return HCCL_SUCCESS;
    1605            0 :         }
    1606              : 
    1607          155 :         if (serverNum_ == 1 && deviceType_ == DevType::DEV_TYPE_910_93 && opType == HcclCMDType::HCCL_CMD_ALLGATHER &&
    1608          313 :             dataSize <= gapMap[opType] &&
    1609            0 :             deviceNumPerAggregation_ > HCCL_DEVICE_NUM_TWO) {
    1610            0 :             constexpr u64 streamForSmallCount = 3;
    1611            0 :             streamNum = streamForSmallCount;
    1612            0 :             HCCL_DEBUG("[GetWorkspaceSubStreamNum]DEV_TYPE_910_93 Single Server, the streamNum is %llu", streamNum);
    1613            0 :             return HCCL_SUCCESS;
    1614              :         }
    1615              : 
    1616          155 :         if (serverNum_ == 1 && deviceType_ == DevType::DEV_TYPE_910_93 && gapMap.find(opType) != gapMap.end() &&
    1617          313 :             dataSize <= gapMap[opType] &&
    1618            0 :             deviceNumPerAggregation_ > HCCL_DEVICE_NUM_TWO) {
    1619            0 :             streamNum = deviceNumPerAggregation_ - HCCL_SUB_STREAM_NP_MESH;
    1620            0 :             HCCL_DEBUG("[GetWorkspaceSubStreamNum]DEV_TYPE_910_93 Single Server, the streamNum is %llu", streamNum);
    1621            0 :             return HCCL_SUCCESS;
    1622              :         }
    1623              : 
    1624          158 :         if (deviceType_ == DevType::DEV_TYPE_910_93) {
    1625            8 :             streamNum = HCCL_SUB_STREAM_NUM_DOUBLE_RING + RDMA_PLANE_NUM_IN_NPRING_DOUBLE;
    1626            8 :             if (algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_NP_DOUBLE_RING) {
    1627            0 :                 streamNum += 1U; // semi_ring算法server内增加一条从流,需要2条从流
    1628              :             }
    1629            8 :             if (opType == HcclCMDType::HCCL_CMD_ALLTOALLV || opType == HcclCMDType::HCCL_CMD_ALLTOALL ||
    1630            8 :                 opType == HcclCMDType::HCCL_CMD_ALLTOALLVC) {
    1631            0 :                 streamNum = MAX_RANK_SIZE;
    1632              :             }
    1633            8 :             HCCL_DEBUG("[GetWorkspaceSubStreamNum]DEV_TYPE_910_93, the streamNum is %llu", streamNum);
    1634            8 :             return HCCL_SUCCESS;
    1635              :         }
    1636              : 
    1637              :         // AR RS 在开启Strict && 静态图、RSv 在开启确定性 && 静态图时, 需要重新计算StreamNum
    1638          300 :         if (deviceType_ == DevType::DEV_TYPE_910B
    1639          262 :             && (((opType == HcclCMDType::HCCL_CMD_ALLREDUCE || opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER)
    1640            0 :                     && GetExternalInputHcclDeterministicV2() == DETERMINISTIC_STRICT)
    1641          112 :                 || (opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V
    1642            0 :                     && GetExternalInputHcclDeterministicV2() != DETERMINISTIC_DISABLE)
    1643          112 :                 || (opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V
    1644            0 :                     && !isSingleMeshAggregation_ && !multiModuleDiffDeviceNumMode_//多机&对称&图模式
    1645            0 :                     && GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB))) {
    1646              :             // 图模式 A2规约保序场景,需要重新计算需要的streamNum
    1647            0 :             streamNum = CalcStreamNumForReduceOrderPreservation();
    1648            0 :             HCCL_DEBUG("[GetWorkspaceSubStreamNum]A2 reduce order preservation, the streamNum is %llu", streamNum);
    1649            0 :             return HCCL_SUCCESS;
    1650              :         }
    1651              : 
    1652          150 :         if (deviceType_ == DevType::DEV_TYPE_910B && opType == HcclCMDType::HCCL_CMD_ALLREDUCE && algType.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_PIPELINE) {
    1653            0 :             streamNum = userRankSize_ / moduleNum_ - 1;
    1654            0 :             HCCL_DEBUG("[GetWorkspaceSubStreamNum]A2 pipeline AllReduce, the streamNum is %llu", streamNum);
    1655            0 :             return HCCL_SUCCESS;
    1656              :         }
    1657              : 
    1658              :         // 设置AG和RS的图模式pipeline算法能够申请的streamNum
    1659          150 :         if (deviceType_ == DevType::DEV_TYPE_910B &&                                                         // 910B
    1660          112 :             algType.algoLevel0 == AlgTypeLevel0::ALG_LEVEL0_NP_MESH &&                                       // fullmesh
    1661           88 :             (opType == HcclCMDType::HCCL_CMD_ALLGATHER || opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER ||
    1662           88 :             opType == HcclCMDType::HCCL_CMD_ALLGATHER_V) && // AG或RS或AGV
    1663            0 :             moduleNum_ > 1 && deviceNumPerAggregation_ > 1 &&                                                // 多机且每机器出多卡
    1664            0 :             (moduleNum_ <= MODULE_NUM_FOUR ||                                                                // "机器数量小于等于4"
    1665            0 :              dataSize > HCCL_SMALL_COUNT_1_MB ||                                                             // "大数据量"
    1666            0 :              algType.algoLevel1 == AlgTypeLevel1::ALG_LEVEL1_PIPELINE)) { // "指定level1的算法为pipeline"
    1667            0 :             streamNum = userRankSize_ / moduleNum_;
    1668            0 :             HCCL_DEBUG("[GetWorkspaceSubStreamNum]DEV_TYPE_910B, the streamNum is %llu", streamNum);
    1669            0 :             return HCCL_SUCCESS;
    1670              :         }
    1671              : 
    1672              :         // 设置310P图模式 alltoall 的streamNum
    1673          150 :         if(deviceType_ == DevType::DEV_TYPE_310P3 && (opType == HcclCMDType::HCCL_CMD_ALLTOALL || opType == HcclCMDType::HCCL_CMD_ALLTOALLV || opType == HcclCMDType::HCCL_CMD_ALLTOALLVC)){
    1674            0 :             streamNum = userRankSize_ * RANK_SET_COMPUTE_CONST;
    1675            0 :             HCCL_DEBUG("[GetWorkspaceSubStreamNum]DEV_TYPE_310P3, the streamNum is %llu", streamNum);
    1676            0 :             return HCCL_SUCCESS;
    1677              :         }
    1678              : 
    1679              :         // 根据所用算法,选择所需的从stream数目
    1680          150 :         switch (algType.algoLevel0) {
    1681           88 :             case AlgTypeLevel0::ALG_LEVEL0_NP_MESH:
    1682           88 :                 streamNum = userRankSize_ / moduleNum_ - HCCL_SUB_STREAM_NP_MESH;
    1683           88 :             break;
    1684           16 :             case AlgTypeLevel0::ALG_LEVEL0_8P_RING:
    1685           16 :                 streamNum = HCCL_SUB_STREAM_NUM_8P_RING;
    1686           16 :             break;
    1687            0 :             case AlgTypeLevel0::ALG_LEVEL0_NP_DOUBLE_RING:
    1688            0 :                 streamNum = HCCL_SUB_STREAM_NUM_DOUBLE_RING;
    1689            0 :             break;
    1690           12 :             case AlgTypeLevel0::ALG_LEVEL0_4P_MESH:
    1691           12 :                 streamNum = HCCL_SUB_STREAM_NUM_4P_MESH;
    1692           12 :             break;
    1693           34 :             default:
    1694           34 :                 streamNum = HCCL_SUB_STREAM_NUM_ZERO;
    1695           34 :             break;
    1696              :         }
    1697              : 
    1698          150 :         if (SatisfyIntraSuperPod(deviceType_, userRankSize_, useSuperPodMode_, superPodNum_)) {
    1699            0 :             streamNum = std::max(static_cast<u64>(userRankSize_ - 1u), streamNum);
    1700          449 :         } else if (FullmeshPairwiseSatisfyHighPerfAlltoallMeshCondition(deviceType_,
    1701          148 :                                                                       meshAggregationRankSize_, useSuperPodMode_, commConfig_.GetConfigHcclAlgo(HcclCMDType::HCCL_CMD_ALLTOALL))) {
    1702            0 :             streamNum = std::max(static_cast<u64>(meshAggregationRankSize_ - 1u), streamNum);
    1703              :         }
    1704              : 
    1705          150 :         auto iter = HCCL_ALGO_LEVEL0_NAME_MAP.find(algType.algoLevel0);
    1706          150 :         CHK_PRT_RET(iter == HCCL_ALGO_LEVEL0_NAME_MAP.end(),
    1707              :                     HCCL_ERROR("[GetWorkspaceSubStreamNum]level0: algType[%u] is invalid.", algType.algoLevel0),
    1708              :                     HCCL_E_INTERNAL);
    1709          150 :         HCCL_DEBUG("[GetWorkspaceSubStreamNum]hccl algorithm: In level0, using %s algo, the streamNum is %llu",
    1710              :                    iter->second.c_str(), streamNum);
    1711              : 
    1712          150 :         u64 sliceNum = CalculatePiplineSliceNum(opType, dataSize, algType, deviceType_, deviceNumPerServer_, serverNum_);
    1713              :         // 图模式下数据量固定, 按照当前数据量判断是否支持pipline切分并申请从流
    1714          150 :         if (implAlg_ != nullptr && sliceNum >= MIN_PIPLINE_SLICE_NUM) {
    1715            0 :             streamNum++;
    1716              :         }
    1717          150 :         return HCCL_SUCCESS;
    1718          158 :     }
    1719              : 
    1720          804 :     HcclResult HcclCommunicator::DestroyNetworkResources()
    1721              :     {
    1722          804 :         transportManager_ = nullptr;
    1723          804 :         if (raResourceInit_) {
    1724          153 :             socketManager_->DestroySockets();
    1725              :         }
    1726              : 
    1727              :         /* 本通信域内只有1个device时,不需要卸载ra资源 */
    1728          804 :         if (userRankSize_ <= 1) {
    1729          208 :             HCCL_INFO("user rank size <= 1, ra is not needed for single device");
    1730          208 :             return HCCL_SUCCESS;
    1731              :         }
    1732              : 
    1733              :         // nic的初始化独立调用,在此单独判断是否需要解初始化
    1734          596 :         if (nicInitialized_ > 0) {
    1735            0 :             CHK_RET(DeinitNic());
    1736              :         }
    1737              : 
    1738          596 :         if (raResourceInit_ && (static_cast<s32>(devicePhyId_) != HOST_DEVICE_ID) && !Is310PDevice()) {
    1739          153 :             CHK_RET(socketManager_->ServerDeInit(netDevCtxMap_[localVnicIp_], localVnicListenPort_));
    1740          153 :             HcclNetCloseDev(netDevCtxMap_[localVnicIp_]);
    1741          153 :             netDevCtxMap_.erase(localVnicIp_);
    1742              :         }
    1743              : 
    1744          595 :         CHK_RET(ReleasePreemptSocket());
    1745              : 
    1746          595 :         if (raResourceInit_) {
    1747          153 :             if (static_cast<s32>(devicePhyId_) != HOST_DEVICE_ID ||
    1748            0 :                 nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_DEVICE) {
    1749          153 :                 if (IsEnableBackupLink()) {
    1750              :                     // 超节点 && level2支持重执行 && Aicpu -> 释放主备hccp资源
    1751            0 :                     CHK_RET(HcclNetDeInit(NICDeployment::NIC_DEPLOYMENT_DEVICE, devicePhyId_, deviceLogicId_));
    1752            0 :                     CHK_RET(HcclNetDeInit(NICDeployment::NIC_DEPLOYMENT_DEVICE, deviceBackUpPhyId_,
    1753              :                                           deviceBackUpLogicId_, true));
    1754            0 :                     HCCL_DEBUG("[%s]Default & backup HcclNetDeInit, deviceLogicId[%d], devicePhyId[%u], "
    1755              :                                "deviceBackUpPhyId_[%u], deviceBackUpLogicId_[%u], nicDeployment_[%d], IsEnableBackupLink[%d]",
    1756              :                                __func__, deviceLogicId_, devicePhyId_, deviceBackUpPhyId_, deviceBackUpLogicId_,
    1757              :                                nicDeployment_, IsEnableBackupLink());
    1758              :                 }
    1759              :                 else {
    1760          153 :                     CHK_RET(HcclNetDeInit(NICDeployment::NIC_DEPLOYMENT_DEVICE, devicePhyId_, deviceLogicId_));
    1761              :                 }
    1762              :             }
    1763              : 
    1764          153 :             if ((static_cast<s32>(devicePhyId_) != HOST_DEVICE_ID && isHaveCpuRank_) ||
    1765          459 :                 (IsEnableRoce() && nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST) ||
    1766          153 :                 (Is310PDevice() && nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST)) {
    1767            0 :                 u32 devicePhyID = (static_cast<s32>(devicePhyId_) == HOST_DEVICE_ID) ? 0 : devicePhyId_;
    1768            0 :                 CHK_RET(HcclNetDeInit(NICDeployment::NIC_DEPLOYMENT_HOST, devicePhyID, deviceLogicId_));
    1769              :             }
    1770              : 
    1771          153 :             socketManager_ = nullptr;
    1772              :         }
    1773              : 
    1774          595 :         raResourceInit_ = false;
    1775          595 :         return HCCL_SUCCESS;
    1776              :     }
    1777              : 
    1778          155 :     HcclResult HcclCommunicator::SetWorkspaceResource(const std::string &tag, void *memPtr, u64 &maxSize,
    1779              :                                                       std::vector<rtStream_t> &stream)
    1780              :     {
    1781          155 :         return workSpaceRes_->SetWorkspaceResource(tag, memPtr, maxSize, stream);
    1782              :     }
    1783              : 
    1784           19 :     void HcclCommunicator::DestroyWorkspaceResource(const std::string &tag)
    1785              :     {
    1786           19 :         if (workSpaceRes_ == nullptr) {
    1787            3 :             return;
    1788              :         }
    1789           14 :         workSpaceRes_->DestroyWorkspaceResource(tag);
    1790              :     }
    1791              : 
    1792          438 :     HcclResult HcclCommunicator::AtomicInitSet()
    1793              :     {
    1794          876 :         CHK_PRT_RET(initializedFlag_.test_and_set(),
    1795              :                     HCCL_ERROR("[HcclCommunicator][AtomicInitSet]errNo[0x%016llx] instance "
    1796              :                                "already been initialized",
    1797              :                                HCCL_ERROR_CODE(HCCL_E_INTERNAL)),
    1798              :                     HCCL_E_INTERNAL);
    1799          437 :         return HCCL_SUCCESS;
    1800              :     }
    1801              : 
    1802            5 :     void HcclCommunicator::AtomicInitClear()
    1803              :     {
    1804            5 :         initializedFlag_.clear();
    1805            5 :     }
    1806              : 
    1807          347 :     u32 HcclCommunicator::GetUserRank()
    1808              :     {
    1809          347 :         return realUserRank_;
    1810              :     }
    1811              : 
    1812           72 :     u32 HcclCommunicator::GetGroupRank()
    1813              :     {
    1814           72 :         return userRank_;
    1815              :     }
    1816              : 
    1817          338 :     u32 HcclCommunicator::GetRankSize()
    1818              :     {
    1819          338 :         return userRankSize_;
    1820              :     }
    1821              : 
    1822            0 :     u32 HcclCommunicator::GetRankInParentComm()
    1823              :     {
    1824            0 :         return rankInParentComm_;
    1825              :     }
    1826              : 
    1827            0 :     bool HcclCommunicator::GetNicInitialized()
    1828              :     {
    1829            0 :         return nicInitialized_ > 0;
    1830              :     }
    1831              : 
    1832              :     /*
    1833              :         1. 选择算法
    1834              :         2. 计算resource,存到request内
    1835              :         3. 创建和分配资源
    1836              :     */
    1837            0 :     HcclResult HcclCommunicator::HcclSelectAlg(HcclCMDType opType, u64 count, void* counts, HcclDataType dataType,
    1838              :                                                HcclReduceOp op, int32_t aivCoreLimit, bool &ifAiv, std::string &algName)
    1839              :     {
    1840            0 :         HCCL_INFO("[HcclCommunicator][HcclSelectAlg] start to run with opType[%d], count[%llu], dataType[%d], reduceOp[%d], aivCoreLimit[%d]",
    1841              :                   opType, count, dataType, op, aivCoreLimit);
    1842            0 :         ifAiv = false;
    1843            0 :         if (opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V || opType == HcclCMDType::HCCL_CMD_ALLGATHER_V || 
    1844            0 :             opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV || opType == HcclCMDType::HCCL_CMD_BATCH_WRITE) {
    1845            0 :             HCCL_INFO("[HcclCommunicator][HcclSelectAlg] opType[%d] no need select AIV algorithm", opType);
    1846            0 :             return HCCL_SUCCESS;
    1847              :         }
    1848              :         /* 选择算法前,先更新成图模式 */
    1849            0 :         auto originWorkflowMode = GetWorkflowMode();
    1850            0 :         SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB);
    1851              : 
    1852            0 :         std::unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(opType);
    1853            0 :         CHK_SMART_PTR_NULL(algOperator);
    1854              : 
    1855            0 :         OpParam param;
    1856            0 :         param.reduceType = op;
    1857            0 :         param.opType = opType;
    1858            0 :         if (opType == HcclCMDType::HCCL_CMD_ALLTOALL || opType == HcclCMDType::HCCL_CMD_ALLTOALLV ||
    1859            0 :             opType == HcclCMDType::HCCL_CMD_ALLTOALLVC) {
    1860            0 :             param.All2AllDataDes.sendType = dataType;
    1861            0 :             param.All2AllDataDes.recvType = dataType;
    1862            0 :             param.All2AllDataDes.sendCount = count;
    1863            0 :         } else if (opType == HcclCMDType::HCCL_CMD_ALLGATHER_V || opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V) {
    1864            0 :             param.VDataDes.counts = counts;
    1865            0 :             param.VDataDes.dataType = dataType;
    1866              :         } else {
    1867            0 :             param.DataDes.count = count;
    1868            0 :             param.DataDes.dataType = dataType;
    1869              :         }
    1870              : 
    1871            0 :         AlgDesc algDesc;
    1872            0 :         std::string newTag;
    1873            0 :         ResourceLimit limit{true, true, 0};
    1874            0 :         limit.aivCoreLimit = aivCoreLimit;
    1875            0 :         CHK_RET(algOperator->SelectAlg("", param, limit, algName, algDesc, newTag));
    1876              : 
    1877              :         /* 非AIV算法直接返回 */
    1878            0 :         if (!algDesc.isAivMode) {
    1879            0 :             HCCL_INFO("[HcclCommunicator][HcclSelectAlg] select non-Aiv alg, early return");
    1880            0 :             return HCCL_SUCCESS;
    1881              :         }
    1882              : 
    1883              :         /* 完成算法选择和记录后,恢复成原来的模式 */
    1884            0 :         SetWorkflowMode(originWorkflowMode);
    1885            0 :         ifAiv = true;
    1886            0 :         HCCL_INFO("[HcclCommunicator][HcclSelectAlg] compile for aiv, select algName is [%s]", algName.c_str());
    1887            0 :         return HCCL_SUCCESS;
    1888            0 :     }
    1889              : 
    1890            0 :     HcclResult HcclCommunicator::HcclCalcNumBlocks(HcclCMDType opType, u64 count, void* counts, HcclDataType dataType,
    1891              :         int32_t aivCoreLimit, std::string &algName, u32 &numBlocks)
    1892              :     {
    1893            0 :         auto originWorkflowMode = GetWorkflowMode();
    1894            0 :         SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB);
    1895            0 :         std::unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(opType);
    1896            0 :         CHK_SMART_PTR_NULL(algOperator);
    1897            0 :         OpParam param;
    1898              : 
    1899            0 :         param.opType = opType;
    1900            0 :         if (opType == HcclCMDType::HCCL_CMD_ALLTOALL || opType == HcclCMDType::HCCL_CMD_ALLTOALLV ||
    1901            0 :             opType == HcclCMDType::HCCL_CMD_ALLTOALLVC) {
    1902            0 :             param.All2AllDataDes.sendType = dataType;
    1903            0 :             param.All2AllDataDes.recvType = dataType;
    1904            0 :             param.All2AllDataDes.sendCount = count;
    1905            0 :         } else if (opType == HcclCMDType::HCCL_CMD_ALLGATHER_V || opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V) {
    1906            0 :             param.VDataDes.counts = counts;
    1907            0 :             param.VDataDes.dataType = dataType;
    1908              :         } else {
    1909            0 :             param.DataDes.count = count;
    1910            0 :             param.DataDes.dataType = dataType;
    1911              :         }
    1912              : 
    1913            0 :         CHK_PRT_RET(algOperator->CalNumBlocks(algName, param, numBlocks, aivCoreLimit) != HCCL_SUCCESS,
    1914              :             HCCL_ERROR("[%s] CalNumBlocks failed", __func__),
    1915              :             HCCL_E_PARA);
    1916            0 :         SetWorkflowMode(originWorkflowMode);
    1917            0 :         return HCCL_SUCCESS;
    1918            0 :     }
    1919              : 
    1920            1 :     HcclResult HcclCommunicator::HcclGetAlgExecParam(const std::string &tag, HcclCMDType opType, u64 count, void *inputPtr, void *outputPtr,
    1921              :                                                      bool clearEnable, HcclDataType dataType, HcclReduceOp op, void *&commContext, u64 &len, u32 aivCoreLimit)
    1922              :     {
    1923              :         /* 将Host申请和注册好的资源,传给AICPU */
    1924              :         // 1\ algName 从getstr里某一个名字里获取出来(要防止名字重复) commContext & len 从 response里拿
    1925              :         // 2\ rtmemcopy 先获取一下algoperator对象,用这个调用getalgxxx
    1926            1 :         AivSuperKernelArgs aivSuperKernelArgs;
    1927            1 :         SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB);
    1928              : 
    1929            1 :         OpParam param;
    1930            1 :         param.DataDes.count = count;
    1931            1 :         param.DataDes.dataType = dataType;
    1932            1 :         param.reduceType = op;
    1933            1 :         param.tag = tag;
    1934            1 :         param.inputPtr = inputPtr;
    1935            1 :         param.outputPtr = outputPtr;
    1936            1 :         param.opType = opType;
    1937              :         u64 totalSize;
    1938            1 :         std::vector<u64> sendCountMatrix(userRankSize_ * userRankSize_, count);
    1939            1 :         if (opType == HcclCMDType::HCCL_CMD_ALLTOALL) {
    1940            0 :             param.All2AllDataDes.sendType = dataType;
    1941            0 :             param.All2AllDataDes.recvType = dataType;
    1942            0 :             param.All2AllDataDes.sendCount = count;
    1943            0 :             param.All2AllDataDes.sendCountMatrix =static_cast<void *>(sendCountMatrix.data());
    1944              :         }
    1945              : 
    1946            1 :         if (opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER || opType == HcclCMDType::HCCL_CMD_ALLTOALL) {
    1947            0 :             totalSize = count * SIZE_TABLE[dataType] * userRankSize_;
    1948              :         } else {
    1949            1 :             totalSize = count * SIZE_TABLE[dataType]; // allreduce就是输入
    1950              :         }
    1951            1 :         param.inputSize = totalSize;
    1952            1 :         std::unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(opType);
    1953            1 :         CHK_SMART_PTR_NULL(algOperator);
    1954            1 :         std::string algName;
    1955            1 :         AlgResourceResponse algResResponse;
    1956            1 :         std::string newTag;
    1957            1 :         ResourceLimit limit;
    1958            1 :         limit.ifLimit = true;
    1959            1 :         limit.aivCoreLimit = aivCoreLimit;
    1960            1 :         AlgDesc algDesc;
    1961            1 :         algDesc.isLastSelect = true;
    1962            1 :         CHK_RET(algOperator->SelectAlg(param.tag, param, limit, algName, algDesc, newTag));
    1963              : 
    1964              :         // 资源创建
    1965            1 :         InsertNewTagToTagMap(newTag, param.tag);
    1966            1 :         if (resMap_.find(newTag) == resMap_.end()) {
    1967            1 :             HCCL_INFO("[HcclCoommunicator][HcclAllocRes] algName[%s], alloc new res", algName.c_str());
    1968            1 :             AlgResourceRequest resRequest;
    1969            1 :             CHK_RET(algOperator->CalcResRequest(algName, param, resRequest)); // [重构建议] 计算和alloc可以拆开
    1970            1 :             CHK_RET(AllocAlgResource(newTag, opType, param, resRequest, resMap_[newTag]));
    1971            1 :             CHK_RET(algOperator->PrepareCommInfoToDevice(algName, resMap_[newTag]));
    1972              :             // 暂不作心跳注册
    1973            1 :         }
    1974              : 
    1975            1 :         CHK_RET(algOperator->GetAivExecParam(algName, param, resMap_[newTag], aivSuperKernelArgs));
    1976              : 
    1977              :         // gettag
    1978            1 :         HCCL_INFO("SPK, rank %llu.", userRank_);
    1979              :         u32 numBlocks;
    1980            1 :         CHK_PRT_RET(algOperator->CalNumBlocks(algName, param, numBlocks, aivCoreLimit) != HCCL_SUCCESS,
    1981              :             HCCL_ERROR("[%s] CalNumBlocks failed", __func__),
    1982              :             HCCL_E_PARA);
    1983            1 :         if (clearEnable) {
    1984            1 :             aivOffloadTag_ = 1;
    1985              :         }
    1986            1 :         GetAivTag(algDesc.aivTagNum, false, aivSuperKernelArgs.tag); // workflowmode为图模式
    1987            1 :         aivSuperKernelArgs.numBlocks = numBlocks;
    1988              : 
    1989            1 :         HCCL_INFO("SPK, Tag %llu  aivCoreLimit %u, numBlocks %llu.", aivSuperKernelArgs.tag,
    1990              :                   aivCoreLimit, aivSuperKernelArgs.numBlocks);
    1991              :         // clearenable
    1992              :         //  拷贝到Device
    1993            1 :         SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
    1994              : 
    1995            1 :         void *sendAlgParamMemPtr = nullptr;
    1996              :         // alloc device 地址
    1997            1 :         CHK_RET(hrtMalloc(&sendAlgParamMemPtr, sizeof(AivSuperKernelArgs)));
    1998            1 :         HCCL_INFO("SPK sendalgparam %p.", sendAlgParamMemPtr);
    1999              : 
    2000            1 :         HcclResult hcclRet = hrtMemSyncCopy(sendAlgParamMemPtr, sizeof(AivSuperKernelArgs),
    2001              :                                             &aivSuperKernelArgs, sizeof(AivSuperKernelArgs),
    2002              :                                             HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE);
    2003            1 :         if (hcclRet != HCCL_SUCCESS) {
    2004            0 :             HCCL_ERROR("[HcclCommunicator][%s]hrtMemSyncCopy error, ret[%d]", __func__, hcclRet);
    2005            0 :             CHK_RET(hrtFree(sendAlgParamMemPtr));
    2006            0 :             return hcclRet;
    2007              :         }
    2008            1 :         commContext = sendAlgParamMemPtr;
    2009            1 :         len = sizeof(AivSuperKernelArgs);
    2010            1 :         return HCCL_SUCCESS;
    2011            1 :     }
    2012              : 
    2013            6 :     HcclResult HcclCommunicator::GetAivTag(s32 tagNum, bool isCapture, s32 &aivTag)
    2014              :     {
    2015            6 :         bool useOpbaseFlag = (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE && !isCapture);
    2016            6 :         if (useOpbaseFlag) {
    2017            2 :             aivTag = aivOpbaseTag_;
    2018            2 :             aivOpbaseTag_ = GetNextAivTag(aivOpbaseTag_, tagNum);
    2019              :         } else {
    2020            4 :             aivTag = aivOffloadTag_;
    2021            4 :             aivOffloadTag_ = GetNextAivTag(aivOffloadTag_, tagNum);
    2022              :         }
    2023            6 :         return HCCL_SUCCESS;
    2024              :     }
    2025              : 
    2026            2 :     HcclResult HcclCommunicator::CheckDeviceType(const DevType deviceType) const
    2027              :     {
    2028            2 :         if ((deviceType >= DevType::DEV_TYPE_COUNT) || (deviceType < DevType::DEV_TYPE_910)) {
    2029            2 :             HCCL_ERROR("[Check][DeviceType]errNo[0x%016llx] device Type[%d] out of range[%d, %d]",
    2030              :                        HCCL_ERROR_CODE(HCCL_E_PARA), deviceType, DevType::DEV_TYPE_910, DevType::DEV_TYPE_NOSOC);
    2031            2 :             return HCCL_E_PARA;
    2032              :         }
    2033            0 :         return HCCL_SUCCESS;
    2034              :     }
    2035              : 
    2036           57 :     HcclResult HcclCommunicator::CheckReductionOp(const HcclReduceOp op) const
    2037              :     {
    2038           57 :         if ((op >= HCCL_REDUCE_RESERVED) || (op < HCCL_REDUCE_SUM)) {
    2039            1 :             HCCL_ERROR("[Check][ReductionOp]errNo[0x%016llx] op:[%d] not supported", HCCL_ERROR_CODE(HCCL_E_PARA), op);
    2040            1 :             return HCCL_E_PARA;
    2041              :         }
    2042           56 :         return HCCL_SUCCESS;
    2043              :     }
    2044              : 
    2045          131 :     HcclResult HcclCommunicator::CheckUserRank(const u32 userRank) const
    2046              :     {
    2047          131 :         if (userRankSize_ <= userRank) {
    2048            3 :             HCCL_ERROR("[Check][UserRank]errNo[0x%016llx] userRank:[%u] is out of range[0 ~ %u]",
    2049              :                        HCCL_ERROR_CODE(HCCL_E_PARA), userRank, userRankSize_);
    2050            3 :             return HCCL_E_PARA;
    2051              :         }
    2052          128 :         return HCCL_SUCCESS;
    2053              :     }
    2054              : 
    2055           93 :     HcclResult HcclCommunicator::CheckCount(const u64 count) const
    2056              :     {
    2057           93 :         if (count > SYS_MAX_COUNT) {
    2058            1 :             HCCL_ERROR("[Check][Count]errNo[0x%016llx] count[%llu] is invalid(bigger than MAX count[%llu])",
    2059              :                        HCCL_ERROR_CODE(HCCL_E_PARA), count, SYS_MAX_COUNT);
    2060            1 :             return HCCL_E_PARA;
    2061              :         }
    2062           92 :         return HCCL_SUCCESS;
    2063              :     }
    2064              : 
    2065            0 :     HcclResult HcclCommunicator::GetGroupRanksInfo(const std::vector<u32> &groupRanks, std::vector<RankInfo> &ranksInfo)
    2066              :     {
    2067            0 :         ranksInfo.clear();
    2068            0 :         std::vector<RankInfo> tmpRankInfoList;
    2069            0 :         tmpRankInfoList.assign(rankInfoList_.begin(), rankInfoList_.end());
    2070              : 
    2071            0 :         for (u32 index = 0; index < groupRanks.size(); index++) {
    2072            0 :             if (tmpRankInfoList.size() <= groupRanks[index]) {
    2073            0 :                 HCCL_ERROR("[Get][GroupRanksInfo]errNo[0x%016llx] groupRanks[%u]=[%u], >= rankinfolist size[%zu]",
    2074              :                            HCCL_ERROR_CODE(HCCL_E_PARA), index, groupRanks[index], tmpRankInfoList.size());
    2075            0 :                 return HCCL_E_PARA;
    2076              :             }
    2077            0 :             tmpRankInfoList[groupRanks[index]].userRank = index;
    2078            0 :             ranksInfo.push_back(tmpRankInfoList[groupRanks[index]]);
    2079            0 :             HCCL_DEBUG("index: %d userRank: %dhost ip: %s host port: %u dev phy id: %d serverIdx:%d",
    2080              :                        index,
    2081              :                        tmpRankInfoList[groupRanks[index]].userRank,
    2082              :                        tmpRankInfoList[groupRanks[index]].hostIp.GetReadableAddress(),
    2083              :                        tmpRankInfoList[groupRanks[index]].hostPort,
    2084              :                        tmpRankInfoList[groupRanks[index]].devicePhyId,
    2085              :                        tmpRankInfoList[groupRanks[index]].serverIdx);
    2086              :         }
    2087              : 
    2088              :         // 按rank id从小到大的顺序返回
    2089            0 :         std::sort(ranksInfo.begin(), ranksInfo.end(), CompareWithUserRank);
    2090              : 
    2091            0 :         for (u32 index = 0; index < ranksInfo.size(); ++index) {
    2092            0 :             if (index != ranksInfo[index].userRank) {
    2093            0 :                 HCCL_ERROR("[Get][GroupRanksInfo]errNo[0x%016llx] index[%u] !=  user rank[%u]",
    2094              :                            HCCL_ERROR_CODE(HCCL_E_PARA), index, ranksInfo[index].userRank);
    2095            0 :                 return HCCL_E_PARA;
    2096              :             }
    2097              :         }
    2098            0 :         return HCCL_SUCCESS;
    2099            0 :     }
    2100              : 
    2101            0 :     HcclResult HcclCommunicator::GetGroupCommonData(WorldGroupInfo &groupCommonData) const
    2102              :     {
    2103            0 :         groupCommonData.inlineReduceSwitchOn = inlineReduceSwitchOn_;
    2104            0 :         groupCommonData.deviceType = deviceType_;
    2105            0 :         groupCommonData.deviceLogicId = deviceLogicId_;
    2106            0 :         groupCommonData.profilingInitiated = profilingInitiated_;
    2107            0 :         groupCommonData.serverId = serverId_;
    2108            0 :         groupCommonData.phyIdNicInfoMap = rankDevicePhyIdNicInfoMap_;
    2109            0 :         groupCommonData.worldRankInfoList = rankInfoList_;
    2110            0 :         groupCommonData.ranksPort = nicRanksPort_;
    2111            0 :         groupCommonData.vnicRanksPort = vnicRanksPort_;
    2112            0 :         groupCommonData.useSuperPodMode = useSuperPodMode_;
    2113            0 :         groupCommonData.devPortSwitchOn = commPortConfig_.devPortSwitchOn;
    2114            0 :         return HCCL_SUCCESS;
    2115              :     }
    2116              : 
    2117           91 :     HcclResult HcclCommunicator::GetWorkspaceMemSize(const std::string &opType, u64 count, HcclDataType dataType,
    2118              :                                                      u32 &rankSize, u64 &memSize, DevType &deviceType) const
    2119              :     {
    2120           91 :         return workSpaceRes_->GetWorkspaceMemSize(opType, count, dataType, rankSize, memSize, deviceType);
    2121              :     }
    2122              : 
    2123           23 :     DeviceMem HcclCommunicator::GetWorkspaceScracthMem(const std::string &tag, u64 allocMemSize)
    2124              :     {
    2125           23 :         return workSpaceRes_->AllocDeviceMem(tag, allocMemSize);
    2126              :     }
    2127              : 
    2128           14 :     std::vector<Stream> HcclCommunicator::GetWorkspaceSubStreams(const std::string &tag, u32 num)
    2129              :     {
    2130           14 :         return workSpaceRes_->AllocSlaveStreams(tag, num);
    2131              :     }
    2132              : 
    2133          488 :     HcclResult HcclCommunicator::InitProfiling()
    2134              :     {
    2135          488 :         if (static_cast<s32>(devicePhyId_) == HOST_DEVICE_ID) {
    2136            0 :             HCCL_ERROR("[Init][Profiling]not support cpu rank");
    2137            0 :             return HCCL_E_NOT_SUPPORT;
    2138              :         }
    2139          488 :         CHK_PRT_RET(profilingInitiated_, HCCL_DEBUG("Profiling plugin has already been Initiated."), HCCL_SUCCESS);
    2140              : 
    2141          488 :         if (profilingMode_ != HcomProfilingMode::PROFILING_OPEN && GetExternalInputProfilingMode()) {
    2142            0 :             profilingMode_ = HcomProfilingMode::PROFILING_OPEN;
    2143            0 :             profilingOption_ = GetExternalInputProfilingOption();
    2144              :         }
    2145          488 :         HCCL_INFO("profiling config information:options[%s], mode[%d]", profilingOption_.c_str(), profilingMode_);
    2146              : 
    2147              :         // profilingInitiated_会广播给所有子通信域,用于避免taskInfoSaver的重复初始化
    2148          488 :         profilingInitiated_ = true;
    2149              :         // isExecuteProfilingInit_用于记录本impl是否执行了taskInfoSaver的初始化,用于进行对应的释放
    2150          488 :         isExecuteProfilingInit_ = true;
    2151          488 :         return HCCL_SUCCESS;
    2152              :     }
    2153              : 
    2154          488 :     HcclResult HcclCommunicator::DeinitProfiling()
    2155              :     {
    2156          488 :         CHK_PRT_RET(!profilingInitiated_, HCCL_DEBUG("Profiling plugin has not been Initiated"), HCCL_SUCCESS);
    2157          488 :         profilingInitiated_ = false;
    2158          488 :         HCCL_INFO("Profiling is deinitiated.");
    2159          488 :         return HCCL_SUCCESS;
    2160              :     }
    2161              : 
    2162          488 :     HcclResult HcclCommunicator::RegistTaskExceptionHandler() const
    2163              :     {
    2164          488 :         CHK_RET(TaskExceptionHandler::Init());
    2165          488 :         return HCCL_SUCCESS;
    2166              :     }
    2167              : 
    2168          804 :     HcclResult HcclCommunicator::UnRegistTaskExceptionHandler() const
    2169              :     {
    2170          804 :         CHK_RET(TaskExceptionHandler::DeInit());
    2171          804 :         return HCCL_SUCCESS;
    2172              :     }
    2173              : 
    2174            0 :     HcclResult HcclCommunicator::GetInCCLbuffer(void *&buffer, u64 &size)
    2175              :     {
    2176            0 :         return cclBufferManager_.GetInCCLbuffer(buffer, size);
    2177              :     }
    2178              : 
    2179            0 :     HcclResult HcclCommunicator::GetOutCCLbuffer(void *&buffer, u64 &size)
    2180              :     {
    2181            0 :         return cclBufferManager_.GetOutCCLbuffer(buffer, size);
    2182              :     }
    2183              : 
    2184            0 :     void HcclCommunicator::ReleaseCommCCLbuffer()
    2185              :     {
    2186            0 :         cclBufferManager_.ReleaseCommCCLbuffer();
    2187            0 :     }
    2188              : 
    2189            0 :     HcclResult HcclCommunicator::ReleaseCommInfos()
    2190              :     {
    2191            0 :         if (implAlg_ != nullptr) {
    2192            0 :             return implAlg_->ReleaseCommInfos();
    2193              :         }
    2194            0 :         return HCCL_SUCCESS;
    2195              :     }
    2196              : 
    2197          309 :     HcclResult HcclCommunicator::InitProfiler()
    2198              :     {
    2199          309 :         profilerManager_.reset(new (std::nothrow) ProfilerManager(devicePhyId_, deviceLogicId_, realUserRank_, userRankSize_));
    2200          309 :         CHK_SMART_PTR_NULL(profilerManager_);
    2201          309 :         HcclResult ret = profilerManager_->InitProfiler();
    2202          309 :         CHK_PRT_RET((ret != HCCL_SUCCESS), HCCL_ERROR("[BASE][InitProfiler]profilerManager_ InitProfiler failed."),
    2203              :                     HCCL_E_PARA);
    2204              : 
    2205          309 :         HCCL_INFO("[BASE][InitProfiler]Register CtrlCallBack success.");
    2206          309 :         return HCCL_SUCCESS;
    2207              :     }
    2208              : 
    2209           76 :     HcclResult HcclCommunicator::CreateCommCCLbuffer()
    2210              :     {
    2211              :         // user mem和CCL buffer互斥,不支持同时使用
    2212           76 :         if (isUserMemRegisted_) {
    2213            0 :             HCCL_ERROR("[HcclCommunicator][%s]tag[%s]The user mem has been registered, "
    2214              :                 "does not support create CCL Buffer.", __func__, identifier_.c_str());
    2215            0 :             return HCCL_E_NOT_SUPPORT;
    2216              :         }
    2217           76 :         return cclBufferManager_.CreateCommCCLbuffer(cclBuffName_);
    2218              :     }
    2219              : 
    2220          242 :     HcclResult HcclCommunicator::InitCCLbuffer(u64 inCCLbufferSize, u64 outCCLbufferSize)
    2221              :     {
    2222          242 :         return cclBufferManager_.InitCCLbuffer(inCCLbufferSize, outCCLbufferSize);
    2223              :     }
    2224              : 
    2225          180 :     u32 HcclCommunicator::GetLocalNicPort(NicType nicType)
    2226              :     {
    2227          180 :         u32 port = HCCL_INVALID_PORT;
    2228          180 :         if (nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST) {
    2229            0 :             return GetHostPort(devicePhyId_);
    2230              :         }
    2231              :         // isUseRankPort_在ranksPort初始化时一同配置:1. 异构场景 2. 开启device侧端口配置
    2232              :         // groupRanksPort_为空说明此时处于全局通信域,要从ranksPort_取监听端口;否则取groupRanksPort_
    2233          180 :         bool devicePortSwitchOn = commPortConfig_.devPortSwitchOn;
    2234          180 :         if (nicType == NicType::HOST_NIC_TYPE) {
    2235            0 :             port = GetHostPort(devicePhyId_);
    2236          180 :         } else if (devicePortSwitchOn && nicType == NicType::VNIC_TYPE) {
    2237              :             // vnic ports仅在开启device侧端口配置时单独配置
    2238            0 :             std::vector<u32> &ranksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    2239            0 :             port = GetNicPort(devicePhyId_, ranksPorts, userRank_, isUseRankPort_);
    2240            0 :         } else {
    2241              :             // 1. 开启device侧端口配置时的nic port时使用ranksPorts
    2242              :             // 2. 异构场景使用ranksPorts
    2243              :             // 3. 其余场景场景isUseRankPort_应当为false,使用默认port
    2244          180 :             std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    2245          180 :             port = GetNicPort(devicePhyId_, ranksPorts, userRank_, isUseRankPort_);
    2246              :         }
    2247          180 :         HCCL_INFO("[HcclCommunicator][GetLocalNicPort] nicType[%u], devicePortSwitchOn[%u], isUseRankPort[%u], "
    2248              :                   "get port[%u], devId[%u]",
    2249              :                   nicType, devicePortSwitchOn, isUseRankPort_, port, devicePhyId_);
    2250          180 :         return port;
    2251              :     }
    2252              : 
    2253            1 :     HcclResult HcclCommunicator::CheckOneSidedBackupAndSetDevId(u32 &backupDevPhyId, u32 &backupDevLogicId,
    2254              :         std::vector<HcclIpAddress> &localIpList, bool &isOneSidedTaskAndBackupInitA3)
    2255              :     {
    2256            1 :         if (!IsOneSidedIdentifier(identifier_)) {
    2257            1 :             isOneSidedTaskAndBackupInitA3 = false;
    2258            1 :             HCCL_INFO("[%s] comm[%s] is not one sided comm.", __func__, identifier_.c_str());
    2259            1 :             return HCCL_SUCCESS;
    2260              :         }
    2261            0 :         DevType deviceType = DevType::DEV_TYPE_COUNT;
    2262            0 :         CHK_RET(hrtGetDeviceType(deviceType));
    2263            0 :         if (deviceType != DevType::DEV_TYPE_910_93) {
    2264            0 :             isOneSidedTaskAndBackupInitA3 = false;
    2265            0 :             HCCL_INFO("[HcclCommunicator::CheckOneSidedBackupAndSetDevId] DeviceType[%d] is not 910_93, one sided backup not support",
    2266              :                 static_cast<u32>(deviceType));
    2267            0 :             return HCCL_SUCCESS;
    2268              :         }
    2269            0 :         CHK_RET(hrtGetPairDevicePhyId(devicePhyId_, backupDevPhyId));
    2270              : 
    2271            0 :         std::vector<HcclIpAddress> backupIpList;
    2272            0 :         std::vector<std::vector<HcclIpAddress>> chipDeviceIPs;
    2273            0 :         CHK_RET(hrtRaGetDeviceAllNicIP(chipDeviceIPs));
    2274            0 :         u32 ipIdex = 1U - (devicePhyId_ % 2U);
    2275            0 :         std::copy_if(chipDeviceIPs[ipIdex].begin(), chipDeviceIPs[ipIdex].end(),
    2276            0 :                     std::back_inserter(backupIpList), [](const HcclIpAddress& ip) { return !ip.IsIPv6(); });
    2277            0 :         HCCL_INFO("devicePhysicID[%u], backupDeviceId[%d], backupDeviceIP[0]:[%s], devIpAddr_[%s], ",
    2278              :             devicePhyId_, backupDevPhyId, backupIpList[0].GetReadableAddress(), devIpAddr_[0].GetReadableAddress());
    2279            0 :         CHK_RET(hrtRaGetDeviceIP(devicePhyId_, localIpList));
    2280            0 :         auto equalToLocal = [this](const HcclIpAddress &entry) { return entry == devIpAddr_[0];};
    2281            0 :         isOneSidedTaskAndBackupInitA3 = any_of(backupIpList.begin(), backupIpList.end(), equalToLocal) &&
    2282            0 :                                         !any_of(localIpList.begin(), localIpList.end(), equalToLocal);
    2283            0 :         if (isOneSidedTaskAndBackupInitA3) {
    2284            0 :             CHK_RET(hrtGetDeviceIndexByPhyId(backupDevPhyId, backupDevLogicId));
    2285              :         }
    2286              : 
    2287            0 :         HCCL_INFO("[HcclCommunicator::CheckOneSidedBackupAndSetDevId] isOneSidedTaskAndBackupInitA3[%s]",
    2288              :             isOneSidedTaskAndBackupInitA3 ? "true" : "false");
    2289            0 :         return HCCL_SUCCESS;
    2290            0 :     }
    2291              : 
    2292            0 :     HcclResult HcclCommunicator::OneSidedBackupInitNetResource(HcclNetDevCtx &nicPortBackUpCtx, u32 &backupDevPhyId,
    2293              :         u32 &backupDevLogicId, std::vector<HcclIpAddress> &localIpList)
    2294              :     {
    2295            0 :         devBackupIpAddr_[0] = devIpAddr_[0];
    2296            0 :         deviceBackUpPhyId_ = backupDevPhyId;
    2297            0 :         deviceBackUpLogicId_ = backupDevLogicId;
    2298            0 :         CHK_RET(HcclNetInit(NICDeployment::NIC_DEPLOYMENT_DEVICE, backupDevPhyId, backupDevLogicId, false, true));
    2299            0 :         HCCL_INFO("[HcclCommunicator::OneSidedBackupInitNetResource] OpenDev with backupDevPhyId[%d], backupDevLogicId[%d], localIpList[%s], backupIp[%s]",
    2300              :                         backupDevPhyId, backupDevLogicId, localIpList[0].GetReadableAddress(), devIpAddr_[0].GetReadableAddress());
    2301            0 :         CHK_RET(HcclNetOpenDev(&nicPortBackUpCtx, NicType::DEVICE_NIC_TYPE, backupDevPhyId, backupDevLogicId, devIpAddr_[0], localIpList[0]));
    2302            0 :         return HCCL_SUCCESS;
    2303              :     }
    2304              : 
    2305            0 :     HcclResult HcclCommunicator::OneSidedBackupServerInit(HcclNetDevCtx &nicPortBackUpCtx)
    2306              :     {
    2307            0 :         u32 backupPort = HCCL_INVALID_PORT;
    2308            0 :         for (const auto &rankInfo : rankInfoList_) {
    2309            0 :             if (rankInfo.userRank == userRank_) {
    2310            0 :                 backupPort = rankInfo.deviceNicPort;
    2311              :             }
    2312              :         }
    2313            0 :         CHK_RET(socketManager_->ServerInit(nicPortBackUpCtx, backupPort));
    2314            0 :         return HCCL_SUCCESS;
    2315              :     }
    2316              : 
    2317            0 :     HcclResult HcclCommunicator::InitDevicePrimaryNic(bool isMC2ReInit, bool isOneSidedTaskAndBackupInitA3)
    2318              :     {
    2319            0 :         std::shared_ptr<HcclSocket> &devNicSocket = commPortConfig_.devNicListen.first;
    2320            0 :         if (devNicSocket && !isOneSidedTaskAndBackupInitA3) {
    2321            0 :             HcclNetDevCtx &devNicCtx = commPortConfig_.devNicListen.second;
    2322            0 :             CHK_PTR_NULL(devNicCtx);
    2323            0 :             netDevCtxMap_.insert(std::make_pair(devNicSocket->GetLocalIp(), devNicCtx));
    2324            0 :             CHK_RET(socketManager_->ServerInit(devNicCtx, devNicSocket->GetLocalPort()));
    2325            0 :             commPortConfig_.devNicListen.second = nullptr;
    2326            0 :             HCCL_INFO("[HcclCommunicator][InitNic] init nic with listened socket success, "
    2327              :                       "listened ip[%s] port[%u]",
    2328              :                       devNicSocket->GetLocalIp().GetReadableAddress(), devNicSocket->GetLocalPort());
    2329            0 :         } else if (!isOneSidedTaskAndBackupInitA3) {
    2330            0 :             u32 port = GetLocalNicPort(NicType::DEVICE_NIC_TYPE);
    2331            0 :             u32 nicNum = devIpAddr_.size();
    2332            0 :             for (u32 i = 0; i < nicNum; i++) {
    2333            0 :                 if (devIpAddr_[i].IsInvalid()) {
    2334            0 :                     HCCL_INFO("[Init][Nic]nic num[%u] deviceip is invalid, total nicNum[%u]", i, nicNum);
    2335            0 :                     continue;
    2336              :                 }
    2337              :                 HcclNetDevCtx nicPortCtx;
    2338            0 :                 CHK_RET(HcclNetOpenDev(&nicPortCtx, NicType::DEVICE_NIC_TYPE, devicePhyId_, deviceLogicId_, devIpAddr_[i]));
    2339            0 :                 CHK_PTR_NULL(nicPortCtx);
    2340            0 :                 netDevCtxMap_.insert(std::make_pair(devIpAddr_[i], nicPortCtx));
    2341            0 :                 CHK_RET(socketManager_->ServerInit(nicPortCtx, port));
    2342            0 :                 HCCL_INFO("[HcclCommunicator][InitNic] init nic with ip[%s] port[%u] success",
    2343              :                           devIpAddr_[i].GetReadableAddress(), port);
    2344              :             }
    2345              :         }
    2346            0 :         return HCCL_SUCCESS;
    2347              :     }
    2348              : 
    2349            0 :     HcclResult HcclCommunicator::InitDeviceBackupNic(u32 backupDevPhyId, u32 backupDevLogicId,
    2350              :         std::vector<HcclIpAddress> &localIpList, bool isOneSidedTaskAndBackupInitA3)
    2351              :     {
    2352            0 :         if (IsEnableBackupLink() || isOneSidedTaskAndBackupInitA3) {
    2353            0 :             std::shared_ptr<HcclSocket> &backupNicSocket = commPortConfig_.backupDevNicListen.first;
    2354            0 :             if (backupNicSocket) {
    2355            0 :                 HcclNetDevCtx &backupNicCtx = commPortConfig_.backupDevNicListen.second;
    2356            0 :                 CHK_PTR_NULL(backupNicCtx);
    2357            0 :                 netDevCtxMap_.insert(std::make_pair(backupNicSocket->GetLocalIp(), backupNicCtx));
    2358            0 :                 CHK_RET(socketManager_->ServerInit(backupNicCtx, backupNicSocket->GetLocalPort()));
    2359            0 :                 commPortConfig_.backupDevNicListen.second = nullptr;
    2360            0 :                 HCCL_INFO("[HcclCommunicator][InitNic] init backup nic with listened socket success, "
    2361              :                           "listened ip[%s] port[%u]",
    2362              :                           backupNicSocket->GetLocalIp().GetReadableAddress(), backupNicSocket->GetLocalPort());
    2363              :             } else {
    2364              :                 HcclNetDevCtx nicPortBackUpCtx;
    2365            0 :                 if (isOneSidedTaskAndBackupInitA3) {
    2366            0 :                     CHK_RET(OneSidedBackupInitNetResource(nicPortBackUpCtx, backupDevPhyId, backupDevLogicId, localIpList));
    2367              :                 } else {
    2368            0 :                     CHK_RET(HcclNetOpenDev(&nicPortBackUpCtx, NicType::DEVICE_NIC_TYPE, deviceBackUpPhyId_,
    2369              :                                         deviceBackUpLogicId_, devBackupIpAddr_[0], devIpAddr_[0]));
    2370              :                 }
    2371            0 :                 CHK_PTR_NULL(nicPortBackUpCtx);
    2372            0 :                 netDevCtxMap_.insert(std::make_pair(devBackupIpAddr_[0], nicPortBackUpCtx));
    2373            0 :                 if (isOneSidedTaskAndBackupInitA3) {
    2374            0 :                     CHK_RET(OneSidedBackupServerInit(nicPortBackUpCtx));
    2375              :                 } else {
    2376            0 :                     CHK_RET(socketManager_->ServerInit(nicPortBackUpCtx, devBackupPort_));
    2377              :                 }
    2378            0 :                 HCCL_DEBUG("[%s]finish backup ServerInit, deviceBackUpPhyId_[%u], deviceBackUpLogicId_[%u], "
    2379              :                            "devBackupIpAddr_[%s], devBackupPort_[%u], nicDeployment_[%d], IsEnableBackupLink[%d], "
    2380              :                            "netDevCtxMap_.size[%d]",
    2381              :                            __func__, deviceBackUpPhyId_, deviceBackUpLogicId_, devBackupIpAddr_[0].GetReadableAddress(),
    2382              :                            devBackupPort_, nicDeployment_, IsEnableBackupLink(), netDevCtxMap_.size());
    2383            0 :                 HCCL_INFO("[HcclCommunicator][InitNic] init backup nic with ip[%s] port[%u] success",
    2384              :                           devBackupIpAddr_[0].GetReadableAddress(), devBackupPort_);
    2385              :             }
    2386              :         }
    2387            0 :         return HCCL_SUCCESS;
    2388              :     }
    2389              : 
    2390            0 :     HcclResult HcclCommunicator::InitNicDeviceDeploy(bool isMC2ReInit, u32 backupDevPhyId, u32 backupDevLogicId,
    2391              :         std::vector<HcclIpAddress> &localIpList, bool isOneSidedTaskAndBackupInitA3)
    2392              :     {
    2393            0 :         CHK_RET(InitDevicePrimaryNic(isMC2ReInit, isOneSidedTaskAndBackupInitA3));
    2394            0 :         attrCollector_.GenUsedRdmaLevel0();
    2395            0 :         isUsedRdmaLevel0_ = attrCollector_.GetUsedRdmaLevel0();
    2396            0 :         CHK_RET(InitDeviceBackupNic(backupDevPhyId, backupDevLogicId, localIpList, isOneSidedTaskAndBackupInitA3));
    2397            0 :         return HCCL_SUCCESS;
    2398              :     }
    2399              : 
    2400            0 :     HcclResult HcclCommunicator::InitNicHostDeploy()
    2401              :     {
    2402            0 :         u32 port = GetLocalNicPort(NicType::HOST_NIC_TYPE);
    2403            0 :         CHK_PRT_RET((hostIp_.IsInvalid()), HCCL_ERROR("[Init][Nic] host ip is invalid when NIC "
    2404              :                                                       "deployment is host. "),
    2405              :                     HCCL_E_PARA);
    2406            0 :         attrCollector_.GenUsedRdmaLevel0();
    2407            0 :         isUsedRdmaLevel0_ = attrCollector_.GetUsedRdmaLevel0();
    2408            0 :         u32 devicePhyID = (static_cast<s32>(devicePhyId_) == HOST_DEVICE_ID) ? 0 : devicePhyId_;
    2409              : 
    2410            0 :         u32 i = 0;
    2411              :         HcclNetDevCtx nicPortCtx;
    2412            0 :         for (i = 0; i < devIpAddr_.size(); i++) {
    2413            0 :             if (devIpAddr_[i].IsInvalid()) {
    2414            0 :                 HCCL_INFO("[Init][Nic]nic num[%u] deviceip is invalid, total nicNum[%u]", i, devIpAddr_.size());
    2415            0 :                 continue;
    2416              :             }
    2417            0 :             std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    2418            0 :             port = GetNicPort(devicePhyId_, ranksPorts, userRank_, isUseRankPort_);
    2419            0 :             CHK_RET(HcclNetOpenDev(&nicPortCtx, NicType::HOST_NIC_TYPE, devicePhyId_, deviceLogicId_, devIpAddr_[i]));
    2420            0 :             CHK_PTR_NULL(nicPortCtx);
    2421            0 :             netDevCtxMap_.insert(std::make_pair(devIpAddr_[i], nicPortCtx));
    2422            0 :             HcclNetDevSetProtoType(nicPortCtx, HCCL_PROTO_TYPE_ROCE);
    2423            0 :             break;
    2424              :         }
    2425              : 
    2426            0 :         if (i == devIpAddr_.size()) {
    2427            0 :             port = GetLocalNicPort(NicType::HOST_NIC_TYPE);
    2428            0 :             CHK_RET(HcclNetOpenDev(&nicPortCtx, NicType::HOST_NIC_TYPE, devicePhyId_, deviceLogicId_, hostIp_));
    2429            0 :             CHK_PTR_NULL(nicPortCtx);
    2430            0 :             netDevCtxMap_.insert(std::make_pair(hostIp_, nicPortCtx));
    2431              :         }
    2432            0 :         HCCL_INFO("[Init][Nic], hostPort[%u], devicePhyID[%u]", port, devicePhyID);
    2433            0 :         CHK_RET(socketManager_->ServerInit(nicPortCtx, port));
    2434            0 :         return HCCL_SUCCESS;
    2435              :     }
    2436              : 
    2437            1 :     HcclResult HcclCommunicator::InitNic(bool isMC2ReInit)
    2438              :     {
    2439            1 :         if (!GetExternalInputIntraRoceSwitch() && servRankInfo_.size() == 1 && isDiffDeviceModule_ && !isMC2ReInit) {
    2440            0 :             return HCCL_SUCCESS;
    2441              :         }
    2442            1 :         u32 backupDevPhyId = INVALID_INT;
    2443            1 :         u32 backupDevLogicId = INVALID_INT;
    2444            1 :         bool isOneSidedTaskAndBackupInitA3 = false;
    2445            1 :         vector<HcclIpAddress> localIpList;
    2446            1 :         CHK_RET(CheckOneSidedBackupAndSetDevId(backupDevPhyId, backupDevLogicId, localIpList, isOneSidedTaskAndBackupInitA3));
    2447              : 
    2448            1 :         if (nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_DEVICE) {
    2449            0 :             CHK_RET(InitNicDeviceDeploy(isMC2ReInit, backupDevPhyId, backupDevLogicId, localIpList, isOneSidedTaskAndBackupInitA3));
    2450            1 :         } else if (nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST) {
    2451            0 :             CHK_RET(InitNicHostDeploy());
    2452              :         } else {
    2453            1 :             HCCL_ERROR("[Init][Nic]nic deployment[%d] is not supported", nicDeployment_);
    2454            1 :             return HCCL_E_PARA;
    2455              :         }
    2456            0 :         isNeedInitNic_ = true;
    2457            0 :         attrCollector_.SetNeedInitNicFlag(isNeedInitNic_);
    2458            0 :         nicInitialized_++;
    2459            0 :         return HCCL_SUCCESS;
    2460            1 :     }
    2461              : 
    2462            0 :     HcclResult HcclCommunicator::DeinitNicHostDeploy()
    2463              :     {
    2464            0 :         u32 port = GetLocalNicPort(NicType::HOST_NIC_TYPE);
    2465            0 :         CHK_PRT_RET((hostIp_.IsInvalid()), HCCL_ERROR("[DeInit][Nic] host ip is invalid when NIC "
    2466              :                                                       "deployment is host. "),
    2467              :                     HCCL_E_PARA);
    2468              : 
    2469            0 :         u32 i = 0;
    2470            0 :         for (i = 0; i < devIpAddr_.size(); i++) {
    2471            0 :             if (devIpAddr_[i].IsInvalid()) {
    2472            0 :                 HCCL_INFO("[Init][Nic]nic num[%u] deviceip is invalid, total nicNum[%u]", i, devIpAddr_.size());
    2473            0 :                 continue;
    2474              :             }
    2475            0 :             std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    2476            0 :             port = GetNicPort(devicePhyId_, ranksPorts, userRank_, isUseRankPort_);
    2477              : 
    2478            0 :             CHK_RET(socketManager_->ServerDeInit(netDevCtxMap_[devIpAddr_[i]], port));
    2479            0 :             HcclNetCloseDev(netDevCtxMap_[devIpAddr_[i]]);
    2480            0 :             netDevCtxMap_.erase(devIpAddr_[i]);
    2481              : 
    2482            0 :             break;
    2483              :         }
    2484              : 
    2485            0 :         if (i == devIpAddr_.size()) {
    2486            0 :             CHK_RET(socketManager_->ServerDeInit(netDevCtxMap_[hostIp_], port));
    2487            0 :             HcclNetCloseDev(netDevCtxMap_[hostIp_]);
    2488            0 :             netDevCtxMap_.erase(hostIp_);
    2489              :         }
    2490            0 :         return HCCL_SUCCESS;
    2491              :     }
    2492              : 
    2493            0 :     HcclResult HcclCommunicator::DeinitNic()
    2494              :     {
    2495            0 :         if (nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_DEVICE) {
    2496            0 :             u32 port = GetLocalNicPort(NicType::DEVICE_NIC_TYPE);
    2497            0 :             u32 nicNum = devIpAddr_.size();
    2498            0 :             for (u32 i = 0; i < nicNum; i++) {
    2499            0 :                 if (devIpAddr_[i].IsInvalid()) {
    2500            0 :                     HCCL_INFO("continue invalid devIp %s", devIpAddr_[i].GetReadableAddress());
    2501            0 :                     continue;
    2502              :                 }
    2503            0 :                 if (netDevCtxMap_.find(devIpAddr_[i]) == netDevCtxMap_.end()) {
    2504            0 :                     HCCL_INFO("devIp[%s] not found in netDevCtxMap_", devIpAddr_[i].GetReadableAddress());
    2505            0 :                     continue;
    2506              :                 }
    2507            0 :                 CHK_RET(socketManager_->ServerDeInit(netDevCtxMap_[devIpAddr_[i]], port));
    2508              :                 // 最后一次调用才删除netCtx
    2509            0 :                 if (nicInitialized_ - 1 <= 0) {
    2510            0 :                     HcclNetCloseDev(netDevCtxMap_[devIpAddr_[i]]);
    2511            0 :                     netDevCtxMap_.erase(devIpAddr_[i]);
    2512              :                 }
    2513              :             }
    2514            0 :             if (IsEnableBackupLink() && netDevCtxMap_.find(devBackupIpAddr_[0]) != netDevCtxMap_.end()) {
    2515              :                 // 超节点 && level2支持重执行 && Aicpu -> 备用网卡 deinit
    2516            0 :                 CHK_RET(socketManager_->ServerDeInit(netDevCtxMap_[devBackupIpAddr_[0]], devBackupPort_));
    2517            0 :                 if (nicInitialized_ - 1 <= 0) {
    2518            0 :                     HcclNetCloseDev(netDevCtxMap_[devBackupIpAddr_[0]]);
    2519            0 :                     netDevCtxMap_.erase(devBackupIpAddr_[0]);
    2520            0 :                     HCCL_DEBUG("[%s]finish backup ServerDeInit devBackupIpAddr_[%s], port[%u], IsEnableBackupLink[%d]",
    2521              :                                __func__, devBackupIpAddr_[0].GetReadableAddress(), devBackupPort_, IsEnableBackupLink());
    2522              :                 }
    2523              :             }
    2524            0 :         } else if (nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST) {
    2525            0 :             CHK_RET(DeinitNicHostDeploy());
    2526              :         } else {
    2527            0 :             HCCL_ERROR("[Deinit][Nic]nic deployment[%d] is not supported", nicDeployment_);
    2528            0 :             return HCCL_E_PARA;
    2529              :         }
    2530            0 :         nicInitialized_--;
    2531            0 :         return HCCL_SUCCESS;
    2532              :     }
    2533              : 
    2534          519 :     HcclResult HcclCommunicator::RegisterRanksToDca()
    2535              :     {
    2536          519 :         if (deviceType_ != DevType::DEV_TYPE_910_93 && deviceType_ != DevType::DEV_TYPE_910B) {
    2537          324 :             HCCL_WARNING("[RegisterRanksToDca] not support deviceType[%d]", deviceType_);
    2538          324 :             return HCCL_SUCCESS;
    2539              :         }
    2540          195 :         CHK_RET(setVnicIpToRankInfoList());
    2541          196 :         DetectConnectionAnomalies::GetInstance(deviceLogicId_).Init(rankInfoList_, isNeedInitNic_);
    2542          193 :         return HCCL_SUCCESS;
    2543              :     }
    2544              : 
    2545            0 :     HcclResult HcclCommunicator::AddOpInfoToHeartBeat(const OpInfoDesc &opInfo, const std::string &tag)
    2546              :     {
    2547            0 :         if (Is310PDevice() || deviceType_ == DevType::DEV_TYPE_310P3 ||
    2548            0 :             GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB) {
    2549            0 :             return HCCL_SUCCESS;
    2550              :         }
    2551            0 :         return Heartbeat::GetInstance(deviceLogicId_).AddOpInfoToHeartBeat(identifier_, opInfo, tag);
    2552              :     }
    2553              : 
    2554          803 :     void HcclCommunicator::DeleteOpInfoToHeartBeat()
    2555              :     {
    2556         1602 :         if (Is310PDevice() || deviceType_ == DevType::DEV_TYPE_310P3 ||
    2557          797 :             GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB) {
    2558          162 :             return ;
    2559              :         }
    2560          641 :         for (const auto &tag : hbSendRecvTags_) {
    2561            0 :             Heartbeat::GetInstance(deviceLogicId_).DeleteOpInfoToHeartBeat(identifier_, tag);
    2562              :         }
    2563         1924 :         Heartbeat::GetInstance(deviceLogicId_).DeleteOpInfoToHeartBeat(identifier_, "");
    2564              :     }
    2565              : 
    2566           21 :     HcclResult HcclCommunicator::RegisterToHeartBeat()
    2567              :     {
    2568           21 :         if (Is310PDevice() || deviceType_ == DevType::DEV_TYPE_310P3) {
    2569            0 :             return HCCL_SUCCESS;
    2570              :         }
    2571           21 :         u32 localPort = commPortConfig_.devPortSwitchOn ? HCCL_INVALID_PORT : GetLocalNicPort(NicType::DEVICE_NIC_TYPE);
    2572           21 :         return Heartbeat::GetInstance(deviceLogicId_).RegisterToHeartBeat(userRank_, deviceType_, rankInfoList_, localPort, isNeedInitNic_, identifier_,useSuperPodMode_, isUsedRdmaLevel0_, retryEnable_, IsEnableBackupLink());
    2573              :     }
    2574              : 
    2575            0 :     HcclResult HcclCommunicator::RegisterToHeartBeat(u32 peerRankId, string &tag)
    2576              :     {
    2577            0 :         u32 localPort = commPortConfig_.devPortSwitchOn ? HCCL_INVALID_PORT : GetLocalNicPort(NicType::DEVICE_NIC_TYPE);
    2578            0 :         return Heartbeat::GetInstance(deviceLogicId_).RegisterToHeartBeat(userRank_, deviceType_, rankInfoList_, localPort, isNeedInitNic_, peerRankId, identifier_, tag, useSuperPodMode_, isUsedRdmaLevel0_, retryEnable_, IsEnableBackupLink());
    2579              :     }
    2580              : 
    2581          803 :     void HcclCommunicator::UnRegisterToHeartBeat()
    2582              :     {
    2583          803 :         for (auto tag : hbSendRecvTags_) {
    2584            0 :             Heartbeat::GetInstance(deviceLogicId_).UnRegisterToHeartBeat(deviceType_, identifier_, tag);
    2585            0 :         }
    2586          804 :         Heartbeat::GetInstance(deviceLogicId_).UnRegisterToHeartBeat(deviceType_, identifier_);
    2587          801 :     }
    2588              : 
    2589          804 :     void HcclCommunicator::UnRegisterToCommConfiger()
    2590              :     {
    2591          804 :         CommConfiger::GetInstance().UnRegisterToCommConfiger(identifier_);
    2592          804 :     }
    2593              : 
    2594           92 :     HcclResult HcclCommunicator::SetGlobalWorkSpace(std::vector<void *> &globalWorkSpaceAddr)
    2595              :     {
    2596           92 :         CHK_RET(HcclSetGlobalWorkSpace(dispatcher_, globalWorkSpaceAddr));
    2597           92 :         return HCCL_SUCCESS;
    2598              :     }
    2599              : 
    2600            0 :     HcclResult HcclCommunicator::GetandClearOverFlowTasks(std::vector<HcclDumpInfo> &hcclDumpInfo)
    2601              :     {
    2602            0 :         if (profilerManager_ != nullptr) {
    2603            0 :             CHK_RET(profilerManager_->GetandClearOverFlowTasks(hcclDumpInfo));
    2604              :         } else {
    2605            0 :             HCCL_WARNING("[impl][GetDumpTask] profilerManager_ not set");
    2606              :         }
    2607            0 :         return HCCL_SUCCESS;
    2608              :     }
    2609              : 
    2610          235 :     HcclResult HcclCommunicator::GetDeviceId(s32 &deviceId) const
    2611              :     {
    2612          235 :         deviceId = deviceLogicId_;
    2613          235 :         return HCCL_SUCCESS;
    2614              :     }
    2615              : 
    2616            1 :     HcclResult HcclCommunicator::GetCqeError(HcclResult &result)
    2617              :     {
    2618            1 :         CHK_RET(Heartbeat::GetInstance(deviceLogicId_).CheckErrorCqe(identifier_, result));
    2619            1 :         return HCCL_SUCCESS;
    2620              :     }
    2621              : 
    2622            0 :     HcclResult HcclCommunicator::GetOpInconsistentError(HcclResult &result)
    2623              :     {
    2624            0 :         CHK_RET(Heartbeat::GetInstance(deviceLogicId_).CheckOpInconsistentError(identifier_, result));
    2625            0 :         return HCCL_SUCCESS;
    2626              :     }
    2627              : 
    2628            0 :     HcclResult HcclCommunicator::SupportDeterministicOptim(bool &isDeterministicOptim)
    2629              :     {
    2630            0 :         CHK_SMART_PTR_NULL(implAlg_);
    2631            0 :         CHK_RET(implAlg_->SupportDeterministicOptim(isDeterministicOptim));
    2632            0 :         return HCCL_SUCCESS;
    2633              :     }
    2634              : 
    2635            0 :     HcclResult HcclCommunicator::GetHccsLinkNum(u32 &numHccsLink)
    2636              :     {
    2637            0 :         auto iter = pairLinkInfo_.find(static_cast<u32>(LinkTypeInServer::HCCS_TYPE));
    2638            0 :         if (iter == pairLinkInfo_.end()) {
    2639            0 :             HCCL_ERROR("[HcclCommunicator][GetHccsLinkNum]HCCS_TYPE is not found");
    2640            0 :             return HCCL_E_PARA;
    2641              :         }
    2642            0 :         numHccsLink = iter->second.size();
    2643            0 :         return HCCL_SUCCESS;
    2644              :     }
    2645              : 
    2646            4 :     HcclResult HcclCommunicator::AllGather(const std::string &tag, void *inputPtr, void *outputPtr, u64 inputCount,
    2647              :                                            HcclDataType dataType, HcclRtStream stream, HcomCollOpInfo *opInfo)
    2648              :     {
    2649            4 :         bool isCapture = StreamIsCapture(stream);
    2650              : 
    2651            9 :         bool aicpuUnfoldMode = false;
    2652            9 :         if (EnableAicpuUnfold(isCapture) && (userRankSize_ != 1)) {
    2653            0 :             aicpuUnfoldMode = true;
    2654              :         }
    2655              : 
    2656            5 :         if (!IsAtomicInit()) {
    2657            0 :             HCCL_ERROR("[HcclCommunicator][AllGather]errNo[0x%016llx] hccl init must be called before call this function",
    2658              :                        HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    2659            0 :             return HCCL_E_UNAVAIL;
    2660              :         }
    2661              : 
    2662           11 :         Stream streamObj(stream);
    2663           11 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    2664              : 
    2665            9 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    2666           10 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    2667            7 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    2668            7 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    2669              : 
    2670            7 :         u32 perDataSize = SIZE_TABLE[dataType];
    2671            7 :         u64 totalSize = inputCount * perDataSize;
    2672              : 
    2673            7 :         OpParam opParam;
    2674            8 :         opParam.tag = tag;
    2675           11 :         opParam.inputPtr = inputPtr;
    2676           11 :         opParam.inputSize = totalSize;
    2677           11 :         opParam.outputPtr = outputPtr;
    2678           11 :         opParam.outputSize = totalSize * userRankSize_;
    2679           11 :         opParam.DataDes.count = inputCount;
    2680           11 :         opParam.DataDes.dataType = dataType;
    2681           11 :         opParam.reduceType = HcclReduceOp::HCCL_REDUCE_RESERVED;
    2682           11 :         opParam.stream = streamObj;
    2683            8 :         opParam.syncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
    2684            8 :         opParam.isCapture = isCapture;
    2685            8 :         opParam.aicpuUnfoldMode = aicpuUnfoldMode;
    2686            8 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    2687            8 :         opParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    2688              : 
    2689            8 :         CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_ALLGATHER, opParam));
    2690              : 
    2691           11 :         return HCCL_SUCCESS;
    2692           11 :     }
    2693              : 
    2694            1 :     HcclResult HcclCommunicator::AllGatherV(const std::string &tag, const void *sendBuf, u64 sendCount, const void *recvBuf,
    2695              :                                             const void *recvCounts, const void *rdispls, HcclDataType dataType, HcclRtStream stream)
    2696              :     {
    2697            1 :         bool aicpuUnfoldMode = false;
    2698              : 
    2699            1 :         if (GetAicpuUnfoldConfig() && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
    2700            0 :             aicpuUnfoldMode = true;
    2701              :         }
    2702              : 
    2703            1 :         if (!IsAtomicInit()) {
    2704            0 :             HCCL_ERROR("[HcclCommunicator][AllGatherV]errNo[0x%016llx] hccl init must be called before call this function",
    2705              :                        HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    2706            0 :             return HCCL_E_UNAVAIL;
    2707              :         }
    2708              : 
    2709            1 :         Stream streamObj(stream);
    2710            1 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    2711              : 
    2712            1 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    2713            1 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    2714            1 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    2715            1 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    2716              : 
    2717            1 :         u32 perDataSize = SIZE_TABLE[dataType];
    2718            1 :         u64 totalSize = sendCount * perDataSize;
    2719              : 
    2720            1 :         u64 outputSize = 0;
    2721            1 :         const u64 *counts = static_cast<const u64 *>(recvCounts);
    2722            3 :         for (u32 i = 0; i < userRankSize_; i++) {
    2723            2 :             outputSize += counts[i] * perDataSize;
    2724              :         }
    2725              : 
    2726            1 :         bool isCapture = StreamIsCapture(stream);
    2727              : 
    2728            1 :         OpParam opParam;
    2729            1 :         opParam.tag = tag;
    2730            1 :         opParam.inputPtr = const_cast<void *>(sendBuf);
    2731            1 :         opParam.inputSize = totalSize;
    2732            1 :         opParam.outputPtr = const_cast<void *>(recvBuf);
    2733            1 :         opParam.outputSize = outputSize;
    2734            1 :         opParam.VDataDes.dataType = dataType;
    2735            1 :         opParam.VDataDes.counts = const_cast<void *>(recvCounts);
    2736            1 :         opParam.VDataDes.displs = const_cast<void *>(rdispls);
    2737            1 :         opParam.reduceType = HcclReduceOp::HCCL_REDUCE_RESERVED;
    2738            1 :         opParam.stream = streamObj;
    2739            1 :         opParam.syncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
    2740            1 :         opParam.aicpuUnfoldMode = aicpuUnfoldMode;
    2741            1 :         opParam.isCapture = isCapture;
    2742            1 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    2743            1 :         opParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
    2744              : 
    2745            1 :         if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
    2746            0 :             for (u32 i = 0; i < userRankSize_; i++) {
    2747            0 :                 HCCL_CONFIG_DEBUG(HCCL_ALG,
    2748              :                                   "[HcclCommunicator][AllGatherV]userRank_[%u], rankIdx[%u], recvCounts[%llu], rdispls[%llu]",
    2749              :                                   userRank_, i, counts[i], static_cast<const u64 *>(rdispls)[i]);
    2750              :             }
    2751              :         }
    2752              : 
    2753            1 :         CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_ALLGATHER_V, opParam));
    2754              : 
    2755            1 :         return HCCL_SUCCESS;
    2756            1 :     }
    2757              : 
    2758            0 :     HcclResult HcclCommunicator::AicpuUnfold(const std::string &tag, void *inputPtr, void *outputPtr, u64 count,
    2759              :                                              HcclDataType dataType, HcclReduceOp op, HcclRtStream stream, HcclCMDType cmdType)
    2760              :     {
    2761            0 :         Stream streamObj(stream);
    2762            0 :         u32 perDataSize = SIZE_TABLE[dataType];
    2763            0 :         u64 totalSize = count * perDataSize;
    2764            0 :         bool isCapture = StreamIsCapture(stream);
    2765            0 :         OpParam opParam;
    2766            0 :         opParam.tag = tag;
    2767            0 :         opParam.inputPtr = inputPtr;
    2768            0 :         opParam.inputSize = totalSize;
    2769            0 :         opParam.outputPtr = outputPtr;
    2770            0 :         opParam.outputSize = totalSize;
    2771            0 :         opParam.DataDes.count = count;
    2772            0 :         opParam.DataDes.dataType = dataType;
    2773            0 :         opParam.reduceType = op;
    2774            0 :         opParam.stream = streamObj;
    2775            0 :         opParam.isCapture = isCapture;
    2776            0 :         opParam.syncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
    2777            0 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    2778            0 :         AlgType algType;
    2779            0 :         algType.algoLevel0 = AlgTypeLevel0::ALG_LEVEL0_NP_MESH;
    2780            0 :         algType.algoLevel1 = AlgTypeLevel1::ALG_LEVEL1_RING;
    2781              : 
    2782              :         // 构造空vector用于入参,无实际意义
    2783            0 :         const std::vector<Stream> slaveStreams;
    2784            0 :         CHK_RET(RegisterDfxInfo(opParam, algType, slaveStreams));
    2785            0 :         HcclResult ret = HCCL_SUCCESS;
    2786            0 :         if (!IsExistCommRes(identifier_)) {
    2787            0 :             HCCL_INFO("[AicpuUnfold] tag[%s] count[%llu] dataType[%s] op[%s].", identifier_.c_str(),
    2788              :                       count, GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str());
    2789            0 :             uint64_t streamMode = 0;
    2790            0 :             CHK_RET(hrtStreamGetMode(stream, &streamMode));
    2791              : 
    2792              :             rtStream_t aicpuStream;
    2793            0 :             ret = Mc2AiCpuStreamAllocAndGet(streamMode, aicpuStream);
    2794            0 :             void *commContext = nullptr;
    2795            0 :             ret = CreateCommResource(identifier_, stream, true, &commContext);
    2796            0 :             if (ret != HCCL_SUCCESS) {
    2797            0 :                 HCCL_ERROR("[hcclImpl][CreateComm]create aicpu unfold comminfo by tag[%s] failed. return[%d]",
    2798              :                            identifier_.c_str(), ret);
    2799            0 :                 return ret;
    2800              :             }
    2801              :         }
    2802              : 
    2803            0 :         std::string kernelName = "RunAicpuRpcSrvLaunch";
    2804            0 :         AicpuOpTiling opTilingInfo;
    2805            0 :         ret = AicpuKfcTilingDataLaunch(opParam, cmdType, commContext_, kernelName, opTilingInfo);
    2806            0 :         if (ret != HCCL_SUCCESS) {
    2807            0 :             HCCL_ERROR("[hcclImpl][TilingData]aicpu unfold tiling data launch failed. return[%d] inputPtr[%p]"
    2808              :                        "outputPtr[%p] count[%llu] dataType[%s] op[%s]",
    2809              :                        ret, inputPtr, outputPtr, count,
    2810              :                        GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str());
    2811            0 :             return ret;
    2812              :         }
    2813            0 :         CHK_RET(UnRegisterDfxInfo(opParam, slaveStreams));
    2814            0 :         return HCCL_SUCCESS;
    2815            0 :     }
    2816              : 
    2817            6 :     HcclResult HcclCommunicator::AllGatherOutPlace(const std::string &tag, void *inputPtr, void *outputPtr,
    2818              :                                                    u64 inputCount, HcclDataType dataType, HcclRtStream stream)
    2819              :     {
    2820            6 :         CHK_RET(CheckSuspendingStatus());
    2821            4 :         if (!IsAtomicInit()) {
    2822            0 :             HCCL_ERROR(
    2823              :                 "[HcclCommunicator][AllGatherOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
    2824              :                 HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    2825            0 :             return HCCL_E_UNAVAIL;
    2826              :         }
    2827              : 
    2828            8 :         bool isCapture = StreamIsCapture(stream);
    2829              : 
    2830            8 :         bool aicpuUnfoldMode = false;
    2831            8 :         if (EnableAicpuUnfold(isCapture) && (userRankSize_ != 1)) {
    2832            4 :             aicpuUnfoldMode = true;
    2833              :         }
    2834              : 
    2835            8 :         Stream streamObj(stream);
    2836            7 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    2837              : 
    2838            5 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    2839            5 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    2840            3 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    2841            3 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    2842              : 
    2843            5 :         u32 perDataSize = SIZE_TABLE[dataType];
    2844            5 :         u64 totalSize = inputCount * perDataSize * userRankSize_;
    2845              : 
    2846            5 :         OpParam opParam;
    2847            6 :         opParam.tag = tag;
    2848            8 :         opParam.inputPtr = inputPtr;
    2849            8 :         opParam.inputSize = inputCount * perDataSize;
    2850            8 :         opParam.outputPtr = outputPtr;
    2851            8 :         opParam.outputSize = totalSize;
    2852            8 :         opParam.DataDes.count = inputCount;
    2853            8 :         opParam.DataDes.dataType = dataType;
    2854            8 :         opParam.reduceType = HcclReduceOp::HCCL_REDUCE_RESERVED;
    2855            8 :         opParam.stream = streamObj;
    2856            6 :         opParam.syncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
    2857            6 :         opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
    2858            6 :         opParam.aicpuUnfoldMode = aicpuUnfoldMode;
    2859            6 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    2860            6 :         opParam.isCapture = isCapture;
    2861            6 :         opParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    2862              : 
    2863            6 :         CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_ALLGATHER, opParam));
    2864              : 
    2865            0 :         return HCCL_SUCCESS;
    2866            8 :     }
    2867              : 
    2868            1 :     HcclResult HcclCommunicator::AllGatherVOutPlace(const std::string &tag, void *inputPtr, void *outputPtr,
    2869              :                                                     u64 inputCount, const void *outputCounts, const void *outputDispls, HcclDataType dataType, HcclRtStream stream)
    2870              :     {
    2871            1 :         CHK_RET(CheckSuspendingStatus());
    2872            1 :         if (userRankSize_ == 1) {
    2873              :             // rankSize为1时,退化为AllGather
    2874            0 :             return AllGatherOutPlace(tag, inputPtr, outputPtr, inputCount, dataType, stream);
    2875              :         }
    2876              : 
    2877            1 :         if (!IsAtomicInit()) {
    2878            0 :             HCCL_ERROR(
    2879              :                 "[HcclCommunicator][AllGatherVOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
    2880              :                 HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    2881            0 :             return HCCL_E_UNAVAIL;
    2882              :         }
    2883              : 
    2884            1 :         bool aicpuUnfoldMode = false;
    2885            1 :         if (GetAicpuUnfoldConfig() && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
    2886            0 :             aicpuUnfoldMode = true;
    2887              :         }
    2888              : 
    2889            1 :         bool isCapture = StreamIsCapture(stream);
    2890              : 
    2891            1 :         Stream streamObj(stream);
    2892            1 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    2893              : 
    2894            1 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    2895            1 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    2896            1 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    2897            1 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    2898              : 
    2899            1 :         u32 perDataSize = SIZE_TABLE[dataType];
    2900            1 :         u64 outputSize = 0;
    2901            1 :         const u64 *counts = static_cast<const u64 *>(outputCounts);
    2902            3 :         for (u32 i = 0; i < userRankSize_; i++) {
    2903            2 :             outputSize += counts[i] * perDataSize;
    2904              :         }
    2905              : 
    2906            1 :         OpParam opParam;
    2907            1 :         opParam.tag = tag;
    2908            1 :         opParam.inputPtr = inputPtr;
    2909            1 :         opParam.inputSize = inputCount * perDataSize;
    2910            1 :         opParam.outputPtr = outputPtr;
    2911            1 :         opParam.outputSize = outputSize;
    2912            1 :         opParam.VDataDes.counts = const_cast<void *>(outputCounts);
    2913            1 :         opParam.VDataDes.displs = const_cast<void *>(outputDispls);
    2914            1 :         opParam.VDataDes.dataType = dataType;
    2915            1 :         opParam.reduceType = HcclReduceOp::HCCL_REDUCE_RESERVED;
    2916            1 :         opParam.stream = streamObj;
    2917            1 :         opParam.syncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
    2918            1 :         opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
    2919            1 :         opParam.aicpuUnfoldMode = aicpuUnfoldMode;
    2920            1 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    2921            1 :         opParam.isCapture = isCapture;
    2922            1 :         opParam.rankSize = userRankSize_;
    2923            1 :         opParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER_V;
    2924              : 
    2925            1 :         if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
    2926            0 :             for (u32 i = 0; i < userRankSize_; i++) {
    2927            0 :                 HCCL_CONFIG_DEBUG(HCCL_ALG,
    2928              :                                   "[HcclCommunicator][AllGatherVOutPlace]userRank_[%u], rankIdx[%u],"
    2929              :                                   "outputCounts[%llu], outputDispls[%llu]",
    2930              :                                   userRank_, i, counts[i], static_cast<const u64 *>(outputDispls)[i]);
    2931              :             }
    2932              :         }
    2933              : 
    2934            1 :         CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_ALLGATHER_V, opParam));
    2935              : 
    2936            1 :         return HCCL_SUCCESS;
    2937            1 :     }
    2938              : 
    2939           79 :     void HcclCommunicator::GetAndSetSyncMode(SyncMode &preSyncMode, SyncMode newSyncMode)
    2940              :     {
    2941           79 :         if (newSyncMode == SyncMode::UNLIMITED_TIMEWAITSYNCMODE) {
    2942            0 :             if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
    2943            0 :                 HCCL_WARNING("310P don't support unlimited notify wait mode");
    2944              :             } else {
    2945            0 :                 HcclGetNotifyWaitMode(dispatcher_, &preSyncMode);
    2946            0 :                 HcclSetNotifyWaitMode(dispatcher_, newSyncMode);
    2947              :             }
    2948              :         }
    2949           79 :     }
    2950              : 
    2951           73 :     void HcclCommunicator::RestorePreSyncMode(SyncMode preSyncMode, SyncMode newSyncMode)
    2952              :     {
    2953           73 :         if (newSyncMode == SyncMode::UNLIMITED_TIMEWAITSYNCMODE && !Is310P3Common(isHaveCpuRank_, deviceType_)) {
    2954            0 :             HcclSetNotifyWaitMode(dispatcher_, preSyncMode);
    2955              :         }
    2956           73 :     }
    2957              : 
    2958           46 :     HcclResult HcclCommunicator::AllReduce(const std::string &tag, void *inputPtr, void *outputPtr, u64 count,
    2959              :                                            HcclDataType dataType, HcclReduceOp op, HcclRtStream stream,
    2960              :                                            SyncMode syncMode, const HcomCollOpInfo *opInfo)
    2961              :     {
    2962           46 :         CHK_RET(CheckSuspendingStatus());
    2963           46 :         bool aicpuUnfoldMode = false;
    2964           46 :         if (GetAicpuUnfoldConfig() == true &&
    2965           16 :             IsSupportSDMAReduce(inputPtr, outputPtr, dataType, op) &&
    2966           61 :             deviceType_ == DevType::DEV_TYPE_910_93 && (userRankSize_ != 1)) {
    2967            0 :             aicpuUnfoldMode = true;
    2968              :         }
    2969              : 
    2970           45 :         if (!IsAtomicInit()) {
    2971            0 :             HCCL_ERROR("[HcclCommunicator][AllReduce]errNo[0x%016llx] hccl init must be called before call this function",
    2972              :                        HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    2973            0 :             return HCCL_E_UNAVAIL;
    2974              :         }
    2975              : 
    2976              :         // 设置notify wait模式
    2977           61 :         SyncMode preSyncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
    2978           61 :         GetAndSetSyncMode(preSyncMode, syncMode);
    2979              : 
    2980           60 :         Stream streamObj(stream);
    2981           60 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    2982              : 
    2983           56 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    2984           51 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    2985           48 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    2986           46 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    2987              : 
    2988           45 :         u32 perDataSize = SIZE_TABLE[dataType];
    2989           45 :         u64 totalSize = count * perDataSize;
    2990              : 
    2991           45 :         OpParam opParam;
    2992           52 :         opParam.tag = tag;
    2993           61 :         opParam.inputPtr = inputPtr;
    2994           61 :         opParam.inputSize = totalSize;
    2995           61 :         opParam.outputPtr = outputPtr;
    2996           61 :         opParam.outputSize = totalSize;
    2997           61 :         opParam.DataDes.count = count;
    2998           61 :         opParam.DataDes.dataType = dataType;
    2999           61 :         opParam.reduceType = op;
    3000           61 :         opParam.stream = streamObj;
    3001           50 :         opParam.syncMode = syncMode;
    3002           50 :         opParam.aicpuUnfoldMode = aicpuUnfoldMode;
    3003           50 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    3004           53 :         opParam.opType = HcclCMDType::HCCL_CMD_ALLREDUCE;
    3005              :         // 用于inplace支持重执行场景的图模式归一至单算子模式
    3006           53 :         retryOrigWorkflowMode_ = GetWorkflowMode();
    3007            0 :         bool isHcclOpInplace = IsHcclOpInplace(HcclCMDType::HCCL_CMD_ALLREDUCE, opParam, userRank_, userRankSize_,
    3008           53 :                                                isInplaceStatus_);
    3009           60 :         if (aicpuUnfoldMode && retryEnable_ && isHcclOpInplace) {
    3010            0 :             HCCL_DEBUG("The retry with inplace case is expected to be supported, "
    3011              :                        "aicpuUnfoldMode[%d], retryEnable_[%d], isHcclOpInplace[%d], "
    3012              :                        "therefore HcclWorkflowMode is converted from [%d] to HCCL_WORKFLOW_MODE_OP_BASE",
    3013              :                        aicpuUnfoldMode, retryEnable_, isHcclOpInplace, static_cast<u8>(retryOrigWorkflowMode_));
    3014            0 :             CHK_RET(SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE));
    3015              :         }
    3016              : 
    3017           60 :         CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_ALLREDUCE, opParam));
    3018              : 
    3019           53 :         RestorePreSyncMode(preSyncMode, syncMode);
    3020           53 :         CHK_RET(SetWorkflowMode(retryOrigWorkflowMode_));
    3021           53 :         return HCCL_SUCCESS;
    3022           58 :     }
    3023              : 
    3024            0 :     HcclResult HcclCommunicator::AllReduceAicpuUnfold(const std::string &tag, void *inputPtr, void *outputPtr, u64 count,
    3025              :                                                       HcclDataType dataType, HcclReduceOp op, HcclRtStream stream)
    3026              :     {
    3027            0 :         Stream streamObj(stream);
    3028            0 :         u32 perDataSize = SIZE_TABLE[dataType];
    3029            0 :         u64 totalSize = count * perDataSize;
    3030            0 :         OpParam opParam;
    3031            0 :         opParam.tag = tag;
    3032            0 :         opParam.inputPtr = inputPtr;
    3033            0 :         opParam.inputSize = totalSize;
    3034            0 :         opParam.outputPtr = outputPtr;
    3035            0 :         opParam.outputSize = totalSize;
    3036            0 :         opParam.DataDes.count = count;
    3037            0 :         opParam.DataDes.dataType = dataType;
    3038            0 :         opParam.reduceType = op;
    3039            0 :         opParam.stream = streamObj;
    3040            0 :         opParam.isCapture = StreamIsCapture(stream);
    3041            0 :         opParam.syncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
    3042            0 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    3043            0 :         AlgType algType;
    3044            0 :         algType.algoLevel0 = AlgTypeLevel0::ALG_LEVEL0_NP_SINGLE_RING;
    3045            0 :         algType.algoLevel1 = AlgTypeLevel1::ALG_LEVEL1_RING;
    3046            0 :         auto slaveStreams = opParam.isCapture ? std::vector<Stream>{opStream_} : std::vector<Stream>{};
    3047            0 :         CaptureSlaveStreams(streamObj.ptr(), slaveStreams);
    3048            0 :         CHK_RET(RegisterDfxInfo(opParam, algType, slaveStreams));
    3049              :         HcclResult ret;
    3050            0 :         if (!IsExistCommRes(tag)) {
    3051            0 :             uint64_t streamMode = 0;
    3052            0 :             CHK_RET(hrtStreamGetMode(stream, &streamMode));
    3053              :             rtStream_t aicpuStream;
    3054            0 :             ret = Mc2AiCpuStreamAllocAndGet(streamMode, aicpuStream);
    3055            0 :             void *commContext = nullptr;
    3056            0 :             ret = CreateCommResource(tag, aicpuStream, true, &commContext);
    3057            0 :             if (ret != HCCL_SUCCESS) {
    3058            0 :                 HCCL_ERROR("[hcclImpl][CreateComm]create aicpu unfold comminfo by tag[%s] failed. return[%d]",
    3059              :                            tag.c_str(), ret);
    3060            0 :                 return ret;
    3061              :             }
    3062              :         }
    3063            0 :         AicpuOpTiling opTilingInfo;
    3064            0 :         std::string kernelName = "RunAicpuRpcSrvLaunch";
    3065            0 :         ret = AicpuKfcTilingDataLaunch(opParam, HcclCMDType::HCCL_CMD_ALLREDUCE, commContext_, kernelName, opTilingInfo);
    3066            0 :         if (ret != HCCL_SUCCESS) {
    3067            0 :             HCCL_ERROR("[hcclImpl][TilingData]aicpu unfold tiling data launch failed. return[%d] inputPtr[%p]"
    3068              :                        "outputPtr[%p] count[%llu] dataType[%s] op[%s]",
    3069              :                        ret, inputPtr, outputPtr, count,
    3070              :                        GetDataTypeEnumStr(dataType).c_str(), GetReduceOpEnumStr(op).c_str());
    3071            0 :             return ret;
    3072              :         }
    3073            0 :         CHK_RET(UnRegisterDfxInfo(opParam, slaveStreams));
    3074            0 :         return HCCL_SUCCESS;
    3075            0 :     }
    3076              : 
    3077           17 :     HcclResult HcclCommunicator::AllReduceOutPlace(const std::string &tag, void *inputPtr, void *outputPtr, u64 count,
    3078              :                                                    HcclDataType dataType, HcclReduceOp op, HcclRtStream stream,
    3079              :                                                    SyncMode syncMode)
    3080              :     {
    3081           17 :         CHK_RET(CheckSuspendingStatus());
    3082           17 :         const u32 RANK_SIZE_TWO = 2;
    3083           17 :         bool aicpuUnfoldMode = false;
    3084           17 :         if (GetAicpuUnfoldConfig() == true &&
    3085            0 :             IsSupportSDMAReduce(inputPtr, outputPtr, dataType, op)) {
    3086            0 :             if (userRankSize_ >= RANK_SIZE_TWO && Is310P3Common(isHaveCpuRank_, deviceType_)) {
    3087            0 :                 HcclResult ret = AllReduceAicpuUnfold(tag, inputPtr, outputPtr, count, dataType, op, stream);
    3088            0 :                 CHK_PRT_RET((ret != HCCL_SUCCESS),
    3089              :                             HCCL_ERROR("[HcclCommunicator][AllReduce]errNo[0x%016llx]  tag[%s], AllReduce aicpu unfold failed",
    3090              :                                        HCCL_ERROR_CODE(ret), tag.c_str()),
    3091              :                             ret);
    3092              : 
    3093            0 :                 return HCCL_SUCCESS;
    3094              :             }
    3095            0 :             if ((deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
    3096            0 :                 aicpuUnfoldMode = true;
    3097              :             }
    3098              :         }
    3099              : 
    3100           16 :         bool isCapture = StreamIsCapture(stream);
    3101              : 
    3102           18 :         if (!IsAtomicInit()) {
    3103            0 :             HCCL_ERROR(
    3104              :                 "[HcclCommunicator][AllReduceOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
    3105              :                 HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    3106            0 :             return HCCL_E_UNAVAIL;
    3107              :         }
    3108              : 
    3109              :         // 设置notify wait模式
    3110           21 :         SyncMode preSyncMode = SyncMode::DEFAULT_TIMEWAITSYNCMODE;
    3111           21 :         GetAndSetSyncMode(preSyncMode, syncMode);
    3112              : 
    3113           19 :         Stream streamObj(stream);
    3114           19 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    3115              : 
    3116           18 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    3117           17 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    3118           13 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    3119           13 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    3120              : 
    3121           15 :         u32 perDataSize = SIZE_TABLE[dataType];
    3122           15 :         u64 totalSize = count * perDataSize;
    3123              : 
    3124           15 :         OpParam opParam;
    3125           17 :         opParam.tag = tag;
    3126           21 :         opParam.inputPtr = inputPtr;
    3127           21 :         opParam.inputSize = totalSize;
    3128           21 :         opParam.outputPtr = outputPtr;
    3129           21 :         opParam.outputSize = totalSize;
    3130           21 :         opParam.DataDes.count = count;
    3131           21 :         opParam.DataDes.dataType = dataType;
    3132           21 :         opParam.reduceType = op;
    3133           21 :         opParam.stream = streamObj;
    3134           18 :         opParam.syncMode = syncMode;
    3135           18 :         opParam.aicpuUnfoldMode = aicpuUnfoldMode;
    3136           18 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    3137           18 :         opParam.isCapture = isCapture;
    3138           18 :         opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
    3139           18 :         opParam.opType = HcclCMDType::HCCL_CMD_ALLREDUCE;
    3140              : 
    3141           18 :         CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_ALLREDUCE, opParam));
    3142              : 
    3143           20 :         RestorePreSyncMode(preSyncMode, syncMode);
    3144           20 :         return HCCL_SUCCESS;
    3145           21 :     }
    3146              : 
    3147            4 :     HcclResult HcclCommunicator::AlltoAllV(const void *sendBuf, const void *sendCounts, const void *sdispls,
    3148              :                                            HcclDataType sendType, const void *recvBuf, const void *recvCounts, const void *rdispls, HcclDataType recvType,
    3149              :                                            rtStream_t stream, const std::string &tag)
    3150              :     {
    3151            4 :         CHK_RET(CheckSuspendingStatus());
    3152              : 
    3153            4 :         if (!IsAtomicInit()) {
    3154            0 :             HCCL_ERROR("[HcclCommunicator][AlltoAllV]errNo[0x%016llx] hccl init must be called before call this function",
    3155              :                        HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    3156            0 :             return HCCL_E_UNAVAIL;
    3157              :         }
    3158              : 
    3159            4 :         if (IsNeedNicInit()) {
    3160            4 :             HCCL_INFO("InitNic.");
    3161            4 :             CHK_RET(InitNic());
    3162              :         }
    3163              : 
    3164            4 :         bool isCapture = StreamIsCapture(stream);
    3165              : 
    3166            4 :         Stream streamObj(stream);
    3167            4 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    3168              : 
    3169            4 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    3170            4 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    3171            4 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    3172            4 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    3173              : 
    3174            4 :         OpParam opParam;
    3175            4 :         opParam.tag = tag;
    3176            4 :         opParam.inputPtr = const_cast<void *>(sendBuf);
    3177            4 :         opParam.outputPtr = const_cast<void *>(recvBuf);
    3178            4 :         opParam.All2AllDataDes.sendType = sendType;
    3179            4 :         opParam.All2AllDataDes.recvType = recvType;
    3180            4 :         opParam.All2AllDataDes.sendCounts = const_cast<void *>(sendCounts);
    3181            4 :         opParam.All2AllDataDes.recvCounts = const_cast<void *>(recvCounts);
    3182            4 :         opParam.All2AllDataDes.sdispls = const_cast<void *>(sdispls);
    3183            4 :         opParam.All2AllDataDes.rdispls = const_cast<void *>(rdispls);
    3184            4 :         opParam.stream = streamObj;
    3185            4 :         opParam.opType = HcclCMDType::HCCL_CMD_ALLTOALLV;
    3186            4 :         opParam.aicpuUnfoldMode = EnableAicpuUnfold(isCapture);
    3187            4 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    3188            4 :         opParam.isCapture = isCapture;
    3189              : 
    3190            4 :         if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
    3191            0 :             for (u32 i = 0; i < userRankSize_; i++) {
    3192            0 :                 HCCL_CONFIG_INFO(HCCL_ALG, "[HcclCommunicator][AlltoAllV] rank[%u], sendCounts[%llu], sendDispls[%llu] "
    3193              :                                            "recvCounts[%llu], recvDispls[%llu]",
    3194              :                                  userRank_,
    3195              :                                  *(static_cast<const u64 *>(opParam.All2AllDataDes.sendCounts) + i),
    3196              :                                  *(static_cast<const u64 *>(opParam.All2AllDataDes.sdispls) + i),
    3197              :                                  *(static_cast<const u64 *>(opParam.All2AllDataDes.recvCounts) + i),
    3198              :                                  *(static_cast<const u64 *>(opParam.All2AllDataDes.rdispls) + i));
    3199              :             }
    3200              :         }
    3201              : 
    3202            4 :         CHK_RET(ExecOpAlltoAll(HcclCMDType::HCCL_CMD_ALLTOALLV, opParam));
    3203            4 :         return HCCL_SUCCESS;
    3204            4 :     }
    3205              : 
    3206            0 :     HcclResult HcclCommunicator::AlltoAllVOutPlace(const void *sendBuf, const void *sendCounts, const void *sdispls,
    3207              :                                                    HcclDataType sendType, const void *recvBuf, const void *recvCounts, const void *rdispls, HcclDataType recvType,
    3208              :                                                    rtStream_t stream, const std::string &tag)
    3209              :     {
    3210            0 :         CHK_RET(CheckSuspendingStatus());
    3211            0 :         CHK_PRT_RET(Is310P3Common(isHaveCpuRank_, deviceType_),
    3212              :                     HCCL_RUN_INFO("[AlltoAllVOutPlace]This method cannot be invoked in the current scenario."), HCCL_SUCCESS);
    3213            0 :         if (!IsAtomicInit()) {
    3214            0 :             HCCL_ERROR(
    3215              :                 "[HcclCommunicator][AlltoAllVOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
    3216              :                 HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    3217            0 :             return HCCL_E_UNAVAIL;
    3218              :         }
    3219              : 
    3220            0 :         if (IsNeedNicInit()) {
    3221            0 :             HCCL_INFO("InitNic.");
    3222            0 :             CHK_RET(InitNic());
    3223              :         }
    3224              : 
    3225            0 :         bool isCapture = StreamIsCapture(stream);
    3226              : 
    3227            0 :         Stream streamObj(stream);
    3228            0 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    3229              : 
    3230            0 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    3231            0 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    3232            0 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    3233            0 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    3234              : 
    3235            0 :         OpParam opParam;
    3236            0 :         opParam.tag = tag;
    3237            0 :         opParam.inputPtr = const_cast<void *>(sendBuf);
    3238            0 :         opParam.outputPtr = const_cast<void *>(recvBuf);
    3239            0 :         opParam.All2AllDataDes.sendType = sendType;
    3240            0 :         opParam.All2AllDataDes.recvType = recvType;
    3241            0 :         opParam.All2AllDataDes.sendCounts = const_cast<void *>(sendCounts);
    3242            0 :         opParam.All2AllDataDes.recvCounts = const_cast<void *>(recvCounts);
    3243            0 :         opParam.All2AllDataDes.sdispls = const_cast<void *>(sdispls);
    3244            0 :         opParam.All2AllDataDes.rdispls = const_cast<void *>(rdispls);
    3245            0 :         opParam.stream = streamObj;
    3246            0 :         opParam.opType = HcclCMDType::HCCL_CMD_ALLTOALLV;
    3247            0 :         opParam.aicpuUnfoldMode = EnableAicpuUnfold(isCapture);
    3248            0 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    3249            0 :         opParam.isCapture = isCapture;
    3250              : 
    3251            0 :         if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
    3252            0 :             for (u32 i = 0; i < userRankSize_; i++) {
    3253            0 :                 HCCL_CONFIG_INFO(HCCL_ALG, "[HcclCommunicator][AlltoAllVOutPlace] rank[%u], sendCounts[%llu],"
    3254              :                                            "sendDispls[%llu], recvCounts[%llu], recvDispls[%llu]",
    3255              :                                  userRank_,
    3256              :                                  *(static_cast<const u64 *>(opParam.All2AllDataDes.sendCounts) + i),
    3257              :                                  *(static_cast<const u64 *>(opParam.All2AllDataDes.sdispls) + i),
    3258              :                                  *(static_cast<const u64 *>(opParam.All2AllDataDes.recvCounts) + i),
    3259              :                                  *(static_cast<const u64 *>(opParam.All2AllDataDes.rdispls) + i));
    3260              :             }
    3261              :         }
    3262              : 
    3263            0 :         CHK_RET(ExecOpAlltoAll(HcclCMDType::HCCL_CMD_ALLTOALLV, opParam));
    3264            0 :         return HCCL_SUCCESS;
    3265            0 :     }
    3266              : 
    3267            1 :     HcclResult HcclCommunicator::AlltoAllVC(const void *sendBuf, const void *sendCountMatrix, HcclDataType sendType,
    3268              :                                             const void *recvBuf, HcclDataType recvType, rtStream_t stream, const std::string &tag)
    3269              :     {
    3270            1 :         CHK_RET(CheckSuspendingStatus());
    3271            1 :         if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
    3272            0 :             HCCL_ERROR("[%s][%s]AlltoAllVC is not supported",
    3273              :                 LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_NOT_SUPPORTED.c_str());
    3274            0 :             return HCCL_E_NOT_SUPPORT;
    3275              :         }
    3276            1 :         if (!IsAtomicInit()) {
    3277            0 :             HCCL_ERROR("[HcclCommunicator][AlltoAllVC]errNo[0x%016llx] hccl init must be called before call this function",
    3278              :                        HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    3279            0 :             return HCCL_E_UNAVAIL;
    3280              :         }
    3281              : 
    3282            1 :         if (IsNeedNicInit()) {
    3283            1 :             HCCL_INFO("InitNic.");
    3284            1 :             CHK_RET(InitNic());
    3285              :         }
    3286              : 
    3287            1 :         bool isCapture = StreamIsCapture(stream);
    3288              : 
    3289            1 :         Stream streamObj(stream);
    3290            1 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    3291              : 
    3292            1 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    3293            1 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    3294            1 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    3295            1 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    3296              : 
    3297            1 :         OpParam opParam;
    3298            1 :         opParam.tag = tag;
    3299            1 :         opParam.inputPtr = const_cast<void *>(sendBuf);
    3300            1 :         opParam.outputPtr = const_cast<void *>(recvBuf);
    3301            1 :         opParam.All2AllDataDes.sendType = sendType;
    3302            1 :         opParam.All2AllDataDes.recvType = recvType;
    3303            1 :         opParam.All2AllDataDes.sendCountMatrix = const_cast<void *>(sendCountMatrix);
    3304            1 :         opParam.stream = streamObj;
    3305            1 :         opParam.opType = HcclCMDType::HCCL_CMD_ALLTOALLVC;
    3306            1 :         opParam.aicpuUnfoldMode = EnableAicpuUnfold(isCapture);
    3307            1 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    3308            1 :         opParam.isCapture = isCapture;
    3309              : 
    3310            1 :         if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
    3311            0 :             for (u32 i = 0; i < userRankSize_; i++) {
    3312            0 :                 for (u32 j = 0; j < userRankSize_; j++) {
    3313            0 :                     HCCL_CONFIG_DEBUG(HCCL_ALG, "[HcclCommunicator][AlltoAllVC] usrRank[%u] rank[%u] to remoteRank[%u], "
    3314              :                                                 "sendCounts[%llu]",
    3315              :                                       userRank_, i, j,
    3316              :                                       *(static_cast<const u64 *>(opParam.All2AllDataDes.sendCountMatrix) + i * userRankSize_ + j));
    3317              :                 }
    3318              :             }
    3319              :         }
    3320              : 
    3321            1 :         CHK_RET(ExecOpAlltoAll(HcclCMDType::HCCL_CMD_ALLTOALLVC, opParam));
    3322            1 :         return HCCL_SUCCESS;
    3323            1 :     }
    3324              : 
    3325            0 :     HcclResult HcclCommunicator::AlltoAllVCOutPlace(const void *sendBuf, const void *sendCountMatrix, HcclDataType sendType,
    3326              :                                                     const void *recvBuf, HcclDataType recvType, rtStream_t stream, const std::string &tag)
    3327              :     {
    3328            0 :         CHK_RET(CheckSuspendingStatus());
    3329            0 :         CHK_PRT_RET(Is310P3Common(isHaveCpuRank_, deviceType_),
    3330              :                     HCCL_RUN_INFO("[AlltoAllVCOutPlace]This method cannot be invoked in the current scenario."), HCCL_SUCCESS);
    3331              : 
    3332            0 :         if (!IsAtomicInit()) {
    3333            0 :             HCCL_ERROR(
    3334              :                 "[HcclCommunicator][AlltoAllVCOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
    3335              :                 HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    3336            0 :             return HCCL_E_UNAVAIL;
    3337              :         }
    3338              : 
    3339            0 :         if (IsNeedNicInit()) {
    3340            0 :             HCCL_INFO("InitNic");
    3341            0 :             CHK_RET(InitNic());
    3342              :         }
    3343              : 
    3344            0 :         bool isCapture = StreamIsCapture(stream);
    3345              : 
    3346            0 :         Stream streamObj(stream);
    3347            0 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    3348              : 
    3349            0 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    3350            0 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    3351            0 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    3352            0 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    3353              : 
    3354            0 :         OpParam opParam;
    3355            0 :         opParam.tag = tag;
    3356            0 :         opParam.inputPtr = const_cast<void *>(sendBuf);
    3357            0 :         opParam.outputPtr = const_cast<void *>(recvBuf);
    3358            0 :         opParam.All2AllDataDes.sendType = sendType;
    3359            0 :         opParam.All2AllDataDes.recvType = recvType;
    3360            0 :         opParam.All2AllDataDes.sendCountMatrix = const_cast<void *>(sendCountMatrix);
    3361            0 :         opParam.stream = streamObj;
    3362            0 :         opParam.opType = HcclCMDType::HCCL_CMD_ALLTOALLVC;
    3363            0 :         opParam.aicpuUnfoldMode = EnableAicpuUnfold(isCapture);
    3364            0 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    3365            0 :         opParam.isCapture = isCapture;
    3366              : 
    3367            0 :         if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
    3368            0 :             for (u32 i = 0; i < userRankSize_; i++) {
    3369            0 :                 for (u32 j = 0; j < userRankSize_; j++) {
    3370            0 :                     HCCL_CONFIG_DEBUG(HCCL_ALG, "[HcclCommunicator][AlltoAllVCOutPlace] usrRank[%u] rank[%u]"
    3371              :                                                 "to remoteRank[%u], sendCounts[%llu]",
    3372              :                                       userRank_, i, j,
    3373              :                                       *(static_cast<const u64 *>(opParam.All2AllDataDes.sendCountMatrix) + i * userRankSize_ + j));
    3374              :                 }
    3375              :             }
    3376              :         }
    3377              : 
    3378            0 :         CHK_RET(ExecOpAlltoAll(HcclCMDType::HCCL_CMD_ALLTOALLVC, opParam));
    3379            0 :         return HCCL_SUCCESS;
    3380            0 :     }
    3381              : 
    3382            0 :     HcclResult HcclCommunicator::AlltoAll(const void *sendBuf, u64 sendCount, HcclDataType sendType,
    3383              :                                           const void *recvBuf, u64 recvCount, HcclDataType recvType, rtStream_t stream, const std::string &tag)
    3384              :     {
    3385            0 :         CHK_RET(CheckSuspendingStatus());
    3386            0 :         if (!IsAtomicInit()) {
    3387            0 :             HCCL_ERROR("[HcclCommunicator][AlltoAll]errNo[0x%016llx] hccl init must be called before call this function",
    3388              :                        HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    3389            0 :             return HCCL_E_UNAVAIL;
    3390              :         }
    3391              : 
    3392            0 :         if (IsNeedNicInit()) {
    3393            0 :             HCCL_INFO("InitNic.");
    3394            0 :             CHK_RET(InitNic());
    3395              :         }
    3396              : 
    3397            0 :         bool isCapture = StreamIsCapture(stream);
    3398              : 
    3399            0 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    3400              : 
    3401              :         // 生成sendCountMatrix矩阵,alltoall的底层实现走alltoallvc
    3402            0 :         std::vector<u64> sendCountMatrix(userRankSize_ * userRankSize_, sendCount);
    3403              : 
    3404            0 :         OpParam opParam;
    3405            0 :         opParam.tag = tag;
    3406            0 :         opParam.inputPtr = const_cast<void *>(sendBuf);
    3407            0 :         opParam.outputPtr = const_cast<void *>(recvBuf);
    3408            0 :         opParam.All2AllDataDes.sendType = sendType;
    3409            0 :         opParam.All2AllDataDes.recvType = recvType;
    3410            0 :         opParam.All2AllDataDes.sendCount = sendCount;
    3411            0 :         opParam.All2AllDataDes.recvCount = recvCount;
    3412            0 :         opParam.All2AllDataDes.sendCountMatrix = static_cast<void *>(sendCountMatrix.data());
    3413            0 :         opParam.stream = Stream(stream);
    3414            0 :         opParam.opType = HcclCMDType::HCCL_CMD_ALLTOALL;
    3415            0 :         opParam.aicpuUnfoldMode = false;
    3416            0 :         opParam.aicpuCacheEnable = 0;
    3417            0 :         opParam.isCapture = isCapture;
    3418            0 :         if (EnableAicpuUnfold(isCapture)) {
    3419            0 :             opParam.aicpuUnfoldMode = true;
    3420            0 :             opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    3421              :         }
    3422              : 
    3423            0 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    3424            0 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    3425            0 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    3426            0 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    3427            0 :         CHK_RET(ExecOpAlltoAll(HcclCMDType::HCCL_CMD_ALLTOALL, opParam));
    3428            0 :         return HCCL_SUCCESS;
    3429            0 :     }
    3430              : 
    3431            2 :     HcclResult HcclCommunicator::Broadcast(const std::string &tag, void *ptr, u64 count, HcclDataType dataType, u32 root,
    3432              :                                            HcclRtStream stream)
    3433              :     {
    3434            2 :         CHK_RET(CheckSuspendingStatus());
    3435            2 :         bool aicpuUnfoldMode = false;
    3436            2 :         if (GetAicpuUnfoldConfig() == true && deviceType_ == DevType::DEV_TYPE_910_93 && (userRankSize_ != 1)) {
    3437            0 :             aicpuUnfoldMode = true;
    3438              :         }
    3439              : 
    3440            2 :         if (!IsAtomicInit()) {
    3441            0 :             HCCL_ERROR("[HcclCommunicator][Broadcast]errNo[0x%016llx] hccl init must be called before call this function",
    3442              :                        HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    3443            0 :             return HCCL_E_UNAVAIL;
    3444              :         }
    3445              : 
    3446            2 :         Stream streamObj(stream);
    3447            2 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    3448              : 
    3449            2 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    3450            2 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    3451            2 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    3452            2 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    3453            2 :         u32 perDataSize = SIZE_TABLE[dataType];
    3454            2 :         u64 totalSize = count * perDataSize;
    3455              : 
    3456            2 :         OpParam opParam;
    3457            2 :         opParam.tag = tag;
    3458            2 :         opParam.inputPtr = ptr;
    3459            2 :         opParam.outputPtr = ptr;
    3460            2 :         opParam.inputSize = totalSize;
    3461            2 :         opParam.outputSize = totalSize;
    3462            2 :         opParam.DataDes.count = count;
    3463            2 :         opParam.DataDes.dataType = dataType;
    3464            2 :         opParam.root = root;
    3465            2 :         opParam.stream = streamObj;
    3466            2 :         opParam.aicpuUnfoldMode = aicpuUnfoldMode;
    3467            2 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    3468            2 :         opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
    3469            2 :         opParam.opType = HcclCMDType::HCCL_CMD_BROADCAST;
    3470              : 
    3471            2 :         CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_BROADCAST, opParam));
    3472              : 
    3473            2 :         return HCCL_SUCCESS;
    3474            2 :     }
    3475              : 
    3476            0 :     HcclResult HcclCommunicator::BroadcastOutPlace(const std::string &tag, void *ptr, u64 count, HcclDataType dataType,
    3477              :                                                    u32 root, HcclRtStream stream)
    3478              :     {
    3479            0 :         CHK_RET(CheckSuspendingStatus());
    3480            0 :         bool aicpuUnfoldMode = false;
    3481            0 :         if (GetAicpuUnfoldConfig() == true && deviceType_ == DevType::DEV_TYPE_910_93 && (userRankSize_ != 1)) {
    3482            0 :             aicpuUnfoldMode = true;
    3483              :         }
    3484              : 
    3485            0 :         CHK_PRT_RET(Is310P3Common(isHaveCpuRank_, deviceType_),
    3486              :                     HCCL_RUN_INFO("[BroadcastOutPlace]This method cannot be invoked in the current scenario."), HCCL_SUCCESS);
    3487              : 
    3488            0 :         if (!IsAtomicInit()) {
    3489            0 :             HCCL_ERROR("[HcclCommunicator][BroadcastOutPlace]errNo[0x%016llx] hccl init must be called before"
    3490              :                        " call this function",
    3491              :                        HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    3492            0 :             return HCCL_E_UNAVAIL;
    3493              :         }
    3494              : 
    3495            0 :         bool isCapture = StreamIsCapture(stream);
    3496              : 
    3497            0 :         Stream streamObj(stream);
    3498            0 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    3499              : 
    3500            0 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    3501            0 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    3502            0 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    3503            0 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    3504              : 
    3505            0 :         u32 perDataSize = SIZE_TABLE[dataType];
    3506            0 :         u64 totalSize = count * perDataSize;
    3507              : 
    3508            0 :         OpParam opParam;
    3509            0 :         opParam.tag = tag;
    3510            0 :         opParam.inputPtr = ptr;
    3511            0 :         opParam.outputPtr = ptr;
    3512            0 :         opParam.inputSize = totalSize;
    3513            0 :         opParam.outputSize = totalSize;
    3514            0 :         opParam.DataDes.count = count;
    3515            0 :         opParam.DataDes.dataType = dataType;
    3516            0 :         opParam.root = root;
    3517            0 :         opParam.stream = streamObj;
    3518            0 :         opParam.aicpuUnfoldMode = aicpuUnfoldMode;
    3519            0 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    3520            0 :         opParam.isCapture = isCapture;
    3521            0 :         opParam.opType = HcclCMDType::HCCL_CMD_BROADCAST;
    3522              : 
    3523            0 :         CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_BROADCAST, opParam));
    3524              : 
    3525            0 :         return HCCL_SUCCESS;
    3526            0 :     }
    3527              : 
    3528            0 :     HcclResult HcclCommunicator::Scatter(const std::string &tag, void *inputPtr, void *outputPtr, u64 recvCount,
    3529              :                                          HcclDataType dataType, u32 root, HcclRtStream stream)
    3530              :     {
    3531            0 :         CHK_RET(CheckSuspendingStatus());
    3532            0 :         if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
    3533            0 :             HCCL_ERROR("[%s][%s]Scatter Not Supported Yet",
    3534              :                 LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_NOT_SUPPORTED.c_str());
    3535            0 :             return HCCL_E_NOT_SUPPORT;
    3536              :         }
    3537            0 :         bool aicpuUnfoldMode = false;
    3538            0 :         if (GetAicpuUnfoldConfig() == true && deviceType_ == DevType::DEV_TYPE_910_93 && (userRankSize_ != 1)) {
    3539            0 :             aicpuUnfoldMode = true;
    3540              :         }
    3541              : 
    3542            0 :         if (!IsAtomicInit()) {
    3543            0 :             HCCL_ERROR("[HcclCommunicator][Scatter]errNo[0x%016llx] hccl init must be called before call this function",
    3544              :                        HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    3545            0 :             return HCCL_E_UNAVAIL;
    3546              :         }
    3547              : 
    3548            0 :         Stream streamObj(stream);
    3549            0 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    3550              : 
    3551            0 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    3552            0 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    3553            0 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    3554            0 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    3555              : 
    3556            0 :         u32 perDataSize = SIZE_TABLE[dataType];
    3557            0 :         u64 outputSize = recvCount * perDataSize;
    3558            0 :         u64 totalSize = outputSize * userRankSize_;
    3559              : 
    3560            0 :         OpParam opParam;
    3561            0 :         opParam.tag = tag;
    3562            0 :         opParam.inputPtr = inputPtr;
    3563            0 :         opParam.inputSize = totalSize;
    3564            0 :         opParam.outputPtr = outputPtr;
    3565            0 :         opParam.outputSize = totalSize;
    3566            0 :         opParam.DataDes.count = recvCount;
    3567            0 :         opParam.DataDes.dataType = dataType;
    3568            0 :         opParam.stream = streamObj;
    3569            0 :         opParam.aicpuUnfoldMode = aicpuUnfoldMode;
    3570            0 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    3571            0 :         opParam.root = root;
    3572            0 :         opParam.opType = HcclCMDType::HCCL_CMD_SCATTER;
    3573            0 :         CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_SCATTER, opParam));
    3574            0 :         return HCCL_SUCCESS;
    3575            0 :     }
    3576              : 
    3577            7 :     HcclResult HcclCommunicator::ScatterOutPlace(const std::string &tag, void *inputPtr, void *outputPtr, u64 recvCount,
    3578              :                                                  HcclDataType dataType, u32 root, HcclRtStream stream)
    3579              :     {
    3580            7 :         CHK_RET(CheckSuspendingStatus());
    3581           10 :         if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
    3582            0 :             HCCL_ERROR("[%s][%s]ScatterOutPlace Not Supported Yet",
    3583              :                 LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_NOT_SUPPORTED.c_str());
    3584            0 :             return HCCL_E_NOT_SUPPORT;
    3585              :         }
    3586              : 
    3587           12 :         bool aicpuUnfoldMode = false;
    3588           12 :         if (GetAicpuUnfoldConfig() == true && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
    3589            0 :             aicpuUnfoldMode = true;
    3590              :         }
    3591              : 
    3592           11 :         bool isCapture = StreamIsCapture(stream);
    3593              : 
    3594           12 :         if (!IsAtomicInit()) {
    3595            0 :             HCCL_ERROR("[HcclCommunicator][ScatterOutPlace]errNo[0x%016llx] hccl init must be called before"
    3596              :                        " call this function",
    3597              :                        HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    3598            0 :             return HCCL_E_UNAVAIL;
    3599              :         }
    3600              : 
    3601           12 :         Stream streamObj(stream);
    3602           12 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    3603              : 
    3604           10 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    3605            8 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    3606            5 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    3607            5 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    3608              : 
    3609            4 :         u32 perDataSize = SIZE_TABLE[dataType];
    3610            4 :         u64 outputSize = recvCount * perDataSize;
    3611            4 :         u64 totalSize = outputSize * userRankSize_;
    3612              : 
    3613            4 :         OpParam opParam;
    3614            5 :         opParam.tag = tag;
    3615           12 :         opParam.inputPtr = inputPtr;
    3616           12 :         opParam.inputSize = totalSize;
    3617           12 :         opParam.outputPtr = outputPtr;
    3618           12 :         opParam.outputSize = totalSize;
    3619           12 :         opParam.DataDes.count = recvCount;
    3620           12 :         opParam.DataDes.dataType = dataType;
    3621           12 :         opParam.stream = streamObj;
    3622            5 :         opParam.aicpuUnfoldMode = aicpuUnfoldMode;
    3623            5 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    3624            5 :         opParam.isCapture = isCapture;
    3625            5 :         opParam.root = root;
    3626            5 :         opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
    3627            5 :         opParam.opType = HcclCMDType::HCCL_CMD_SCATTER;
    3628              : 
    3629            5 :         CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_SCATTER, opParam));
    3630              : 
    3631            8 :         return HCCL_SUCCESS;
    3632           12 :     }
    3633              : 
    3634            5 :     HcclResult HcclCommunicator::Reduce(const std::string &tag, void *inputPtr, void *outputPtr, u64 count,
    3635              :                                         HcclDataType dataType, HcclReduceOp op, u32 root, HcclRtStream stream)
    3636              :     {
    3637            5 :         CHK_RET(CheckSuspendingStatus());
    3638            5 :         if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
    3639            0 :             HCCL_ERROR("[%s][%s]Reduce Not Supported Yet",
    3640              :                 LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_NOT_SUPPORTED.c_str());
    3641            0 :             return HCCL_E_NOT_SUPPORT;
    3642              :         }
    3643            5 :         bool aicpuUnfoldMode = false;
    3644            5 :         if (GetAicpuUnfoldConfig() == true &&
    3645            5 :             IsSupportSDMAReduce(inputPtr, outputPtr, dataType, op) && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
    3646            0 :             aicpuUnfoldMode = true;
    3647              :         }
    3648              : 
    3649            5 :         if (!IsAtomicInit()) {
    3650            0 :             HCCL_ERROR("[HcclCommunicator][Reduce]errNo[0x%016llx] hccl init must be called before call this function",
    3651              :                        HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    3652            0 :             return HCCL_E_UNAVAIL;
    3653              :         }
    3654              : 
    3655            5 :         Stream streamObj(stream);
    3656            5 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    3657              : 
    3658            5 :         u32 perDataSize = SIZE_TABLE[dataType];
    3659            5 :         u64 totalSize = count * perDataSize;
    3660            5 :         OpParam opParam;
    3661            5 :         opParam.tag = tag;
    3662            5 :         opParam.inputPtr = inputPtr;
    3663            5 :         opParam.inputSize = totalSize;
    3664            5 :         opParam.outputPtr = outputPtr;
    3665            5 :         opParam.outputSize = totalSize;
    3666            5 :         opParam.DataDes.count = count;
    3667            5 :         opParam.DataDes.dataType = dataType;
    3668            5 :         opParam.reduceType = op;
    3669            5 :         opParam.root = root;
    3670            5 :         opParam.stream = streamObj;
    3671            5 :         opParam.aicpuUnfoldMode = aicpuUnfoldMode;
    3672            5 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    3673            5 :         opParam.opType = HcclCMDType::HCCL_CMD_REDUCE;
    3674              : 
    3675            5 :         CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_REDUCE, opParam));
    3676              : 
    3677            5 :         return HCCL_SUCCESS;
    3678            5 :     }
    3679              : 
    3680            2 :     HcclResult HcclCommunicator::ReduceOutPlace(const std::string &tag, void *inputPtr, void *outputPtr, u64 count,
    3681              :                                                 HcclDataType dataType, HcclReduceOp op, u32 root, HcclRtStream stream)
    3682              :     {
    3683            2 :         CHK_RET(CheckSuspendingStatus());
    3684            2 :         CHK_PRT_RET(Is310P3Common(isHaveCpuRank_, deviceType_),
    3685              :                     HCCL_RUN_INFO("[ReduceOutPlace]This method cannot be invoked in the current scenario."), HCCL_SUCCESS);
    3686              : 
    3687            2 :         bool aicpuUnfoldMode = false;
    3688            2 :         if (GetAicpuUnfoldConfig() == true &&
    3689            2 :             IsSupportSDMAReduce(inputPtr, outputPtr, dataType, op) && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
    3690            0 :             aicpuUnfoldMode = true;
    3691              :         }
    3692              : 
    3693            2 :         bool isCapture = StreamIsCapture(stream);
    3694              : 
    3695            4 :         if (!IsAtomicInit()) {
    3696            0 :             HCCL_ERROR(
    3697              :                 "[HcclCommunicator][ReduceOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
    3698              :                 HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    3699            0 :             return HCCL_E_UNAVAIL;
    3700              :         }
    3701              : 
    3702            4 :         Stream streamObj(stream);
    3703            4 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    3704              : 
    3705            4 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    3706            4 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    3707            3 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    3708            3 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    3709              : 
    3710            3 :         u32 perDataSize = SIZE_TABLE[dataType];
    3711            3 :         u64 totalSize = count * perDataSize;
    3712            3 :         OpParam opParam;
    3713            4 :         opParam.tag = tag;
    3714            4 :         opParam.inputPtr = inputPtr;
    3715            4 :         opParam.inputSize = totalSize;
    3716            4 :         opParam.outputPtr = outputPtr;
    3717            4 :         opParam.outputSize = totalSize;
    3718            4 :         opParam.DataDes.count = count;
    3719            4 :         opParam.DataDes.dataType = dataType;
    3720            4 :         opParam.reduceType = op;
    3721            4 :         opParam.root = root;
    3722            4 :         opParam.stream = streamObj;
    3723            4 :         opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
    3724            4 :         opParam.aicpuUnfoldMode = aicpuUnfoldMode;
    3725            4 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    3726            4 :         opParam.isCapture = isCapture;
    3727            4 :         opParam.opType = HcclCMDType::HCCL_CMD_REDUCE;
    3728              : 
    3729            4 :         CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_REDUCE, opParam));
    3730              : 
    3731            0 :         return HCCL_SUCCESS;
    3732            4 :     }
    3733              : 
    3734           15 :     HcclResult HcclCommunicator::ReduceScatter(const std::string &tag, void *inputPtr, void *outputPtr, u64 count,
    3735              :                                                HcclDataType dataType, HcclReduceOp op, HcclRtStream stream, HcomCollOpInfo *opInfo)
    3736              :     {
    3737           15 :         CHK_RET(CheckSuspendingStatus());
    3738           16 :         bool aicpuUnfoldMode = false;
    3739           16 :         if (GetAicpuUnfoldConfig() == true &&
    3740           17 :             IsSupportSDMAReduce(inputPtr, outputPtr, dataType, op) && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
    3741            0 :             aicpuUnfoldMode = true;
    3742              :         }
    3743              : 
    3744           17 :         if (!IsAtomicInit()) {
    3745            0 :             HCCL_ERROR(
    3746              :                 "[HcclCommunicator][ReduceScatter]errNo[0x%016llx] hccl init must be called before call this function",
    3747              :                 HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    3748            0 :             return HCCL_E_UNAVAIL;
    3749              :         }
    3750              : 
    3751           21 :         Stream streamObj(stream);
    3752           20 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    3753              : 
    3754           21 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    3755           19 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    3756           19 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    3757           18 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    3758              : 
    3759           15 :         u32 perDataSize = SIZE_TABLE[dataType];
    3760              : 
    3761           15 :         OpParam opParam;
    3762           16 :         opParam.tag = tag;
    3763           21 :         opParam.inputPtr = inputPtr;
    3764           21 :         opParam.inputSize = userRankSize_ * count * perDataSize;
    3765           21 :         opParam.outputPtr = outputPtr;
    3766           21 :         opParam.outputSize = count * perDataSize;
    3767           21 :         opParam.DataDes.count = count;
    3768           21 :         opParam.DataDes.dataType = dataType;
    3769           21 :         opParam.reduceType = op;
    3770           21 :         opParam.stream = streamObj;
    3771           15 :         opParam.opType = HcclCMDType::HCCL_CMD_REDUCE_SCATTER;
    3772           15 :         opParam.aicpuUnfoldMode = aicpuUnfoldMode;
    3773           15 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    3774              :         // 用于inplace支持重执行场景的图模式归一至单算子模式
    3775           15 :         retryOrigWorkflowMode_ = GetWorkflowMode();
    3776            0 :         bool isHcclOpInplace = IsHcclOpInplace(HcclCMDType::HCCL_CMD_REDUCE_SCATTER, opParam, userRank_, userRankSize_,
    3777           15 :                                                isInplaceStatus_);
    3778           21 :         if (aicpuUnfoldMode && retryEnable_ && isHcclOpInplace) {
    3779            0 :             HCCL_DEBUG("The retry with inplace case is expected to be supported, "
    3780              :                        "aicpuUnfoldMode[%d], retryEnable_[%d], isHcclOpInplace[%d], "
    3781              :                        "therefore HcclWorkflowMode is converted from [%d] to HCCL_WORKFLOW_MODE_OP_BASE",
    3782              :                        aicpuUnfoldMode, retryEnable_, isHcclOpInplace, static_cast<u8>(retryOrigWorkflowMode_));
    3783            0 :             CHK_RET(SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE));
    3784              :         }
    3785              : 
    3786           21 :         CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_REDUCE_SCATTER, opParam));
    3787              : 
    3788            9 :         CHK_RET(SetWorkflowMode(retryOrigWorkflowMode_));
    3789            9 :         return HCCL_SUCCESS;
    3790           20 :     }
    3791              : 
    3792           25 :     HcclResult HcclCommunicator::ReduceScatterOutPlace(const std::string &tag, void *inputPtr, void *outputPtr,
    3793              :                                                        u64 count, HcclDataType dataType, HcclReduceOp op, HcclRtStream stream)
    3794              :     {
    3795           25 :         CHK_RET(CheckSuspendingStatus());
    3796           26 :         if (userRankSize_ > 1) {
    3797           25 :             CHK_RET(CreateCommCCLbuffer());
    3798              :         }
    3799              : 
    3800           38 :         bool aicpuUnfoldMode = false;
    3801           38 :         if (GetAicpuUnfoldConfig() == true &&
    3802           16 :             IsSupportSDMAReduce(cclBufferManager_.GetInCCLbuffer().ptr(), cclBufferManager_.GetOutCCLbuffer().ptr(),
    3803           12 :                                 dataType, op) &&
    3804           53 :             (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
    3805            4 :             aicpuUnfoldMode = true;
    3806              :         }
    3807              : 
    3808           37 :         bool isCapture = StreamIsCapture(stream);
    3809              : 
    3810           36 :         if (!IsAtomicInit()) {
    3811            0 :             HCCL_ERROR("[HcclCommunicator][ReduceScatterOutPlace]errNo[0x%016llx] hccl init must be called before"
    3812              :                        " call this function",
    3813              :                        HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    3814            0 :             return HCCL_E_UNAVAIL;
    3815              :         }
    3816              : 
    3817           37 :         Stream streamObj(stream);
    3818           37 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    3819              : 
    3820           37 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    3821           36 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    3822           36 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    3823           36 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    3824              : 
    3825           37 :         u32 perDataSize = SIZE_TABLE[dataType];
    3826              : 
    3827           37 :         OpParam opParam;
    3828           36 :         opParam.tag = tag;
    3829           37 :         opParam.inputPtr = inputPtr;
    3830           37 :         opParam.inputSize = userRankSize_ * count * perDataSize;
    3831           37 :         opParam.outputPtr = outputPtr;
    3832           37 :         opParam.outputSize = count * perDataSize;
    3833           37 :         opParam.DataDes.count = count;
    3834           37 :         opParam.DataDes.dataType = dataType;
    3835           37 :         opParam.reduceType = op;
    3836           37 :         opParam.stream = streamObj;
    3837           36 :         opParam.opType = HcclCMDType::HCCL_CMD_REDUCE_SCATTER;
    3838           36 :         opParam.aicpuUnfoldMode = aicpuUnfoldMode;
    3839           36 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    3840           36 :         opParam.isCapture = isCapture;
    3841           36 :         opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
    3842              : 
    3843           36 :         CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_REDUCE_SCATTER, opParam));
    3844              : 
    3845           11 :         return HCCL_SUCCESS;
    3846           35 :     }
    3847              : 
    3848            0 :     HcclResult HcclCommunicator::ReduceScatterV(const std::string &tag, void *inputPtr,
    3849              :                                                 const void *inputCounts, const void *inputDispls, void *outputPtr, u64 outputCount,
    3850              :                                                 HcclDataType dataType, HcclReduceOp op, HcclRtStream stream, HcomCollOpInfo *opInfo)
    3851              :     {
    3852            0 :         CHK_RET(CheckSuspendingStatus());
    3853            0 :         if (userRankSize_ == 1) {
    3854              :             // rankSize为1时,退化为ReduceScatter
    3855            0 :             return ReduceScatter(tag, inputPtr, outputPtr, outputCount, dataType, op, stream);
    3856              :         }
    3857              : 
    3858            0 :         if (!IsAtomicInit()) {
    3859            0 :             HCCL_ERROR(
    3860              :                 "[HcclCommunicator][ReduceScatterV]errNo[0x%016llx] hccl init must be called before call this function",
    3861              :                 HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    3862            0 :             return HCCL_E_UNAVAIL;
    3863              :         }
    3864              : 
    3865            0 :         Stream streamObj(stream);
    3866            0 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    3867              : 
    3868            0 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    3869            0 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    3870            0 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    3871            0 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    3872              : 
    3873            0 :         const bool aicpuUnfoldMode = GetAicpuUnfoldConfig() &&
    3874            0 :                                      IsSupportSDMAReduce(inputPtr, outputPtr, dataType, op) && (deviceType_ == DevType::DEV_TYPE_910_93);
    3875              : 
    3876            0 :         u32 perDataSize = SIZE_TABLE[dataType];
    3877            0 :         u64 inputSize = 0;
    3878            0 :         const u64 *counts = static_cast<const u64 *>(inputCounts);
    3879            0 :         for (u32 i = 0; i < userRankSize_; i++) {
    3880            0 :             inputSize += counts[i] * perDataSize;
    3881              :         }
    3882            0 :         CHK_PRT_RET(inputSize == 0, HCCL_WARNING("inputSize is 0, return ReduceScatterV success"), HCCL_SUCCESS);
    3883              : 
    3884            0 :         OpParam opParam;
    3885            0 :         opParam.tag = tag;
    3886            0 :         opParam.inputPtr = inputPtr;
    3887            0 :         opParam.inputSize = inputSize;
    3888            0 :         opParam.outputPtr = outputPtr;
    3889            0 :         opParam.outputSize = outputCount * perDataSize;
    3890            0 :         opParam.srcRank = userRank_; // rankId for access counts
    3891            0 :         opParam.VDataDes.counts = const_cast<void *>(inputCounts);
    3892            0 :         opParam.VDataDes.displs = const_cast<void *>(inputDispls);
    3893            0 :         opParam.VDataDes.dataType = dataType;
    3894            0 :         opParam.reduceType = op;
    3895            0 :         opParam.stream = streamObj;
    3896            0 :         opParam.opType = HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V;
    3897            0 :         opParam.aicpuUnfoldMode = aicpuUnfoldMode;
    3898            0 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    3899              : 
    3900            0 :         if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
    3901            0 :             for (u32 i = 0; i < userRankSize_; i++) {
    3902            0 :                 HCCL_CONFIG_DEBUG(HCCL_ALG,
    3903              :                                   "[HcclCommunicator][ReduceScatterV]userRank_[%u], rankIdx[%u], inputCounts[%llu], inputDispls[%llu]",
    3904              :                                   userRank_, i, counts[i], static_cast<const u64 *>(inputDispls)[i]);
    3905              :             }
    3906              :         }
    3907              : 
    3908            0 :         CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V, opParam));
    3909              : 
    3910            0 :         return HCCL_SUCCESS;
    3911            0 :     }
    3912              : 
    3913            0 :     HcclResult HcclCommunicator::ReduceScatterVOutPlace(const std::string &tag, void *inputPtr, void *outputPtr,
    3914              :                                                         const void *inputCounts, const void *inputDispls, u64 outputCount,
    3915              :                                                         HcclDataType dataType, HcclReduceOp op, HcclRtStream stream)
    3916              :     {
    3917            0 :         CHK_RET(CheckSuspendingStatus());
    3918            0 :         if (userRankSize_ == 1) {
    3919              :             // rankSize为1时,退化为ReduceScatter
    3920            0 :             return ReduceScatterOutPlace(tag, inputPtr, outputPtr, outputCount, dataType, op, stream);
    3921              :         }
    3922              : 
    3923            0 :         CHK_RET(CreateCommCCLbuffer());
    3924            0 :         if (!IsAtomicInit()) {
    3925            0 :             HCCL_ERROR("[HcclCommunicator][ReduceScatterVOutPlace]errNo[0x%016llx] hccl init must be called before"
    3926              :                        " call this function",
    3927              :                        HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    3928            0 :             return HCCL_E_UNAVAIL;
    3929              :         }
    3930              : 
    3931            0 :         bool isCapture = StreamIsCapture(stream);
    3932              : 
    3933            0 :         Stream streamObj(stream);
    3934            0 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    3935              : 
    3936            0 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    3937            0 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    3938            0 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    3939            0 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    3940              : 
    3941            0 :         const bool aicpuUnfoldMode = GetAicpuUnfoldConfig() &&
    3942            0 :                                      IsSupportSDMAReduce(inputPtr, outputPtr, dataType, op) && (deviceType_ == DevType::DEV_TYPE_910_93);
    3943              : 
    3944            0 :         u32 perDataSize = SIZE_TABLE[dataType];
    3945            0 :         u64 inputSize = 0;
    3946            0 :         const u64 *counts = static_cast<const u64 *>(inputCounts);
    3947            0 :         for (u32 i = 0; i < userRankSize_; i++) {
    3948            0 :             inputSize += counts[i] * perDataSize;
    3949              :         }
    3950              : 
    3951            0 :         OpParam opParam;
    3952            0 :         opParam.tag = tag;
    3953            0 :         opParam.inputPtr = inputPtr;
    3954            0 :         opParam.inputSize = inputSize;
    3955            0 :         opParam.outputPtr = outputPtr;
    3956            0 :         opParam.outputSize = outputCount * perDataSize;
    3957            0 :         opParam.srcRank = userRank_; // rankId for access counts
    3958            0 :         opParam.VDataDes.counts = const_cast<void *>(inputCounts);
    3959            0 :         opParam.VDataDes.displs = const_cast<void *>(inputDispls);
    3960            0 :         opParam.VDataDes.dataType = dataType;
    3961            0 :         opParam.reduceType = op;
    3962            0 :         opParam.stream = streamObj;
    3963            0 :         opParam.opType = HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V;
    3964            0 :         opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
    3965            0 :         opParam.aicpuUnfoldMode = aicpuUnfoldMode;
    3966            0 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    3967            0 :         opParam.isCapture = isCapture;
    3968            0 :         opParam.rankSize = userRankSize_;
    3969              : 
    3970            0 :         if (UNLIKELY(GetDebugConfig() & HCCL_ALG)) {
    3971            0 :             for (u32 i = 0; i < userRankSize_; i++) {
    3972            0 :                 HCCL_CONFIG_DEBUG(HCCL_ALG,
    3973              :                                   "[HcclCommunicator][ReduceScatterVOutPlace]userRank_[%u],"
    3974              :                                   "rankIdx[%u], inputCounts[%llu], inputDispls[%llu]",
    3975              :                                   userRank_, i, counts[i], static_cast<const u64 *>(inputDispls)[i]);
    3976              :             }
    3977              :         }
    3978              : 
    3979            0 :         CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V, opParam));
    3980              : 
    3981            0 :         return HCCL_SUCCESS;
    3982            0 :     }
    3983              : 
    3984            0 :     HcclResult HcclCommunicator::BatchSendRecv(const std::string &tag, HcclSendRecvItem *sendRecvItemsPtr, u32 itemNum,
    3985              :                                                rtStream_t stream)
    3986              :     {
    3987            0 :         if (!IsAtomicInit()) {
    3988            0 :             HCCL_ERROR(
    3989              :                 "[HcclCommunicator][BatchSendRecv]errNo[0x%016llx] hccl init must be called before call this function",
    3990              :                 HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    3991            0 :             return HCCL_E_UNAVAIL;
    3992              :         }
    3993              : 
    3994            0 :         bool aicpuUnfoldMode = false;
    3995            0 :         if (GetAicpuUnfoldConfig() == true && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
    3996            0 :             aicpuUnfoldMode = true;
    3997              :         }
    3998              : 
    3999            0 :         bool isCapture = StreamIsCapture(stream);
    4000              : 
    4001            0 :         if (!IsAtomicInit()) {
    4002            0 :             HCCL_ERROR(
    4003              :                 "[HcclCommunicator][BatchSendRecv]errNo[0x%016llx] hccl init must be called before call this function",
    4004              :                 HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    4005            0 :             return HCCL_E_UNAVAIL;
    4006              :         }
    4007            0 :         Stream streamObj(stream);
    4008            0 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    4009              : 
    4010            0 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    4011            0 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    4012            0 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    4013            0 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    4014            0 :         OpParam opParam;
    4015            0 :         opParam.tag = tag;
    4016            0 :         opParam.stream = streamObj;
    4017            0 :         opParam.aicpuUnfoldMode = aicpuUnfoldMode;
    4018            0 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    4019            0 :         opParam.isCapture = isCapture;
    4020            0 :         opParam.BatchSendRecvDataDes.sendRecvItemsPtr = sendRecvItemsPtr;
    4021            0 :         opParam.BatchSendRecvDataDes.itemNum = itemNum;
    4022            0 :         opParam.opType = HcclCMDType::HCCL_CMD_BATCH_SEND_RECV;
    4023            0 :         opParam.isGroupMode = isGroupMode_;
    4024            0 :         if (isGroupMode_) {
    4025            0 :             opParam.aicpuUnfoldMode = true; // A2的GroupSendRecv也走aicpu模式
    4026              :         }
    4027              : 
    4028            0 :         CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_BATCH_SEND_RECV, opParam));
    4029              : 
    4030            0 :         return HCCL_SUCCESS;
    4031            0 :     }
    4032              : 
    4033            0 :     HcclResult HcclCommunicator::Send(const std::string &tag, void *inputPtr, u64 count, HcclDataType dataType,
    4034              :                                       u32 destRank, rtStream_t stream, u32 srTag, u32 localGroupRank)
    4035              :     {
    4036            0 :         CHK_RET(CheckSuspendingStatus());
    4037            0 :         bool aicpuUnfoldMode = false;
    4038            0 :         if (GetAicpuUnfoldConfig() == true && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
    4039            0 :             aicpuUnfoldMode = true;
    4040              :         }
    4041              : 
    4042            0 :         if (!IsAtomicInit()) {
    4043            0 :             HCCL_ERROR("[HcclCommunicator][Send]errNo[0x%016llx] hccl init must be called before call this function",
    4044              :                        HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    4045            0 :             return HCCL_E_UNAVAIL;
    4046              :         }
    4047              : 
    4048            0 :         Stream streamObj(stream);
    4049            0 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    4050              : 
    4051            0 :         u32 perDataSize = SIZE_TABLE[dataType];
    4052            0 :         u64 totalSize = count * perDataSize;
    4053              : 
    4054            0 :         OpParam opParam;
    4055            0 :         opParam.tag = tag;
    4056            0 :         opParam.inputPtr = inputPtr;
    4057            0 :         opParam.inputSize = totalSize;
    4058            0 :         opParam.outputPtr = inputPtr;
    4059            0 :         opParam.outputSize = totalSize;
    4060            0 :         opParam.DataDes.count = count;
    4061            0 :         opParam.DataDes.dataType = dataType;
    4062            0 :         opParam.stream = streamObj;
    4063            0 :         opParam.aicpuUnfoldMode = aicpuUnfoldMode;
    4064            0 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    4065            0 :         opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
    4066            0 :         opParam.dstRank = destRank;
    4067            0 :         opParam.opType = HcclCMDType::HCCL_CMD_SEND;
    4068            0 :         opParam.srTag = srTag;
    4069            0 :         opParam.localGroupRank = localGroupRank;
    4070            0 :         CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_SEND, opParam));
    4071              : 
    4072            0 :         return HCCL_SUCCESS;
    4073            0 :     }
    4074              : 
    4075            0 :     HcclResult HcclCommunicator::SendOutPlace(const std::string &tag, void *inputPtr, u64 count, HcclDataType dataType,
    4076              :                                               u32 destRank, rtStream_t stream)
    4077              :     {
    4078            0 :         CHK_RET(CheckSuspendingStatus());
    4079            0 :         bool aicpuUnfoldMode = false;
    4080            0 :         if (GetAicpuUnfoldConfig() == true && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
    4081            0 :             aicpuUnfoldMode = true;
    4082              :         }
    4083              : 
    4084            0 :         if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
    4085            0 :             HCCL_ERROR("[%s][%s]SendOutPlace is not supported",
    4086              :                 LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_NOT_SUPPORTED.c_str());
    4087            0 :             return HCCL_E_NOT_SUPPORT;
    4088              :         }
    4089            0 :         if (!IsAtomicInit()) {
    4090            0 :             HCCL_ERROR(
    4091              :                 "[HcclCommunicator][SendOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
    4092              :                 HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    4093            0 :             return HCCL_E_UNAVAIL;
    4094              :         }
    4095              : 
    4096            0 :         bool isCapture = StreamIsCapture(stream);
    4097              : 
    4098            0 :         Stream streamObj(stream);
    4099            0 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    4100              : 
    4101            0 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    4102            0 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    4103            0 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    4104            0 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    4105              : 
    4106            0 :         u32 perDataSize = SIZE_TABLE[dataType];
    4107            0 :         u64 totalSize = count * perDataSize;
    4108              : 
    4109            0 :         OpParam opParam;
    4110            0 :         opParam.tag = tag;
    4111            0 :         opParam.inputPtr = inputPtr;
    4112            0 :         opParam.inputSize = totalSize;
    4113            0 :         opParam.outputPtr = inputPtr;
    4114            0 :         opParam.outputSize = totalSize;
    4115            0 :         opParam.DataDes.count = count;
    4116            0 :         opParam.DataDes.dataType = dataType;
    4117            0 :         opParam.stream = streamObj;
    4118            0 :         opParam.aicpuUnfoldMode = aicpuUnfoldMode;
    4119            0 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    4120            0 :         opParam.isCapture = isCapture;
    4121            0 :         opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
    4122            0 :         opParam.dstRank = destRank;
    4123            0 :         opParam.opType = HcclCMDType::HCCL_CMD_SEND;
    4124            0 :         opParam.localGroupRank = userRank_;
    4125            0 :         CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_SEND, opParam));
    4126              : 
    4127            0 :         return HCCL_SUCCESS;
    4128            0 :     }
    4129              : 
    4130            0 :     HcclResult HcclCommunicator::Receive(const std::string &tag, void *outputPtr, u64 count, HcclDataType dataType,
    4131              :                                          u32 srcRank, rtStream_t stream, u32 srTag, u32 localGroupRank)
    4132              :     {
    4133            0 :         CHK_RET(CheckSuspendingStatus());
    4134            0 :         bool aicpuUnfoldMode = false;
    4135            0 :         if (GetAicpuUnfoldConfig() == true && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
    4136            0 :             aicpuUnfoldMode = true;
    4137              :         }
    4138              : 
    4139            0 :         if (!IsAtomicInit()) {
    4140            0 :             HCCL_ERROR("[HcclCommunicator][Receive]errNo[0x%016llx] hccl init must be called before call this function",
    4141              :                        HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    4142            0 :             return HCCL_E_UNAVAIL;
    4143              :         }
    4144              : 
    4145            0 :         Stream streamObj(stream);
    4146            0 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    4147              : 
    4148            0 :         u32 perDataSize = SIZE_TABLE[dataType];
    4149            0 :         u64 totalSize = count * perDataSize;
    4150              : 
    4151            0 :         OpParam opParam;
    4152            0 :         opParam.tag = tag;
    4153            0 :         opParam.inputPtr = outputPtr;
    4154            0 :         opParam.inputSize = totalSize;
    4155            0 :         opParam.outputPtr = outputPtr;
    4156            0 :         opParam.outputSize = totalSize;
    4157            0 :         opParam.DataDes.count = count;
    4158            0 :         opParam.DataDes.dataType = dataType;
    4159            0 :         opParam.stream = streamObj;
    4160            0 :         opParam.aicpuUnfoldMode = aicpuUnfoldMode;
    4161            0 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    4162            0 :         opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
    4163            0 :         opParam.srcRank = srcRank;
    4164            0 :         opParam.opType = HcclCMDType::HCCL_CMD_RECEIVE;
    4165            0 :         opParam.srTag = srTag;
    4166            0 :         opParam.localGroupRank = localGroupRank;
    4167            0 :         CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_RECEIVE, opParam));
    4168              : 
    4169            0 :         return HCCL_SUCCESS;
    4170            0 :     }
    4171              : 
    4172            0 :     HcclResult HcclCommunicator::ReceiveOutPlace(const std::string &tag, void *outputPtr, u64 count,
    4173              :                                                  HcclDataType dataType, u32 srcRank, rtStream_t stream)
    4174              :     {
    4175            0 :         CHK_RET(CheckSuspendingStatus());
    4176            0 :         bool aicpuUnfoldMode = false;
    4177            0 :         if (GetAicpuUnfoldConfig() == true && (deviceType_ == DevType::DEV_TYPE_910_93) && (userRankSize_ != 1)) {
    4178            0 :             aicpuUnfoldMode = true;
    4179              :         }
    4180              : 
    4181            0 :         if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
    4182            0 :             HCCL_ERROR("[%s][%s]ReceiveOutPlace is not supported",
    4183              :                 LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_NOT_SUPPORTED.c_str());
    4184            0 :             return HCCL_E_NOT_SUPPORT;
    4185              :         }
    4186            0 :         if (!IsAtomicInit()) {
    4187            0 :             HCCL_ERROR(
    4188              :                 "[HcclCommunicator][ReceiveOutPlace]errNo[0x%016llx] hccl init must be called before call this function",
    4189              :                 HCCL_ERROR_CODE(HCCL_E_UNAVAIL));
    4190            0 :             return HCCL_E_UNAVAIL;
    4191              :         }
    4192              : 
    4193            0 :         bool isCapture = StreamIsCapture(stream);
    4194              : 
    4195            0 :         Stream streamObj(stream);
    4196            0 :         CHK_RET(callbackTask_->CallbackRegStream(stream));
    4197              : 
    4198            0 :         std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    4199            0 :         std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    4200            0 :         implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts,
    4201            0 :             vnicRanksPorts, isSetHDCModeInfo_, isUseRankPort_);
    4202              : 
    4203            0 :         u32 perDataSize = SIZE_TABLE[dataType];
    4204            0 :         u64 totalSize = count * perDataSize;
    4205              : 
    4206            0 :         OpParam opParam;
    4207            0 :         opParam.tag = tag;
    4208            0 :         opParam.inputPtr = outputPtr;
    4209            0 :         opParam.inputSize = totalSize;
    4210            0 :         opParam.outputPtr = outputPtr;
    4211            0 :         opParam.outputSize = totalSize;
    4212            0 :         opParam.DataDes.count = count;
    4213            0 :         opParam.DataDes.dataType = dataType;
    4214            0 :         opParam.stream = streamObj;
    4215            0 :         opParam.aicpuUnfoldMode = aicpuUnfoldMode;
    4216            0 :         opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    4217            0 :         opParam.isCapture = isCapture;
    4218            0 :         opParam.opBaseAtraceInfo = opBaseAtraceInfo_.get();
    4219            0 :         opParam.srcRank = srcRank;
    4220            0 :         opParam.opType = HcclCMDType::HCCL_CMD_RECEIVE;
    4221            0 :         opParam.localGroupRank = userRank_;
    4222            0 :         CHK_RET(ExecOp(HcclCMDType::HCCL_CMD_RECEIVE, opParam));
    4223              : 
    4224            0 :         return HCCL_SUCCESS;
    4225            0 :     }
    4226              : 
    4227            0 :     HcclResult HcclCommunicator::RegressCalPreOp(AlltoAllOperator *&alltoAllOperator, const OpParam &opParam,
    4228              :                                                  std::unique_ptr<PreProcessMetaInfo> &preMetaInfo)
    4229              :     {
    4230            0 :         HCCL_INFO("Run with Graph, alloc new stream");
    4231            0 :         Stream stream(StreamType::STREAM_TYPE_ONLINE);
    4232            0 :         return RegressCalPreOp(alltoAllOperator, opParam, preMetaInfo, stream);
    4233            0 :     }
    4234              : 
    4235            0 :     HcclResult HcclCommunicator::RegressCalPreOp(AlltoAllOperator *&alltoAllOperator, const OpParam &opParam,
    4236              :                                                  std::unique_ptr<PreProcessMetaInfo> &preMetaInfo, Stream &preProcessStream)
    4237              :     {
    4238            0 :         OpParam preProcessOpParam;
    4239            0 :         HcclWorkflowMode mode = GetWorkflowMode();
    4240            0 :         CHK_PRT_RET(mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_RESERVED, HCCL_ERROR("Invalid Workflow Mode[%d]", mode), HCCL_E_INTERNAL);
    4241              : 
    4242              :         // h to d
    4243            0 :         CHK_RET(SetInfoToDevice(preMetaInfo, preProcessStream));
    4244              :         // opParam准备
    4245            0 :         CHK_RET(alltoAllOperator->PreparePreOpParam(preProcessOpParam, preMetaInfo, preProcessStream));
    4246              : 
    4247              :         // 回归调用其它算子
    4248            0 :         HCCL_INFO("[HcclCommunicator][RegressCalPreOp] Regression calls other operators and opType[%u]",
    4249              :                   preMetaInfo->opType);
    4250            0 :         CHK_RET(ExecOp(preMetaInfo->opType, preProcessOpParam));
    4251            0 :         CHK_RET(hcclStreamSynchronize(preProcessStream.ptr(), commConfig_.GetConfigExecTimeOut()));
    4252            0 :         HCCL_DEBUG("[HcclCommunicator][RegressCalPreOp] preProcess tag[%s].", preProcessOpParam.tag.c_str());
    4253            0 :         SetWorkflowMode(mode);
    4254              : 
    4255              :         // d to h
    4256            0 :         HostMem hostCollectBuffer = HostMem::alloc(preMetaInfo->outputSize);
    4257            0 :         CHK_PTR_NULL(hostCollectBuffer.ptr());
    4258            0 :         CHK_RET(GetInfoFromDevice(preMetaInfo, mode, hostCollectBuffer));
    4259              : 
    4260            0 :         hostCollectBuffer_ = hostCollectBuffer;
    4261            0 :         alltoAllOperator->SetPreProcessResult(std::move(hostCollectBuffer));
    4262            0 :         HCCL_INFO("[HcclCommunicator][RegressCalPreOp] run success!");
    4263            0 :         return HCCL_SUCCESS;
    4264            0 :     }
    4265              : 
    4266            0 :     HcclResult HcclCommunicator::SaveRankInfoHasLinked(const AlgResourceRequest& resRequest)
    4267              :     {
    4268            0 :         for (auto &levelNSubCommTransport : resRequest.opTransport) {
    4269            0 :             for (auto &singleSubCommTransport : levelNSubCommTransport) {
    4270            0 :                 for (auto &transportRequest : singleSubCommTransport.transportRequests) {
    4271            0 :                     if (transportRequest.isValid) {
    4272            0 :                         ranksLinked_.insert(transportRequest.remoteUserRank);
    4273            0 :                         HCCL_INFO("[HcclCommunicator][SaveRankInfoHasLinked]Insert remote Rank[%u] to ranksLinked Set.",
    4274              :                             transportRequest.remoteUserRank);
    4275              :                     }
    4276              :                 }
    4277              :             }
    4278              :         }
    4279              : 
    4280            0 :         return HCCL_SUCCESS;
    4281              :     }
    4282              : 
    4283            2 :     HcclResult HcclCommunicator::GetCacheMap(std::unique_ptr<CollAlgOperator>& algOperator , OpParam& opParam,
    4284              :         AlgType& algType, bool selectAivAlg, std::string& newTag)
    4285              :     {
    4286            2 :         HcclCacheInfo cacheInfo;
    4287            2 :         CHK_RET(algOperator->GetCache(cacheInfo));
    4288            2 :         if (cacheInfo.isUseCache == false) {
    4289            2 :             return HCCL_SUCCESS;
    4290              :         }
    4291            0 :         cacheInfo.algType = algType;
    4292            0 :         cacheInfo.selectAivAlg = selectAivAlg;
    4293            0 :         cacheInfo.newTag = newTag;
    4294              : 
    4295            0 :         if (hcclCacheMap_.size() > CACHEMAP_MAXSIZE) {
    4296            0 :             size_t clearCount = static_cast<size_t>(CACHEMAP_MAXSIZE * CACHEMAP_CLEARPERCENT);
    4297            0 :             for (auto it = hcclCacheMap_.begin(); clearCount > 0 && it != hcclCacheMap_.end(); clearCount--) {
    4298            0 :                 it = hcclCacheMap_.erase(it);
    4299              :             }
    4300              :         }
    4301              : 
    4302            0 :         hcclCacheMap_.emplace(std::make_pair(opParam, std::move(cacheInfo)));
    4303              : 
    4304            0 :         HCCL_INFO("[HcclCommunicator][GetCacheMap] algType %s, selectAivAlg %d, newTag %s", AlgTypeToStr(algType).c_str(),
    4305              :             selectAivAlg, newTag.c_str());
    4306            0 :         return HCCL_SUCCESS;
    4307            2 :     }
    4308              : 
    4309            0 :     HcclResult HcclCommunicator::ExecOpCache(HcclCMDType opType, OpParam &opParam, HcclCacheInfo& cacheInfo)
    4310              :     {
    4311              :         //可用核数也需要作为key的一部分,防止cache中拿出来的和计算出来的实际核数不一致
    4312              :         //cache目前仅支持executor的kernel为1的情况
    4313            0 :         cacheInfo.resourceArgs.buffersIn = cacheInfo.buffersIn;
    4314            0 :         cacheInfo.resourceArgs.buffersOut = cacheInfo.buffersOut;
    4315            0 :         cacheInfo.resourceArgs.stream = opParam.stream.ptr(); // 刷新cache下发的stream
    4316            0 :         cacheInfo.opArgs.input = opParam.inputPtr;
    4317            0 :         cacheInfo.opArgs.output = opParam.outputPtr;
    4318            0 :         AlgType& algType = cacheInfo.algType;
    4319            0 :         bool selectAivAlg = cacheInfo.selectAivAlg;
    4320            0 :         std::string newTag = cacheInfo.newTag;
    4321            0 :         HcclResult ret = HCCL_SUCCESS;
    4322              :         //更新aivtag
    4323            0 :         GetAivTag(1, opParam.isCapture, cacheInfo.resourceArgs.aivTag);
    4324            0 :         HCCL_INFO("[HcclCommunicator][ExecOpCache]buffersIn[%p] buffersOut[%p] tag[%s] opType[%d] "
    4325              :             "deterministic [%u] count[%llu] op[%d] userRank[%u] aiv tag [%d] stream [%d]",
    4326              :             cacheInfo.buffersIn, cacheInfo.buffersOut, identifier_.c_str(), opType, opParam.deterministic,
    4327              :             cacheInfo.opArgs.count, cacheInfo.opArgs.op, userRank_, cacheInfo.resourceArgs.aivTag, opParam.stream.id());
    4328            0 :         CHK_RET(HandleAclGraphFirstOpAivBuff(opParam.stream.ptr()));
    4329              :         //保留dfx
    4330            0 :         CHK_RET(RegisterDfxInfo(opParam, algType, resMap_[newTag].slaveStreams, selectAivAlg));
    4331              :         // 头计数
    4332            0 :         CHK_RET(StarsCounter(dispatcher_, opParam.stream, HEAD, opParam.aicpuUnfoldMode, retryEnable_, selectAivAlg));
    4333            0 :         u64 dataSize = (opParam.opType == HcclCMDType::HCCL_CMD_ALLTOALL ?
    4334            0 :             opParam.All2AllDataDes.sendCount * SIZE_TABLE[opParam.All2AllDataDes.sendType] : 0);
    4335            0 :         if (opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V || opType == HcclCMDType::HCCL_CMD_ALLGATHER_V ||
    4336            0 :             (opType == HcclCMDType::HCCL_CMD_ALLTOALL && dataSize >= AIV_ALL_TO_ALL_BIG_SIZE)) {
    4337            0 :             ret = ExecuteKernelLaunch(cacheInfo.opArgs, cacheInfo.topoArgs, cacheInfo.resourceArgs,
    4338            0 :                 cacheInfo.algArgs, cacheInfo.extraArgs, cacheInfo.profilingInfo);
    4339              :         } else {
    4340            0 :             ret = ExecuteKernelLaunch(cacheInfo.opArgs, cacheInfo.topoArgs, cacheInfo.resourceArgs,
    4341            0 :                 cacheInfo.algArgs, cacheInfo.profilingInfo);
    4342              :         }
    4343              :         //刷新核数
    4344            0 :         numBlocks_ = cacheInfo.resourceArgs.numBlocks;
    4345            0 :         CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[ExecOpCache]launch aiv failed, return[%d]", ret), ret);
    4346            0 :         CHK_RET(StarsCounter(dispatcher_, opParam.stream, TAIL, opParam.aicpuUnfoldMode, retryEnable_, selectAivAlg));
    4347            0 :         CHK_RET(UnRegisterDfxInfo(opParam, resMap_[newTag].slaveStreams));
    4348            0 :         if (selectAivAlg) {
    4349            0 :             aivClearEnable_ = false;
    4350              :         }
    4351            0 :         return HCCL_SUCCESS;
    4352            0 :     }
    4353              : 
    4354            1 :     void HcclCommunicator::SplitBsrData(OpParam &opParam, std::vector<u8>& isDirectRemoteRank,
    4355              :         std::vector<HcclSendRecvItem>& hostSendRecvInfo, std::vector<HcclSendRecvItem>& aicpuSendRecvInfo)
    4356              :     {
    4357            1 :         u32 itemNum = opParam.BatchSendRecvDataDes.itemNum;
    4358            1 :         isDirectRemoteRank.resize(userRankSize_);
    4359            1 :         HCCL_INFO("[HcclCommunicator][SplitBsrData] rankSize %u", userRankSize_);
    4360            1 :         HcclSendRecvItem* sendRecvInfo = opParam.BatchSendRecvDataDes.sendRecvItemsPtr;
    4361            3 :         for (u32 i = 0; i < itemNum; i++) {
    4362            2 :             if (sendRecvInfo->buf == nullptr) {
    4363            2 :                 sendRecvInfo++;
    4364            2 :                 continue;
    4365              :             }
    4366            0 :             if (remoteTransportMap_[sendRecvInfo->remoteRank] == TransportType::TRANS_TYPE_DEVICE_DIRECT) {
    4367              :                 //host 侧需要下发的数据
    4368            0 :                 HCCL_INFO("[HcclCommunicator][SplitBsrData]host localRank %u remoteRank %u type %d sendRecvType %d count %llu",
    4369              :                     userRank_, sendRecvInfo->remoteRank, remoteTransportMap_[sendRecvInfo->remoteRank],
    4370              :                     sendRecvInfo->sendRecvType, sendRecvInfo->count);
    4371            0 :                 isDirectRemoteRank[sendRecvInfo->remoteRank] = true;
    4372            0 :                 hostSendRecvInfo.push_back(*sendRecvInfo);
    4373              :             } else {
    4374              :                 //aicpu侧需要下发的数据
    4375            0 :                 HCCL_INFO("[HcclCommunicator][SplitBsrData]aicpu localRank %u remoteRank %u type %d sendRecvType %d count %llu",
    4376              :                     userRank_, sendRecvInfo->remoteRank, remoteTransportMap_[sendRecvInfo->remoteRank],
    4377              :                     sendRecvInfo->sendRecvType, sendRecvInfo->count);
    4378            0 :                 isDirectRemoteRank[sendRecvInfo->remoteRank] = false;
    4379            0 :                 aicpuSendRecvInfo.push_back(*sendRecvInfo);
    4380              :             }
    4381            0 :             sendRecvInfo++;
    4382              :         }
    4383            1 :         HCCL_INFO("[HcclCommunicator][SplitBsrData] itemNum %u hostItemNum %zu aicpuItemNum %zu", itemNum, hostSendRecvInfo.size(),
    4384              :             aicpuSendRecvInfo.size());
    4385            1 :         return;
    4386              :     }
    4387              : 
    4388            0 :     bool HcclCommunicator::IsReduceWithInt64OrProd(HcclCMDType opType, const OpParam &opParam) const
    4389              :     {
    4390            0 :         if (opType == HcclCMDType::HCCL_CMD_ALLREDUCE || opType == HcclCMDType::HCCL_CMD_REDUCE ||
    4391              :             opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER) {
    4392            0 :             if (opParam.reduceType == HcclReduceOp::HCCL_REDUCE_PROD ||
    4393            0 :                 opParam.DataDes.dataType == HcclDataType::HCCL_DATA_TYPE_INT64) {
    4394            0 :                 return true;
    4395              :             }
    4396              :         }
    4397              : 
    4398            0 :         if (opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V) {
    4399            0 :             if (opParam.reduceType == HcclReduceOp::HCCL_REDUCE_PROD ||
    4400            0 :                 opParam.VDataDes.dataType == HcclDataType::HCCL_DATA_TYPE_INT64) {
    4401            0 :                 return true;
    4402              :             }
    4403              :         }
    4404            0 :         return false;
    4405              :     }
    4406              : 
    4407           69 :     HcclResult HcclCommunicator::ExecOp(HcclCMDType opType, OpParam &opParam, bool isCustom)
    4408              :     {
    4409           69 :         CHK_PRT_RET(isInvalidComm_,
    4410              :             HCCL_ERROR("[HcclCommunicator][%s] comm[%s], rank[%u], devId[%d], snapshot recoverying, "
    4411              :             "this comm is invalid, no operator is allowed to execute.",
    4412              :             __func__, identifier_.c_str(), userRank_, deviceLogicId_), HCCL_E_UNAVAIL);
    4413              : 
    4414           69 :         if (retryEnable_ && needWarnAboutReduceProdInt64_ && IsReduceWithInt64OrProd(opType, opParam)) {
    4415            0 :             HCCL_RUN_WARNING("[HcclCommunicator][%s]comm[%s], opType[%d], reduceType[%d]. Reduce operators with prod operation or int64 data type. This operator type unsupportd for AICPU mode, retry disabled",
    4416              :                              __func__, identifier_.c_str(), opType, opParam.reduceType);
    4417            0 :             needWarnAboutReduceProdInt64_ = false;
    4418              :         }
    4419           69 :         std::string tag = opParam.tag;
    4420           73 :         u32 aivCoreLimit = numBlocks_;
    4421              :         //单机AIV场景下cache复用,提升下发性能
    4422           73 :         if (implAlg_->GetAivModeConfig() && GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
    4423            0 :             if (aivCoreLimit == 0) {
    4424            0 :                 aclError acl_ret = aclrtGetResInCurrentThread(ACL_RT_DEV_RES_VECTOR_CORE, &aivCoreLimit);
    4425            0 :                 CHK_PRT_RET(acl_ret != ACL_SUCCESS,
    4426              :                     HCCL_ERROR("[HcclCommunicator][ExecOp] aclrtGetResInCurrentThread failed, ret=[%d]", acl_ret),
    4427              :                     HCCL_E_PARA);
    4428              :             }
    4429            0 :             opParam.deterministic = implAlg_->GetDeterministicConfig();
    4430            0 :             opParam.aivCoreLimit = aivCoreLimit;
    4431            0 :             auto it = hcclCacheMap_.find(opParam);
    4432            0 :             if (it != hcclCacheMap_.end()) {
    4433            0 :                 CHK_RET(ExecOpCache(opType, opParam, it->second));
    4434            0 :                 return HCCL_SUCCESS;
    4435              :             }
    4436              :         }
    4437              : 
    4438           68 :         ForceProf(opParam.isCapture);
    4439           66 :         opParam.supportSymmetricMemory = IsSupportSymmetricMemory(opType, opParam);
    4440           73 :         opParam.supportZeroCopy = !opParam.supportSymmetricMemory && IsSupportZeroCopy(opParam);
    4441           71 :         opParam.aclGraphZeroCopyEnable = GetConfigAclGraphZeroCopyEnable();
    4442           72 :         bool isInGraphCaptureZeroCopy = false;
    4443           72 :         zeroCopyAclGraph_->SetRetryEnable(retryEnable_);
    4444           68 :         isInGraphCaptureZeroCopy = zeroCopyAclGraph_->SetAclGraphZeroCopyMode(
    4445              :             deviceType_, opType, opParam, implAlg_.get(), cclBufferManager_.GetOutCCLbufferSize());
    4446           73 :         if (isInGraphCaptureZeroCopy && userRankSize_ > 1) {
    4447            0 :             CHK_RET(CreateCommCCLbuffer());
    4448              :         }
    4449           73 :         if (isShareComm_) {
    4450            0 :             CHK_RET(ShareCCLbufferMgr::GetInstance().CheckCCLbuffConflict(cclBuffName_, opParam.stream.id()));
    4451              :         }
    4452           73 :         std::unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(opType);
    4453           67 :         CHK_SMART_PTR_NULL(algOperator);
    4454              :         // 算法选择
    4455           67 :         std::string algName;
    4456           67 :         std::string newTag;
    4457           67 :         if (opParam.aicpuUnfoldMode) {
    4458              :             // 用于inplace支持重执行判断
    4459            3 :             CHK_RET(algOperator->SetRetryEnable(retryEnable_));
    4460              :         }
    4461           67 :         if (GetExternalInputHcclAivMode()) {
    4462              :             // 用于判断图模式是否清零
    4463            0 :             CHK_RET(algOperator->SetAivClearEnable(aivClearEnable_));
    4464              :         }
    4465              : 
    4466           67 :         std::unique_lock<std::mutex> lock(commResMutex_);
    4467           72 :         ResourceLimit limit;
    4468           72 :         limit.ifLimit = true;
    4469           72 :         limit.aivCoreLimit = aivCoreLimit;
    4470           72 :         AlgDesc algDesc;
    4471           70 :         algDesc.isLastSelect = true;
    4472           70 :         CHK_RET(algOperator->SelectAlg(opParam.tag, opParam, limit, algName, algDesc, newTag));
    4473           68 :         if (isOnlyAiv_ && !algDesc.isAivMode) {
    4474            0 :             std::string opTypeName = GetCMDTypeEnumStr(opType);
    4475            0 :             HCCL_ERROR("[HcclCommunicator][ExecOp] opType[%s] currently do not select aiv mode, aiv only not support.",
    4476              :                 opTypeName.c_str());
    4477            0 :             return HCCL_E_NOT_SUPPORT;
    4478            0 :         }
    4479           68 :         CHK_RET(PrepareZeroCopy(algName, algDesc, opParam));
    4480              : 
    4481           70 :         if (opParam.isCapture) {
    4482              :             // aclgraph使用新的Tag,避免影响其他操作
    4483            0 :             newTag += "_Capture";
    4484              :             // aclgraph零拷贝场景下,每个算子都有单独的tag,需要记录,在graph销毁时清理相关资源
    4485            0 :             if (isInGraphCaptureZeroCopy) {
    4486            0 :                 CHK_RET(AclgraphCallback::GetInstance().InsertNewTagToCaptureResMap(this, newTag, opParam));
    4487            0 :                 tagsRequiringHostCleanup_.insert(newTag);
    4488              :             }
    4489              :         }
    4490              : 
    4491           70 :         if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE && userRankSize_ > 1) {
    4492           40 :             CHK_RET(CreateCommCCLbuffer());
    4493              :         }
    4494           73 :         if (hcclNslbDp::GetInstance().GetGlobalCommTaskId() != 0) {
    4495            0 :             NslbDp_CollectOperTable(opType, opParam, algOperator->GetAlgType(), algName);
    4496              :         }
    4497              : 
    4498              :         // 资源创建
    4499           73 :         if ((resMap_.find(newTag) != resMap_.end()) && opParam.isCapture) {
    4500            0 :             AlgResourceRequest resRequest;
    4501            0 :             CHK_RET(algOperator->CalcResRequest(algName, opParam, resRequest));
    4502            0 :             if (HasRoceTransportLinks(resRequest.opTransport)) {
    4503            0 :                 auto resTmp = resMap_[newTag];
    4504            0 :                 ++captureCnt_;
    4505            0 :                 newTag += std::to_string(captureCnt_);
    4506            0 :                 resMap_[newTag] = resTmp;
    4507            0 :                 resRequest.isInGraphCaptureZeroCopy = isInGraphCaptureZeroCopy;
    4508            0 :                 CHK_RET(CleanTransportLinks(resRequest.opTransport, resMap_[newTag].opTransportResponse));
    4509            0 :                 if (IsEnableBackupLink()) {
    4510            0 :                     CHK_RET(CleanTransportLinks(resRequest.opTransport, resMap_[newTag].opTransportResponseBackUp));
    4511              :                 }
    4512              :                 // 记录指令信息用于一致性校验
    4513            0 :                 CHK_RET(RecordOpPara(opType, opParam));
    4514            0 :                 CHK_RET(IncreAllocLink(newTag, opParam, resRequest, resMap_[newTag]));
    4515              :                 // 移除tag对应的指令信息
    4516            0 :                 CHK_RET(RankConsistentcyChecker::GetInstance().DelOpPara(opParam.tag));
    4517              :                 // aclgraph零拷贝场景下,除第一个capture外,需要记录,在graph销毁时清理相关资源
    4518            0 :                 CHK_RET(AclgraphCallback::GetInstance().InsertNewTagToCaptureResMap(this, newTag, opParam));
    4519            0 :                 tagsRequiringHostCleanup_.insert(newTag);
    4520            0 :             }
    4521            0 :         }
    4522           72 :         InsertNewTagToTagMap(newTag, opParam.tag);
    4523           73 :         bool needIncreLink = false;
    4524              :         // aiv算法不需要申请host和device侧的从流
    4525           73 :         bool selectAivAlg = algDesc.isAivMode;
    4526           73 :         if (resMap_.find(newTag) == resMap_.end()) {
    4527           73 :             AlgResourceRequest resRequest;
    4528           71 :             CHK_RET(algOperator->CalcResRequest(algName, opParam, resRequest));
    4529           72 :             if (opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV) {
    4530            0 :                 CHK_RET(SaveRankInfoHasLinked(resRequest));
    4531              :             }
    4532           72 :             resRequest.isInGraphCaptureZeroCopy = isInGraphCaptureZeroCopy;
    4533           72 :             CHK_RET(RecordOpPara(opType, opParam));
    4534           72 :             HcclResult ret = AllocAlgResource(newTag, opType, opParam, resRequest, resMap_[newTag], selectAivAlg);
    4535           73 :             CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[HcclCommunicator][ExecOp] AllocAlgResource failed, algName=[%s]", algName.c_str()), ret);
    4536           20 :             CHK_RET(RankConsistentcyChecker::GetInstance().DelOpPara(opParam.tag));
    4537              : 
    4538              :             // 对于91093超节点内aiv跨机通信算子,将不同机的CCLbuffer地址存在约定好的aiv将读取的HBM位置
    4539           20 :             CHK_RET(algOperator->PrepareCommInfoToDevice(algName, resMap_[newTag]));
    4540              : 
    4541           20 :             if (!isHaveCpuRank_) {
    4542           20 :                 if (isUseRankPort_) {
    4543           20 :                     std::vector<u32> &nicPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    4544           20 :                     std::vector<u32> &vnicPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    4545           20 :                     Heartbeat::GetInstance(deviceLogicId_).SetRankPortInfo(isUseRankPort_, nicPorts, vnicPorts, commPortConfig_.devPortSwitchOn);
    4546              :                 }
    4547              :                 // 开始注册心跳
    4548           20 :                 if (opType == HcclCMDType::HCCL_CMD_SEND) {
    4549            0 :                     CHK_RET(RegisterToHeartBeat(opParam.dstRank, tag));
    4550            0 :                     hbSendRecvTags_.emplace(tag);
    4551           20 :                 } else if (opType == HcclCMDType::HCCL_CMD_RECEIVE) {
    4552            0 :                     CHK_RET(RegisterToHeartBeat(opParam.srcRank, tag));
    4553            0 :                     hbSendRecvTags_.emplace(tag);
    4554              :                 } else {
    4555           20 :                     CHK_RET(RegisterToHeartBeat());
    4556              :                 }
    4557              :             }
    4558           19 :             CHK_RET(UpdateZeroCopy(opParam, resMap_[newTag]));
    4559           73 :         } else if (opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV) {
    4560              :             // batchsendrecv需要根据任务来确定和哪些卡建链,因此复用tag,并在此基础上实现增量建链
    4561            0 :             AlgResourceRequest resRequest;
    4562            0 :             CHK_RET(algOperator->CalcIncreLinkRequest(algName, opParam, ranksLinked_, resRequest, needIncreLink));
    4563            0 :             if (needIncreLink) {
    4564            0 :                 CHK_RET(RecordOpPara(opType, opParam));
    4565            0 :                 CHK_RET(IncreAllocLink(newTag, opParam, resRequest, resMap_[newTag]));
    4566            0 :                 CHK_RET(RankConsistentcyChecker::GetInstance().DelOpPara(opParam.tag));
    4567            0 :                 opParam.needIncreLink = true;
    4568              :             }
    4569            0 :         }
    4570              : 
    4571              :         // 算法执行
    4572           19 :         if (selectAivAlg) {
    4573            0 :             CHK_RET(HandleAclGraphFirstOpAivBuff(opParam.stream.ptr()));
    4574            0 :             if (aivClearEnable_) {
    4575              :                 // 用于判断图模式是否清零
    4576            0 :                 CHK_RET(algOperator->SetAivClearEnable(aivClearEnable_));
    4577            0 :                 aivOffloadTag_ = 1;
    4578              :             }
    4579            0 :             GetAivTag(algDesc.aivTagNum, opParam.isCapture, opParam.aivTag);
    4580            0 :             HCCL_INFO("[HcclCommunicator][ExecOp] tag[%s] userRank[%u] cur aiv tag [%d]",
    4581              :                 identifier_.c_str(), userRank_, opParam.aivTag);
    4582            0 :             opParam.aicpuUnfoldMode = false;
    4583            0 :             opParam.aicpuCacheEnable = 0;
    4584            0 :             CHK_RET(algOperator->SetNumBlocks(aivCoreLimit));
    4585              :         }
    4586           19 :         std::vector<HcclSendRecvItem> hostSendRecvInfo;
    4587           19 :         std::vector<HcclSendRecvItem> aicpuSendRecvInfo;
    4588           19 :         std::vector<u8> isDirectRemoteRank;
    4589           19 :         if (opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV && deviceType_ == DevType::DEV_TYPE_910_93) {
    4590            0 :             SplitBsrData(opParam, isDirectRemoteRank, hostSendRecvInfo, aicpuSendRecvInfo);
    4591              :             // A3 bsr记录Direct下发方式数据
    4592            0 :             opParam.BatchSendRecvDataDes.isDirectRemoteRank = isDirectRemoteRank.data();
    4593            0 :             if (!retryEnable_) {
    4594            0 :                 opParam.BatchSendRecvDataDes.sendRecvItemsPtr = aicpuSendRecvInfo.data();
    4595            0 :                 opParam.BatchSendRecvDataDes.itemNum = aicpuSendRecvInfo.size();
    4596              :             }
    4597              :         }
    4598              :         // A2 Group SendRecv 将isDirectRemoteRank全部置为false
    4599           19 :         if (opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV && deviceType_ == DevType::DEV_TYPE_910B && isGroupMode_) {
    4600            0 :             isDirectRemoteRank.resize(userRankSize_, 0);
    4601            0 :             opParam.BatchSendRecvDataDes.isDirectRemoteRank = isDirectRemoteRank.data();
    4602              :         }
    4603           19 :         auto algType = algOperator->GetAlgType();
    4604           19 :         CHK_RET(RegisterDfxInfo(opParam, algType, resMap_[newTag].slaveStreams, selectAivAlg, tag));
    4605              :         // 头计数
    4606           19 :         CHK_RET(StarsCounter(dispatcher_, opParam.stream, HEAD, opParam.aicpuUnfoldMode, retryEnable_, selectAivAlg));
    4607           19 :         if (opParam.aicpuUnfoldMode) {
    4608            0 :             isInplaceStatus_ = 0;
    4609            0 :             inPlaceSupportRetryStatus_ = InplaceSupportRetryStatus::INPLACE_STATUS_END;
    4610              :             // algOperator->SupportRetryWithInplaceCheck 依赖 algOperator->SetRetryEnable 才能正确返回是否支持inplace
    4611              : 
    4612            0 :             inplaceSupportRetry_ = algOperator->SupportRetryWithInplaceCheck(
    4613            0 :                 opType, opParam, algName, isInplaceStatus_, inPlaceSupportRetryStatus_);
    4614            0 :             HCCL_INFO("[HcclCommunicator][ExecOp] aicpu Unfold mode algType[%s], inplaceSupportRetry_[%d], opType[%d], "
    4615              :                       "isInplaceStatus_[%d], inPlaceSupportRetryStatus_[%d].",
    4616              :                       AlgTypeToStr(algType).c_str(), inplaceSupportRetry_, opType, isInplaceStatus_, inPlaceSupportRetryStatus_);
    4617            0 :             CHK_RET(OrchestrateAicpu(opType, algName, opParam, resMap_[newTag], newTag, algType, isCustom,
    4618              :                 needIncreLink));
    4619              :         } else {
    4620              :             // HOST展开aclgraph场景,capture从流
    4621           19 :             if (!selectAivAlg) {
    4622           22 :                 CHK_RET(CaptureSlaveStreams(opParam.stream.ptr(), resMap_[newTag].slaveStreams));
    4623              :             }
    4624           19 :             OpCounterInfo opCounter;
    4625           19 :             CHK_RET(GetOpCountInfo(opCounter));
    4626           19 :             CHK_RET(algOperator->SetOpCounter(opCounter));
    4627           19 :             CHK_RET(algOperator->Orchestrate(algName, opParam, resMap_[newTag]));
    4628           16 :             if (hostResMap_.find(newTag) == hostResMap_.end()) {
    4629           16 :                 hostResMap_.insert(newTag);
    4630              :             }
    4631           16 :             CHK_RET(algOperator->GetNumBlocks(numBlocks_));
    4632           16 :             if (implAlg_->GetAivModeConfig() && GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE && !opParam.isCapture) {
    4633            0 :                 CHK_RET(GetCacheMap(algOperator, opParam, algType, selectAivAlg, newTag));
    4634              :             }
    4635              :         }
    4636              :         //A3 bsr 只有走NPU直驱的时候hostSendRecvInfo才有内容
    4637           16 :         if (!hostSendRecvInfo.empty()) {
    4638              :             // A3 bsr获取到host侧需要下发的数据
    4639            0 :             HCCL_INFO("[HcclCommunicator][ExecOp] hostSendRecvInfo size %zu", hostSendRecvInfo.size());
    4640            0 :             opParam.BatchSendRecvDataDes.sendRecvItemsPtr = hostSendRecvInfo.data();
    4641            0 :             opParam.BatchSendRecvDataDes.itemNum = hostSendRecvInfo.size();
    4642            0 :             opParam.aicpuUnfoldMode = false;
    4643            0 :             opParam.aicpuCacheEnable = 0;
    4644            0 :             std::string tempTag;
    4645            0 :             std::unique_ptr<CollAlgOperator> newalgOperator = implAlg_->GetAlgOperator(opType);
    4646            0 :             CHK_SMART_PTR_NULL(newalgOperator);
    4647            0 :             CHK_RET(newalgOperator->SelectAlg(opParam.tag, opParam, limit, algName, algDesc, tempTag));
    4648            0 :             CHK_RET(newalgOperator->Orchestrate(algName, opParam, resMap_[newTag]));
    4649            0 :         }
    4650           16 :         lock.unlock();
    4651              :         // 尾计数
    4652           16 :         CHK_RET(StarsCounter(dispatcher_, opParam.stream, TAIL, opParam.aicpuUnfoldMode, retryEnable_, selectAivAlg));
    4653           16 :         CHK_RET(UnRegisterDfxInfo(opParam, resMap_[newTag].slaveStreams));
    4654           16 :         if (selectAivAlg) {
    4655            0 :             CHK_RET(algOperator->SetAivClearEnable(false));
    4656            0 :             aivClearEnable_ = false;
    4657              :         }
    4658           16 :         if (hcclNslbDp::GetInstance().GetGlobalCommTaskId() != 0 && hcclNslbDp::GetInstance().GetInitNetCoFlag() == true) {
    4659            0 :             AdjInfo nslbAdjInfo = {};
    4660            0 :             CHK_RET(algOperator->GetAdjInfo(algName, opParam, resMap_[newTag], nslbAdjInfo));
    4661            0 :             NslbDp_CollectSendAdjTable(opType, opParam, algOperator->GetAlgType(), nslbAdjInfo);
    4662            0 :         }
    4663           16 :         if (isInGraphCaptureZeroCopy) {
    4664            0 :             SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
    4665              :         }
    4666           16 :         return HCCL_SUCCESS;
    4667           72 :     }
    4668              : 
    4669            0 :     HcclResult HcclCommunicator::FreeScratchMemOnOpBaseMode(DeviceMem &scratchMem, const OpParam &opParam,
    4670              :                                                             const HcclCMDType &opType)
    4671              :     {
    4672              :         // 当前单算子模式下scratch内存为手动申请,需要手动进行释放
    4673            0 :         if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE || IsForceAicpuOpBaseMode(opParam, opType)) {
    4674            0 :             scratchMem.free();
    4675              :         }
    4676            0 :         return HCCL_SUCCESS;
    4677              :     }
    4678              : 
    4679            0 :     HcclResult HcclCommunicator::ReAllocScratchMemForAlltoall(HcclCMDType opType, const OpParam &opParam,
    4680              :         AlgResourceRequest &resRequest, AlgResourceResponse &algResResponse)
    4681              :     {
    4682            0 :         if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB &&
    4683            0 :             !IsForceAicpuOpBaseMode(opParam, opType)) {
    4684            0 :             if (resRequest.scratchMemSize > 0) {
    4685            0 :                 algResResponse.scratchMem = GetWorkspaceScracthMem(opParam.tag, resRequest.scratchMemSize);
    4686              :             }
    4687            0 :             HCCL_DEBUG("[%s] WorkflowMode set for workspace opType[%u] tag[%s]", __func__, opType, opParam.tag.c_str());
    4688            0 :         } else if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE ||
    4689            0 :             IsForceAicpuOpBaseMode(opParam, opType)) {
    4690            0 :             CHK_RET(AllocOpBaseModeScratchMem(opType, opParam, resRequest, algResResponse));
    4691            0 :             HCCL_DEBUG("[%s] WorkflowMode set for opType[%u] tag[%s]", __func__, opType, opParam.tag.c_str());
    4692              :         } else {
    4693            0 :             HCCL_ERROR("[%s] WorkflowMode is not set for opType[%u] tag[%s]", __func__, opType, opParam.tag.c_str());
    4694            0 :             return HCCL_E_PARA;
    4695              :         }
    4696            0 :         return HCCL_SUCCESS;
    4697              :     }
    4698              : 
    4699            1 :     HcclResult HcclCommunicator::HandleExistAlgResource(const std::string& newTag, const std::string& algName,
    4700              :         HcclCMDType opType, const OpParam& opParam, std::unique_ptr<CollAlgOperator>& algOperator,
    4701              :         bool selectAivAlg, bool aicpuUnfoldModeFor910B, bool needRecreateAlltoallComm)
    4702              :     {
    4703            1 :         if (needRecreateAlltoallComm) {
    4704            0 :             CHK_RET(hcclStreamSynchronize(opParam.stream.ptr(), commConfig_.GetConfigExecTimeOut()));
    4705              : 
    4706            0 :             AlgResourceRequest resRequest;
    4707            0 :             CHK_RET(algOperator->CalcResRequest(algName, opParam, resRequest));
    4708              : 
    4709              :             // 释放旧内存防止泄漏
    4710            0 :             CHK_RET(FreeScratchMemOnOpBaseMode(resMap_[newTag].scratchMem, opParam, opType));
    4711              : 
    4712            0 :             if (aicpuUnfoldModeFor910B) {
    4713            0 :                 CHK_RET(ReAllocScratchMemForAlltoall(opType, opParam, resRequest, resMap_[newTag]));
    4714            0 :                 isContextLaunched_ = true;
    4715              :             } else {
    4716            0 :                 CHK_RET(RecordOpPara(opType, opParam));
    4717            0 :                 CHK_RET(AllocAlgResource(newTag, opType, opParam, resRequest, resMap_[newTag], selectAivAlg));
    4718            0 :                 CHK_RET(RankConsistentcyChecker::GetInstance().DelOpPara(opParam.tag));
    4719              : 
    4720            0 :                 if (!isHaveCpuRank_) {
    4721            0 :                     if (isUseRankPort_) {
    4722            0 :                         std::vector<u32>& nicPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    4723            0 :                         std::vector<u32>& vnicPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    4724            0 :                         Heartbeat::GetInstance(deviceLogicId_).SetRankPortInfo(
    4725            0 :                             isUseRankPort_, nicPorts, vnicPorts, commPortConfig_.devPortSwitchOn);
    4726              :                     }
    4727            0 :                     CHK_RET(RegisterToHeartBeat());
    4728              :                 }
    4729              :             }
    4730            0 :         } else {
    4731            1 :             DeviceMem tinySendRecvMem;
    4732            1 :             CHK_RET(implAlg_->GetTinyMem(tinySendRecvMem));
    4733            1 :             CHK_RET(CalcTinySendRecvMem(opParam, resMap_[newTag], tinySendRecvMem));
    4734            1 :         }
    4735            1 :         return HCCL_SUCCESS;
    4736              :     }
    4737              : 
    4738            5 :     HcclResult HcclCommunicator::ExecOpAlltoAll(HcclCMDType opType, OpParam &opParam, bool isCustom)
    4739              :     {
    4740            5 :         CHK_PRT_RET(isInvalidComm_,
    4741              :             HCCL_ERROR("[HcclCommunicator][%s] comm[%s], rank[%u], devId[%d], snapshot recoverying, "
    4742              :             "this comm is invalid, no operator is allowed to execute.",
    4743              :             __func__, identifier_.c_str(), userRank_, deviceLogicId_), HCCL_E_UNAVAIL);
    4744              : 
    4745            5 :         std::string &tag = opParam.tag;
    4746            5 :         u32 aivCoreLimit = numBlocks_;
    4747              :         //单机AIV场景下cache复用,提升下发性能
    4748            5 :         if (implAlg_->GetAivModeConfig() && GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
    4749            5 :             if (aivCoreLimit == 0) {
    4750            4 :                 aclError acl_ret = aclrtGetResInCurrentThread(ACL_RT_DEV_RES_VECTOR_CORE, &aivCoreLimit);
    4751            4 :                 CHK_PRT_RET(acl_ret != ACL_SUCCESS,
    4752              :                     HCCL_ERROR("[HcclCommunicator][ExecOpAlltoAll] aclrtGetResInCurrentThread failed, ret=[%d]", acl_ret),
    4753              :                     HCCL_E_PARA);
    4754              :             }
    4755            5 :             opParam.deterministic = implAlg_->GetDeterministicConfig();
    4756            5 :             opParam.aivCoreLimit = aivCoreLimit;
    4757            5 :             auto it = hcclCacheMap_.find(opParam);
    4758            5 :             if (it != hcclCacheMap_.end()) {
    4759            0 :                 CHK_RET(ExecOpCache(opType, opParam, it->second));
    4760            0 :                 return HCCL_SUCCESS;
    4761              :             }
    4762              :         }
    4763              : 
    4764            5 :         ForceProf(opParam.isCapture);
    4765            5 :         bool isInGraphCaptureZeroCopy = false;
    4766            5 :         zeroCopyAclGraph_->SetRetryEnable(retryEnable_);
    4767            5 :         opParam.supportSymmetricMemory = IsSupportSymmetricMemory(opType, opParam);
    4768            5 :         opParam.supportZeroCopy = !opParam.supportSymmetricMemory && IsSupportZeroCopy(opParam);
    4769            5 :         opParam.aclGraphZeroCopyEnable = GetConfigAclGraphZeroCopyEnable();
    4770            5 :         isInGraphCaptureZeroCopy = zeroCopyAclGraph_->SetAclGraphZeroCopyMode(
    4771              :             deviceType_, opType, opParam, implAlg_.get(), cclBufferManager_.GetOutCCLbufferSize());
    4772            5 :         if (isInGraphCaptureZeroCopy && userRankSize_ > 1) {
    4773            0 :             CHK_RET(CreateCommCCLbuffer());
    4774              :         }
    4775            5 :         if (isShareComm_) {
    4776            0 :             CHK_RET(ShareCCLbufferMgr::GetInstance().CheckCCLbuffConflict(cclBuffName_, opParam.stream.id()));
    4777              :         }
    4778            5 :         std::unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(opType);
    4779            5 :         AlltoAllOperator *alltoAllOperator = dynamic_cast<AlltoAllOperator *>(algOperator.get());
    4780            5 :         CHK_PTR_NULL(alltoAllOperator);
    4781              : 
    4782            5 :         bool isSatisfyA2ACPForA3Condition = alltoAllOperator->IsSatisfyA2AContinuousPipelineFor91093Condition(opParam);
    4783            5 :         bool IsSatisfyA2ACPForA2Condition = alltoAllOperator->IsSatisfyAlltoallContinuousPipelineCondition(opParam);
    4784            5 :         if (IsSatisfyA2ACPForA2Condition || isSatisfyA2ACPForA3Condition) {
    4785            0 :             opParam.aicpuUnfoldMode = true;
    4786            0 :             opParam.aicpuCacheEnable = GetExternalInputAicpuCacheEnable();
    4787              :         }
    4788              : 
    4789              :         // 算法选择
    4790            5 :         std::string algName;
    4791            5 :         std::string newTag;
    4792            5 :         if (opParam.aicpuUnfoldMode) {
    4793              :             // 用于inplace支持重执行判断
    4794            0 :             CHK_RET(algOperator->SetRetryEnable(retryEnable_));
    4795              :         }
    4796            5 :         std::unique_ptr<PreProcessMetaInfo> preMetaInfo = std::make_unique<PreProcessMetaInfo>();
    4797            5 :         CHK_SMART_PTR_NULL(preMetaInfo);
    4798              : 
    4799            5 :         bool preProcessFlag = alltoAllOperator->JudgeIfNeedPreProcessAndGetParam(opParam, preMetaInfo);
    4800            5 :         if (preProcessFlag) {
    4801            0 :             if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
    4802            0 :                 CHK_RET(RegressCalPreOp(alltoAllOperator, opParam, preMetaInfo, const_cast<Stream &>(opParam.stream)));
    4803              :             } else {
    4804            0 :                 CHK_RET(RegressCalPreOp(alltoAllOperator, opParam, preMetaInfo));
    4805              :             }
    4806              :         }
    4807              : 
    4808            5 :         if (deviceType_ == DevType::DEV_TYPE_910B && userRankSize_ > 1) {
    4809              :             // 用于AIV支持Roce直驱判断
    4810            5 :             CHK_RET(IsSupportAIVNormalQP(devicePhyId_, opParam.supportRoceDirect));
    4811              :         }
    4812              : 
    4813            5 :         std::unique_lock<std::mutex> lock(commResMutex_);
    4814            5 :         ResourceLimit limit;
    4815            5 :         limit.ifLimit = true;
    4816            5 :         limit.aivCoreLimit = aivCoreLimit;
    4817            5 :         AlgDesc algDesc;
    4818            5 :         algDesc.isLastSelect = true;
    4819            5 :         CHK_RET(algOperator->SelectAlg(opParam.tag, opParam, limit, algName, algDesc, newTag));
    4820              :         // 是否是AIV直驱Roce场景
    4821            5 :         opParam.isNpuDirectRoce = algName == "AlltoAllDirectFullmeshAIVExecutor";
    4822            5 :         if (isOnlyAiv_ && !algDesc.isAivMode) {
    4823            0 :             std::string opTypeName = GetCMDTypeEnumStr(opType);
    4824            0 :             HCCL_ERROR("[HcclCommunicator][ExecOp] opType[%s] currently do not select aiv mode, aiv only not support.",
    4825              :                 opTypeName.c_str());
    4826            0 :             return HCCL_E_NOT_SUPPORT;
    4827            0 :         }
    4828            5 :         CHK_RET(PrepareZeroCopy(algName, algDesc, opParam));
    4829              : 
    4830            5 :         if (opParam.isCapture) {
    4831              :             // aclgraph使用新的Tag,避免影响其他操作
    4832            3 :             newTag += "_Capture";
    4833              :             // aclgraph零拷贝场景下,每个算子都有单独的tag,需要记录,在graph销毁时清理相关资源
    4834            3 :             if (isInGraphCaptureZeroCopy) {
    4835            0 :                 CHK_RET(AclgraphCallback::GetInstance().InsertNewTagToCaptureResMap(this, newTag, opParam));
    4836            0 :                 tagsRequiringHostCleanup_.insert(newTag);
    4837              :             }
    4838              :         }
    4839              : 
    4840            0 :         auto isSupportAlg = [](const std::string &algName, bool aicpuUnfoldMode) -> bool {
    4841            0 :             return ((algName == "RunAlltoAllVFullMesh" || algName == "RunAlltoAllVTwoLevelPipeline") && aicpuUnfoldMode) ||
    4842            0 :                 (algName == "RunAlltoAllDirectFullmesh" || algName == "RunAlltoAllFullMeshSymmetricMemory");
    4843              :         };
    4844            5 :         bool isOpbaseMode = GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE;
    4845            5 :         if ((isOpbaseMode && userRankSize_ > 1) || (isSupportAlg(algName, opParam.aicpuUnfoldMode))) {
    4846            5 :             CHK_RET(CreateCommCCLbuffer());
    4847              :         }
    4848              :         // 资源创建
    4849            5 :         bool selectAivAlg = algDesc.isAivMode;
    4850            5 :         if ((resMap_.find(newTag) != resMap_.end()) && opParam.isCapture) {
    4851            1 :             AlgResourceRequest resRequest;
    4852            1 :             CHK_RET(algOperator->CalcResRequest(algName, opParam, resRequest));
    4853            1 :             if (HasRoceTransportLinks(resRequest.opTransport)) {
    4854            0 :                 auto resTmp = resMap_[newTag];
    4855            0 :                 ++captureCnt_;
    4856            0 :                 newTag += std::to_string(captureCnt_);
    4857            0 :                 resMap_[newTag] = resTmp;
    4858            0 :                 resRequest.isInGraphCaptureZeroCopy = isInGraphCaptureZeroCopy;
    4859            0 :                 CHK_RET(CleanTransportLinks(resRequest.opTransport, resMap_[newTag].opTransportResponse));
    4860            0 :                 if (IsEnableBackupLink()) {
    4861            0 :                     CHK_RET(CleanTransportLinks(resRequest.opTransport, resMap_[newTag].opTransportResponseBackUp));
    4862              :                 }
    4863              :                 // 记录指令信息用于一致性校验
    4864            0 :                 CHK_RET(RecordOpPara(opType, opParam));
    4865            0 :                 CHK_RET(IncreAllocLink(newTag, opParam, resRequest, resMap_[newTag]));
    4866              :                 // 移除tag对应的指令信息
    4867            0 :                 CHK_RET(RankConsistentcyChecker::GetInstance().DelOpPara(opParam.tag));
    4868              :                 // aclgraph零拷贝场景下,除第一个capture外,需要记录,在graph销毁时清理相关资源
    4869            0 :                 CHK_RET(AclgraphCallback::GetInstance().InsertNewTagToCaptureResMap(this, newTag, opParam));
    4870            0 :                 tagsRequiringHostCleanup_.insert(newTag);
    4871            0 :             }
    4872            1 :         }
    4873            5 :         InsertNewTagToTagMap(newTag, opParam.tag);
    4874              :         bool aicpuUnfoldModeFor910B =
    4875            5 :             deviceType_ == DevType::DEV_TYPE_910B && opParam.aicpuUnfoldMode &&
    4876            0 :             (algName == "RunAlltoAllVStaged" || algName == "RunAlltoAllVFullMesh");
    4877            5 :         bool needRecreateAlltoallComm = false;
    4878            5 :         if (resMap_.find(newTag) == resMap_.end()) {
    4879            4 :             AlgResourceRequest resRequest;
    4880            4 :             CHK_RET(algOperator->CalcResRequest(algName, opParam, resRequest));
    4881            4 :             resRequest.isInGraphCaptureZeroCopy = isInGraphCaptureZeroCopy;
    4882            4 :             CHK_RET(RecordOpPara(opType, opParam));
    4883            4 :             CHK_RET(AllocAlgResource(newTag, opType, opParam, resRequest, resMap_[newTag], selectAivAlg));
    4884            4 :             CHK_RET(RankConsistentcyChecker::GetInstance().DelOpPara(opParam.tag));
    4885            4 :             if (opParam.isNpuDirectRoce) {
    4886              :                 // AIV直驱roce多机场景,需要生成RMAInfo并拷贝至Device
    4887            0 :                 CHK_RET(GenAiRMAInfoV2(newTag));
    4888            0 :                 CHK_RET(H2DAiRMAInfoV2(newTag, opParam.stream.ptr()));
    4889              :             }
    4890              :             // 对于91093超节点内aiv跨机通信算子,将不同机的CCLbuffer地址存在约定好的aiv将读取的HBM位置
    4891            4 :             CHK_RET(algOperator->PrepareCommInfoToDevice(algName, resMap_[newTag]));
    4892              : 
    4893            4 :             if (!isHaveCpuRank_) {
    4894            4 :                 if (isUseRankPort_) {
    4895            4 :                     std::vector<u32> &nicPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    4896            4 :                     std::vector<u32> &vnicPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    4897            4 :                     Heartbeat::GetInstance(deviceLogicId_).SetRankPortInfo(isUseRankPort_, nicPorts, vnicPorts, commPortConfig_.devPortSwitchOn);
    4898              :                 }
    4899            4 :                 CHK_RET(RegisterToHeartBeat());
    4900              :             }
    4901            4 :             CHK_RET(UpdateZeroCopy(opParam, resMap_[newTag]));
    4902            4 :         } else {
    4903            1 :             CHK_RET(alltoAllOperator->CheckNeedRecreateComm(algName, opParam, resMap_[newTag].scratchMem.size(),
    4904              :                                                             needRecreateAlltoallComm));
    4905            1 :             HCCL_INFO("resMap_ find this newTag[%s], and need to judge whether recreate comm [%d]", newTag.c_str(),
    4906              :                       needRecreateAlltoallComm);
    4907            1 :             CHK_RET(HandleExistAlgResource(newTag, algName, opType, opParam, algOperator,
    4908              :                 selectAivAlg, aicpuUnfoldModeFor910B, needRecreateAlltoallComm));
    4909              :         }
    4910            5 :         auto &algRes = resMap_[newTag];
    4911              : 
    4912            5 :         if (hcclNslbDp::GetInstance().GetGlobalCommTaskId() != 0 && hcclNslbDp::GetInstance().GetInitNetCoFlag() == true) {
    4913              :             /* NSLB 填充 表  */
    4914            0 :             u32 srcLocalRankId = userRank_;
    4915            0 :             u32 rootRank = (opParam.root == INVALID_VALUE_RANKID) ? 0 : opParam.root;
    4916            0 :             AlgType nslbAlgType = algOperator->GetAlgType();
    4917            0 :             AlgTypeLevel1 algValue = nslbAlgType.algoLevel1;
    4918            0 :             uint8_t nslbAlg = hcclNslbDp::GetInstance().GetNslbLevel1AlgType(algValue);
    4919              : 
    4920            0 :             if (algName == "RunAlltoAllVFullMesh" || algName == "RunAlltoAllDirectFullmesh") {
    4921            0 :                 nslbAlg = NSLBDP_PAIRWISE;
    4922            0 :                 if (deviceType_ == DevType::DEV_TYPE_910_93) {
    4923            0 :                     nslbAlg = NSLB_ALGO_TYPE_FULLMESH;
    4924              :                 }
    4925              :             }
    4926              : 
    4927            0 :             std::string nslb_identifier = identifier_;
    4928            0 :             HCCL_INFO("NSLBDP-SWK NslbDp_CollectOperTable nslb_identifier[%s] .", nslb_identifier.c_str());
    4929            0 :             u32 rankSize = userRankSize_;
    4930            0 :             u64 count = opParam.All2AllDataDes.sendCount * SIZE_TABLE[opParam.All2AllDataDes.sendType];
    4931              :             // 填充表2
    4932            0 :             hcclNslbDp::GetInstance().GenerateOpAndAdjTable(opType, rootRank, srcLocalRankId, nslbAlg, nslb_identifier, count, rankSize);
    4933            0 :             AdjInfo nslbAdjInfo = {};
    4934            0 :             CHK_RET(algOperator->GetAdjInfo(algName, opParam, algRes, nslbAdjInfo));
    4935            0 :             HCCL_INFO("[NSLBDP-WEN]-nslbAdjInfosize[%u]-algName[%s]-rankSize[%u]-commDesc[%s]..",
    4936              :                           nslbAdjInfo.dstRankNum, algName.c_str(), userRankSize_, identifier_.c_str());
    4937              :             // 填充表3
    4938            0 :             hcclNslbDp::GetInstance().GetAlgAdjacencyTable(opType, srcLocalRankId, rootRank, nslbAlg, nslb_identifier, nslbAdjInfo);
    4939              :             /*发送流程*/
    4940            0 :             hcclNslbDp::GetInstance().SendAlgorithmInfoTable();
    4941            0 :         }
    4942              :         // 算法执行
    4943            5 :         if (opParam.isNpuDirectRoce) {
    4944              :             // AIV直驱roce多机场景,需要生成RMAInfo并拷贝至Device
    4945            0 :             CHK_PTR_NULL(combinOparaMem_);
    4946            0 :             HcclCombinOpParam *combinOparaPtr = reinterpret_cast<HcclCombinOpParam*>(combinOparaMem_->ptr());
    4947            0 :             CHK_PTR_NULL(combinOparaPtr);
    4948            0 :             CHK_RET(algOperator->SetRmaInfo(combinOparaPtr->aiRMAInfo));
    4949              :         }
    4950            5 :         if (selectAivAlg) {
    4951            5 :             CHK_RET(HandleAclGraphFirstOpAivBuff(opParam.stream.ptr()));
    4952            5 :             if (aivClearEnable_) {
    4953              :                 // 用于判断图模式是否清零
    4954            2 :                 CHK_RET(algOperator->SetAivClearEnable(aivClearEnable_));
    4955            2 :                 aivOffloadTag_ = 1;
    4956              :             }
    4957            5 :             GetAivTag(algDesc.aivTagNum, opParam.isCapture, opParam.aivTag);
    4958            5 :             HCCL_INFO("[HcclCommunicator][ExecOpAlltoAll] tag[%s] userRank[%u] cur aiv tag [%d].",
    4959              :                 identifier_.c_str(), userRank_, opParam.aivTag);
    4960            5 :             opParam.aicpuUnfoldMode = false;
    4961            5 :             opParam.aicpuCacheEnable = 0;
    4962            5 :             CHK_RET(algOperator->SetNumBlocks(aivCoreLimit));
    4963              :         }
    4964              : 
    4965            5 :         auto algType = algOperator->GetAlgType();
    4966            5 :         CHK_RET(RegisterDfxInfo(opParam, algType, algRes.slaveStreams, selectAivAlg, tag));
    4967              :         // 头计数
    4968            5 :         CHK_RET(StarsCounter(dispatcher_, opParam.stream, HEAD, opParam.aicpuUnfoldMode, retryEnable_, selectAivAlg));
    4969              :         // 算法执行
    4970            0 :         auto isSupportAicpuAlg = [](const std::string &algName) {
    4971              :             static const std::set<std::string> aicpuAlgs = {
    4972              :                 "RunAlltoAllVFullMesh",
    4973              :                 "RunAlltoAllDirectFullmesh",
    4974              :                 "RunAlltoAllVTwoLevelPipeline",
    4975              :                 "RunAlltoAllFullMeshSymmetricMemory",
    4976              :                 "RunAlltoAllVContinuousPipeline",
    4977              :                 "RunAlltoAllVPipelineFor91093"
    4978            0 :             };
    4979            0 :             return aicpuAlgs.count(algName) > 0;
    4980              :         };
    4981            5 :         if (opParam.aicpuUnfoldMode && (isSupportAicpuAlg(algName) || aicpuUnfoldModeFor910B)) {
    4982            0 :             isInplaceStatus_ = 0;
    4983            0 :             inPlaceSupportRetryStatus_ = InplaceSupportRetryStatus::INPLACE_STATUS_END;
    4984              :             // algOperator->SupportRetryWithInplaceCheck 依赖 algOperator->SetRetryEnable 才能正确返回是否支持inplace
    4985              : 
    4986            0 :             inplaceSupportRetry_ = algOperator->SupportRetryWithInplaceCheck(
    4987            0 :                 opType, opParam, algName, isInplaceStatus_, inPlaceSupportRetryStatus_);
    4988            0 :             HCCL_INFO("[HcclCommunicator][ExecOp] aicpu Unfold mode algType[%s], inplaceSupportRetry_[%d], opType[%d], "
    4989              :                       "isInplaceStatus_[%d], inPlaceSupportRetryStatus_[%d].",
    4990              :                       AlgTypeToStr(algType).c_str(), inplaceSupportRetry_, opType, isInplaceStatus_, inPlaceSupportRetryStatus_);
    4991            0 :             CHK_RET(OrchestrateAicpu(opType, algName, opParam, algRes, newTag, algType, isCustom, false, needRecreateAlltoallComm));
    4992              :         } else {
    4993              :             // HOST展开aclgraph场景,capture从流
    4994            5 :             if (!selectAivAlg) {
    4995            0 :                 CHK_RET(CaptureSlaveStreams(opParam.stream.ptr(), algRes.slaveStreams));
    4996              :             }
    4997            5 :             OpCounterInfo opCounter;
    4998            5 :             CHK_RET(GetOpCountInfo(opCounter));
    4999            5 :             CHK_RET(algOperator->SetOpCounter(opCounter));
    5000            5 :             CHK_RET(algOperator->Orchestrate(algName, opParam, algRes));
    5001              :             // for profiling, numBlocks upload
    5002            5 :             CHK_RET(algOperator->GetNumBlocks(numBlocks_));
    5003            5 :             if (implAlg_->GetAivModeConfig() && GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE && !opParam.isCapture) {
    5004            2 :                 CHK_RET(GetCacheMap(algOperator, opParam, algType, selectAivAlg, newTag));
    5005              :             }
    5006              :         }
    5007            5 :         lock.unlock();
    5008              :         // 尾计数
    5009            5 :         CHK_RET(StarsCounter(dispatcher_, opParam.stream, TAIL, opParam.aicpuUnfoldMode, retryEnable_, selectAivAlg));
    5010            5 :         CHK_RET(UnRegisterDfxInfo(opParam, algRes.slaveStreams));
    5011            5 :         if (selectAivAlg) {
    5012            5 :             CHK_RET(algOperator->SetAivClearEnable(false));
    5013            5 :             aivClearEnable_ = false;
    5014              :         }
    5015              : 
    5016            5 :         if (isInGraphCaptureZeroCopy) {
    5017            0 :             SetWorkflowMode(HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
    5018              :         }
    5019            5 :         return HCCL_SUCCESS;
    5020            5 :     }
    5021              : 
    5022           76 :     HcclResult HcclCommunicator::RecordOpPara(HcclCMDType opType, const OpParam &opParam)
    5023              :     {
    5024           76 :         u32 aivCoreLimit = (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB) ? numBlocks_ : 0;
    5025           76 :         u8 deterministic = implAlg_->GetDeterministicConfig();
    5026           74 :         switch (opType) {
    5027           62 :             case HcclCMDType::HCCL_CMD_ALLGATHER:
    5028              :             case HcclCMDType::HCCL_CMD_ALLREDUCE:
    5029              :             case HcclCMDType::HCCL_CMD_REDUCE_SCATTER:
    5030              :             case HcclCMDType::HCCL_CMD_BROADCAST:
    5031           62 :                 CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(opType,
    5032              :                         opParam.tag, opParam.DataDes.count, opParam.DataDes.dataType, opParam.reduceType, opParam.root,
    5033              :                         cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(),
    5034              :                         identifier_.c_str(), ranktableCrc_, deterministic, aivCoreLimit));
    5035           65 :                 break;
    5036            8 :             case HcclCMDType::HCCL_CMD_SCATTER:
    5037              :             case HcclCMDType::HCCL_CMD_REDUCE:
    5038            8 :                 CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(opType,
    5039              :                         opParam.tag, opParam.DataDes.count, opParam.DataDes.dataType, opParam.reduceType, opParam.root,
    5040              :                         cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(),
    5041              :                         identifier_.c_str(), ranktableCrc_, deterministic));
    5042            8 :                 break;
    5043            0 :             case HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V:
    5044              :             case HcclCMDType::HCCL_CMD_ALLGATHER_V:
    5045            0 :                 CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(opType,
    5046              :                         opParam.tag, opParam.VDataDes.counts, opParam.VDataDes.displs, userRankSize_, opParam.VDataDes.dataType, opParam.reduceType,
    5047              :                         cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(),
    5048              :                         identifier_.c_str(), ranktableCrc_, deterministic, aivCoreLimit));
    5049            0 :                 break;
    5050            0 :             case HcclCMDType::HCCL_CMD_BATCH_SEND_RECV:
    5051            0 :                 CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(opType,
    5052              :                         opParam.tag, cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(),
    5053              :                         identifier_.c_str(), ranktableCrc_));
    5054            0 :                 break;
    5055            0 :             case HcclCMDType::HCCL_CMD_SEND:
    5056            0 :                 CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(opType,
    5057              :                         opParam.tag, opParam.DataDes.count, opParam.DataDes.dataType, opParam.dstRank, opParam.srTag, opParam.localGroupRank,
    5058              :                         cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(),
    5059              :                         identifier_.c_str(), ranktableCrc_));
    5060            0 :                 break;
    5061            0 :             case HcclCMDType::HCCL_CMD_RECEIVE:
    5062            0 :                 CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(opType,
    5063              :                         opParam.tag, opParam.DataDes.count, opParam.DataDes.dataType, opParam.srcRank, opParam.srTag, opParam.localGroupRank,
    5064              :                         cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(),
    5065              :                         identifier_.c_str(), ranktableCrc_));
    5066            0 :                 break;
    5067            0 :             case HcclCMDType::HCCL_CMD_ALLTOALL:
    5068            0 :                 CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(opType,
    5069              :                         opParam.tag, opParam.All2AllDataDes.sendCount, opParam.All2AllDataDes.sendType, opParam.reduceType, opParam.root,
    5070              :                         cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(),
    5071              :                         identifier_.c_str(), ranktableCrc_, aivCoreLimit));
    5072            0 :                 break;
    5073            4 :             case HcclCMDType::HCCL_CMD_ALLTOALLV:
    5074              :             case HcclCMDType::HCCL_CMD_ALLTOALLVC:
    5075            4 :                 CHK_RET(RankConsistentcyChecker::GetInstance().RecordOpPara(opType,
    5076              :                         opParam.tag, 0, HCCL_DATA_TYPE_RESERVED, opParam.reduceType, opParam.root,
    5077              :                         cclBufferManager_.GetInCCLbufferSize(), cclBufferManager_.GetOutCCLbufferSize(),
    5078              :                         identifier_.c_str(), ranktableCrc_, aivCoreLimit));
    5079            4 :                 break;
    5080            0 :             default:
    5081            0 :                 break;
    5082              :         }
    5083           77 :         return HCCL_SUCCESS;
    5084              :     }
    5085            5 :     HcclResult HcclCommunicator::HandleAclGraphFirstOpAivBuff(rtStream_t mainStream)
    5086              :     {
    5087            5 :         aclmdlRI rtModel = nullptr;
    5088            5 :         bool isCapture = false;
    5089            5 :         u64 modelId = 0;
    5090            5 :         CHK_RET(GetStreamCaptureInfo(mainStream, rtModel, isCapture));
    5091            5 :         if (isCapture) {
    5092            3 :             CHK_PTR_NULL(rtModel);
    5093              :             // 获取不到modelId会报错
    5094            3 :             CHK_RET(GetModelId(rtModel, modelId));
    5095            3 :             if (captureModelIds_.find(modelId) == captureModelIds_.end()) {
    5096              :                 // aclgraph场景,首算子清理AIV buff
    5097            2 :                 aivClearEnable_ = true;
    5098            2 :                 captureModelIds_.insert(modelId);
    5099            2 :                 HCCL_INFO("[HcclCommunicator][%s] modelId[%u] is inserted to captureModelIds_", __func__, modelId);
    5100              :             }
    5101              :         }
    5102            5 :         return HCCL_SUCCESS;
    5103              :     }
    5104              : 
    5105           84 :     bool HcclCommunicator::StreamIsCapture(rtStream_t mainStream)
    5106              :     {
    5107           84 :         bool isCapture = false;
    5108           84 :         aclmdlRI rtModel = nullptr;
    5109           84 :         CHK_RET(GetStreamCaptureInfo(mainStream, rtModel, isCapture));
    5110           94 :         return isCapture;
    5111              :     }
    5112              : 
    5113           19 :     HcclResult HcclCommunicator::CaptureSlaveStreams(rtStream_t mainStream, vector<Stream> &slaveStreams)
    5114              :     {
    5115           19 :         if ((deviceType_ != DevType::DEV_TYPE_910_93) && (deviceType_ != DevType::DEV_TYPE_310P3) && (deviceType_ != DevType::DEV_TYPE_910B || GetExternalInputHcclEnableFfts())) {
    5116            0 :             HCCL_INFO("[HcclCommunicator][%s]Only 310P3 or A2 or A3 device in host expand mode need to capture slave streams.", __func__);
    5117            0 :             return HCCL_SUCCESS;
    5118              :         }
    5119           19 :         aclmdlRI rtModel = nullptr;
    5120           19 :         bool isCapture = false;
    5121           19 :         u64 modelId = 0;
    5122           19 :         CHK_RET(GetStreamCaptureInfo(mainStream, rtModel, isCapture));
    5123           19 :         if (isCapture) {
    5124            0 :             CHK_PTR_NULL(rtModel);
    5125            0 :             CHK_RET(GetModelId(rtModel, modelId));
    5126            0 :             for (auto slaveStream : slaveStreams) {
    5127            0 :                 CHK_RET(AddStreamToModel(slaveStream.ptr(), rtModel));
    5128            0 :                 HCCL_DEBUG("[HcclCommunicator][%s]Add stream[%d] to model[%u] success.", __func__, slaveStream.id(),
    5129              :                            modelId);
    5130            0 :             }
    5131              :         }
    5132           19 :         return HCCL_SUCCESS;
    5133              :     }
    5134              : 
    5135            0 :     HcclResult HcclCommunicator::BuildOpLocalScratchMemResParam(
    5136              :         const AlgResourceResponse &algResource, const std::string &newTag, LocalResInfoV2 *localResHostPtr)
    5137              :     {
    5138            0 :         if (algResource.scratchMem.size() > 0) {
    5139            0 :             hostMemVec_.resize(hostMemVec_.size() + 1);
    5140            0 :             CHK_RET(AllocAndClearHostMem(sizeof(HccltagLocalResV2), hostMemVec_.back()));
    5141            0 :             HccltagLocalResV2 *tagLocalResHostPtr = static_cast<HccltagLocalResV2 *>(hostMemVec_.back().get()->ptr());
    5142              : 
    5143            0 :             deviceMemVec_.resize(deviceMemVec_.size() + 1);
    5144            0 :             CHK_RET(AllocAndClearDeviceMem(sizeof(HccltagLocalResV2), deviceMemVec_.back()));
    5145            0 :             HccltagLocalResV2 *tagLocalResDevicePtr = static_cast<HccltagLocalResV2 *>(deviceMemVec_.back().get()->ptr());
    5146              : 
    5147              :             // 初始化HcclRankRelationResV2中的tagRes链表
    5148            0 :             ListCommonInit(&tagLocalResDevicePtr->nextTagRes, &tagLocalResHostPtr->nextTagRes);
    5149              :             // 刷新host空间内容
    5150            0 :             CHK_SAFETY_FUNC_RET(
    5151              :                 memcpy_s(tagLocalResHostPtr->tag, sizeof(tagLocalResHostPtr->tag), newTag.c_str(), newTag.length() + 1));
    5152            0 :             tagLocalResHostPtr->ScratchmemSize = algResource.scratchMem.size();
    5153            0 :             tagLocalResHostPtr->Scratchmem = reinterpret_cast<u64>(algResource.scratchMem.ptr());
    5154              : 
    5155              :             // 3、将节点插入链表头
    5156            0 :             ListCommonAddHead(&tagLocalResDevicePtr->nextTagRes,
    5157              :                               &tagLocalResHostPtr->nextTagRes,
    5158              :                               &localResHostPtr->nextTagRes,
    5159            0 :                               &opResDeviceParaPtr_->localRes.nextTagRes);
    5160            0 :             HCCL_RUN_INFO("[HcclCommunicator][BuildOpLocalScratchMemResParam] LocalResHostPtr head addr[%p], nextHost[%p], "
    5161              :                        "preHost[%p], tag LocalResHostPtr head addr[%p], nextHost[%p],"
    5162              :                        "preHost[%p], tag[%s]",
    5163              :                        &localResHostPtr->nextTagRes, localResHostPtr->nextTagRes.nextHost,
    5164              :                        localResHostPtr->nextTagRes.preHost, &tagLocalResHostPtr->nextTagRes,
    5165              :                        tagLocalResHostPtr->nextTagRes.nextHost, tagLocalResHostPtr->nextTagRes.preHost,
    5166              :                        tagLocalResHostPtr->tag);
    5167              :         }
    5168            0 :         return HCCL_SUCCESS;
    5169              :     }
    5170              : 
    5171            0 :     HcclResult HcclCommunicator::CheckSetRetryStateToWaitResume()
    5172              :     {
    5173            0 :         if (retryEnable_ && opRetryManager_ != nullptr) {
    5174            0 :             HcclResult ret = opRetryManager_->SetRetryStateToWaitResume(identifier_, commConnections_.isRoot);
    5175            0 :             CHK_PRT_RET(ret != HCCL_SUCCESS,
    5176              :                         HCCL_ERROR("[NsRecovery]set opretry state to wait resume timeout."), HCCL_E_INTERNAL);
    5177              :         }
    5178            0 :         return HCCL_SUCCESS;
    5179              :     }
    5180              : 
    5181            0 :     HcclResult HcclCommunicator::BuildOpLocalResParam(const AlgResourceResponse &algResource, const std::string &newTag)
    5182              :     {
    5183            0 :         LocalResInfoV2 *localResHostPtr = &opResPara_.localRes;
    5184            0 :         ListCommonInit(&opResDeviceParaPtr_->localRes.nextTagRes, &opResPara_.localRes.nextTagRes);
    5185            0 :         if (algResource.slaveDevStreams.size() > LOCAL_STREAM_MAX_NUM) {
    5186            0 :             HCCL_ERROR("[HcclCommunicator][BuildOpLocalResParam]Fail to assign stream for tag[%s]", newTag.c_str());
    5187            0 :             return HCCL_E_PARA;
    5188              :         }
    5189            0 :         auto signalM2SNum = algResource.notifiesDevMain.size();
    5190            0 :         auto signalS2MNum = algResource.notifiesDevAux.size();
    5191            0 :         auto signalNum = signalM2SNum + signalS2MNum;
    5192            0 :         if (signalNum > LOCAL_NOTIFY_MAX_NUM) {
    5193            0 :             HCCL_ERROR("[HcclCommunicator][BuildOpLocalResParam]Fail to assign local notify for tag[%s]", newTag.c_str());
    5194            0 :             return HCCL_E_PARA;
    5195              :         }
    5196              : 
    5197            0 :         localResHostPtr->streamNum = algResource.slaveDevStreams.size();
    5198            0 :         for (u32 i = 0; i < algResource.slaveDevStreams.size(); i++) {
    5199            0 :             localResHostPtr->streamParam[i].streamInfo.streamIds = algResource.slaveDevStreams[i].id();
    5200            0 :             localResHostPtr->streamParam[i].streamInfo.sqIds = algResource.slaveDevStreams[i].sqId();
    5201            0 :             localResHostPtr->streamParam[i].streamInfo.cqIds = algResource.slaveDevStreams[i].cqId();
    5202            0 :             localResHostPtr->streamParam[i].streamInfo.logicCqids = algResource.slaveDevStreams[i].logicCqId();
    5203            0 :             CHK_RET(AllocAndGetStreamContextBuff(algResource.slaveDevStreams[i].id(),
    5204              :                                                  localResHostPtr->streamParam[i].sqCqContextAddr, localResHostPtr->streamParam[i].sqCqContextSize));
    5205              :         }
    5206              : 
    5207            0 :         localResHostPtr->signalNum = signalNum;
    5208              : 
    5209            0 :         for (u32 i = 0; i < signalM2SNum; i++) {
    5210            0 :             algResource.notifiesDevMain[i]->GetNotifyData(localResHostPtr->localSignals[i << 1]);
    5211            0 :             algResource.notifiesDevAux[i]->GetNotifyData(localResHostPtr->localSignals[(i << 1) + 1]);
    5212              :         }
    5213            0 :         HcclResult ret = HCCL_SUCCESS;
    5214            0 :         ret = CreateAndGetAiCpuNotify(localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_0)],
    5215            0 :             localResHostPtr->aicpuOpNotify[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_0)]);
    5216            0 :         CHK_PRT_RET(ret != HCCL_SUCCESS,
    5217              :                     HCCL_ERROR("[HcclCommunicator][BuildOpLocalResParam]get aicpu notify 0 error,"
    5218              :                                "errNo[0x%016llx]",
    5219              :                                HCCL_ERROR_CODE(ret)),
    5220              :                     ret);
    5221            0 :         ret = CreateAndGetAiCpuNotify(localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_1)],
    5222            0 :             localResHostPtr->aicpuOpNotify[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_1)]);
    5223            0 :         CHK_PRT_RET(ret != HCCL_SUCCESS,
    5224              :                     HCCL_ERROR(
    5225              :                         "[HcclCommunicator][BuildOpLocalResParam]get aicpu notify 1 error,errNo[0x%016llx]", HCCL_ERROR_CODE(ret)),
    5226              :                     ret);
    5227              : 
    5228            0 :         if (opMainStream_.ptr() == nullptr) {
    5229            0 :             opMainStream_ = Stream(StreamType::STREAM_TYPE_DEVICE);
    5230              :         }
    5231            0 :         localResHostPtr->mainStreamParam.streamInfo.streamIds = opMainStream_.id();
    5232            0 :         localResHostPtr->mainStreamParam.streamInfo.sqIds = opMainStream_.sqId();
    5233            0 :         localResHostPtr->mainStreamParam.streamInfo.cqIds = opMainStream_.cqId();
    5234            0 :         localResHostPtr->mainStreamParam.streamInfo.logicCqids = opMainStream_.logicCqId();
    5235            0 :         CHK_RET(AllocAndGetStreamContextBuff(opMainStream_.id(),
    5236              :                                              localResHostPtr->mainStreamParam.sqCqContextAddr, localResHostPtr->mainStreamParam.sqCqContextSize));
    5237              : 
    5238              :         // 按序下发的aicpu控制流
    5239            0 :         if (aicpuOrderStream_.ptr() == nullptr) {
    5240            0 :             aicpuOrderStream_ = Stream(StreamType::STREAM_TYPE_DEVICE);
    5241              :         }
    5242            0 :         opResPara_.aicpuOrderStreamParam.streamInfo.streamIds = aicpuOrderStream_.id();
    5243            0 :         opResPara_.aicpuOrderStreamParam.streamInfo.sqIds = aicpuOrderStream_.sqId();
    5244            0 :         opResPara_.aicpuOrderStreamParam.streamInfo.cqIds = aicpuOrderStream_.cqId();
    5245            0 :         opResPara_.aicpuOrderStreamParam.streamInfo.logicCqids = aicpuOrderStream_.logicCqId();
    5246            0 :         CHK_RET(AllocAndGetStreamContextBuff(opResPara_.aicpuOrderStreamParam.streamInfo.streamIds,
    5247              :             opResPara_.aicpuOrderStreamParam.sqCqContextAddr,
    5248              :             opResPara_.aicpuOrderStreamParam.sqCqContextSize));
    5249              :         
    5250              :         #ifndef CCL_KERNEL_AICPU
    5251            0 :         for (u32 i = 0; i < AICPU_LOCAL_EVENT_SIZE; ++i) {
    5252            0 :             aclError ret = aclrtCreateEventExWithFlag(&localAicpuOpEvent_[i], ACL_EVENT_SYNC);
    5253            0 :             CHK_PRT_RET(ret != ACL_SUCCESS, HCCL_ERROR("[%s]aclrtCreateEventExWithFlag failed, ret[%d] event[%p].",
    5254              :                 __func__, ret, localAicpuOpEvent_[i]), HCCL_E_RUNTIME);
    5255              :         }
    5256              :         #endif
    5257              : 
    5258            0 :         CHK_RET(BuildOpLocalScratchMemResParam(algResource, newTag, localResHostPtr));
    5259            0 :         return HCCL_SUCCESS;
    5260              :     }
    5261              : 
    5262            0 :     HcclResult HcclCommunicator::AllocAndGetStreamContextBuff(u32 streamId, u64 &addr, u64 &size)
    5263              :     {
    5264            0 :         if (streamIdToStreamContext_.find(streamId) == streamIdToStreamContext_.end()) {
    5265            0 :             DeviceMem streamContext;
    5266            0 :             CHK_RET(CreateWorkSpace(sizeof(SqCqeContext), streamContext));
    5267            0 :             streamIdToStreamContext_.insert({streamId, std::move(streamContext)});
    5268            0 :         }
    5269            0 :         addr = reinterpret_cast<u64>(streamIdToStreamContext_.at(streamId).ptr());
    5270            0 :         size = streamIdToStreamContext_.at(streamId).size();
    5271            0 :         HCCL_INFO("%s success, streamId:%u, addr:0x%llx, size:%llu", __func__, streamId, addr, size);
    5272            0 :         return HCCL_SUCCESS;
    5273              :     }
    5274              : 
    5275            0 :     u32 HcclCommunicator::UpdateOpIndex(const OpParam &opParam)
    5276              :     {
    5277            0 :         u32 opIndex = 0;
    5278            0 :         u32 commIndex = 0;
    5279              :         // 用于重执行和taskException打印的算子计数,bsr/sendrecv/其他算子分别计数
    5280            0 :         if (opParam.opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV) {
    5281            0 :             constexpr s32 batSendRecvIndex = -1; // batchSendRecv使用 key = -1
    5282            0 :             commIndex = batSendRecvIndex;
    5283            0 :         } else if (opParam.opType == HcclCMDType::HCCL_CMD_SEND) {
    5284            0 :             commIndex = opParam.dstRank;
    5285            0 :         } else if (opParam.opType == HcclCMDType::HCCL_CMD_RECEIVE) {
    5286            0 :             commIndex = opParam.srcRank;
    5287              :         } else {
    5288            0 :             commIndex = userRank_;
    5289              :         }
    5290              : 
    5291            0 :         auto it = opIndexMap_.find(commIndex);
    5292            0 :         if (it != opIndexMap_.end()) {
    5293            0 :             opIndex = ++(it->second);
    5294              :         } else {
    5295            0 :             opIndexMap_.insert({commIndex, 1});
    5296            0 :             opIndex = 1;
    5297              :         }
    5298              : 
    5299            0 :         HCCL_DEBUG("%s tag:%s opType:%u commIndex:%u opIndex:%u",
    5300              :                    __func__, opParam.tag.c_str(), opParam.opType, commIndex, opIndex);
    5301            0 :         return opIndex;
    5302              :     }
    5303              : 
    5304            0 :     HcclResult HcclCommunicator::BuildAicpuCustomParam()
    5305              :     {
    5306            0 :         if (aicpuCustomDev_.ptr() == nullptr) {
    5307            0 :             CHK_RET(CreateWorkSpace(sizeof(AicpuCustomParam), aicpuCustomDev_));
    5308              :         }
    5309              : 
    5310            0 :         opResPara_.aicpuCustomParamAddr = reinterpret_cast<u64>(aicpuCustomDev_.ptr());
    5311            0 :         opResPara_.aicpuCustomParamSize = aicpuCustomDev_.size();
    5312            0 :         HCCL_INFO("%s success, aicpuCustomParamAddr:0x%llx, aicpuCustomParamSize:%llu",
    5313              :                   __func__, opResPara_.aicpuCustomParamAddr, opResPara_.aicpuCustomParamSize);
    5314            0 :         return HCCL_SUCCESS;
    5315              :     }
    5316              : 
    5317            0 :     HcclResult HcclCommunicator::BuildAicpuOrderLaunchNotify()
    5318              :     {
    5319            0 :         if (aicpuOrderNotifyAddr_.ptr() == nullptr) {
    5320            0 :             CHK_RET(CreateWorkSpace(sizeof(HcclSignalInfo) * AICPU_ORDER_NOTIFY_MAX_NUM, aicpuOrderNotifyAddr_));
    5321              :         }
    5322              : 
    5323            0 :         opResPara_.aicpuOrderNotifyAddr = reinterpret_cast<u64>(aicpuOrderNotifyAddr_.ptr());
    5324            0 :         opResPara_.aicpuOrderNotifySize = aicpuOrderNotifyAddr_.size();
    5325            0 :         HCCL_INFO("%s success, aicpuOrderNotifyAddr:0x%llx, aicpuOrderNotifySize:%llu",
    5326              :                   __func__, opResPara_.aicpuOrderNotifyAddr, opResPara_.aicpuOrderNotifySize);
    5327            0 :         return HCCL_SUCCESS;
    5328              :     }
    5329              : 
    5330            0 :     HcclResult HcclCommunicator::BuildAiRmaInfoParam(const std::string &newTag, const std::string &algName, const HcclCMDType opType)
    5331              :     {
    5332            0 :         HCCL_DEBUG("[HcclCommunicator][%s] Start prepare.", __func__);
    5333            0 :         CHK_PTR_NULL(aiRMAInfoMem_);
    5334            0 :         HcclAiRMAInfo *aiRMAInfoPtr = reinterpret_cast<HcclAiRMAInfo*>(aiRMAInfoMem_->ptr());
    5335            0 :         CHK_PTR_NULL(aiRMAInfoPtr);
    5336            0 :         aiRMAInfoPtr->curRankId = userRank_;
    5337            0 :         aiRMAInfoPtr->rankNum = userRankSize_;
    5338            0 :         u32 localRankSize = meshAggregationRankSize_;
    5339            0 :         LevelNSubCommTransport& commTransport = resMap_[newTag].opTransportResponse[COMM_LEVEL0];
    5340            0 :         CHK_PRT_RET(commTransport.size() <= 0, HCCL_ERROR("[%s] no LevelComm resource, please create comm first. "
    5341              :             "tag[%s], curRankId[%u] rankNum[%u]", __func__, newTag.c_str(), aiRMAInfoPtr->curRankId,
    5342              :             aiRMAInfoPtr->rankNum), HCCL_E_INTERNAL);
    5343            0 :         std::vector<LINK>& links = commTransport[0].links;
    5344            0 :         CHK_PRT_RET(links.size() <= 0, HCCL_ERROR("[%s] no transport resource, please create links first. "
    5345              :             "tag[%s], curRankId[%u] rankNum[%u]", __func__, newTag.c_str(), aiRMAInfoPtr->curRankId,
    5346              :             aiRMAInfoPtr->rankNum), HCCL_E_INTERNAL);
    5347              :         
    5348            0 :         LevelNSubCommTransport& tmpCommTransport = resMap_[newTag].opTransportResponse[COMM_MESH_L1];
    5349            0 :         CHK_PRT_RET(tmpCommTransport.size() <= 0, HCCL_ERROR("[%s] no LevelComm resource, please create comm first. "
    5350              :             "tag[%s], curRankId[%u] rankNum[%u]", __func__, newTag.c_str(), aiRMAInfoPtr->curRankId,
    5351              :             aiRMAInfoPtr->rankNum), HCCL_E_INTERNAL);
    5352            0 :         std::vector<LINK>& tmpLinks = tmpCommTransport[0].links;
    5353            0 :         CHK_PRT_RET(tmpLinks.size() <= 0, HCCL_ERROR("[%s] no transport resource, please create links first. "
    5354              :             "tag[%s], curRankId[%u] rankNum[%u]", __func__, newTag.c_str(), aiRMAInfoPtr->curRankId,
    5355              :             aiRMAInfoPtr->rankNum), HCCL_E_INTERNAL);
    5356              :         
    5357            0 :         CHK_RET(GetAivQPInfoV2(tmpLinks, newTag));
    5358            0 :         u32 tmpQueueSize = aiRMAInfoPtr->rankNum * aiRMAInfoPtr->qpNum;
    5359            0 :         u32 tmpMemSize = aiRMAInfoPtr->rankNum;
    5360            0 :         u32 tmpMemDetailSize = aiRMAInfoPtr->rankNum * AiMemMaxNum;
    5361              :         
    5362            0 :         CHK_RET(AllocAndClearHostMem(sizeof(HcclAiRMAWQ) * tmpQueueSize, aiSqMem_));
    5363            0 :         CHK_RET(AllocAndClearHostMem(sizeof(HcclAiRMACQ) * tmpQueueSize, aiScqMem_));
    5364            0 :         CHK_RET(AllocAndClearHostMem(sizeof(HcclAiRMAWQ) * tmpQueueSize, aiRqMem_));
    5365            0 :         CHK_RET(AllocAndClearHostMem(sizeof(HcclAiRMACQ) * tmpQueueSize, aiRcqMem_));
    5366            0 :         CHK_RET(AllocAndClearHostMem(sizeof(HcclAiRMAMemInfo) * tmpMemSize, aiMemMem_));
    5367            0 :         HcclAiRMAMemInfo *aiMemHost = reinterpret_cast<HcclAiRMAMemInfo*>(aiMemMem_->ptr());
    5368              :  
    5369            0 :         CHK_RET(AllocAndClearHostMem(sizeof(MemDetails) * tmpMemDetailSize, aiMemDetailsMem_));
    5370            0 :         MemDetails *aiMemDetailsHost = reinterpret_cast<MemDetails*>(aiMemDetailsMem_->ptr());
    5371              :  
    5372            0 :         CHK_RET(DeviceMem::alloc(aiMemDetailsDev_, aiMemDetailsMem_->size()));
    5373            0 :         u64 memBase = reinterpret_cast<uint64_t>(aiMemDetailsDev_.ptr());
    5374              :  
    5375            0 :         for (u32 i = 0; i < aiRMAInfoPtr->rankNum; i++)
    5376              :         {
    5377            0 :             MemDetails &remoteIn = aiMemDetailsHost[i * AiMemMaxNum +
    5378            0 :                                                     GetAiMemTypeVal(HcclAiRMAMemType::REMOTE_INPUT)];
    5379            0 :             MemDetails &remoteOut = aiMemDetailsHost[i * AiMemMaxNum +
    5380            0 :                                                     GetAiMemTypeVal(HcclAiRMAMemType::REMOTE_OUTPUT)];
    5381            0 :             MemDetails &localIn = aiMemDetailsHost[i * AiMemMaxNum +
    5382            0 :                                                     GetAiMemTypeVal(HcclAiRMAMemType::LOCAL_INPUT)];
    5383            0 :             MemDetails &localOut = aiMemDetailsHost[i * AiMemMaxNum +
    5384            0 :                                                     GetAiMemTypeVal(HcclAiRMAMemType::LOCAL_OUTPUT)];
    5385            0 :             if (i != aiRMAInfoPtr->curRankId && ((i % localRankSize) == (aiRMAInfoPtr->curRankId % localRankSize)  
    5386            0 :                 || (i / localRankSize) == (aiRMAInfoPtr->curRankId / localRankSize))) {
    5387            0 :                 auto transport = links[i % localRankSize]; // localranksize个
    5388            0 :                 if ((i % localRankSize) == (aiRMAInfoPtr->curRankId % localRankSize)){
    5389            0 :                     transport = tmpLinks[i / localRankSize];// servernum个
    5390              :                 }
    5391              :                 // link rank info
    5392            0 :                 CHK_RET(GetTransportRemoteMem(transport, UserMemType::INPUT_MEM, remoteIn));
    5393            0 :                 CHK_RET(GetTransportRemoteMem(transport, UserMemType::OUTPUT_MEM, remoteOut));
    5394            0 :                 CHK_RET(GetTransportLocalMem(transport, UserMemType::INPUT_MEM, localIn));
    5395            0 :                 CHK_RET(GetTransportLocalMem(transport, UserMemType::OUTPUT_MEM, localOut));
    5396              :  
    5397            0 :                 if (transport->GetTransportType() == TransportType::TRANS_TYPE_IBV_EXP) {
    5398            0 :                     CHK_RET(GenIbvAiRMAInfo(i, transport, newTag, aiRMAInfoPtr));
    5399              :                 }
    5400            0 :             } 
    5401            0 :             else if (i == aiRMAInfoPtr->curRankId)
    5402              :             {
    5403            0 :                 void *commInPtr = nullptr;
    5404            0 :                 void *commOutPtr = nullptr;
    5405              :                 u64 commInSize;
    5406              :                 u64 commOutSize;
    5407            0 :                 CHK_RET(cclBufferManager_.GetInCCLbuffer(commInPtr, commInSize));
    5408            0 :                 CHK_RET(cclBufferManager_.GetOutCCLbuffer(commOutPtr, commOutSize));
    5409            0 :                 localIn.addr = reinterpret_cast<uint64_t>(commInPtr);
    5410            0 :                 localIn.size = commInSize;
    5411            0 :                 localOut.addr = reinterpret_cast<uint64_t>(commOutPtr);
    5412            0 :                 localOut.size = commOutSize;
    5413              :             }
    5414              :  
    5415            0 :             aiMemHost[i].memMaxNum = AiMemMaxNum;
    5416            0 :             aiMemHost[i].sizeOfMemDetails = static_cast<u32>(sizeof(MemDetails));
    5417            0 :             aiMemHost[i].memDetailPtr = memBase + i * AiMemMaxNum * aiMemHost[i].sizeOfMemDetails;
    5418              :  
    5419            0 :             HCCL_DEBUG("[%s] tag[%s] curRankId[%u] dstRankId[%u] rankNum[%u] qpNum[%u] memMaxNum[%u] sizeOfMemDetails[%u] "
    5420              :                        "memDetailPtr[%p] remoteInAddr[%p] remoteInSize[%llu] remoteOutAddr[%p] "
    5421              :                        "remoteOutSize[%llu] localInAddr[%p] localInSize[%llu] "
    5422              :                        "localOutAddr[%p] localOutSize[%llu] ",
    5423              :                        __func__, newTag.c_str(), aiRMAInfoPtr->curRankId, i, aiRMAInfoPtr->rankNum, aiRMAInfoPtr->qpNum,
    5424              :                        aiMemHost[i].memMaxNum, aiMemHost[i].sizeOfMemDetails, aiMemHost[i].memDetailPtr,
    5425              :                        remoteIn.addr, remoteIn.size, remoteOut.addr, remoteOut.size,
    5426              :                        localIn.addr, localIn.size, localOut.addr, localOut.size);
    5427              :             }
    5428            0 :         return HCCL_SUCCESS;
    5429              :     }
    5430              : 
    5431              :     template <typename T>
    5432            1 :     HcclResult HcclCommunicator::CopyVectorToDeviceMem(const u64 len, DeviceMem &dstDeviceMem, const std::vector<T> &srcVec)
    5433              :     {
    5434            1 :         CHK_PRT_RET(!len,
    5435              :                     HCCL_INFO("[HcclCommunicator][CopyVectorToDeviceMem] space size is zero. not need to malloc memory"),
    5436              :                     HCCL_SUCCESS);
    5437              : 
    5438              :         CHK_PRT_RET((len > ULONG_MAX),
    5439              :                     HCCL_ERROR("[HcclCommunicator][CopyVectorToDeviceMem] space size is greater than %llu", ULONG_MAX),
    5440              :                     HCCL_E_PARA);
    5441              : 
    5442            0 :         CHK_RET(CreateWorkSpace(len, dstDeviceMem));
    5443            0 :         std::shared_ptr<HostMem> srcHostMem;
    5444            0 :         CHK_RET(AllocAndClearHostMem(len, srcHostMem));
    5445            0 :         std::copy(srcVec.begin(), srcVec.end(), static_cast<T *>(srcHostMem.get()->ptr()));
    5446            0 :         CHK_RET(hrtMemSyncCopy(
    5447              :             dstDeviceMem.ptr(), len, srcHostMem.get()->ptr(), len, HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
    5448            0 :         return HCCL_SUCCESS;
    5449            0 :     }
    5450              : 
    5451            0 :     HcclResult HcclCommunicator::BuildOpTopoResTlvParam(const std::string &algName,
    5452              :                                                         const std::vector<std::vector<std::vector<u32>>> &inputVectorInfo, DeviceMem &dstTlvDeviceMem, u64 &tlvLen)
    5453              :     {
    5454            0 :         vector<u32> tlv;
    5455              :         CommonTlv commonTlv;
    5456            0 :         HCCL_DEBUG("[HcclCommunicator][BuildOpTopoResTlvParam] input vector size[%lu], group[%s].",
    5457              :                    inputVectorInfo.size(), identifier_.c_str());
    5458            0 :         for (u16 level0Idx = 0; level0Idx < inputVectorInfo.size(); level0Idx++) {
    5459            0 :             for (u16 level1Idx = 0; level1Idx < inputVectorInfo[level0Idx].size(); level1Idx++) {
    5460            0 :                 commonTlv.type = ((level0Idx << TOP_COMM_LEVEL0_SHIFT) | level1Idx);
    5461            0 :                 commonTlv.length = (sizeof(LENGTH_TYPE) + sizeof(TAG_TYPE)) +
    5462            0 :                                    inputVectorInfo[level0Idx][level1Idx].size() * sizeof(RANK_TYPE);
    5463            0 :                 tlv.push_back(commonTlv.type);
    5464            0 :                 tlv.push_back(commonTlv.length);
    5465            0 :                 tlv.insert(tlv.end(), inputVectorInfo[level0Idx][level1Idx].begin(),
    5466            0 :                            inputVectorInfo[level0Idx][level1Idx].end());
    5467              :             }
    5468              :         }
    5469            0 :         for (u64 idx = 0; idx < tlv.size(); idx++) {
    5470            0 :             HCCL_DEBUG("[HcclCommunicator][BuildOpTopoResTlvParam] idx[%lu] tlv[%lu].", idx, tlv[idx]);
    5471              :         }
    5472            0 :         tlvLen = tlv.size() * sizeof(u32);
    5473            0 :         CHK_RET(CopyVectorToDeviceMem(tlvLen, dstTlvDeviceMem, tlv));
    5474            0 :         return HCCL_SUCCESS;
    5475            0 :     }
    5476              : 
    5477            0 :     HcclResult HcclCommunicator::BuildOpTopoResVectorTlvParam(const std::string &algName,
    5478              :                                                               const std::vector<std::vector<std::vector<std::vector<u32>>>> &inputVectorInfo, DeviceMem &dstTlvDeviceMem, u64 &tlvLen)
    5479              :     {
    5480            0 :         vector<u32> tlv;
    5481              :         CommonTlv commonTlv;
    5482            0 :         HCCL_DEBUG("[HcclCommunicator][BuildOpTopoResVectorTlvParam] input vector size[%lu], group[%s]",
    5483              :                    inputVectorInfo.size(), identifier_.c_str());
    5484            0 :         for (u16 level0Idx = 0; level0Idx < inputVectorInfo.size(); level0Idx++) {
    5485            0 :             for (u16 level1Idx = 0; level1Idx < inputVectorInfo[level0Idx].size(); level1Idx++) {
    5486            0 :                 for (u16 level2Idx = 0; level2Idx < inputVectorInfo[level0Idx][level1Idx].size(); level2Idx++) {
    5487            0 :                     commonTlv.type = (((level0Idx << TOP_HIERARCHICAL_COMM_LEVEL0_SHIFT) | level1Idx) << TOP_HIERARCHICAL_COMM_LEVEL1_SHIFT) | level2Idx;
    5488            0 :                     commonTlv.length = (sizeof(LENGTH_TYPE) + sizeof(TAG_TYPE)) +
    5489            0 :                                        inputVectorInfo[level0Idx][level1Idx][level2Idx].size() * sizeof(RANK_TYPE);
    5490            0 :                     tlv.push_back(commonTlv.type);
    5491            0 :                     tlv.push_back(commonTlv.length);
    5492            0 :                     tlv.insert(tlv.end(), inputVectorInfo[level0Idx][level1Idx][level2Idx].begin(),
    5493            0 :                                inputVectorInfo[level0Idx][level1Idx][level2Idx].end());
    5494              :                 }
    5495              :             }
    5496              :         }
    5497            0 :         for (u64 idx = 0; idx < tlv.size(); idx++) {
    5498            0 :             HCCL_DEBUG("[HcclCommunicator][BuildOpTopoResVectorTlvParam] idx[%lu] tlv[%lu]", idx, tlv[idx]);
    5499              :         }
    5500            0 :         tlvLen = tlv.size() * sizeof(u32);
    5501            0 :         CHK_RET(CopyVectorToDeviceMem(tlvLen, dstTlvDeviceMem, tlv));
    5502            0 :         return HCCL_SUCCESS;
    5503            0 :     }
    5504              : 
    5505            0 :     HcclResult HcclCommunicator::BuildPairLinkCounter(const std::string &algName)
    5506              :     {
    5507            0 :         constexpr u32 KEY_VALUE_TO_VECTOR_MODULUS = 2;
    5508            0 :         if (pairLinkCounterDevice_.ptr() == nullptr) {
    5509            0 :             u64 pairLinkCounterSize = pairLinkCounter_.size();
    5510            0 :             HCCL_DEBUG("[HcclCommunicator][BuildPairLinkCounter] pairLinkCounter size[%lu], group[%s]",
    5511              :                        pairLinkCounterSize, identifier_.c_str());
    5512            0 :             std::vector<u32> pairLinkCounterVec(pairLinkCounterSize * KEY_VALUE_TO_VECTOR_MODULUS);
    5513            0 :             u64 index = 0;
    5514            0 :             for (auto &kt : pairLinkCounter_) {
    5515            0 :                 pairLinkCounterVec[index] = kt.first;
    5516            0 :                 pairLinkCounterVec[index + 1] = kt.second;
    5517            0 :                 index += KEY_VALUE_TO_VECTOR_MODULUS; // 每次根据
    5518              :             }
    5519            0 :             u64 len = pairLinkCounterSize * sizeof(u32) * KEY_VALUE_TO_VECTOR_MODULUS; // key-value,都为u32
    5520            0 :             CHK_RET(CopyVectorToDeviceMem(len, pairLinkCounterDevice_, pairLinkCounterVec));
    5521            0 :             opResPara_.topoInfo.pairLinkCounter = reinterpret_cast<u64>(pairLinkCounterDevice_.ptr());
    5522            0 :             opResPara_.topoInfo.pairLinkCounterNum = pairLinkCounterSize * KEY_VALUE_TO_VECTOR_MODULUS;
    5523            0 :         }
    5524            0 :         return HCCL_SUCCESS;
    5525              :     }
    5526              : 
    5527            0 :     HcclResult HcclCommunicator::BuildIsUsedRdmaRank(const std::string &algName)
    5528              :     {
    5529            0 :         constexpr u32 KEY_VALUE_TO_VECTOR_MODULUS = 2;
    5530            0 :         if (isUsedRdmaRankPairDevice_.ptr() == nullptr) {
    5531            0 :             std::unordered_map<u32, bool> isUsedRdmaMap;
    5532            0 :             CHK_RET(implAlg_->GetIsUsedRdmaMap(isUsedRdmaMap));
    5533            0 :             u64 isUsedRdmaMapSize = isUsedRdmaMap.size();
    5534            0 :             HCCL_DEBUG("[HcclCommunicator][BuildIsUsedRdmaRank] is used Rdma rank size[%lu], group[%s]",
    5535              :                        isUsedRdmaMapSize, identifier_.c_str());
    5536            0 :             std::vector<u32> isUsedRdmaPairVec(isUsedRdmaMapSize * KEY_VALUE_TO_VECTOR_MODULUS);
    5537            0 :             u64 index = 0;
    5538            0 :             for (auto &kt : isUsedRdmaMap) {
    5539            0 :                 isUsedRdmaPairVec[index] = kt.first;
    5540            0 :                 isUsedRdmaPairVec[index + 1] = static_cast<u32>(kt.second);
    5541            0 :                 index += KEY_VALUE_TO_VECTOR_MODULUS;
    5542              :             }
    5543            0 :             u64 len = isUsedRdmaMapSize * sizeof(u32) * KEY_VALUE_TO_VECTOR_MODULUS; // key-value,都为u32
    5544            0 :             CHK_RET(CopyVectorToDeviceMem(len, isUsedRdmaRankPairDevice_, isUsedRdmaPairVec));
    5545            0 :             opResPara_.topoInfo.isUsedRdmaRankPair = reinterpret_cast<u64>(isUsedRdmaRankPairDevice_.ptr());
    5546            0 :             opResPara_.topoInfo.isUsedRdmaRankPairNum = isUsedRdmaMapSize * KEY_VALUE_TO_VECTOR_MODULUS;
    5547            0 :         }
    5548            0 :         return HCCL_SUCCESS;
    5549              :     }
    5550              : 
    5551            0 :     HcclResult HcclCommunicator::BuildNicList(const std::string &algName)
    5552              :     {
    5553            0 :         if (nicListDevice_.ptr() == nullptr) {
    5554            0 :             u64 len = nicList_.size() * sizeof(u32);
    5555            0 :             HCCL_DEBUG("[HcclCommunicator][BuildNicList] niclist size[%lu], group[%s]",
    5556              :                        nicList_.size(), identifier_.c_str());
    5557            0 :             CHK_RET(CopyVectorToDeviceMem(len, nicListDevice_, nicList_));
    5558            0 :             opResPara_.topoInfo.nicList = reinterpret_cast<u64>(nicListDevice_.ptr());
    5559            0 :             opResPara_.topoInfo.nicNum = nicList_.size();
    5560              :         }
    5561            0 :         return HCCL_SUCCESS;
    5562              :     }
    5563              : 
    5564            0 :     HcclResult HcclCommunicator::BuildBridgeRank(const std::string &algName)
    5565              :     {
    5566            0 :         if (bridgeRankDevice_.ptr() == nullptr) {
    5567            0 :             std::vector<bool> isBridgeVector;
    5568            0 :             CHK_RET(implAlg_->GetIsBridgeVector(isBridgeVector));
    5569            0 :             u64 len = isBridgeVector.size() * sizeof(bool);
    5570            0 :             HCCL_DEBUG("[HcclCommunicator][BuildBridgeRank] Bridge size[%lu], group[%s]",
    5571              :                        isBridgeVector.size(), identifier_.c_str());
    5572            0 :             CHK_RET(CopyVectorToDeviceMem(len, bridgeRankDevice_, isBridgeVector));
    5573            0 :             opResPara_.topoInfo.bridgeRank = reinterpret_cast<u64>(bridgeRankDevice_.ptr());
    5574            0 :             opResPara_.topoInfo.bridgeRankNum = isBridgeVector.size();
    5575            0 :         }
    5576            0 :         return HCCL_SUCCESS;
    5577              :     }
    5578              : 
    5579            0 :     HcclResult HcclCommunicator::BuildCommPlanRank(const std::string &algName)
    5580              :     {
    5581            0 :         opResPara_.topoInfo.complanRank = 0;
    5582            0 :         opResPara_.topoInfo.complanRankLength = 0;
    5583            0 :         if (complanRankDevice_.ptr() == nullptr) {
    5584            0 :             std::vector<std::vector<std::vector<u32>>> commPlaneRanks;
    5585            0 :             CHK_RET(implAlg_->GetCommPlaneRanks(commPlaneRanks));
    5586            0 :             u64 tlvLen = 0;
    5587            0 :             CHK_RET(BuildOpTopoResTlvParam(algName, commPlaneRanks, complanRankDevice_, tlvLen));
    5588            0 :             opResPara_.topoInfo.complanRank = reinterpret_cast<u64>(complanRankDevice_.ptr());
    5589            0 :             opResPara_.topoInfo.complanRankLength = tlvLen;
    5590            0 :             HCCL_DEBUG("[HcclCommunicator][BuildCommPlanRank] comm plane ranks tlv length[%lu], ptr[%p], group[%s], "
    5591              :                        "local user rankId[%u] ",
    5592              :                        tlvLen, complanRankDevice_.ptr(), identifier_.c_str(), userRank_);
    5593            0 :         }
    5594            0 :         return HCCL_SUCCESS;
    5595              :     }
    5596              : 
    5597            0 :     HcclResult HcclCommunicator::BuildServerAndsuperPodRank(const std::string &algName)
    5598              :     {
    5599            0 :         opResPara_.topoInfo.serverAndsuperPodRank = 0;
    5600            0 :         opResPara_.topoInfo.serverAndsuperPodRankLength = 0;
    5601            0 :         if (serverAndsuperPodToRankDevice_.ptr() == nullptr) {
    5602            0 :             std::vector<std::vector<std::vector<u32>>> serverAndsuperPodToRank;
    5603            0 :             CHK_RET(implAlg_->GetRankVecInfo(serverAndsuperPodToRank));
    5604            0 :             u64 tlvLen = 0;
    5605            0 :             CHK_RET(BuildOpTopoResTlvParam(algName, serverAndsuperPodToRank, serverAndsuperPodToRankDevice_, tlvLen));
    5606            0 :             opResPara_.topoInfo.serverAndsuperPodRank = reinterpret_cast<u64>(serverAndsuperPodToRankDevice_.ptr());
    5607            0 :             opResPara_.topoInfo.serverAndsuperPodRankLength = tlvLen;
    5608            0 :             HCCL_DEBUG("[HcclCommunicator][BuildServerAndsuperPodRank] server and super pod ranks tlv length[%lu], ptr[%p], "
    5609              :                        "group[%s],  local user rankId[%u] ",
    5610              :                        tlvLen, serverAndsuperPodToRankDevice_.ptr(),
    5611              :                        identifier_.c_str(), userRank_);
    5612            0 :         }
    5613            0 :         return HCCL_SUCCESS;
    5614              :     }
    5615              : 
    5616            0 :     HcclResult HcclCommunicator::BuildOpRetryParam(const AlgResourceResponse &algResource, const std::string &newTag)
    5617              :     {
    5618            0 :         opResPara_.config.retryEnable = static_cast<u8>(retryEnable_);
    5619            0 :         opResPara_.config.retryHoldTime = commConfig_.GetConfigRetryHoldTime();
    5620            0 :         opResPara_.config.retryIntervalTime = commConfig_.GetConfigRetryIntervalTime();
    5621              :         // aicpu和custom共用同一个opResPara_,aicpu初始化完成后,会修改h2d/d2h的指针,然后重新传给custom
    5622            0 :         opResPara_.kfcControlTransferH2DParams = kfcControlTransferH2D_->GetCommunicateParams();
    5623            0 :         opResPara_.kfcStatusTransferD2HParams = kfcStatusTransferD2H_->GetCommunicateParams();
    5624            0 :         opResPara_.debugConfig = GetDebugConfig();
    5625              : 
    5626            0 :         CHK_SMART_PTR_NULL(opRetryStreamPtr_);
    5627            0 :         if (opRetryStreamPtr_->find(newTag) == opRetryStreamPtr_->end()) {
    5628            0 :             std::vector<Stream> retryStreams(algResource.slaveDevStreams.begin(), algResource.slaveDevStreams.end());
    5629            0 :             retryStreams.push_back(opMainStream_);
    5630            0 :             opRetryStreamPtr_->insert(std::make_pair(newTag, retryStreams));
    5631            0 :         }
    5632            0 :         return HCCL_SUCCESS;
    5633              :     }
    5634              : 
    5635            0 :     HcclResult HcclCommunicator::BuildCommPlaneSubGroupRank(const std::string &algName)
    5636              :     {
    5637            0 :         opResPara_.hierarchicalAlgInfo.commplaneSubGroupRank = 0;
    5638            0 :         opResPara_.hierarchicalAlgInfo.commplaneSubGroupRankLength = 0;
    5639            0 :         if (commplaneSubGroupRankDevice_.ptr() == nullptr) {
    5640            0 :             std::vector<std::vector<std::vector<std::vector<u32>>>> commplaneSubGroupVector;
    5641            0 :             CHK_RET(implAlg_->GetCommPlaneSubGroupVector(commplaneSubGroupVector));
    5642            0 :             u64 tlvLen = 0;
    5643            0 :             CHK_RET(BuildOpTopoResVectorTlvParam(algName, commplaneSubGroupVector, commplaneSubGroupRankDevice_, tlvLen));
    5644            0 :             opResPara_.hierarchicalAlgInfo.commplaneSubGroupRank = reinterpret_cast<u64>(commplaneSubGroupRankDevice_.ptr());
    5645            0 :             opResPara_.hierarchicalAlgInfo.commplaneSubGroupRankLength = tlvLen;
    5646            0 :             HCCL_DEBUG("[HcclCommunicator][BuildCommPlaneSubGroupRank] comm plane subGroups ranks tlv length[%lu], ptr[%p], "
    5647              :                        "group[%s],  local user rankId[%u] ",
    5648              :                        tlvLen, commplaneSubGroupRankDevice_.ptr(),
    5649              :                        identifier_.c_str(), userRank_);
    5650            0 :         }
    5651            0 :         return HCCL_SUCCESS;
    5652              :     }
    5653              : 
    5654            0 :     HcclResult HcclCommunicator::BuildHierarchicalAlgOption(u32 *ahcConfInfo)
    5655              :     {
    5656            0 :         std::map<AHCConcOpType, TemplateType> hierarchicalAlgOption;
    5657            0 :         CHK_RET(implAlg_->GetAHCAlgOption(hierarchicalAlgOption));
    5658            0 :         ahcConfInfo[TOP_HIERARCHICAL_CONF_lENGTH_INDEX] = hierarchicalAlgOption.size();
    5659              : 
    5660            0 :         if (hierarchicalAlgOption.size() >= (TOP_HIERARCHICAL_CONF_SIZE-1)) {
    5661            0 :             HCCL_ERROR("[HcclCommunicator][BuildHierarchicalAlgOption] host hierarchicalAlgOption size[%u]  exceed maxsize[%u]",
    5662              :                 hierarchicalAlgOption.size(), (TOP_HIERARCHICAL_CONF_SIZE-1));
    5663            0 :             return HCCL_E_INTERNAL;
    5664              :         }
    5665              : 
    5666            0 :         HCCL_DEBUG("[HcclCommunicator][BuildHierarchicalAlgOption] host hierarchicalAlgOption.size() [%u]", hierarchicalAlgOption.size());
    5667              : 
    5668              :         //默认清空内存
    5669            0 :         for (u32 i = TOP_HIERARCHICAL_CONF_INFO_INDEX ; i < TOP_HIERARCHICAL_CONF_SIZE; i++) {
    5670            0 :             ahcConfInfo[i] = 0;
    5671              :         }
    5672              : 
    5673            0 :         u32  confDataStartIndex = TOP_HIERARCHICAL_CONF_INFO_INDEX;
    5674            0 :         for (auto it = hierarchicalAlgOption.begin(); it != hierarchicalAlgOption.end(); ++it) {
    5675            0 :             HCCL_DEBUG("[HcclCommunicator][BuildHierarchicalAlgOption] host Level [%u], ConcType[%u] AHCOpType[%u], TemplateType [%u]",
    5676              :                 it->first.ahcLevel, it->first.concType, it->first.ahcOpType, it->second);
    5677              : 
    5678            0 :             u32 confData = (static_cast<u32>(it->first.ahcLevel) << TOP_HIERARCHICAL_CONF_LEVEL_SHIFT) |
    5679            0 :                         (static_cast<u32>(it->first.concType) << TOP_HIERARCHICAL_CONF_CONC_TYPE_SHIFT) |
    5680            0 :                         (static_cast<u32>(it->first.ahcOpType) << TOP_HIERARCHICAL_CONF_OP_TYPE_SHIFT) |
    5681            0 :                         (static_cast<u32>(it->second) << TOP_HIERARCHICAL_CONF_TEMPLATE_TYPE_SHIFT);
    5682            0 :             ahcConfInfo[confDataStartIndex] = confData;
    5683            0 :             confDataStartIndex = confDataStartIndex + 1;
    5684              :         }
    5685            0 :         return HCCL_SUCCESS;
    5686            0 :     }
    5687              : 
    5688              : 
    5689            0 :     HcclResult HcclCommunicator::BuildOpTopoResParam(const std::string &algName, const AlgResourceResponse &algResource)
    5690              :     {
    5691            0 :         opResPara_.topoInfo.userRank = userRank_;
    5692            0 :         opResPara_.topoInfo.userRankSize = userRankSize_;
    5693            0 :         opResPara_.topoInfo.deviceLogicId = deviceLogicId_;
    5694            0 :         opResPara_.topoInfo.isSingleMeshAggregation = isSingleMeshAggregation_;
    5695            0 :         opResPara_.topoInfo.deviceNumPerAggregation = deviceNumPerAggregation_;
    5696            0 :         opResPara_.topoInfo.superPodNum = superPodNum_;
    5697            0 :         opResPara_.topoInfo.devicePhyId = devicePhyId_;
    5698            0 :         opResPara_.topoInfo.deviceType = static_cast<u32>(deviceType_);
    5699              :         TopoType topoType;
    5700            0 :         CHK_RET(implAlg_->GetTopoType(topoType));
    5701            0 :         opResPara_.topoInfo.topoType = static_cast<u32>(topoType);
    5702            0 :         opResPara_.topoInfo.serverNum = serverNum_;
    5703            0 :         opResPara_.topoInfo.meshAggregationRankSize = meshAggregationRankSize_;
    5704            0 :         opResPara_.topoInfo.multiModuleDiffDeviceNumMode = multiModuleDiffDeviceNumMode_;
    5705            0 :         opResPara_.topoInfo.multiSuperPodDiffServerNumMode = multiSuperPodDiffServerNumMode_;
    5706            0 :         opResPara_.topoInfo.realUserRank = realUserRank_;
    5707            0 :         opResPara_.topoInfo.isDiffDeviceModule = isDiffDeviceModule_;
    5708            0 :         opResPara_.topoInfo.isDiffDeviceType = isDiffDeviceType_;
    5709            0 :         opResPara_.topoInfo.gcdDeviceNumPerAggregation = gcdDeviceNumPerAggregation_;
    5710            0 :         opResPara_.topoInfo.moduleNum = moduleNum_;
    5711            0 :         opResPara_.isARSDoubleRing = isARSDoubleRing_;
    5712            0 :         opResPara_.multiSuperPodDiffDeviceNumMode = multiSuperPodDiffDeviceNumMode_;
    5713            0 :         CHK_RET(BuildPairLinkCounter(algName));
    5714            0 :         CHK_RET(BuildIsUsedRdmaRank(algName));
    5715            0 :         CHK_RET(BuildNicList(algName));
    5716            0 :         CHK_RET(BuildBridgeRank(algName));
    5717            0 :         CHK_RET(BuildCommPlanRank(algName));
    5718            0 :         CHK_RET(BuildServerAndsuperPodRank(algName));
    5719            0 :         CHK_RET(BuildCommPlaneSubGroupRank(algName));
    5720            0 :         return HCCL_SUCCESS;
    5721              :     }
    5722              : 
    5723            0 :     HcclResult HcclCommunicator::BuildOpRemoteLinkP2pResParam(const LINK &link, HccltagRemoteResV3 &tagRemoteRes,
    5724              :                                                               TransportLinkType linkType)
    5725              :     {
    5726              :         // hccs sio并发场景,sio链路(linkTyp为SIO)打包到linkP2pSio, hccs链路(linkTyp为HCCS)打包到linkP2p;
    5727              :         // 其他场景打包到linkP2p
    5728            0 :         HcclLinkP2pV2 *linkp2p = &(tagRemoteRes.tagRemoteResPtr->linkP2p);
    5729            0 :         if (linkType == TransportLinkType::SIO) {
    5730            0 :             linkp2p = &(tagRemoteRes.tagRemoteResPtr->linkP2pSio);
    5731              :         }
    5732            0 :         if (linkp2p->localIpcSignal[0].resId != INVALID_U64) {
    5733            0 :             HCCL_INFO("[%s]the linkP2p is existed, no need to refresh transport resource, resId[%llu]",
    5734              :                       __func__, linkp2p->localIpcSignal[0].resId);
    5735            0 :             return HCCL_SUCCESS;
    5736              :         }
    5737              :         // localMem & remoteMem
    5738            0 :         void *inbufferPtr = nullptr;
    5739            0 :         void *outbufferPtr = nullptr;
    5740            0 :         CHK_RET(link->GetRemoteMem(UserMemType::INPUT_MEM, &inbufferPtr));
    5741            0 :         CHK_RET(link->GetRemoteMem(UserMemType::OUTPUT_MEM, &outbufferPtr));
    5742            0 :         (linkp2p->remoteMem)[INPUT].addr = reinterpret_cast<u64>(inbufferPtr);
    5743            0 :         (linkp2p->remoteMem)[OUTPUT].addr = reinterpret_cast<u64>(outbufferPtr);
    5744            0 :         CHK_RET(link->GetRemoteMemSize(UserMemType::INPUT_MEM, (linkp2p->remoteMem)[INPUT].size));
    5745            0 :         CHK_RET(link->GetRemoteMemSize(UserMemType::OUTPUT_MEM, (linkp2p->remoteMem)[OUTPUT].size));
    5746            0 :         MemDetails localMem; // 暂时预留,赋值为空
    5747            0 :         (linkp2p->localMem)[0] = localMem;
    5748            0 :         (linkp2p->localMem)[1] = localMem;
    5749            0 :         HCCL_DEBUG("[%s] finish set localMem & remoteMem info", __func__);
    5750              :         // localnotify & remotenotify
    5751            0 :         u64 notifyNum = 0;
    5752            0 :         std::vector<HcclSignalInfo> locIpcSignals;
    5753            0 :         std::vector<HcclSignalInfo> rmtIpcSignals;
    5754            0 :         CHK_RET(link->GetLocalNotify(locIpcSignals));
    5755            0 :         CHK_RET(link->GetRemoteNotify(rmtIpcSignals));
    5756              : 
    5757            0 :         for (size_t i = 0; i < locIpcSignals.size(); i++) {
    5758            0 :             CHK_RET(CheckNotifyOrQPMaxNum(notifyNum, LINK_P2P_MAX_NUM, true));
    5759            0 :             linkp2p->localIpcSignal[notifyNum] = locIpcSignals[i];
    5760            0 :             linkp2p->remoteIpcSignal[notifyNum] = rmtIpcSignals[i];
    5761            0 :             notifyNum++;
    5762              :         }
    5763            0 :         tagRemoteRes.p2pNotifyNum = notifyNum;
    5764            0 :         HCCL_DEBUG("[%s] finish set localnotify & remotenotify info, notifyNum[%llu]", __func__, notifyNum);
    5765              :         // transportAttr
    5766            0 :         CHK_RET(link->GetTransportAttr(linkp2p->transportAttr));
    5767            0 :         HCCL_DEBUG("[%s] finish set RemoteLinkP2pResParam info", __func__);
    5768            0 :         return HCCL_SUCCESS;
    5769            0 :     }
    5770              : 
    5771            0 :     HcclResult HcclCommunicator::BuildOpRemoteLinkRoceResParam(const LINK &link, HccltagRemoteResV3 &tagRemoteRes,
    5772              :                                                                bool isBackup, bool isRetry, bool isSecondBuild)
    5773              :     {
    5774            0 :         u32 iter = isSecondBuild ? 2 : 0;
    5775            0 :         HcclLinkRoceV2 *linkRoce = isBackup ? &(tagRemoteRes.tagRemoteResPtr->linkRoce[AICPU_RETRY_LINKROCE_BACKUP + iter])
    5776            0 :                                             : &(tagRemoteRes.tagRemoteResPtr->linkRoce[AICPU_RETRY_LINKROCE_DEFAULT + iter]);
    5777            0 :         if (!isRetry && linkRoce->localNotifyList != 0) {
    5778            0 :             HCCL_INFO("[%s]the linkRoce is existed, no need to refresh transport resource, localNotifyListPtr[%p], iter[%u]",
    5779              :                       __func__, reinterpret_cast<void *>(linkRoce->localNotifyList), iter);
    5780            0 :             return HCCL_SUCCESS;
    5781              :         }
    5782              :         // localMem & remoteMem
    5783            0 :         CHK_RET(link->GetLocalMemDetails(UserMemType::INPUT_MEM, (linkRoce->localMem)[INPUT]));
    5784            0 :         CHK_RET(link->GetLocalMemDetails(UserMemType::OUTPUT_MEM, (linkRoce->localMem)[OUTPUT]));
    5785            0 :         void *inbufferPtr = nullptr;
    5786            0 :         void *outbufferPtr = nullptr;
    5787            0 :         CHK_RET(link->GetRemoteMem(UserMemType::INPUT_MEM, &inbufferPtr));
    5788            0 :         CHK_RET(link->GetRemoteMem(UserMemType::OUTPUT_MEM, &outbufferPtr));
    5789            0 :         HCCL_DEBUG("[%s]inbufferPtr[%p], outbufferPtr[%p]", __func__, inbufferPtr, outbufferPtr);
    5790            0 :         if (inbufferPtr == nullptr || outbufferPtr == nullptr) {
    5791            0 :             HCCL_ERROR("[%s]inbufferPtr[%p], outbufferPtr[%p]", __func__, inbufferPtr, outbufferPtr);
    5792            0 :             return HCCL_E_INTERNAL;
    5793              :         }
    5794            0 :         (linkRoce->remoteMem)[INPUT].addr = reinterpret_cast<u64>(inbufferPtr);
    5795            0 :         (linkRoce->remoteMem)[OUTPUT].addr = reinterpret_cast<u64>(outbufferPtr);
    5796            0 :         CHK_RET(link->GetRemoteMemKey(UserMemType::INPUT_MEM, &((linkRoce->remoteMem)[INPUT].key)));
    5797            0 :         CHK_RET(link->GetRemoteMemKey(UserMemType::OUTPUT_MEM, &((linkRoce->remoteMem)[OUTPUT].key)));
    5798            0 :         CHK_RET(link->GetRemoteMemSize(UserMemType::INPUT_MEM, (linkRoce->remoteMem)[INPUT].size));
    5799            0 :         CHK_RET(link->GetRemoteMemSize(UserMemType::OUTPUT_MEM, (linkRoce->remoteMem)[OUTPUT].size));
    5800            0 :         HCCL_DEBUG("[%s] finish set localMem & remoteMem info", __func__);
    5801              :         // notifyValue & Key
    5802            0 :         std::vector<AddrKey> notifyValueAddrKey;
    5803            0 :         CHK_RET(link->GetLocalNotifyValueAddrKey(notifyValueAddrKey));
    5804            0 :         linkRoce->notifyValue = notifyValueAddrKey[0].addr;
    5805            0 :         linkRoce->notifyValueKey = notifyValueAddrKey[0].key;
    5806              :         // QPInfo
    5807            0 :         std::vector<HcclQpInfoV2> aiQpInfos;
    5808            0 :         CHK_RET(link->GetAiQpInfo(aiQpInfos));
    5809            0 :         u32 qpNum = aiQpInfos.size();
    5810            0 :         if (qpNum > RDMA_QP_MAX_NUM || qpNum < 1) {
    5811            0 :             return HCCL_E_INTERNAL;
    5812              :         }
    5813            0 :         std::copy_n(aiQpInfos.begin(), qpNum, linkRoce->QpInfo);
    5814            0 :         linkRoce->qpsPerConnection = qpNum - static_cast<u32>(qpNum > 1); // 多QP数量或单QP模式
    5815              : 
    5816              :         // localnotify & remotenotify
    5817            0 :         std::vector<AddrKey> notifyAddrKey;
    5818            0 :         std::vector<HcclSignalInfo> signalInfos;
    5819            0 :         CHK_RET(link->GetLocalRdmaNotify(signalInfos));
    5820            0 :         CHK_RET(link->GetRemoteRdmaNotifyAddrKey(notifyAddrKey));
    5821            0 :         if ((signalInfos.size() != notifyAddrKey.size()) || (signalInfos.size() < RDMA_NOTIFY_MIN_NUM) ||
    5822            0 :             (signalInfos.size() > RDMA_NOTIFY_MAX_NUM) || (notifyAddrKey.size() < RDMA_NOTIFY_MIN_NUM) ||
    5823            0 :             (notifyAddrKey.size() > RDMA_NOTIFY_MAX_NUM) ||
    5824            0 :             ((signalInfos.size() - RDMA_NOTIFY_MIN_NUM) % linkRoce->qpsPerConnection) ||
    5825            0 :             ((notifyAddrKey.size() - RDMA_NOTIFY_MIN_NUM) % linkRoce->qpsPerConnection)) {
    5826            0 :             HCCL_ERROR("[HcclCommunicator][BuildOpRemoteLinkRoceResParam] signalInfos %zu notifyAddrKey %zu "
    5827              :                 "qpsPerConnection %u", signalInfos.size(), notifyAddrKey.size(), linkRoce->qpsPerConnection);
    5828            0 :             return HCCL_E_INTERNAL;
    5829              :         }
    5830            0 :         u64 notifyNum = (notifyAddrKey.size() - RDMA_NOTIFY_MIN_NUM) / linkRoce->qpsPerConnection - static_cast<u32>(linkRoce->qpsPerConnection > 1);
    5831            0 :         linkRoce->singleQPNotifyNum = notifyNum;
    5832              : 
    5833            0 :         u64 len = signalInfos.size() * sizeof(HcclSignalInfo);
    5834            0 :         DeviceMem localNotifyListMem;
    5835            0 :         CHK_RET(CopyVectorToDeviceMem(len, localNotifyListMem, signalInfos));
    5836            0 :         linkRoce->localNotifyList = reinterpret_cast<u64>(localNotifyListMem.ptr());
    5837            0 :         ibverbsLocalNotify_[tagRemoteRes.tagRemoteResPtr->tag].emplace_back(std::move(localNotifyListMem));
    5838              : 
    5839            0 :         len = notifyAddrKey.size() * sizeof(AddrKey);
    5840            0 :         DeviceMem remoteNotifyListMem;
    5841            0 :         CHK_RET(CopyVectorToDeviceMem(len, remoteNotifyListMem, notifyAddrKey));
    5842            0 :         linkRoce->remoteNotifyList = reinterpret_cast<u64>(remoteNotifyListMem.ptr());
    5843            0 :         ibverbsRemoteNotify_[tagRemoteRes.tagRemoteResPtr->tag].emplace_back(std::move(remoteNotifyListMem));
    5844              : 
    5845            0 :         HCCL_DEBUG("[%s] finish set localnotify & remotenotify info, notifyNum[%llu], linkNotifyNum[%llu]",
    5846              :                    __func__, notifyNum, signalInfos.size());
    5847              : 
    5848            0 :         if (isBackup) {
    5849            0 :             tagRemoteRes.roceNotifyNumBackup = linkRoce->singleQPNotifyNum;
    5850            0 :             tagRemoteRes.qpNumBackup = linkRoce->qpsPerConnection;
    5851              :         } else {
    5852            0 :             tagRemoteRes.roceNotifyNum = linkRoce->singleQPNotifyNum;
    5853            0 :             tagRemoteRes.qpNum = linkRoce->qpsPerConnection;
    5854              :         }
    5855              : 
    5856            0 :         linkRoce->useAtomicWrite = link->GetIsUseAtomicWrite();
    5857            0 :         HCCL_DEBUG("[%s] finish set Qp info qpNum[%u], linkRoce->localNotifyList[0].resId[%llu], "
    5858              :                    "notifyNum[%u], isBackup[%d], isSecond[%d], qpPtr[%llu], useAtomicWrite[%d]",
    5859              :                    __func__, linkRoce->qpsPerConnection,
    5860              :                    signalInfos[0].resId, linkRoce->singleQPNotifyNum, isBackup, isSecondBuild,
    5861              :                    linkRoce->QpInfo[0].qpPtr, linkRoce->useAtomicWrite);
    5862            0 :         return HCCL_SUCCESS;
    5863            0 :     }
    5864              : 
    5865              :     template <typename T>
    5866            0 :     HcclResult HcclCommunicator::CreateListNode(T **resHostPtr, T **resDevicePtr)
    5867              :     {
    5868            0 :         hostMemVec_.resize(hostMemVec_.size() + 1);
    5869            0 :         CHK_RET(AllocAndClearHostMem(sizeof(T), hostMemVec_.back()));
    5870            0 :         *resHostPtr = static_cast<T *>(hostMemVec_.back().get()->ptr());
    5871              : 
    5872            0 :         deviceMemVec_.resize(deviceMemVec_.size() + 1);
    5873            0 :         CHK_RET(AllocAndClearDeviceMem(sizeof(T), deviceMemVec_.back()));
    5874              : 
    5875            0 :         *resDevicePtr = static_cast<T *>(deviceMemVec_.back().get()->ptr());
    5876              :         // 初始化HcclRankRelationResV2中的tagRes链表
    5877            0 :         ListCommonInit(&((*resDevicePtr)->nextTagRes), &((*resHostPtr)->nextTagRes));
    5878            0 :         return HCCL_SUCCESS;
    5879              :     }
    5880              : 
    5881            0 :     HcclResult HcclCommunicator::BuildRemoteResByTag(const std::string &newTag, const u32 &usrRankId,
    5882              :                                                      HcclRankRelationResV2 *&rankRelationResHostPtr, HcclRankRelationResV2 *&rankRelationResDevicePtr, bool isBackup,
    5883              :                                                      bool isRetry)
    5884              :     {
    5885            0 :         HCCL_DEBUG("[%s]start to add RemoteRes with newtag[%s] and remoteRankId[%u] to list",
    5886              :                    __func__, newTag.c_str(), usrRankId);
    5887            0 :         if (rankTagRemoteRes_.find(usrRankId) == rankTagRemoteRes_.end() ||
    5888            0 :             rankTagRemoteRes_[usrRankId].find(newTag) == rankTagRemoteRes_[usrRankId].end()) {
    5889            0 :             HccltagRemoteResV2 *tagRemoteResHostPtr = nullptr;
    5890            0 :             HccltagRemoteResV2 *tagRemoteResDevicePtr = nullptr;
    5891            0 :             CHK_RET(CreateListNode(&tagRemoteResHostPtr, &tagRemoteResDevicePtr));
    5892            0 :             CHK_SAFETY_FUNC_RET(memcpy_s(tagRemoteResHostPtr->tag, sizeof(tagRemoteResHostPtr->tag),
    5893              :                                          newTag.c_str(), newTag.length() + 1));
    5894            0 :             tagRemoteResHostPtr->linkP2p.localIpcSignal[0].resId = INVALID_U64;
    5895            0 :             tagRemoteResHostPtr->linkP2pSio.localIpcSignal[0].resId = INVALID_U64;
    5896            0 :             tagRemoteResHostPtr->linkRoce[0].localNotifyList = 0;
    5897            0 :             tagRemoteResHostPtr->linkRoce[1].localNotifyList = 0;
    5898            0 :             tagRemoteResHostPtr->linkRoce[2].localNotifyList = 0;
    5899            0 :             tagRemoteResHostPtr->linkRoce[3].localNotifyList = 0;
    5900            0 :             ListCommonAddHead(&tagRemoteResDevicePtr->nextTagRes, &tagRemoteResHostPtr->nextTagRes,
    5901            0 :                               &rankRelationResHostPtr->nextTagRes, &rankRelationResDevicePtr->nextTagRes);
    5902            0 :             HccltagRemoteResV3 tempTagRemoteRes;
    5903            0 :             tempTagRemoteRes.tagRemoteResPtr = tagRemoteResHostPtr;
    5904            0 :             rankTagRemoteRes_[usrRankId][newTag] = tempTagRemoteRes;
    5905            0 :             HCCL_RUN_INFO("[%s] successfully add RemoteRes to list with newtag[%s], remoteRankId[%u]"
    5906              :                        "rankRelationResHostPtr head addr[%p], nextHost[%p], preHost[%p], nextDevice[%p], preDevice[%p], "
    5907              :                        "tagRemoteResDevicePtr head addr[%p]",
    5908              :                        __func__, newTag.c_str(), usrRankId,
    5909              :                        &rankRelationResHostPtr->nextTagRes, rankRelationResHostPtr->nextTagRes.nextHost,
    5910              :                        rankRelationResHostPtr->nextTagRes.preHost, rankRelationResHostPtr->nextTagRes.nextDevice,
    5911              :                        rankRelationResHostPtr->nextTagRes.preDevice, &tagRemoteResDevicePtr->nextTagRes);
    5912              :         } else {
    5913            0 :             HCCL_DEBUG("[%s] the RemoteRes with usr rankid[%u] tag[%s] has been added list",
    5914              :                        __func__, usrRankId, newTag.c_str());
    5915              :         }
    5916            0 :         return HCCL_SUCCESS;
    5917              :     }
    5918              : 
    5919            0 :     HcclResult HcclCommunicator::BuildRelationResByRemoteRankId(const TransportRequest &transportRequest, const LINK &link,
    5920              :                                                                 HcclRankRelationResV2 *&rankRelationResHostPtr, HcclRankRelationResV2 *&rankRelationResDevicePtr)
    5921              :     {
    5922            0 :         const u32 usrRankId = transportRequest.remoteUserRank;
    5923            0 :         HCCL_INFO("[%s]start to add RelationRes with remote usr rankid[%u] to list", __func__, usrRankId);
    5924            0 :         if (opResPara_.remoteRes[usrRankId].nextHostPtr != 0 && opResPara_.remoteRes[usrRankId].nextDevicePtr != 0) {
    5925            0 :             rankRelationResHostPtr =
    5926            0 :                 reinterpret_cast<HcclRankRelationResV2 *>(opResPara_.remoteRes[usrRankId].nextHostPtr);
    5927            0 :             rankRelationResDevicePtr =
    5928            0 :                 reinterpret_cast<HcclRankRelationResV2 *>(opResPara_.remoteRes[usrRankId].nextDevicePtr);
    5929            0 :             HCCL_DEBUG("[%s] RelationRes with remote usr rankid[%u] has been added to list, "
    5930              :                        "rankRelationResHostPtr[%p], rankRelationResDevicePtr[%p]",
    5931              :                        __func__, usrRankId, rankRelationResHostPtr, rankRelationResDevicePtr);
    5932              :         } else {
    5933            0 :             CHK_RET(CreateListNode(&rankRelationResHostPtr, &rankRelationResDevicePtr));
    5934            0 :             opResPara_.remoteRes[usrRankId].nextHostPtr = reinterpret_cast<u64>(rankRelationResHostPtr);
    5935            0 :             opResPara_.remoteRes[usrRankId].nextDevicePtr = reinterpret_cast<u64>(rankRelationResDevicePtr);
    5936            0 :             rankRelationResHostPtr->remoteUsrRankId = usrRankId;
    5937            0 :             rankRelationResHostPtr->remoteWorldRank = rankInfoList_[usrRankId].worldRank;
    5938            0 :             HCCL_DEBUG("[%s]successfully add RelationRes with remote usr rankid[%u] to list, rankRelationResHostPtr[%p],"
    5939              :                        "rankRelationResDevicePtr[%p]",
    5940              :                        __func__, usrRankId, rankRelationResHostPtr, rankRelationResDevicePtr);
    5941              :         }
    5942              :         // 刷新远端对应的cclbuffer
    5943            0 :         std::vector<void *> extraMemVector;
    5944            0 :         if (transportRequest.inputMemType == TransportMemType::CCL_INPUT && rankRelationResHostPtr->windowsIn == 0) {
    5945            0 :             void *inbufferPtr = nullptr;
    5946            0 :             CHK_RET(link->GetRemoteMem(UserMemType::INPUT_MEM, &inbufferPtr));
    5947            0 :             rankRelationResHostPtr->windowsIn = reinterpret_cast<u64>(inbufferPtr);
    5948              :         }
    5949            0 :         if (transportRequest.outputMemType == TransportMemType::CCL_OUTPUT && rankRelationResHostPtr->windowsOut == 0) {
    5950            0 :             void *outbufferPtr = nullptr;
    5951            0 :             CHK_RET(link->GetRemoteMem(UserMemType::OUTPUT_MEM, &outbufferPtr));
    5952            0 :             rankRelationResHostPtr->windowsOut = reinterpret_cast<u64>(outbufferPtr);
    5953              :         }
    5954            0 :         if (rankRelationResHostPtr->windowsExp == 0) {
    5955            0 :             std::vector<void *> memPtrVec = {};
    5956            0 :             CHK_RET(link->GetRemoteMem(&memPtrVec));
    5957            0 :             if (memPtrVec.size() != 0) {
    5958            0 :                 rankRelationResHostPtr->windowsExp = reinterpret_cast<u64>(memPtrVec[0]);
    5959            0 :                 if (link->GetTransportType() == TransportType::TRANS_TYPE_P2P) {
    5960            0 :                     p2pCclBuf_[usrRankId] = memPtrVec[0];
    5961              :                 } else {
    5962            0 :                     cclBuf_[usrRankId] = memPtrVec[0];
    5963              :                 }
    5964            0 :                 rankRelationResHostPtr->windowsExp += cclBufferManager_.GetInCCLbufferSize() + cclBufferManager_.GetOutCCLbufferSize();
    5965              :             }
    5966            0 :         }
    5967            0 :         HCCL_INFO("group[%s] successfully set windowsIn & windowsOut & windowsExp info: userRank[%u], groupRank[%u], "
    5968              :                   "remoteRank[%u], windowsIn[0x%llx], InSize[0x%llx], windowOut[0x%llx], OutSize[0x%llx], "
    5969              :                   "windowExp[0x%llx], ExpSize[0x%llx]",
    5970              :                   identifier_.c_str(), GetUserRank(), GetGroupRank(), transportRequest.remoteUserRank,
    5971              :                   rankRelationResHostPtr->windowsIn, cclBufferManager_.GetInCCLbufferSize(),
    5972              :                   rankRelationResHostPtr->windowsOut, cclBufferManager_.GetOutCCLbufferSize(),
    5973              :                   rankRelationResHostPtr->windowsExp, cclBufferManager_.GetExpBufferSize());
    5974            0 :         return HCCL_SUCCESS;
    5975            0 :     }
    5976              : 
    5977            0 :     HcclResult HcclCommunicator::ParseRemoteDataToMem(const OpCommTransport &opTransportResponse, const std::string &newTag,
    5978              :                                                       const HcclCMDType opType, bool isBackup, bool isRetry)
    5979              :     {
    5980            0 :         HCCL_INFO("[%s] entry process newtag[%s], isBackup[%d]", __func__, newTag.c_str(), isBackup);
    5981            0 :         std::set<u32> bsrTansportRank;
    5982            0 :         for (auto &levelNSubCommTransport : opTransportResponse) {
    5983            0 :             for (auto &singleSubCommTransport : levelNSubCommTransport) {
    5984            0 :                 u32 linkIdx = 0;
    5985            0 :                 for (auto &transportRequest : singleSubCommTransport.transportRequests) {
    5986            0 :                     if (transportRequest.isValid) {
    5987            0 :                         auto tempLink = singleSubCommTransport.links[linkIdx];
    5988            0 :                         HCCL_INFO("[%s]transportRequest.isUsedRdma[%d], isBackup[%d]", __func__,
    5989              :                                   transportRequest.isUsedRdma, isBackup);
    5990            0 :                         if ((!transportRequest.isUsedRdma || tempLink->GetLinkType() == LinkType::LINK_SIO) &&
    5991            0 :                             (isBackup || isRetry)) {
    5992            0 :                             HCCL_INFO("[%s]no need to add p2p backup Link resource, transportRequest.isUsedRdma[%d], "
    5993              :                                       "isBackup[%d]",
    5994              :                                       __func__, transportRequest.isUsedRdma, isBackup);
    5995            0 :                             linkIdx++;
    5996            0 :                             continue;
    5997              :                         }
    5998            0 :                         HcclRankRelationResV2 *rankRelationResHostPtr = nullptr;
    5999            0 :                         HcclRankRelationResV2 *rankRelationResDevicePtr = nullptr;
    6000            0 :                         CHK_RET(BuildRelationResByRemoteRankId(transportRequest, tempLink, rankRelationResHostPtr,
    6001              :                                                                rankRelationResDevicePtr));
    6002            0 :                         const u32 usrRankId = transportRequest.remoteUserRank;
    6003            0 :                         HCCL_INFO("[%s]successfully BuildRelationResByRemoteRankId with remote usr rankid[%u], "
    6004              :                                   "rankRelationResHostPtr[%p], rankRelationResDevicePtr[%p], newTage[%s]",
    6005              :                                   __func__, usrRankId, rankRelationResHostPtr, rankRelationResDevicePtr, newTag.c_str());
    6006            0 :                         CHK_RET(BuildRemoteResByTag(newTag, usrRankId, rankRelationResHostPtr,
    6007              :                                                     rankRelationResDevicePtr, isBackup, isRetry));
    6008              :                         // transport信息保存(notify、qp)
    6009            0 :                         if (!transportRequest.isUsedRdma || tempLink->GetLinkType() == LinkType::LINK_SIO) {
    6010              :                             // sdma -> P2P
    6011            0 :                             CHK_RET(BuildOpRemoteLinkP2pResParam(tempLink, rankTagRemoteRes_[usrRankId][newTag],
    6012              :                                                                  transportRequest.linkType));
    6013              :                         } else {
    6014              :                             // rdma -> roce
    6015            0 :                             bool isSecondBuild = false;
    6016            0 :                             if (opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV &&
    6017            0 :                                 bsrTansportRank.find(transportRequest.remoteUserRank) != bsrTansportRank.end()) {
    6018            0 :                                 isSecondBuild = true;
    6019              :                             }
    6020            0 :                             bsrTansportRank.insert(transportRequest.remoteUserRank);
    6021            0 :                             CHK_RET(BuildOpRemoteLinkRoceResParam(tempLink, rankTagRemoteRes_[usrRankId][newTag],
    6022              :                                                                   isBackup, isRetry, isSecondBuild));
    6023              :                         }
    6024            0 :                         HCCL_INFO("[%s] successfully add RemoteRes to list with newtag[%s] rankRelationResHostPtr "
    6025              :                                   "head addr[%p], nextHost[%p], preHost[%p], nextDevice[%p], preDevice[%p], "
    6026              :                                   "rankRelationResDevicePtr head addr[%p]",
    6027              :                                   __func__, newTag.c_str(),
    6028              :                                   &rankRelationResHostPtr->nextTagRes, rankRelationResHostPtr->nextTagRes.nextHost,
    6029              :                                   rankRelationResHostPtr->nextTagRes.preHost, rankRelationResHostPtr->nextTagRes.nextDevice,
    6030              :                                   rankRelationResHostPtr->nextTagRes.preDevice, &rankRelationResDevicePtr->nextTagRes);
    6031            0 :                         HCCL_INFO("[%s] create link success with newtag[%s], linkIdx[%u], isBackup[%d], usrRankId[%u]",
    6032              :                                   __func__, newTag.c_str(), linkIdx, isBackup, usrRankId);
    6033            0 :                     }
    6034            0 :                     linkIdx++;
    6035              :                 }
    6036              :             }
    6037              :         }
    6038            0 :         HCCL_DEBUG("[%s] process success newtag[%s]", __func__, newTag.c_str());
    6039            0 :         return HCCL_SUCCESS;
    6040            0 :     }
    6041              : 
    6042            0 :     HcclResult HcclCommunicator::BuildOpRemoteResParam(const AlgResourceResponse &algResource, const std::string &newTag,
    6043              :                                                        const HcclCMDType opType, bool isRetry)
    6044              :     {
    6045            0 :         HCCL_DEBUG("[%s]start ParseRemoteDataToMem, IsEnableBackupLink[%d]", __func__, IsEnableBackupLink());
    6046            0 :         CHK_RET(ParseRemoteDataToMem(algResource.opTransportResponse, newTag, opType, false, isRetry));
    6047            0 :         if (IsEnableBackupLink()) {
    6048            0 :             HCCL_DEBUG("[%s]start Parse backupRemoteDataToMem, IsEnableBackupLink[%d]", __func__, IsEnableBackupLink());
    6049            0 :             CHK_RET(ParseRemoteDataToMem(algResource.opTransportResponseBackUp, newTag, opType, true, isRetry));
    6050              :         }
    6051            0 :         if (deviceType_ == DevType::DEV_TYPE_910_93 || deviceType_ == DevType::DEV_TYPE_910B) {
    6052            0 :             opResPara_.notifysize = 4; // 910B & 910_93 每个notify占4个字节
    6053              :         } else {
    6054            0 :             opResPara_.notifysize = 8; // 其他芯片类型每个notify占8个字节
    6055              :         }
    6056            0 :         return HCCL_SUCCESS;
    6057              :     }
    6058              : 
    6059            5 :     HcclResult HcclCommunicator::CopyHostListResToDeviceParam(const std::string &newTag, const ListCommon *headHostList, const u64 size)
    6060              :     {
    6061            5 :         ListCommon *nextHostList = reinterpret_cast<ListCommon *>(headHostList->nextHost);
    6062            5 :         ListCommon *nextDeviceList = reinterpret_cast<ListCommon *>(headHostList->nextDevice);
    6063              :         // 该tag已分配过资源,只需刷新单节点(精确匹配);否则为首次分配,批量拷贝前N个节点
    6064            5 :         bool isRefreshSingleNode = (newTagResAlloced_.find(newTag) != newTagResAlloced_.end());
    6065              :         // ListCommonAddHead把新节点头插,原头节点的preHost/preDevice被改写,两节点都需要刷新到device
    6066            5 :         constexpr uint32_t UPDATE_NODE_NUM = 2;
    6067            5 :         uint32_t updateNodeCnt = 0;
    6068              : 
    6069           12 :         while (nextHostList != headHostList && updateNodeCnt < UPDATE_NODE_NUM) {
    6070            9 :             HCCL_INFO(
    6071              :                 "[HcclCommunicator][CopyHostListResToDeviceParam] remote resource, tag[%s], head Host List[%p], next "
    6072              :                 "Host List[%p],next Device List[%p]",
    6073              :                 newTag.c_str(), headHostList, nextHostList, nextDeviceList);
    6074            9 :             if (isRefreshSingleNode) {
    6075              :                 // 刷新已有资源:遍历链表找到匹配的tag,只拷贝该节点,避免全量刷新
    6076            7 :                 std::string curTag = (size == sizeof(HccltagLocalResV2)) ? reinterpret_cast<HccltagLocalResV2 *>(nextHostList)->tag :
    6077            7 :                     reinterpret_cast<HccltagRemoteResV2 *>(nextHostList)->tag;
    6078            7 :                 if (curTag == newTag) {
    6079            2 :                     CHK_RET(hrtMemSyncCopy(reinterpret_cast<void *>(nextDeviceList), size, reinterpret_cast<void *>(nextHostList),
    6080              :                         size, HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
    6081            2 :                     break;
    6082              :                 }
    6083            7 :             } else {
    6084              :                 // 首分配置:拷贝前UPDATE_NODE_NUM个节点到device,减少H2D次数
    6085            2 :                 CHK_RET(hrtMemSyncCopy(reinterpret_cast<void *>(nextDeviceList), size, reinterpret_cast<void *>(nextHostList),
    6086              :                                     size, HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
    6087            2 :                 updateNodeCnt++;
    6088              :             }
    6089            7 :             nextDeviceList = reinterpret_cast<ListCommon *>(nextHostList->nextDevice);
    6090            7 :             nextHostList = reinterpret_cast<ListCommon *>(nextHostList->nextHost);
    6091              :         }
    6092            5 :         return HCCL_SUCCESS;
    6093              :     }
    6094              : 
    6095            0 :     HcclResult HcclCommunicator::CopyHostOpResToDeviceParam(const std::string &newTag)
    6096              :     {
    6097              :         // 1、将opResPara_,H2D到device
    6098            0 :         CHK_RET(hrtMemSyncCopy(opResDevicePara_.ptr(), sizeof(HcclOpResParam), reinterpret_cast<void *>(&opResPara_),
    6099              :                                sizeof(HcclOpResParam), HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
    6100            0 :         HCCL_DEBUG("[HcclCommunicator][CopyHostOpResToDeviceParam] tag[%s] local rankId[%u] workspace[%p] "
    6101              :                    "workspacesize[%lu] ranksize[%u], cclbuffersize[%lu], cclinbuffer[%p], ccloutbuffer[%p], "
    6102              :                    "remote winStart[%u], remote rWinOffset[%u], hostStateInfo[%p], aicpuStateInfo[%p], notifysize[%u], "
    6103              :                    "sizeOfAiRMAInfo[%u],aiRMAInfo[%u]",
    6104              :                    newTag.c_str(), userRank_, opResPara_.mc2WorkSpace.workSpace, opResPara_.mc2WorkSpace.workSpaceSize,
    6105              :                    opResPara_.rankSize, opResPara_.winSize, opResPara_.localWindowsIn, opResPara_.localWindowsOut,
    6106              :                    opResPara_.rWinStart, opResPara_.rWinOffset, opResPara_.hostStateInfo, opResPara_.aicpuStateInfo,
    6107              :                    opResPara_.notifysize, opResPara_.sizeOfAiRMAInfo,opResPara_.aiRMAInfo);
    6108              :         // 2、将opResPara_中localres的tagRes,H2D到device
    6109            0 :         HCCL_DEBUG("[HcclCommunicator][CopyHostOpResToDeviceParam] local resource, tag[%s] streamNum[%u] signalNum[%u]",
    6110              :                    newTag.c_str(), opResPara_.localRes.streamNum, opResPara_.localRes.signalNum);
    6111            0 :         CHK_RET(CopyHostListResToDeviceParam(
    6112              :             newTag, reinterpret_cast<ListCommon *>(&opResPara_.localRes.nextTagRes), sizeof(HccltagLocalResV2)));
    6113              :         // 3、遍历rank中tag资源,H2D到device
    6114            0 :         CHK_RET(CopyHostOpRemoteResToDeviceParam(newTag));
    6115            0 :         HCCL_DEBUG("[HcclCommunicator][CopyHostOpResToDeviceParam] copy host resource success!, tag[%s]", newTag.c_str());
    6116            0 :         return HCCL_SUCCESS;
    6117              :     }
    6118              : 
    6119            0 :     HcclResult HcclCommunicator::CopyHostAirmaInfoToDeviceParam(const std::string &newTag, const HcclCMDType opType, const rtStream_t aiCpuStream)
    6120              :     {
    6121            0 :         HCCL_INFO("[HcclCommunicator][%s] Start prepare.", __func__);
    6122            0 :         CHK_PTR_NULL(aiRMAInfoMem_);
    6123            0 :         HcclAiRMAInfo *aiRMAInfoPtr = reinterpret_cast<HcclAiRMAInfo*>(aiRMAInfoMem_->ptr());
    6124            0 :         CHK_PTR_NULL(aiRMAInfoPtr);
    6125              :  
    6126            0 :         aiRMAInfoPtr->sizeOfAiRMAWQ = static_cast<u32>(sizeof(HcclAiRMAWQ));
    6127            0 :         aiRMAInfoPtr->sizeOfAiRMACQ = static_cast<u32>(sizeof(HcclAiRMACQ));
    6128            0 :         aiRMAInfoPtr->sizeOfAiRMAMem = static_cast<u32>(sizeof(HcclAiRMAMemInfo));
    6129              :  
    6130            0 :         CHK_RET(DeviceMem::alloc(aiSqDev_, aiSqMem_->size()));
    6131            0 :         aiRMAInfoPtr->sqPtr = aiSqDev_.ptr();
    6132            0 :         CHK_RET(hrtMemAsyncCopy(aiSqDev_.ptr(), aiSqDev_.size(), aiSqMem_->ptr(), aiSqDev_.size(),
    6133              :                                      HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
    6134              :  
    6135            0 :         CHK_RET(DeviceMem::alloc(aiScqDev_, aiScqMem_->size()));
    6136            0 :         aiRMAInfoPtr->scqPtr = aiScqDev_.ptr();
    6137            0 :         CHK_RET(hrtMemAsyncCopy(aiScqDev_.ptr(), aiScqDev_.size(), aiScqMem_->ptr(), aiScqDev_.size(),
    6138              :                                      HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
    6139              :  
    6140            0 :         CHK_RET(DeviceMem::alloc(aiRqDev_, aiRqMem_->size()));
    6141            0 :         aiRMAInfoPtr->rqPtr = aiRqDev_.ptr();
    6142            0 :         CHK_RET(hrtMemAsyncCopy(aiRqDev_.ptr(), aiRqDev_.size(), aiRqMem_->ptr(), aiRqDev_.size(),
    6143              :                                      HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
    6144              :  
    6145            0 :         CHK_RET(DeviceMem::alloc(aiRcqDev_, aiRcqMem_->size()));
    6146            0 :         aiRMAInfoPtr->rcqPtr = aiRcqDev_.ptr();
    6147            0 :         CHK_RET(hrtMemAsyncCopy(aiRcqDev_.ptr(), aiRcqDev_.size(), aiRcqMem_->ptr(), aiRcqDev_.size(),
    6148              :                                      HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
    6149              :  
    6150            0 :         CHK_RET(hrtMemAsyncCopy(aiMemDetailsDev_.ptr(), aiMemDetailsDev_.size(), aiMemDetailsMem_->ptr(),
    6151              :                                      aiMemDetailsDev_.size(), HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
    6152              :  
    6153            0 :         CHK_RET(DeviceMem::alloc(aiMemDev_, aiMemMem_->size()));
    6154            0 :         aiRMAInfoPtr->memPtr = aiMemDev_.ptr();
    6155            0 :         CHK_RET(hrtMemAsyncCopy(aiMemDev_.ptr(), aiMemDev_.size(), aiMemMem_->ptr(), aiMemDev_.size(),
    6156              :                                      HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
    6157              :  
    6158            0 :         opResPara_.sizeOfAiRMAInfo = static_cast<u64>(sizeof(HcclAiRMAInfo));
    6159            0 :         CHK_RET(DeviceMem::alloc(aiRMAInfoDev_, opResPara_.sizeOfAiRMAInfo));
    6160            0 :         opResPara_.aiRMAInfo = reinterpret_cast<u64>(aiRMAInfoDev_.ptr());
    6161              :  
    6162            0 :         CHK_RET(hrtMemAsyncCopy(aiRMAInfoDev_.ptr(), aiRMAInfoDev_.size(), aiRMAInfoMem_->ptr(), aiRMAInfoDev_.size(),
    6163              :                                      HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
    6164            0 :         HCCL_INFO("[%s] tag[%s] curRankId[%u] rankNum[%u] qpNum[%u] aiRMAInfo[%p] sizeOfAiRMAInfo[%llu] "
    6165              :                   "sizeOfAiRMAWQ[%u] sizeOfAiRMACQ[%u] sizeOfAiRMAMem[%u] sqPtr[%p] sqSize[%llu] sqCount[%zu] "
    6166              :                   "scqPtr[%p] scqSize[%llu] scqCount[%zu] rqPtr[%p] rqSize[%llu] rqCount[%zu] rcqPtr[%p] "
    6167              :                   "rcqSize[%llu] rcqCount[%zu] memPtr[%p] memSize[%llu] memCount[%zu] memDetailCount[%zu],opResPara_.aiRMAInfo",
    6168              :                   __func__, newTag.c_str(), aiRMAInfoPtr->curRankId, aiRMAInfoPtr->rankNum, aiRMAInfoPtr->qpNum,
    6169              :                   opResPara_.aiRMAInfo, opResPara_.sizeOfAiRMAInfo, aiRMAInfoPtr->sizeOfAiRMAWQ,
    6170              :                   aiRMAInfoPtr->sizeOfAiRMACQ, aiRMAInfoPtr->sizeOfAiRMAMem, aiRMAInfoPtr->sqPtr,
    6171              :                   aiSqDev_.size(), aiSqMem_->size(), aiRMAInfoPtr->scqPtr, aiScqDev_.size(), aiScqMem_->size(),
    6172              :                   aiRMAInfoPtr->rqPtr, aiRqDev_.size(), aiRqMem_->size(), aiRMAInfoPtr->rcqPtr, aiRcqDev_.size(),
    6173              :                   aiRcqMem_->size(), aiRMAInfoPtr->memPtr, aiMemDev_.size(), aiMemMem_->size(), aiMemDetailsMem_->size());
    6174            0 :         return HCCL_SUCCESS;
    6175              :     }
    6176              :  
    6177            0 :     HcclResult HcclCommunicator::BuildOpResParam(
    6178              :         const std::string &algName, const AlgResourceResponse &algResource, const std::string &newTag,
    6179              :         const HcclCMDType opType, const rtStream_t aicpuStream)
    6180              :     {
    6181            0 :         opResPara_.localUsrRankId = userRank_;
    6182            0 :         opResPara_.rankSize = userRankSize_;
    6183              : 
    6184            0 :         bool isUseUserMem = isUserMemRegisted_ && !userMemMap_.empty();
    6185            0 :         if (!isUseUserMem) {
    6186            0 :             opResPara_.winSize = algResource.cclInputMem.size();
    6187            0 :             opResPara_.localWindowsIn = reinterpret_cast<u64>(algResource.cclInputMem.ptr());
    6188            0 :             opResPara_.localWindowsOut = reinterpret_cast<u64>(algResource.cclOutputMem.ptr());
    6189              :         } else {
    6190            0 :             opResPara_.winSize = userMemMap_.begin()->second->size();
    6191            0 :             opResPara_.localWindowsIn = reinterpret_cast<u64>(userMemMap_.begin()->second->ptr());
    6192            0 :             opResPara_.localWindowsOut = reinterpret_cast<u64>(userMemMap_.begin()->second->ptr());
    6193              :         }
    6194              :         // 填充Exp相关信息 当前该块内存大小恒为1M
    6195            0 :         opResPara_.winExpSize = EXP_BUFFER_SIZE;
    6196            0 :         opResPara_.localWindowsExp = reinterpret_cast<u64>(cclBufferManager_.GetCommExpBuffer().ptr());
    6197            0 :         HCCL_INFO("[HcclCommunicator][%s] isUseUserMem[%d], winSize[%llu], localWindowsIn[%llu],"
    6198              :                   "localWindowsOut[%llu], localWindowsExp[%llu]", __func__, isUseUserMem, opResPara_.winSize,
    6199              :                   opResPara_.localWindowsIn, opResPara_.localWindowsOut, opResPara_.localWindowsExp);
    6200              : 
    6201            0 :         CHK_SAFETY_FUNC_RET(
    6202              :             memcpy_s(opResPara_.hcomId, sizeof(opResPara_.hcomId), identifier_.c_str(), identifier_.length() + 1));
    6203              : 
    6204            0 :         opResPara_.config.deterministic = GetDeterministicConfig();
    6205            0 :         opResPara_.config.highPerfEnable = 0;
    6206            0 :         aclrtFloatOverflowMode floatOverflowMode = ACL_RT_OVERFLOW_MODE_UNDEF;
    6207            0 :         CHK_RET(hrtGetDeviceSatMode(&floatOverflowMode));
    6208            0 :         opResPara_.config.floatOverflowMode = floatOverflowMode;
    6209            0 :         opResPara_.config.taskMonitorInterval = GetExternalInputDfsTaskMonitorInterval();
    6210            0 :         bool isSupportAtomicWrite = false; // 涉及到任务编排,当前不能只判断本机驱动版本是否支持
    6211            0 :         opResPara_.config.isSupportAtomicWrite = static_cast<u8>(isSupportAtomicWrite);
    6212            0 :         opResPara_.config.notifyWaitTime =
    6213            0 :             (GetExternalInputHcclExecTimeoutSet() != HcclExecTimeoutSet::HCCL_EXEC_TIMEOUT_NOT_SET ||
    6214            0 :             commConfig_.GetConfigExecTimeOutSet())
    6215            0 :                 ? commConfig_.GetConfigExecTimeOut()
    6216              :                 : NOTIFY_DEFAULT_WAIT_TIME;
    6217            0 :         opResPara_.config.linkTimeOut = std::chrono::seconds(GetExternalInputHcclLinkTimeOut());
    6218            0 :         opResPara_.config.retryEnable = static_cast<u8>(retryEnable_);
    6219            0 :         opResPara_.config.interHccsDisable = GetExternalInputInterHccsDisable();
    6220            0 :         opResPara_.config.multiQpThreshold = GetExternalInputMultiQpThreshold();
    6221            0 :         opResPara_.rWinStart = offsetof(HcclOpResParam, remoteRes);
    6222            0 :         opResPara_.rWinOffset = sizeof(RemoteResPtr);
    6223            0 :         opResPara_.notifysize = 0;
    6224            0 :         opResPara_.lockAddr = hostDeviceLock_->GetDevMemAddr();
    6225            0 :         opResPara_.utraceStatusFlag = GetExternalInputHcclEnableEntryLog();
    6226            0 :         DeviceMem tinySendRecvMem;
    6227            0 :         CHK_RET(implAlg_->GetTinyMem(tinySendRecvMem));
    6228            0 :         opResPara_.tinyMem = reinterpret_cast<u64>(tinySendRecvMem.ptr());
    6229            0 :         opResPara_.tinyMemSize = reinterpret_cast<u64>(tinySendRecvMem.size());
    6230            0 :         opResPara_.opEntry = GetExternalInputHcclEnableEntryLog();
    6231            0 :         opResPara_.hcclSdmaQos = GetHcclQos();
    6232              : 
    6233            0 :         CHK_RET(BuildOpLocalResParam(algResource, newTag));
    6234            0 :         CHK_RET(BuildOpRemoteResParam(algResource, newTag, opType));
    6235            0 :         CHK_RET(BuildOpTopoResParam(algName, algResource));
    6236            0 :         CHK_RET(BuildOpRetryParam(algResource, newTag));
    6237            0 :         CHK_RET(BuildZeroCopyParam());
    6238            0 :         CHK_RET(BuildAicpuCustomParam());
    6239            0 :         CHK_RET(BuildAicpuOrderLaunchNotify()); // 先申请device侧的关于按序下发的Notify内存
    6240            0 :         if (algName == "RunAlltoAllAivDirect") {
    6241              :             // AIV直驱ROCE
    6242            0 :             CHK_RET(BuildAiRmaInfoParam(newTag, algName, opType));
    6243            0 :             CHK_RET(CopyHostAirmaInfoToDeviceParam(newTag, opType, aicpuStream));
    6244              :         }
    6245            0 :         CHK_RET(CopyHostOpResToDeviceParam(newTag));
    6246            0 :         HCCL_RUN_INFO("[%s]build aicpu unfold resource success, tag[%s] rWinStart[%u] rWinOffset[%u] opEntry[%d]",
    6247              :                       __func__, newTag.c_str(), opResPara_.rWinStart, opResPara_.rWinOffset, opResPara_.opEntry);
    6248            0 :         return HCCL_SUCCESS;
    6249            0 :     }
    6250              : 
    6251            0 :     HcclResult HcclCommunicator::BuildCustomOpResParam()
    6252              :     {
    6253              :         // custom进程需要刷新h2d/d2h内存
    6254            0 :         opResPara_.kfcControlTransferH2DParams = customControlTransferH2D_->GetCommunicateParams();
    6255            0 :         opResPara_.kfcStatusTransferD2HParams = customStatusTransferD2H_->GetCommunicateParams();
    6256            0 :         CHK_RET(hrtMemSyncCopy(opResDevicePara_.ptr(), sizeof(HcclOpResParam), reinterpret_cast<void *>(&opResPara_),
    6257              :                                sizeof(HcclOpResParam), HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
    6258            0 :         return HCCL_SUCCESS;
    6259              :     }
    6260              : 
    6261           24 :     HcclResult HcclCommunicator::RegisterDfxInfo(const OpParam &param, AlgType algType,
    6262              :                                                  const std::vector<Stream> &slaveStreams, bool isAiv, const std::string &tag)
    6263              :     {
    6264           24 :         u64 count = 0;
    6265           24 :         HcclDataType dataType = HcclDataType::HCCL_DATA_TYPE_RESERVED;
    6266           24 :         switch (param.opType) {
    6267            0 :         case HcclCMDType::HCCL_CMD_SEND:
    6268              :         case HcclCMDType::HCCL_CMD_RECEIVE:
    6269              :         case HcclCMDType::HCCL_CMD_BATCH_SEND_RECV:
    6270            0 :             count = param.GetDataCount(userRank_);
    6271            0 :             dataType = param.GetDataType();
    6272            0 :             HCCL_PROFILER_ADD_TAG_SENDRECV(param.tag, identifier_, GetWorkflowMode());
    6273            0 :             HCCL_PROFILER_ADD_GROUPRANK_SENDRECV(identifier_, userRankSize_, userRank_, param.dstRank);
    6274            0 :             break;
    6275            5 :         case HcclCMDType::HCCL_CMD_ALLTOALL:
    6276              :         case HcclCMDType::HCCL_CMD_ALLTOALLV:
    6277              :         case HcclCMDType::HCCL_CMD_ALLTOALLVC:
    6278            5 :             CHK_RET(AddGroupTagInfo(param.tag, isAiv));
    6279            5 :             count = param.All2AllDataDes.sendCount;
    6280            5 :             dataType = param.All2AllDataDes.sendType;
    6281            5 :             break;
    6282           19 :         default:
    6283           19 :             CHK_RET(AddGroupTagInfo(param.tag, isAiv));
    6284           19 :             count = param.GetDataCount(userRank_);
    6285           19 :             dataType = param.GetDataType();
    6286              :         }
    6287              : 
    6288           24 :         if(GetExternalInconsistentCheckSwitch() == InconsistentCheckMode::ON){
    6289            0 :             if (param.opType != HcclCMDType::HCCL_CMD_BATCH_SEND_RECV) {
    6290            0 :                 OpInfoDesc opInfo;
    6291            0 :                 opInfo.opType = param.opType;
    6292            0 :                 opInfo.dataType = dataType;
    6293            0 :                 opInfo.reduceOp = param.reduceType;
    6294            0 :                 opInfo.count = count;
    6295            0 :                 opInfo.root = param.root;
    6296            0 :                 opInfo.isValid = true;
    6297            0 :                 AddOpInfoToHeartBeat(opInfo, tag);
    6298              :             }
    6299              :         }
    6300              : 
    6301              :         // task exception使用: 算子计数,算子入参信息(src/dst/datatype/reducetype)
    6302           24 :         HCCL_PROFILER_ADD_OPDATA_OP(param.tag, count, param.inputPtr, param.outputPtr, dataType, param.root, identifier_,
    6303              :                                     param.reduceType);
    6304              :         // 记录主流相关信息, 给profiling和task exception使用
    6305           24 :         HCCL_PROFILER_ADD_STREAM_BY_STREAMID(param.stream.id(), param.tag, 0, algType);
    6306           24 :         if (((GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) &&
    6307           14 :              !hccl::ProfilingManagerPub::GetAdditionInfoState() &&
    6308           52 :              !hccl::ProfilingManagerPub::GetTaskApiState()) &&
    6309           14 :              !param.isCapture) {
    6310           11 :             return HCCL_SUCCESS;
    6311              :         }
    6312              :         // 从流信息profiling开关打开的话再注册
    6313           13 :         for (u32 streamIndex = 0; streamIndex < slaveStreams.size(); streamIndex++) {
    6314            0 :             HCCL_PROFILER_ADD_STREAM_BY_STREAMID(slaveStreams[streamIndex].id(), param.tag, streamIndex + 1, algType);
    6315              :         }
    6316           13 :         return HCCL_SUCCESS;
    6317              :     }
    6318              : 
    6319            0 :     HcclResult HcclCommunicator::GetReportHcclMC2Info(const Stream &kfcStream, const std::vector<Stream> &aicpuStreams)
    6320              :     {
    6321            0 :         hcclMc2Info_.groupName = hrtMsprofGetHashId(identifier_.c_str(), identifier_.length());
    6322            0 :         hcclMc2Info_.rankSize = userRankSize_;
    6323            0 :         hcclMc2Info_.rankId = userRank_;
    6324            0 :         hcclMc2Info_.usrRankId = realUserRank_;
    6325            0 :         hcclMc2Info_.aicpuKfcStreamId = static_cast<uint32_t>(kfcStream.id());
    6326            0 :         hcclMc2Info_.reserve = 0;
    6327            0 :         const uint32_t ONCE_REPORT_STREAM_NUM_MAX = 8;
    6328            0 :         for (uint32_t streamIndex = 0, reportId = 0; streamIndex < aicpuStreams.size(); streamIndex++) {
    6329            0 :             HCCL_INFO("streamIndex:%u, reportId:%u, streamId:%d, ", streamIndex, reportId, aicpuStreams[streamIndex].id());
    6330            0 :             hcclMc2Info_.commStreamIds[reportId++] = aicpuStreams[streamIndex].id();
    6331            0 :             if (reportId == ONCE_REPORT_STREAM_NUM_MAX) {
    6332            0 :                 hcclMc2Info_.commStreamSize = reportId;
    6333            0 :                 CHK_RET(ProfilingManagerPub::CallMsprofReportMc2CommInfo(hrtMsprofSysCycleTime(), &hcclMc2Info_,
    6334              :                                                                          sizeof(hcclMc2Info_)));
    6335            0 :                 reportId = 0;
    6336              :             }
    6337            0 :             if (streamIndex == (aicpuStreams.size() - 1)) {
    6338            0 :                 HCCL_INFO("streamIndex:%u, reportId:%u, streamId:%d", streamIndex, reportId, opMainStream_.id());
    6339            0 :                 hcclMc2Info_.commStreamIds[reportId++] = opMainStream_.id();
    6340            0 :                 hcclMc2Info_.commStreamSize = reportId;
    6341            0 :                 CHK_RET(ProfilingManagerPub::CallMsprofReportMc2CommInfo(hrtMsprofSysCycleTime(), &hcclMc2Info_,
    6342              :                                                                          sizeof(hcclMc2Info_)));
    6343            0 :                 reportId = 0;
    6344              :             }
    6345              :         }
    6346            0 :         if (aicpuStreams.empty()) {
    6347            0 :             HCCL_INFO("only exist main stream, streamId:%d", opMainStream_.id());
    6348            0 :             hcclMc2Info_.commStreamIds[0] = opMainStream_.id();
    6349            0 :             hcclMc2Info_.commStreamSize = 1; // 只有主流1条
    6350            0 :             CHK_RET(ProfilingManagerPub::CallMsprofReportMc2CommInfo(hrtMsprofSysCycleTime(), &hcclMc2Info_,
    6351              :                                                                      sizeof(hcclMc2Info_)));
    6352              :         }
    6353            0 :         return HCCL_SUCCESS;
    6354              :     }
    6355              : 
    6356            0 :     HcclResult HcclCommunicator::OrchestrateAicpu(const HcclCMDType &opType, const std::string &algName,
    6357              :                                                   const OpParam &param, const AlgResourceResponse &algResource, const std::string &newTag, AlgType algType,
    6358              :                                                   bool isCustom, bool needIncreLink, bool needRecreateAlltoallComm)
    6359              :     {
    6360            0 :         uint64_t streamMode = 0;
    6361            0 :         CHK_RET(hrtStreamGetMode(param.stream.ptr(), &streamMode));
    6362              :         rtStream_t aicpuStream;
    6363            0 :         Mc2AiCpuStreamAllocAndGet(streamMode, aicpuStream); // aicpuStream需要在首次下发时申请
    6364            0 :         if (!isContextLaunched_) {
    6365              :             // 1、通信域内首次下发,从algResource中获取资源,H2D刷新资源,launch init
    6366              :             rtStream_t aicpuInitStream;
    6367            0 :             Mc2AiCpuInitStreamAllocAndGet(streamMode, aicpuInitStream); // 使用aicpuInitStream_下初始化kernel
    6368            0 :             Stream tmpStream(aicpuInitStream);
    6369            0 :             HCCL_DEBUG("%s ContextLaunched, aicpuInitStream:%p, aicpuStream:%p", __func__, aicpuInitStream, aicpuStream);
    6370            0 :             CHK_RET(AicpuResourceInit(algName, algResource, newTag, aicpuInitStream, opType, isCustom));
    6371            0 :             CHK_RET(GetReportHcclMC2Info(tmpStream, algResource.slaveDevStreams));
    6372            0 :             CHK_RET(SetAicpuUnfoldFlag());
    6373            0 :         } else if (newTagResAlloced_.find(newTag) == newTagResAlloced_.end() ||
    6374            0 :                 (opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV && needIncreLink) || needRecreateAlltoallComm) {
    6375              :             // 2、通信域内非首次,但是有新的newTag,查看是否需要补充资源。
    6376            0 :             PetersonLockGuard guard(hostDeviceLock_.get());
    6377            0 :             CHK_PRT_RET(guard.IsLockFailed(),
    6378              :                         HCCL_ERROR("[HcclCommunicator][OrchestrateAicp] hostDeviceLock lock failed"), HCCL_E_INTERNAL);
    6379            0 :             CHK_RET(AicpuResourceRefresh(algResource, newTag, opType));
    6380            0 :         }
    6381            0 :         HCCL_DEBUG("%s isContextLaunched[%u], needRecreateAlltoallComm[%u]", __func__, isContextLaunched_, needRecreateAlltoallComm);
    6382            0 :         bool isUsedMainStream = (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB);
    6383              :         // inplace支持重执行的stream资源处理逻辑
    6384            0 :         bool isHcclOpInplace = IsHcclOpInplace(opType, param, userRank_, userRankSize_, isInplaceStatus_);
    6385            0 :         if ((retryOrigWorkflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB) &&
    6386            0 :             retryEnable_ && isHcclOpInplace &&
    6387            0 :             (opType == HcclCMDType::HCCL_CMD_ALLREDUCE || opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER)) {
    6388            0 :             isUsedMainStream = true;
    6389              :         }
    6390            0 :         AicpuOpTiling opTilingInfo;
    6391            0 :         opTilingInfo.algName = algName;
    6392            0 :         opTilingInfo.newTag = newTag;
    6393            0 :         opTilingInfo.algType = algType;
    6394            0 :         opTilingInfo.isUsedMainStream = isUsedMainStream;
    6395            0 :         opTilingInfo.dumpDebug = GetExternalInputHcclDumpDebug();
    6396            0 :         aclrtFloatOverflowMode floatOverflowMode = ACL_RT_OVERFLOW_MODE_UNDEF;
    6397            0 :         CHK_RET(hrtGetDeviceSatMode(&floatOverflowMode));
    6398            0 :         opTilingInfo.floatOverflowMode = floatOverflowMode;
    6399            0 :         HcclResult ret = HCCL_SUCCESS;
    6400              :         // 根据算子类型,获取 Aicpu Kernel 名称
    6401            0 :         auto iter = HCOM_CMD_TYPE_STR_MAP.find(opType);
    6402            0 :         CHK_PRT_RET((iter == HCOM_CMD_TYPE_STR_MAP.end()),
    6403              :             HCCL_ERROR("[%s] RunAicpuRpcSrvLaunchV2 kernel not found, opType=[%d]", __func__, static_cast<int>(opType)),
    6404              :             HCCL_E_INTERNAL);
    6405            0 :         std::string kernelName = std::string("RunAicpuRpcSrvLaunchV2") + "_" + iter->second;
    6406            0 :         ret = AicpuKfcTilingDataLaunchExt(param, opType, opResDevicePara_, kernelName, opTilingInfo, isCustom);
    6407            0 :         if (ret != HCCL_SUCCESS) {
    6408            0 :             HCCL_ERROR("[HcclCommunicator][OrchestrateAicpu]aicpu unfold launch kernel[%s] failed. ret[%d] inputPtr[%p]"
    6409              :                        "outputPtr[%p] count[%llu] dataType[%s] op[%s]",
    6410              :                        kernelName.c_str(), ret, param.inputPtr, param.outputPtr,
    6411              :                        param.DataDes.count, GetDataTypeEnumStr(param.DataDes.dataType).c_str(),
    6412              :                        GetReduceOpEnumStr(param.reduceType).c_str());
    6413            0 :             return ret;
    6414              :         }
    6415            0 :         return HCCL_SUCCESS;
    6416            0 :     }
    6417              : 
    6418            5 :     HcclResult HcclCommunicator::CalcTinySendRecvMem(const OpParam &opParam, AlgResourceResponse &algResResponse,
    6419              :                                                      DeviceMem &tinySendRecvMem)
    6420              :     {
    6421            5 :         u64 sendCount = 0;
    6422            5 :         u64 recvCount = 0;
    6423            5 :         if (opParam.opType == HcclCMDType::HCCL_CMD_ALLTOALLV) {
    6424           12 :             for (u32 i = 0; i < userRankSize_; i++) {
    6425            8 :                 u64 curSendCount = *(static_cast<const u64 *>(opParam.All2AllDataDes.sendCounts) + i) +
    6426            8 :                                    *(static_cast<const u64 *>(opParam.All2AllDataDes.sdispls) + i);
    6427            8 :                 sendCount = std::max(sendCount, curSendCount);
    6428            8 :                 u64 curRecvCount = *(static_cast<const u64 *>(opParam.All2AllDataDes.recvCounts) + i) +
    6429            8 :                                    *(static_cast<const u64 *>(opParam.All2AllDataDes.rdispls) + i);
    6430            8 :                 recvCount = std::max(recvCount, curRecvCount);
    6431              :             }
    6432              :         } else {
    6433            3 :             for (u32 i = 0; i < userRankSize_; i++) {
    6434            2 :                 sendCount += *(static_cast<const u64 *>(opParam.All2AllDataDes.sendCountMatrix) +
    6435            2 :                                userRank_ * userRankSize_ + i);
    6436            2 :                 recvCount += *(static_cast<const u64 *>(opParam.All2AllDataDes.sendCountMatrix) +
    6437            2 :                                userRank_ + userRankSize_ * i);
    6438              :             }
    6439              :         }
    6440              : 
    6441            5 :         u32 sendTypeSize = 0, recvTypeSize = 0;
    6442            5 :         CHK_RET(SalGetDataTypeSize(opParam.All2AllDataDes.sendType, sendTypeSize));
    6443            5 :         CHK_RET(SalGetDataTypeSize(opParam.All2AllDataDes.recvType, recvTypeSize));
    6444              : 
    6445              :         // 在sendCount/recvCount全0时, 使用tinySendRecvMem, 避免使用空deviceMem
    6446            5 :         algResResponse.paramInputMem = sendCount == 0 ? DeviceMem::create(tinySendRecvMem.ptr(), tinySendRecvMem.size()) : DeviceMem::create(opParam.inputPtr, sendCount * sendTypeSize);
    6447            5 :         algResResponse.paramOutputMem = recvCount == 0 ? DeviceMem::create(tinySendRecvMem.ptr(), tinySendRecvMem.size()) : DeviceMem::create(opParam.outputPtr, recvCount * recvTypeSize);
    6448              : 
    6449            5 :         HCCL_INFO("[HcclCommunicator][CalcTinySendRecvMem] senMem addr[%p], sendSize[%llu], "
    6450              :                   "RecvMem addr[%p], RecvSize[%llu],",
    6451              :                   algResResponse.paramInputMem.ptr(),
    6452              :                   algResResponse.paramInputMem.size(), algResResponse.paramOutputMem.ptr(),
    6453              :                   algResResponse.paramOutputMem.size());
    6454            5 :         return HCCL_SUCCESS;
    6455              :     }
    6456              : 
    6457            3 :     bool HcclCommunicator::HasRoceTransportLinks(OpCommTransport &opTransportReq)
    6458              :     {
    6459           20 :         for (u32 levelIndex = 0; levelIndex < opTransportReq.size(); levelIndex++) {
    6460           20 :             for (u32 ringIndex = 0; ringIndex < opTransportReq[levelIndex].size(); ringIndex++) {
    6461            3 :                 SingleSubCommTransport &reqSingleSubComm = opTransportReq[levelIndex][ringIndex];
    6462            5 :                 for (u32 rankIndex = 0; rankIndex < reqSingleSubComm.transportRequests.size(); rankIndex++) {
    6463            3 :                     TransportRequest &transportRequest = reqSingleSubComm.transportRequests[rankIndex];
    6464            3 :                     if (transportRequest.isUsedRdma) {
    6465            1 :                         return true;
    6466              :                     }
    6467              :                 }
    6468              :             }
    6469              :         }
    6470            2 :         return false;
    6471              :     }
    6472              : 
    6473            0 :     HcclResult HcclCommunicator::CleanTransportLinks(OpCommTransport &opTransportReq, OpCommTransport &opTransportResponse)
    6474              :     {
    6475            0 :         for (u32 levelIndex = 0; levelIndex < opTransportReq.size(); levelIndex++) {
    6476            0 :             for (u32 ringIndex = 0; ringIndex < opTransportReq[levelIndex].size(); ringIndex++) {
    6477            0 :                 SingleSubCommTransport &reqSingleSubComm = opTransportReq[levelIndex][ringIndex];
    6478            0 :                 SingleSubCommTransport &respSingleSubComm = opTransportResponse[levelIndex][ringIndex];
    6479            0 :                 for (u32 rankIndex = 0; rankIndex < reqSingleSubComm.transportRequests.size();  rankIndex++) {
    6480            0 :                     TransportRequest &transportRequest = reqSingleSubComm.transportRequests[rankIndex];
    6481            0 :                     CHK_PRT_RET(rankIndex >= respSingleSubComm.links.size(),
    6482              :                         HCCL_ERROR("[CleanTransportLinks] The remote rank_id[%u] is larger than the existent respSingleSubComm map "\
    6483              :                         "size[%u]", rankIndex, respSingleSubComm.links.size()), HCCL_E_PARA);
    6484            0 :                     if (respSingleSubComm.links[rankIndex] != nullptr &&
    6485            0 :                         respSingleSubComm.links[rankIndex]->GetLinkType() != hccl::LinkType::LINK_RESERVED && !transportRequest.isUsedRdma) {
    6486            0 :                         HCCL_INFO("[CleanTransportLinks] The link to remote userRank[%u] has existed", transportRequest.remoteUserRank);
    6487            0 :                         continue;
    6488              :                     }
    6489            0 :                     respSingleSubComm.links[rankIndex] = nullptr;
    6490              :                 }
    6491              :             }
    6492              :         }
    6493            0 :         return HCCL_SUCCESS;
    6494              :     }
    6495              : 
    6496          116 :     HcclResult HcclCommunicator::AllocAlgNotifys(const std::string &tag, const NotifyLoadType notifyLoadType, const u32 notifyNum,
    6497              :                                                  std::vector<std::shared_ptr<LocalNotify>> &notifiesMain, std::vector<std::shared_ptr<LocalNotify>> &notifiesAux)
    6498              :     {
    6499          116 :         std::vector<std::shared_ptr<LocalNotify>> notifys(notifyNum, nullptr);
    6500          114 :         CHK_RET(queueNotifyManagerRefac_->Alloc(tag, notifyNum, notifys, notifyLoadType));
    6501              : 
    6502          116 :         u32 signalNum = notifyNum >> 1;
    6503          116 :         notifiesMain.resize(signalNum);
    6504          114 :         notifiesAux.resize(signalNum);
    6505          372 :         for (u32 i = 0; i < signalNum; i++) {
    6506          256 :             notifiesMain[i] = notifys[i << 1];
    6507          257 :             notifiesAux[i] = notifys[(i << 1) + 1];
    6508              :         }
    6509          116 :         return HCCL_SUCCESS;
    6510          116 :     }
    6511              : 
    6512          111 :     HcclResult HcclCommunicator::AllocAlgResource(const std::string &newTag, HcclCMDType opType, const OpParam &opParam,
    6513              :         AlgResourceRequest &resRequest, AlgResourceResponse &algResResponse, bool selectAivAlg)
    6514              :     {
    6515          111 :         HcclResult ret = HCCL_SUCCESS;
    6516          111 :         bool isGraphZeroCopyAlgAlloc = false;
    6517              :         // 只有aicpu模式下才需要申请从流和相关的notify资源,isNeedSlaveStream为true就代表算子下发是aicpu模式
    6518          111 :         bool isNeedSlaveStream = !selectAivAlg && opParam.aicpuUnfoldMode;
    6519          170 :         if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB &&
    6520           60 :             !IsForceAicpuOpBaseMode(opParam, opType)) {
    6521           59 :             isGraphZeroCopyAlgAlloc = resRequest.isInGraphCaptureZeroCopy;
    6522           59 :             if (isGraphZeroCopyAlgAlloc) {
    6523            0 :                 if (resRequest.scratchMemSize > 0) {
    6524            0 :                     algResResponse.scratchMem =
    6525            0 :                         DeviceMem::create(cclBufferManager_.GetOutCCLbuffer().ptr(), resRequest.scratchMemSize);
    6526              :                 }
    6527           59 :             } else if (resRequest.scratchMemSize > 0) {
    6528           22 :                 algResResponse.scratchMem = GetWorkspaceScracthMem(opParam.tag, resRequest.scratchMemSize);
    6529              :             }
    6530              : 
    6531           59 :             if (resRequest.streamNum > 0) {
    6532           18 :                 if (isGraphZeroCopyAlgAlloc) {
    6533            0 :                     CHK_RET(opStreamManager_->RegisterMaster(opParam.stream));
    6534              :                     algResResponse.slaveStreams =
    6535            0 :                         opStreamManager_->AllocSlaves(StreamType::STREAM_TYPE_ONLINE, resRequest.streamNum);
    6536            0 :                     CHK_PRT_RET(algResResponse.slaveStreams.empty(),
    6537              :                                 HCCL_ERROR("[AllocAlgResource]tag[%s] get slave stream failed, "
    6538              :                                            "expect to get size [%u], but only alloc 0.",
    6539              :                                            newTag.c_str(), resRequest.streamNum),
    6540              :                                 HCCL_E_INTERNAL);
    6541              :                 } else {
    6542              :                     // aicpu模式下不申请host从流
    6543           18 :                     if (!opParam.aicpuUnfoldMode) {
    6544           14 :                         algResResponse.slaveStreams = GetWorkspaceSubStreams(opParam.tag, resRequest.streamNum);
    6545              :                     }
    6546              :                 }
    6547              :             }
    6548           50 :         } else if (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE ||
    6549            0 :                  IsForceAicpuOpBaseMode(opParam, opType)) {
    6550           50 :             CHK_RET(AllocOpBaseModeScratchMem(opType, opParam, resRequest, algResResponse));
    6551           51 :             if ((resRequest.streamNum > 0) && !selectAivAlg) {
    6552           36 :                 CHK_RET(opStreamManager_->RegisterMaster(opParam.stream));
    6553              :                 algResResponse.slaveStreams =
    6554           35 :                     opStreamManager_->AllocSlaves(StreamType::STREAM_TYPE_ONLINE, resRequest.streamNum);
    6555           36 :                 CHK_PRT_RET(algResResponse.slaveStreams.empty(),
    6556              :                             HCCL_ERROR("[AllocAlgResource]tag[%s] get slave stream failed, "
    6557              :                                        "expect to get size [%u], but only alloc 0.",
    6558              :                                        newTag.c_str(), resRequest.streamNum),
    6559              :                             HCCL_E_INTERNAL);
    6560              :             }
    6561              :         } else {
    6562            1 :             HCCL_ERROR("[AllocAlgResource]WorkflowMode is not set.");
    6563            0 :             return HCCL_E_PARA;
    6564              :         }
    6565              : 
    6566          109 :         if (isNeedSlaveStream && ((userRankSize_ != 1) || IsForceAicpuOpBaseMode(opParam, opType))) {
    6567            4 :             CHK_RET(opStreamManager_->RegisterMaster(opParam.stream));
    6568              :             algResResponse.slaveDevStreams =
    6569            4 :                     opStreamManager_->AllocSlaves(StreamType::STREAM_TYPE_DEVICE, LOCAL_STREAM_MAX_NUM);
    6570            4 :             CHK_PRT_RET(algResResponse.slaveDevStreams.empty(),
    6571              :                         HCCL_ERROR("[AllocAlgResource]tag[%s] get slave device stream failed, "
    6572              :                                    "expect to get size [%u], but only alloc 0.",
    6573              :                                    newTag.c_str(), LOCAL_STREAM_MAX_NUM),
    6574              :                         HCCL_E_INTERNAL);
    6575            4 :             CHK_RET(AllocAlgNotifys(opParam.tag, NotifyLoadType::DEVICE_NOTIFY, LOCAL_NOTIFY_MAX_NUM,
    6576              :                                     algResResponse.notifiesDevMain, algResResponse.notifiesDevAux));
    6577              :         }
    6578          109 :         uint8_t devNotifyNum = algResResponse.notifiesDevMain.size() + algResResponse.notifiesDevAux.size();
    6579          109 :         HCCL_INFO("[AllocAlgResource] tag[%s] alloc host slaveStreamNum[%u],"
    6580              :             "device slaveStreamNum[%u], devNotifyNum[%u], hostNotifyNum[%u]",
    6581              :             newTag.c_str(), algResResponse.slaveStreams.size(),
    6582              :             algResResponse.slaveDevStreams.size(), devNotifyNum, resRequest.notifyNum);
    6583          112 :         CHK_RET(AllocAlgNotifys(opParam.tag, NotifyLoadType::HOST_NOTIFY, resRequest.notifyNum, algResResponse.notifiesMain,
    6584              :                                 algResResponse.notifiesAux));
    6585              : 
    6586          112 :         algResResponse.cclInputMem = cclBufferManager_.GetInCCLbuffer();
    6587          112 :         algResResponse.cclOutputMem = cclBufferManager_.GetOutCCLbuffer();
    6588          111 :         DeviceMem expMem = cclBufferManager_.GetCommCCLBuffer(); // 获取拓展内存
    6589          111 :         if (opParam.opType == HcclCMDType::HCCL_CMD_ALLTOALLV || opParam.opType == HcclCMDType::HCCL_CMD_ALLTOALLVC || opParam.opType == HcclCMDType::HCCL_CMD_ALLTOALL) {
    6590            4 :             DeviceMem tinySendRecvMem;
    6591            4 :             CHK_RET(implAlg_->GetTinyMem(tinySendRecvMem));
    6592            4 :             CHK_RET(CalcTinySendRecvMem(opParam, algResResponse, tinySendRecvMem));
    6593            8 :         } else {
    6594          107 :             algResResponse.paramInputMem = DeviceMem::create(opParam.inputPtr, opParam.inputSize);
    6595          107 :             algResResponse.paramOutputMem = DeviceMem::create(opParam.outputPtr, opParam.outputSize);
    6596              :         }
    6597              : 
    6598          111 :         bool useOpbaseFlag = (GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE && !opParam.isCapture);
    6599          111 :         if (AIV_COMM_BUFFER_BITMASK & resRequest.aivBufferRequest) {
    6600            4 :             ret = cclBufferManager_.CreateCommAIVbuffer(useOpbaseFlag);
    6601            4 :             CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Alloc][AlgResource]Create CommAIVbuffer failed"), ret);
    6602            4 :             if (useOpbaseFlag) { // 单算子非Capture模式,对应aivOpbaseTag_
    6603            2 :                 algResResponse.aivInputMem = cclBufferManager_.GetInAivOpbaseBuffer();
    6604            2 :                 algResResponse.aivOutputMem = cclBufferManager_.GetOutAivOpbaseBuffer();
    6605              :             } else { // 静态图或者Capture模式,对应aivOffloadTag_
    6606            2 :                 algResResponse.aivInputMem = cclBufferManager_.GetInAivOffloadbuffer();
    6607            2 :                 algResResponse.aivOutputMem = cclBufferManager_.GetOutAivOffloadbuffer();
    6608              :             }
    6609            4 :             HCCL_INFO("[AllocAlgResource] tag[%s] alloc aiv buffer", newTag.c_str());
    6610              :         }
    6611          111 :         if ((AIV_COMM_INFO_BUFFER_BITMASK & resRequest.aivBufferRequest) || opParam.isNpuDirectRoce) {
    6612            0 :             if (!useOpbaseFlag) {
    6613            0 :                 DeviceMem aivCommInfoMem; // 图模式每个算子单独一块内存
    6614            0 :                 CHK_RET(DeviceMem::alloc(aivCommInfoMem, AIV_COMM_INFO_SIZE));
    6615            0 :                 algResResponse.aivCommInfoMem = aivCommInfoMem;
    6616            0 :                 aivOffloadCommInfoMem_.emplace_back(std::move(aivCommInfoMem));
    6617            0 :             } else {
    6618            0 :                 ret = cclBufferManager_.CreateCommInfoAIVbuffer();
    6619            0 :                 CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Alloc][AlgResource]Create CommInfoAIVbuffer failed"), ret);
    6620            0 :                 algResResponse.aivCommInfoMem = cclBufferManager_.GetAivCommInfoBuffer(); // 单算子每个通信域只用一块内存
    6621              :             }
    6622            0 :             HCCL_INFO("[AllocAlgResource] tag[%s] alloc aiv comm info buffer", newTag.c_str());
    6623              :         }
    6624              : 
    6625          222 :         TransportIOMem transMem{algResResponse.cclInputMem, algResResponse.cclOutputMem,
    6626          333 :                                 algResResponse.paramInputMem, algResResponse.paramOutputMem, algResResponse.scratchMem,
    6627          111 :                                 algResResponse.aivInputMem, algResResponse.aivOutputMem, expMem, DeviceMem()};
    6628          110 :         HCCL_DEBUG("algResResponse.cclInputMem[%p], size[%llu]; algResResponse.cclOutputMem[%p], "
    6629              :                    "size[%llu]; algResResponse.paramInputMem[%p], size[%llu]; algResResponse.paramOutputMem[%p], size[%llu].",
    6630              :                    algResResponse.cclInputMem.ptr(), algResResponse.cclInputMem.size(),
    6631              :                    algResResponse.cclOutputMem.ptr(), algResResponse.cclOutputMem.size(),
    6632              :                    algResResponse.paramInputMem.ptr(), algResResponse.paramInputMem.size(),
    6633              :                    algResResponse.paramOutputMem.ptr(), algResResponse.paramOutputMem.size());
    6634          112 :         algResResponse.opTransportResponse = resRequest.opTransport;
    6635              : 
    6636              :         // 零拷贝场景这里只借助P2p的openIpc能力交换控制面zeroCopyLocalBuffer_,不交换实际用户的输出输出
    6637          111 :         if (opParam.isZeroCopy) {
    6638            0 :             HCCL_INFO("[AllocAlgResource] zero copy change paramInput[%p] paramOutput[%p] scratchMem[%p] to localBuffer[%p]",
    6639              :                       transMem.paramInputMem.ptr(), transMem.paramOutputMem.ptr(), transMem.scratchMem.ptr(), zeroCopyLocalBuffer_.ptr());
    6640            0 :             transMem.scratchMem = zeroCopyLocalBuffer_;
    6641            0 :             transMem.paramInputMem = zeroCopyLocalBuffer_;
    6642            0 :             transMem.paramOutputMem = zeroCopyLocalBuffer_;
    6643              :         } else {
    6644          111 :             if (isGraphZeroCopyAlgAlloc) {
    6645            0 :                 transMem.scratchMem =
    6646            0 :                     DeviceMem::create(cclBufferManager_.GetOutCCLbuffer().ptr(), resRequest.scratchMemSize);
    6647            0 :                 HCCL_INFO("[AllocAlgResource] acl graph set transMem.scratchMem =%ul", transMem.scratchMem.size());
    6648              :             }
    6649              :         }
    6650              : 
    6651          111 :         ClearOpTransportResponseLinks(algResResponse.opTransportResponse);
    6652          111 :         if (IsEnableBackupLink()) {
    6653            0 :             algResResponse.opTransportResponseBackUp = resRequest.opTransport;
    6654            0 :             ClearOpTransportResponseLinks(algResResponse.opTransportResponseBackUp);
    6655            0 :             HCCL_DEBUG("[%s]IsEnableBackupLink[%d] init backup & default opTransportResponse", __func__,
    6656              :                        IsEnableBackupLink());
    6657              :         }
    6658              : 
    6659          111 :         if (!GetExternalInputHcclEnableFfts() && GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
    6660           43 :             u32 slaveNum = algResResponse.slaveStreams.size();
    6661           43 :             algResResponse.threadManage.resize(slaveNum);
    6662          113 :             for (u32 ringIndex = 0; ringIndex < slaveNum; ringIndex++) {
    6663          140 :                 algResResponse.threadManage[ringIndex].reset(new (std::nothrow) ThreadManage(deviceLogicId_,
    6664              :                                                                                              userRank_,
    6665           70 :                                                                                              dispatcher_));
    6666           70 :                 CHK_SMART_PTR_NULL(algResResponse.threadManage[ringIndex]);
    6667           70 :                 HcclResult ret = algResResponse.threadManage[ringIndex]->Init();
    6668           70 :                 CHK_PRT_RET(ret != HCCL_SUCCESS,
    6669              :                             HCCL_ERROR("[Init][MultiRingResource]ringIndex[%u] ThreadManage failed,return[%d]",
    6670              :                                        ringIndex, ret),
    6671              :                             ret);
    6672           70 :                 HCCL_INFO("ringThreadsManage Init success[%u]", ringIndex);
    6673              :             }
    6674              :         }
    6675          111 :         transportManager_->SetOpType(opParam.opType);
    6676          112 :         if (isUserMemRegisted_) {
    6677              :             // user win模式,用exchange接口建链的transport
    6678            0 :             algResResponse.opTransportResponse = userMemTransport_;
    6679            0 :             CHK_RET(GetRemoteUserMemResource());
    6680              :         } else {
    6681          112 :             StateGuard<HcclCommunicator, HcclCommState> guard(this, HcclCommState::BUILDING);
    6682          112 :             ret = transportManager_->Alloc(opParam.tag, transMem, algResResponse.opTransportResponse,
    6683          112 :                                            opParam.aicpuUnfoldMode, false, opParam.isZeroCopy, opParam.opType,
    6684          112 :                                            opParam.isCapture, false, opParam.isNpuDirectRoce, &opParam);
    6685          112 :             CHK_PRT_RET(ret != HCCL_SUCCESS,
    6686              :                 HCCL_ERROR("[%s]Alloc transports failed, tag[%s]", __func__, newTag.c_str()), ret);
    6687          112 :         }
    6688           59 :         CHK_PRT_RET(ret != HCCL_SUCCESS,
    6689              :                     HCCL_ERROR("[%s]Alloc transports failed, tag[%s]", __func__, newTag.c_str()), ret);
    6690              : 
    6691           59 :         if (retryEnable_) {
    6692              :             // 获取当前rdma相连的所有对端rankList
    6693            0 :             std::vector<u32> rankList;
    6694            0 :             CHK_RET(transportManager_->GetRemoteRankList(algResResponse.opTransportResponse, rankList,
    6695              :                                                          TransportType::TRANS_TYPE_IBV_EXP));
    6696            0 :             std::string rankListStr = "";
    6697            0 :             for (auto remoteRank : rankList) {
    6698            0 :                 rankListStr += (std::to_string(remoteRank) + ";");
    6699              :             }
    6700            0 :             HCCL_DEBUG("identifier[%s] newTag[%s] rankList[%s]", identifier_.c_str(), newTag.c_str(), rankListStr.c_str());
    6701            0 :             CHK_RET(OpRetryManager::AddLinkInfoByIdentifier(deviceLogicId_, identifier_, newTag, rankList));
    6702            0 :         }
    6703              : 
    6704           59 :         if (IsEnableBackupLink()) {
    6705              :             // 超节点 && level2支持重执行 && Aicpu:创建备用Transport资源
    6706            0 :             StateGuard<HcclCommunicator, HcclCommState> guard(this, HcclCommState::BUILDING);
    6707            0 :             ret = transportManager_->Alloc(opParam.tag, transMem, algResResponse.opTransportResponseBackUp,
    6708            0 :                                            opParam.aicpuUnfoldMode, true, opParam.isCapture);
    6709            0 :             CHK_PRT_RET(ret != HCCL_SUCCESS,
    6710              :                         HCCL_ERROR("[%s]Alloc backup transports failed, tag[%s]", __func__, newTag.c_str()), ret);
    6711            0 :         }
    6712           59 :         SaveLinkRes(algResResponse.opTransportResponse);
    6713           59 :         SaveLinkRes(algResResponse.opTransportResponseBackUp);
    6714           59 :         remoteTransportMap_ = transportManager_->GetRemoteTransportMap();
    6715           59 :         HCCL_DEBUG("[%s] process success newtag[%s]", __func__, newTag.c_str());
    6716           59 :         return HCCL_SUCCESS;
    6717          112 :     }
    6718              : 
    6719            0 :     HcclResult HcclCommunicator::GetRemoteUserMemResource()
    6720              :     {
    6721            0 :         for (auto &levelNSubCommTransport : userMemTransport_) {
    6722            0 :             for (auto &singleSubCommTransport : levelNSubCommTransport) {
    6723            0 :                 u32 linkIdx = 0;
    6724            0 :                 for (auto &transportRequest : singleSubCommTransport.transportRequests) {
    6725            0 :                     if (!transportRequest.isValid) {
    6726            0 :                         continue;
    6727              :                     }
    6728            0 :                     auto tempLink = singleSubCommTransport.links[linkIdx];
    6729            0 :                     MemDetails remoteMem;
    6730            0 :                     u32 remoteId = tempLink->GetRemoteRank();
    6731            0 :                     CHK_PRT_RET((remoteId >= MAX_RANK_NUM_A3),
    6732              :                         HCCL_ERROR("[%s]Invalid remoteId, valid range is [0, %u), remoteId[%u]", __func__,
    6733              :                             MAX_RANK_NUM_A3, remoteId), HCCL_E_PARA);
    6734            0 :                     void *userMemPtr = nullptr;
    6735            0 :                     CHK_RET(tempLink->GetRemoteMem(UserMemType::INPUT_MEM, &userMemPtr));
    6736            0 :                     CHK_PTR_NULL(userMemPtr);
    6737            0 :                     remoteMem.addr = reinterpret_cast<u64>(userMemPtr);
    6738            0 :                     CHK_RET(tempLink->GetRemoteMemSize(UserMemType::INPUT_MEM, remoteMem.size));
    6739            0 :                     opResPara_.userMemRes[remoteId] = remoteMem;
    6740            0 :                     HCCL_INFO("[%s]add userMem res success, remoteId[%u], "
    6741              :                             "remote addr[%llu], linkIdx[%u]", __func__, remoteId, remoteMem.addr, linkIdx);
    6742            0 :                     linkIdx++;
    6743            0 :                 }
    6744              :             }
    6745              :         }
    6746            0 :         opResPara_.userMemType = TYPE_USER_MEM;
    6747            0 :         return HCCL_SUCCESS;
    6748              :     }
    6749              : 
    6750            0 :     HcclResult HcclCommunicator::IncreAllocLink(const std::string &newTag, const OpParam &opParam,
    6751              :                                                 AlgResourceRequest &resRequest, AlgResourceResponse &algResResponse)
    6752              :     {
    6753            0 :         algResResponse.cclInputMem = cclBufferManager_.GetInCCLbuffer();
    6754            0 :         algResResponse.cclOutputMem = cclBufferManager_.GetOutCCLbuffer();
    6755            0 :         DeviceMem expMem = cclBufferManager_.GetCommCCLBuffer();
    6756            0 :         transportManager_->SetOpType(opParam.opType);
    6757              : 
    6758            0 :         TransportIOMem transMem{algResResponse.cclInputMem, algResResponse.cclOutputMem,
    6759            0 :                                 algResResponse.paramInputMem, algResResponse.paramOutputMem, algResResponse.scratchMem,
    6760            0 :                                 algResResponse.aivInputMem, algResResponse.aivOutputMem, expMem, DeviceMem()};
    6761              :         {
    6762            0 :             StateGuard<HcclCommunicator, HcclCommState> guard(this, HcclCommState::BUILDING);
    6763            0 :             CHK_RET(transportManager_->IncreAlloc(opParam.tag, transMem, resRequest.opTransport,
    6764              :                                                   algResResponse.opTransportResponse, opParam.aicpuUnfoldMode, false,
    6765              :                                                   opParam.isCapture, opParam.opType));
    6766            0 :         }
    6767            0 :         if (retryEnable_) {
    6768              :             // 获取当前rdma相连的所有对端rankList
    6769            0 :             std::vector<u32> rankList;
    6770            0 :             CHK_RET(transportManager_->GetIncreRemoteRankList(resRequest.opTransport,
    6771              :                                                               rankList, TransportType::TRANS_TYPE_IBV_EXP));
    6772            0 :             std::string rankListStr = "";
    6773            0 :             for (auto remoteRank : rankList)
    6774              :             {
    6775            0 :                 rankListStr += (std::to_string(remoteRank) + ";");
    6776              :             }
    6777            0 :             HCCL_DEBUG("identifier[%s] newTag[%s] rankList[%s]", identifier_.c_str(), newTag.c_str(), rankListStr.c_str());
    6778            0 :             CHK_RET(OpRetryManager::AddLinkInfoByIdentifier(deviceLogicId_, identifier_, newTag, rankList, true));
    6779            0 :         }
    6780            0 :         if (IsEnableBackupLink()) {
    6781            0 :             StateGuard<HcclCommunicator, HcclCommState> guard(this, HcclCommState::BUILDING);
    6782            0 :             CHK_RET(transportManager_->IncreAlloc(opParam.tag, transMem, resRequest.opTransport,
    6783              :                                                   algResResponse.opTransportResponseBackUp, opParam.aicpuUnfoldMode, true,
    6784              :                                                   opParam.isCapture, opParam.opType));
    6785            0 :         }
    6786            0 :         remoteTransportMap_ = transportManager_->GetRemoteTransportMap();
    6787            0 :         SaveLinkRes(algResResponse.opTransportResponse);
    6788            0 :         SaveLinkRes(algResResponse.opTransportResponseBackUp);
    6789            0 :         return HCCL_SUCCESS;
    6790            0 :     }
    6791              : 
    6792            0 :     HcclResult HcclCommunicator::SetDevicePid(s32 devicePid)
    6793              :     {
    6794            0 :         devicePid_ = devicePid;
    6795            0 :         return HCCL_SUCCESS;
    6796              :     }
    6797              : 
    6798          804 :     void HcclCommunicator::ReleaseWorkSpacebuffer()
    6799              :     {
    6800          804 :         workSpace_.free();
    6801          803 :     }
    6802              : 
    6803            0 :     HcclResult HcclCommunicator::AllocAndClearDeviceMem(u64 size, std::shared_ptr<DeviceMem> &bufferPtr) const
    6804              :     {
    6805            0 :         CHK_PRT_RET(!size,
    6806              :                     HCCL_INFO("[HcclCommunicator][AllocAndClearDeviceMem]device memory size is zero. not need to malloc memory"),
    6807              :                     HCCL_SUCCESS);
    6808              : 
    6809              :         CHK_PRT_RET((size > ULONG_MAX),
    6810              :                     HCCL_ERROR("[HcclCommunicator][AllocAndClearDeviceMem]device memory size is greater than %llu", ULONG_MAX),
    6811              :                     HCCL_E_PARA);
    6812              : 
    6813            0 :         DeviceMem tmpBuffer;
    6814            0 :         CHK_RET(DeviceMem::alloc(tmpBuffer, size));
    6815            0 :         EXCEPTION_CATCH((bufferPtr = std::make_shared<DeviceMem>(std::move(tmpBuffer))), return HCCL_E_PTR);
    6816              : 
    6817            0 :         CHK_PRT_RET(size && !bufferPtr.get()->ptr(),
    6818              :                     HCCL_ERROR("[HcclCommunicator][AllocAndClearDeviceMem]Create DeviceMem size[%llu] fail,"
    6819              :                                "please check workspace size.",
    6820              :                                size),
    6821              :                     HCCL_E_PTR);
    6822            0 :         CHK_RET(hrtMemSet(bufferPtr.get()->ptr(), size, size));
    6823            0 :         return HCCL_SUCCESS;
    6824            0 :     }
    6825              : 
    6826         1488 :     HcclResult HcclCommunicator::AllocAndClearHostMem(u64 size, std::shared_ptr<HostMem> &bufferPtr) const
    6827              :     {
    6828         1488 :         CHK_PRT_RET(!size,
    6829              :                     HCCL_INFO("[HcclCommunicator][AllocAndClearHostMem] host memory size is zero. not need to malloc memory"),
    6830              :                     HCCL_SUCCESS);
    6831              : 
    6832              :         CHK_PRT_RET((size > ULONG_MAX),
    6833              :                     HCCL_ERROR("[HcclCommunicator][AllocAndClearHostMem] host memory size is greater than %llu", ULONG_MAX),
    6834              :                     HCCL_E_PARA);
    6835              : 
    6836         1488 :         HostMem tmpBuffer = HostMem::alloc(size);
    6837         1495 :         EXCEPTION_CATCH((bufferPtr = std::make_shared<HostMem>(std::move(tmpBuffer))), return HCCL_E_PTR);
    6838              : 
    6839         1492 :         CHK_PRT_RET(size && !bufferPtr.get()->ptr(),
    6840              :                     HCCL_ERROR("[HcclCommunicator][AllocAndClearHostMem]host memory space size[%llu] fail,"
    6841              :                                "please check workspace size.",
    6842              :                                size),
    6843              :                     HCCL_E_PTR);
    6844         1492 :         CHK_SAFETY_FUNC_RET(memset_s(bufferPtr.get()->ptr(), size, 0, size));
    6845         1491 :         return HCCL_SUCCESS;
    6846         1491 :     }
    6847              : 
    6848          520 :     HcclResult HcclCommunicator::CreateWorkSpace(u64 size, DeviceMem &buffer) const
    6849              :     {
    6850          520 :         CHK_PRT_RET(!size, HCCL_INFO("[Create][WorkSpace]work space size is zero. not need to malloc memory"),
    6851              :                     HCCL_SUCCESS);
    6852              : 
    6853              :         CHK_PRT_RET((size > ULONG_MAX),
    6854              :                     HCCL_ERROR("[Create][WorkSpace]work space size is greater than %llu",
    6855              :                                ULONG_MAX),
    6856              :                     HCCL_E_PARA);
    6857              : 
    6858          520 :         u64 memSize = size;
    6859          520 :         CHK_RET(DeviceMem::alloc(buffer, memSize));
    6860          520 :         CHK_RET(hrtMemSet(buffer.ptr(), size, size));
    6861          520 :         return HCCL_SUCCESS;
    6862              :     }
    6863              : 
    6864            0 :     HcclResult HcclCommunicator::GetWorkSpace(u64 *workSpaceSize, u64 *workSpace) const
    6865              :     {
    6866            0 :         *workSpaceSize = workSpaceSize_;
    6867            0 :         *workSpace = reinterpret_cast<u64>(workSpace_.ptr());
    6868            0 :         return HCCL_SUCCESS;
    6869              :     }
    6870              : 
    6871            0 :     HcclResult HcclCommunicator::InitWorkSpace()
    6872              :     {
    6873            0 :         if (workSpace_.ptr() == nullptr) {
    6874            0 :             workSpaceSize_ = COMM_MAX_WORK_SPACE_SIZE;
    6875            0 :             CHK_RET(CreateWorkSpace(workSpaceSize_, workSpace_));
    6876              :         }
    6877            0 :         return HCCL_SUCCESS;
    6878              :     }
    6879              : 
    6880            0 :     HcclResult HcclCommunicator::FillOpParam(const HcclCMDType commType, OpParam &opParam,
    6881              :                                              const uint64_t count, void *pCount, void *pDispls)
    6882              :     {
    6883            0 :         if (commType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER ||
    6884            0 :             commType == HcclCMDType::HCCL_CMD_ALLGATHER ||
    6885              :             commType == HcclCMDType::HCCL_CMD_ALLREDUCE) {
    6886            0 :             opParam.DataDes.count = count;
    6887            0 :             opParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_FP16; // 按照fp16配置
    6888            0 :         } else if (commType == HcclCMDType::HCCL_CMD_ALLTOALLV ||
    6889            0 :                  commType == HcclCMDType::HCCL_CMD_ALLTOALL ||
    6890              :                  commType == HcclCMDType::HCCL_CMD_ALLTOALLVC) {
    6891            0 :             opParam.All2AllDataDes.sendType = HcclDataType::HCCL_DATA_TYPE_FP16;
    6892            0 :             opParam.All2AllDataDes.recvType = HcclDataType::HCCL_DATA_TYPE_FP16;
    6893            0 :             opParam.All2AllDataDes.sendCounts = pCount;
    6894            0 :             opParam.All2AllDataDes.recvCounts = pCount;
    6895            0 :             opParam.All2AllDataDes.sdispls = pDispls;
    6896            0 :             opParam.All2AllDataDes.rdispls = pDispls;
    6897            0 :             opParam.All2AllDataDes.sendCountMatrix = pCount;
    6898            0 :         } else if (commType == HcclCMDType::HCCL_CMD_BATCH_WRITE) {
    6899              :         } else {
    6900            0 :             HCCL_ERROR("[%s] invalid commType=[%u]",
    6901              :                        __func__, static_cast<uint32_t>(commType));
    6902            0 :             return HCCL_E_PARA;
    6903              :         }
    6904            0 :         return HCCL_SUCCESS;
    6905              :     }
    6906              : 
    6907            0 :     HcclResult HcclCommunicator::AllocComResource(const string &newTag, const string &algName,
    6908              :         const HcclCMDType commType, const OpParam &opParam, rtStream_t stream, bool isNeedHostSlaveStream)
    6909              :     {
    6910            0 :         if (resMap_.find(newTag) == resMap_.end()) { // 计算&申请通信资源
    6911            0 :             unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(commType);
    6912            0 :             CHK_PRT_RET(algOperator == nullptr,
    6913              :                         HCCL_ERROR("[%s] algOperator is nullptr", __func__), HCCL_E_INTERNAL);
    6914            0 :             AlgResourceRequest resRequest;
    6915            0 :             CHK_RET(algOperator->CalcResRequest(algName, opParam, resRequest));
    6916            0 :             AlgResourceResponse algResResponse;
    6917            0 :             CHK_RET(AllocAlgResource(newTag, commType, opParam, resRequest, algResResponse, isNeedHostSlaveStream));
    6918            0 :             resMap_[newTag] = algResResponse;
    6919            0 :             CHK_RET(RegisterToHeartBeat());
    6920            0 :         }
    6921              : 
    6922            0 :         CHK_RET(InitWorkSpace());
    6923            0 :         HcclResult ret = GetWorkSpace(&(opResPara_.mc2WorkSpace.workSpaceSize), &(opResPara_.mc2WorkSpace.workSpace));
    6924            0 :         CHK_PRT_RET(ret != HCCL_SUCCESS,
    6925              :                     HCCL_ERROR("%s GetWorkSpace fail, size[%llu] space[%llu]", __func__,
    6926              :                                opResPara_.mc2WorkSpace.workSpaceSize, opResPara_.mc2WorkSpace.workSpace),
    6927              :                     ret);
    6928              : 
    6929            0 :         if (!isContextLaunched_) { // 通信域内首次下发
    6930            0 :             uint64_t streamMode = 0;
    6931            0 :             CHK_RET(hrtStreamGetMode(opParam.stream.ptr(), &streamMode));
    6932              :             rtStream_t aicpuStream;
    6933            0 :             Mc2AiCpuStreamAllocAndGet(streamMode, aicpuStream); // aicpuStream需要在首次下发时申请
    6934              : 
    6935              :             rtStream_t aicpuInitStream;
    6936            0 :             Mc2AiCpuInitStreamAllocAndGet(streamMode, aicpuInitStream);
    6937            0 :             Stream tmpStream(aicpuInitStream);
    6938            0 :             HCCL_DEBUG("%s ContextLaunched, aicpuInitStream:%p, aicpuStream:%p", __func__, aicpuInitStream, aicpuStream);
    6939            0 :             CHK_RET(AicpuResourceInit(algName, resMap_[newTag], newTag, stream, commType));
    6940            0 :             CHK_RET(GetReportHcclMC2Info(tmpStream, resMap_[newTag].slaveDevStreams));
    6941            0 :         } else if (newTagResAlloced_.find(newTag) == newTagResAlloced_.end()) {
    6942              :             // 通信域内非首次,但是有新的newTag
    6943            0 :             PetersonLockGuard guard(hostDeviceLock_.get());
    6944            0 :             CHK_PRT_RET(guard.IsLockFailed(),
    6945              :                         HCCL_ERROR("[%s] hostDeviceLock lock failed", __func__), HCCL_E_INTERNAL);
    6946            0 :             CHK_RET(AicpuResourceRefresh(resMap_[newTag], newTag, commType));
    6947            0 :         }
    6948            0 :         return HCCL_SUCCESS;
    6949              :     }
    6950              : 
    6951            2 :     HcclResult HcclCommunicator::AllocComResourceByTiling(const string &algConfig, void *param)
    6952              :     {
    6953            2 :         CHK_PTR_NULL(combinOparaMem_);
    6954            2 :         HcclCombinOpParam *combinOparaPtr = reinterpret_cast<HcclCombinOpParam*>(combinOparaMem_->ptr());
    6955            2 :         CHK_PTR_NULL(combinOparaPtr);
    6956              : 
    6957            2 :         string algName, newTag;
    6958            2 :         OpParam &opParam = *static_cast<OpParam *>(param);
    6959            2 :         CHK_RET(GetAlgInfo(algConfig, opParam.tag, opParam.opType, algName, newTag));
    6960            2 :         if (algName == "RunAlltoAllAivDirect") {
    6961            1 :             opParam.isNpuDirectRoce = true;
    6962              :         }
    6963            2 :         CHK_RET(CreateAndGetAiCpuNotifyWithNotifyRes(combinOparaPtr->signalInfo.aicpuNotify));
    6964            0 :         HCCL_INFO("Create aicpu notify %p.", localAiCpuNotifyRes_[0]->ptr());
    6965              : 
    6966              :         // 只有第一次创建,此处通过CCL Buffer地址有效来防止通信域内非首次重新申请内存
    6967              :         // 已注册user mem情况下,不创建ccl buffer,使用user mem通信
    6968            0 :         if (userMemMap_.empty()) {
    6969            0 :             CHK_RET(CreateCommCCLbuffer());
    6970            0 :             CHK_RET(cclBufferManager_.GetInCCLbuffer(opParam.inputPtr, opParam.inputSize));
    6971            0 :             CHK_RET(cclBufferManager_.GetOutCCLbuffer(opParam.outputPtr, opParam.outputSize));
    6972              :         } else {
    6973            0 :             auto it = userMemMap_.begin();
    6974            0 :             opParam.outputSize = it->second->size();
    6975            0 :             opParam.inputSize = it->second->size();
    6976              :         }
    6977              : 
    6978              :         // 按照 ccl buffer size 折算,不同算子折算方式不同, allreduce和cclbuffer size相同
    6979              :         // allgather、reducescatter、alltoall需除以rank size
    6980            0 :         uint64_t count = opParam.outputSize / SIZE_TABLE[HcclDataType::HCCL_DATA_TYPE_FP16];
    6981            0 :         if (opParam.opType != HcclCMDType::HCCL_CMD_ALLREDUCE) {
    6982            0 :             count = (count + userRankSize_ - 1) / userRankSize_;
    6983              :         }
    6984            0 :         HCCL_INFO("[%s] userRankSize=[%u], count=[%u]", __func__, userRankSize_, count);
    6985            0 :         vector<uint64_t> countList(userRankSize_ * userRankSize_, count);
    6986            0 :         vector<uint64_t> displsList(userRankSize_, 0);
    6987            0 :         void *pCount = reinterpret_cast<void *>(&countList[0]);
    6988            0 :         void *pDispls = reinterpret_cast<void *>(&displsList[0]);
    6989            0 :         CHK_RET(FillOpParam(opParam.opType, opParam, count, pCount, pDispls));
    6990              :         // MC2算子不需要申请host侧的从流
    6991            0 :         bool isNeedHostSlaveStream = false;
    6992            0 :         CHK_RET(AllocComResource(newTag, algName, opParam.opType, opParam, opParam.stream.ptr(), isNeedHostSlaveStream));
    6993            0 :         return HCCL_SUCCESS;
    6994            2 :     }
    6995              : 
    6996            1 :     HcclResult HcclCommunicator::CreateCommResource(const std::string &tag, rtStream_t aiCpuStream, bool isOpbaseMode,
    6997              :                                                     void **commContext, const std::string &algConfig)
    6998              :     {
    6999            1 :         const std::string &suffix = HCCL_MC2_MULTISERVER_SUFFIX;
    7000            1 :         string algName = "";
    7001            1 :         string newTag = tag;
    7002            1 :         if (tag.size() > suffix.size() && tag.compare(tag.size() - suffix.size(), suffix.size(), suffix) == 0)
    7003              :         {
    7004            0 :             HCCL_INFO("[HcclCommunicator][CreateCommResource] Set isA2MC2MultiServer_ to [true]");
    7005            0 :             isA2MC2MultiServer_ = true;
    7006            0 :             char* mmSysGetEnvValue = nullptr;
    7007            0 :             MM_SYS_GET_ENV(MM_ENV_HCCL_INTRA_PCIE_ENABLE, mmSysGetEnvValue);
    7008            0 :             std::string intraPcieEnableEnv = (mmSysGetEnvValue != nullptr) ? mmSysGetEnvValue : "EmptyString";
    7009            0 :             bool envA2MC2Hie = (intraPcieEnableEnv == "1") && (GetExternalInputIntraRoceSwitch() == 0);
    7010            0 :             if (!algConfig.empty()) {
    7011            0 :                 CHK_RET(GetAlgInfo(algConfig, tag, algName));
    7012            0 :                 if (algName == "DispatchCombineHierarchy" || (algName == "BatchWriteBySdma" && envA2MC2Hie)) {
    7013            0 :                     isA2MC2IntraHie_ = true;
    7014            0 :                     newTag.insert(newTag.size() - suffix.size(), "_HIE");
    7015              :                 }
    7016              :             }
    7017            0 :         }
    7018            1 :         if (isA2MC2MultiServer_ && !isNeedInitNic_) {
    7019            0 :             InitNic(true);
    7020              :         }
    7021              : 
    7022            1 :         if ((deviceType_ != DevType::DEV_TYPE_910_93 && moduleNum_ > 1 && !isA2MC2MultiServer_) ||
    7023            1 :             (deviceType_ == DevType::DEV_TYPE_910_93 && superPodNum_ > 1)) {
    7024            0 :             HCCL_ERROR("[HcclCommunicator][CommResource]MC2 does not support in the current scenario, "
    7025              :                        "device type[%d] moduleNum[%d] serverNum[%d] superPodNum[%d], isMC2MultiServer[%d].",
    7026              :                        deviceType_, moduleNum_, serverNum_, superPodNum_, isA2MC2MultiServer_);
    7027            0 :             return HCCL_E_NOT_SUPPORT;
    7028              :         }
    7029              : 
    7030            1 :         HCCL_INFO("[HcclCommunicator][CommResource]newTag[%s] aicpu stream[%p] isOpbaseMode[%u]", newTag.c_str(), aiCpuStream,
    7031              :                   isOpbaseMode);
    7032              : 
    7033            1 :         Stream stream(aiCpuStream);
    7034            1 :         CHK_RET(CreateCommAndStreamRes(newTag, stream));
    7035              : 
    7036            1 :         CHK_RET(Mc2CreateAndLaunchContext(aiCpuStream, isOpbaseMode, commContext, newTag));
    7037            1 :         return HCCL_SUCCESS;
    7038            1 :     }
    7039              : 
    7040            0 :     HcclResult HcclCommunicator::Mc2CreateAndLaunchContext(rtStream_t aiCpuStream, bool isOpbaseMode, void **commContext, const string &tag)
    7041              :     {
    7042            0 :         CHK_PTR_NULL(combinOparaMem_);
    7043            0 :         HcclCombinOpParam *combinOparaPtr = reinterpret_cast<HcclCombinOpParam*>(combinOparaMem_->ptr());
    7044            0 :         CHK_PTR_NULL(combinOparaPtr);
    7045            0 :         CHK_RET(InitWorkSpace());
    7046              : 
    7047            0 :         HcclResult result = GetWorkSpace(&(combinOparaPtr->mc2WorkSpace.workSpaceSize), &(combinOparaPtr->mc2WorkSpace.workSpace));
    7048            0 :         CHK_PRT_RET(result != HCCL_SUCCESS,
    7049              :                     HCCL_ERROR("[HcclCommunicator][CommResource]errNo[0x%016llx] size[%llu] space[%llu]",
    7050              :                                HCCL_ERROR_CODE(result), combinOparaPtr->mc2WorkSpace.workSpaceSize, combinOparaPtr->mc2WorkSpace.workSpace),
    7051              :                     result);
    7052              : 
    7053            0 :         CHK_SAFETY_FUNC_RET(memcpy_s(combinOparaPtr->hcomId, sizeof(combinOparaPtr->hcomId),
    7054              :                                      identifier_.c_str(), identifier_.length() + 1));
    7055              : 
    7056            0 :         Stream tmpStream(aiCpuStream);
    7057            0 :         CHK_RET(CreateAndGetAiCpuNotifyWithNotifyRes(combinOparaPtr->signalInfo.aicpuNotify));
    7058            0 :         CHK_RET(CreateAndGetAiCpuNotify(localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_0)],
    7059              :             combinOparaPtr->signalInfo.aicpuOpNotify[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_0)]));
    7060            0 :         CHK_RET(CreateAndGetAiCpuNotify(localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_1)],
    7061              :             combinOparaPtr->signalInfo.aicpuOpNotify[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_1)]));
    7062              :         // 申请集合通信域存储context的device空间
    7063            0 :         CHK_RET(CreateDeviceCommContext(sizeof(HcclCombinOpParam), commContext_));
    7064            0 :         combinOparaPtr->config.deterministic = GetDeterministicConfig();
    7065              :         // retryEnable 写入aicpu_ctx
    7066            0 :         combinOparaPtr->config.retryEnable = static_cast<u8>(retryEnable_);
    7067            0 :         combinOparaPtr->config.retryHoldTime = commConfig_.GetConfigRetryHoldTime();
    7068            0 :         combinOparaPtr->config.retryIntervalTime = commConfig_.GetConfigRetryIntervalTime();
    7069            0 :         combinOparaPtr->config.notifyWaitTime =
    7070            0 :             (GetExternalInputHcclExecTimeoutSet() != HcclExecTimeoutSet::HCCL_EXEC_TIMEOUT_NOT_SET ||
    7071            0 :             commConfig_.GetConfigExecTimeOutSet()) ? commConfig_.GetConfigExecTimeOut() : NOTIFY_DEFAULT_WAIT_TIME;
    7072            0 :         combinOparaPtr->config.linkTimeOut = std::chrono::seconds(GetExternalInputHcclLinkTimeOut());
    7073              : 
    7074            0 :         combinOparaPtr->kfcControlTransferH2DParams = kfcControlTransferH2D_->GetCommunicateParams();
    7075            0 :         combinOparaPtr->kfcStatusTransferD2HParams = kfcStatusTransferD2H_->GetCommunicateParams();
    7076              : 
    7077            0 :         void *overflowAddr = nullptr;
    7078            0 :         if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
    7079            0 :             CHK_RET(hrtCtxGetOverflowAddr(&overflowAddr));
    7080            0 :             combinOparaPtr->overFlowAddr = reinterpret_cast<u64>(overflowAddr);
    7081            0 :             HCCL_INFO("[HcclImplBase][Mc2CreateAndLaunchContext]get combinOparaPtr->overFlowAddr %llx",
    7082              :                       combinOparaPtr->overFlowAddr);
    7083              :             // 非整卡 (2DUO卡各取1芯的场景) 因为受到PCIE限制,不可以使用读操作进行数据拷贝
    7084            0 :             if (pairLinkInfo_[static_cast<u32>(LinkTypeInServer::HCCS_TYPE)].size() != userRankSize_) {
    7085            0 :                 combinOparaPtr->onlyRead = 1;
    7086              :             }
    7087              :         }
    7088            0 :         HCCL_INFO("read only is set to %u", combinOparaPtr->onlyRead);
    7089              : 
    7090            0 :         if (isA2MC2MultiServer_) {
    7091              :             // 拷贝normal transport信息到device侧
    7092            0 :             bool isSupportAIVNormalQP = false;
    7093            0 :             CHK_RET(IsSupportAIVNormalQP(devicePhyId_, isSupportAIVNormalQP));
    7094            0 :             CHK_PTR_NULL(transDevIbverbsDataMem_);
    7095            0 :             const u64 ibverbsDataSize = transDevIbverbsDataMem_->size();
    7096            0 :             CHK_RET(DeviceMem::alloc(ibverbsDataBuffer_, ibverbsDataSize));
    7097            0 :             CHK_RET(hrtMemAsyncCopy(ibverbsDataBuffer_.ptr(),
    7098              :                                          ibverbsDataBuffer_.size(),
    7099              :                                          transDevIbverbsDataMem_->ptr(),
    7100              :                                          ibverbsDataSize,
    7101              :                                          HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE,
    7102              :                                          aiCpuStream));
    7103              : 
    7104            0 :             combinOparaPtr->ibverbsData = reinterpret_cast<u64>(ibverbsDataBuffer_.ptr());
    7105            0 :             combinOparaPtr->ibverbsDataSize = ibverbsDataSize;
    7106            0 :             combinOparaPtr->multiServerFlag = static_cast<u8>(true);
    7107              : 
    7108            0 :             CHK_PTR_NULL(combinedCapabilityMem_);
    7109            0 :             const u64 capabilitySize = sizeof(CombinedCapability);
    7110            0 :             CHK_RET(DeviceMem::alloc(combinedCapabilityBuffer_, capabilitySize));
    7111            0 :             CHK_RET(hrtMemAsyncCopy(combinedCapabilityBuffer_.ptr(),
    7112              :                                          combinedCapabilityBuffer_.size(),
    7113              :                                          combinedCapabilityMem_->ptr(),
    7114              :                                          capabilitySize,
    7115              :                                          HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE,
    7116              :                                          aiCpuStream));
    7117              : 
    7118            0 :             combinOparaPtr->capabilityPtr = reinterpret_cast<u64>(combinedCapabilityBuffer_.ptr());
    7119            0 :             combinOparaPtr->capabilitySize = capabilitySize;
    7120              : 
    7121            0 :             HCCL_INFO("[HcclImplBase][Mc2CreateAndLaunchContext] set ibverbsData to [%llu], "
    7122              :                       "multiServerFlag to [%u]",
    7123              :                       combinOparaPtr->ibverbsData, combinOparaPtr->multiServerFlag);
    7124            0 :             if (isSupportAIVNormalQP && isA2MC2IntraHie_) {
    7125            0 :                 CHK_RET(H2DAiRMAInfo(tag, aiCpuStream));
    7126              :             }
    7127              :         }
    7128              : 
    7129              :         // 将通信数据拷贝到device侧,供AICPU算法编排使用
    7130            0 :         CHK_RET(hrtMemAsyncCopy(commContext_.ptr(), commContext_.size(), combinOparaMem_->ptr(), combinOparaMem_->size(),
    7131              :                                      HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE, aiCpuStream));
    7132              : 
    7133            0 :         std::string kernelName = "RunAicpuKfcResInit";
    7134            0 :         CHK_RET(AiCpuKernelLaunch(tmpStream.ptr(), reinterpret_cast<u64>(commContext_.ptr()), kernelName));
    7135            0 :         SetMC2EnvFlag();
    7136            0 :         if (isOpbaseMode == true) {
    7137            0 :             CHK_RET(hcclStreamSynchronize(tmpStream.ptr(), commConfig_.GetConfigExecTimeOut()));
    7138              :         }
    7139              : 
    7140            0 :         *commContext = commContext_.ptr();
    7141            0 :         return HCCL_SUCCESS;
    7142            0 :     }
    7143              : 
    7144            0 :     HcclResult HcclCommunicator::GetAiCpuNotifyData(const std::shared_ptr<LocalNotify> &localNotify,
    7145              :                                                     HcclSignalInfo &notifyInfo)
    7146              :     {
    7147            0 :         if (localNotify == nullptr) {
    7148            0 :             HCCL_INFO("[HcclCommunicator][GetAiCpuNotifyData]notifyHandle is null");
    7149            0 :             notifyInfo.resId = INVALID_U64;
    7150            0 :             return HCCL_SUCCESS;
    7151              :         }
    7152              : 
    7153            0 :         CHK_RET(localNotify->GetNotifyData(notifyInfo));
    7154            0 :         HCCL_INFO("[HcclCommunicator][GetAiCpuNotifyData]resId[%lld], addr[%lld], devId[%u], tsId[%u].",
    7155              :                   notifyInfo.resId, notifyInfo.addr, notifyInfo.devId, notifyInfo.tsId);
    7156            0 :         return HCCL_SUCCESS;
    7157              :     }
    7158              : 
    7159            0 :     HcclResult HcclCommunicator::CreateAndGetAiCpuNotify(std::shared_ptr<LocalNotify> &localNotify,
    7160              :                                                          HcclSignalInfo &notifyInfo)
    7161              :     {
    7162            0 :         if (localNotify != nullptr) {
    7163            0 :             CHK_RET(GetAiCpuNotifyData(localNotify, notifyInfo));
    7164            0 :             HCCL_INFO("[HcclCommunicator][CreateAndGetAiCpuNotify]aicpu notify already create ptr[%p]",
    7165              :                       localNotify->ptr());
    7166            0 :             return HCCL_SUCCESS;
    7167              :         }
    7168              : 
    7169            0 :         EXCEPTION_CATCH((localNotify = std::make_shared<LocalNotify>()), return HCCL_E_PTR);
    7170            0 :         CHK_RET(localNotify->Init(NotifyLoadType::DEVICE_NOTIFY));
    7171            0 :         CHK_RET(localNotify->SetIpc());
    7172              : 
    7173            0 :         CHK_RET(GetAiCpuNotifyData(localNotify, notifyInfo));
    7174            0 :         return HCCL_SUCCESS;
    7175              :     }
    7176              : 
    7177            2 :     HcclResult HcclCommunicator::Mc2AiCpuStreamAllocAndGet(u32 streamMode, rtStream_t &aiCpuStream)
    7178              :     {
    7179            2 :         if (opStream_.ptr() != nullptr) {
    7180            0 :             HCCL_INFO("%s already alloc, group:%s, stream id:%u", __func__, identifier_.c_str(), opStream_.id());
    7181            0 :             aiCpuStream = opStream_.ptr();
    7182            0 :             return HCCL_SUCCESS;
    7183              :         }
    7184              : 
    7185            2 :         constexpr u32 aicpuStreamMode = 1; // 单独申请的kernel流,使能遇错即停,避免出错后流卡住不退
    7186            2 :         opStream_ = Stream(StreamType::STREAM_TYPE_ONLINE);
    7187            2 :         CHK_RET(hrtStreamSetMode(opStream_.ptr(), aicpuStreamMode));
    7188            2 :         aiCpuStream = opStream_.ptr();
    7189            2 :         HCCL_RUN_INFO("%s alloc success, group:%s, stream id:%u, mainStreamMode:%u, aicpuStreamMode:%u",
    7190              :                       __func__, identifier_.c_str(), opStream_.id(), streamMode, aicpuStreamMode);
    7191            2 :         return HCCL_SUCCESS;
    7192              :     }
    7193              : 
    7194            2 :     HcclResult HcclCommunicator::Mc2AiCpuInitStreamAllocAndGet(u32 streamMode, rtStream_t &aiCpuStream)
    7195              :     {
    7196            2 :         if (aicpuInitStream_.ptr() != nullptr) {
    7197            1 :             HCCL_INFO("%s already alloc, group:%s, stream id:%u", __func__, identifier_.c_str(), aicpuInitStream_.id());
    7198            1 :             aiCpuStream = aicpuInitStream_.ptr();
    7199            1 :             return HCCL_SUCCESS;
    7200              :         }
    7201              : 
    7202            1 :         constexpr u32 aicpuStreamMode = 1; // 单独申请的kernel流,使能遇错即停,避免出错后流卡住不退
    7203            1 :         aicpuInitStream_ = Stream(StreamType::STREAM_TYPE_ONLINE);
    7204            1 :         CHK_RET(hrtStreamSetMode(aicpuInitStream_.ptr(), aicpuStreamMode));
    7205            1 :         aiCpuStream = aicpuInitStream_.ptr();
    7206            1 :         HCCL_RUN_INFO("%s alloc success, group:%s, stream id:%u, mainStreamMode:%u, aicpuStreamMode:%u",
    7207              :                       __func__, identifier_.c_str(), aicpuInitStream_.id(), streamMode, aicpuStreamMode);
    7208            1 :         return HCCL_SUCCESS;
    7209              :     }
    7210              : 
    7211            0 :     HcclResult HcclCommunicator::AicpuResourceInit(const std::string &algName,
    7212              :         const AlgResourceResponse &algResource, const std::string &newTag, const rtStream_t &aicpuStream,
    7213              :         const HcclCMDType opType, bool isCustom)
    7214              :     {
    7215            0 :         HCCL_RUN_INFO("[%s] start to init group[%s] aicpu resources newTag[%s] local rankId[%u]",
    7216              :                       __func__, identifier_.c_str(), newTag.c_str(), userRank_);
    7217            0 :         isContextLaunched_ = true;
    7218            0 :         CHK_RET(BuildOpResParam(algName, algResource, newTag, opType, aicpuStream)); // 构建context结构体
    7219            0 :         std::string kernelName = "RunAicpuKfcResInitV2";
    7220              :         // 在这里构建suspending状态码的HDC通道初始化,并且在host侧进行init
    7221              :         // (这个主要是针对hcomId;对算子通信域的复用;也就是多个算子复用(tag+Identifier)这个通信域的情况)
    7222            0 :         CHK_RET(AiCpuKernelLaunch(aicpuStream, reinterpret_cast<u64>(opResDevicePara_.ptr()), kernelName));
    7223            0 :         SetMC2EnvFlag();
    7224            0 :         newTagResAlloced_.insert(newTag);
    7225              :         // 图模多档位场景,需要保证执行序上优先下资源初始化的kernel
    7226            0 :         CHK_RET(hcclStreamSynchronize(aicpuStream, commConfig_.GetConfigExecTimeOut()));
    7227              : 
    7228            0 :         if (IsEnableCustom()) {
    7229              :             struct InitTask
    7230              :             {
    7231              :                 u64 context; // A矩阵地址,通信在前时为sendbuffer
    7232              :                 bool isCustom;
    7233              :             };
    7234            0 :             InitTask customInitTask = {0};
    7235            0 :             customInitTask.context = reinterpret_cast<u64>(opResDevicePara_.ptr());
    7236            0 :             customInitTask.isCustom = true;
    7237            0 :             CHK_RET(BuildCustomOpResParam());
    7238            0 :             uint64_t customBeginTime = hrtMsprofSysCycleTime();
    7239            0 :             const std::string customProfName = "hcomAicpuCustomInit";
    7240              : 
    7241            0 :             u16 timeOut = 0;
    7242            0 :             if (opResPara_.config.notifyWaitTime == 0) {
    7243            0 :                 timeOut = opResPara_.config.notifyWaitTime;
    7244            0 :             } else if (opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC >=  MAX_VALUE_U16) {
    7245            0 :                 timeOut = MAX_VALUE_U16;
    7246              :             } else {
    7247            0 :                 timeOut = opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC;
    7248              :             }
    7249              : 
    7250            0 :             CHK_RET(AicpuAclKernelLaunch(aicpuStream, reinterpret_cast<void *>(&customInitTask),
    7251              :                 sizeof(customInitTask), binCustomHandle_, kernelName, true, timeOut));
    7252            0 :             uint64_t customEndTime = hrtMsprofSysCycleTime();
    7253            0 :             s32 customthreadId = SalGetTid();
    7254            0 :             CHK_RET(ProfilingManagerPub::CallMsprofReportNodeInfo(customBeginTime, customEndTime, customProfName,
    7255              :                                                                   customthreadId));
    7256            0 :             CHK_RET(hcclStreamSynchronize(aicpuStream, commConfig_.GetConfigExecTimeOut()));
    7257            0 :         }
    7258              : 
    7259            0 :         return HCCL_SUCCESS;
    7260            0 :     }
    7261              : 
    7262            0 :     HcclResult HcclCommunicator::AiCpuKernelLaunch(const rtStream_t stm, u64 addr, const std::string &kernelName)
    7263              :     {
    7264            0 :         uint64_t beginTime = hrtMsprofSysCycleTime();
    7265            0 :         const std::string profName = "hcomAicpuInit";
    7266              :         struct InitTask
    7267              :         {
    7268              :             u64 context; // A矩阵地址,通信在前时为sendbuffer
    7269              :             bool isCustom;
    7270              :         };
    7271            0 :         InitTask initTask = {0};
    7272            0 :         initTask.context = addr;
    7273            0 :         initTask.isCustom = false;
    7274              : 
    7275            0 :         u16 timeOut = 0;
    7276            0 :         if (opResPara_.config.notifyWaitTime == 0) {
    7277            0 :             timeOut = opResPara_.config.notifyWaitTime;
    7278            0 :         } else if (opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC >=  MAX_VALUE_U16) {
    7279            0 :             timeOut = MAX_VALUE_U16;
    7280              :         } else {
    7281            0 :             timeOut = opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC;
    7282              :         }
    7283            0 :         CHK_RET(AicpuAclKernelLaunch(stm, reinterpret_cast<void *>(&initTask), sizeof(initTask),
    7284              :             binHandle_, kernelName, true, timeOut));
    7285            0 :         uint64_t endTime = hrtMsprofSysCycleTime();
    7286            0 :         s32 threadId = SalGetTid();
    7287            0 :         CHK_RET(ProfilingManagerPub::CallMsprofReportNodeInfo(beginTime, endTime, profName, threadId));
    7288            0 :         return HCCL_SUCCESS;
    7289            0 :     }
    7290              : 
    7291            0 :     HcclResult HcclCommunicator::AicpuKfcTilingDataLaunch(const OpParam &opParam, const HcclCMDType &opType,
    7292              :                                                           const DeviceMem &deviceContext, const std::string &kernelName, const AicpuOpTiling opTilingInfo)
    7293              :     {
    7294            0 :         HCCL_DEBUG("AicpuKfcTilingDataLaunch count %llu dataType %s op %s opType %u", opParam.GetDataCount(userRank_),
    7295              :                    GetDataTypeEnumStr(opParam.GetDataType()).c_str(), GetReduceOpEnumStr(opParam.reduceType).c_str(), opType);
    7296            0 :         struct HcclKFCTilingData tilingDate = {0};
    7297            0 :         tilingDate.sendCnt = opParam.DataDes.count;
    7298            0 :         tilingDate.dataType = opParam.DataDes.dataType;
    7299            0 :         tilingDate.commType = static_cast<uint8_t>(opType);
    7300            0 :         tilingDate.reduceOp = opParam.reduceType;
    7301            0 :         tilingDate.taskType = HCCL_KFC_TASK_HCCL_ONLY_EXE;
    7302            0 :         tilingDate.totalCnt = 1;
    7303            0 :         tilingDate.turnNum = 1;
    7304            0 :         tilingDate.hasCommOut = 1;
    7305            0 :         tilingDate.debugMode = 0;
    7306            0 :         CHK_RET(SetNormalMode(dispatcher_));
    7307            0 :         HcclWorkflowMode mode = GetWorkflowMode();
    7308            0 :         Stream mainStream(opParam.stream.ptr());
    7309            0 :         CHK_RET(LocalNotify::Post(mainStream, dispatcher_,
    7310              :             localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_0)], INVALID_VALUE_STAGE));
    7311            0 :         rtStream_t kfcOpStream = opStream_.ptr();
    7312            0 :         if (opTilingInfo.isUsedMainStream) {
    7313            0 :             kfcOpStream = opParam.stream.ptr();
    7314              :         }
    7315            0 :         CHK_RET(AicpuUnfoldKernelLaunch(opParam.inputPtr, opParam.outputPtr, kfcOpStream,
    7316              :                                         reinterpret_cast<u64>(deviceContext.ptr()), &tilingDate, sizeof(HcclKFCTilingData),
    7317              :                                         kernelName, mode, opParam.tag));
    7318            0 :         CHK_RET(LocalNotify::Wait(mainStream, dispatcher_,
    7319              :             localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_1)], INVALID_VALUE_STAGE));
    7320            0 :         return HCCL_SUCCESS;
    7321            0 :     }
    7322              : 
    7323            6 :     HcclResult HcclCommunicator::AicpuKfcClearOpResLaunch(const std::unordered_set<std::string> &tags)
    7324              :     {
    7325            6 :         if (tags.empty()) {
    7326            1 :             return HCCL_SUCCESS;
    7327              :         }
    7328              :         // 仅 aicpu unfold 模式有 aicpu 端 resMap_/linkRes_ 需要清理;host 模式下没有 binHandle_
    7329            5 :         if (binHandle_ == nullptr) {
    7330            2 :             HCCL_DEBUG("[AicpuKfcClearOpResLaunch] binHandle_ null (host-mode communicator), skip; tagCount[%zu]",
    7331              :                 tags.size());
    7332            2 :             return HCCL_SUCCESS;
    7333              :         }
    7334            3 :         if (opStream_.ptr() == nullptr) {
    7335            1 :             HCCL_WARNING("[AicpuKfcClearOpResLaunch] opStream_ null, skip aicpu cleanup; tagCount[%zu]", tags.size());
    7336            1 :             return HCCL_SUCCESS;
    7337              :         }
    7338              :         // host args 通道有 size 上限,大 payload 走 args/tiling 会被拒绝。沿用 RunAicpuKfcResInit 模式:HBM buffer 持载 payload
    7339            2 :         if (!aicpuCleanupBuf_) {
    7340            0 :             CHK_RET(DeviceMem::alloc(aicpuCleanupBuf_, sizeof(HcclKfcClearOpResTilingData)));
    7341              :         }
    7342            2 :         if (!aicpuCleanupHostBuf_) {
    7343            0 :             aicpuCleanupHostBuf_.reset(new (std::nothrow) HcclKfcClearOpResTilingData());
    7344            0 :             CHK_SMART_PTR_NULL(aicpuCleanupHostBuf_);
    7345              :         }
    7346            2 :         HcclKfcClearOpResTilingData &payload = *aicpuCleanupHostBuf_;
    7347              : 
    7348              :         // 必须与 aicpu_kfc_def.h 中 KFCResInitTask 布局一致,aicpu 端按此解包
    7349              :         struct KFCResInitTask { u64 context; bool isCustom; };
    7350            2 :         KFCResInitTask initTask = { reinterpret_cast<u64>(aicpuCleanupBuf_.ptr()), false };
    7351            2 :         const u16 timeOut = MAX_VALUE_U16;
    7352            2 :         const size_t groupCopyLen = std::min(identifier_.length() + 1, sizeof(payload.group));
    7353            2 :         size_t totalBatches = 0;
    7354              : 
    7355              :         // 分批 launch:同 buffer 复用,每批最多 MAX_BATCH 个 tag;launch 后 sync 保证 aicpu 完成才覆盖 buffer 下一批
    7356            2 :         auto it = tags.begin();
    7357            5 :         while (it != tags.end()) {
    7358            3 :             payload.magic = HCCL_KFC_CLEAR_OP_RES_MAGIC;
    7359            3 :             CHK_SAFETY_FUNC_RET(memcpy_s(payload.group, sizeof(payload.group), identifier_.c_str(), groupCopyLen));
    7360            3 :             payload.group[sizeof(payload.group) - 1] = '\0';
    7361              : 
    7362            3 :             u32 idx = 0;
    7363        10255 :             while (it != tags.end() && idx < HCCL_KFC_CLEAR_OP_RES_MAX_BATCH) {
    7364        10252 :                 const std::string &t = *it;
    7365        10252 :                 const size_t tagCopyLen = std::min(t.length() + 1, sizeof(payload.tags[idx]));
    7366        10252 :                 CHK_SAFETY_FUNC_RET(memcpy_s(payload.tags[idx], sizeof(payload.tags[idx]), t.c_str(), tagCopyLen));
    7367        10252 :                 payload.tags[idx][sizeof(payload.tags[idx]) - 1] = '\0';
    7368        10252 :                 ++idx;
    7369        10252 :                 ++it;
    7370              :             }
    7371            3 :             payload.tagCount = idx;
    7372              : 
    7373            3 :             CHK_RET(hrtMemSyncCopy(aicpuCleanupBuf_.ptr(), sizeof(payload), reinterpret_cast<void *>(&payload),
    7374              :                 sizeof(payload), HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
    7375              : 
    7376            3 :             HcclResult ret = AicpuAclKernelLaunchV2(opStream_.ptr(), reinterpret_cast<void *>(&initTask),
    7377            3 :                 sizeof(initTask), binHandle_, "RunAicpuKfcClearOpRes", true, timeOut, nullptr, 0, identifier_);
    7378            3 :             if (ret != HCCL_SUCCESS) {
    7379            0 :                 HCCL_ERROR("[AicpuKfcClearOpResLaunch] launch fail, group[%s] batch[%zu] tagCount[%u] ret[%d]",
    7380              :                     identifier_.c_str(), totalBatches, idx, ret);
    7381            0 :                 return ret;
    7382              :             }
    7383            3 :             CHK_RET(hcclStreamSynchronize(opStream_.ptr(), commConfig_.GetConfigExecTimeOut()));
    7384            3 :             ++totalBatches;
    7385              :         }
    7386              : 
    7387            2 :         HCCL_INFO("[AicpuKfcClearOpResLaunch] dispatched aicpu cleanup, group[%s] totalTags[%zu] batches[%zu]",
    7388              :             identifier_.c_str(), tags.size(), totalBatches);
    7389            2 :         return HCCL_SUCCESS;
    7390              :     }
    7391              : 
    7392            8 :     HcclResult HcclCommunicator::AicpuInitOpTilingDataAicpuCache(const OpParam &opParam, const HcclCMDType &opType, struct OpTilingData *opTilingData)
    7393              :     {
    7394            8 :         opTilingData->aicpuCacheEnable = opParam.aicpuCacheEnable;
    7395              :         // 开启aicpu cache, 且原来是图模式建链但强制走单算子模式展开
    7396              :         // 开启aicpu cache,isCapture为true,且是图模式,证明选择了aclgraph零拷贝算法,需要强制刷新cache
    7397           21 :         if (opParam.aicpuCacheEnable != 0 &&
    7398            9 :             GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB &&
    7399            1 :             ((IsForceAicpuOpBaseMode(opParam, opType) && !opParam.isZeroCopy) || opParam.isCapture)) {
    7400              :             // 环境变量传入的aicpuCacheEnable一定 < 10
    7401            1 :             constexpr uint8_t FORCE_OP_BASE_DELTA = 10;
    7402            1 :             CHK_PRT_RET(opParam.aicpuCacheEnable >= FORCE_OP_BASE_DELTA,
    7403              :                 HCCL_ERROR("[HcclCommunicator][AicpuInitOpTilingDataBuf] enforce opbase mode: opParam.aicpuCacheEnable >= %u",
    7404              :                     opParam.aicpuCacheEnable, FORCE_OP_BASE_DELTA),
    7405              :                 HCCL_E_INTERNAL);
    7406              : 
    7407              :             // 1 -> 11: 开启aicpu cache且存在强制单算子模式转换
    7408            1 :             opTilingData->aicpuCacheEnable += FORCE_OP_BASE_DELTA;
    7409            1 :             HCCL_WARNING("[HcclCommunicator][AicpuInitOpTilingDataBuf] enforce opbase mode: opParam.aicpuCacheEnable[%u]"\
    7410              :                 "opTilingData->aicpuCacheEnable[%u]", opParam.aicpuCacheEnable, opTilingData->aicpuCacheEnable);
    7411              :             
    7412              :             // 注意: 开启aicpu cache且存在强制单算子模式转换, 传入device的aicpuCacheEnable一定 > 10
    7413            1 :             CHK_PRT_RET(opTilingData->aicpuCacheEnable <= FORCE_OP_BASE_DELTA,
    7414              :                 HCCL_ERROR("[HcclCommunicator][AicpuInitOpTilingDataBuf] enforce opbase mode: opTilingData->aicpuCacheEnable[%u] <= %u",
    7415              :                     opTilingData->aicpuCacheEnable, FORCE_OP_BASE_DELTA),
    7416              :                 HCCL_E_INTERNAL);
    7417              :         }
    7418              : 
    7419            8 :         return HCCL_SUCCESS;
    7420              :     }
    7421              : 
    7422            1 :     HcclResult HcclCommunicator::AicpuInitOpTilingDataBuf(const OpParam &opParam, const HcclCMDType &opType,
    7423              :                                                           const std::string &kernelName, const AicpuOpTiling opTilingInfo, u64 dynamicDataSize)
    7424              :     {
    7425            1 :         u32 opTilingDataSize = sizeof(struct OpTilingData) + dynamicDataSize;
    7426              : 
    7427            1 :         if (opTilingDataBuf_.ptr() == nullptr) {
    7428            1 :             opTilingDataBuf_ = HostMem::alloc(TILINGDATA_BUF_SIZE);
    7429            1 :             CHK_PRT_RET(opTilingDataBuf_.ptr() == nullptr,
    7430              :                         HCCL_ERROR("[HcclCommunicator][AicpuInitOpTilingDataBuf] Alloc opTilingDataBuf failed!"),
    7431              :                         HCCL_E_INTERNAL);
    7432              :         }
    7433              : 
    7434            1 :         if (opTilingDataBuf_.ptr() != nullptr && opTilingDataSize > opTilingDataBuf_.size()) {
    7435            0 :             opTilingDataBuf_.free();
    7436            0 :             opTilingDataBuf_ = HostMem::alloc(opTilingDataSize);
    7437            0 :             CHK_PRT_RET(opTilingDataBuf_.ptr() == nullptr,
    7438              :                         HCCL_ERROR("[HcclCommunicator][AicpuInitOpTilingDataBuf] in create opTilingDataBuf len[%llu] failed!",
    7439              :                                    opTilingDataSize),
    7440              :                         HCCL_E_INTERNAL);
    7441              :         }
    7442              : 
    7443              :         // 填充固定内容
    7444            1 :         HostMem opTilingDataMem = opTilingDataBuf_.range(0, opTilingDataSize);
    7445            1 :         struct OpTilingData *opTilingData = static_cast<struct OpTilingData *>(opTilingDataMem.ptr());
    7446            1 :         u32 algTypeTranfer = (static_cast<u32>(opTilingInfo.algType.algoLevel2) << (HCCL_LEVEL_ALGO_WIDTH + HCCL_LEVEL_ALGO_WIDTH)) +
    7447            1 :                              (static_cast<u32>(opTilingInfo.algType.algoLevel1) << HCCL_LEVEL_ALGO_WIDTH) +
    7448            1 :                              static_cast<u32>(opTilingInfo.algType.algoLevel0);
    7449            1 :         opTilingData->algType = static_cast<u64>(algTypeTranfer);
    7450            1 :         opTilingData->floatOverflowMode = opTilingInfo.floatOverflowMode;
    7451            1 :         opTilingData->dumpDebug = opTilingInfo.dumpDebug;
    7452            1 :         CHK_RET(AicpuInitOpTilingDataFromOpParam(opParam, opType, opTilingData));
    7453            1 :         opTilingData->length = dynamicDataSize;
    7454            1 :         opTilingData->customDataLength = 0;
    7455            1 :         opTilingData->index = UpdateOpIndex(opParam);
    7456            1 :         opTilingData->debugMode = 0;
    7457            1 :         opTilingData->isZeroCopy = opParam.isZeroCopy;
    7458            1 :         opTilingData->isCapture = opParam.isCapture;
    7459            1 :         opTilingData->orderLaunchMode = GetOrderLaunchMode(opParam.isCapture);
    7460            1 :         opTilingData->isSymmetricMemory = opParam.supportSymmetricMemory;
    7461            1 :         opTilingData->needIncreLink = opParam.needIncreLink;
    7462              :         // 有没有存在对应的Notify
    7463            1 :         CHK_RET(InitAndCheckAicpuOrderNotify(opTilingData->orderLaunchMode));
    7464            1 :         CHK_RET(BuildHierarchicalAlgOption(opTilingData->ahcConfInfo));
    7465            1 :         CHK_RET(AicpuInitOpTilingDataAicpuCache(opParam, opType, opTilingData));
    7466              : 
    7467              :         // 填充动态内容
    7468            1 :         HostMem dynamicDataMem = opTilingDataBuf_.range(sizeof(struct OpTilingData), dynamicDataSize);
    7469            1 :         CHK_PTR_NULL(dynamicDataMem.ptr());
    7470            1 :         if (opType == HcclCMDType::HCCL_CMD_BATCH_SEND_RECV) {
    7471              :             struct OpTilingBatchSendRecvDataDes *batchSendRecvDataPtr =
    7472            1 :                 reinterpret_cast<struct OpTilingBatchSendRecvDataDes *>(dynamicDataMem.ptr());
    7473            1 :             batchSendRecvDataPtr->itemNum = opParam.BatchSendRecvDataDes.itemNum;
    7474            2 :             for (u32 i = 0; i < opParam.BatchSendRecvDataDes.itemNum; i++) {
    7475            1 :                 CHK_PTR_NULL(opParam.BatchSendRecvDataDes.sendRecvItemsPtr + i);
    7476            1 :                 batchSendRecvDataPtr->batchSendRecvItem[i] = *(opParam.BatchSendRecvDataDes.sendRecvItemsPtr + i);
    7477              :             }
    7478              : 
    7479            1 :             u8 *isDirectRemoteRankPtr = reinterpret_cast<u8*>(batchSendRecvDataPtr->batchSendRecvItem + opParam.BatchSendRecvDataDes.itemNum);
    7480            3 :             for (u32 i = 0; i < userRankSize_; i++) {
    7481            2 :                 CHK_PTR_NULL(isDirectRemoteRankPtr + i);
    7482            2 :                 isDirectRemoteRankPtr[i] = *(opParam.BatchSendRecvDataDes.isDirectRemoteRank + i);
    7483              :             }
    7484            0 :         } else if (opType == HcclCMDType::HCCL_CMD_ALLTOALL) {
    7485            0 :             CHK_RET(SetDynamicTilingDataAlltoall(opParam, dynamicDataMem));
    7486            0 :         } else if (opType == HcclCMDType::HCCL_CMD_ALLTOALLV) {
    7487            0 :             CHK_RET(SetDynamicTilingDataAlltoallv(opParam, dynamicDataMem, opTilingInfo.algName));
    7488            0 :         } else if (opType == HcclCMDType::HCCL_CMD_ALLTOALLVC) {
    7489            0 :             CHK_RET(SetDynamicTilingDataAlltoallvc(opParam, dynamicDataMem));
    7490            0 :         } else if (opType == HcclCMDType::HCCL_CMD_ALLGATHER_V || opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V) {
    7491            0 :             CHK_RET(SetDynamicTilingDataV(opParam, dynamicDataMem));
    7492            0 :         } else {
    7493            0 :             struct OpTilingDataDes *opDataDesPtr = reinterpret_cast<struct OpTilingDataDes *>(dynamicDataMem.ptr());
    7494            0 :             opDataDesPtr->count = opParam.DataDes.count;
    7495            0 :             opDataDesPtr->dataType = static_cast<u8>(opParam.DataDes.dataType);
    7496              :         }
    7497              : 
    7498            1 :         HCCL_INFO("[HcclCommunicator][AicpuInitOpTilingDataBuf]algType[%lu]", opTilingData->algType);
    7499            1 :         CHK_SAFETY_FUNC_RET(memcpy_s(opTilingData->algName, sizeof(opTilingData->algName), opTilingInfo.algName.c_str(),
    7500              :                                      opTilingInfo.algName.length() + 1));
    7501            1 :         CHK_SAFETY_FUNC_RET(memcpy_s(opTilingData->newTag, sizeof(opTilingData->newTag),
    7502              :                                      opTilingInfo.newTag.c_str(), opTilingInfo.newTag.length() + 1));
    7503            1 :         CHK_SAFETY_FUNC_RET(memcpy_s(opTilingData->tag, sizeof(opTilingData->tag), opParam.tag.c_str(),
    7504              :                                      opParam.tag.length() + 1));
    7505            1 :         return HCCL_SUCCESS;
    7506            1 :     }
    7507              : 
    7508            0 :     u8 HcclCommunicator::GetOrderLaunchMode (bool isCapture)
    7509              :     {
    7510            0 :         bool isSupportHcomAttachedStream = !(attachedStreams_.empty() || attachedStreams_[0].ptr() == nullptr); // true 表示图模式下成功申请附属从流
    7511            0 :         u8 orderLaunchMode = 0;
    7512            0 :         HcclWorkflowMode mode = GetWorkflowMode();
    7513            0 :         if (isCapture) {
    7514            0 :             orderLaunchMode = static_cast<u8>(AicpuNotifyMode::ACLGRAPH_MODE);
    7515            0 :         } else if (mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
    7516            0 :             orderLaunchMode = static_cast<u8>(AicpuNotifyMode::OPBASE_MODE);
    7517            0 :         } else if (mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB && isSupportHcomAttachedStream) {
    7518            0 :             orderLaunchMode = static_cast<u8>(AicpuNotifyMode::HCOM_MODE);
    7519              :         } else {
    7520            0 :             orderLaunchMode = AICPU_ORDERLAUNCH_INVALID_HCOM_MODE;
    7521              :         }
    7522              : 
    7523            0 :         return orderLaunchMode;
    7524              :     }
    7525              : 
    7526            0 :     HcclResult HcclCommunicator::InitAndCheckAicpuOrderNotify(u8 &orderLaunchMode)
    7527              :     {
    7528            0 :         if (orderLaunchMode == AICPU_ORDERLAUNCH_INVALID_HCOM_MODE) {
    7529            0 :             HCCL_INFO("[HcclCommunicator][InitAndCheckAicpuOrderNotify] orderLaunchMode is invalid in hcom "
    7530              :                       "for there is no attached stream included in this operator!");
    7531            0 :             return HCCL_SUCCESS;
    7532              :         }
    7533              :         u32 idx0;
    7534              :         u32 idx1;
    7535            0 :         if (orderLaunchMode == 0) {
    7536            0 :             idx0 = static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_OPBASE_0);
    7537            0 :             idx1 = static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_OPBASE_1);
    7538            0 :         } else if (orderLaunchMode == 1) {
    7539            0 :             idx0 = static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_ACLGRAPH_0);
    7540            0 :             idx1 = static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_ACLGRAPH_1);
    7541              :         } else {
    7542            0 :             idx0 = static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_HCOM_0);
    7543            0 :             idx1 = static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_HCOM_1);
    7544              :         }
    7545              : 
    7546            0 :         if (localAiCpuOpNotify_[idx0] != nullptr) {
    7547            0 :             HCCL_INFO("[%s], the orderNotify of orderLaunchMode [%u] is available", __func__, orderLaunchMode);
    7548            0 :             return HCCL_SUCCESS;
    7549              :         }
    7550              :         HcclSignalInfo orderSignalInfo0;
    7551            0 :         HcclResult ret = CreateAndGetAiCpuNotify(localAiCpuOpNotify_[idx0],
    7552              :             orderSignalInfo0);
    7553            0 :         CHK_PRT_RET(ret != HCCL_SUCCESS,
    7554              :             HCCL_ERROR("[HcclCommunicator][InitAndCheckAicpuOrderNotify]get aicpu notify [%u] errorCode[%u]", idx0,
    7555              :             HCCL_ERROR_CODE(ret)), ret);
    7556              : 
    7557              :         // 按序下发(aicpu控制流 record host控制流) 使用的notify信息
    7558              :         HcclSignalInfo orderSignalInfo1;
    7559            0 :         ret = CreateAndGetAiCpuNotify(localAiCpuOpNotify_[idx1], orderSignalInfo1);
    7560            0 :         CHK_PRT_RET(ret != HCCL_SUCCESS,
    7561              :             HCCL_ERROR("[HcclCommunicator][InitAndCheckAicpuOrderNotify]get aicpu notify [%u] errorCode[%u]", idx1,
    7562              :             HCCL_ERROR_CODE(ret)), ret);
    7563            0 :         HCCL_INFO("[HcclCommunicator][InitAndCheckAicpuOrderNotify] ORDER INDEX 0: resId[%u], ORDER INDEX 1: resId[%u]",
    7564              :             orderSignalInfo0.resId, orderSignalInfo1.resId);
    7565              : 
    7566            0 :         CHK_RET(hrtMemSyncCopy(
    7567              :             static_cast<char*>(aicpuOrderNotifyAddr_.ptr()) + (sizeof(HcclSignalInfo) * orderLaunchMode),
    7568              :             sizeof(HcclSignalInfo), &orderSignalInfo1, sizeof(HcclSignalInfo),
    7569              :             HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
    7570              : 
    7571            0 :         return HCCL_SUCCESS;
    7572              :     }
    7573              : 
    7574            2 :     HcclResult HcclCommunicator::AicpuKfcTilingDataLaunchIn(const OpParam &opParam, const DeviceMem &deviceContext,
    7575              :                                                             const std::string &kernelName, const AicpuOpTiling opTilingInfo, u64 opTilingDataSize, bool isCustom)
    7576              :     {
    7577            2 :         HostMem opTilingDataMem = opTilingDataBuf_.range(0, opTilingDataSize);
    7578            2 :         CHK_RET(SetNormalMode(dispatcher_));
    7579            0 :         Stream &mainStream = const_cast<Stream &>(opParam.stream);
    7580            0 :         CHK_RET(LocalNotify::Post(mainStream, dispatcher_,
    7581              :             localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_0)], INVALID_VALUE_STAGE));
    7582              : 
    7583            0 :         Stream kfcOpStream;
    7584            0 :         HcclWorkflowMode mode = GetWorkflowMode();
    7585            0 :         bool isSupportHcomAttachedStream = !(attachedStreams_.empty() || attachedStreams_[0].ptr() == nullptr); // true 表示图模式下成功申请附属从流
    7586            0 :         if (opParam.isCapture || mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
    7587            0 :             kfcOpStream = opStream_;
    7588              :         } else {
    7589              :             // 如果是图模式,则尝试从附属从流中获取一下stream,如果能拿到则使用,否则退化
    7590            0 :             if (isSupportHcomAttachedStream) {
    7591            0 :                 HCCL_INFO("[HcclCommunicator][AicpuKfcTilingDataLaunchIn] attachedStreams_ is valid in graph mode");
    7592            0 :                 kfcOpStream = attachedStreams_[0];
    7593              :             } else {
    7594            0 :                 HCCL_INFO("[HcclCommunicator][AicpuKfcTilingDataLaunchIn] attachedStreams_ is invalid in graph mode");
    7595            0 :                 kfcOpStream = opParam.stream;
    7596              :             }
    7597              :         }
    7598            0 :         uint64_t beginTime = hrtMsprofSysCycleTime();
    7599            0 :         std::string profName = GetCMDTypeEnumStr(opParam.opType);
    7600            0 :         if (profName == "Invalid HcclCMDType" || profName == "invalid") {
    7601            0 :             profName = "HcclOpAicpuKernel";
    7602              :         } else {
    7603            0 :             profName += "AicpuKernel";
    7604              :         }
    7605            0 :         s32 streamId = kfcOpStream.id();
    7606            0 :         auto getAicpuTaskExceptionCallBack = [this]() {
    7607            0 :             return this->GetAicpuTaskException();
    7608            0 :         };
    7609            0 :         RegisterGetAicpuTaskExceptionCallBack(streamId, deviceLogicId_, getAicpuTaskExceptionCallBack);
    7610            0 :         aicpuStreamIds_.insert(streamId);
    7611            0 :         if (streamId != opParam.stream.id()) {
    7612            0 :             RegisterGetAicpuTaskExceptionCallBack(opParam.stream.id(), deviceLogicId_, getAicpuTaskExceptionCallBack);
    7613            0 :             aicpuStreamIds_.insert(opParam.stream.id());
    7614              :         }
    7615              : 
    7616            0 :         HCCL_INFO("%s profName[%s] tag[%s] kfcOpStreamId[%d] mainStreamId[%u] kfcStreamId[%d] isCapture[%d] mode[%d] ",
    7617              :             __func__, profName.c_str(), opParam.tag.c_str(), streamId, opParam.stream.id(), opStream_.id(),
    7618              :             opParam.isCapture, mode);
    7619              : 
    7620            0 :         if (opParam.isCapture) { // 非主流下发时,acl graph场景,capture从流
    7621            0 :             u64 modelId = UINT64_MAX;
    7622            0 :             rtModel_t rtModel = nullptr;
    7623            0 :             bool isCapture = false;
    7624            0 :             CHK_RET(GetStreamCaptureInfo(opParam.stream.ptr(), rtModel, isCapture));
    7625            0 :             CHK_PTR_NULL(rtModel);
    7626            0 :             CHK_RET(AddStreamToModel(kfcOpStream.ptr(), rtModel));
    7627              : 
    7628            0 :             CHK_RET(GetModelId(rtModel, modelId));
    7629            0 :             HCCL_INFO("[HcclCommunicator][%s]tag[%s], add stream[%d] to modelId[%llu] success.",
    7630              :                 __func__, opParam.tag.c_str(), streamId, modelId);
    7631              :         }
    7632              : 
    7633            0 :         u32 timeOut = (opResPara_.config.notifyWaitTime == 0) ? opResPara_.config.notifyWaitTime :
    7634            0 :                                                                (opResPara_.config.notifyWaitTime + AICPU_H2D_TIMEOUT_INC);
    7635            0 :         OrderLaunch& orderLaunch = OrderLaunch::GetInstance(deviceLogicId_);
    7636            0 :         std::shared_ptr<LocalNotify> notify0;
    7637            0 :         std::shared_ptr<LocalNotify> notify1;
    7638            0 :         if (opParam.isCapture) {
    7639            0 :             notify0 = localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_ACLGRAPH_0)];
    7640            0 :             notify1 = localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_ACLGRAPH_1)];
    7641            0 :             HcclRtEvent event0 = localAicpuOpEvent_[static_cast<u32>(AicpuLocalEventIdx::ORDER_INDEX_ACLGRAPH_EVENT_0)];
    7642            0 :             CHK_RET(orderLaunch.AclgraphLaunchInOrderToOrderStream(
    7643              :                 identifier_, kfcOpStream, notify0, notify1, timeOut, event0));
    7644            0 :         } else if (mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
    7645            0 :             notify0 = localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_OPBASE_0)];
    7646            0 :             notify1 = localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_OPBASE_1)];
    7647            0 :             CHK_RET(orderLaunch.OpbaseLaunchInOrder(identifier_, kfcOpStream, notify0, notify1, timeOut));
    7648            0 :         } else if (mode == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OPS_KERNEL_INFO_LIB && isSupportHcomAttachedStream) {
    7649            0 :             notify0 = localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_HCOM_0)];
    7650            0 :             notify1 = localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::ORDER_INDEX_HCOM_1)];
    7651            0 :             CHK_RET(orderLaunch.HcomLaunchInOrder(identifier_, kfcOpStream, graphId_, notify0,
    7652              :                 notify1, timeOut));
    7653              :         }
    7654            0 :         CHK_RET(KernelLaunchChooseAicpuOrCustom(opParam.inputPtr, opParam.outputPtr, kfcOpStream.ptr(),
    7655              :                                                 reinterpret_cast<u64>(deviceContext.ptr()), opTilingDataMem.ptr(), opTilingDataSize,
    7656              :                                                 kernelName, mode, opParam.tag, isCustom));
    7657            0 :         if (opParam.isCapture) {
    7658            0 :             HcclRtEvent event1 = localAicpuOpEvent_[static_cast<u32>(AicpuLocalEventIdx::ORDER_INDEX_ACLGRAPH_EVENT_1)];
    7659            0 :             CHK_RET(orderLaunch.AclgraphLaunchInOrderToKernelStream(identifier_, kfcOpStream, event1));
    7660              :         }
    7661              : 
    7662            0 :         uint64_t endTime = hrtMsprofSysCycleTime();
    7663            0 :         s32 threadId = SalGetTid();
    7664            0 :         CHK_RET(ProfilingManagerPub::CallMsprofReportNodeInfo(beginTime, endTime, profName, threadId));
    7665            0 :         CHK_RET(LocalNotify::Wait(mainStream, dispatcher_,
    7666              :             localAiCpuOpNotify_[static_cast<u32>(AicpuLocalNotifyIdx::HOST_TO_AICPU_1)], INVALID_VALUE_STAGE, timeOut));
    7667            0 :         return HCCL_SUCCESS;
    7668            2 :     }
    7669              : 
    7670              : 
    7671            0 :     HcclResult HcclCommunicator::SetAttachedStream(u32 graphId, const std::vector<rtStream_t> &streams)
    7672              :     {
    7673            0 :         constexpr u32 GRAPH_ATTACHED_STREAM_INDEX = 0; // 图粒度的附属从流
    7674            0 :         constexpr u32 GROUP_ATTACHED_STREAM_INDEX = 1; // 通信域粒度的附属从流
    7675              : 
    7676              :         // 在图模式下,通信使用的附属从流可能不同,所以这里直接刷新所有
    7677            0 :         attachedStreams_.clear();
    7678              : 
    7679            0 :         bool isValid = !streams.empty() && (streams.size() > GROUP_ATTACHED_STREAM_INDEX) &&
    7680            0 :             streams[GRAPH_ATTACHED_STREAM_INDEX] != nullptr && streams[GROUP_ATTACHED_STREAM_INDEX] != nullptr;
    7681            0 :         if (!isValid) {
    7682            0 :             HCCL_ERROR("%s Invalid stream configuration, streams vector is null or invalid", __func__);
    7683            0 :             return HCCL_E_NOT_FOUND;
    7684              :         }
    7685              : 
    7686              :         // 向GE申请流的时候,图粒度的流排在第一个,所以在streams列表中,第一条流是图粒度的附属从流
    7687            0 :         s32 graphAttachedStreamId = 0;
    7688            0 :         OrderLaunch& orderLaunch = OrderLaunch::GetInstance(deviceLogicId_);
    7689            0 :         auto& graphStream = streams[GRAPH_ATTACHED_STREAM_INDEX];
    7690            0 :         CHK_RET(hrtGetStreamId(graphStream, graphAttachedStreamId));
    7691            0 :         orderLaunch.SetHcomStream(graphId, Stream(graphStream, false));
    7692            0 :         graphId_ = graphId;
    7693              : 
    7694              :         // 设置通信域粒度流
    7695            0 :         auto& groupStream = streams[GROUP_ATTACHED_STREAM_INDEX];
    7696            0 :         attachedStreams_.emplace_back(Stream(groupStream, false));
    7697              : 
    7698            0 :         HCCL_INFO("%s Streams configured graph[%u], graphAttachedStreamId[%d], group[%u],"
    7699              :                 "groupStreamId[%u], graphId[%u], groupId[%s]", __func__,
    7700              :                 GRAPH_ATTACHED_STREAM_INDEX, graphAttachedStreamId, GROUP_ATTACHED_STREAM_INDEX,
    7701              :                 attachedStreams_.back().id(), graphId, identifier_.c_str());
    7702              : 
    7703            0 :         return HCCL_SUCCESS;
    7704              :     }
    7705              : 
    7706            4 :     HcclResult HcclCommunicator::AicpuKfcTilingDataLaunchExt(const OpParam &opParam, const HcclCMDType &opType,
    7707              :                                                              const DeviceMem &deviceContext, const std::string &kernelName, const AicpuOpTiling opTilingInfo,
    7708              :                                                              bool isCustom)
    7709              :     {
    7710            4 :         const u64 dataCount = opParam.GetDataCount(userRank_);
    7711            4 :         const HcclDataType dataType = opParam.GetDataType();
    7712            4 :         HCCL_DEBUG("AicpuKfcTilingDataLaunchExt count %llu dataType %s op %s opType %u retryEnable_ %d, "
    7713              :                    "inPlaceSupportRetryStatus_ %d",
    7714              :                    dataCount, GetDataTypeEnumStr(dataType).c_str(),
    7715              :                    GetReduceOpEnumStr(opParam.reduceType).c_str(), opType, retryEnable_, inPlaceSupportRetryStatus_);
    7716              : 
    7717            4 :         bool postSyncEnable = false;
    7718            4 :         u32 severNum4PostSync = 4;
    7719            4 :         bool needPostSync = (superPodNum_ > 1 || serverNum_ >= severNum4PostSync) && postSyncEnable; // reduce/reduce scatter算子是否需要PostSync
    7720            4 :         if (opType == HcclCMDType::HCCL_CMD_ALLREDUCE &&
    7721            1 :             retryEnable_ && (inPlaceSupportRetryStatus_ == InplaceSupportRetryStatus::USER_LARGER_THAN_CCL) &&
    7722            1 :             (!opParam.isZeroCopy)) {
    7723            1 :             u32 itemNum = 2;
    7724            3 :             for (u32 i = 0; i < itemNum; i++) {
    7725            2 :                 if (i == 0) {
    7726            1 :                     isInplacePreSync_ = true;
    7727              :                 } else {
    7728            1 :                     isInplacePreSync_ = false;
    7729              :                 }
    7730            2 :                 HCCL_DEBUG("[AicpuKfcTilingDataLaunchExt][PreSync]The op with isInplacePreSync_[%d].",
    7731              :                            isInplacePreSync_);
    7732            2 :                 u64 dynamicDataSize = CalcOpTilingDynamicDataSize(opParam, opType, GetRankSize(), opTilingInfo.algName);
    7733            2 :                 CHK_RET(AicpuInitOpTilingDataBuf(opParam, opType, kernelName, opTilingInfo, dynamicDataSize));
    7734            2 :                 CHK_RET(AicpuKfcTilingDataLaunchIn(opParam, deviceContext, kernelName, opTilingInfo,
    7735              :                                                    sizeof(struct OpTilingData) + dynamicDataSize, isCustom));
    7736            2 :                 isInplacePreSync_ = false;
    7737              :             }
    7738            4 :         } else if (opType == HcclCMDType::HCCL_CMD_REDUCE && retryEnable_ && needPostSync && (!opParam.isZeroCopy)) {
    7739            0 :             isPostSync_ = true;
    7740            0 :             HCCL_DEBUG("[AicpuKfcTilingDataLaunchExt][PreSync]The op with isPostSync_[%d].",
    7741              :                        isPostSync_);
    7742            0 :             u64 dynamicDataSize = CalcOpTilingDynamicDataSize(opParam, opType, GetRankSize(), opTilingInfo.algName);
    7743            0 :             CHK_RET(AicpuInitOpTilingDataBuf(opParam, opType, kernelName, opTilingInfo, dynamicDataSize));
    7744            0 :             CHK_RET(AicpuKfcTilingDataLaunchIn(opParam, deviceContext, kernelName, opTilingInfo,
    7745              :                                                sizeof(struct OpTilingData) + dynamicDataSize, isCustom));
    7746            0 :             isPostSync_ = false;
    7747            3 :         } else if (retryEnable_ && opType == HcclCMDType::HCCL_CMD_REDUCE_SCATTER && (!opParam.isZeroCopy)) {
    7748            1 :             if (inPlaceSupportRetryStatus_ == InplaceSupportRetryStatus::USER_LARGER_THAN_CCL) {
    7749            1 :                 isInplacePreSync_ = true;
    7750            1 :                 HCCL_DEBUG("[AicpuKfcTilingDataLaunchExt][PreSync]The op with isInplacePreSync_[%d].",
    7751              :                            isInplacePreSync_);
    7752            1 :                 u64 dynamicDataSize = CalcOpTilingDynamicDataSize(opParam, opType, GetRankSize(), opTilingInfo.algName);
    7753            1 :                 CHK_RET(AicpuInitOpTilingDataBuf(opParam, opType, kernelName, opTilingInfo, dynamicDataSize));
    7754            1 :                 CHK_RET(AicpuKfcTilingDataLaunchIn(opParam, deviceContext, kernelName, opTilingInfo,
    7755              :                                                    sizeof(struct OpTilingData) + dynamicDataSize, isCustom));
    7756            1 :                 isInplacePreSync_ = false;
    7757              :             }
    7758            1 :             isInplacePreSync_ = false;
    7759            1 :             if (needPostSync) {
    7760            0 :                 isPostSync_ = true;
    7761              :             }
    7762            1 :             HCCL_DEBUG("[AicpuKfcTilingDataLaunchExt][PreSync]The op with "
    7763              :                        "isInplacePreSync_[%d], isPostSync_[%d].",
    7764              :                        isInplacePreSync_, isPostSync_);
    7765            1 :             u64 dynamicDataSize = CalcOpTilingDynamicDataSize(opParam, opType, GetRankSize(), opTilingInfo.algName);
    7766            1 :             CHK_RET(AicpuInitOpTilingDataBuf(opParam, opType, kernelName, opTilingInfo, dynamicDataSize));
    7767            1 :             CHK_RET(AicpuKfcTilingDataLaunchIn(opParam, deviceContext, kernelName, opTilingInfo,
    7768              :                                                sizeof(struct OpTilingData) + dynamicDataSize, isCustom));
    7769            1 :             isPostSync_ = false;
    7770            3 :         } else if (retryEnable_ &&
    7771            2 :                  (opType == HcclCMDType::HCCL_CMD_ALLTOALL ||
    7772            2 :                   opType == HcclCMDType::HCCL_CMD_ALLTOALLV ||
    7773            2 :                   opType == HcclCMDType::HCCL_CMD_ALLTOALLVC) && (!opParam.isZeroCopy)) {
    7774            1 :             isPostSync_ = postSyncEnable;
    7775            1 :             HCCL_DEBUG("[AicpuKfcTilingDataLaunchExt][PreSync]The op with "
    7776              :                        "isInplacePreSync_[%d], isPostSync_[%d].",
    7777              :                        isInplacePreSync_, isPostSync_);
    7778            1 :             u64 dynamicDataSize = CalcOpTilingDynamicDataSize(opParam, opType, GetRankSize(), opTilingInfo.algName);
    7779            1 :             CHK_RET(AicpuInitOpTilingDataBuf(opParam, opType, kernelName, opTilingInfo, dynamicDataSize));
    7780            1 :             CHK_RET(AicpuKfcTilingDataLaunchIn(opParam, deviceContext, kernelName, opTilingInfo,
    7781              :                                                sizeof(struct OpTilingData) + dynamicDataSize, isCustom));
    7782            1 :             isPostSync_ = false;
    7783            1 :         } else {
    7784            1 :             u64 dynamicDataSize = CalcOpTilingDynamicDataSize(opParam, opType, GetRankSize(), opTilingInfo.algName);
    7785            1 :             HCCL_DEBUG("[AicpuKfcTilingDataLaunchExt]dynamicDataSize[%u]", dynamicDataSize);
    7786            1 :             CHK_RET(AicpuInitOpTilingDataBuf(opParam, opType, kernelName, opTilingInfo, dynamicDataSize));
    7787            1 :             CHK_RET(AicpuKfcTilingDataLaunchIn(opParam, deviceContext, kernelName, opTilingInfo,
    7788              :                                                sizeof(struct OpTilingData) + dynamicDataSize, isCustom));
    7789              :         }
    7790              : 
    7791            4 :         return HCCL_SUCCESS;
    7792              :     }
    7793              : 
    7794            0 :     HcclResult HcclCommunicator::AicpuUnfoldKernelLaunch(void *inputPtr, void *outputPtr, const rtStream_t stm, u64 addr,
    7795              :                                                          void *tilingDataPtr, u32 tilingDataSize, const std::string &kernelName, HcclWorkflowMode mode, const std::string &tag)
    7796              :     {
    7797              :         struct ApiParamDef
    7798              :         {
    7799              :             uint64_t x1; // 算子sendbuffer地址
    7800              :             uint64_t y = 0;
    7801              :             uint64_t gatherOut;     // 算子recvbuffer地址
    7802              :             uint64_t context;       // 通信资源准备的地址
    7803              :             uint64_t workspace;     // 消息区地址
    7804              :         };
    7805              : 
    7806            0 :         struct ApiParamDef apiParam;
    7807            0 :         apiParam.x1 = reinterpret_cast<uint64_t>(inputPtr);
    7808            0 :         apiParam.gatherOut = reinterpret_cast<uint64_t>(outputPtr);
    7809            0 :         apiParam.context = addr;
    7810            0 :         apiParam.workspace = reinterpret_cast<uint64_t>(workSpace_.ptr());
    7811            0 :         u16 timeOut = 0;
    7812            0 :         if (opResPara_.config.notifyWaitTime == 0) {
    7813            0 :             timeOut = opResPara_.config.notifyWaitTime;
    7814            0 :         } else if (opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC >=  MAX_VALUE_U16) {
    7815            0 :             timeOut = MAX_VALUE_U16;
    7816              :         } else {
    7817            0 :             timeOut = opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC;
    7818              :         }
    7819            0 :         CHK_PRT(AicpuAclKernelLaunch(stm, reinterpret_cast<void *>(&apiParam), sizeof(apiParam),
    7820              :             binHandle_, kernelName, false, timeOut, tilingDataPtr, tilingDataSize));
    7821            0 :         HCCL_INFO("[HcclCommunicator][AicpuUnfoldKernelLaunch] exec succ.");
    7822            0 :         return HCCL_SUCCESS;
    7823              :     }
    7824              : 
    7825            0 :     HcclResult HcclCommunicator::AicpuUnfoldKernelLaunchV2(void *inputPtr, void *outputPtr, const rtStream_t stm,
    7826              :         u64 addr, void *tilingDataPtr, u32 tilingDataSize, const std::string &kernelName, HcclWorkflowMode mode,
    7827              :         const std::string &tag, bool isCustom)
    7828              :     {
    7829            0 :         u64 context = addr;
    7830            0 :         HCCL_INFO("[HcclCommunicator]context[%p] tilingDataPtr[%p] tilingData[%p]", context,
    7831              :                   tilingDataPtr, tilingDataSize);
    7832              : 
    7833            0 :         aclrtBinHandle binHandle = isCustom ? binCustomHandle_ : binHandle_;
    7834            0 :         if (binHandle == nullptr) {
    7835            0 :             HCCL_ERROR("[AicpuUnfoldKernelLaunchV2]isCustom[%d] binHandle is nullptr, please check.", isCustom);
    7836            0 :             return HCCL_E_NOT_SUPPORT;
    7837              :         }
    7838            0 :         u16 timeOut = 0;
    7839            0 :         if (opResPara_.config.notifyWaitTime == 0) {
    7840            0 :             timeOut = opResPara_.config.notifyWaitTime;
    7841            0 :         } else if (opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC >=  MAX_VALUE_U16) {
    7842            0 :             timeOut = MAX_VALUE_U16;
    7843              :         } else {
    7844            0 :             timeOut = opResPara_.config.notifyWaitTime + AICPU_KERNEL_TIMEOUT_INC;
    7845              :         }
    7846            0 :         HcclResult ret = AicpuAclKernelLaunchV2(stm, reinterpret_cast<void *>(&context), sizeof(context),
    7847            0 :             binHandle, kernelName, false, timeOut, tilingDataPtr, tilingDataSize, identifier_);
    7848            0 :         CHK_PRT_RET(ret != HCCL_SUCCESS,
    7849              :             HCCL_ERROR("[HcclCommunicator][AicpuUnfoldKernelLaunchV2]isCustom[%d] binHandle[%p]",
    7850              :                 isCustom, binHandle), ret);
    7851            0 :         HCCL_INFO("[HcclCommunicator][AicpuUnfoldKernelLaunchV2] exec succ, isCustom[%d].", isCustom);
    7852            0 :         return HCCL_SUCCESS;
    7853              :     }
    7854              : 
    7855          487 :     HcclResult HcclCommunicator::InitCombinOpara()
    7856              :     {
    7857          487 :         if (combinOparaMem_ == nullptr) {
    7858          488 :             CHK_RET(AllocAndClearHostMem(sizeof(HcclCombinOpParam), combinOparaMem_));
    7859              :         }
    7860          487 :         CHK_PTR_NULL(combinOparaMem_);
    7861          487 :         HcclCombinOpParam *combinOparaPtr = reinterpret_cast<HcclCombinOpParam*>(combinOparaMem_->ptr());
    7862          488 :         CHK_PTR_NULL(combinOparaPtr);
    7863              : 
    7864          488 :         if (aiRMAInfoMem_ == nullptr) {
    7865          485 :             CHK_RET(AllocAndClearHostMem(sizeof(HcclAiRMAInfo), aiRMAInfoMem_));
    7866              :         }
    7867          489 :         if (rmaInfoMem_ == nullptr) {
    7868          485 :             CHK_RET(AllocAndClearHostMem(sizeof(HcclRMAInfo), rmaInfoMem_));
    7869              :         }
    7870          487 :         CHK_PTR_NULL(aiRMAInfoMem_);
    7871          487 :         CHK_PTR_NULL(aiRMAInfoMem_->ptr());
    7872          487 :         CHK_PTR_NULL(rmaInfoMem_);
    7873          487 :         CHK_PTR_NULL(rmaInfoMem_->ptr());
    7874              : 
    7875          487 :         CHK_SAFETY_FUNC_RET(memset_s(combinOparaPtr, sizeof(HcclCombinOpParam), 0, sizeof(HcclCombinOpParam)));
    7876              : 
    7877          488 :         combinOparaPtr->rankId = INVALID_UINT;
    7878          488 :         combinOparaPtr->signalInfo.aicpuNotify.rankId = INVALID_UINT;
    7879              : 
    7880        31682 :         for (u32 i = 0; i < sizeof(combinOparaPtr->signalInfo.noIpcNotifys) / sizeof(combinOparaPtr->signalInfo.noIpcNotifys[0]);
    7881              :              i++) {
    7882        31194 :             combinOparaPtr->signalInfo.noIpcNotifys[i].rankId = INVALID_UINT;
    7883              :         }
    7884              : 
    7885        62800 :         for (u32 i = 0; i < sizeof(combinOparaPtr->signalInfo.ipcNotifys) / sizeof(combinOparaPtr->signalInfo.ipcNotifys[0]);
    7886              :              i++) {
    7887        62312 :             combinOparaPtr->signalInfo.ipcNotifys[i].rankId = INVALID_UINT;
    7888              :         }
    7889              : 
    7890        16102 :         for (u32 i = 0; i < sizeof(combinOparaPtr->signalInfo.noIpcEvents) / sizeof(combinOparaPtr->signalInfo.noIpcEvents[0]);
    7891              :              i++) {
    7892        15614 :             combinOparaPtr->signalInfo.noIpcEvents[i].rankId = INVALID_UINT;
    7893              :         }
    7894          488 :         return HCCL_SUCCESS;
    7895              :     }
    7896              : 
    7897            7 :     bool HcclCommunicator::GetCommResource(const std::string &tag, void **commContext)
    7898              :     {
    7899            7 :         if (LIKELY(IsExistCommRes(tag))) {
    7900            0 :             *commContext = commContext_.ptr();
    7901            0 :             return true;
    7902              :         }
    7903            7 :         return false;
    7904              :     }
    7905              : 
    7906            7 :     bool HcclCommunicator::GetCommResource(void *&commContext)
    7907              :     {
    7908            7 :         commContext = opResDevicePara_.ptr();
    7909            7 :         return true;
    7910              :     }
    7911              : 
    7912            0 :     HcclResult HcclCommunicator::GetAicpuOpStreamNotify(HcclRtStream *opStream, u8 aicpuNotifyNum, void **aicpuNotify)
    7913              :     {
    7914            0 :         CHK_RET(GetAicpuOpStreamAndNotify(opStream, aicpuNotifyNum, aicpuNotify));
    7915            0 :         HCCL_INFO("[HcclCommunicator][GetAicpuOpStreamNotify]opStream %p aicpuNotify %p.", *opStream, *aicpuNotify);
    7916            0 :         return HCCL_SUCCESS;
    7917              :     }
    7918              : 
    7919            0 :     HcclResult HcclCommunicator::GetAicpuOpStreamAndNotify(HcclRtStream *opStream, u8 aicpuNotifyNum, void **aicpuNotify)
    7920              :     {
    7921            0 :         *opStream = opStream_.ptr();
    7922            0 :         if (localAiCpuNotifyRes_.size() < aicpuNotifyNum) {
    7923            0 :             for (u16 i = localAiCpuNotifyRes_.size(); i < aicpuNotifyNum; i++) {
    7924            0 :                 std::shared_ptr<LocalNotify> localNotify = {nullptr};
    7925              :                 HcclSignalInfo aicpuNotify;
    7926            0 :                 CHK_RET(CreateAndGetAiCpuNotify(localNotify, aicpuNotify));
    7927            0 :                 localAiCpuNotifyRes_.push_back(localNotify);
    7928            0 :             }
    7929              :         }
    7930              : 
    7931            0 :         for (u16 i = 0; i < aicpuNotifyNum; i++) {
    7932            0 :             *(aicpuNotify + i) = localAiCpuNotifyRes_[i]->ptr();
    7933              :         }
    7934            0 :         return HCCL_SUCCESS;
    7935              :     }
    7936              : 
    7937            0 :     HcclResult HcclCommunicator::SetAicpuNotifyInvalid()
    7938              :     {
    7939            0 :         CHK_PTR_NULL(combinOparaMem_);
    7940            0 :         HcclCombinOpParam *combinOparaPtr = reinterpret_cast<HcclCombinOpParam*>(combinOparaMem_->ptr());
    7941            0 :         CHK_PTR_NULL(combinOparaPtr);
    7942            0 :         combinOparaPtr->signalInfo.aicpuNotify.resId = INVALID_U64;
    7943            0 :         return HCCL_SUCCESS;
    7944              :     }
    7945              : 
    7946            1 :     HcclResult HcclCommunicator::ReplaceCommInfoByTag(const std::string &tag, std::unique_ptr<CommInfo> &commInfo)
    7947              :     {
    7948            1 :         std::unique_lock<std::mutex> replLock(commLock_);
    7949            1 :         tagCommInfo_.erase(tag);
    7950            1 :         tagCommInfo_.insert(std::pair<std::string, CommInfo>(tag, std::move(*commInfo)));
    7951            1 :         return HCCL_SUCCESS;
    7952            1 :     }
    7953              : 
    7954            0 :     HcclResult HcclCommunicator::CreateMutiStreamResFor310P(const std::string &tag, level1StreamInfo_t &streamInfo)
    7955              :     {
    7956            0 :         u32 rankSize = GetRankSize();
    7957              :         s32 pid;
    7958            0 :         if (SalGetBareTgid(&pid) != HCCL_SUCCESS) {
    7959            0 :             HCCL_DEBUG("get pid fail");
    7960              :         }
    7961            0 :         HCCL_INFO("[HcclCommunicator][CreateMutiStreamRes]tag[%s] ranksize[%u] comminfo ranksize[%u] "
    7962              :                   "auxRingCommStreamsDev_ size[%u] ringDeviceSignalAux size[%u] ringDeviceSignal size[%u] "
    7963              :                   "ringDeviceStreams size[%u]",
    7964              :                   tag.c_str(), rankSize, tagCommInfo_[tag].commIntraServer->RankSize(),
    7965              :                   auxRingCommStreamsDev_.size(), streamInfo.ringDeviceSignalAux.size(),
    7966              :                   streamInfo.ringDeviceSignal.size(), streamInfo.ringDeviceStreams.size());
    7967            0 :         if (auxRingCommStreamsDev_.empty() || auxRingCommStreamsDev_.size() < rankSize) {
    7968            0 :             auxRingCommStreamsDev_.resize(rankSize);
    7969            0 :             u32 resNum = rankSize - 1;
    7970            0 :             streamInfo.ringDeviceSignalAux.resize(resNum);
    7971            0 :             streamInfo.ringDeviceSignal.resize(resNum);
    7972            0 :             for (u32 ringIndex = 0; ringIndex < rankSize; ringIndex++) {
    7973            0 :                 auxRingCommStreamsDev_[ringIndex] = Stream(StreamType::STREAM_TYPE_DEVICE);
    7974              :                 // 给device侧申请的流不需要setmode,否则rts会捕获流成员Flags为1024的异常
    7975              :             }
    7976            0 :             for (auto &signal : streamInfo.ringDeviceSignal) {
    7977            0 :                 signal = nullptr;
    7978              :             }
    7979            0 :             for (auto &signal : streamInfo.ringDeviceSignalAux) {
    7980            0 :                 signal = nullptr;
    7981              :             }
    7982              : 
    7983            0 :             u32 notifyNum = resNum * 2; // 2:Signal + SignalAux
    7984            0 :             std::vector<std::shared_ptr<LocalNotify>> notifys(notifyNum, nullptr);
    7985            0 :             CHK_RET(queueNotifyManager_->Alloc(tag, notifyNum, notifys, NotifyLoadType::DEVICE_NOTIFY));
    7986            0 :             for (u32 i = 0; i < resNum; i++) {
    7987            0 :                 streamInfo.ringDeviceSignal[i] = notifys[2 * i];
    7988            0 :                 streamInfo.ringDeviceSignalAux[i] = notifys[2 * i + 1];
    7989              :             }
    7990            0 :         }
    7991              : 
    7992            0 :         if (streamInfo.ringDeviceStreams.empty() || streamInfo.ringDeviceStreams.size() < rankSize) {
    7993            0 :             streamInfo.ringDeviceStreams.resize(rankSize);
    7994            0 :             for (u32 ringIndex = 0; ringIndex < rankSize; ringIndex++) {
    7995            0 :                 streamInfo.ringDeviceStreams[ringIndex] = auxRingCommStreamsDev_[ringIndex];
    7996            0 :                 CHK_SMART_PTR_NULL(streamInfo.ringDeviceStreams[ringIndex]);
    7997              :             }
    7998              :         }
    7999            0 :         return HCCL_SUCCESS;
    8000              :     }
    8001              : 
    8002            1 :     HcclResult HcclCommunicator::CreateCommAndStreamRes(const std::string &tag, Stream &stream)
    8003              :     {
    8004            1 :         CHK_SMART_PTR_NULL(implAlg_);
    8005            1 :         void *commInputPtr = nullptr;
    8006            1 :         void *commOutputPtr = nullptr;
    8007              :         u64 commInputSize, commOutputSize;
    8008              : 
    8009            1 :         HcclResult ret = CreateCommCCLbuffer();
    8010            1 :         CHK_PRT_RET(ret != HCCL_SUCCESS,
    8011              :                     HCCL_ERROR("[HcclImplBase][CreateCommAndStreamRes]errNo[0x%016llx],create cclbuff failed",
    8012              :                                HCCL_ERROR_CODE(ret)),
    8013              :                     ret);
    8014              : 
    8015            1 :         if (isA2MC2MultiServer_) {
    8016              :             // 该场景下ccl buffer有一块区域在上层会被用作flag区,因此需要先清理一下
    8017            0 :             CHK_RET(cclBufferManager_.CleanCCLbuffer());
    8018              :         }
    8019              : 
    8020            1 :         CHK_RET(cclBufferManager_.GetInCCLbuffer(commInputPtr, commInputSize));
    8021            1 :         CHK_RET(cclBufferManager_.GetOutCCLbuffer(commOutputPtr, commOutputSize));
    8022            1 :         DeviceMem expMem = cclBufferManager_.GetCommExpBuffer();
    8023            1 :         DeviceMem inputMem = DeviceMem::create(commInputPtr, commInputSize);
    8024            1 :         DeviceMem outputMem = DeviceMem::create(commOutputPtr, commOutputSize);
    8025            1 :         AlgType algType;
    8026            1 :         AlgType algTypeTmp;
    8027              : 
    8028            1 :         CHK_RET(GetAlgType(algType, HcclCMDType::HCCL_CMD_ALL));
    8029            1 :         algTypeTmp = algType;
    8030              : 
    8031            1 :         CHK_RET(notifyPool_->RegisterOp(tag));
    8032              : 
    8033              :         // 根据tag创建comm和流资源
    8034            1 :         if (!(IsExistCommRes(tag))) {
    8035            1 :             std::unique_ptr<CommInfo> commInfo = nullptr;
    8036            1 :             std::vector<u32> &ranksPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    8037            1 :             std::vector<u32> &vnicRanksPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    8038            1 :             implAlg_->SetHDCModeInfo(rankDevicePhyIdNicInfoMap_, ranksPorts, vnicRanksPorts, isSetHDCModeInfo_,
    8039            1 :                 isUseRankPort_);
    8040            1 :             HcclResult ret = implAlg_->CreateComm(tag, inputMem, outputMem, algType, commInfo,
    8041              :                                                   INVALID_VALUE_RANKID, false, true);
    8042              : 
    8043            1 :             CHK_PRT_RET(ret != HCCL_SUCCESS,
    8044              :                         HCCL_ERROR(
    8045              :                             "[HcclCommunicator][CreateCommAndStreamRes]errNo[0x%016llx]tag[%s],comm resource create comm failed",
    8046              :                             HCCL_ERROR_CODE(ret),
    8047              :                             tag.c_str()),
    8048              :                         ret);
    8049              : 
    8050            1 :             CHK_RET(ReplaceCommInfoByTag(tag, commInfo));
    8051            1 :             if (isA2MC2MultiServer_ && isA2MC2IntraHie_) {
    8052            0 :                 std::string hieSuffix = "_HIE";
    8053            0 :                 size_t pos = tag.find(hieSuffix);
    8054            0 :                 std::string oldtag = tag;
    8055            0 :                 oldtag.erase(pos, hieSuffix.size());
    8056            0 :                 CHK_RET(ReplaceCommInfoByTag(oldtag, commInfo));
    8057            0 :             }
    8058            1 :         }
    8059              : 
    8060            1 :         if (!(IsExistMutiStreamRes(tag))) {
    8061            1 :             level1StreamInfo_t streamInfo;
    8062            1 :             std::unique_lock<std::mutex> mutiStreamLock(tagStreamInfoLock_);
    8063              :             // 2p场景下,mc2当前algType为518,streamInfo.ringNum走默认流程值为1导致资源申请不足,910_93 mc2固定在节点内默认用mesh
    8064            1 :             constexpr u32 RANK_SIZE_TWO = 2;
    8065            1 :             if ((GetRankSize() == RANK_SIZE_TWO && !isA2MC2MultiServer_) || (deviceType_ == DevType::DEV_TYPE_910_93)) {
    8066            0 :                 algTypeTmp.algoLevel0 = AlgTypeLevel0::ALG_LEVEL0_NP_MESH;
    8067            0 :                 algTypeTmp.algoLevel1 = AlgTypeLevel1::ALG_LEVEL1_RING;
    8068              :             }
    8069            1 :             HcclResult ret = HCCL_SUCCESS;
    8070            1 :             if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
    8071            0 :                 ret = CreateMutiStreamResFor310P(tag, streamInfo);
    8072              :             } else {
    8073            1 :                 ret = implAlg_->CreateMutiStreamRes(tag, stream, streamInfo, algTypeTmp, true);
    8074              :             }
    8075            1 :             CHK_PRT_RET(ret != HCCL_SUCCESS,
    8076              :                         HCCL_ERROR("[HcclCommunicator][CreateCommAndStreamRes]errNo[0x%016llx]tag[%s],comm resource create stream "
    8077              :                                    "resource",
    8078              :                                    HCCL_ERROR_CODE(ret),
    8079              :                                    tag.c_str()),
    8080              :                         ret);
    8081            1 :             tagStreamInfo_.insert(std::pair<std::string, Level1StreamInfo>(tag, std::move(streamInfo)));
    8082            1 :             opRetryStreamPtr_->insert(std::make_pair(tag, tagStreamInfo_[tag].ringDeviceStreams));
    8083            1 :             mutiStreamLock.unlock();
    8084            1 :         }
    8085              : 
    8086            1 :         HCCL_INFO("resource creation (AllReduce) success, tag[%s]", tag.c_str());
    8087            1 :         CHK_RET(notifyPool_->UnregisterOp(tag));
    8088            1 :         if (commPortConfig_.devPortSwitchOn) {
    8089            0 :             std::vector<u32> &nicPorts = groupNicRanksPort_.empty() ? nicRanksPort_ : groupNicRanksPort_;
    8090            0 :             std::vector<u32> &vnicPorts = groupVnicRanksPort_.empty() ? vnicRanksPort_ : groupVnicRanksPort_;
    8091            0 :             Heartbeat::GetInstance(deviceLogicId_).SetRankPortInfo(isUseRankPort_, nicPorts, vnicPorts,
    8092            0 :                 commPortConfig_.devPortSwitchOn);
    8093              :         }
    8094            1 :         CHK_RET(RegisterToHeartBeat());
    8095              : 
    8096            1 :         CommBase *comm = nullptr;
    8097            1 :         CHK_RET(GetComm(tag, &comm));
    8098            1 :         if (comm == nullptr) {
    8099            0 :             HCCL_ERROR("comm get err, comm %p", comm);
    8100            0 :             return HCCL_E_PTR;
    8101              :         }
    8102            1 :         CHK_RET(SetCommResource(commInputSize, commInputPtr, commOutputPtr, expMem.ptr(),
    8103              :                                 comm, tagStreamInfo_[tag], stream));
    8104              : 
    8105            1 :         return HCCL_SUCCESS;
    8106            1 :     }
    8107              : 
    8108            1 :     HcclResult HcclCommunicator::GetComm(const std::string &tag, CommBase **comm)
    8109              :     {
    8110            1 :         if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
    8111            0 :             *comm = tagCommInfo_[tag].commIntraServer.get();
    8112            1 :         } else if (isA2MC2MultiServer_) {
    8113              :             // 使用打平RDMA Mesh子通信域
    8114            0 :             *comm = tagCommInfo_[tag].commLevel1Rdma[0].get();
    8115              :         } else {
    8116            1 :             *comm = tagCommInfo_[tag].commLevel0[0].get();
    8117              :         }
    8118            1 :         return HCCL_SUCCESS;
    8119              :     }
    8120              : 
    8121            1 :     HcclResult HcclCommunicator::SetCommResource(u64 commBufferSize, void *commInPtr, void *commOutPtr, void *commExpPtr,
    8122              :                                                  CommBase *comm, level1StreamInfo_t &streamInfo, Stream &stream)
    8123              :     {
    8124            1 :         CHK_PTR_NULL(combinOparaMem_);
    8125            1 :         HcclCombinOpParam *combinOparaPtr = reinterpret_cast<HcclCombinOpParam*>(combinOparaMem_->ptr());
    8126            1 :         CHK_PTR_NULL(combinOparaPtr);
    8127              : 
    8128            1 :         u32 rankSize = comm->RankSize();
    8129            1 :         u32 curRankId = comm->Rank();
    8130            1 :         u32 usrRankId = comm->UserRank();
    8131            1 :         combinOparaPtr->rankId = curRankId;
    8132            1 :         combinOparaPtr->signalInfo.aicpuNotify.rankId = curRankId;
    8133            1 :         combinOparaPtr->rankNum = rankSize;
    8134            1 :         combinOparaPtr->winSize = commBufferSize;
    8135            1 :         combinOparaPtr->winExpSize = EXP_BUFFER_SIZE;
    8136            1 :         combinOparaPtr->config.deterministic = GetDeterministicConfig();
    8137            1 :         combinOparaPtr->config.notifyWaitTime =
    8138            1 :             (GetExternalInputHcclExecTimeoutSet() != HcclExecTimeoutSet::HCCL_EXEC_TIMEOUT_NOT_SET ||
    8139            1 :             commConfig_.GetConfigExecTimeOutSet()) ? commConfig_.GetConfigExecTimeOut() : NOTIFY_DEFAULT_WAIT_TIME;
    8140            1 :         hcclMc2Info_.groupName = hrtMsprofGetHashId(identifier_.c_str(), identifier_.length());
    8141            1 :         combinOparaPtr->config.linkTimeOut = std::chrono::seconds(GetExternalInputHcclLinkTimeOut());
    8142            1 :         hcclMc2Info_.rankSize = rankSize;
    8143            1 :         hcclMc2Info_.rankId = curRankId;
    8144            1 :         hcclMc2Info_.usrRankId = usrRankId;
    8145            1 :         hcclMc2Info_.aicpuKfcStreamId = static_cast<uint32_t>(stream.id());
    8146            1 :         hcclMc2Info_.commStreamSize = rankSize;
    8147            1 :         hcclMc2Info_.reserve = 0;
    8148            1 :         rtEvent_t event = nullptr;
    8149            1 :         u32 eventId = 0;
    8150            1 :         u32 idx = 0;
    8151            1 :         u32 txSigleBase = 2;
    8152            1 :         u32 rxSigleBase = 3;
    8153              : 
    8154            1 :         if (isA2MC2MultiServer_) {
    8155              :             // MoE融合算子优化,MC2多机场景
    8156              :             // 判断是否支持NormalQP创建,若不支持,需要额外下发敲Doorbell任务
    8157            0 :             bool isSupportNormalQP = false;
    8158            0 :             CHK_RET(IsSupportAicpuNormalQP(devicePhyId_, isSupportNormalQP));
    8159            0 :             CHK_RET(SetDevIbverbsData(comm, isSupportNormalQP, commBufferSize, commInPtr, commOutPtr));
    8160              : 
    8161            0 :             bool isSupportAIVNormalQP = false;
    8162            0 :             CHK_RET(IsSupportAIVNormalQP(devicePhyId_, isSupportAIVNormalQP));
    8163            0 :             if (isSupportAIVNormalQP && isA2MC2IntraHie_) {
    8164            0 :                 CHK_RET(GenAiRMAInfo(comm));
    8165            0 :             } else {
    8166            0 :                 HCCL_WARNING("[%s] db transfer normal qp not support. tag[%s] curRankId[%u] rankNum[%u] isSupportAIVNormalQP[%u]",
    8167              :                              __func__, comm->Tag().c_str(), curRankId, rankSize, isSupportAIVNormalQP);
    8168              :             }
    8169              : 
    8170            0 :             if (combinedCapabilityMem_ == nullptr) {
    8171            0 :                 CHK_RET(AllocAndClearHostMem(sizeof(CombinedCapability), combinedCapabilityMem_));
    8172              :             }
    8173            0 :             CHK_PTR_NULL(combinedCapabilityMem_);
    8174            0 :             CombinedCapability *combinedCapabilityPtr = reinterpret_cast<CombinedCapability*>(combinedCapabilityMem_->ptr());
    8175            0 :             CHK_PTR_NULL(combinedCapabilityPtr);
    8176            0 :             SalSetBitOne(combinedCapabilityPtr->dataplaneModeBitmap, POS_DATA_PLANE_MODE_HOST);
    8177            0 :             if (isSupportAIVNormalQP && isA2MC2IntraHie_) {
    8178            0 :                 SalSetBitOne(combinedCapabilityPtr->dataplaneModeBitmap, POS_DATA_PLANE_MODE_AIV);
    8179              :             }
    8180            0 :             SalSetBitOne(combinedCapabilityPtr->dataplaneModeBitmap, POS_DATA_PLANE_MODE_AICPU);
    8181              : 
    8182            0 :             HCCL_INFO("[SetCommResource] Set dataplaneModeBitmap to [%llu]", combinedCapabilityPtr->dataplaneModeBitmap);
    8183              : 
    8184              :             // 非NormalQP场景需要传一条流,用于敲Doorbell
    8185            0 :             combinOparaPtr->streamInfo[0].streamIds = streamInfo.ringDeviceStreams[0].id();
    8186            0 :             combinOparaPtr->streamInfo[0].sqIds = streamInfo.ringDeviceStreams[0].sqId();
    8187            0 :             combinOparaPtr->streamInfo[0].cqIds = streamInfo.ringDeviceStreams[0].cqId();
    8188            0 :             combinOparaPtr->streamInfo[0].logicCqids = streamInfo.ringDeviceStreams[0].logicCqId();
    8189            0 :             HCCL_DEBUG("[SetCommResource] Set streamInfo[0].streamIds[%u].sqIds[%u].cqIds[%u].logicCqids[%u]",
    8190              :                        combinOparaPtr->streamInfo[0].streamIds,
    8191              :                        combinOparaPtr->streamInfo[0].sqIds,
    8192              :                        combinOparaPtr->streamInfo[0].cqIds,
    8193              :                        combinOparaPtr->streamInfo[0].logicCqids);
    8194              :         } else {
    8195            2 :             for (u32 i = 0; i < rankSize; i++) {
    8196            1 :                 if (i != curRankId) {
    8197              :                     void *bufferIn;
    8198              :                     void *bufferOut;
    8199            0 :                     std::vector<void *> remotePtrVec;
    8200            0 :                     CHK_RET(comm->GetTransportByRank(i)->GetRemoteMem(UserMemType::INPUT_MEM, &bufferIn));
    8201            0 :                     combinOparaPtr->windowsIn[i] = reinterpret_cast<u64>(bufferIn);
    8202              : 
    8203            0 :                     CHK_RET(comm->GetTransportByRank(i)->GetRemoteMem(UserMemType::OUTPUT_MEM, &bufferOut));
    8204            0 :                     combinOparaPtr->windowsOut[i] = reinterpret_cast<u64>(bufferOut);
    8205              : 
    8206            0 :                     CHK_RET(comm->GetTransportByRank(i)->GetRemoteMem(&remotePtrVec));
    8207            0 :                     if (remotePtrVec.size() != 0) {
    8208            0 :                         combinOparaPtr->windowsExp[i] = reinterpret_cast<u64>(remotePtrVec[0]);
    8209            0 :                         if (comm->GetTransportByRank(i)->GetTransportType() == TransportType::TRANS_TYPE_P2P) {
    8210            0 :                             p2pCclBuf_[i] = remotePtrVec[0];
    8211              :                         } else {
    8212            0 :                             cclBuf_[i] = remotePtrVec[0];
    8213              :                         }
    8214            0 :                         combinOparaPtr->windowsExp[i] += cclBufferManager_.GetInCCLbufferSize() + cclBufferManager_.GetOutCCLbufferSize();
    8215              :                     }
    8216            0 :                     CHK_RET(comm->GetTransportByRank(i)->GetTxAckDevNotifyInfo(combinOparaPtr->signalInfo.ipcNotifys[i]));
    8217            0 :                     CHK_RET(comm->GetTransportByRank(i)->GetRxAckDevNotifyInfo(combinOparaPtr->signalInfo.ipcNotifys[i + rankSize]));
    8218            0 :                     CHK_RET(comm->GetTransportByRank(i)->GetTxDataSigleDevNotifyInfo(combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase]));
    8219            0 :                     CHK_RET(comm->GetTransportByRank(i)->GetRxDataSigleDevNotifyInfo(combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase]));
    8220            0 :                     CHK_RET(GetAiCpuNotifyData(streamInfo.ringDeviceSignalAux[idx],
    8221              :                                                combinOparaPtr->signalInfo.noIpcNotifys[i]));
    8222              : 
    8223            0 :                     CHK_RET(GetAiCpuNotifyData(streamInfo.ringDeviceSignal[idx],
    8224              :                                                combinOparaPtr->signalInfo.noIpcNotifys[i + rankSize]));
    8225            0 :                     idx++;
    8226            0 :                 } else {
    8227            1 :                     combinOparaPtr->windowsIn[i] = reinterpret_cast<u64>(commInPtr);
    8228            1 :                     combinOparaPtr->windowsOut[i] = reinterpret_cast<u64>(commOutPtr);
    8229            1 :                     combinOparaPtr->windowsExp[i] = reinterpret_cast<u64>(commExpPtr);
    8230              :                     // 在与aicpu商议后,本卡不再防止无效值。后续代码要删掉
    8231            1 :                     combinOparaPtr->signalInfo.ipcNotifys[i].resId = INVALID_U64;
    8232            1 :                     combinOparaPtr->signalInfo.ipcNotifys[i + rankSize].resId = INVALID_U64;
    8233            1 :                     combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase].resId = INVALID_U64;
    8234            1 :                     combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase].resId = INVALID_U64;
    8235              :                 }
    8236            1 :                 HCCL_INFO("group[%s] successfully set windowsIn & windowsOut & windowsExp info: userRank[%u], groupRank[%u], "
    8237              :                           "windowsIn[0x%llx], InSize[0x%llx], windowOut[0x%llx], OutSize[0x%llx], windowExp[0x%llx], ExpSize[0x%llu]",
    8238              :                           identifier_.c_str(), GetUserRank(), GetGroupRank(),
    8239              :                           combinOparaPtr->windowsIn[i], cclBufferManager_.GetInCCLbufferSize(),
    8240              :                           combinOparaPtr->windowsOut[i], cclBufferManager_.GetOutCCLbufferSize(),
    8241              :                           combinOparaPtr->windowsExp[i], cclBufferManager_.GetExpBufferSize());
    8242              : 
    8243            1 :                 combinOparaPtr->signalInfo.ipcNotifys[i].rankId = i;
    8244            1 :                 combinOparaPtr->signalInfo.ipcNotifys[i + rankSize].rankId = i;
    8245            1 :                 combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase].rankId = i;
    8246            1 :                 combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase].rankId = i;
    8247            1 :                 combinOparaPtr->signalInfo.noIpcNotifys[i].rankId = i;
    8248              : 
    8249            1 :                 hcclMc2Info_.commStreamIds[i] = streamInfo.ringDeviceStreams[i].id();
    8250            1 :                 combinOparaPtr->streamInfo[i].streamIds = streamInfo.ringDeviceStreams[i].id();
    8251            1 :                 combinOparaPtr->streamInfo[i].sqIds = streamInfo.ringDeviceStreams[i].sqId();
    8252            1 :                 combinOparaPtr->streamInfo[i].cqIds = streamInfo.ringDeviceStreams[i].cqId();
    8253            1 :                 combinOparaPtr->streamInfo[i].logicCqids = streamInfo.ringDeviceStreams[i].logicCqId();
    8254            1 :                 HCCL_DEBUG("[hccl_Mc2_Info] commStreamIds[%u]:[%u]", i, streamInfo.ringDeviceStreams[i].id());
    8255              : 
    8256            1 :                 CHK_RET(hrtEventCreateWithFlag(&event));
    8257              : 
    8258            1 :                 CHK_RET(hrtGetEventID(event, &eventId));
    8259            1 :                 aiCpuNoIpcEvnet_.push_back(event);
    8260            1 :                 combinOparaPtr->signalInfo.noIpcEvents[i].resId = eventId;
    8261            1 :                 HCCL_DEBUG("SetCommResource ipc notify info pre record local rankid: %u: remote rankid:%u, resId:%llu, "
    8262              :                            "devId:%u, tsId:%u, addr:%llu.",
    8263              :                            curRankId, combinOparaPtr->signalInfo.ipcNotifys[i].rankId, combinOparaPtr->signalInfo.ipcNotifys[i].resId,
    8264              :                            combinOparaPtr->signalInfo.ipcNotifys[i].devId, combinOparaPtr->signalInfo.ipcNotifys[i].tsId,
    8265              :                            combinOparaPtr->signalInfo.ipcNotifys[i].addr);
    8266            1 :                 HCCL_DEBUG("SetCommResource ipc notify info pre wait local rankid: %u: remote rankid:%u, resId:%llu, "
    8267              :                            "devId:%u, tsId:%u, addr:%llu.",
    8268              :                            curRankId, combinOparaPtr->signalInfo.ipcNotifys[i + rankSize].rankId,
    8269              :                            combinOparaPtr->signalInfo.ipcNotifys[i + rankSize].resId,
    8270              :                            combinOparaPtr->signalInfo.ipcNotifys[i + rankSize].devId,
    8271              :                            combinOparaPtr->signalInfo.ipcNotifys[i + rankSize].tsId,
    8272              :                            combinOparaPtr->signalInfo.ipcNotifys[i + rankSize].addr);
    8273            1 :                 HCCL_DEBUG("SetCommResource ipc notify info post record local rankid: %u: remote rankid:%u, resId:%llu, "
    8274              :                            "devId:%u, tsId:%u, addr:%llu.",
    8275              :                            curRankId,
    8276              :                            combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase].rankId,
    8277              :                            combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase].resId,
    8278              :                            combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase].devId,
    8279              :                            combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase].tsId,
    8280              :                            combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * txSigleBase].addr);
    8281            1 :                 HCCL_DEBUG("SetCommResource ipc notify info post wait local rankid: %u: remote rankid:%u, resId:%llu, "
    8282              :                            "devId:%u, tsId:%u, addr:%llu.",
    8283              :                            curRankId,
    8284              :                            combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase].rankId,
    8285              :                            combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase].resId,
    8286              :                            combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase].devId,
    8287              :                            combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase].tsId,
    8288              :                            combinOparaPtr->signalInfo.ipcNotifys[i + rankSize * rxSigleBase].addr);
    8289              :             }
    8290              :         }
    8291            1 :         HCCL_DEBUG("[hccl_Mc2_Info] groupname:[%s][%llu], rankSize[%u], rankId[%u], usrRankId[%u], aicpuKfcStreamId[%u], "
    8292              :                    "commStreamSize[%u]",
    8293              :                    identifier_.c_str(), hcclMc2Info_.groupName, rankSize, curRankId, usrRankId,
    8294              :                    static_cast<uint32_t>(stream.id()), rankSize);
    8295            1 :         CHK_RET(ProfilingManagerPub::CallMsprofReportMc2CommInfo(hrtMsprofSysCycleTime(), &hcclMc2Info_,
    8296              :                                                                  sizeof(hcclMc2Info_)));
    8297            1 :         return HCCL_SUCCESS;
    8298              :     }
    8299              : 
    8300          801 :     void HcclCommunicator::ReleaseCommContextbuffer()
    8301              :     {
    8302          801 :         commContext_.free();
    8303          804 :     }
    8304              : 
    8305            0 :     HcclResult HcclCommunicator::CreateDeviceCommContext(u64 size, DeviceMem &buffer) const
    8306              :     {
    8307            0 :         CHK_PRT_RET(!size, HCCL_INFO("[Create][DeviceCommContext]device commContext size is zero. "
    8308              :                                      "not need to malloc memory"),
    8309              :                     HCCL_SUCCESS);
    8310              : 
    8311              :         CHK_PRT_RET((size > ULONG_MAX),
    8312              :                     HCCL_ERROR("[Create][DeviceCommContext]device commContext size %llu is large than ULONG_MAX",
    8313              :                                size),
    8314              :                     HCCL_E_PARA);
    8315              : 
    8316            0 :         if (!buffer.ptr()) {
    8317            0 :             u64 memSize = size;
    8318            0 :             CHK_RET(DeviceMem::alloc(buffer, memSize));
    8319              :         }
    8320            0 :         return HCCL_SUCCESS;
    8321              :     }
    8322              : 
    8323            0 :     void HcclCommunicator::Break()
    8324              :     {
    8325            0 :         if (implAlg_ != nullptr) {
    8326            0 :             implAlg_->Break();
    8327              :         }
    8328            0 :         return;
    8329              :     }
    8330              : 
    8331            3 :     HcclResult HcclCommunicator::GetAlltoAllStagedWorkSpaceMemSize(u64 *sendCounts, u64 *sdispls, HcclDataType sendType,
    8332              :                                                                    u64 *recvCounts, u64 *rdispls, HcclDataType recvType, u64 &memSize)
    8333              :     {
    8334            3 :         if (Is310P3Common(isHaveCpuRank_, deviceType_)) {
    8335            0 :             HCCL_ERROR("[%s][%s]GetAlltoAllStagedWorkSpaceMemSize Not Supported!",
    8336              :                 LOG_KEYWORDS_TASK_EXEC.c_str(), LOG_KEYWORDS_NOT_SUPPORTED.c_str());
    8337            0 :             return HCCL_E_NOT_SUPPORT;
    8338              :         }
    8339            3 :         CHK_SMART_PTR_NULL(implAlg_);
    8340            3 :         std::unique_ptr<CollAlgOperator> algOperator = implAlg_->GetAlgOperator(HcclCMDType::HCCL_CMD_ALLTOALLV);
    8341            3 :         AlltoAllOperator *alltoAllOperator = dynamic_cast<AlltoAllOperator *>(algOperator.get());
    8342            3 :         CHK_PTR_NULL(alltoAllOperator);
    8343              : 
    8344            3 :         OpParam opParam;
    8345            3 :         opParam.All2AllDataDes.sendType = sendType;
    8346            3 :         opParam.All2AllDataDes.recvType = recvType;
    8347            3 :         opParam.All2AllDataDes.sendCounts = static_cast<void *>(sendCounts);
    8348            3 :         opParam.All2AllDataDes.recvCounts = static_cast<void *>(recvCounts);
    8349            3 :         opParam.All2AllDataDes.sdispls = static_cast<void *>(sdispls);
    8350            3 :         opParam.All2AllDataDes.rdispls = static_cast<void *>(rdispls);
    8351            3 :         opParam.opType = HcclCMDType::HCCL_CMD_ALLTOALLV;
    8352            3 :         opParam.aicpuUnfoldMode = false;
    8353            3 :         opParam.aicpuCacheEnable = 0;
    8354              : 
    8355            3 :         if (alltoAllOperator->IsSatisfyAlltoAllAivCondition(opParam) ||
    8356            0 :             alltoAllOperator->IsSatisfy91093OffloadCondition()) {
    8357            3 :             memSize = 0;
    8358            3 :             HCCL_INFO("Calculate workSpace MemSize for aiv AllToAll done, memSize[%llu]", memSize);
    8359            3 :             return HCCL_SUCCESS;
    8360              :         }
    8361              : 
    8362            0 :         std::unique_ptr<PreProcessMetaInfo> preMetaInfo = std::make_unique<PreProcessMetaInfo>();
    8363            0 :         CHK_SMART_PTR_NULL(preMetaInfo);
    8364              : 
    8365            0 :         CHK_RET(alltoAllOperator->PrepareAlltoAllAddrInfo(opParam.All2AllDataDes.sendCounts, opParam.All2AllDataDes.sdispls,
    8366              :                                                           opParam.All2AllDataDes.sendType, opParam.All2AllDataDes.recvCounts, opParam.All2AllDataDes.rdispls,
    8367              :                                                           opParam.All2AllDataDes.recvType, preMetaInfo));
    8368              : 
    8369            0 :         preMetaInfo->opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    8370              : 
    8371            0 :         CHK_RET(RegressCalPreOp(alltoAllOperator, opParam, preMetaInfo));
    8372              : 
    8373            0 :         return alltoAllOperator->GetAlltoAllStagedWorkSpaceMemSize(opParam, memSize);
    8374            3 :     }
    8375              : 
    8376            1 :     HcclResult HcclCommunicator::GetAlltoAllStagedWorkSpaceMemSize(
    8377              :         std::vector<SendRecvInfo> &allMeshAggregationSendRecvInfo, u64 &memSize)
    8378              :     {
    8379            1 :         CHK_PRT_RET(Is310P3Common(isHaveCpuRank_, deviceType_),
    8380              :                     HCCL_ERROR("[HcclCommunicator][GetAlltoAllStagedWorkSpaceMemSize]Not Supported!"), HCCL_E_NOT_SUPPORT);
    8381              : 
    8382            1 :         CHK_SMART_PTR_NULL(implAlg_);
    8383            0 :         return implAlg_->GetAlltoAllStagedWorkSpaceMemSize(allMeshAggregationSendRecvInfo, memSize);
    8384              :     }
    8385              : 
    8386            0 :     HcclResult HcclCommunicator::GetAllReduceScratchSize(
    8387              :         const u64 count, const HcclDataType dataType, u64 &scratchSize) const
    8388              :     {
    8389            0 :         CHK_SMART_PTR_NULL(implAlg_);
    8390            0 :         return implAlg_->GetAllReduceScratchSize(count, dataType, scratchSize);
    8391              :     }
    8392              : 
    8393           33 :     HcclResult HcclCommunicator::SetWorldGroupInfo(
    8394              :         std::unordered_map<std::string, std::map<u32, HcclIpAddress>> phyIdNicInfoMap,
    8395              :         vector<RankInfo> worldRankInfoList, vector<u32> &nicRanksPort, vector<u32> &vnicRanksPort)
    8396              :     {
    8397           33 :         for (auto &ipInfo : phyIdNicInfoMap) {
    8398            0 :             for (auto &devInfo : ipInfo.second) {
    8399            0 :                 rankDevicePhyIdNicInfoMap_[ipInfo.first][devInfo.first] = devInfo.second;
    8400            0 :                 HCCL_DEBUG("phyIdNicInfoMap print hostIp[%s] devId[%u] devIp[%s]",
    8401              :                            ipInfo.first.c_str(), devInfo.first, devInfo.second.GetReadableAddress());
    8402              :             }
    8403              :         }
    8404              : 
    8405           33 :         for (auto &rankInfo : worldRankInfoList) {
    8406            0 :             worldRankInfoList_.push_back(rankInfo);
    8407              :         }
    8408              : 
    8409           33 :         for (auto &port : nicRanksPort) {
    8410            0 :             nicRanksPort_.push_back(port);
    8411            0 :             HCCL_DEBUG("nicRanksPort port[%u]", port);
    8412              :         }
    8413           33 :         for (auto &port : vnicRanksPort) {
    8414            0 :             vnicRanksPort_.push_back(port);
    8415            0 :             HCCL_DEBUG("vnicRanksPort port[%u]", port);
    8416              :         }
    8417           33 :         return HCCL_SUCCESS;
    8418              :     }
    8419              : 
    8420            0 :     HcclResult HcclCommunicator::GetTopoDesc(HcclTopoDescs *topoDescs, uint32_t topoSize)
    8421              :     {
    8422            0 :         if (topoSize < static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_MAX)) {
    8423            0 :             HCCL_ERROR("topoDescs size is not enough, please check topoSize[%u]", topoSize);
    8424            0 :             return HCCL_E_PARA;
    8425              :         }
    8426              : 
    8427            0 :         if (deviceType_ == DevType::DEV_TYPE_910_93) {
    8428            0 :             topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)].algSets = HCCL_ALG_SWITCH | HCCL_ALG_RING;
    8429            0 :             topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L1)].algSets = HCCL_ALG_RING;
    8430            0 :         } else if (deviceType_ == DevType::DEV_TYPE_910B) {
    8431            0 :             topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)].algSets = HCCL_ALG_MESH;
    8432            0 :             topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L1)].algSets = 0;
    8433            0 :         } else if (deviceType_ == DevType::DEV_TYPE_310P3) {
    8434            0 :             topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)].algSets = HCCL_ALG_RING;
    8435            0 :             topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L1)].algSets = 0;
    8436              :         }
    8437              : 
    8438            0 :         topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)].rankSize = userRankSize_;
    8439            0 :         topoDescs[static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L1)].rankSize = 0;
    8440            0 :         return HCCL_SUCCESS;
    8441              :     }
    8442              : 
    8443          234 :     HcclResult HcclCommunicator::SetAivModeConfig(const bool aivMode)
    8444              :     {
    8445          234 :         CHK_SMART_PTR_NULL(implAlg_);
    8446          234 :         CHK_RET(implAlg_->SetAivModeConfig(aivMode));
    8447          234 :         return HCCL_SUCCESS;
    8448              :     }
    8449              : 
    8450          234 :     HcclResult HcclCommunicator::SetOnlyAivModeConfig(const bool isOnlyAiv)
    8451              :     {
    8452          234 :         CHK_SMART_PTR_NULL(implAlg_);
    8453          234 :         CHK_RET(implAlg_->SetOnlyAivModeConfig(isOnlyAiv));
    8454          234 :         isOnlyAiv_ = isOnlyAiv;
    8455          234 :         return HCCL_SUCCESS;
    8456              :     }
    8457              : 
    8458          234 :     HcclResult HcclCommunicator::SetAicpuUnfoldConfig(const bool aicpuUnfold)
    8459              :     {
    8460          234 :         CHK_SMART_PTR_NULL(implAlg_);
    8461          234 :         CHK_RET(implAlg_->SetAicpuUnfoldConfig(aicpuUnfold));
    8462          234 :         return HCCL_SUCCESS;
    8463              :     }
    8464              : 
    8465          234 :     HcclResult HcclCommunicator::SetExecTimeOutConfig(const s32 execTimeOut)
    8466              :     {
    8467          234 :         CHK_SMART_PTR_NULL(implAlg_);
    8468          234 :         CHK_RET(implAlg_->SetExecTimeOutConfig(execTimeOut));
    8469          234 :         return HCCL_SUCCESS;
    8470              :     }
    8471              : 
    8472          234 :     HcclResult HcclCommunicator::SetAlgoConfig(const std::map<HcclCMDType, std::vector<HcclAlgoType>>& algoMap)
    8473              :     {
    8474          234 :         CHK_SMART_PTR_NULL(implAlg_);
    8475          234 :         CHK_RET(implAlg_->SetAlgoConfig(algoMap));
    8476          234 :         return HCCL_SUCCESS;
    8477              :     }
    8478              : 
    8479          527 :     bool HcclCommunicator::GetAivModeConfig()
    8480              :     {
    8481          527 :         return commConfig_.GetConfigAivMode();
    8482              :     }
    8483              : 
    8484          529 :     bool HcclCommunicator::GetConfigIsOnlyAivMode()
    8485              :     {
    8486          529 :         return commConfig_.GetConfigIsOnlyAivMode();
    8487              :     }
    8488              : 
    8489         1981 :     bool HcclCommunicator::GetAicpuUnfoldConfig()
    8490              :     {
    8491         1981 :         return commConfig_.GetConfigAicpuUnfold();
    8492              :     }
    8493              : 
    8494            0 :     void HcclCommunicator::SetQpQosAttr(u32 trafficClass, u32 serviceLevel)
    8495              :     {
    8496            0 :         if (oneSideService_) {
    8497            0 :             oneSideService_->SetTCAndSL(trafficClass, serviceLevel);
    8498            0 :             HCCL_INFO("[%s]Set TC[%u] and SL[%u] for oneSidedService success.", __func__, trafficClass, serviceLevel);
    8499              :         }
    8500            0 :         transportManager_->SetQpQosAttr(trafficClass, serviceLevel);
    8501            0 :         indptOpTransportManager_->SetQpQosAttr(trafficClass, serviceLevel);
    8502            0 :     }
    8503              : 
    8504            0 :     HcclResult HcclCommunicator::CheckExitWaitResumeState(bool &isChangedLink)
    8505              :     {
    8506            0 :         if (retryEnable_ && opRetryManager_ != nullptr) {
    8507            0 :             bool haveCommEnableBackupLink = false;
    8508            0 :             if (g_enableBackupLinkCommCount.load() > 0) {
    8509            0 :                 haveCommEnableBackupLink = true;
    8510              :             }
    8511            0 :             HcclResult ret = opRetryManager_->ExitWaitResumeState(identifier_, commConnections_.isRoot, haveCommEnableBackupLink, isChangedLink);
    8512            0 :             CHK_PRT_RET(ret != HCCL_SUCCESS,
    8513              :                         HCCL_ERROR("[HcclCommunicator][Resume]opretry exit wait resume state failed."), ret);
    8514              :         }
    8515            0 :         return HCCL_SUCCESS;
    8516              :     }
    8517              : 
    8518            0 :     HcclResult HcclCommunicator::SetMemoryRange(void *baseVirPtr, size_t size, size_t alignment, uint64_t flags)
    8519              :     {
    8520            0 :         CHK_PRT_RET(deviceType_ != DevType::DEV_TYPE_910_93,
    8521              :                     HCCL_ERROR("[HcclCommunicator][SetMemoryRange] deviceType[%d] not support zero copy", deviceType_), HCCL_E_NOT_SUPPORT);
    8522            0 :         if (zeroCopyMemoryAgent_ == nullptr) {
    8523            0 :             CHK_RET(InitZeroCopyMemoryAgent());
    8524              :         }
    8525            0 :         CHK_RET(zeroCopyMemoryAgent_->SetMemoryRange(baseVirPtr, size, alignment, flags));
    8526            0 :         return HCCL_SUCCESS;
    8527              :     }
    8528              : 
    8529            0 :     HcclResult HcclCommunicator::UnsetMemoryRange(void *baseVirPtr)
    8530              :     {
    8531            0 :         CHK_PRT_RET(zeroCopyMemoryAgent_ == nullptr,
    8532              :                     HCCL_ERROR("[HcclCommunicator][UnsetMemoryRange] not call HcclCommSetMemoryRange()"), HCCL_E_PARA);
    8533            0 :         CHK_RET(zeroCopyMemoryAgent_->UnsetMemoryRange(baseVirPtr));
    8534            0 :         return HCCL_SUCCESS;
    8535              :     }
    8536              : 
    8537            0 :     HcclResult HcclCommunicator::ActivateCommMemory(void *virPtr, size_t size, size_t offset, void *handle, uint64_t flags)
    8538              :     {
    8539            0 :         CHK_PRT_RET(zeroCopyMemoryAgent_ == nullptr,
    8540              :                     HCCL_ERROR("[HcclCommunicator][ActivateCommMemory] not call HcclCommSetMemoryRange()"), HCCL_E_PARA);
    8541            0 :         CHK_RET(zeroCopyMemoryAgent_->ActivateCommMemory(virPtr, size, offset, handle, flags));
    8542            0 :         return HCCL_SUCCESS;
    8543              :     }
    8544              : 
    8545            0 :     HcclResult HcclCommunicator::DeactivateCommMemory(void *virPtr)
    8546              :     {
    8547            0 :         CHK_PRT_RET(zeroCopyMemoryAgent_ == nullptr,
    8548              :                     HCCL_ERROR("[HcclCommunicator][DeactivateCommMemory] not call HcclCommSetMemoryRange()"), HCCL_E_PARA);
    8549            0 :         CHK_RET(zeroCopyMemoryAgent_->DeactivateCommMemory(virPtr));
    8550            0 :         return HCCL_SUCCESS;
    8551              :     }
    8552              : 
    8553            0 :     HcclResult HcclCommunicator::SetSingleLinkInfo(std::unordered_map<u32, bool> &switchRanks, u32 remoteRankId,
    8554              :                                                    ChangeLinkInfo &changeLinkInfo)
    8555              :     {
    8556            0 :         auto iterLocal = switchRanks.find(userRank_);
    8557            0 :         auto iterRemote = switchRanks.find(remoteRankId);
    8558              : 
    8559            0 :         bool useBackupLink = false;
    8560            0 :         if (iterLocal != switchRanks.end() && iterRemote != switchRanks.end()) {
    8561              :             // 本端卡和对端卡都切,如果两者的目标网卡冲突,则切换失败;否则使用一致的目标网卡的的对应链路
    8562            0 :             CHK_PRT_RET(iterLocal->second ^ iterRemote->second,
    8563              :                         HCCL_ERROR("[HcclCommunicator][SetSingleLinkInfo] local rank[%u] plan to switch to nic[%u], "
    8564              :                                    "which is conflict with remote rank[%u] planning to switch to nic[%u].",
    8565              :                                    userRank_, iterLocal->second, remoteRankId, iterRemote->second),
    8566              :                         HCCL_E_PARA);
    8567            0 :             useBackupLink = iterLocal->second;
    8568            0 :         } else if (iterLocal != switchRanks.end()) {
    8569              :             // 仅切换本端卡,根据本端卡的目标网卡,刷新对应链路
    8570            0 :             useBackupLink = iterLocal->second;
    8571            0 :         } else if (iterRemote != switchRanks.end()) {
    8572              :             // 仅切换对端卡,根据对端卡的目标网卡,刷新对应链路
    8573            0 :             useBackupLink = iterRemote->second;
    8574              :         } else {
    8575            0 :             HCCL_INFO("[HcclCommunicator][SetSingleLinkInfo] comm identifier[%s], local rank[%u], "
    8576              :                       "remote rank[%u], neither the rank need switch, link will not be refreshed.",
    8577              :                       identifier_.c_str(), userRank_, remoteRankId);
    8578            0 :             return HCCL_SUCCESS;
    8579              :         }
    8580              : 
    8581            0 :         changeLinkInfo.remoteRankList[changeLinkInfo.remoteRankNum] = remoteRankId;
    8582            0 :         changeLinkInfo.isUseDefaultPort[changeLinkInfo.remoteRankNum] = !(useBackupLink);
    8583            0 :         changeLinkInfo.remoteRankNum++;
    8584            0 :         remoteRankNicStatus_[remoteRankId] = useBackupLink ? CONNECT_REMOTE_BACKUP : CONNECT_REMOTE_DEFAULT;
    8585            0 :         needCheckBackupNic_ |= useBackupLink;
    8586            0 :         needCheckDefaultNic_ |= !useBackupLink;
    8587              : 
    8588            0 :         HCCL_RUN_INFO("[HcclCommunicator][SetSingleLinkInfo] comm identifier[%s], local rank[%u], "
    8589              :                       "remote rank[%u], useBackupLink[%u], link info refreshed.",
    8590              :                       identifier_.c_str(), userRank_, remoteRankId, useBackupLink);
    8591            0 :         return HCCL_SUCCESS;
    8592              :     }
    8593              : 
    8594            0 :     HcclResult HcclCommunicator::SetRemoteRankLinkInfo(std::unordered_map<u32, bool> &switchRanks,
    8595              :                                                        ChangeLinkInfo &changeLinkInfo)
    8596              :     {
    8597              :         // 初始化重置changeLinkInfo
    8598            0 :         changeLinkInfo.remoteRankNum = 0;
    8599            0 :         needCheckBackupNic_ = false;
    8600            0 :         needCheckDefaultNic_ = false;
    8601              :         // 初始化重置remoteRankNicStatus_
    8602            0 :         (void)memset_s(remoteRankNicStatus_, sizeof(remoteRankNicStatus_), 0, sizeof(remoteRankNicStatus_));
    8603              : 
    8604            0 :         for (auto resIt : resMap_) {
    8605            0 :             for (auto &levelNSubCommTransport : resIt.second.opTransportResponse) {
    8606            0 :                 for (auto &singleSubCommTransport : levelNSubCommTransport) {
    8607            0 :                     for (auto &transportRequest : singleSubCommTransport.transportRequests) {
    8608            0 :                         if (transportRequest.isValid && transportRequest.isUsedRdma) { // 仅RDMA链路需要刷新
    8609            0 :                             CHK_RET(SetSingleLinkInfo(switchRanks, transportRequest.remoteUserRank, changeLinkInfo));
    8610              :                         }
    8611              :                     }
    8612              :                 }
    8613              :             }
    8614            0 :         }
    8615            0 :         return HCCL_SUCCESS;
    8616              :     }
    8617              : 
    8618            0 :     HcclResult HcclCommunicator::ActiveStoppedLink(std::map<u32, bool> &remoteRankPortMap,
    8619              :                                                    OpCommTransport &opTransportResponse, bool isBackup)
    8620              :     {
    8621            0 :         for (auto &levelNSubCommTransport : opTransportResponse) {
    8622            0 :             for (auto &singleSubCommTransport : levelNSubCommTransport) {
    8623            0 :                 if (singleSubCommTransport.status.size() == 0) {
    8624            0 :                     continue;
    8625              :                 }
    8626            0 :                 if (singleSubCommTransport.status.size() != singleSubCommTransport.transportRequests.size()
    8627            0 :                     || singleSubCommTransport.links.size() != singleSubCommTransport.transportRequests.size()) {
    8628            0 :                     HCCL_ERROR("[HcclCommunicator][ActiveStoppedLink] comm identifier[%s], local rank[%u], "
    8629              :                                "status num[%u] or links num[%u] is inconsistent with transport request num[%u]. "
    8630              :                                "Please check whether the resources are allocated correctly.",
    8631              :                                identifier_.c_str(), userRank_, singleSubCommTransport.status.size(),
    8632              :                                singleSubCommTransport.links.size(), singleSubCommTransport.transportRequests.size());
    8633            0 :                     return HCCL_E_INTERNAL;
    8634              :                 }
    8635              : 
    8636            0 :                 for (size_t i = 0; i < singleSubCommTransport.transportRequests.size(); i++) {
    8637            0 :                     auto &transportRequest = singleSubCommTransport.transportRequests[i];
    8638            0 :                     auto remoteRankIter = remoteRankPortMap.find(transportRequest.remoteUserRank);
    8639            0 :                     bool needLink = transportRequest.isValid && transportRequest.isUsedRdma && remoteRankIter != remoteRankPortMap.end()
    8640            0 :                         && (remoteRankIter->second ^ isBackup);
    8641              :                     // STOP状态的Transport需要唤醒,重置位到READY
    8642            0 :                     if (needLink && singleSubCommTransport.status[i] == TransportStatus::STOP) {
    8643            0 :                         HCCL_INFO("[HcclCommunicator][ActiveStoppedLink] comm identifier[%s], local rank[%u], "
    8644              :                                   "resuming link of remote rank[%u]",
    8645              :                                   identifier_.c_str(), userRank_,
    8646              :                                   transportRequest.remoteUserRank);
    8647            0 :                         CHK_RET(singleSubCommTransport.links[i]->Resume());
    8648            0 :                         singleSubCommTransport.status[i] = TransportStatus::READY;
    8649              :                     }
    8650              :                 }
    8651              :             }
    8652              :         }
    8653            0 :         return HCCL_SUCCESS;
    8654              :     }
    8655              : 
    8656            0 :     HcclResult HcclCommunicator::PrepareLinkForSwitchNic(std::unordered_map<u32, bool> &switchRanks,
    8657              :                                                          ChangeLinkInfo &changeLinkInfo)
    8658              :     {
    8659            0 :         CHK_RET(SetRemoteRankLinkInfo(switchRanks, changeLinkInfo));
    8660              : 
    8661            0 :         std::map<u32, bool> remoteRankPortMap;
    8662            0 :         for (u32 i = 0; i < changeLinkInfo.remoteRankNum; i++) {
    8663            0 :             remoteRankPortMap.emplace(changeLinkInfo.remoteRankList[i], changeLinkInfo.isUseDefaultPort[i]);
    8664              :         }
    8665            0 :         for (auto resIt : resMap_) {
    8666            0 :             CHK_RET(ActiveStoppedLink(remoteRankPortMap, resIt.second.opTransportResponse, false));
    8667            0 :             CHK_RET(ActiveStoppedLink(remoteRankPortMap, resIt.second.opTransportResponseBackUp, true));
    8668            0 :         }
    8669            0 :         return HCCL_SUCCESS;
    8670            0 :     }
    8671              : 
    8672            0 :     HcclResult HcclCommunicator::ParseSwitchRanks(uint32_t nRanks, uint32_t *ranks, bool *useBackup,
    8673              :                                                   std::unordered_map<u32, bool> &switchRanks)
    8674              :     {
    8675            0 :         CHK_PTR_NULL(ranks);
    8676            0 :         CHK_PTR_NULL(useBackup);
    8677            0 :         switchRanksNum_ = nRanks;
    8678            0 :         (void)memset_s(switchRankList_, sizeof(switchRankList_), 0, sizeof(switchRankList_));
    8679            0 :         (void)memset_s(switchUseBackup_, sizeof(switchUseBackup_), 0, sizeof(switchUseBackup_));
    8680            0 :         s32 ret = memcpy_s(switchRankList_, sizeof(switchRankList_), ranks, sizeof(u32) * nRanks);
    8681            0 :         CHK_PRT_RET(ret != EOK, HCCL_ERROR("[HcclCommunicator][ParseSwitchRanks] mem copy switch ranks fail."),
    8682              :                     HCCL_E_INTERNAL);
    8683            0 :         ret = memcpy_s(switchUseBackup_, sizeof(switchUseBackup_), useBackup, sizeof(bool) * nRanks);
    8684            0 :         CHK_PRT_RET(ret != EOK, HCCL_ERROR("[HcclCommunicator][ParseSwitchRanks] mem copy switch use backup fail."),
    8685              :                     HCCL_E_INTERNAL);
    8686              : 
    8687            0 :         std::string switchRankStr{};
    8688            0 :         for (uint32_t i = 0; i < nRanks; i++) {
    8689            0 :             CHK_PTR_NULL(ranks + i);
    8690            0 :             CHK_PTR_NULL(useBackup + i);
    8691            0 :             uint32_t switchRankId = ranks[i];
    8692            0 :             bool backup = useBackup[i];
    8693            0 :             CHK_PRT_RET(switchRankId >= userRankSize_,
    8694              :                         HCCL_ERROR("[HcclCommunicator][ParseSwitchRanks] invalid switchRankId[%u], "
    8695              :                                    "which should not be greater than rankSize[%u]",
    8696              :                                    switchRankId, userRankSize_),
    8697              :                         HCCL_E_PARA);
    8698            0 :             CHK_PRT_RET(switchRanks.find(switchRankId) != switchRanks.end(),
    8699              :                         HCCL_ERROR("[HcclCommunicator][ParseSwitchRanks] duplicated switchRankId[%u]", switchRankId), HCCL_E_PARA);
    8700            0 :             switchRanks.emplace(switchRankId, backup);
    8701            0 :             switchRankStr += std::to_string(switchRankId) + ":" + std::to_string(backup) + ";";
    8702              :         }
    8703            0 :         HCCL_RUN_INFO("[HcclCommunicator][ParseSwitchRanks] comm identifier[%s], userRank[%u], load switchRanks:%s.",
    8704              :                       identifier_.c_str(), userRank_, switchRankStr.c_str());
    8705            0 :         return HCCL_SUCCESS;
    8706            0 :     }
    8707              : 
    8708            0 :     HcclResult HcclCommunicator::SwitchNic(uint32_t nRanks, uint32_t *ranks, bool *useBackup,
    8709              :                                            std::shared_ptr<HDCommunicate> &controlH2D, std::shared_ptr<HDCommunicate> &statusD2H)
    8710              :     {
    8711            0 :         HcclResult ret = HCCL_SUCCESS;
    8712            0 :         CHK_PRT_RET(!IsEnableBackupLink(), HCCL_RUN_WARNING("[HcclCommunicator][%s]Backup link is not enabled, "
    8713              :             "switch nic will not be prorocessed, comm identifier[%s], rank[%u], devType[%u], opretry enable[%u], "
    8714              :             "backup ip valid[%u], roce enable[%u].", __func__, identifier_.c_str(), userRank_, deviceType_,
    8715              :             GetAicpuUnfoldConfig() && commConfig_.GetConfigInterSuperPodRetryEnable(),
    8716              :             !devBackupIpAddr_[0].IsInvalid(), IsEnableRoce()), HCCL_SUCCESS);
    8717            0 :         CHK_PRT_RET(resMap_.empty(), HCCL_ERROR("[HcclCommunicator][%s] "
    8718              :             "no collective operation has been executed in this communication[%s] on rank[%u], "
    8719              :             "which does not support to set working device nic.", __func__, identifier_.c_str(), userRank_),
    8720              :             HCCL_E_PARA);
    8721            0 :         std::unordered_map<u32, bool> switchRanks;
    8722            0 :         ChangeLinkInfo changeLinkInfo;
    8723            0 :         ret = ParseSwitchRanks(nRanks, ranks, useBackup, switchRanks);
    8724            0 :         if (ret == HCCL_SUCCESS) {
    8725            0 :             ret = PrepareLinkForSwitchNic(switchRanks, changeLinkInfo);
    8726              :         }
    8727            0 :         changeLinkInfo.isChangeLinkFlag = ret == HCCL_SUCCESS; // 如果入参校验失败,则无需刷新链路;通知aicpu侧,防止其他卡超时等待
    8728              : 
    8729            0 :         switchNicWaitingResult_ = false;
    8730              : 
    8731            0 :         u32 changeLinkInfoStart = sizeof(KfcCommand) + sizeof(BackgroundCommand) + sizeof(HcclComSuspendingFlag) +
    8732              :                                   sizeof(HcclOpIdentifier);
    8733            0 :         CHK_RET(controlH2D->Put(changeLinkInfoStart, sizeof(ChangeLinkInfo),
    8734              :                                 reinterpret_cast<uint8_t *>(&changeLinkInfo)));
    8735              : 
    8736            0 :         KfcCommand switchNicCommand = KfcCommand::kSwitchNic;
    8737            0 :         CHK_RET(controlH2D->Put(0, sizeof(KfcCommand), reinterpret_cast<uint8_t *>(&switchNicCommand)));
    8738              : 
    8739            0 :         KfcExecStatus switchStatus;
    8740            0 :         switchStatus.execStatus.kfcStatus = KfcStatus::kNull;
    8741            0 :         u32 waitSwitchExecCmdTimeout = static_cast<u32>(GetExternalInputHcclLinkTimeOut() * 1000 * 2.5f);
    8742            0 :         auto waitSwitchExecCmdTimeoutMs = std::chrono::milliseconds(waitSwitchExecCmdTimeout); // 等待2.5倍的建链超时时间,给快慢卡场景提供冗余
    8743            0 :         auto startTime = std::chrono::steady_clock::now();
    8744              :         while (true) {
    8745            0 :             if (switchNicWaitingResult_) {
    8746            0 :                 CHK_RET(statusD2H->Get(0, sizeof(KfcExecStatus), reinterpret_cast<uint8_t *>(&switchStatus)));
    8747              :             }
    8748            0 :             if (switchStatus.execStatus.kfcStatus == KfcStatus::kSwitchSuccess) {
    8749            0 :                 HCCL_INFO("[HcclCommunicator][%s] comm identifier[%s], devicePhyId[%u], userRank[%u] switch nic success.",
    8750              :                           __func__, identifier_.c_str(), devicePhyId_, userRank_);
    8751            0 :                 ret = HCCL_SUCCESS;
    8752            0 :                 break;
    8753            0 :             } else if (switchStatus.execStatus.kfcStatus == KfcStatus::kSwitchFail) {
    8754            0 :                 HCCL_ERROR("[HcclCommunicator][%s] comm identifier[%s], devicePhyId[%u], userRank[%u] switch nic fail.",
    8755              :                            __func__, identifier_.c_str(), devicePhyId_, userRank_);
    8756            0 :                 ret = HCCL_E_INTERNAL;
    8757            0 :                 break;
    8758            0 :             }  else if ((std::chrono::steady_clock::now() - startTime) >= waitSwitchExecCmdTimeoutMs) {
    8759            0 :                 HCCL_ERROR("[HcclCommunicator][%s] comm identifier[%s], devicePhyId[%u], "
    8760              :                            "userRank[%u] switch nic timeout[%u ms], the transport status is undefined. "
    8761              :                            "Please search log with keyword [ErrToWarn] for detail.",
    8762              :                            __func__, identifier_.c_str(), devicePhyId_, userRank_, waitSwitchExecCmdTimeout);
    8763            0 :                 ret = HCCL_E_TIMEOUT;
    8764            0 :                 break;
    8765              :             } else {
    8766            0 :                 SaluSleep(ONE_MILLISECOND_OF_USLEEP);
    8767              :             }
    8768            0 :         }
    8769            0 :         KfcExecControl clearCommand{};
    8770            0 :         CHK_RET(controlH2D->Put(0, sizeof(KfcExecControl), reinterpret_cast<uint8_t *>(&clearCommand)));
    8771            0 :         KfcExecStatus clearStatus{};
    8772            0 :         CHK_RET(controlH2D->Put(0, sizeof(KfcExecStatus), reinterpret_cast<uint8_t *>(&clearStatus)));
    8773            0 :         switchRanksNum_ = 0;
    8774            0 :         return ret;
    8775            0 :     }
    8776              : 
    8777            0 :     HcclResult HcclCommunicator::GetSwitchRanks(u32 *distSwitchRankList, bool *distSwitchUseBackup, u32 &distSwitchRankNum,
    8778              :                                                 u8 *distRemoteRankNicStatus, u32 &distNicStatusNum, bool &needCheckDefaultNic, bool &needCheckBackupNic)
    8779              :     {
    8780            0 :         s32 ret = memcpy_s(distSwitchRankList, sizeof(u32) * AICPU_MAX_RANK_NUM, switchRankList_,
    8781            0 :                            sizeof(u32) * switchRanksNum_);
    8782            0 :         CHK_PRT_RET(ret != EOK, HCCL_ERROR("[HcclCommunicator][GetSwitchRanks] mem copy switch rank list fail, ret[%u].", ret), HCCL_E_INTERNAL);
    8783            0 :         ret = memcpy_s(distSwitchUseBackup, sizeof(bool) * AICPU_MAX_RANK_NUM, switchUseBackup_,
    8784            0 :                        sizeof(bool) * switchRanksNum_);
    8785            0 :         CHK_PRT_RET(ret != EOK, HCCL_ERROR("[HcclCommunicator][GetSwitchRanks] mem copy switch use backup fail, ret[%u].", ret), HCCL_E_INTERNAL);
    8786            0 :         distSwitchRankNum = switchRanksNum_;
    8787            0 :         ret = memcpy_s(distRemoteRankNicStatus, sizeof(u8) * AICPU_MAX_RANK_NUM, remoteRankNicStatus_,
    8788            0 :                        sizeof(u8) * userRankSize_);
    8789            0 :         CHK_PRT_RET(ret != EOK, HCCL_ERROR("[HcclCommunicator][GetSwitchRanks] mem copy remote rank nic status fail, "
    8790              :                                            "ret[%u].",
    8791              :                                            ret),
    8792              :                     HCCL_E_INTERNAL);
    8793            0 :         distNicStatusNum = userRankSize_;
    8794            0 :         needCheckDefaultNic = needCheckDefaultNic_;
    8795            0 :         needCheckBackupNic = needCheckBackupNic_;
    8796            0 :         switchNicWaitingResult_ = true;
    8797            0 :         return HCCL_SUCCESS;
    8798              :     }
    8799              : 
    8800            0 :     HcclResult GetCannPath(const char *binPath, std::string &cannPath)
    8801              :     {
    8802            0 :         CHK_PRT_RET(binPath == nullptr,
    8803              :             HCCL_ERROR("[HcclCommunicator][GetCannPath]binary path is nullptr"),
    8804              :             HCCL_E_PTR);
    8805              : 
    8806            0 :         std::string tmpPath(binPath); // 存放cann安装路径
    8807            0 :         std::string libraryPath;
    8808            0 :         HcclResult ret = ParseLibraryPath(libraryPath);
    8809            0 :         CHK_PRT_RET(ret != HCCL_SUCCESS,
    8810              :             HCCL_ERROR("[GetCannPath]errNo[0x%016llx]parse path fail.", ret), ret);
    8811              : 
    8812            0 :         ret = GetKeyWordPath(libraryPath, "/hccl", tmpPath);
    8813            0 :         CHK_PRT_RET(ret != HCCL_SUCCESS,
    8814              :             HCCL_ERROR("[GetCannPath]cannot found version file in %s.", libraryPath.c_str()),
    8815              :             HCCL_E_PARA);
    8816            0 :         tmpPath += binPath;
    8817            0 :         cannPath = tmpPath;
    8818              : 
    8819            0 :         return HCCL_SUCCESS;
    8820            0 :     }
    8821              : 
    8822          520 :     HcclResult HcclCommunicator::LoadCustomFile(const char *binPath, aclrtBinaryLoadOptionType optionType, uint32_t cpuKernelMode,
    8823              :                                                 aclrtBinHandle &binHandle)
    8824              :     {
    8825          520 :         binHandle = nullptr;
    8826          520 :         HCCL_INFO("[LoadCustomFile]binPath[%s], optionType[%u], cpuKernelMode[%u]", binPath, optionType, cpuKernelMode);
    8827          520 :         return HCCL_SUCCESS;
    8828              :     }
    8829              : 
    8830            0 :     void HcclCommunicator::UnloadBinary(aclrtBinHandle &binHandle)
    8831              :     {
    8832            0 :         if (binHandle != nullptr) {
    8833            0 :             aclError ret = aclrtBinaryUnLoad(binHandle);
    8834            0 :             if (ret != ACL_SUCCESS) {
    8835            0 :                 HCCL_ERROR("[UnloadBinary]errNo[0x%016llx] unload binary from file error.", ret);
    8836              :             }
    8837            0 :             binHandle = nullptr;
    8838              :         }
    8839            0 :         return;
    8840              :     }
    8841              : 
    8842            0 :     HcclResult HcclCommunicator::RegisterCommUserMem(void* addr, u64 size, void **handle)
    8843              :     {
    8844              :         // user mem和ccl buffer互斥,不支持同时创建
    8845            0 :         if (deviceType_ != DevType::DEV_TYPE_910_93 || superPodNum_ > 1 || isUserMemRegisted_ ||
    8846            0 :             cclBufferManager_.GetInCCLbuffer().ptr() != nullptr) {
    8847            0 :             HCCL_ERROR("[HcclCommunicator][%s]Registration user mem is not supported with the params. "
    8848              :                 "Device type[%d], superPodNum[%u]; Or user mem/CCL buffer has already registered, addr[%p], "
    8849              :                 "isUserMemRegisted[%d]", __func__, deviceType_, superPodNum_, addr, isUserMemRegisted_);
    8850            0 :             return HCCL_E_NOT_SUPPORT;
    8851              :         }
    8852              :         // DeviceMem::create创建的DeviceMem对象为拷贝构造,析构时不释放内存,内存由上层管理
    8853            0 :         DeviceMem userMem =  DeviceMem::create(addr, size);
    8854            0 :         std::shared_ptr<DeviceMem> userMemPtr = nullptr;
    8855            0 :         EXCEPTION_CATCH((userMemPtr = std::make_shared<DeviceMem>(std::move(userMem))), return HCCL_E_PTR);
    8856            0 :         *handle = static_cast<void *>(userMemPtr.get());
    8857            0 :         userMemMap_.insert(std::make_pair(*handle, userMemPtr));
    8858            0 :         HCCL_INFO("[HcclCommunicator][%s]Register user mem success, group[%s], handle[%p], addr[%llu], size[%llu]",
    8859              :             __func__, identifier_.c_str(), *handle, reinterpret_cast<uint64_t>(addr), size);
    8860            0 :         isUserMemRegisted_ = true;
    8861            0 :         return HCCL_SUCCESS;
    8862            0 :     }
    8863              : 
    8864            0 :     HcclResult HcclCommunicator::DeregisterCommUserMem(void* handle)
    8865              :     {
    8866            0 :         if (deviceType_ != DevType::DEV_TYPE_910_93 || superPodNum_ > 1) {
    8867            0 :             HCCL_ERROR("[HcclCommunicator][%s]Unsupported on the device type[%d] or superPodNum[%u]", __func__,
    8868              :                 deviceType_, superPodNum_);
    8869            0 :             return HCCL_E_NOT_SUPPORT;
    8870              :         }
    8871              : 
    8872            0 :         CHK_PRT_RET(!userMemMap_.erase(handle),
    8873              :             HCCL_RUN_WARNING("[HcclCommunicator][%s]Mem is not exist, handle[%p]", __func__, handle), HCCL_SUCCESS);
    8874              : 
    8875              :         // 重置user mem和userMemType
    8876            0 :         CHK_SAFETY_FUNC_RET(memset_s(opResPara_.userMemRes, sizeof(opResPara_.userMemRes), 0,
    8877              :             sizeof(opResPara_.userMemRes)));
    8878            0 :         opResPara_.userMemType = 0;  // CCL Buffer
    8879            0 :         isUserMemRegisted_ = false;
    8880            0 :         HCCL_INFO("[HcclCommunicator][%s]Deregister mem success, group[%s], handle[%p]", __func__,
    8881              :             identifier_.c_str(), handle);
    8882            0 :         return HCCL_SUCCESS;
    8883              :     }
    8884              : 
    8885            0 :     HcclResult HcclCommunicator::ExchangeCommUserMem(void* handle, std::vector<u32>& peerRanks)
    8886              :     {
    8887            0 :         if (deviceType_ != DevType::DEV_TYPE_910_93 || superPodNum_ > 1 || GetExternalInputInterHccsDisable()) {
    8888            0 :             HCCL_ERROR("[HcclCommunicator][%s]Unsupported configuration: device type[%d], superPodNum[%u], "
    8889              :                 "or RDMA usage", __func__, deviceType_, superPodNum_);
    8890            0 :             return HCCL_E_NOT_SUPPORT;
    8891              :         }
    8892              : 
    8893            0 :         if ((peerRanks.size() > rankInfoList_.size())) {
    8894            0 :             HCCL_ERROR("[HcclCommunicator][%s]Invalid peerRanksNum[%u], which should be less than communicator "
    8895              :                 "rank nums[%u]", __func__, peerRanks.size(), rankInfoList_.size());
    8896            0 :             return HCCL_E_PARA;
    8897              :         }
    8898              :         // 获取user mem,调exchange接口前需要先调注册接口注册user mem
    8899            0 :         if (userMemMap_.find(handle) == userMemMap_.end()) {
    8900            0 :             HCCL_ERROR("[HcclCommunicator][%s]Find user mem failed, handle[%p] is not registered", __func__, handle);
    8901            0 :             return HCCL_E_NOT_FOUND;
    8902              :         }
    8903            0 :         DeviceMem userMem = *userMemMap_[handle].get();
    8904            0 :         CHK_PTR_NULL(userMem.ptr());
    8905              :         // 构造建链param
    8906            0 :         TransportIOMem transMem;
    8907            0 :         transMem.userMem = userMem;
    8908            0 :         OpCommTransport opCommTransport;
    8909            0 :         LevelNSubCommTransport level0Transport;
    8910            0 :         SingleSubCommTransport commTransport;
    8911              : 
    8912            0 :         for (u32 rankIdx = 0; rankIdx < peerRanks.size(); rankIdx++) {
    8913            0 :             TransportRequest tmpTransport;
    8914            0 :             if (userRank_ != peerRanks[rankIdx]) {
    8915            0 :                 tmpTransport.isValid = true;
    8916            0 :                 tmpTransport.localUserRank = userRank_;
    8917            0 :                 tmpTransport.remoteUserRank = peerRanks[rankIdx];
    8918            0 :                 tmpTransport.inputMemType = TransportMemType::USER_MEM;
    8919            0 :                 tmpTransport.outputMemType = TransportMemType::USER_MEM;
    8920              :             } else {
    8921              :                 // 本rank不需要创建transport
    8922            0 :                 tmpTransport.isValid = false;
    8923              :             }
    8924            0 :             commTransport.transportRequests.push_back(tmpTransport);
    8925              :         }
    8926            0 :         level0Transport.push_back(commTransport);
    8927            0 :         opCommTransport.push_back(level0Transport);
    8928            0 :         ClearOpTransportResponseLinks(opCommTransport);
    8929              :         // 建链
    8930            0 :         constexpr char EXCHANGE_USER_MEM_TAG_PREFIX[] = "ExchangeUserMem_";
    8931            0 :         string tag = EXCHANGE_USER_MEM_TAG_PREFIX + identifier_;
    8932            0 :         HcclResult ret = HCCL_SUCCESS;
    8933              :         {
    8934            0 :             StateGuard<HcclCommunicator, HcclCommState> guard(this, HcclCommState::BUILDING);
    8935            0 :             HCCL_RUN_INFO("[%s]Alloc transport, level size[%u], trans request size[%u], mem ptr[%p], mem size[%llu]",
    8936              :                 __func__, opCommTransport.size(), commTransport.transportRequests.size(), userMem.ptr(),
    8937              :                 userMem.size());
    8938            0 :             CHK_PTR_NULL(transportManager_);
    8939            0 :             ret = transportManager_->Alloc(tag, transMem, opCommTransport, false);
    8940            0 :         }
    8941            0 :         CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[%s]Alloc transports failed, tag[%s]", __func__, tag.c_str()),
    8942              :             ret);
    8943            0 :         userMemTransport_ = opCommTransport;
    8944            0 :         return HCCL_SUCCESS;
    8945            0 :     }
    8946            0 :     HcclResult HcclCommunicator::GetLocalCCLBuf(void **addr, uint64_t *size)
    8947              :     {
    8948            0 :         uint64_t cclbufSize = cclBufferManager_.GetInCCLbufferSize() + cclBufferManager_.GetOutCCLbufferSize() + cclBufferManager_.GetExpBufferSize();
    8949            0 :         *addr = cclBufferManager_.GetCommCCLBuffer().ptr();
    8950            0 :         if (nullptr == cclBufferManager_.GetCommCCLBuffer().ptr()) {
    8951            0 :             cclbufSize = 0;
    8952              :         }
    8953            0 :         *size = cclbufSize;
    8954            0 :         HCCL_INFO("[%s] GetlocalCCLBuf success, addr[%p], size[%u]", identifier_.c_str(), cclBufferManager_.GetCommCCLBuffer().ptr(), cclbufSize);
    8955            0 :         return HCCL_SUCCESS;
    8956              :     }
    8957              : 
    8958            2 :     HcclResult HcclCommunicator::GetRemoteCCLBuf(uint32_t remoteRank, void **addr, uint64_t *size)
    8959              :     {
    8960            2 :         CHK_PRT_RET((remoteRank >= AICPU_MAX_RANK_NUM),
    8961              :             HCCL_ERROR("[%s] invalid remoteRank[%d]", __func__, remoteRank), HCCL_E_PARA);
    8962              :         //仅sdma场景
    8963            1 :         uint64_t cclbufSize = cclBufferManager_.GetInCCLbufferSize() + cclBufferManager_.GetOutCCLbufferSize() + cclBufferManager_.GetExpBufferSize();
    8964            1 :         *addr = p2pCclBuf_[remoteRank];
    8965              : 
    8966            1 :         if (nullptr == p2pCclBuf_[remoteRank]) {
    8967            1 :             cclbufSize = 0;
    8968              :         }
    8969            1 :         *size = cclbufSize;
    8970            1 :         HCCL_INFO("[%s] GetRemoteCCLBuf success, remoteRank[%u], addr[%p], size[%u]", identifier_.c_str(), remoteRank, p2pCclBuf_[remoteRank], cclbufSize);
    8971            1 :         return HCCL_SUCCESS;
    8972              :     }
    8973            0 :     HcclResult HcclCommunicator::GetKFCWorkSpace(void **addr, uint64_t *size)
    8974              :     {
    8975            0 :         *addr = workSpace_.ptr();
    8976            0 :         *size = workSpaceSize_;
    8977            0 :         HCCL_INFO("[%s] GetKFCWorkSpace success, addr[%p], size[%u]", identifier_.c_str(), workSpace_.ptr(), workSpaceSize_);
    8978            0 :         return HCCL_SUCCESS;
    8979              :     }
    8980            4 :     HcclResult HcclCommunicator::IndOpTransportAlloc(const std::string &tag, OpCommTransport &opCommTransport,
    8981              :         TransportIOMem& transMem, bool isAicpuModeEn)
    8982              :     {
    8983              :         // Aicpu侧不支持用户注册额外内存
    8984            4 :         if (isAicpuModeEn) {
    8985            3 :             if (transMem.indOpMem.userDeviceMem.size() > 0 ||
    8986            1 :                 transMem.indOpMem.userHostMem.size() > 0) {
    8987            2 :                 HCCL_ERROR("[%s] AICPU engine does not support user-registered memory", __func__);
    8988            2 :                 return HCCL_E_NOT_SUPPORT;
    8989              :             }
    8990              :         }
    8991              : 
    8992            2 :         StateGuard<HcclCommunicator, HcclCommState> guard(this, HcclCommState::BUILDING);
    8993            2 :         CHK_PTR_NULL(indptOpTransportManager_);
    8994            1 :         bool isIndOp = true;
    8995            1 :         HcclResult ret = indptOpTransportManager_->Alloc(tag, transMem, opCommTransport, isAicpuModeEn, false, false,
    8996            1 :             HcclCMDType::HCCL_CMD_INVALID, false, isIndOp);
    8997            1 :         if (ret != HCCL_SUCCESS) {
    8998            1 :             HCCL_ERROR("[%s] Failed to alloc transport, tag[%s], isAicpuModeEn[%d], ret[%d]",
    8999              :                 __func__, tag.c_str(), isAicpuModeEn, ret);
    9000            1 :             return ret;
    9001              :         }
    9002              : 
    9003            0 :         HCCL_RUN_INFO("[%s] Alloc transport success, tag[%s], isAicpuModeEn[%d], ret[%d]",
    9004              :             __func__, tag.c_str(), isAicpuModeEn, ret);
    9005            0 :         return HCCL_SUCCESS;
    9006            2 :     }
    9007              : 
    9008          234 :     HcclTopoAttr HcclCommunicator::GetTopoAttr()
    9009              :     {
    9010          234 :         HcclTopoAttr topoAttr;
    9011          234 :         attrCollector_.GetTopoAttr(topoAttr);
    9012          234 :         return topoAttr;
    9013            0 :     }
    9014              : 
    9015          102 :     HcclResult HcclCommunicator::GetHDCommunicate(HDCommunicateParams &kfcControlTransferH2DParams,
    9016              :         HDCommunicateParams &kfcStatusTransferD2HParams)
    9017              :     {
    9018          102 :         if (GetSupportHDCommunicate() == false) {
    9019          101 :             HCCL_WARNING("%s not support HDCommunicate, skip", __func__);
    9020          101 :             return HCCL_SUCCESS;
    9021              :         }
    9022            1 :         CHK_SMART_PTR_NULL(kfcControlTransferH2D_);
    9023            1 :         CHK_SMART_PTR_NULL(kfcStatusTransferD2H_);
    9024            1 :         kfcControlTransferH2DParams = kfcControlTransferH2D_->GetCommunicateParams();
    9025            1 :         kfcStatusTransferD2HParams = kfcStatusTransferD2H_->GetCommunicateParams();
    9026            1 :         HCCL_INFO("%s success, group[%s]", __func__, identifier_.c_str());
    9027            1 :         return HCCL_SUCCESS;
    9028              :     }
    9029              : 
    9030          234 :     HcclResult HcclCommunicator::SetGetAicpuCommState(std::function<bool()> getAicpuCommState)
    9031              :     {
    9032          234 :         getAicpuCommState_ = getAicpuCommState;
    9033          234 :         HCCL_DEBUG("%s success, group[%s]", __func__, identifier_.c_str());
    9034          234 :         return HCCL_SUCCESS;
    9035              :     }
    9036              : 
    9037            0 :     HcclResult HcclCommunicator::CommGetNetLayers(uint32_t **netLayers, uint32_t *netLayerNum)
    9038              :     {
    9039            0 :         if (deviceType_ == DevType::DEV_TYPE_910_93) {
    9040            0 :             netLayer_[0] = static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0);
    9041            0 :             netLayer_[1] = static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L1);
    9042            0 :             *netLayerNum = COMM_LAYER_NUM_MAX;
    9043            0 :         } else if (deviceType_ == DevType::DEV_TYPE_910B || deviceType_ == DevType::DEV_TYPE_310P3) {
    9044            0 :             netLayer_[0] =static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0);
    9045            0 :             *netLayerNum = 1;
    9046              :         }
    9047            0 :         *netLayers = netLayer_;
    9048            0 :         return HCCL_SUCCESS;
    9049              :     }
    9050              : 
    9051            0 :     HcclResult HcclCommunicator::CommGetInstSizeByNetLayer(uint32_t netLayer, uint32_t *rankNum)
    9052              :     {
    9053            0 :         if ((netLayer == static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)) ||
    9054              :             (netLayer == static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L1))) {
    9055            0 :                 *rankNum = userRankSize_;
    9056              :             }
    9057            0 :         return HCCL_SUCCESS;
    9058              :     }
    9059              : 
    9060            0 :     HcclResult HcclCommunicator::CommGetInstTopoTypeByNetLayer(uint32_t netLayer, u32 *topoType)
    9061              :     {
    9062            0 :         if (deviceType_ == DevType::DEV_TYPE_910_93) {
    9063            0 :             if (netLayer == static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)) {
    9064            0 :                 *topoType = HCCL_ALG_SWITCH | HCCL_ALG_RING;
    9065            0 :             } else if (netLayer == static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L1)) {
    9066            0 :                 *topoType = HCCL_ALG_RING;
    9067              :             }
    9068            0 :         } else if (deviceType_ == DevType::DEV_TYPE_910B) {
    9069            0 :             if (netLayer == static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)) {
    9070            0 :                 *topoType = HCCL_ALG_MESH;
    9071              :             }
    9072            0 :         } else if (deviceType_ == DevType::DEV_TYPE_310P3) {
    9073            0 :             if (netLayer == static_cast<uint32_t>(HcclTopoLevel::HCCL_TOPO_L0)) {
    9074            0 :                 *topoType = HCCL_ALG_RING;
    9075              :             }
    9076              :         }
    9077            0 :         return HCCL_SUCCESS;
    9078              :     }
    9079              : 
    9080            0 :     HcclResult HcclCommunicator::GetNetLayers(uint32_t **netLayers, uint32_t *netLayerNum)
    9081              :     {
    9082            0 :         return rankGraph_.GetNetLayers(netLayers, netLayerNum);
    9083              :     }
    9084              :     
    9085            0 :     HcclResult HcclCommunicator::GetInstSizeByNetLayer(uint32_t netLayer, uint32_t *rankNum)
    9086              :     {
    9087            0 :         return rankGraph_.GetInstSizeByNetLayer(netLayer, rankNum);
    9088              :     }
    9089              :     
    9090          520 :     HcclResult HcclCommunicator::GetInstTopoTypeByNetLayer(uint32_t netLayer, CommTopo *topoType)
    9091              :     {
    9092          520 :         return rankGraph_.GetInstTopoTypeByNetLayer(netLayer, topoType);
    9093              :     }
    9094              : 
    9095            0 :     HcclResult HcclCommunicator::GetInstRanksByNetLayer(uint32_t netLayer, uint32_t **rankList, uint32_t *rankNum)
    9096              :     {
    9097            0 :         return rankGraph_.GetInstRanksByNetLayer(netLayer, rankList, rankNum);
    9098              :     }
    9099              :     
    9100            0 :     HcclResult HcclCommunicator::GetInstSizeListByNetLayer(uint32_t netLayer, uint32_t **instSizeList, uint32_t *listSize)
    9101              :     {
    9102            0 :         return rankGraph_.GetInstSizeListByNetLayer(netLayer, instSizeList, listSize);
    9103              :     }
    9104              : 
    9105            0 :     HcclResult HcclCommunicator::GetTopoInstsByLayer(uint32_t netLayer, uint32_t **topoInsts, uint32_t *topoInstNum)
    9106              :     {
    9107            0 :         return rankGraph_.GetTopoInstsByLayer(netLayer, topoInsts, topoInstNum);
    9108              :     }
    9109              : 
    9110            0 :     HcclResult HcclCommunicator::GetTopoType(uint32_t netLayer, uint32_t topoInstId, CommTopo *topoType)
    9111              :     {
    9112            0 :         return rankGraph_.GetTopoType(netLayer, topoType);
    9113              :     }
    9114              : 
    9115            0 :     HcclResult HcclCommunicator::GetRanksByTopoInst(uint32_t netLayer, uint32_t topoInstId, uint32_t **ranks, uint32_t *rankNum)
    9116              :     {
    9117            0 :         return rankGraph_.GetRanksByTopoInst(netLayer, topoInstId, ranks, rankNum);
    9118              :     }
    9119              : 
    9120            0 :     HcclResult HcclCommunicator::GetEndpointNum(uint32_t netLayer, uint32_t topoInstId, uint32_t *num)
    9121              :     {
    9122            0 :         return rankGraph_.GetEndpointNum(netLayer, topoInstId, num);
    9123              :     }
    9124              : 
    9125            0 :     HcclResult HcclCommunicator::GetEndpointDesc(uint32_t netLayer, uint32_t topoInstId, uint32_t *descNum, EndpointDesc *endpointDesc)
    9126              :     {
    9127            0 :         return rankGraph_.GetEndpointDesc(netLayer, topoInstId, descNum, endpointDesc);
    9128              :     }
    9129              : 
    9130            0 :     HcclResult HcclCommunicator::GetEndpointInfo(uint32_t rankId, const EndpointDesc *endPointDesc, EndpointAttr endpointAttr,
    9131              :                                  uint32_t infoLen, void *info)
    9132              :     {
    9133            0 :         return rankGraph_.GetEndpointInfo(rankId, endPointDesc, endpointAttr, infoLen, info);
    9134              :     }
    9135              : 
    9136            0 :     HcclResult HcclCommunicator::GetRankGraph(GraphType type, void **graph, uint32_t *len)
    9137              :     {
    9138            0 :         return rankGraph_.GetRankGraphInfo(type, graph, len);
    9139              :     }
    9140              : 
    9141            0 :     void* HcclCommunicator::GetRankGraphV1()
    9142              :     {
    9143            0 :         return static_cast<void*>(&rankGraph_);
    9144              :     }
    9145              : 
    9146            0 :     HcclResult HcclCommunicator::GetLinks(uint32_t netLayer, uint32_t srcRank, uint32_t dstRank,
    9147              :         CommLink **linkList, uint32_t *listSize)
    9148              :     {
    9149            0 :         return rankGraph_.GetLinks(netLayer, srcRank, dstRank, linkList, listSize);
    9150              :     }
    9151              : 
    9152            0 :     HcclResult HcclCommunicator::GetHeterogMode(HcclHeterogMode *mode)
    9153              :     {
    9154            0 :         return rankGraph_.GetHeterogMode(mode);
    9155              :     }
    9156              : 
    9157          520 :     HcclResult HcclCommunicator::RegisterToSnapshot()
    9158              :     {
    9159          520 :         if (deviceType_ != DevType::DEV_TYPE_910B && deviceType_ != DevType::DEV_TYPE_910_93) {
    9160          324 :             return HCCL_SUCCESS;
    9161              :         }
    9162          196 :         if (userRankSize_ <= 1) {
    9163            2 :             HCCL_RUN_INFO("[HcclCommunicator][RegisterToSnapshot]comm identifier[%s], deviceLogicId[%d], "
    9164              :                 "rank size[%u] is no greater than 1, and then will not register to snapshot",
    9165              :                 identifier_.c_str(), deviceLogicId_, userRankSize_);
    9166            2 :             return HCCL_SUCCESS;
    9167              :         }
    9168            0 :         auto setInvalidCommCallback = [this](bool isInvalid) {
    9169            0 :             return this->SetInvalidComm(isInvalid);
    9170          194 :         };
    9171            0 :         auto preProcessCallback = [this]() {
    9172            0 :             return this->SnapshotCheckPreProcess();
    9173          194 :         };
    9174            0 :         auto postProcessCallback = [this]() {
    9175            0 :             return this->SnapshotCheckPostProcess();
    9176          194 :         };
    9177          194 :         CHK_RET(SnapshotControl::GetInstance(deviceLogicId_).RegisterComm(identifier_, setInvalidCommCallback,
    9178              :             preProcessCallback, postProcessCallback));
    9179          194 :         if (IsEnableBackupLink()) {
    9180            0 :             CHK_RET(SnapshotControl::GetInstance(deviceLogicId_).RegisterBackup(identifier_, deviceBackUpPhyId_));
    9181              :         }
    9182          194 :         return HCCL_SUCCESS;
    9183              :     }
    9184              : 
    9185          212 :     HcclResult HcclCommunicator::UnRegisterFromSnapshot()
    9186              :     {
    9187          212 :         if (deviceType_ != DevType::DEV_TYPE_910B && deviceType_ != DevType::DEV_TYPE_910_93) {
    9188            0 :             return HCCL_SUCCESS;
    9189              :         }
    9190          212 :         if (userRankSize_ <= 1) {
    9191            3 :             HCCL_RUN_INFO("[HcclCommunicator][UnRegisterFromSnapshot]comm identifier[%s], deviceLogicId[%d], "
    9192              :                 "rank size[%u] is no greater than 1, and then will not unregister from snapshot",
    9193              :                 identifier_.c_str(), deviceLogicId_, userRankSize_);
    9194            3 :             return HCCL_SUCCESS;
    9195              :         }
    9196          209 :         CHK_RET(SnapshotControl::GetInstance(deviceLogicId_).UnRegisterComm(identifier_));
    9197          209 :         if (IsEnableBackupLink()) {
    9198            0 :             CHK_RET(SnapshotControl::GetInstance(deviceLogicId_).UnRegisterBackup(identifier_, deviceBackUpPhyId_));
    9199              :         }
    9200          209 :         return HCCL_SUCCESS;
    9201              :     }
    9202              : 
    9203            0 :     HcclResult HcclCommunicator::SetInvalidComm(bool isInvalid) {
    9204            0 :         isInvalidComm_ = isInvalid;
    9205            0 :         HCCL_INFO("[HcclCommunicator][SetInvalidComm] comm[%s] is set to invalid, rank[%u], deviceLogicId[%d]",
    9206              :             identifier_.c_str(), userRank_, deviceLogicId_);
    9207            0 :         return HCCL_SUCCESS;
    9208              :     }
    9209              : 
    9210            0 :     HcclResult HcclCommunicator::SnapshotCheckPreProcess()
    9211              :     {
    9212            0 :         bool errorFlag = false;
    9213            0 :         auto pauseTimeout = std::chrono::seconds(GetExternalInputHcclLinkTimeOut());
    9214            0 :         auto startTime = std::chrono::steady_clock::now();
    9215              :         while (true) {
    9216            0 :             CHK_PRT_BREAK(Heartbeat::GetInstance(deviceLogicId_).IsPaused(),
    9217              :                 HCCL_INFO("[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], deviceLogicId[%d], "
    9218              :                 "heartbeat thread has been paused.", identifier_.c_str(), userRank_, deviceLogicId_),);
    9219            0 :             CHK_PRT_BREAK((std::chrono::steady_clock::now() - startTime) >= pauseTimeout,
    9220              :                 HCCL_ERROR("[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], deviceLogicId[%d], "
    9221              :                 "pause heartbeat thread timeout[%u s].",
    9222              :                 identifier_.c_str(), userRank_, deviceLogicId_, GetExternalInputHcclLinkTimeOut()), errorFlag = true);
    9223              :         }
    9224            0 :         startTime = std::chrono::steady_clock::now();
    9225            0 :         while (retryEnable_ && opRetryManager_) {
    9226            0 :             CHK_PRT_BREAK(opRetryManager_->IsPaused(identifier_),
    9227              :                 HCCL_INFO("[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], deviceLogicId[%d], "
    9228              :                 "opretry threads have been paused.", identifier_.c_str(), userRank_, deviceLogicId_),);
    9229            0 :             CHK_PRT_BREAK((std::chrono::steady_clock::now() - startTime) >= pauseTimeout,
    9230              :                 HCCL_ERROR("[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], deviceLogicId[%d], "
    9231              :                 "pause opretry threads timeout[%u s].",
    9232              :                 identifier_.c_str(), userRank_, deviceLogicId_, GetExternalInputHcclLinkTimeOut()), errorFlag = true);
    9233              :         }
    9234            0 :         startTime = std::chrono::steady_clock::now();
    9235            0 :         while (zeroCopyMemoryAgent_) {
    9236            0 :             CHK_PRT_BREAK(zeroCopyMemoryAgent_->IsPaused(),
    9237              :                 HCCL_INFO("[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], deviceLogicId[%d], "
    9238              :                 "zero-copy memory agent thread has been paused.", identifier_.c_str(), userRank_, deviceLogicId_),);
    9239            0 :             CHK_PRT_BREAK((std::chrono::steady_clock::now() - startTime) >= pauseTimeout,
    9240              :                 HCCL_ERROR("[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], deviceLogicId[%d], "
    9241              :                 "pause zero-copy memory agent thread timeout[%u s].",
    9242              :                 identifier_.c_str(), userRank_, deviceLogicId_, GetExternalInputHcclLinkTimeOut()), errorFlag = true);
    9243              :         }
    9244            0 :         CHK_PRT_RET(errorFlag, HCCL_ERROR("[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], "
    9245              :             "deviceLogicId[%d], snapshot pre-process fail due to some background threads pause timeout, please check.",
    9246              :             identifier_.c_str(), userRank_, deviceLogicId_), HCCL_E_INTERNAL);
    9247            0 :         HCCL_INFO("[HcclCommunicator][SnapshotCheckPreProcess] comm[%s], rank[%u], deviceLogicId[%d], "
    9248              :             "snapshot pre-process success.", identifier_.c_str(), userRank_, deviceLogicId_);
    9249            0 :         return HCCL_SUCCESS;
    9250              :     }
    9251              : 
    9252            0 :     HcclResult HcclCommunicator::SnapshotCheckPostProcess()
    9253              :     {
    9254            0 :         bool errorFlag = false;
    9255            0 :         auto resumeTimeout = std::chrono::seconds(GetExternalInputHcclLinkTimeOut());
    9256            0 :         auto startTime = std::chrono::steady_clock::now();
    9257              :         while (true) {
    9258            0 :             CHK_PRT_BREAK(Heartbeat::GetInstance(deviceLogicId_).IsResumed(),
    9259              :                 HCCL_INFO("[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], deviceLogicId[%d], "
    9260              :                 "heartbeat thread has been resumed.", identifier_.c_str(), userRank_, deviceLogicId_),);
    9261            0 :             CHK_PRT_BREAK((std::chrono::steady_clock::now() - startTime) >= resumeTimeout,
    9262              :                 HCCL_ERROR("[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], deviceLogicId[%d], "
    9263              :                 "resume heartbeat thread timeout[%u s].",
    9264              :                 identifier_.c_str(), userRank_, deviceLogicId_, GetExternalInputHcclLinkTimeOut()), errorFlag = true);
    9265              :         }
    9266            0 :         startTime = std::chrono::steady_clock::now();
    9267            0 :         while (retryEnable_ && opRetryManager_) {
    9268            0 :             CHK_PRT_BREAK(opRetryManager_->IsResumed(identifier_),
    9269              :                 HCCL_INFO("[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], deviceLogicId[%d], "
    9270              :                 "opretry threads have been resumed.", identifier_.c_str(), userRank_, deviceLogicId_),);
    9271            0 :             CHK_PRT_BREAK((std::chrono::steady_clock::now() - startTime) >= resumeTimeout,
    9272              :                 HCCL_ERROR("[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], deviceLogicId[%d], "
    9273              :                 "resume opretry threads timeout[%u s].",
    9274              :                 identifier_.c_str(), userRank_, deviceLogicId_, GetExternalInputHcclLinkTimeOut()), errorFlag = true);
    9275              :         }
    9276            0 :         startTime = std::chrono::steady_clock::now();
    9277            0 :         while (zeroCopyMemoryAgent_) {
    9278            0 :             CHK_PRT_BREAK(zeroCopyMemoryAgent_->IsResumed(),
    9279              :                 HCCL_INFO("[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], deviceLogicId[%d], "
    9280              :                 "zero-copy memory agent thread has been resumed.", identifier_.c_str(), userRank_, deviceLogicId_),);
    9281            0 :             CHK_PRT_BREAK((std::chrono::steady_clock::now() - startTime) >= resumeTimeout,
    9282              :                 HCCL_ERROR("[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], deviceLogicId[%d], "
    9283              :                 "resume zero-copy memory agent thread timeout[%u s].",
    9284              :                 identifier_.c_str(), userRank_, deviceLogicId_, GetExternalInputHcclLinkTimeOut()), errorFlag = true);
    9285              :         }
    9286            0 :         CHK_PRT_RET(errorFlag, HCCL_ERROR("[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], "
    9287              :             "deviceLogicId[%d], snapshot post-process check fail due to some background threads resume timeout, "
    9288              :             "please check.", identifier_.c_str(), userRank_, deviceLogicId_), HCCL_E_INTERNAL);
    9289            0 :         HCCL_INFO("[HcclCommunicator][SnapshotCheckPostProcess] comm[%s], rank[%u], deviceLogicId[%d], "
    9290              :             "snapshot post-process check success.", identifier_.c_str(), userRank_, deviceLogicId_);
    9291            0 :         return HCCL_SUCCESS;
    9292              :     }
    9293              : 
    9294          403 :     void HcclCommunicator::SetReleaseChannel(std::function<HcclResult()> releaseChannel)
    9295              :     {
    9296          403 :         releaseChannel_ = releaseChannel;
    9297          403 :         return;
    9298              :     }
    9299              : 
    9300          234 :     CCLBufferManager& HcclCommunicator::GetCCLbufferManager()
    9301              :     {
    9302          234 :         return cclBufferManager_;
    9303              :     }
    9304              : 
    9305            0 :     void HcclCommunicator::SetHcclQos(u32 hcclQos)
    9306              :         {
    9307            0 :         HCCL_INFO("[HcclCommunicator][host][SetHcclQos] hcclQos[%u]", hcclQos);
    9308            0 :             hcclQos_ = hcclQos;
    9309            0 :         }
    9310              : 
    9311            0 :     u32 HcclCommunicator::GetHcclQos()
    9312              :         {
    9313            0 :         HCCL_INFO("[HcclCommunicator][host][GetHcclQos] hcclQos[%u]", hcclQos_);
    9314            0 :             return hcclQos_;
    9315              :         }
    9316              : 
    9317          529 :     HcclResult HcclCommunicator::InitSymmetricMemory()
    9318              :     {
    9319          529 :         if (superPodNum_ > 1) {
    9320           13 :             HCCL_DEBUG("[InitSymmetricMemory] Cross-SuperNode not support symmetric memory");
    9321           13 :             return HCCL_SUCCESS;
    9322              :         }
    9323          516 :         if (deviceType_ != DevType::DEV_TYPE_910_93) {
    9324          488 :             HCCL_DEBUG("[%s] deviceType:%d not support symmetric memory", __func__, deviceType_);
    9325          488 :             return HCCL_SUCCESS;
    9326              :         }
    9327              :         
    9328           28 :         u64 stride = commConfig_.GetConfigSymmetricMemoryStride() * GIGABYTE_TO_BYTE;
    9329           28 :         HCCL_RUN_INFO("InitSymmetricMemory, comm identifier[%s], userRank[%u], userRankSize[%u], stride[%llu], devicePhyId[%u].",
    9330              :             identifier_.c_str(), realUserRank_, userRankSize_, stride, devicePhyId_);
    9331              :         
    9332           28 :         symmetricMemoryAgent_ = std::make_shared<SymmetricMemoryAgent>(socketManager_, devicePhyId_,
    9333           28 :             deviceLogicId_, localVnicIp_, rankInfoList_, realUserRank_, useSuperPodMode_, identifier_);
    9334           28 :         CHK_SMART_PTR_NULL(symmetricMemoryAgent_);
    9335              : 
    9336           28 :         symmetricMemory_ = std::make_unique<SymmetricMemory>(realUserRank_, userRankSize_, stride, symmetricMemoryAgent_);
    9337           28 :         CHK_SMART_PTR_NULL(symmetricMemory_);
    9338           28 :         return HCCL_SUCCESS;
    9339              :     }
    9340              : 
    9341            0 :     HcclResult HcclCommunicator::RegisterWindow(void* ptr, size_t size, HcclCommSymWindow *winHandle)
    9342              :     {
    9343            0 :         CHK_PRT_RET(superPodNum_ > 1, 
    9344              :             HCCL_ERROR("[RegisterWindow] Cross-SuperNode not support symmetric memory"), HCCL_E_NOT_SUPPORT);
    9345              : 
    9346            0 :         CHK_PRT_RET(deviceType_ != DevType::DEV_TYPE_910_93, 
    9347              :             HCCL_ERROR("[%s] deviceType:%d not support symmetric memory", __func__, deviceType_), HCCL_E_NOT_SUPPORT);
    9348              : 
    9349            0 :         CHK_SMART_PTR_NULL(symmetricMemory_);
    9350            0 :         return symmetricMemory_->RegisterSymmetricMem(ptr, size, winHandle);
    9351              :     }
    9352              : 
    9353            0 :     HcclResult HcclCommunicator::DeregisterWindow(HcclCommSymWindow winHandle)
    9354              :     {
    9355            0 :         CHK_SMART_PTR_NULL(symmetricMemory_);
    9356            0 :         return symmetricMemory_->DeregisterSymmetricMem(winHandle);
    9357              :     }
    9358              : 
    9359            0 :     HcclResult HcclCommunicator::GetCommSymWin(void* ptr, size_t size, HcclCommSymWindow *winHandle, size_t *offset)
    9360              :     {
    9361            0 :         CHK_SMART_PTR_NULL(symmetricMemory_);
    9362            0 :         return symmetricMemory_->FindSymmetricWindow(ptr, size, winHandle, reinterpret_cast<u64*>(offset));
    9363              :     }
    9364              : 
    9365           24 :     bool HcclCommunicator::EnableAicpuUnfold(bool isCapture)
    9366              :     {
    9367           24 :         if (deviceType_ != DevType::DEV_TYPE_910_93 && deviceType_ != DevType::DEV_TYPE_910B) {
    9368            5 :             return false;
    9369              :         }
    9370              :         // 910B在acl graph场景(isCapture)不启用aicpu展开
    9371           19 :         if (deviceType_ == DevType::DEV_TYPE_910B && isCapture) {
    9372            3 :             HCCL_INFO("[%s] deviceType[%d] isCapture[1], aicpuUnfoldConfig[%u] 910B does not support aicpuUnfold in acl graph mode",
    9373              :                 __func__, deviceType_, GetAicpuUnfoldConfig());
    9374            3 :             return false;
    9375              :         }
    9376           16 :         HCCL_INFO("[%s] aicpuUnfoldConfig[%u]", __func__, GetAicpuUnfoldConfig());
    9377           16 :         return GetAicpuUnfoldConfig();
    9378              :     }
    9379              : 
    9380          234 :     aclrtBinHandle HcclCommunicator::GetBinHandle() {
    9381          234 :         if (binHandle_ == nullptr) {
    9382          234 :             HCCL_ERROR("[HcclCommunicator][GetBinHandle] GetBinHandle binHandle failed.binHandle is nullptr");
    9383          234 :             return nullptr;
    9384              :         }
    9385            0 :         return binHandle_;
    9386              :     }
    9387            0 :     HcclResult HcclCommunicator::GetDevMemWorkSpace(const std::string &memTag, uint64_t *size, void **addr,
    9388              :         bool *newCreated)
    9389              :     {
    9390            0 :         return dpuManager_->GetDevMemWorkSpace(memTag, size, addr, newCreated);
    9391              :     }
    9392              : }
        

Generated by: LCOV version 2.0-1