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 "rank_level_info.h"
12 : #include <sstream>
13 : #include <string>
14 : #include <unordered_map>
15 : #include "json_parser.h"
16 : #include "invalid_params_exception.h"
17 : #include "exception_util.h"
18 :
19 : namespace Hccl {
20 : using namespace std;
21 :
22 : const unordered_map<string, NetType> RankLevelInfo::strToNetType
23 : = (unordered_map<string, NetType>{{"1DMESH", NetType::MESH_1D},
24 : {"2DMESH", NetType::MESH_2D},
25 : {"A3_SERVER", NetType::A3_SERVER},
26 : {"A2_AX_SERVER", NetType::A2_AX_SERVER},
27 : {"TOPO_FILE_DESC", NetType::TOPO_FILE_DESC},
28 : {"CLOS", NetType::CLOS}});
29 :
30 :
31 174 : void RankLevelInfo::Deserialize(const nlohmann::json &rankLevelInfoJson)
32 : {
33 348 : std::string msgNetlayer = "error occurs when parser object of propName \"net_layer\"";
34 174 : std::string msgNetinstid = "error occurs when parser object of propName \"net_instance_id\"";
35 174 : TRY_CATCH_THROW(InvalidParamsException, msgNetlayer, netLayer = GetJsonPropertyUInt(rankLevelInfoJson, "net_layer"););
36 174 : TRY_CATCH_THROW(InvalidParamsException, msgNetinstid, netInstId = GetJsonProperty(rankLevelInfoJson, "net_instance_id"););
37 :
38 174 : if (netLayer > MAX_VALUE_NETLAYER) {
39 2 : THROW<InvalidParamsException>(StringFormat( "netLayer[%u] out of range [%u] to [%u]", netLayer, MIN_VALUE_U32, MAX_VALUE_NETLAYER));
40 : }
41 173 : if (netInstId.length()< MIN_VALUE_NETID || netInstId.length()> MAX_VALUE_NETID) {
42 2 : THROW<InvalidParamsException>(StringFormat( "netInstId length[%zu] out of range [%u] to [%u]", netInstId.length(), MIN_VALUE_NETID, MAX_VALUE_NETID));
43 : }
44 :
45 172 : netAttr=rankLevelInfoJson.value<std::string>("net_attr", "");
46 :
47 172 : if (rankLevelInfoJson.contains("net_type")){
48 172 : string netTypeStr;
49 172 : std::string msgNettype = "error occurs when parser object of propName \"net_type\"";
50 172 : TRY_CATCH_THROW(InvalidParamsException, msgNettype,netTypeStr = GetJsonProperty(rankLevelInfoJson, "net_type"););
51 172 : if (!IsStringInNetType(netTypeStr)) {
52 2 : THROW<InvalidParamsException>(StringFormat("[RankLevelInfo::%s] failed with Invalid netType. ", __func__));
53 : }
54 171 : netType = strToNetType.at(netTypeStr);
55 173 : }
56 171 : nlohmann::json rank_addrs;
57 171 : std::string msgAddrs = "error occurs when parser object of propName \"rank_addrs\"";
58 171 : TRY_CATCH_THROW(InvalidParamsException, msgAddrs, GetJsonPropertyList(rankLevelInfoJson, "rank_addr_list", rank_addrs););
59 714 : for (auto &addr : rank_addrs) {
60 543 : AddressInfo addressInfo;
61 543 : addressInfo.Deserialize(addr);
62 543 : rankAddrs.emplace_back(addressInfo);
63 543 : }
64 171 : if (rankAddrs.size()> MAX_VALUE_RANKADDR_SIZE) {
65 2 : THROW<InvalidParamsException>(StringFormat( "rank_addr_list [%u] out of range [%u] to [%u]", rankAddrs.size(), MIN_VALUE_RANKADDR_SIZE, MAX_VALUE_RANKADDR_SIZE));
66 : }
67 688 : for (auto& rankAddr : rankAddrs) {
68 518 : IpAddress ipAddress = rankAddr.addr;
69 1118 : for (auto& port : rankAddr.ports) {
70 600 : if (portAddrMap.find(port) != portAddrMap.end()) {
71 2 : portAddrMap[port].push_back(ipAddress);
72 : } else {
73 598 : std::vector<IpAddress> ipAddrList;
74 598 : ipAddrList.push_back(ipAddress);
75 598 : portAddrMap[port] = ipAddrList;
76 598 : }
77 : }
78 : }
79 180 : }
80 :
81 40 : string RankLevelInfo::Describe() const
82 : {
83 40 : return StringFormat("RankLevelInfo[net_layer=%u, net_instance_id=%s, netType=%s, rankAddrs size=%d]", netLayer, netInstId.c_str(),
84 80 : netType.Describe().c_str(), rankAddrs.size());
85 : }
86 :
87 9 : RankLevelInfo::RankLevelInfo(BinaryStream &binStream)
88 : {
89 9 : binStream >> netLayer >> netInstId>>netAttr;
90 9 : u32 netTypeInt{0};
91 9 : binStream >> netTypeInt;
92 9 : netType = static_cast<NetType::Value>(netTypeInt);
93 9 : size_t addrSize{0};
94 9 : binStream >> addrSize;
95 27 : HCCL_INFO("[%s] net_layer[%u] net_instance_id[%s] netType[%s] addrs size[%u]", __func__, netLayer, netInstId.c_str(),
96 : netType.Describe().c_str(), rankAddrs.size());
97 19 : for (u32 i = 0; i < addrSize; i++) {
98 10 : AddressInfo addressInfo(binStream);
99 10 : rankAddrs.emplace_back(addressInfo);
100 10 : }
101 :
102 19 : for (auto& rankAddr : rankAddrs) {
103 10 : IpAddress ipAddress = rankAddr.addr;
104 32 : for (auto& port : rankAddr.ports) {
105 22 : if (portAddrMap.find(port) != portAddrMap.end()) {
106 0 : portAddrMap[port].push_back(ipAddress);
107 : } else {
108 22 : std::vector<IpAddress> ipAddrList;
109 22 : ipAddrList.push_back(ipAddress);
110 22 : portAddrMap[port] = ipAddrList;
111 22 : }
112 : }
113 : }
114 9 : }
115 :
116 72 : void RankLevelInfo::GetBinStream(BinaryStream &binStream) const
117 : {
118 72 : binStream << netLayer << netInstId <<netAttr<< static_cast<u32>(netType);
119 72 : binStream << rankAddrs.size();
120 216 : HCCL_INFO("[%s] net_layer[%u] net_instance_id[%s] netType[%s] addrs size[%u]", __func__, netLayer, netInstId.c_str(),
121 : netType.Describe().c_str(), rankAddrs.size());
122 72 : if (rankAddrs.size() == 0) {
123 5 : return;
124 : }
125 179 : for (auto &rankAddr : rankAddrs) {
126 112 : rankAddr.GetBinStream(binStream);
127 : }
128 : }
129 : } // namespace Hccl
|