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 <unordered_map>
19 : #include "exception_util.h"
20 : #include "dma_mode.h"
21 : #include "ip_address.h"
22 : #include "op_type.h"
23 : #include "types.h"
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 45 : : configIfNames(configIfNames),
60 45 : searchNot(searchNot),
61 135 : searchExact(searchExact) {};
62 : };
63 :
64 : struct DfsConfig {
65 : bool taskExceptionEnable{true};
66 : bool clusterHeartBeatEnable{true};
67 : int32_t rankConsistentState{0}; // -1:off 0:first 1:on
68 : DfsConfig() = default;
69 26 : DfsConfig(bool taskException, bool clusterHeartBeatEnable, int32_t consistentState)
70 26 : : taskExceptionEnable(taskException),
71 26 : clusterHeartBeatEnable(clusterHeartBeatEnable),
72 26 : rankConsistentState(consistentState) {};
73 : };
74 :
75 : enum class NpuProtoType {
76 : TCP = 1, // 拉远TCP模式
77 : RDMA, // 拉远RDMA模式
78 : RESERVED // 拉远未进行模式使能
79 : };
80 :
81 : using SocketPortRange = struct SocketPortRangeDef {
82 : u32 min;
83 : u32 max;
84 5 : bool operator==(const SocketPortRangeDef& other) const { return min == other.min && max == other.max; }
85 : };
86 :
87 : // HCCL通信算法类型
88 1620 : MAKE_ENUM(
89 : HcclAlgoType,
90 : HCCL_ALGO_TYPE_DEFAULT, // 默认算法,配置为此时,使用HCCL内藏算法选择逻辑
91 : HCCL_ALGO_TYPE_RING, HCCL_ALGO_TYPE_PIPELINE, HCCL_ALGO_TYPE_FULLMESH, HCCL_ALGO_TYPE_HDR, HCCL_ALGO_TYPE_PAIRWISE,
92 : HCCL_ALGO_TYPE_NHR, HCCL_ALGO_TYPE_NB, HCCL_ALGO_TYPE_NULL, HCCL_ALGO_TYPE_NA, HCCL_ALGO_TYPE_NHR_V1,
93 : HCCL_ALGO_TYPE_AHC)
94 :
95 : const std::set<OpType> OP_TYPE_SET
96 : = {OpType::ALLREDUCE, OpType::BROADCAST, OpType::ALLGATHER, OpType::REDUCESCATTER, OpType::SEND,
97 : OpType::RECV, OpType::BARRIER, OpType::ALLTOALL, OpType::REDUCE, OpType::GATHER,
98 : OpType::SCATTER, OpType::ALLTOALLV, OpType::ALLTOALLVC, OpType::BATCHSENDRECV, OpType::DEBUGCASE};
99 :
100 : MAKE_ENUM(OrchestrateWay, PRIM, INS)
101 :
102 : // HCCL绕路类型
103 1111 : MAKE_ENUM(
104 : HcclDetourType,
105 : HCCL_DETOUR_DISABLE, // 绕路不使能,默认为此值
106 : HCCL_DETOUR_ENABLE_2P, // 2P间绕路
107 : HCCL_DETOUR_ENABLE_4P, // 4P间绕路
108 : HCCL_DETOUR_ENABLE_2P_AND_4P) // 2P和4P间绕路
109 :
110 : MAKE_ENUM(
111 : HcclTopoType, HCCL_TOPO_4P4K, HCCL_TOPO_4P4K_2D, HCCL_TOPO_4P1K, HCCL_TOPO_4P1K_2D, HCCL_TOPO_2P2K, HCCL_TOPO_2P1K,
112 : HCCL_TOPO_1P1K)
113 :
114 : MAKE_ENUM(HcclDebugTestCase, HCCL_INTRA_RANK_CNT_NOTIFY, HCCL_INTRA_RANK_NOTIFY, NONE)
115 :
116 : /*------------------- string to type cast functions ---------------------------------------
117 : * Several template cast functions are provided.
118 : * Register your customized cast functions in the second section.
119 : *-----------------------------------------------------------------------------------------*/
120 :
121 : /*------------- common template cast functions -------------*/
122 : template <class T>
123 59 : inline T Str2T(const std::string& s)
124 : {
125 : // 检查数字长度
126 59 : if (s.size() > MAX_LEN_OF_DIGIT_ENV) {
127 6 : THROW<InvalidParamsException>(
128 18 : StringFormat("Invalid env len, len[%zu] should not be bigger than %u.", s.size(), MAX_LEN_OF_DIGIT_ENV));
129 : }
130 : // 检查是否为全数字
131 53 : if (!std::all_of(s.begin(), s.end(), ::isdigit)) {
132 10 : THROW<InvalidParamsException>(
133 20 : StringFormat("[Init][EnvVarParam]Invalid env config, [%s] contains non-digit char.", s.c_str()));
134 : }
135 43 : return String2T<T>(s);
136 : }
137 :
138 : template <>
139 13 : inline std::string Str2T<std::string>(const std::string& s)
140 : {
141 13 : return s;
142 : }
143 :
144 : template <>
145 : inline bool Str2T<bool>(const std::string& s)
146 : {
147 : bool b = true;
148 : std::string flag = s;
149 : std::transform(flag.begin(), flag.end(), flag.begin(), ::toupper);
150 : if (flag == "FALSE") {
151 : b = false;
152 : } else if (flag == "TRUE") {
153 : b = true;
154 : } else {
155 : THROW<InvalidParamsException>(StringFormat("Env config \"%s\" is not valid.", s.c_str()));
156 : }
157 : return b;
158 : }
159 :
160 : template <>
161 5 : inline IpAddress Str2T<IpAddress>(const std::string& s)
162 : {
163 5 : return IpAddress(s);
164 : }
165 :
166 : /*------------------ customized cast functions ------------------*/
167 : extern bool CastBin2Bool(const std::string& s);
168 :
169 : extern SocketIfName CastSocketIfName(const std::string& s);
170 :
171 : extern std::vector<HcclAlgoType> CastAlgoTypeVec(const std::string& s);
172 :
173 : extern std::map<OpType, std::vector<HcclAlgoType>> SetHcclAlgoConfig(const std::string& hcclAlgo);
174 :
175 : extern HcclResult
176 : SetSpecificAlgType(std::vector<std::string>& algos, std::map<OpType, std::vector<HcclAlgoType>>& hcclAlgoConfig);
177 :
178 : extern HcclResult
179 : SetCommonAlgType(std::vector<std::string>& algos, std::map<OpType, std::vector<HcclAlgoType>>& hcclAlgoConfig);
180 :
181 : extern HcclResult SplitHcclAlgoLevel(const std::string& algoConfig, std::vector<std::string>& algos);
182 :
183 : extern HcclResult ParserHcclAlgoLevel(const std::string& algoLevel, u32& level, HcclAlgoType& algoType);
184 :
185 : extern HcclResult ParseAlgoString(std::string opName, std::string& algoString, std::vector<HcclAlgoType>& algType);
186 :
187 : extern HcclResult CheckAlgoConfigValid(std::vector<std::string>& algos, bool& anyCommonConfig, bool& anySpecificConfig);
188 :
189 : extern HcclResult SplitHcclOpType(const std::string& algoConfig, std::vector<std::string>& algos);
190 :
191 : extern HcclDetourType CastDetourType(const std::string& s);
192 :
193 : extern HcclAccelerator CastHcclAccelerator(const std::string& s);
194 :
195 : extern s32 CastSocketFamily(const std::string& s);
196 :
197 : extern std::string CastCannVersion(const std::string& cannEnv);
198 :
199 : extern DfsConfig CastDfsConfig(const std::string& dfsConfigEnv);
200 :
201 : extern u32 CastBin2UInt(const std::string& s);
202 :
203 : extern void CheckSocketIfName(const SocketIfName& config);
204 :
205 : extern std::vector<SocketPortRange> CastSocketPortRange(const std::string& s, const std::string& envName);
206 :
207 : /*----------------------- env variable validate functions -----------------------------
208 : * Several template cast functions are provided.
209 : * Register your customized validate functions in the second section.
210 : *--------------------------------------------------------------------------------------*/
211 :
212 : /*-------------- common template validate functions ------------*/
213 : template <class T>
214 41 : void CheckRange(const T& value, const T min, const T max, bool closed = true)
215 : {
216 41 : if (closed) {
217 41 : if (value < min || value > max) {
218 9 : THROW<InvalidParamsException>("value[%u] is out of range[%u, %u].", value, min, max);
219 : }
220 : }
221 32 : }
222 : // 为了可读性,用编译期函数封装绑定操作,使用者只需关心 T min max 三个字段
223 : template <class T>
224 493 : constexpr std::function<void(const T&)> CHK_RANGE_CLOSED(const T min, const T max)
225 : {
226 493 : return std::bind(CheckRange<T>, std::placeholders::_1, min, max, true);
227 : }
228 :
229 : /*--------------- customized validate functions ---------------*/
230 : extern void CheckExecTimeOut(const u32& timeOut);
231 :
232 : extern void CheckFilePath(const string& filePath);
233 :
234 : extern void CheckRdmaTimeout(const u32& timeout);
235 :
236 : /*----------------------- env variable post process functions --------------------------
237 : * Register your customized post process functions here if necessary.
238 : *--------------------------------------------------------------------------------------*/
239 : extern void SetRealPath(string& filePath);
240 :
241 : extern void ProcExecTimeOut(u32& timeOut);
242 :
243 : extern void ProcRdmaTimeout(u32& timeout);
244 :
245 : extern void CheckRDMATrafficClass(const u32& rdmaTrafficClass);
246 :
247 : extern void ConvertUnitQpThreshold(u32& multiQpThreshold);
248 :
249 : /*----------------------- multi qp src port config --------------------------*/
250 : struct MultiQpSrcPortConfig {
251 : std::string configDirPath;
252 : std::unordered_map<std::string, std::vector<std::uint16_t>> ipPairToPorts;
253 :
254 76 : bool IsAvailable() const { return !ipPairToPorts.empty(); }
255 :
256 : static constexpr u32 CONFIG_FILE_LINE_MAX = 128 * 1024;
257 : static constexpr u32 CONFIG_SRC_PORT_NUM_MAX = 32;
258 : static constexpr u32 CONFIG_SRC_PORT_ID_MAX = 65535;
259 : static constexpr u32 CONFIG_IP_NUM = 2;
260 : };
261 :
262 : extern u32
263 : GetMultiQpPortsNumByIpPair(const MultiQpSrcPortConfig& config, const IpAddress& srcIp, const IpAddress& dstIp);
264 :
265 : extern std::vector<std::uint16_t>
266 : GetMultiQpSrcPortsByIpPair(const MultiQpSrcPortConfig& config, const IpAddress& srcIp, const IpAddress& dstIp);
267 :
268 : } // namespace Hccl
269 :
270 : #endif // HCCLV2_ENV_FUNC_H
|