LCOV - code coverage report
Current view: top level - legacy/ascend950/framework/env_config - env_func.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 26 26
Test Date: 2026-07-28 12:11:00 Functions: 93.3 % 30 28

            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              : #ifndef HCCLV2_ENV_FUNC_H
      11              : #define HCCLV2_ENV_FUNC_H
      12              : 
      13              : #include <vector>
      14              : #include <algorithm>
      15              : #include <functional>
      16              : #include <sstream>
      17              : #include <set>
      18              : #include "exception_util.h"
      19              : #include "dma_mode.h"
      20              : #include "ip_address.h"
      21              : #include "op_type.h"
      22              : #include "types.h"
      23              : 
      24              : 
      25              : namespace Hccl {
      26              : 
      27              : constexpr u32 MAX_LEN_OF_DIGIT_ENV     = 10; // 数字环境变量最大长度
      28              : constexpr u32 NPU_NET_PROTOCOL_MAX_LEN = 127;
      29              : 
      30              : constexpr u32 HCCL_ALGO_LEVEL_0   = 0; // HCCL 算法层级0
      31              : constexpr u32 HCCL_ALGO_LEVEL_1   = 1; // HCCL 算法层级1
      32              : constexpr u32 HCCL_ALGO_LEVEL_2   = 2; // HCCL 算法层级2
      33              : constexpr u32 HCCL_ALGO_LEVEL_3   = 3; // HCCL 算法层级3
      34              : constexpr u32 HCCL_ALGO_LEVEL_NUM = 4; // HCCL 算法层级最多4级
      35              : 
      36              : constexpr s32 NOTIFY_MAX_WAIT_TIME = 255 * 68; // 非910A2和910A3场景notify wait最大等待时长,由硬件决定
      37              : constexpr s32 NOTIFY_MAX_WAIT_TIME_910A3 = 2147483647; // 910A2和910A3场景notify wait最大等待时长,由软件实现
      38              : constexpr s32 HCCL_EXEC_TIME_OUT_S
      39              :     = NOTIFY_MAX_WAIT_TIME; // 910A2和910A3场景非HCCL默认的Notify wait超时时间设置为最大超时时间 // 改成与A3相同超时时长
      40              : constexpr s32 HCCL_EXEC_TIME_OUT_S_910A3
      41              :     = NOTIFY_MAX_WAIT_TIME_910A3; // 910A2和910A3 HCCL默认的Notify wait超时时间设置为最大超时时间
      42              : constexpr s32 HCCL_INTEVAL_EXEC_TIME_OUT_S = 68; // notifywait的设置参数必须是68的整数倍
      43              : 
      44              : constexpr u32 HCCL_CCU_CONTINUOUS_MS_ID_CONFIG_DIR_NUM = 2; // HCCL CCU MS ID配置层级最多2维
      45              : 
      46              : constexpr u32 HCCL_CCU_FLAG_NUM = 2; // HCCL NEW CCU 最大是2
      47              : 
      48              : constexpr char HCCL_AUTO_PORT_CONFIG[] = "auto";
      49              : constexpr u32 HCCL_SOCKET_PORT_RANGE_AUTO = 0;
      50              : constexpr u32 MAX_PORT_NUMBER = 65535;
      51              : 
      52              : struct SocketIfName {
      53              :     std::vector<std::string> configIfNames{};  // 用户输入的网卡名列表
      54              :     bool                     searchNot{false}; // 匹配还是不匹配,TRUE:不匹配,FALSE:匹配
      55              :     bool searchExact{false}; // 精确匹配或前缀匹配,TRUE:精确匹配,FALSE:前缀匹配
      56              :     std::string              configIfNameStr{""};
      57            6 :     SocketIfName() = default;
      58           45 :     SocketIfName(const std::vector<std::string> &configIfNames, bool searchNot, bool searchExact)
      59          135 :         : configIfNames(configIfNames), searchNot(searchNot), searchExact(searchExact){};
      60              : };
      61              : 
      62              : struct DfsConfig {
      63              :     bool taskExceptionEnable{true};
      64              :     bool clusterHeartBeatEnable{true};
      65              :     int32_t rankConsistentState{0}; // -1:off 0:first 1:on
      66              :     DfsConfig() = default;
      67           26 :     DfsConfig(bool taskException, bool clusterHeartBeatEnable, int32_t consistentState)
      68           26 :         : taskExceptionEnable(taskException), clusterHeartBeatEnable(clusterHeartBeatEnable), rankConsistentState(consistentState){};
      69              : };
      70              : 
      71              : enum class NpuProtoType {
      72              :     TCP = 1, // 拉远TCP模式
      73              :     RDMA,    // 拉远RDMA模式
      74              :     RESERVED // 拉远未进行模式使能
      75              : };
      76              : 
      77              : using SocketPortRange = struct SocketPortRangeDef {
      78              :     u32 min;
      79              :     u32 max;
      80            5 :     bool operator==(const SocketPortRangeDef& other) const {
      81            5 :         return min == other.min && max == other.max;
      82              :     }
      83              : };
      84              : 
      85              : // HCCL通信算法类型
      86         1791 : MAKE_ENUM(HcclAlgoType,
      87              :           HCCL_ALGO_TYPE_DEFAULT, // 默认算法,配置为此时,使用HCCL内藏算法选择逻辑
      88              :           HCCL_ALGO_TYPE_RING, HCCL_ALGO_TYPE_PIPELINE, HCCL_ALGO_TYPE_FULLMESH, HCCL_ALGO_TYPE_HDR,
      89              :           HCCL_ALGO_TYPE_PAIRWISE, HCCL_ALGO_TYPE_NHR, HCCL_ALGO_TYPE_NB, HCCL_ALGO_TYPE_NULL, HCCL_ALGO_TYPE_NA,
      90              :           HCCL_ALGO_TYPE_NHR_V1, HCCL_ALGO_TYPE_AHC)
      91              : 
      92              : const std::set<OpType> OP_TYPE_SET = {OpType::ALLREDUCE, OpType::BROADCAST, OpType::ALLGATHER, OpType::REDUCESCATTER, OpType::SEND,
      93              :                                OpType::RECV, OpType::BARRIER, OpType::ALLTOALL, OpType::REDUCE, OpType::GATHER, OpType::SCATTER,
      94              :                                OpType::ALLTOALLV, OpType::ALLTOALLVC, OpType::BATCHSENDRECV, OpType::DEBUGCASE};
      95              : 
      96              : MAKE_ENUM(OrchestrateWay, PRIM, INS)
      97              : 
      98              : // HCCL绕路类型
      99         1111 : MAKE_ENUM(HcclDetourType,
     100              :           HCCL_DETOUR_DISABLE,          // 绕路不使能,默认为此值
     101              :           HCCL_DETOUR_ENABLE_2P,        // 2P间绕路
     102              :           HCCL_DETOUR_ENABLE_4P,        // 4P间绕路
     103              :           HCCL_DETOUR_ENABLE_2P_AND_4P) // 2P和4P间绕路
     104              : 
     105              : MAKE_ENUM(HcclTopoType, HCCL_TOPO_4P4K, HCCL_TOPO_4P4K_2D, HCCL_TOPO_4P1K, HCCL_TOPO_4P1K_2D, HCCL_TOPO_2P2K,
     106              :           HCCL_TOPO_2P1K, HCCL_TOPO_1P1K)
     107              : 
     108              : MAKE_ENUM(HcclDebugTestCase, HCCL_INTRA_RANK_CNT_NOTIFY, HCCL_INTRA_RANK_NOTIFY, NONE)
     109              : 
     110              : /*------------------- string to type cast functions ---------------------------------------
     111              :  *   Several template cast functions are provided.
     112              :  *   Register your customized cast functions in the second section.
     113              :  *-----------------------------------------------------------------------------------------*/
     114              : 
     115              : /*------------- common template cast functions -------------*/
     116           59 : template <class T> inline T Str2T(const std::string &s)
     117              : {
     118              :     // 检查数字长度
     119           59 :     if (s.size() > MAX_LEN_OF_DIGIT_ENV) {
     120           12 :         THROW<InvalidParamsException>(StringFormat("Invalid env len, len[%zu] should not be bigger than %u.", s.size(), MAX_LEN_OF_DIGIT_ENV));
     121              :     }
     122              :     // 检查是否为全数字
     123           53 :     if (!std::all_of(s.begin(), s.end(), ::isdigit)) {
     124           10 :         THROW<InvalidParamsException>(StringFormat("[Init][EnvVarParam]Invalid env config, [%s] contains non-digit char.", s.c_str()));
     125              :     }
     126           43 :     return String2T<T>(s);
     127              : }
     128              : 
     129            5 : template <> inline std::string Str2T<std::string>(const std::string &s)
     130              : {
     131            5 :     return s;
     132              : }
     133              : 
     134              : template <> inline bool Str2T<bool>(const std::string &s)
     135              : {
     136              :     bool        b    = true;
     137              :     std::string flag = s;
     138              :     std::transform(flag.begin(), flag.end(), flag.begin(), ::toupper);
     139              :     if (flag == "FALSE") {
     140              :         b = false;
     141              :     } else if (flag == "TRUE") {
     142              :         b = true;
     143              :     } else {
     144              :         THROW<InvalidParamsException>(StringFormat("Env config \"%s\" is not valid.", s.c_str()));
     145              :     }
     146              :     return b;
     147              : }
     148              : 
     149            5 : template <> inline IpAddress Str2T<IpAddress>(const std::string &s)
     150              : {
     151            5 :     return IpAddress(s);
     152              : }
     153              : 
     154              : /*------------------ customized cast functions ------------------*/
     155              : extern bool CastBin2Bool(const std::string &s);
     156              : 
     157              : extern SocketIfName CastSocketIfName(const std::string &s);
     158              : 
     159              : extern std::vector<HcclAlgoType> CastAlgoTypeVec(const std::string &s);
     160              : 
     161              : extern std::map<OpType, std::vector<HcclAlgoType>> SetHcclAlgoConfig(const std::string &hcclAlgo);
     162              : 
     163              : extern HcclResult SetSpecificAlgType(std::vector<std::string> &algos, std::map<OpType, std::vector<HcclAlgoType>>& hcclAlgoConfig);
     164              : 
     165              : extern HcclResult SetCommonAlgType(std::vector<std::string> &algos, std::map<OpType, std::vector<HcclAlgoType>>& hcclAlgoConfig);
     166              : 
     167              : extern HcclResult SplitHcclAlgoLevel(const std::string &algoConfig, std::vector<std::string> &algos);
     168              : 
     169              : extern HcclResult ParserHcclAlgoLevel(const std::string &algoLevel, u32 &level, HcclAlgoType &algoType);
     170              : 
     171              : extern HcclResult ParseAlgoString(std::string opName, std::string &algoString, std::vector<HcclAlgoType>& algType);
     172              : 
     173              : extern HcclResult CheckAlgoConfigValid(std::vector<std::string> &algos, bool& anyCommonConfig, bool& anySpecificConfig);
     174              : 
     175              : extern HcclResult SplitHcclOpType(const std::string &algoConfig, std::vector<std::string> &algos);
     176              : 
     177              : extern HcclDetourType CastDetourType(const std::string &s);
     178              : 
     179              : extern HcclAccelerator CastHcclAccelerator(const std::string &s);
     180              : 
     181              : extern s32 CastSocketFamily(const std::string &s);
     182              : 
     183              : extern std::string CastCannVersion(const std::string &cannEnv);
     184              : 
     185              : extern DfsConfig CastDfsConfig(const std::string &dfsConfigEnv);
     186              : 
     187              : extern u32 CastBin2UInt(const std::string &s);
     188              : 
     189              : extern void CheckSocketIfName(const SocketIfName &config);
     190              : 
     191              : extern std::vector<SocketPortRange> CastSocketPortRange(const std::string &s, const std::string &envName);
     192              : 
     193              : /*----------------------- env variable validate functions -----------------------------
     194              :  *   Several template cast functions are provided.
     195              :  *   Register your customized validate functions in the second section.
     196              :  *--------------------------------------------------------------------------------------*/
     197              : 
     198              : /*-------------- common template validate functions ------------*/
     199           41 : template <class T> void CheckRange(const T &value, const T min, const T max, bool closed = true)
     200              : {
     201           41 :     if (closed) {
     202           41 :         if (value < min || value > max) {
     203            9 :             THROW<InvalidParamsException>("value[%u] is out of range[%u, %u].", value, min, max);
     204              :         }
     205              :     }
     206           32 : }
     207              : // 为了可读性,用编译期函数封装绑定操作,使用者只需关心 T min max 三个字段
     208          425 : template <class T> constexpr std::function<void(const T &)> CHK_RANGE_CLOSED(const T min, const T max)
     209              : {
     210          425 :     return std::bind(CheckRange<T>, std::placeholders::_1, min, max, true);
     211              : }
     212              : 
     213              : /*--------------- customized validate functions ---------------*/
     214              : extern void CheckExecTimeOut(const u32 &timeOut);
     215              : 
     216              : extern void CheckFilePath(const string &filePath);
     217              : 
     218              : extern void CheckRdmaTimeout(const u32 &timeout);
     219              : 
     220              : /*----------------------- env variable post process functions --------------------------
     221              :  *   Register your customized post process functions here if necessary.
     222              :  *--------------------------------------------------------------------------------------*/
     223              : extern void SetRealPath(string &filePath);
     224              : 
     225              : extern void ProcExecTimeOut(u32 &timeOut);
     226              : 
     227              : extern void ProcRdmaTimeout(u32 &timeout);
     228              : 
     229              : extern void CheckRDMATrafficClass(const u32 &rdmaTrafficClass);
     230              : 
     231              : extern void ConvertUnitQpThreshold(u32 &multiQpThreshold);
     232              : 
     233              : } // namespace Hccl
     234              : 
     235              : #endif // HCCLV2_ENV_FUNC_H
        

Generated by: LCOV version 2.0-1