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 = (unordered_map<string, NetType>{
23 : {"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 102 : void RankLevelInfo::Deserialize(const nlohmann::json& rankLevelInfoJson)
31 : {
32 204 : std::string msgNetlayer = "error occurs when parser object of propName \"net_layer\"";
33 102 : std::string msgNetinstid = "error occurs when parser object of propName \"net_instance_id\"";
34 102 : TRY_CATCH_THROW(InvalidParamsException, msgNetlayer,
35 : netLayer = GetJsonPropertyUInt(rankLevelInfoJson, "net_layer"););
36 102 : TRY_CATCH_THROW(InvalidParamsException, msgNetinstid,
37 : netInstId = GetJsonProperty(rankLevelInfoJson, "net_instance_id"););
38 :
39 102 : if (netLayer > MAX_VALUE_NETLAYER) {
40 1 : THROW<InvalidParamsException>(
41 3 : StringFormat("netLayer[%u] out of range [%u] to [%u]", netLayer, MIN_VALUE_U32, MAX_VALUE_NETLAYER));
42 : }
43 101 : if (netInstId.length() < MIN_VALUE_NETID || netInstId.length() > MAX_VALUE_NETID) {
44 2 : THROW<InvalidParamsException>(StringFormat(
45 : "netInstId length[%zu] out of range [%u] to [%u]", netInstId.length(), MIN_VALUE_NETID, MAX_VALUE_NETID));
46 : }
47 :
48 100 : netAttr = rankLevelInfoJson.value<std::string>("net_attr", "");
49 :
50 100 : if (rankLevelInfoJson.contains("net_type")) {
51 100 : string netTypeStr;
52 100 : std::string msgNettype = "error occurs when parser object of propName \"net_type\"";
53 100 : TRY_CATCH_THROW(InvalidParamsException, msgNettype,
54 : netTypeStr = GetJsonProperty(rankLevelInfoJson, "net_type"););
55 100 : if (!IsStringInNetType(netTypeStr)) {
56 2 : THROW<InvalidParamsException>(StringFormat("[RankLevelInfo::%s] failed with Invalid netType. ", __func__));
57 : }
58 99 : netType = strToNetType.at(netTypeStr);
59 101 : }
60 99 : nlohmann::json rank_addrs;
61 99 : std::string msgAddrs = "error occurs when parser object of propName \"rank_addrs\"";
62 99 : TRY_CATCH_THROW(InvalidParamsException, msgAddrs,
63 : GetJsonPropertyList(rankLevelInfoJson, "rank_addr_list", rank_addrs););
64 393 : for (auto& addr : rank_addrs) {
65 294 : AddressInfo addressInfo;
66 294 : addressInfo.Deserialize(addr);
67 294 : rankAddrs.emplace_back(addressInfo);
68 294 : }
69 99 : if (rankAddrs.size() > MAX_VALUE_RANKADDR_SIZE) {
70 2 : THROW<InvalidParamsException>(StringFormat(
71 : "rank_addr_list [%u] out of range [%u] to [%u]", rankAddrs.size(), MIN_VALUE_RANKADDR_SIZE,
72 : MAX_VALUE_RANKADDR_SIZE));
73 : }
74 367 : for (auto& rankAddr : rankAddrs) {
75 269 : IpAddress ipAddress = rankAddr.addr;
76 608 : for (auto& port : rankAddr.ports) {
77 339 : if (portAddrMap.find(port) != portAddrMap.end()) {
78 2 : portAddrMap[port].push_back(ipAddress);
79 : } else {
80 337 : std::vector<IpAddress> ipAddrList;
81 337 : ipAddrList.push_back(ipAddress);
82 337 : portAddrMap[port] = ipAddrList;
83 337 : }
84 : }
85 : }
86 108 : }
87 :
88 8 : string RankLevelInfo::Describe() const
89 : {
90 : return StringFormat(
91 8 : "RankLevelInfo[net_layer=%u, net_instance_id=%s, netType=%s, rankAddrs size=%d]", netLayer, netInstId.c_str(),
92 16 : netType.Describe().c_str(), rankAddrs.size());
93 : }
94 :
95 9 : RankLevelInfo::RankLevelInfo(BinaryStream& binStream)
96 : {
97 9 : binStream >> netLayer >> netInstId >> netAttr;
98 9 : u32 netTypeInt{0};
99 9 : binStream >> netTypeInt;
100 9 : netType = static_cast<NetType::Value>(netTypeInt);
101 9 : size_t addrSize{0};
102 9 : binStream >> addrSize;
103 13 : HCCL_INFO(
104 : "[%s] net_layer[%u] net_instance_id[%s] netType[%s] addrs size[%u]", __func__, netLayer, netInstId.c_str(),
105 : netType.Describe().c_str(), rankAddrs.size());
106 19 : for (u32 i = 0; i < addrSize; i++) {
107 10 : AddressInfo addressInfo(binStream);
108 10 : rankAddrs.emplace_back(addressInfo);
109 10 : }
110 :
111 19 : for (auto& rankAddr : rankAddrs) {
112 10 : IpAddress ipAddress = rankAddr.addr;
113 32 : for (auto& port : rankAddr.ports) {
114 22 : if (portAddrMap.find(port) != portAddrMap.end()) {
115 0 : portAddrMap[port].push_back(ipAddress);
116 : } else {
117 22 : std::vector<IpAddress> ipAddrList;
118 22 : ipAddrList.push_back(ipAddress);
119 22 : portAddrMap[port] = ipAddrList;
120 22 : }
121 : }
122 : }
123 9 : }
124 :
125 72 : void RankLevelInfo::GetBinStream(BinaryStream& binStream) const
126 : {
127 72 : binStream << netLayer << netInstId << netAttr << static_cast<u32>(netType);
128 72 : binStream << rankAddrs.size();
129 196 : HCCL_INFO(
130 : "[%s] net_layer[%u] net_instance_id[%s] netType[%s] addrs size[%u]", __func__, netLayer, netInstId.c_str(),
131 : netType.Describe().c_str(), rankAddrs.size());
132 72 : if (rankAddrs.size() == 0) {
133 5 : return;
134 : }
135 179 : for (auto& rankAddr : rankAddrs) {
136 112 : rankAddr.GetBinStream(binStream);
137 : }
138 : }
139 : } // namespace Hccl
|