LCOV - code coverage report
Current view: top level - coll_communicator_mgr/rank_graph/rank_graph - net_instance.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 91.7 % 339 311
Test Date: 2026-07-28 12:11:00 Functions: 96.2 % 79 76

            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 "net_instance.h"
      12              : #include "exception_util.h"
      13              : #include "not_support_exception.h"
      14              : #include "invalid_params_exception.h"
      15              : 
      16              : namespace Hccl {
      17              : 
      18              : using namespace std;
      19              : 
      20          690 : NetInstance::NetInstance(const u32 netLayer, const string &netInstId, const NetType netType)
      21              : {
      22          230 :     this->netLayer = netLayer;
      23          230 :     this->netInstId = netInstId;
      24          230 :     this->netType = netType;
      25          230 : }
      26              : 
      27         1119 : u32 NetInstance::GetNetLayer() const
      28              : {
      29         1119 :     return netLayer;
      30              : }
      31              : 
      32          516 : string NetInstance::GetNetInstId() const
      33              : {
      34          516 :     return netInstId;
      35              : }
      36              : 
      37          124 : NetType NetInstance::GetNetType() const
      38              : {
      39          124 :     return netType;
      40              : }
      41              : 
      42          491 : set<RankId> NetInstance::GetRankIds() const
      43              : {
      44          491 :     return rankIds;
      45              : }
      46              : 
      47           24 : u32 NetInstance::GetRankSize() const
      48              : {
      49           24 :     return rankIds.size();
      50              : }
      51              : 
      52           96 : bool NetInstance::HasNode(const NodeId nodeId) const
      53              : {
      54           96 :     return vGraph.HasNode(nodeId);
      55              : }
      56              : 
      57           37 : const std::unordered_map<RankId, std::shared_ptr<NetInstance::Peer>>& NetInstance::GetPeers() const
      58              : {
      59           37 :     return peers;
      60              : }
      61              : 
      62           51 : const std::vector<std::shared_ptr<NetInstance::Fabric>>& NetInstance::GetFabrics() const
      63              : {
      64           51 :     return fabrics;
      65              : }
      66              : 
      67          206 : Graph<NetInstance::Node, NetInstance::Link>& NetInstance::GetGraph()
      68              : {
      69          206 :     return vGraph;
      70              : }
      71              : 
      72          468 : void NetInstance::AddRankId(const RankId rankId)
      73              : {
      74          468 :     rankIds.insert(rankId);
      75          680 :     HCCL_DEBUG("[NetInstance::AddRankId] add rank id [%d] to %s", rankId, this->Describe().c_str());
      76          468 : }
      77              : 
      78          413 : void NetInstance::AddNode(const shared_ptr<Node> &node)
      79              : {
      80          413 :     NetInstance::Node::NodeType nodeType = node->GetType();
      81          413 :     if (nodeType == NetInstance::Node::NodeType::PEER) {
      82          359 :         AddPeer(dynamic_pointer_cast<NetInstance::Peer>(node));
      83           54 :     } else if (nodeType == NetInstance::Node::NodeType::FABRIC) {
      84           54 :         AddFabric(dynamic_pointer_cast<NetInstance::Fabric>(node));
      85              :     } else {
      86            0 :         THROW<NotSupportException>(StringFormat("[NetInstance::AddNode] failed to add %s to %s, "
      87              :                                                 "only PEER or FABRIC type node can be added.",
      88            0 :                                                 node->Describe().c_str(), this->Describe().c_str()));
      89              :     }
      90          413 : }
      91              : 
      92          359 : void NetInstance::AddPeer(const shared_ptr<Peer> &peer)
      93              : {
      94          359 :     if (netLayer == 0 && localIdsMap.find(peer->GetLocalId()) != localIdsMap.end()) {
      95            0 :         THROW<InvalidParamsException>(StringFormat("[NetInstance][%s] when netLayer is 0, local id[%u] is repeat. "
      96              :             "rank id [%u], netInstId[%s]", __func__, peer->GetLocalId(), peer->GetRankId(), netInstId.c_str()));
      97              :     }
      98          359 :     localIdsMap.insert({peer->GetLocalId(), peer->GetRankId()});
      99              : 
     100          359 :     peers[peer->GetRankId()] = peer;
     101          359 :     vGraph.AddNode(peer->GetNodeId(), peer);
     102              : 
     103          537 :     HCCL_DEBUG("[NetInstance::AddPeer] add %s to %s", peer->Describe().c_str(), this->Describe().c_str());
     104          359 : }
     105              : 
     106           54 : void NetInstance::AddFabric(const shared_ptr<NetInstance::Fabric> &fabric)
     107              : {
     108           54 :     if (netType != NetType::CLOS && netType!= NetType::TOPO_FILE_DESC) {
     109            0 :         THROW<NotSupportException>(StringFormat("[NetInstance::AddFabric] failed to add %s to %s, "
     110              :                                                 "only CLOS type NetInstance can add Fabrics.",
     111            0 :                                                 fabric->Describe().c_str(), this->Describe().c_str()));
     112              :     }
     113              : 
     114           54 :     NodeId fabricId = fabric->GetNodeId();
     115           54 :     fabrics.emplace_back(fabric);
     116           54 :     vGraph.AddNode(fabricId, fabric);
     117              : 
     118          138 :     HCCL_DEBUG("[NetInstance::AddFabric] add %s to %s", fabric->Describe().c_str(), this->Describe().c_str());
     119           54 : }
     120              : 
     121          550 : void NetInstance::AddLink(const shared_ptr<NetInstance::Link>& link)
     122              : {
     123          550 :     NodeId srcNodeId = link->GetSourceNode()->GetNodeId();
     124          550 :     NodeId dstNodeId = link->GetTargetNode()->GetNodeId();
     125              : 
     126          550 :     bool hasLink = false;
     127          550 :     vGraph.TraverseEdge(srcNodeId, dstNodeId, [&](shared_ptr<NetInstance::Link> edge) {
     128           65 :         if (*edge == *link) {
     129           17 :             hasLink = true;
     130           17 :             return;
     131              :         }
     132              :     });
     133              : 
     134          550 :     if (hasLink) {
     135           17 :         HCCL_WARNING("[NetInstance::AddLink] failed to add %s to %s, "
     136              :                      "the fabric group already has the same link.",
     137              :                      link->Describe().c_str(), this->Describe().c_str());
     138           17 :         return;
     139              :     }
     140              : 
     141          533 :     vGraph.AddEdge(srcNodeId, dstNodeId, link);
     142              : 
     143         1117 :     HCCL_DEBUG("[NetInstance::AddLink] add %s to %s", link->Describe().c_str(), this->Describe().c_str());
     144              : }
     145              : 
     146            8 : void NetInstance::DeleteLink(const NodeId srcNodeId, const NodeId dstNodeId)
     147              : {
     148           24 :     HCCL_RUN_INFO("[NetInstance::DeleteLink] delete %lu -> %lu", srcNodeId, dstNodeId);
     149            8 :     vGraph.DeleteEdge(srcNodeId, dstNodeId);
     150            8 :     vGraph.DeleteEdge(dstNodeId, srcNodeId);
     151            8 : }
     152              : 
     153          204 : void NetInstance::UpdateTopoInst(u32 topoInstId, TopoType topoType, RankId rankId)
     154              : {
     155          204 :     auto it = topoInsts_.find(topoInstId);
     156          204 :     if (it != topoInsts_.end()) {
     157          146 :         TopoInstance& existingInst = *it->second;
     158          146 :         existingInst.ranks.insert(rankId);
     159              :     } else {
     160              :         // 创建新的TopoInstance
     161           58 :         TopoInstance newInst;
     162           58 :         newInst.topoInstId = topoInstId;
     163           58 :         newInst.topoType = topoType;
     164           58 :         newInst.ranks.insert(rankId);
     165           58 :         topoInsts_.emplace(topoInstId, std::make_shared<TopoInstance>(std::move(newInst)));
     166           58 :     }
     167          204 : }
     168              : 
     169            2 : void NetInstance::GetTopoInstsByLayer(std::vector<u32> &topoInsts, u32 &topoInstNum) const
     170              : {
     171            4 :     for (const auto &entry : topoInsts_) {
     172            2 :         topoInsts.push_back(entry.first);
     173              :     }
     174              : 
     175            2 :     topoInstNum = static_cast<u32>(topoInsts.size());
     176            2 : }
     177              : 
     178            3 : HcclResult NetInstance::GetTopoType(const u32 topoInstId, TopoType& topoType) const
     179              : {
     180            3 :     auto it = topoInsts_.find(topoInstId);
     181            3 :     if (it != topoInsts_.end()) {
     182            2 :         const std::shared_ptr<TopoInstance>& topoInstPtr = it->second;
     183            2 :         topoType = topoInstPtr->topoType;
     184            2 :         return HCCL_SUCCESS;
     185              :     }
     186              : 
     187            1 :     HCCL_ERROR("[NetInstance::GetTopoType] Failed to find TopoInstance with ID: %u", topoInstId);
     188            1 :     return HCCL_E_PARA;
     189              : }
     190              : 
     191            3 : HcclResult NetInstance::GetRanksByTopoInst(const u32 topoInstId, std::vector<u32>& ranks, u32& rankNum) const
     192              : {
     193            3 :     auto it = topoInsts_.find(topoInstId);
     194            3 :     if (it != topoInsts_.end()) {
     195            2 :         const std::shared_ptr<TopoInstance>& topoInstPtr = it->second;
     196            2 :         ranks.assign(topoInstPtr->ranks.begin(), topoInstPtr->ranks.end());
     197            2 :         rankNum = static_cast<u32>(ranks.size());
     198            2 :         return HCCL_SUCCESS;
     199              :     }
     200            1 :     HCCL_ERROR("[NetInstance::GetRanksByTopoInst] Failed to find ranks with ID: %u", topoInstId);
     201            1 :     return HCCL_E_PARA;
     202              : }
     203              : 
     204         1469 : string NetInstance::Describe() const
     205              : {
     206         1469 :     return StringFormat("NetInstance[ID=%s, Level=%u, FabType=%s, RankIds_Size=%zu]", netInstId.c_str(), netLayer,
     207         2938 :                         netType.Describe().c_str(), rankIds.size());
     208              : }
     209              : 
     210          114 : bool CheckPortGroupSize(u32 netLayer, NetInstance::Link& srcLink, NetInstance::Link& dstLink)
     211              : {
     212          114 :     auto srcConnIface = srcLink.GetSourceIface();
     213          114 :     auto targetConnIface = dstLink.GetTargetIface();
     214          114 :     auto srcPortGroupSize = static_cast<u8>(srcConnIface->GetPorts().size());
     215          114 :     auto tgtPortGroupSize = static_cast<u8>(targetConnIface->GetPorts().size());
     216          114 :     if (srcPortGroupSize != tgtPortGroupSize) {
     217            0 :         auto srcPeer = srcLink.GetSourceNode();
     218            0 :         auto targetPeer = dstLink.GetTargetNode();
     219            0 :         auto localAddr = srcConnIface->GetAddr();
     220            0 :         auto remoteAddr = targetConnIface->GetAddr();
     221            0 :         auto localRankId = std::dynamic_pointer_cast<NetInstance::Peer>(srcPeer)->GetRankId();
     222            0 :         auto remoteRankId = std::dynamic_pointer_cast<NetInstance::Peer>(targetPeer)->GetRankId();
     223            0 :         HCCL_WARNING("[GetPaths][CheckPortGroupSize] portGroupSize is not equal => src[%u], target[%u]."
     224              :                 "LocatedInfo: NetLayer[%u], localRank[%u], rmtRank[%u], localAddr[%s], rmtAddr[%s]", srcPortGroupSize, tgtPortGroupSize,
     225              :                 netLayer, localRankId, remoteRankId, localAddr.Describe().c_str(), remoteAddr.Describe().c_str());
     226            0 :         return false;
     227            0 :     }
     228          114 :     return true;
     229          114 : }
     230              : 
     231          114 : vector<NetInstance::Path> InnerNetInstance::GetPaths(const RankId srcRankId, const RankId dstRankId) const
     232              : {
     233          114 :     vector<NetInstance::Path> paths;
     234          114 :     if (peers.count(srcRankId) == 0 || peers.count(dstRankId) == 0) {
     235            0 :         HCCL_WARNING("[InnerNetInstance::GetPaths] srcRankId[%d] or dstRankId[%d] not exist in netInstance, "
     236              :                      "netLayer[%u], netInstId[%s]", srcRankId, dstRankId, netLayer, netInstId.c_str());
     237            0 :         return paths;
     238              :     }
     239          114 :     NodeId srcPeerId = peers.at(srcRankId)->GetNodeId();
     240          114 :     NodeId dstPeerId = peers.at(dstRankId)->GetNodeId();
     241              :     // 1. 获取边
     242          114 :     vGraph.TraverseEdge(srcPeerId, dstPeerId, [&](shared_ptr<NetInstance::Link> edge) {
     243           98 :         NetInstance::Path path;
     244          196 :         path.links = {*edge};
     245           98 :         path.direction = edge->GetLinkDirection();
     246           98 :         paths.emplace_back(path);
     247          192 :         HCCL_DEBUG("[InnerNetInstance::GetPaths] netLayer[%u], from src[%s] to dst[%s] get path.",
     248              :                    netLayer, peers.at(srcRankId)->Describe().c_str(), peers.at(dstRankId)->Describe().c_str());
     249          192 :         HCCL_DEBUG("[InnerNetInstance::GetPaths] netLayer[%u], srcRankId[%u], dstRankId[%u], path[%s]", netLayer,
     250              :                    srcRankId, dstRankId, path.links[0].Describe().c_str());
     251          196 :     });
     252              : 
     253              :     // 2. 通过 fabric 的路径
     254          146 :     for (auto& fabric : fabrics) {
     255           32 :         NodeId fabricId = fabric->GetNodeId();
     256              : 
     257              :         // 所有 src -> fabric 的链路
     258           32 :         vector<NetInstance::Link> srcToFabricLinks;
     259           32 :         vGraph.TraverseEdge(srcPeerId, fabricId, [&](shared_ptr<NetInstance::Link> edge) {
     260           28 :             srcToFabricLinks.push_back(*edge);
     261           28 :             return;
     262              :         });
     263              : 
     264              :         // 所有 fabric -> dst 的链路
     265           32 :         vector<NetInstance::Link> fabricToDstLinks;
     266           32 :         vGraph.TraverseEdge(fabricId, dstPeerId, [&](shared_ptr<NetInstance::Link> edge) {
     267           28 :             fabricToDstLinks.push_back(*edge);
     268           28 :             return;
     269              :         });
     270              : 
     271           32 :         if (!srcToFabricLinks.empty() && !fabricToDstLinks.empty()) {
     272           48 :             for (auto& srcLink : srcToFabricLinks) {
     273           48 :                 for (auto& dstLink : fabricToDstLinks) {
     274           24 :                     if (!CheckPortGroupSize(netLayer, srcLink, dstLink)) {
     275            0 :                         continue;
     276              :                     }
     277           24 :                     NetInstance::Path path;
     278           72 :                     path.links = {srcLink, dstLink};
     279           24 :                     paths.emplace_back(path);
     280           24 :                 }
     281              :             }
     282              :         } else {
     283           24 :             HCCL_WARNING("[NetInstance::GetPaths] netLayer[%u], srcRankId[%d], dstRankId[%d], netInstId[%s], "
     284              :                          "from src[%s] to dst[%s] link via fabric[%s] not found.", netLayer, srcRankId, dstRankId,
     285              :                          netInstId.c_str(), peers.at(srcRankId)->Describe().c_str(),
     286              :                          peers.at(dstRankId)->Describe().c_str(), fabric->Describe().c_str());
     287              :         }
     288           32 :     }
     289              : 
     290          114 :     return paths;
     291           24 : }
     292              : 
     293          190 : const std::unordered_map<u32, std::vector<std::shared_ptr<NetInstance::ConnInterface>>> NetInstance::Node::GetInterfacesMap() const
     294              : {
     295          190 :     return interfacesMap_;
     296              : }
     297              : 
     298           46 : vector<NetInstance::Path> ClosNetInstance::GetPaths(const RankId srcRankId, const RankId dstRankId) const
     299              : {
     300           46 :     vector<NetInstance::Path> paths;
     301           46 :     if (peers.count(srcRankId) == 0 || peers.count(dstRankId) == 0) {
     302            0 :         HCCL_WARNING("[ClosNetInstance::GetPaths] srcRankId[%u] or dstRankId[%u] not exist in netInstance, "
     303              :                      "netLayer[%u], netInstId[%s].", srcRankId, dstRankId, netLayer, netInstId.c_str());
     304            0 :         return paths;
     305              :     }
     306           46 :     NodeId srcPeerId = peers.at(srcRankId)->GetNodeId();
     307           46 :     NodeId dstPeerId = peers.at(dstRankId)->GetNodeId();
     308          220 :     for (auto &fabric : fabrics) {
     309          174 :         NodeId fabricId = fabric->GetNodeId();
     310              : 
     311          174 :         NetInstance::Link srcToFabricLink;
     312          174 :         vGraph.TraverseEdge(srcPeerId, fabricId, [&](shared_ptr<NetInstance::Link> edge) {
     313           90 :             srcToFabricLink = *edge;
     314           90 :             return;
     315              :         });
     316              : 
     317          174 :         NetInstance::Link fabricToDstLink;
     318          174 :         vGraph.TraverseEdge(fabricId, dstPeerId, [&](shared_ptr<NetInstance::Link> edge) {
     319           90 :             fabricToDstLink = *edge;
     320           90 :             return;
     321              :         });
     322              : 
     323          174 :         if (!srcToFabricLink.IsEmpty() && !fabricToDstLink.IsEmpty()) {
     324           90 :             if (!CheckPortGroupSize(netLayer, srcToFabricLink, fabricToDstLink)) {
     325            0 :                 continue;
     326              :             }
     327           90 :             NetInstance::Path path;
     328          270 :             path.links = {srcToFabricLink, fabricToDstLink};
     329           90 :             paths.emplace_back(path);
     330           90 :         } else {
     331           84 :             HCCL_WARNING("[ClosNetInstance::GetPaths] from src[%s] to dst[%s] link by fabric[%s] not found.",
     332              :                        peers.at(srcRankId)->Describe().c_str(), peers.at(dstRankId)->Describe().c_str(),
     333              :                        fabric->Describe().c_str());
     334              :         }
     335          174 :     }
     336              : 
     337           46 :     return paths;
     338           90 : }
     339              : 
     340          827 : void NetInstance::Node::AddConnInterface(u32 layer, const shared_ptr<NetInstance::ConnInterface>& interface)
     341              : {
     342          827 :     auto& interfacesVec = interfacesMap_[layer];
     343         1248 :     for (const auto& iface : interfacesVec) {
     344          552 :         if (*iface == *interface) {
     345          311 :             HCCL_WARNING("[NetInstance][Node][AddConnInterface] interface addr[%s] has existed.",
     346              :                          interface->GetAddr().Describe().c_str());
     347          131 :             return;
     348              :         }
     349              :     }
     350              : 
     351          696 :     interfacesVec.emplace_back(interface);
     352              : }
     353              : 
     354          188 : void NetInstance::Node::AddConnInterfaces(u32 layer,
     355              :                                           const std::vector<std::shared_ptr<NetInstance::ConnInterface>>& interfaces)
     356              : {
     357          188 :     if (interfaces.empty()) {
     358            0 :         return;
     359              :     }
     360          376 :     for (auto interface : interfaces) {
     361          188 :         AddConnInterface(layer, interface);
     362          188 :     }
     363              : }
     364              : 
     365          471 : NetInstance::Node::NodeType NetInstance::Node::GetType() const
     366              : {
     367          471 :     return type_;
     368              : }
     369              : 
     370           43 : std::vector<std::shared_ptr<NetInstance::ConnInterface>> NetInstance::Node::GetIfacesByLayer(u32 layer) const
     371              : {
     372           43 :     auto it = interfacesMap_.find(layer);
     373           43 :     if (it == interfacesMap_.end()) {
     374            2 :         HCCL_WARNING("[NetInstance][Node][GetIfacesByLayer] netLayer[%u] not exist.", layer);
     375            2 :         return std::vector<std::shared_ptr<NetInstance::ConnInterface>>{};
     376              :     }
     377           41 :     return it->second;
     378              : }
     379              : 
     380           78 : std::vector<std::shared_ptr<NetInstance::ConnInterface>> NetInstance::Node::GetIfaces() const
     381              : {
     382           78 :     std::vector<std::shared_ptr<NetInstance::ConnInterface>> ifaces;
     383          108 :     for (auto layerIfacesPair : interfacesMap_) {
     384           80 :         for (auto iface : layerIfacesPair.second) {
     385           50 :             ifaces.emplace_back(iface);
     386           50 :         }
     387           30 :     }
     388           78 :     return ifaces;
     389            0 : }
     390              : 
     391              : 
     392           59 : void NetInstance::Node::SetEndpointToIface(const CommAddr& commAddr, CommProtocol protocol,
     393              :                                            const std::shared_ptr<NetInstance::ConnInterface>& iface)
     394              : {
     395           59 :     endpointToIfaceMap_[std::make_pair(commAddr, protocol)] = iface;
     396           59 : }
     397              : 
     398           16 : const std::unordered_map<std::pair<CommAddr, CommProtocol>, std::shared_ptr<NetInstance::ConnInterface>>  NetInstance::Node::GetEndpointToIfaceMap() const
     399              : {
     400           16 :     return endpointToIfaceMap_;
     401              : }
     402              : 
     403         4740 : NodeId NetInstance::Node::GetNodeId() const
     404              : {
     405         4740 :     return nodeId_;
     406              : }
     407              : 
     408         1231 : LocalId NetInstance::Peer::GetLocalId() const
     409              : {
     410         1231 :     return localId_;
     411              : }
     412              : 
     413           16 : LocalId NetInstance::Peer::GetReplacedLocalId() const
     414              : {
     415           16 :     return replacedLocalId_;
     416              : }
     417              : 
     418          132 : DeviceId NetInstance::Peer::GetDeviceId() const
     419              : {
     420          132 :     return deviceId_;
     421              : }
     422              : 
     423            8 : u32 NetInstance::Peer::GetDevicePort() const
     424              : {
     425            8 :     return devicePort_;
     426              : }
     427              : 
     428            6 : u32 NetInstance::Peer::GetHostPort() const
     429              : {
     430            6 :     return hostPort_;
     431              : }
     432              : 
     433         1975 : RankId NetInstance::Peer::GetRankId() const
     434              : {
     435         1975 :     return rankId_;
     436              : }
     437              : 
     438          108 : set<u32> NetInstance::Peer::GetLevels() const
     439              : {
     440          108 :     return netLayers_;
     441              : }
     442              : 
     443          385 : const NetInstance *NetInstance::Peer::GetNetInstance(u32 netLayer) const
     444              : {
     445          385 :     if (netLayer >= netInsts_.size() || netInsts_.at(netLayer) == nullptr) {
     446           68 :         HCCL_WARNING("[NetInstance][Peer][GetNetInstance] netLayer[%u] not exist.", netLayer);
     447           24 :         return nullptr;
     448              :     }
     449          361 :     return netInsts_[netLayer];
     450              : }
     451              : 
     452          714 : NodeId NetInstance::Peer::GenerateNodeId(RankId rankId)
     453              : {
     454          714 :     return (static_cast<u64>(rankId) | static_cast<u64>(0) << 32); // 第32位为0 + rankId
     455              : }
     456              : 
     457         1017 : string NetInstance::Peer::Describe() const
     458              : {
     459         1017 :     return StringFormat("NetInstance::Peer[rankId=%d, localId=%u, NodeId=%llu, netLayers_size=%zu]", rankId_, localId_, nodeId_,
     460         1017 :                         netLayers_.size());
     461              : }
     462              : 
     463          449 : void NetInstance::Peer::AddNetInstance(const std::shared_ptr<NetInstance> &netInst)
     464              : {
     465          449 :     u32 netLayer = netInst->GetNetLayer();
     466          449 :     if (netLayer >= netInsts_.size()) {
     467          448 :         netInsts_.resize(netLayer + 1);
     468              :     }
     469              : 
     470          449 :     if (netInsts_[netLayer] != nullptr) {
     471            1 :         THROW<InvalidParamsException>(
     472            3 :             StringFormat("[NetInstance][Peer][AddNetInstance]rankId[%d] netLayer[%u] NetInstance has existed", rankId_, netLayer));
     473              :     }
     474          448 :     netInsts_[netLayer] = netInst.get();
     475          448 :     netLayers_.insert(netInst->GetNetLayer());
     476          448 : }
     477              : 
     478           50 : void NetInstance::Peer::SetPortPortAddrMapLayer0(std::map<std::string, std::vector<IpAddress>> portAddrMap)
     479              : {
     480           50 :     portAddrMapLayer0_ = std::move(portAddrMap);
     481           50 : }
     482              : 
     483          268 : std::map<std::string, std::vector<IpAddress>> NetInstance::Peer::GetPortAddrMapLayer0() const
     484              : {
     485          268 :     return portAddrMapLayer0_;
     486              : }
     487              : 
     488           13 : PlaneId NetInstance::Fabric::GetPlaneId() const
     489              : {
     490           13 :     return planeId_;
     491              : }
     492              : 
     493           55 : NodeId NetInstance::Fabric::GenerateNodeId(FabricId fabricId) const
     494              : {
     495           55 :     return (static_cast<u64>(fabricId) | static_cast<u64>(1) << 32); // 第32位为1 + netplaneId
     496              : }
     497              : 
     498          243 : string NetInstance::Fabric::Describe() const
     499              : {
     500          243 :     return StringFormat("NetInstance::Fabric[netplaneId=%s, FabricNodeId=%llu]", planeId_.c_str(), nodeId_);
     501              : }
     502              : 
     503           31 : LinkType NetInstance::Link::GetType() const
     504              : {
     505           31 :     return type_;
     506              : }
     507              : 
     508          131 : std::set<LinkProtocol> NetInstance::Link::GetLinkProtocols() const
     509              : {
     510          131 :     return linkProtocols_;
     511              : }
     512              : 
     513          127 : LinkDirection NetInstance::Link::GetLinkDirection() const
     514              : {
     515          127 :     return direction_;
     516              : }
     517              : 
     518          119 : u32 NetInstance::Link::GetHop() const
     519              : {
     520          119 :     return hop_;
     521              : }
     522              : 
     523          836 : shared_ptr<NetInstance::Node> NetInstance::Link::GetSourceNode() const
     524              : {
     525          836 :     return source_;
     526              : }
     527              : 
     528          836 : shared_ptr<NetInstance::Node> NetInstance::Link::GetTargetNode() const
     529              : {
     530          836 :     return target_;
     531              : }
     532              : 
     533          269 : shared_ptr<NetInstance::ConnInterface> NetInstance::Link::GetSourceIface() const
     534              : {
     535          269 :     return sourceIface_;
     536              : }
     537              : 
     538          269 : shared_ptr<NetInstance::ConnInterface> NetInstance::Link::GetTargetIface() const
     539              : {
     540          269 :     return targetIface_;
     541              : }
     542              : 
     543          973 : string NetInstance::Link::Describe() const
     544              : {
     545          973 :     stringstream iFace;
     546          973 :     if (sourceIface_ != nullptr) {
     547          739 :         iFace << ", srcIface=" << sourceIface_->Describe();
     548              :     }
     549          973 :     if (targetIface_ != nullptr) {
     550          739 :         iFace << ", dstIface=" << targetIface_->Describe();
     551              :     }
     552          973 :     std::stringstream linkProtocolsStr;
     553         1950 :     for (auto protocol : linkProtocols_) {
     554          977 :         if (!linkProtocolsStr.str().empty()) {
     555            4 :             linkProtocolsStr << ", ";
     556              :         }
     557          977 :         linkProtocolsStr << protocol;
     558              :     }
     559              :     return StringFormat("NetInstance::Link[srcId=%llu, dstId=%llu, type=%s, hop=%u, dir=%s, proto=%s%s]",
     560         1946 :                         source_->GetNodeId(), target_->GetNodeId(), type_.Describe().c_str(), hop_,
     561         3892 :                         direction_.Describe().c_str(), linkProtocolsStr.str().c_str(), iFace.str().c_str());
     562          973 : }
     563              : 
     564          264 : bool NetInstance::Link::IsEmpty() const
     565              : {
     566          264 :     return (source_ == nullptr) && (target_ == nullptr);
     567              : }
     568              : 
     569           65 : bool NetInstance::Link::operator==(const NetInstance::Link &rhs) const
     570              : {
     571          130 :     return source_->GetNodeId() == rhs.source_->GetNodeId() && target_->GetNodeId() == rhs.target_->GetNodeId()
     572           65 :            && sourceIface_ == rhs.sourceIface_ && targetIface_ == rhs.targetIface_ && type_ == rhs.type_
     573          130 :            && linkProtocols_ == rhs.linkProtocols_ && direction_ == rhs.direction_ && hop_ == rhs.hop_;
     574              : }
     575              : 
     576            0 : bool NetInstance::Link::operator!=(const NetInstance::Link &rhs) const
     577              : {
     578            0 :     return !(rhs == *this);
     579              : }
     580              : 
     581          545 : IpAddress NetInstance::ConnInterface::GetAddr() const
     582              : {
     583          545 :     return addr;
     584              : }
     585              : 
     586          230 : std::set<string> NetInstance::ConnInterface::GetPorts() const
     587              : {
     588          230 :     return ports;
     589              : }
     590              : 
     591          439 : AddrPosition NetInstance::ConnInterface::GetPos() const
     592              : {
     593          439 :     return pos;
     594              : }
     595              : 
     596            0 : LinkType NetInstance::ConnInterface::GetLinkType() const
     597              : {
     598            0 :     return linkType;
     599              : }
     600              : 
     601          774 : std::set<LinkProtocol> NetInstance::ConnInterface::GetLinkProtocols() const
     602              : {
     603          774 :     return linkProtocols;
     604              : }
     605              : 
     606           51 : void NetInstance::ConnInterface::SetLocalDieId(u32 dieId)
     607              : {
     608           51 :     localDieId_ = dieId;
     609           51 : }
     610              : 
     611           44 : u32 NetInstance::ConnInterface::GetLocalDieId() const
     612              : {
     613           44 :     return localDieId_;
     614              : }
     615              : 
     616           33 : TopoType NetInstance::ConnInterface::GetTopoType() const
     617              : {
     618           33 :     return topoType;
     619              : }
     620              : 
     621           56 : u32 NetInstance::ConnInterface::GetTopoInstId() const
     622              : {
     623           56 :     return topoInstId;
     624              : }
     625              : 
     626         1478 : std::string NetInstance::ConnInterface::Describe() const
     627              : {
     628         1478 :     return StringFormat("Iface[addr=%s, pos=%s, topoInstId=%u, topoType=%d, localDieId=%u]", addr.Describe().c_str(), pos.Describe().c_str(), topoInstId, topoType, localDieId_);
     629              : }
     630              : 
     631          552 : bool NetInstance::ConnInterface::operator==(const NetInstance::ConnInterface &rhs) const
     632              : {
     633          707 :     return addr == rhs.addr && pos == rhs.pos && linkType == rhs.linkType &&
     634          707 :         linkProtocols == rhs.linkProtocols && ports == rhs.ports && topoInstId == rhs.topoInstId && topoType == rhs.topoType;
     635              : }
     636              : 
     637            0 : bool NetInstance::ConnInterface::operator!=(const NetInstance::ConnInterface &rhs) const
     638              : {
     639            0 :     return !(rhs == *this);
     640              : }
     641              : 
     642              : } // namespace Hccl
        

Generated by: LCOV version 2.0-1