LCOV - code coverage report
Current view: top level - coll_communicator_mgr/resource_mgr/local/my_rank - my_rank.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 92.9 % 14 13
Test Date: 2026-08-25 19:18:03 Functions: 88.9 % 9 8

            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 MY_RANK_H
      12              : #define MY_RANK_H
      13              : 
      14              : #include <cstdint>
      15              : #include <memory>
      16              : #include <mutex>
      17              : #include <string>
      18              : #include <unordered_map>
      19              : #include <utility>
      20              : #include <vector>
      21              : 
      22              : #include "hccl/hccl_types.h"
      23              : #include "hccl/base.h"
      24              : #include "hccl/hccl_res.h"
      25              : #include "hccl_mem_defs.h"
      26              : #include "acl/acl_rt.h"
      27              : #include "socket_manager.h"
      28              : #include "hcomm_res_defs.h"
      29              : #include "hcomm_channel.h"
      30              : #include "mem_host_pub.h"
      31              : #include "rank_pair_mgr.h"
      32              : #include "endpoint_mgr.h"
      33              : #include "comm_config_pub.h"
      34              : #include "manager_common.h"
      35              : #include "common.h"
      36              : #include "comm_mems/comm_mems.h"
      37              : #include "engine_ctxs.h"
      38              : #include "../../../dfx/ns_recovery/ns_recovery.h"
      39              : #include "hdc_pub.h"
      40              : #include "rank_graph.h"
      41              : #include "orion_adapter_hccp.h"
      42              : #include "coll_comm_config_consistency.h"
      43              : #include "exchange_info_mgr.h"
      44              : 
      45              : #include "ccu_types.h"
      46              : #include "ccu_drv_handle.h"
      47              : #include "ccu_device_res.h"
      48              : #include "dev_type.h"
      49              : 
      50              : namespace hccl {
      51              : 
      52              : constexpr uint32_t DEFAULT_MODE = 0;
      53              : constexpr uint32_t AICPU_TS_MODE = 2;
      54              : constexpr uint32_t CCU_MS_MODE = 5;
      55              : constexpr uint32_t CCU_SCHED_MODE = 6;
      56              : // opExpansionMode 到 CcuInstanceType 的映射,仅作数据类型转换,不做逻辑处理。
      57              : // 供 MyRank 内部及外部适配层(如 coll_comm_ccu_c_adpt.cc)共用。
      58           13 : inline CcuInstanceType OpExpansionModeToCcuInstanceType(uint32_t opExpansionMode)
      59              : {
      60           13 :     if (opExpansionMode == CCU_SCHED_MODE) {
      61            3 :         return CcuInstanceType::CCU_SCHED;
      62              :     }
      63           10 :     if (opExpansionMode == CCU_MS_MODE) {
      64            7 :         return CcuInstanceType::CCU_MS;
      65              :     }
      66            3 :     return CcuInstanceType::CCU_UNUSED;
      67              : }
      68              : 
      69              : /**
      70              :  * @note 职责:管理当前通信域下本Rank的信息和通信资源
      71              :  */
      72              : class MyRank {
      73              : public:
      74              :     MyRank(
      75              :         aclrtBinHandle binHandle, uint32_t rankId, const CommConfig& config, const ManagerCallbacks& callbacks,
      76              :         RankGraph* rankGraph, const Hccl::RankIpPortMapPtr& rankIpPortMap);
      77              :     ~MyRank();
      78              : 
      79              :     HcclResult Init(HcclMem cclBuffer, const uint32_t opExpansionMode, uint32_t rankNum);
      80              : 
      81           36 :     CommMems* GetCommMems() const { return commMems_.get(); }
      82              : 
      83           17 :     EngineCtxs* GetEngineCtxs() const { return engineCtxs_.get(); }
      84              : 
      85              :     HcclResult UnregMemByTag(const std::string& tag);
      86          177 :     uint32_t GetOpExpansionMode() { return opExpansionMode_; }
      87           17 :     CcuInsHandle GetCcuInstance() const { return ccuInsHandle_; }
      88            3 :     void SetCcuInstance(CcuInsHandle ccuInsHandle) { ccuInsHandle_ = ccuInsHandle; }
      89           22 :     CcuInsHandle GetAssignedCcuInstance() const { return assignedCcuInsHandle_; }
      90            4 :     void SetAssignedCcuInstance(CcuInsHandle ccuInsHandle) { assignedCcuInsHandle_ = ccuInsHandle; }
      91              : 
      92              :     CollCommConfigConsistency& GetCollCommConfigConsistency();
      93              : 
      94            0 :     hcomm::EndpointMgr* GetEndpointMgr() const { return endpointMgr_.get(); }
      95              : 
      96              :     HcclResult CreateChannels(
      97              :         CommEngine engine, const std::string& commTag, const HcclChannelDesc* channelDescs, uint32_t channelNum,
      98              :         ChannelHandle* channels);
      99              : 
     100              :     HcclResult
     101              :     QueryChannels(CommEngine engine, const HcclChannelDesc* channelDescs, uint32_t channelNum, ChannelHandle* channels);
     102              :     HcclResult DestroyChannels(const ChannelHandle* channels, uint32_t channelNum);
     103              : 
     104              :     HcclResult ChannelGetHcclBuffer(ChannelHandle channel, void** buffer, uint64_t* size);
     105              :     HcclResult
     106              :     ChannelGetRemoteMems(ChannelHandle channel, uint32_t* memNum, CommMem** remoteMem, char*** memTags) const;
     107              :     HcclResult ChannelGetRemoteMems(
     108              :         ChannelHandle channel, uint32_t* memNum, CommMem** remoteMem, std::vector<std::string>& memTags) const;
     109              : 
     110              :     // Ns recovery
     111              :     void SetKfcControlTransfer(
     112              :         std::shared_ptr<HDCommunicate> kfcControlTransferH2D, std::shared_ptr<HDCommunicate> kfcStatusTransferD2H);
     113              :     std::vector<ChannelHandle> GetAllChannelList();
     114              :     HcclResult StopLaunch();
     115              :     HcclResult Clean();
     116              :     HcclResult Resume();
     117              : 
     118              :     /**
     119              :      * @brief 批量预建 socket(server 监听 + client 连接),与非共享路径 MyRank::CreateChannels 一致。
     120              :      *        共享 jetty 路径在 createFunc 中调用,确保两端 socket 体系一致、可对接。
     121              :      * @param[in] channelDescs HCCL 层 channel desc 数组
     122              :      * @param[in] channelNum 数量
     123              :      * @param[in] socketTag socket 标签(commTag + "_engine_" + engine)
     124              :      * @param[out] hcommDescs 输出的 HcommChannelDesc 数组(调用前需用 ChannelDescHccl2Hcomm 填充基础字段,
     125              :      *             本方法补上 socket/role/port 字段)
     126              :      */
     127              :     HcclResult BatchCreateSockets(
     128              :         const HcclChannelDesc* channelDescs, uint32_t channelNum, const std::string& socketTag,
     129              :         std::vector<HcommChannelDesc>& hcommDescs);
     130              : 
     131              :     /**
     132              :      * @brief 在已建好的 socket 上执行通信域一致性校验交换(CheckFrameV2 + 用户信息)。
     133              :      *        与非共享路径 MyRank::CreateChannels 内部调用 exchangeInfoMgr_ 的逻辑一致,
     134              :      *        供共享 jetty 路径在 createFunc 中调用,确保两端无论 shared/非shared 配置是否对称,
     135              :      *        都会在 socket 上完成定长 CheckFrameV2(120B)的对称收发,避免一端死等。
     136              :      * @param[in] channelDescs HCCL 层 channel desc 数组
     137              :      * @param[in] hcommDescs 已通过 BatchCreateSockets 填充 socket 字段的 HcommChannelDesc 数组
     138              :      * @param[in] channelNum 数量
     139              :      * @param[in] newChannels 新建 channel 的 (idx, reuseIdx) 列表;空表示全部按新建处理
     140              :      * @param[in] engine 通信引擎
     141              :      * @note 仅 DEV_TYPE_950 实际执行交换,其它设备类型直接返回 SUCCESS,与非共享路径保持一致。
     142              :      */
     143              :     HcclResult BatchExchangeAndCheckConsistency(
     144              :         const HcclChannelDesc* channelDescs, const std::vector<HcommChannelDesc>& hcommDescs, uint32_t channelNum,
     145              :         const std::vector<std::pair<u32, u32>>& newChannels, CommEngine engine);
     146              :     HcclResult PrepareMemHandles(
     147              :         EndpointHandle epHandle, void** memHandles, uint32_t memHandleNum, std::vector<MemHandle>& memHandleVec);
     148              : 
     149              : private:
     150              :     using ReuseSocketIdxMap = std::unordered_map<RankPair*, std::unordered_map<hcomm::EndpointPair*, u32>>;
     151              :     HcclResult GetEndpointPairFromChannel(
     152              :         const HcclChannelDesc& channelDesc, uint32_t channelIndex, uint32_t channelNum, uint32_t& remoteRank,
     153              :         hcomm::EndpointPair*& endpointPair, RankPair*& rankPair);
     154              :     HcclResult BatchServerInitForChannels(
     155              :         const HcclChannelDesc* channelDescs, uint32_t channelNum, const std::string& socketTag,
     156              :         ReuseSocketIdxMap& reuseSocketIdxMap);
     157              :     HcclResult BatchGetSocketsForChannels(
     158              :         const HcclChannelDesc* channelDescs, uint32_t channelNum, const std::string& socketTag,
     159              :         std::vector<HcommChannelDesc>& hcommDescs, ReuseSocketIdxMap& reuseSocketIdxMap);
     160              :     HcclResult BatchCreateChannels(
     161              :         CommEngine engine, const HcclChannelDesc* channelDescs, uint32_t channelNum,
     162              :         std::vector<HcommChannelDesc>& hcommDescs, ChannelHandle* channelHandles,
     163              :         std::vector<std::vector<MemHandle>>& allHandles);
     164              :     HcclResult
     165              :     BatchConnectChannels(const HcclChannelDesc* channelDescs, ChannelHandle* channelHandles, uint32_t channelNum);
     166              :     void LogChannelCreationInfo(
     167              :         CommEngine engine, const std::string& commTag, const HcclChannelDesc* channelDescs, uint32_t channelNum,
     168              :         ChannelHandle* hostChannelHandleList);
     169              :     HcclResult FinalizeChannelsByEngine(
     170              :         CommEngine engine, const std::string& commTag, const HcclChannelDesc* channelDescs, uint32_t channelNum,
     171              :         std::vector<HcommChannelDesc>& hcommDescs, ChannelHandle* hostChannelHandleList, ChannelHandle* channelHandles);
     172              :     HcclResult CheckChannelParam(CommEngine engine, const HcclChannelDesc* channelDesc, uint32_t channelNum) const;
     173              :     HcclResult QueryListenPort(
     174              :         uint32_t localRank, uint32_t remoteRank, const EndpointDesc& localEndpointDesc,
     175              :         const EndpointDesc& remoteEndpointDesc, uint32_t& listenPort, HcommChannelDesc& hcommDesc);
     176              :     HcclResult GetLocalTlsStatus(Hccl::TlsStatus& tlsStatus) const;
     177              :     HcclResult RegisterCommMemsToEndpoint(EndpointHandle epHandle);
     178              :     HcclResult TryInitCcuInstance();
     179              :     HcclResult ReserveCcuMsCommOrFallback();
     180              :     HcclResult TryInitCcuInstanceOnDemand();
     181              :     void ReconcileCcuMsCommReservation(HcclResult initRet);
     182              :     void ReleaseCcuMsCommReservation();
     183              :     HcclResult ConfigSqDepthByExpansionMode(CommEngine engine, HcommChannelDesc& hcommDesc) const;
     184              :     HcclResult DestroyNewChannels(
     185              :         CommEngine engine, const HcclChannelDesc* channelDescs, const std::vector<std::pair<u32, u32>>& newChannels);
     186              :     HcclResult
     187              :     QueryOneChannel(CommEngine engine, const HcclChannelDesc& channelDesc, u32 reuseIdx, ChannelHandle& handle);
     188              :     HcclResult
     189              :     DestroyOneChannel(ChannelHandle userHandle, u32 index, HcclResult& firstErr, u32& invalidHandleCnt, u32& failedCnt);
     190              :     aclrtBinHandle binHandle_{nullptr};
     191              :     uint32_t rankId_{};
     192              :     int32_t devLogicId_{};
     193              :     CommConfig config_{};
     194              : 
     195              :     // 当前通信域初始化没有处理CommConfig,暂时只使用展开模式
     196              :     uint32_t opExpansionMode_{0};
     197              : 
     198              :     std::unique_ptr<RankPairMgr> rankPairMgr_{nullptr};
     199              :     std::unique_ptr<hcomm::EndpointMgr> endpointMgr_{nullptr};
     200              :     std::unique_ptr<CommMems> commMems_{nullptr};
     201              :     std::unique_ptr<EngineCtxs> engineCtxs_{nullptr};
     202              : 
     203              :     CcuInsHandle ccuInsHandle_{0}; // 按固定量CCU资源方式创建的 ccuInsHandle(HcclCommQueryCcuIns)(兼容旧版本)
     204              :     CcuInsHandle assignedCcuInsHandle_{0}; // 按需申请CCU资源方式绑定的 ccuInsHandle(HcommCcuInsCreate + Assign)
     205              : 
     206              :     ManagerCallbacks callbacks_;
     207              : 
     208              :     // RankGraph (临时放在myRank里面,后面会随着createchannel整体迁移到RankPairMgr上)
     209              :     RankGraph* rankGraph_{nullptr};
     210              : 
     211              :     // 记录每次调用BatchCreateChannels时新增的channelIndex, reuseIdx
     212              :     std::vector<std::pair<u32, u32>> newChannels_{};
     213              : 
     214              :     // channelHandle(host) -> EndpointPair 裸指针反查索引:EndpointPair 由 EndpointPairMgr 以 unique_ptr 持有,
     215              :     // 其生命周期与 RankPairMgr 一致(MyRank 析构时先 clear 反查表再释放 rankPairMgr_)
     216              :     std::unordered_map<ChannelHandle, hcomm::EndpointPair*> handleToEpPair_{};
     217              : 
     218              :     // 保护 newChannels_ / handleToEpPair_ 的并发访问
     219              :     std::mutex channelIndexMtx_{};
     220              : 
     221              :     // Ns recovery
     222              :     std::unique_ptr<NsRecoveryProcessor> nsRecoveryProcessor_{nullptr};
     223              :     // 内部获取 port 的方法,根据 mode_ 区分 v1/v2
     224              :     HcclResult GetDevicePortInternal(uint32_t rank, uint32_t* devPort, EndpointLocType locType);
     225              : 
     226              :     Hccl::RankIpPortMapPtr rankIpPortMap_;
     227              : 
     228              :     CollCommConfigConsistency collCommConfigConsistency_;
     229              :     ExchangeInfoMgr exchangeInfoMgr_;
     230              :     std::shared_ptr<hcomm::CcuDrvHandle> ccuDrvHandle_{};
     231              :     bool ccuMsCommReserved_{false};
     232              :     Hccl::HcclMainboardId mainBoardType_{Hccl::HcclMainboardId::MAINBOARD_OTHERS};
     233              : };
     234              : 
     235              : } // namespace hccl
     236              : 
     237              : namespace MyRankUtils {
     238              : 
     239              : HcommChannelDesc ChannelDescHccl2Hcomm(const HcclChannelDesc& hcclDesc, const hccl::CommConfig& commConfig);
     240              : 
     241              : HcclResult
     242              : FillRoceSrcPortList(const HcclChannelDesc& hcclDesc, HcommChannelDesc& hcommDesc, std::vector<uint16_t>& srcPortBuf);
     243              : 
     244              : } // namespace MyRankUtils
     245              : 
     246              : #endif // MY_RANK_H
        

Generated by: LCOV version 2.0-1