LCOV - code coverage report
Current view: top level - legacy/ascend910/algorithm/base/inc - comm_base_pub.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 31.6 % 19 6
Test Date: 2026-08-04 10:52:23 Functions: 42.9 % 7 3

            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              : #ifndef COMM_BASE_PUB_H
      12              : #define COMM_BASE_PUB_H
      13              : 
      14              : #include <memory>
      15              : #include <mutex>
      16              : #include <map>
      17              : #include <sys/types.h>
      18              : #include <hccl/hccl_types.h>
      19              : 
      20              : #include "hccl_common.h"
      21              : #include "hccl_socket_manager.h"
      22              : #include "transport_pub.h"
      23              : #include "alg_template_base_pub.h"
      24              : #include "workflow_pub.h"
      25              : #include "common.h"
      26              : 
      27              : namespace hccl {
      28              : constexpr u32 HCCL_RANK_SIZE_EQ_TWO = 2;
      29              : constexpr u32 HCCL_RANK_ZERO = 0;
      30              : constexpr u32 HCCL_RANK_OFFSET = 1;
      31              : 
      32              : constexpr u32 FACTOR_NUM_TWO = 2;
      33              : constexpr s32 DEVICE_PER_MODULE = 8;         // 单module支持最大device数量
      34              : constexpr u32 MC2_PLANE_MODE_HOST = 0;    // HOST调度RoCE模式
      35              : constexpr u32 MC2_PLANE_MODE_COMBINE = 1;    // 非分层建链模式
      36              : constexpr u32 MC2_PLANE_MODE_HIERARCHY = 2;    // 分层建链模式
      37              : 
      38              : constexpr u32 GROUP_MAX_CONCURRENT = 8;
      39              : 
      40              : class CommBase {
      41              : public:
      42              :     explicit CommBase(const std::string &collectiveId,
      43              :                       const u32 userRank, const u32 userRankSize, const u32 rank,
      44              :                       const u32 rankSize, const std::vector<RankInfo> paraVector,
      45              :                       const TopoType topoFlag,
      46              :                       const HcclDispatcher dispatcher,
      47              :                       const std::unique_ptr<NotifyPool> &notifyPool,
      48              :                       std::map<HcclIpAddress, HcclNetDevCtx> &netDevCtxMap,
      49              :                       const IntraExchanger &exchanger,
      50              :                       const DeviceMem &inputMem, const DeviceMem &outputMem, const bool isUsedRdmaLevel0,
      51              :                       const std::string &tag = "",
      52              :                       const NICDeployment nicDeployInner = NICDeployment::NIC_DEPLOYMENT_DEVICE,
      53              :                       bool isAlltoAllCommMesh = false, const bool useOneDoorbell = false,
      54              :                       const bool isAicpuModeEn = false, const u32 rankRoot = INVALID_UINT,
      55              :                       const bool isHaveCpuRank = false, const bool useSuperPodMode = false,
      56              :                       DeviceMem expMem = DeviceMem());
      57              :     virtual ~CommBase();
      58              : 
      59              :     inline const std::string &CollectiveId() const
      60              :     {
      61              :         return collectiveId_;
      62              :     }
      63              : 
      64            0 :     inline const std::vector<LINK> &TransportInfo() const
      65              :     {
      66            0 :         return transportInfo_;
      67              :     }
      68              : 
      69            1 :     inline u32 UserRank() const
      70              :     {
      71            1 :         return userRank_;
      72              :     }
      73              : 
      74              :     inline u32 UserRankSize() const
      75              :     {
      76              :         return userRankSize_;
      77              :     }
      78              : 
      79            1 :     inline u32 Rank() const
      80              :     {
      81            1 :         return rank_;
      82              :     }
      83              : 
      84            1 :     inline u32 RankSize() const
      85              :     {
      86            1 :         return rankSize_;
      87              :     }
      88              : 
      89            0 :     inline const std::string &Tag() const
      90              :     {
      91            0 :         return tag_;
      92              :     }
      93              : 
      94            0 :     inline void SetHeterogP2PType()
      95              :     {
      96            0 :         isNeedHeterogP2P_ = true;
      97            0 :     }
      98              : 
      99              :     inline void SetHostUseDevNic()
     100              :     {
     101              :         isHostUseDevNic_ = true;
     102              :     }
     103              : 
     104              :     virtual HcclResult Init(); // 初始化必要信息
     105              :     virtual HcclResult DeInit();
     106              : 
     107              :     std::shared_ptr<Transport> &GetTransportByRank(const u32 dstRank); // 获取当前rank与dst rank的link信息
     108              :     HcclResult GetRankByUserRank(const u32 userRank, u32 &rank) const; // 获取当前userrank在重新排序后的rank
     109              :     HcclResult GetUserRankByRank(const u32 rank, u32 &userRank) const; // 获取当前rank的userrank
     110              :     HcclResult GetRaSocket(const u32 role, HcclSocketInfo conn[], const u32 num);
     111              :     HcclResult CreateIntraThread(const u32 role, u32 dstRank,
     112              :         const std::vector<std::shared_ptr<HcclSocket> > &sockets); // 节点内建链起线程
     113              :     HcclResult CreateInterThread(const u32 role, u32 dstRank,
     114              :         const std::vector<std::shared_ptr<HcclSocket> > &sockets); // 节点间建链起线程
     115              :     HcclResult RunTemplateAlg(const std::unique_ptr<AlgTemplateBase> &tempAlg);
     116              :     HcclResult RunTemplateAlgStaged(const std::unique_ptr<AlgTemplateBase> &tempAlg, const RunStage &stage);
     117              :     std::shared_ptr<Transport> &GetTrasportInfoByVTransportInfoIndex(u32 index);
     118              :     HcclResult BuildAsync(u32& status);
     119              :     HcclResult BuildQuerry(u32& status);
     120              :     HcclResult SetHDCModeInfo(
     121              :         std::unordered_map<std::string, std::map<u32, HcclIpAddress>> &rankDevicePhyIdNicInfoMap,
     122              :         std::vector<u32> &ranksPort, std::vector<u32> &vnicRanksPort, bool isSetHDCModeInfo, bool isUseRankPort);
     123              :     u32 IsSupportMC2(const std::string &tag);
     124            0 :     void Break()
     125              :     {
     126            0 :         for (auto &ptr : transportInfo_) {
     127            0 :             if (ptr == nullptr) {
     128            0 :                 continue;
     129              :             }
     130            0 :             ptr->Break();
     131              :         }
     132            0 :     }
     133              : 
     134              :     std::shared_ptr<Transport> linkDummy_; // 用于无效的link返回
     135              : protected:
     136              :     // 创建当前rank与其他rank之间的link:ring\mesh\H-D分别在对应派生类实现
     137              :     virtual HcclResult CreateLinks();
     138              :     // 计算当前rank与其他rank之间的link个数:server/client两种角色,ring\mesh\H-D分别在对应派生类实现
     139              :     virtual HcclResult CalcLink();
     140              :     // 获取每个 link 需要的 socket 数量
     141              :     virtual u32 GetSocketsPerLink();
     142              :     virtual bool NeedDataReceivedAck();
     143              :     virtual void SetMachineLinkMode(MachinePara &machinePara);
     144              :     virtual HcclResult CreateIntraLinks(); // 当前rank在服务器内与对端的建链
     145              :     virtual HcclResult CreateInterLinks(); // 当前rank在服务期间与对端的建链
     146              :     virtual HcclResult SetMachinePara(MachineType machineType, const std::string &serverId, u32 dstRank,
     147              :         const std::vector<std::shared_ptr<HcclSocket> > &sockets, MachinePara &machinePara);
     148              :     virtual void SetTransportParam(TransportPara &para, MachinePara &machinePara);
     149              :     virtual HcclResult MakeClientInfo(const u32 dstRank, RankInfo &dstRankInfo, bool isInterRdma, bool isInterHccs);
     150              :     virtual HcclResult MakeServerInfo(const u32 dstRank, RankInfo &dstRankInfo, bool isInterRdma, bool isInterHccs);
     151              : 
     152              :     virtual HcclResult CreateExchangerNetwork(); // server间HCCS通信模式下,创建节点间的建链关系
     153              :     HcclResult GetRankLinkInfo(bool &isInterServer, bool &isInterHccs, std::map<u32, HcclSocketRole> &rankRole);
     154              : 
     155              :     HcclResult CalcLinksNum(const MachineType machineType,
     156              :         const u32 dstRank); // 填充dst_inter_server_num_/dst_inter_client_num_
     157              :     HcclResult CreateDestLink(const ErrContextPub &error_context, const MachineType machineType,
     158              :         const std::string &serverId, const u32 dstRank, const std::string &threadStr,
     159              :         const std::vector<std::shared_ptr<HcclSocket> > &sockets); // 创建transhport
     160              :     HcclResult TransportInit(const u32 dstRank, MachinePara &machinePara);
     161              :     HcclResult SetRankMap(); // 获取rank->userrank以及userrank->rank的映射关系
     162              :     HcclResult GetBuildStatus(u32& status);
     163              :     HcclResult TransportBuildAsync(const MachineType machineType, const std::string &serverId, u32 dstRank,
     164              :         const std::vector<std::shared_ptr<HcclSocket> > &sockets, u32& status);
     165              :     HcclResult TransportBuildQuerry(u32 dstRank, u32& status);
     166              : 
     167              :     const std::string collectiveId_; /* * 本节点所在的通信域ID */
     168              : 
     169              :     const u32 userRank_;      /* * 本节点的用户原始rank号 */
     170              :     const u32 userRankSize_; /* * 本节点所在的用户通信域rank size */
     171              : 
     172              :     const u32 rank_;      /* * 本节点在子通信域的rank号 */
     173              :     const u32 rankSize_; /* * 本节点所在的子通信域的rank size */
     174              :     std::vector<RankInfo> paraVector_;        // 子通信域内各个rank的基本信息
     175              :     // 与link_info_中每个link类型对应的vector:默认值均为-1,建链成功后填入对应type
     176              :     std::vector<TransportType> transportType_;
     177              :     std::unordered_map<std::string, std::map<u32, HcclIpAddress>> rankDevicePhyIdNicInfoMap_;
     178              :     std::vector<u32> ranksPort_;
     179              :     std::vector<u32> vnicRanksPort_;
     180              : 
     181              :     s32 deviceLogicId_;
     182              :     u32 devicePhyId_;
     183              :     std::map<u32, std::unique_ptr<HcclSocketManager>> pyhIdResourseSockets_{};
     184              : protected:
     185              :     const TopoType topoFlag_;     // 当前通信域内服务器间拓扑组合类型
     186              : 
     187              :     const std::string tag_; /* * 多stream是的tag信息 */
     188              :     std::vector<std::shared_ptr<Transport> > transportInfo_; // 当前rank与其他rank对应的link信息
     189              :     std::vector<std::shared_ptr<Transport> > vTransportInfo_; // 当前rank与其他rank对应的virtual link信息
     190              : 
     191              :     std::vector<u32> rankMap_;       // rank->userrank的映射关系:vector下标是user_rank,存的数据是rank
     192              :     std::vector<u32> userRankMap_; // userrank->rank的映射关系:vector下标是rank,存的数据是user_rank
     193              : 
     194              :     const HcclDispatcher dispatcher_;    // signal调度句柄(event/notify机制)
     195              :     const std::unique_ptr<NotifyPool> &notifyPool_;
     196              :     std::map<HcclIpAddress, HcclNetDevCtx> &netDevCtxMap_;
     197              :     const IntraExchanger &exchanger_;
     198              :     std::map <u32, std::vector<std::shared_ptr<HcclSocket> > > intraSocketsMap_;
     199              :     const DeviceMem inputMem_;
     200              :     const DeviceMem outputMem_;
     201              :     const bool isUsedRdmaLevel0_;
     202              : 
     203              :     // 当前rank作为client端,需要连接的节点间server端的集合:键为rank 号,value为 HcclRankLinkInfo
     204              :     // 当前rank作为client端, 需要连接的节点间server端的集合
     205              :     std::map<u32, HcclRankLinkInfo> dstInterServerMap_;
     206              :     // 当前rank作为server端, 需要连接的节点间client端的集合
     207              :     std::map<u32, HcclRankLinkInfo> dstInterClientMap_;
     208              : 
     209              :     // 当前rank作为client端,需要连接的节点内server端的集合
     210              :     std::vector<u32> dstIntraServerVec_;
     211              :     // 当前rank作为server端,需要连接的节点内client端的集合
     212              :     std::vector<u32> dstIntraClientVec_;
     213              : 
     214              :     std::vector<std::unique_ptr<std::thread> > linkThreads_; // 建链所需线程
     215              :     u32 threadsRapplyNum_;                                   // 线程使用计数器
     216              : 
     217              :     dev_t shmDev_; // dev_t 为u32类型
     218              :     bool isAlltoAllCommMesh_;
     219              :     NICDeployment nicDeployInner_;
     220              :     std::vector<u32> transportStatus_;
     221              :     std::vector<bool> checkStatus_;
     222              :     bool isNeedHeterogP2P_;
     223              :     bool useOneDoorbell_;
     224              : 
     225              :     // 获取当前rank和目的rank之间的link type
     226              :     HcclResult SetTransportType(const u32 dstRank);
     227              :     HcclResult CheckLinks() const; // 校验当前rank与dst rank建链的链路有效性
     228              :     HcclResult CreateVirturalTransport();
     229              : 
     230              :     // 统一接口
     231              :     HcclResult GetRankIPInfo(bool isInterServer, bool isInterHccs, bool &isSupportReuse,
     232              :         std::map<u32, HcclSocketRole> &rankRole, HcclIpAddress &localIP,
     233              :         std::map<u32, HcclRankLinkInfo> &dstServerMap,
     234              :         std::map<u32, HcclRankLinkInfo> &dstClientMap,
     235              :         std::shared_ptr<HcclSocketManager> &socketManager);
     236              : 
     237              :     // server内使用
     238              :     HcclResult GetIntraRankIPInfo(std::map<u32, HcclSocketRole> &rankRole,
     239              :         HcclIpAddress &localIP,
     240              :         std::map<u32, HcclRankLinkInfo> &dstServerMap,
     241              :         std::map<u32, HcclRankLinkInfo> &dstClientMap);
     242              : 
     243              :     // 310p device侧、isHaveCpuRank_ 业务场景使用
     244              :     HcclResult GetIntraRankIPInfo(std::vector<u32> &dstIntraVec,
     245              :         HcclIpAddress &localIP,
     246              :         std::map<u32, HcclRankLinkInfo> &dstServerMap,
     247              :         std::map<u32, HcclRankLinkInfo> &dstClientMap);
     248              : 
     249              :     // 超节点server间HCCS场景使用
     250              :     HcclResult GetSuperNodeIntraRankIPInfo(std::map<u32, HcclSocketRole> &rankRole,
     251              :         HcclIpAddress &localIP,
     252              :         std::map<u32, HcclRankLinkInfo> &dstServerMap,
     253              :         std::map<u32, HcclRankLinkInfo> &dstClientMap);
     254              : 
     255              :     bool IsSupportInterHccs(const u32 dstRank); // 是否支持走节点间HCCS通信
     256              :     u32 GetInterRemotePort(s32 devicePhyId, u32 dstUserRank);
     257              :     void PrintCreateInterLinksInfo(); // 打印当前rank在服务器间建链通信topo对
     258              :     HcclResult CheckExchangeInfo(const std::shared_ptr<Transport> &link, const s32 deviceId);
     259              :     const bool isAicpuModeEn_;
     260              :     std::unique_ptr<HcclSocketManager> interSocketManager_;
     261              :     const u32 subUserRankRoot_;
     262              :     bool isHostUseDevNic_{ false };
     263              :     bool isSetHDCModeInfo_{ false };
     264              :     bool isUseRankPort_{ false };
     265              :     bool isHaveCpuRank_{ false };
     266              :     bool useSuperPodMode_{ false };
     267              :     const DeviceMem expMem_;
     268              :     std::shared_ptr<HcclSocketManager> socketManager_{ nullptr };
     269              :     HcclWorkflowMode workflowMode_{HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE};
     270              : };
     271              : }  // namespace hccl
     272              : 
     273              : #endif /* COMM_BASE_PUB_H */
        

Generated by: LCOV version 2.0-1