LCOV - code coverage report
Current view: top level - legacy/ascend950/framework/resource_manager/socket - socket_config.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 94 94
Test Date: 2026-08-18 17:47:01 Functions: 94.4 % 18 17

            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 HCCL_SOCKET_CONFIG_H
      12              : #define HCCL_SOCKET_CONFIG_H
      13              : 
      14              : #include "types.h"
      15              : #include "virtual_topo.h"
      16              : #include "hash_utils.h"
      17              : #include "log.h"
      18              : 
      19              : namespace Hccl {
      20         1224 : MAKE_ENUM(SocketRole, SERVER, CLIENT)
      21              : class SocketConfig {
      22              : public:
      23              :     RankId remoteRank;
      24              :     LinkData link;
      25              :     uint32_t listeningPort{DEFAULT_LISTENING_PORT};
      26              :     const std::string tag;
      27              :     uint32_t hostNic2DeviceNicMode_{0}; // 0 normal, 1: host(host cpu roce channel) - device(transport ibv)
      28              : 
      29          107 :     SocketConfig(RankId remoteRank, const LinkData& link, const std::string& tag)
      30          107 :         : remoteRank(remoteRank),
      31          107 :           link(link),
      32          107 :           tag(tag),
      33          107 :           role(link.GetLocalRankId() < link.GetRemoteRankId() ? SocketRole::SERVER : SocketRole::CLIENT),
      34          107 :           hccpTag(
      35          107 :               role == SocketRole::SERVER ?
      36          212 :                   tag + "_" + to_string(link.GetLocalRankId()) + "_" + to_string(link.GetRemoteRankId()) + "_"
      37          424 :                       + link.GetLocalAddr().GetIpStr() + "_" + link.GetRemoteAddr().GetIpStr() :
      38          109 :                   tag + "_" + to_string(link.GetRemoteRankId()) + "_" + to_string(link.GetLocalRankId()) + "_"
      39          325 :                       + link.GetRemoteAddr().GetIpStr() + "_" + link.GetLocalAddr().GetIpStr())
      40          107 :     {}
      41              : 
      42              :     SocketConfig(const LinkData& link, const std::string& tag)
      43              :         : remoteRank(link.GetRemoteRankId()),
      44              :           link(link),
      45              :           tag(tag),
      46              :           role(link.GetLocalAddr() < link.GetRemoteAddr() ? SocketRole::SERVER : SocketRole::CLIENT),
      47              :           hccpTag(
      48              :               role == SocketRole::SERVER ?
      49              :                   tag + "_" + to_string(link.GetLocalRankId()) + "_" + to_string(link.GetRemoteRankId()) + "_"
      50              :                       + link.GetLocalAddr().GetIpStr() + "_" + link.GetRemoteAddr().GetIpStr() :
      51              :                   tag + "_" + to_string(link.GetRemoteRankId()) + "_" + to_string(link.GetLocalRankId()) + "_"
      52              :                       + link.GetRemoteAddr().GetIpStr() + "_" + link.GetLocalAddr().GetIpStr())
      53              :     {}
      54              : 
      55            8 :     SocketConfig(const LinkData& link, const std::string& tag, SocketRole role, const uint32_t listenPort)
      56            8 :         : remoteRank(link.GetRemoteRankId()),
      57            8 :           link(link),
      58            8 :           listeningPort(listenPort),
      59            8 :           tag(tag),
      60            8 :           role(role),
      61            8 :           hccpTag(
      62            8 :               role == SocketRole::SERVER ?
      63            8 :                   tag + "_" + link.GetLocalAddr().GetIpStr() + "_" + link.GetRemoteAddr().GetIpStr() :
      64           24 :                   tag + "_" + link.GetRemoteAddr().GetIpStr() + "_" + link.GetLocalAddr().GetIpStr())
      65            8 :     {}
      66              : 
      67            3 :     SocketConfig(const LinkData& link, const std::string& tag, bool noRankId)
      68            3 :         : remoteRank(link.GetRemoteRankId()),
      69            3 :           link(link),
      70            3 :           tag(tag),
      71            3 :           role(link.GetLocalAddr() < link.GetRemoteAddr() ? SocketRole::SERVER : SocketRole::CLIENT),
      72            3 :           hccpTag(
      73            3 :               role == SocketRole::SERVER ?
      74            3 :                   tag + "_" + link.GetLocalAddr().GetIpStr() + "_" + link.GetRemoteAddr().GetIpStr() :
      75            6 :                   tag + "_" + link.GetRemoteAddr().GetIpStr() + "_" + link.GetLocalAddr().GetIpStr()),
      76            3 :           noRankId(noRankId)
      77            3 :     {}
      78              : 
      79           13 :     SocketConfig(
      80              :         const LinkData& link, const uint32_t listenPort, const std::string& tag, uint32_t hostNic2DeviceNicMode,
      81              :         const uint32_t myRank, const uint32_t rmtRank)
      82           13 :         : SocketConfig(link, listenPort, tag)
      83              :     {
      84           13 :         if (hostNic2DeviceNicMode == 0) {
      85            4 :             return;
      86              :         }
      87              :         // Parse commTag from tag prefix: tag format is "commTag_engine_X" or "commTag_engine_X_protocol_Y"
      88            9 :         std::string commTag = tag;
      89            9 :         size_t enginePos = commTag.find("_engine_");
      90            9 :         if (enginePos != std::string::npos) {
      91            6 :             commTag = commTag.substr(0, enginePos);
      92              :         } else {
      93            3 :             HCCL_WARNING("[SocketConfig] socketTag[%s] format error, using original tag as commTag", tag.c_str());
      94              :         }
      95            9 :         remoteRank = rmtRank;
      96            9 :         role = myRank < rmtRank ? SocketRole::SERVER : SocketRole::CLIENT;
      97            9 :         if (role == SocketRole::SERVER) { // server: tag_local_remote
      98           14 :             hccpTag = commTag + "_" + to_string(myRank) + "_" + to_string(rmtRank) + "_"
      99           21 :                       + link.GetLocalAddr().GetIpStr() + "_" + link.GetRemoteAddr().GetIpStr();
     100              :         } else { // client: tag_remote_local
     101            4 :             hccpTag = commTag + "_" + to_string(rmtRank) + "_" + to_string(myRank) + "_"
     102            6 :                       + link.GetRemoteAddr().GetIpStr() + "_" + link.GetLocalAddr().GetIpStr();
     103              :         }
     104            9 :         hostNic2DeviceNicMode_ = hostNic2DeviceNicMode;
     105            9 :     }
     106              : 
     107           14 :     SocketConfig(const LinkData& link, const uint32_t listenPort, const std::string& tag)
     108           14 :         : remoteRank(link.GetRemoteRankId()),
     109           14 :           link(link),
     110           14 :           listeningPort(listenPort),
     111           14 :           tag(tag)
     112              :     {
     113           14 :         role = link.GetLocalAddr() < link.GetRemoteAddr() ? SocketRole::SERVER : SocketRole::CLIENT;
     114              : 
     115           14 :         if (role == SocketRole::SERVER) { // server: tag_local_remote
     116           26 :             hccpTag = tag + "_" + link.GetLocalAddr().GetIpStr() + "_" + link.GetRemoteAddr().GetIpStr() + "_"
     117           39 :                       + to_string(listenPort);
     118              :         } else { // client: tag_remote_local
     119            2 :             hccpTag = tag + "_" + link.GetRemoteAddr().GetIpStr() + "_" + link.GetLocalAddr().GetIpStr() + "_"
     120            3 :                       + to_string(listenPort);
     121              :         }
     122           14 :     }
     123              : 
     124            4 :     SocketConfig(const LinkData& link, const uint32_t listenPort, const std::string& tag, const bool isServer)
     125            4 :         : remoteRank(link.GetRemoteRankId()),
     126            4 :           link(link),
     127            4 :           listeningPort(listenPort),
     128            4 :           tag(tag)
     129              :     {
     130            4 :         role = isServer ? SocketRole::SERVER : SocketRole::CLIENT;
     131              : 
     132            4 :         if (role == SocketRole::SERVER) { // server: tag_local_remote
     133            2 :             hccpTag = tag + "_" + link.GetLocalAddr().GetIpStr() + "_" + link.GetRemoteAddr().GetIpStr() + "_"
     134            3 :                       + to_string(listenPort);
     135              :         } else { // client: tag_remote_local
     136            6 :             hccpTag = tag + "_" + link.GetRemoteAddr().GetIpStr() + "_" + link.GetLocalAddr().GetIpStr() + "_"
     137            9 :                       + to_string(listenPort);
     138              :         }
     139            4 :     }
     140              : 
     141           33 :     SocketRole GetRole() const { return role; }
     142              : 
     143          164 :     const string& GetHccpTag() const { return hccpTag; }
     144              : 
     145              : private:
     146              :     SocketRole role{};
     147              :     string hccpTag;
     148              : 
     149              : public:
     150              :     bool noRankId{false};
     151              : };
     152              : } // namespace Hccl
     153              : 
     154              : namespace std {
     155              : // 特化SocketConfig的hash和equal模板,使其可用做map的key
     156              : template <>
     157              : class hash<Hccl::SocketConfig> {
     158              : public:
     159          118 :     size_t operator()(const Hccl::SocketConfig& socketConfig) const
     160              :     {
     161          118 :         auto remoteRankHash = hash<Hccl::RankId>{}(socketConfig.remoteRank);
     162          118 :         auto localPortHash = hash<Hccl::PortData>{}(socketConfig.link.GetLocalPort());
     163          118 :         auto remotePortHash = hash<Hccl::PortData>{}(socketConfig.link.GetRemotePort());
     164          118 :         auto tagHash = hash<string>{}(socketConfig.tag);
     165          118 :         auto portHash = hash<uint32_t>{}(socketConfig.listeningPort);
     166              : 
     167          118 :         return Hccl::HashCombine({remoteRankHash, localPortHash, remotePortHash, tagHash, portHash});
     168              :     }
     169              : };
     170              : 
     171              : template <>
     172              : class equal_to<Hccl::SocketConfig> {
     173              : public:
     174           57 :     bool operator()(const Hccl::SocketConfig& config, const Hccl::SocketConfig& otherConfig) const
     175              :     {
     176           57 :         bool IsOthersSame = config.link.GetLocalPort().GetAddr() == otherConfig.link.GetLocalPort().GetAddr()
     177           57 :                             && config.link.GetRemotePort().GetAddr() == otherConfig.link.GetRemotePort().GetAddr()
     178           54 :                             && config.tag == otherConfig.tag && config.GetHccpTag() == otherConfig.GetHccpTag()
     179          114 :                             && config.listeningPort == otherConfig.listeningPort;
     180              : 
     181           57 :         if (config.noRankId && otherConfig.noRankId) {
     182            3 :             return IsOthersSame;
     183              :         }
     184              : 
     185           54 :         return IsOthersSame && config.remoteRank == otherConfig.remoteRank;
     186              :     }
     187              : };
     188              : } // namespace std
     189              : 
     190              : #endif // HCCL_SOCKET_CONFIG_H
        

Generated by: LCOV version 2.0-1