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