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 : #include "topoinfo_ranktableStandard.h"
12 :
13 : #include <map>
14 : #include <vector>
15 : #include <string>
16 : #include <fstream>
17 : #include <algorithm>
18 : #include <unistd.h>
19 : #include <chrono>
20 : #include <iostream>
21 : #include <arpa/inet.h>
22 :
23 : // ltm指定config路径
24 : #include "common/src/config.h"
25 : #include "workflow_pub.h"
26 :
27 : using namespace std;
28 : using namespace hccl;
29 :
30 :
31 387 : TopoinfoRanktableStandard::TopoinfoRanktableStandard(const std::string &rankTableM, const std::string &identify)
32 387 : : TopoInfoRanktableParser(rankTableM, identify)
33 : {
34 387 : }
35 :
36 774 : TopoinfoRanktableStandard::~TopoinfoRanktableStandard()
37 : {
38 774 : }
39 :
40 387 : HcclResult TopoinfoRanktableStandard::Init()
41 : {
42 387 : CHK_RET(LoadRankTableString(rankTableFile_));
43 387 : HcclResult ret = ParserClusterInfo(params_, rankTable_);
44 387 : if (ret != HCCL_SUCCESS) {
45 1 : HCCL_ERROR("[TopoinfoRanktableStandard][Init] RanktableStandard init failed! Please check if the ranktable file"
46 : " is configured with a 'version' field.");
47 1 : return ret;
48 : }
49 386 : return HCCL_SUCCESS;
50 : }
51 :
52 386 : HcclResult TopoinfoRanktableStandard::GetSelfClusterInfo(HcclCommParams ¶ms)
53 : {
54 : // 获取芯片类型信息
55 386 : CHK_RET(hrtGetDeviceType(params.deviceType));
56 386 : params.rank = params_.rank;
57 386 : params.userRank = params_.rank;
58 386 : params.logicDevId = params_.logicDevId;
59 386 : params.totalRanks = params_.totalRanks;
60 386 : params.serverId = params_.serverId;
61 386 : return HCCL_SUCCESS;
62 : }
63 :
64 386 : HcclResult TopoinfoRanktableStandard::GetClusterInfo(hccl::HcclCommParams ¶ms,
65 : hccl::RankTable_t &rankTable)
66 : {
67 386 : CHK_RET(GetClusterInfo(rankTable));
68 386 : CHK_RET(GetSelfClusterInfo(params));
69 386 : return HCCL_SUCCESS;
70 : }
71 386 : HcclResult TopoinfoRanktableStandard::GetClusterInfo(RankTable_t &clusterInfo)
72 : {
73 386 : clusterInfo.nicDeploy = rankTable_.nicDeploy;
74 386 : clusterInfo.deviceNum = rankTable_.deviceNum;
75 386 : clusterInfo.serverNum = rankTable_.serverNum;
76 386 : clusterInfo.groupNum = rankTable_.groupNum;
77 386 : clusterInfo.nicNum = rankTable_.nicNum;
78 386 : clusterInfo.nicNames = rankTable_.nicNames;
79 385 : clusterInfo.rankNum = rankTable_.rankNum;
80 385 : clusterInfo.rankList = rankTable_.rankList;
81 386 : clusterInfo.serverList = rankTable_.serverList;
82 :
83 386 : return HCCL_SUCCESS;
84 : }
85 :
86 387 : HcclResult TopoinfoRanktableStandard::ParserClusterInfo(hccl::HcclCommParams ¶ms, hccl::RankTable_t &rankTable)
87 : {
88 387 : CHK_RET(GetDeployMode(cloudFlag_));
89 387 : HCCL_INFO("deploy mode is %s", cloudFlag_ ? "cloud" : "Laborratory");
90 387 : if (!IsTaskNumCalMode()) {
91 387 : CHK_RET(hrtGetDeviceType(params.deviceType));
92 : }
93 :
94 387 : u32 rankId = INVALID_VALUE_RANKID;
95 387 : if (cloudFlag_) {
96 0 : CHK_RET(GetCloudHcomInfo(params, rankTable, identify_, rankId));
97 : } else {
98 387 : if (!IsTaskNumCalMode()) {
99 387 : CHK_RET(CheckRankId(identify_.c_str()));
100 387 : if (SalStrToULong(identify_, HCCL_BASE_DECIMAL, rankId) != HCCL_SUCCESS) {
101 0 : RPT_INPUT_ERR(true, "EI0014", std::vector<std::string>({ "value", "variable" ,"expect" }),
102 : std::vector<std::string>({ identify_, "rank_id", "a valid integer." }));
103 0 : HCCL_ERROR("[%s][%s]errNo[0x%016llx] identify[%s] is invalid", LOG_KEYWORDS_INIT_GROUP.c_str(),
104 : LOG_KEYWORDS_RANKTABLE_CHECK.c_str(), HCOM_ERROR_CODE(HCCL_E_PARA), identify_.c_str());
105 0 : return HCCL_E_PARA;
106 : }
107 : }
108 387 : CHK_RET(GetHcomInfo(params, rankTable));
109 : }
110 :
111 387 : if (!IsTaskNumCalMode()) {
112 387 : std::sort(rankTable.rankList.begin(), rankTable.rankList.end(),
113 1499 : [&](const RankInfo_t &a, const RankInfo_t &b) -> bool {return a.rankId < b.rankId;});
114 :
115 : // 校验rank id合法性
116 387 : if (rankId >= rankTable.rankList.size()) {
117 14 : RPT_INPUT_ERR(true,
118 : "EI0014",
119 : std::vector<std::string>({ "value", "variable" ,"expect" }),
120 : std::vector<std::string>({identify_, "rank_id", "a valid integer"}));
121 1 : HCCL_ERROR("[%s][%s]rankid[%u] is invalid", LOG_KEYWORDS_INIT_GROUP.c_str(),
122 : LOG_KEYWORDS_RANKTABLE_CHECK.c_str(), rankId);
123 1 : return HCCL_E_PARA;
124 : }
125 386 : CHK_PRT_RET(rankId != rankTable.rankList[rankId].rankId,
126 : HCCL_ERROR("[Parse][ClusterInfo]check rankList[%u] rankId[%u] failed",
127 : rankId, rankTable.rankList[rankId].rankId), HCCL_E_UNAVAIL);
128 386 : u32 devId = rankTable.rankList[rankId].deviceInfo.devicePhyId;
129 386 : CHK_RET(hrtGetDevice(¶ms.logicDevId));
130 :
131 386 : u32 devicePhyId = 0;
132 386 : CHK_RET(hrtGetDevicePhyIdByIndex(static_cast<u32>(params.logicDevId), devicePhyId));
133 :
134 386 : CHK_PRT_RET(devicePhyId != static_cast<u32>(devId),
135 : HCCL_ERROR("[Parse][ClusterInfo]ranktable config devId[%d],but local devId[%u]", devId,
136 : devicePhyId), HCCL_E_UNAVAIL);
137 :
138 386 : params.rank = rankId;
139 386 : params.totalRanks = rankTable.rankNum;
140 386 : params.serverId = rankTable.rankList[rankId].serverId;
141 : }
142 386 : return HCCL_SUCCESS;
143 2 : }
144 :
145 387 : HcclResult TopoinfoRanktableStandard::GetHcomInfo(hccl::HcclCommParams ¶ms, hccl::RankTable_t &rankTable)
146 : {
147 : // para_plane_location
148 387 : std::string paraPlaneLocation;
149 387 : CHK_RET(GetJsonProperty(fileContent_, "para_plane_nic_location", paraPlaneLocation, false));
150 :
151 387 : HCCL_DEBUG("%s.json -> para_plane_location: %s", fileName_.c_str(), paraPlaneLocation.c_str());
152 :
153 387 : if (paraPlaneLocation == "host") { // 不支持host 网卡
154 0 : HCCL_ERROR("[Get][HcomInfo]errNo[0x%016llx] host nic is unsupported", HCOM_ERROR_CODE(HCCL_E_PARA));
155 0 : return HCCL_E_PARA;
156 : }
157 :
158 138 : if ((params.deviceType == DevType::DEV_TYPE_910 || params.deviceType == DevType::DEV_TYPE_910B ||
159 525 : params.deviceType == DevType::DEV_TYPE_910_93) && paraPlaneLocation != "device") {
160 0 : HCCL_ERROR("[Get][HcomInfo]errNo[0x%016llx] paraPlaneLocation should be 'device'",
161 : HCOM_ERROR_CODE(HCCL_E_PARA));
162 0 : return HCCL_E_PARA;
163 : }
164 : // 当前只支持device侧的网卡
165 387 : rankTable.nicDeploy = ((paraPlaneLocation == "device") ? NICDeployment::NIC_DEPLOYMENT_DEVICE :
166 : NICDeployment::NIC_DEPLOYMENT_RESERVED);
167 : // group_count
168 387 : std::string groupCount;
169 387 : CHK_RET(GetJsonProperty(fileContent_, "group_count", groupCount, false));
170 :
171 387 : HCCL_DEBUG("%s.json -> group_count: %s", fileName_.c_str(), groupCount.c_str());
172 387 : CHK_RET(SalStrToULong(groupCount, HCCL_BASE_DECIMAL, rankTable.groupNum));
173 : // 校验groupCount ,groupCount不能为0
174 387 : if (rankTable.groupNum == 0) {
175 0 : HCCL_ERROR("[Get][HcomInfo]errNo[0x%016llx] groupNum is zero", HCOM_ERROR_CODE(HCCL_E_PARA));
176 0 : return HCCL_E_PARA;
177 : }
178 : // group_list
179 387 : CHK_RET(GetGroupList(params, rankTable));
180 :
181 387 : return HCCL_SUCCESS;
182 387 : }
183 :
184 0 : HcclResult TopoinfoRanktableStandard::GetServerList(const nlohmann::json &obj, u32 objIndex,
185 : hccl::RankTable_t &rankTable, u32 serverNum)
186 : {
187 0 : if (serverNum == 0) {
188 0 : HCCL_ERROR("[Get][ServerList]errNo[0x%016llx] server num is zero", HCOM_ERROR_CODE(HCCL_E_PARA));
189 0 : return HCCL_E_PARA;
190 : }
191 0 : HCCL_DEBUG("get serverList[%u]", objIndex);
192 0 : rankTable.serverList.clear();
193 0 : nlohmann::json serverList;
194 0 : CHK_RET(GetJsonArrayMemberProperty(obj, objIndex, "server_list", serverList, false));
195 :
196 0 : HCCL_DEBUG("%s.json -> server_list[%u]: size:%zu", fileName_.c_str(), objIndex, serverList.size());
197 0 : if (serverList.size() == 0) {
198 0 : HCCL_ERROR("[Get][ServerList]errNo[0x%016llx] serverList[%u] size is zero",
199 : HCOM_ERROR_CODE(HCCL_E_PARA), objIndex);
200 0 : return HCCL_E_PARA;
201 : }
202 0 : if (serverList.size() != serverNum) {
203 0 : HCCL_ERROR("[Get][ServerList]errNo[0x%016llx] serverList[%u] size[%zu] neq server num[%u]",
204 : HCOM_ERROR_CODE(HCCL_E_PARA), objIndex, serverList.size(), serverNum);
205 0 : return HCCL_E_PARA;
206 : }
207 :
208 0 : for (u32 index = 0; index < serverList.size(); index++) {
209 0 : CHK_RET(GetSingleServer(serverList, index, rankTable));
210 : }
211 :
212 0 : return HCCL_SUCCESS;
213 0 : }
214 :
215 0 : HcclResult TopoinfoRanktableStandard::GetSingleServer(const nlohmann::json &serverListObj, u32 objIndex,
216 : hccl::RankTable_t &rankTable)
217 : {
218 0 : ServerInfo_t serverInfo;
219 0 : std::string serverId;
220 0 : CHK_RET(GetJsonArrayMemberProperty(serverListObj, objIndex, "server_id", serverId, false));
221 :
222 0 : CHK_RET(CheckUniqueAndInsertPool(JsonUniqueInfoType::UNIQUE_INFO_TYPE_SERVER_ID, serverId,
223 : JsonCheckOpType::CHECK_OP_TYPE_INSERT));
224 0 : HCCL_DEBUG("server id[%u]:[%s]", objIndex, serverId.c_str());
225 0 : serverInfo.serverId = serverId;
226 : // 解析内层-参数平面的网卡信息
227 0 : nlohmann::json paraPlaneInfo;
228 0 : CHK_RET(GetJsonArrayMemberProperty(serverListObj, objIndex, "para_plane_info", paraPlaneInfo, false));
229 : // server list中的网卡个数应该与ranktable中的网卡个数一致
230 0 : if (paraPlaneInfo.size() != rankTable.nicNum) {
231 0 : HCCL_ERROR("[Get][SingleServer]errNo[0x%016llx] paraPlaneInfo[%u] size[%zu] neq nicNum[%u]",
232 : HCOM_ERROR_CODE(HCCL_E_PARA), objIndex, paraPlaneInfo.size(), rankTable.nicNum);
233 0 : return HCCL_E_PARA;
234 : }
235 0 : serverInfo.networkInfo.clear();
236 :
237 0 : for (u32 innerIndex = 0; innerIndex < rankTable.nicNames.size(); innerIndex++) {
238 0 : NetworkInfo_t networdInfo;
239 0 : networdInfo.ethName = rankTable.nicNames[innerIndex];
240 :
241 : // 依照nicNames来搜索
242 0 : for (u32 i = 0; i < paraPlaneInfo.size(); i++) {
243 0 : auto findEth = paraPlaneInfo.at(i).find(networdInfo.ethName);
244 0 : if (findEth != paraPlaneInfo.at(i).end()) { // 找到ethName
245 0 : std::string ethIp = findEth->get<std::string>();
246 0 : CHK_RET(CheckUniqueAndInsertPool(JsonUniqueInfoType::UNIQUE_INFO_TYPE_ETH_IP, ethIp,
247 : JsonCheckOpType::CHECK_OP_TYPE_INSERT));
248 0 : CHK_RET(ConvertIpAddress(ethIp, networdInfo.ipAddr));
249 0 : break;
250 0 : }
251 : }
252 0 : if (networdInfo.ipAddr.IsInvalid()) {
253 0 : HCCL_ERROR("[Get][SingleServer]errNo[0x%016llx] networdInfo [%s] ipAddr is invalid",
254 : HCOM_ERROR_CODE(HCCL_E_PARA), networdInfo.ethName.c_str());
255 0 : return HCCL_E_PARA;
256 : }
257 0 : HCCL_DEBUG("networdInfo[%u] [%s] ipAddr[%s]", objIndex, networdInfo.ethName.c_str(), \
258 : networdInfo.ipAddr.GetReadableAddress());
259 0 : serverInfo.networkInfo.push_back(networdInfo);
260 0 : }
261 :
262 0 : rankTable.serverList.push_back(serverInfo);
263 0 : return HCCL_SUCCESS;
264 0 : }
265 :
266 0 : HcclResult TopoinfoRanktableStandard::GetCloudHcomInfo(hccl::HcclCommParams ¶ms, hccl::RankTable_t &rankTable,
267 : const std::string &identify, u32 &rank)
268 : {
269 0 : HCCL_DEBUG("get cloud hcom info: identify[%s]", identify.c_str());
270 : // group_count
271 0 : std::string groupCount;
272 0 : CHK_RET(GetJsonProperty(fileContent_, "group_count", groupCount, false));
273 0 : HCCL_DEBUG("%s.json -> group_count: %s", fileName_.c_str(), groupCount.c_str());
274 :
275 0 : CHK_RET(SalStrToULong(groupCount, HCCL_BASE_DECIMAL, rankTable.groupNum));
276 : // 校验groupCount ,groupCount不能为0
277 0 : if (rankTable.groupNum == 0) {
278 0 : HCCL_ERROR("[Get][CloudHcomInfo]errNo[0x%016llx] group num is zero", HCOM_ERROR_CODE(HCCL_E_PARA));
279 0 : return HCCL_E_PARA;
280 : }
281 0 : devMap_.clear();
282 : // group_list
283 0 : CHK_RET(GetGroupList(params, rankTable));
284 0 : CHK_RET(GetSortClouldRankList(rankTable));
285 :
286 0 : if (!IsTaskNumCalMode()) {
287 : // 获取当前操作的逻辑ID并转换为物理ID
288 0 : s32 deviceLogicId = -1; // device logic id 的无效值
289 0 : CHK_RET(hrtGetDevice(&deviceLogicId));
290 :
291 0 : u32 devicePhyId = INVALID_UINT; // device phy id 的无效值
292 0 : CHK_RET(hrtGetDevicePhyIdByIndex(static_cast<u32>(deviceLogicId), devicePhyId));
293 :
294 0 : for (u32 index = 0; index < rankTable.rankList.size(); index++) {
295 0 : HCCL_INFO(" rank: %u phyId:%u identify:%s podName:%s", rank, devicePhyId, identify.c_str(),
296 : rankTable.rankList[index].podName.c_str());
297 0 : if ((rankTable.rankList[index].podName == identify) &&
298 0 : (rankTable.rankList[index].deviceInfo.devicePhyId == static_cast<s32>(devicePhyId))) {
299 0 : rank = rankTable.rankList[index].rankId;
300 0 : break;
301 : }
302 : }
303 : }
304 :
305 0 : CHK_RET(GetDevNum(rankTable.rankList, rankTable.deviceNum));
306 0 : rankTable.serverNum = devMap_.size();
307 0 : rankTable.nicDeploy = NICDeployment::NIC_DEPLOYMENT_DEVICE; // 910A 对应的nicDeploy
308 0 : rankTable.rankNum = rankTable.rankList.size();
309 :
310 : // alg_type
311 0 : HCCL_INFO("%s.json -> rank %u : deviceNum is %u, serverNum is %u, nicDeploy is %u, rankNum is %u",
312 : fileName_.c_str(), rank, rankTable.deviceNum, rankTable.serverNum,
313 : rankTable.nicDeploy, rankTable.rankNum);
314 :
315 0 : return HCCL_SUCCESS;
316 0 : }
317 :
318 :
319 0 : HcclResult TopoinfoRanktableStandard::GetSortClouldRankList(hccl::RankTable_t &rankTable)
320 : {
321 : // sort device id in each server
322 0 : for (auto iter = devMap_.begin(); iter != devMap_.end(); iter++) {
323 0 : if (!(iter->second).empty()) {
324 0 : std::sort((iter->second).begin(), (iter->second).end(), [&](const RankInfo_t &a,
325 0 : const RankInfo_t &b) -> bool {return a.deviceInfo.devicePhyId < b.deviceInfo.devicePhyId;});
326 : }
327 : }
328 :
329 : // sort rank and filling rankList
330 0 : rankTable.rankList.clear();
331 0 : u32 initialRank = 0;
332 0 : for (auto iterMap = devMap_.begin(); iterMap != devMap_.end(); iterMap++) {
333 0 : if (!(iterMap->second).empty()) {
334 0 : for (u32 vecIndex = 0; vecIndex < (iterMap->second).size(); vecIndex++) {
335 0 : (iterMap->second)[vecIndex].rankId = initialRank;
336 0 : rankTable.rankList.push_back((iterMap->second)[vecIndex]);
337 0 : initialRank++;
338 : }
339 : }
340 : }
341 0 : return HCCL_SUCCESS;
342 : }
343 :
344 :
345 387 : HcclResult TopoinfoRanktableStandard::GetSingleGroupDeviceCount(nlohmann::json &obj, u32 objIndex,
346 : hccl::RankTable_t &rankTable, u32 &deviceNum)
347 : {
348 : // device_num
349 387 : std::string strDeviceNum;
350 :
351 387 : if (!cloudFlag_) {
352 387 : CHK_RET(GetJsonArrayMemberProperty(obj, objIndex, "device_num", strDeviceNum, false));
353 : } else {
354 0 : CHK_RET(GetJsonArrayMemberProperty(obj, objIndex, "device_count", strDeviceNum, false));
355 : }
356 387 : HCCL_DEBUG("%s.json -> device_num: %s", fileName_.c_str(), strDeviceNum.c_str());
357 387 : CHK_RET(SalStrToULong(strDeviceNum, HCCL_BASE_DECIMAL, deviceNum));
358 387 : if (deviceNum == 0) {
359 0 : HCCL_ERROR("[Get][SingleGroupDeviceCount]errNo[0x%016llx] device num is zero", HCOM_ERROR_CODE(HCCL_E_PARA));
360 0 : return HCCL_E_PARA;
361 : }
362 387 : rankTable.deviceNum += deviceNum;
363 :
364 387 : return HCCL_SUCCESS;
365 387 : }
366 :
367 387 : HcclResult TopoinfoRanktableStandard::GetLabSingleGroup(nlohmann::json &obj, u32 objIndex, hccl::HcclCommParams ¶ms,
368 : hccl::RankTable_t &rankTable, u32 instanceNum)
369 : {
370 387 : u32 uDeviceNum = 0;
371 387 : u32 uServerNum = 0;
372 : // device_num
373 387 : CHK_RET(GetSingleGroupDeviceCount(obj, objIndex, rankTable, uDeviceNum));
374 :
375 : // server_num
376 387 : std::string serverNum;
377 387 : CHK_RET(GetJsonArrayMemberProperty(obj, objIndex, "server_num", serverNum, false));
378 387 : HCCL_DEBUG("%s.json -> server_num: %s", fileName_.c_str(), serverNum.c_str());
379 :
380 387 : CHK_RET(SalStrToULong(serverNum, HCCL_BASE_DECIMAL, uServerNum));
381 387 : if (uServerNum == 0) {
382 0 : HCCL_ERROR("[Get][LabSingleGroup]errNo[0x%016llx] server num is zero", HCOM_ERROR_CODE(HCCL_E_PARA));
383 0 : return HCCL_E_PARA;
384 : }
385 387 : rankTable.serverNum += uServerNum;
386 :
387 387 : if (params.deviceType != DevType::DEV_TYPE_310P3) {
388 387 : CHK_RET(CheckAverageDev(uDeviceNum, uServerNum));
389 : }
390 : // server_list
391 387 : if (static_cast<u32>(rankTable.nicDeploy) == 0) { // 网卡挂载在host侧
392 0 : CHK_RET(GetServerList(obj, objIndex, rankTable, uServerNum));
393 : }
394 :
395 387 : if (instanceNum != uDeviceNum) {
396 0 : HCCL_ERROR("[Get][LabSingleGroup]errNo[0x%016llx] instance num error", HCOM_ERROR_CODE(HCCL_E_PARA));
397 0 : return HCCL_E_PARA;
398 : }
399 387 : rankTable.rankNum += instanceNum;
400 :
401 387 : return HCCL_SUCCESS;
402 387 : }
403 :
404 :
405 387 : HcclResult TopoinfoRanktableStandard::GetGroupList(hccl::HcclCommParams ¶ms, hccl::RankTable_t &rankTable)
406 : {
407 387 : rankTable.rankList.clear();
408 387 : nlohmann::json groupList;
409 386 : CHK_RET(GetJsonProperty(fileContent_, "group_list", groupList, false));
410 :
411 385 : HCCL_DEBUG("group_list.size[%zu] groupNum[%u]", groupList.size(), rankTable.groupNum);
412 387 : CHK_PRT_RET(groupList.size() != rankTable.groupNum, HCCL_ERROR("[Get][GroupList]errNo[0x%016llx] "\
413 : "groupList size[%zu] error, groupNum[%u]", HCOM_ERROR_CODE(HCCL_E_PARA), groupList.size(),
414 : rankTable.groupNum), HCCL_E_PARA);
415 :
416 387 : rankTable.deviceNum = 0;
417 387 : rankTable.serverNum = 0;
418 387 : rankTable.rankNum = 0;
419 774 : for (u32 index = 0; index < groupList.size(); index++) {
420 : // group_name
421 387 : std::string groupName;
422 387 : CHK_RET(GetJsonArrayMemberProperty(groupList, index, "group_name", groupName, false));
423 :
424 387 : CHK_RET(CheckUniqueAndInsertPool(JsonUniqueInfoType::UNIQUE_INFO_TYPE_GROUP_NAME, groupName,
425 : JsonCheckOpType::CHECK_OP_TYPE_INSERT));
426 387 : HCCL_DEBUG("%s.json -> group_name: %s", fileName_.c_str(), groupName.c_str());
427 :
428 : // instance_count
429 387 : std::string instanceCount;
430 387 : CHK_RET(GetJsonArrayMemberProperty(groupList, index, "instance_count", instanceCount, false));
431 387 : HCCL_DEBUG("%s.json -> rank_count: %s", fileName_.c_str(), instanceCount.c_str());
432 :
433 387 : u32 instanceNum = 0;
434 387 : CHK_RET(SalStrToULong(instanceCount, HCCL_BASE_DECIMAL, instanceNum));
435 387 : if (instanceNum == 0) {
436 0 : HCCL_ERROR("[Get][GroupList]errNo[0x%016llx] instance num[%u] invalid", HCOM_ERROR_CODE(HCCL_E_PARA),
437 : instanceNum);
438 0 : return HCCL_E_PARA;
439 : }
440 387 : u32 deviceNum = 0;
441 387 : if (!cloudFlag_) {
442 387 : CHK_RET(GetLabSingleGroup(groupList, index, params, rankTable, instanceNum));
443 387 : deviceNum = instanceNum;
444 : } else {
445 0 : CHK_RET(GetSingleGroupDeviceCount(groupList, index, rankTable, deviceNum));
446 : }
447 :
448 387 : nlohmann::json instanceList;
449 387 : CHK_RET(GetJsonArrayMemberProperty(groupList, index, "instance_list", instanceList, false));
450 :
451 387 : CHK_RET(GetInstanceList(instanceList, params, rankTable, instanceNum, deviceNum));
452 387 : }
453 :
454 387 : return HCCL_SUCCESS;
455 387 : }
456 :
457 387 : HcclResult TopoinfoRanktableStandard::GetInstanceList(nlohmann::json &instanceList, hccl::HcclCommParams ¶ms,
458 : hccl::RankTable_t &rankTable, u32 instanceNum, u32 deviceNum)
459 : {
460 387 : HCCL_DEBUG("get instanceList: instanceNum[%u], deviceNum[%u]", instanceNum, deviceNum);
461 387 : u32 checkCount = 0;
462 387 : u32 checkDevCount = 0;
463 :
464 1524 : for (u32 podIndex = 0; podIndex < instanceList.size(); podIndex++) {
465 1137 : std::string serverId;
466 1137 : CHK_RET(GetJsonArrayMemberProperty(instanceList, podIndex, "server_id", serverId, false));
467 1135 : HCCL_DEBUG("%s.json -> server_id: %s", fileName_.c_str(), serverId.c_str());
468 1137 : if ((!cloudFlag_) && (static_cast<u32>(rankTable.nicDeploy) == 0)) {
469 0 : CHK_RET(CheckUniqueAndInsertPool(JsonUniqueInfoType::UNIQUE_INFO_TYPE_SERVER_ID, serverId,
470 : JsonCheckOpType::CHECK_OP_TYPE_FIND));
471 : }
472 :
473 1137 : if (serverId.empty()) {
474 0 : HCCL_ERROR("[Get][GetInstanceList]errNo[0x%016llx] serverId[%s] is empty",
475 : HCOM_ERROR_CODE(HCCL_E_PARA), serverId.c_str());
476 0 : return HCCL_E_PARA;
477 : }
478 :
479 1137 : if (serverId.length() > SERVERID_MAX_LENGTH) {
480 0 : HCCL_ERROR("[Get][GetInstanceList]errNo[0x%016llx] serverId[%s] length[%u] is more than %u",
481 : HCOM_ERROR_CODE(HCCL_E_PARA), serverId.c_str(), serverId.length(), SERVERID_MAX_LENGTH);
482 0 : return HCCL_E_PARA;
483 : }
484 :
485 : u32 serverIdx;
486 1137 : GenerateServerIdx(serverId, serverIdx);
487 1136 : HCCL_DEBUG("instance id[%u]:[%s], serverIdx[%u]", podIndex, serverId.c_str(), serverIdx);
488 :
489 1137 : nlohmann::json deviceList;
490 1137 : CHK_RET(GetJsonArrayMemberProperty(instanceList, podIndex, "devices", deviceList, false));
491 1135 : if (cloudFlag_) {
492 0 : CHK_RET(GetCloudDevList(instanceList, podIndex, deviceList, serverId, serverIdx));
493 : } else {
494 1135 : CHK_RET(GetDevList(instanceList, podIndex, deviceList, params, rankTable, serverId, serverIdx));
495 : }
496 1136 : checkDevCount = checkDevCount + deviceList.size();
497 1136 : checkCount++;
498 1136 : }
499 :
500 387 : HCCL_DEBUG("instance_num %u, check_count %u", instanceNum, checkCount);
501 387 : bool hcclCheck = (instanceNum != checkCount) || (deviceNum != checkDevCount);
502 387 : CHK_PRT_RET(hcclCheck,
503 : HCCL_ERROR("[Get][InstanceList]errNo[0x%016llx] check instanceNum[%u] or devNum[%u] error, checkInstance[%u], "\
504 : "checkDev[%u]", HCOM_ERROR_CODE(HCCL_E_PARA), instanceNum, deviceNum, checkCount,
505 : checkDevCount), HCCL_E_PARA);
506 :
507 387 : return HCCL_SUCCESS;
508 : }
509 :
510 0 : HcclResult TopoinfoRanktableStandard::GetCloudDevList(nlohmann::json &instanceList, u32 podIndex,
511 : nlohmann::json &deviceList, std::string &serverId, u32 &serverIdx)
512 : {
513 0 : std::string podName;
514 0 : CHK_RET(GetJsonArrayMemberProperty(instanceList, podIndex, "pod_name", podName, false));
515 0 : HCCL_DEBUG("%s.json -> pod_name: %s", fileName_.c_str(), podName.c_str());
516 :
517 0 : CHK_RET(CheckUniqueAndInsertPool(JsonUniqueInfoType::UNIQUE_INFO_TYPE_POD_NAME, podName,
518 : JsonCheckOpType::CHECK_OP_TYPE_INSERT));
519 0 : for (u32 deviceIndex = 0; deviceIndex < deviceList.size(); deviceIndex++) {
520 0 : std::string strDevid;
521 0 : CHK_RET(GetJsonArrayMemberProperty(deviceList, deviceIndex, "device_id", strDevid, false));
522 :
523 0 : u32 devicePhyId = 0;
524 0 : CHK_RET(SalStrToULong(strDevid, HCCL_BASE_DECIMAL, devicePhyId));
525 0 : if (devicePhyId > (HCCL_AISERVER_DEVICE_NUM - 1)) { // deviceid in 0 ~ 7
526 0 : HCCL_ERROR("[Get][CloudDevList]errNo[0x%016llx] devicePhyId[%u] more than [%u] is invalid",
527 : HCOM_ERROR_CODE(HCCL_E_PARA), devicePhyId, HCCL_AISERVER_DEVICE_NUM - 1);
528 0 : return HCCL_E_PARA;
529 : }
530 0 : HCCL_DEBUG("%s.json -> device_id: %s", fileName_.c_str(), strDevid.c_str());
531 :
532 0 : RankInfo_t rankinfo;
533 : // 1.非cloud场景下,网卡挂载在device侧2.cloud场景
534 0 : HcclIpAddress ipAddr;
535 0 : if (instanceList.size() > 1) {
536 0 : std::string deviceIp;
537 0 : CHK_RET(GetJsonArrayMemberProperty(deviceList, deviceIndex, "device_ip", deviceIp, false));
538 0 : HCCL_DEBUG("%s.json -> device_ip: %s", fileName_.c_str(), deviceIp.c_str());
539 0 : if (!deviceIp.empty()) {
540 0 : CHK_RET(CheckUniqueAndInsertPool(JsonUniqueInfoType::UNIQUE_INFO_TYPE_DEVICE_IP, deviceIp,
541 : JsonCheckOpType::CHECK_OP_TYPE_INSERT));
542 :
543 0 : HcclResult ret = ConvertIpAddress(deviceIp, ipAddr);
544 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
545 : HCCL_ERROR("[Get][CloudDevList]deviceIp[%s] is invalid", deviceIp.c_str()), ret);
546 : }
547 0 : } else {
548 0 : HCCL_INFO("single server don't need devIP");
549 : }
550 0 : rankinfo.deviceInfo.deviceIp.push_back(ipAddr);
551 0 : rankinfo.serverId = serverId;
552 0 : rankinfo.serverIdx = serverIdx;
553 0 : rankinfo.deviceInfo.devicePhyId = devicePhyId;
554 0 : rankinfo.podName = podName;
555 :
556 : // 回填dev_map_
557 0 : auto iter = devMap_.find(serverId);
558 0 : if (iter != devMap_.end()) {
559 0 : iter->second.push_back(rankinfo); // 存在该服务器内相关dev的对应信息
560 : } else {
561 0 : std::vector<RankInfo_t> vecDev;
562 0 : vecDev.push_back(rankinfo);
563 0 : devMap_.insert(std::make_pair(serverId, vecDev)); // 不存在则新增一条map记录
564 0 : }
565 0 : HCCL_DEBUG("%s.json->serverId[%s], podName[%s], devicePhyId[%d]", fileName_.c_str(),
566 : rankinfo.serverId.c_str(), rankinfo.podName.c_str(), rankinfo.deviceInfo.devicePhyId);
567 0 : }
568 0 : return HCCL_SUCCESS;
569 0 : }
570 :
571 1136 : HcclResult TopoinfoRanktableStandard::GetDevList(nlohmann::json &instanceList, u32 podIndex,
572 : nlohmann::json &deviceList, hccl::HcclCommParams ¶ms, hccl::RankTable_t &rankTable,
573 : std::string &serverId, u32 &serverIdx)
574 : {
575 1136 : std::string rankId;
576 1136 : CHK_RET(GetJsonArrayMemberProperty(instanceList, podIndex, "rank_id", rankId, false));
577 1137 : HCCL_DEBUG("%s.json -> rankId: %s", fileName_.c_str(), rankId.c_str());
578 2273 : for (u32 deviceIndex = 0; deviceIndex < deviceList.size(); deviceIndex++) {
579 1137 : std::string strDevid;
580 1137 : CHK_RET(GetJsonArrayMemberProperty(deviceList, deviceIndex, "device_id", strDevid, false));
581 :
582 1135 : u32 devicePhyId = 0;
583 1135 : CHK_RET(SalStrToULong(strDevid, HCCL_BASE_DECIMAL, devicePhyId));
584 1137 : if ((params.deviceType != DevType::DEV_TYPE_310P3 &&
585 1137 : params.deviceType != DevType::DEV_TYPE_910B &&
586 473 : params.deviceType != DevType::DEV_TYPE_910_93) &&
587 440 : (devicePhyId > (HCCL_AISERVER_DEVICE_NUM - 1))) {
588 0 : HCCL_ERROR("[Get][DevList]errNo[0x%016llx] device_id[%u] more than 7 is invalid",
589 : HCOM_ERROR_CODE(HCCL_E_PARA), devicePhyId);
590 0 : return HCCL_E_PARA;
591 : }
592 1137 : HCCL_DEBUG("%s.json -> device_id: %s", fileName_.c_str(), strDevid.c_str());
593 :
594 1137 : RankInfo_t rankinfo;
595 : // 1.非cloud场景下,网卡挂载在device侧2.cloud场景
596 : // 推荐网络场景,单servere需要使用RDMA网卡
597 1137 : HcclIpAddress ipAddr;
598 1136 : if (rankTable.nicDeploy == NICDeployment::NIC_DEPLOYMENT_DEVICE &&
599 1136 : (rankTable.serverNum > 0)) {
600 1136 : std::string deviceIp;
601 1136 : CHK_RET(GetJsonArrayMemberProperty(deviceList, deviceIndex, "device_ip", deviceIp, false));
602 1135 : HCCL_DEBUG("%s.json -> device_ip: %s", fileName_.c_str(), deviceIp.c_str());
603 1137 : if (deviceIp.compare("") != 0) {
604 1137 : CHK_RET(CheckUniqueAndInsertPool(JsonUniqueInfoType::UNIQUE_INFO_TYPE_DEVICE_IP, deviceIp,
605 : JsonCheckOpType::CHECK_OP_TYPE_INSERT));
606 1136 : HcclResult ret = ConvertIpAddress(deviceIp, ipAddr);
607 1137 : CHK_PRT_RET(ret != HCCL_SUCCESS,
608 : HCCL_ERROR("[Get][DevList]deviceIp[%s] is invalid", deviceIp.c_str()), ret);
609 : }
610 2274 : } else {
611 0 : HCCL_INFO("single server don't need devIP");
612 : }
613 1137 : rankinfo.deviceInfo.deviceIp.push_back(ipAddr);
614 :
615 1137 : rankinfo.serverId = serverId;
616 1137 : rankinfo.serverIdx = serverIdx;
617 1137 : rankinfo.deviceInfo.devicePhyId = devicePhyId;
618 1137 : if (SalStrToULong(rankId, HCCL_BASE_DECIMAL, rankinfo.rankId) != HCCL_SUCCESS) {
619 0 : RPT_INPUT_ERR(true, "EI0014", std::vector<std::string>({ "value", "variable" ,"expect" }),
620 : std::vector<std::string>({ rankId, "rank_id", "a valid integer" }));
621 0 : HCCL_ERROR("[%s][%s]errNo[0x%016llx] rankid[%s] is invalid", LOG_KEYWORDS_INIT_GROUP.c_str(),
622 : LOG_KEYWORDS_RANKTABLE_CHECK.c_str(), HCOM_ERROR_CODE(HCCL_E_PARA), rankId.c_str());
623 0 : return HCCL_E_PARA;
624 : }
625 :
626 1135 : rankinfo.podName = ""; // podname在实验室场景下置空
627 1136 : rankId = "";
628 1137 : rankTable.rankList.push_back(rankinfo);
629 1137 : HCCL_DEBUG("%s.json->rankId[%u], serverId[%s], devicePhyId[%d]", fileName_.c_str(),
630 : rankinfo.rankId, rankinfo.serverId.c_str(), rankinfo.deviceInfo.devicePhyId);
631 1137 : }
632 1136 : return HCCL_SUCCESS;
633 1136 : }
634 :
635 387 : HcclResult TopoinfoRanktableStandard::GetDeployMode(bool &cloudFlag) const
636 : {
637 387 : cloudFlag = fileContent_.find("deploy_mode") == fileContent_.end() ;
638 387 : return HCCL_SUCCESS;
639 : }
|