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