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 729 : NetInstance::NetInstance(const u32 netLayer, const string &netInstId, const NetType netType)
21 : {
22 243 : this->netLayer = netLayer;
23 243 : this->netInstId = netInstId;
24 243 : this->netType = netType;
25 243 : }
26 :
27 1207 : u32 NetInstance::GetNetLayer() const
28 : {
29 1207 : return netLayer;
30 : }
31 :
32 568 : string NetInstance::GetNetInstId() const
33 : {
34 568 : return netInstId;
35 : }
36 :
37 155 : NetType NetInstance::GetNetType() const
38 : {
39 155 : return netType;
40 : }
41 :
42 534 : set<RankId> NetInstance::GetRankIds() const
43 : {
44 534 : return rankIds;
45 : }
46 :
47 24 : u32 NetInstance::GetRankSize() const
48 : {
49 24 : return rankIds.size();
50 : }
51 :
52 116 : bool NetInstance::HasNode(const NodeId nodeId) const
53 : {
54 116 : return vGraph.HasNode(nodeId);
55 : }
56 :
57 47 : const std::unordered_map<RankId, std::shared_ptr<NetInstance::Peer>>& NetInstance::GetPeers() const
58 : {
59 47 : return peers;
60 : }
61 :
62 59 : const std::vector<std::shared_ptr<NetInstance::Fabric>>& NetInstance::GetFabrics() const
63 : {
64 59 : return fabrics;
65 : }
66 :
67 270 : Graph<NetInstance::Node, NetInstance::Link>& NetInstance::GetGraph()
68 : {
69 270 : return vGraph;
70 : }
71 :
72 515 : void NetInstance::AddRankId(const RankId rankId)
73 : {
74 515 : rankIds.insert(rankId);
75 1097 : HCCL_DEBUG("[NetInstance::AddRankId] add rank id [%d] to %s", rankId, this->Describe().c_str());
76 515 : }
77 :
78 482 : void NetInstance::AddNode(const shared_ptr<Node> &node)
79 : {
80 482 : NetInstance::Node::NodeType nodeType = node->GetType();
81 482 : if (nodeType == NetInstance::Node::NodeType::PEER) {
82 406 : AddPeer(dynamic_pointer_cast<NetInstance::Peer>(node));
83 76 : } else if (nodeType == NetInstance::Node::NodeType::FABRIC) {
84 76 : 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 482 : }
91 :
92 406 : void NetInstance::AddPeer(const shared_ptr<Peer> &peer)
93 : {
94 406 : 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 406 : localIdsMap.insert({peer->GetLocalId(), peer->GetRankId()});
99 :
100 406 : peers[peer->GetRankId()] = peer;
101 406 : vGraph.AddNode(peer->GetNodeId(), peer);
102 :
103 770 : HCCL_DEBUG("[NetInstance::AddPeer] add %s to %s", peer->Describe().c_str(), this->Describe().c_str());
104 406 : }
105 :
106 76 : void NetInstance::AddFabric(const shared_ptr<NetInstance::Fabric> &fabric)
107 : {
108 76 : 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 76 : NodeId fabricId = fabric->GetNodeId();
115 76 : fabrics.emplace_back(fabric);
116 76 : vGraph.AddNode(fabricId, fabric);
117 :
118 228 : HCCL_DEBUG("[NetInstance::AddFabric] add %s to %s", fabric->Describe().c_str(), this->Describe().c_str());
119 76 : }
120 :
121 769 : void NetInstance::AddLink(const shared_ptr<NetInstance::Link>& link)
122 : {
123 769 : NodeId srcNodeId = link->GetSourceNode()->GetNodeId();
124 769 : NodeId dstNodeId = link->GetTargetNode()->GetNodeId();
125 :
126 769 : bool hasLink = false;
127 769 : vGraph.TraverseEdge(srcNodeId, dstNodeId, [&](shared_ptr<NetInstance::Link> edge) {
128 125 : if (*edge == *link) {
129 17 : hasLink = true;
130 17 : return;
131 : }
132 : });
133 :
134 769 : if (hasLink) {
135 51 : 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 752 : vGraph.AddEdge(srcNodeId, dstNodeId, link);
142 :
143 2032 : HCCL_DEBUG("[NetInstance::AddLink] add %s to %s", link->Describe().c_str(), this->Describe().c_str());
144 : }
145 :
146 24 : void NetInstance::DeleteLink(const NodeId srcNodeId, const NodeId dstNodeId)
147 : {
148 72 : HCCL_RUN_INFO("[NetInstance::DeleteLink] delete %lu -> %lu", srcNodeId, dstNodeId);
149 24 : vGraph.DeleteEdge(srcNodeId, dstNodeId);
150 24 : vGraph.DeleteEdge(dstNodeId, srcNodeId);
151 24 : }
152 :
153 312 : void NetInstance::UpdateTopoInst(u32 topoInstId, TopoType topoType, RankId rankId)
154 : {
155 312 : auto it = topoInsts_.find(topoInstId);
156 312 : if (it != topoInsts_.end()) {
157 234 : TopoInstance& existingInst = *it->second;
158 234 : existingInst.ranks.insert(rankId);
159 : } else {
160 : // 创建新的TopoInstance
161 78 : TopoInstance newInst;
162 78 : newInst.topoInstId = topoInstId;
163 78 : newInst.topoType = topoType;
164 78 : newInst.ranks.insert(rankId);
165 78 : topoInsts_.emplace(topoInstId, std::make_shared<TopoInstance>(std::move(newInst)));
166 78 : }
167 312 : }
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 1813 : string NetInstance::Describe() const
205 : {
206 1813 : return StringFormat("NetInstance[ID=%s, Level=%u, FabType=%s, RankIds_Size=%zu]", netInstId.c_str(), netLayer,
207 3626 : netType.Describe().c_str(), rankIds.size());
208 : }
209 :
210 143 : bool CheckPortGroupSize(u32 netLayer, NetInstance::Link& srcLink, NetInstance::Link& dstLink)
211 : {
212 143 : auto srcConnIface = srcLink.GetSourceIface();
213 143 : auto targetConnIface = dstLink.GetTargetIface();
214 143 : auto srcPortGroupSize = static_cast<u8>(srcConnIface->GetPorts().size());
215 143 : auto tgtPortGroupSize = static_cast<u8>(targetConnIface->GetPorts().size());
216 143 : if (srcPortGroupSize != tgtPortGroupSize) {
217 1 : auto srcPeer = srcLink.GetSourceNode();
218 1 : auto targetPeer = dstLink.GetTargetNode();
219 1 : auto localAddr = srcConnIface->GetAddr();
220 1 : auto remoteAddr = targetConnIface->GetAddr();
221 1 : auto localRankId = std::dynamic_pointer_cast<NetInstance::Peer>(srcPeer)->GetRankId();
222 1 : auto remoteRankId = std::dynamic_pointer_cast<NetInstance::Peer>(targetPeer)->GetRankId();
223 3 : HCCL_WARNING("[GetPaths][CheckPortGroupSize] portGroupSize is not equal => src[%u], target[%u]."
224 : "LocatedInfo: NetLayer[%u], localRank[%d], rmtRank[%d], localAddr[%s], rmtAddr[%s]", srcPortGroupSize, tgtPortGroupSize,
225 : netLayer, localRankId, remoteRankId, localAddr.Describe().c_str(), remoteAddr.Describe().c_str());
226 1 : return false;
227 1 : }
228 142 : return true;
229 143 : }
230 :
231 135 : vector<NetInstance::Path> InnerNetInstance::GetPaths(const RankId srcRankId, const RankId dstRankId) const
232 : {
233 135 : vector<NetInstance::Path> paths;
234 135 : 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 135 : NodeId srcPeerId = peers.at(srcRankId)->GetNodeId();
240 135 : NodeId dstPeerId = peers.at(dstRankId)->GetNodeId();
241 : // 1. 获取边
242 135 : vGraph.TraverseEdge(srcPeerId, dstPeerId, [&](shared_ptr<NetInstance::Link> edge) {
243 116 : NetInstance::Path path;
244 232 : path.links = {*edge};
245 116 : path.direction = edge->GetLinkDirection();
246 116 : paths.emplace_back(path);
247 346 : 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 346 : HCCL_DEBUG("[InnerNetInstance::GetPaths] netLayer[%u], srcRankId[%d], dstRankId[%d], path[%s]", netLayer,
250 : srcRankId, dstRankId, path.links[0].Describe().c_str());
251 232 : });
252 :
253 : // 2. 通过 fabric 的路径
254 240 : for (auto& fabric : fabrics) {
255 105 : NodeId fabricId = fabric->GetNodeId();
256 :
257 : // 所有 src -> fabric 的链路
258 105 : vector<NetInstance::Link> srcToFabricLinks;
259 105 : vGraph.TraverseEdge(srcPeerId, fabricId, [&](shared_ptr<NetInstance::Link> edge) {
260 69 : srcToFabricLinks.push_back(*edge);
261 69 : return;
262 : });
263 :
264 : // 所有 fabric -> dst 的链路
265 105 : vector<NetInstance::Link> fabricToDstLinks;
266 105 : vGraph.TraverseEdge(fabricId, dstPeerId, [&](shared_ptr<NetInstance::Link> edge) {
267 69 : fabricToDstLinks.push_back(*edge);
268 69 : return;
269 : });
270 :
271 105 : if (!srcToFabricLinks.empty() && !fabricToDstLinks.empty()) {
272 114 : for (auto& srcLink : srcToFabricLinks) {
273 114 : for (auto& dstLink : fabricToDstLinks) {
274 57 : if (!CheckPortGroupSize(netLayer, srcLink, dstLink)) {
275 1 : continue;
276 : }
277 56 : NetInstance::Path path;
278 168 : path.links = {srcLink, dstLink};
279 56 : paths.emplace_back(path);
280 56 : }
281 : }
282 : } else {
283 144 : 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 105 : }
289 :
290 135 : return paths;
291 56 : }
292 :
293 202 : const std::unordered_map<u32, std::vector<std::shared_ptr<NetInstance::ConnInterface>>> NetInstance::Node::GetInterfacesMap() const
294 : {
295 202 : return interfacesMap_;
296 : }
297 :
298 48 : vector<NetInstance::Path> ClosNetInstance::GetPaths(const RankId srcRankId, const RankId dstRankId) const
299 : {
300 48 : vector<NetInstance::Path> paths;
301 48 : if (peers.count(srcRankId) == 0 || peers.count(dstRankId) == 0) {
302 6 : HCCL_WARNING("[ClosNetInstance::GetPaths] srcRankId[%d] or dstRankId[%d] not exist in netInstance, "
303 : "netLayer[%u], netInstId[%s].", srcRankId, dstRankId, netLayer, netInstId.c_str());
304 2 : return paths;
305 : }
306 46 : NodeId srcPeerId = peers.at(srcRankId)->GetNodeId();
307 46 : NodeId dstPeerId = peers.at(dstRankId)->GetNodeId();
308 216 : for (auto &fabric : fabrics) {
309 170 : NodeId fabricId = fabric->GetNodeId();
310 :
311 170 : NetInstance::Link srcToFabricLink;
312 170 : vGraph.TraverseEdge(srcPeerId, fabricId, [&](shared_ptr<NetInstance::Link> edge) {
313 86 : srcToFabricLink = *edge;
314 86 : return;
315 : });
316 :
317 170 : NetInstance::Link fabricToDstLink;
318 170 : vGraph.TraverseEdge(fabricId, dstPeerId, [&](shared_ptr<NetInstance::Link> edge) {
319 86 : fabricToDstLink = *edge;
320 86 : return;
321 : });
322 :
323 170 : if (!srcToFabricLink.IsEmpty() && !fabricToDstLink.IsEmpty()) {
324 86 : if (!CheckPortGroupSize(netLayer, srcToFabricLink, fabricToDstLink)) {
325 0 : continue;
326 : }
327 86 : NetInstance::Path path;
328 258 : path.links = {srcToFabricLink, fabricToDstLink};
329 86 : paths.emplace_back(path);
330 86 : } else {
331 252 : 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 170 : }
336 :
337 46 : return paths;
338 86 : }
339 :
340 987 : void NetInstance::Node::AddConnInterface(u32 layer, const shared_ptr<NetInstance::ConnInterface>& interface)
341 : {
342 987 : auto& interfacesVec = interfacesMap_[layer];
343 1870 : for (const auto& iface : interfacesVec) {
344 1052 : if (*iface == *interface) {
345 507 : HCCL_WARNING("[NetInstance][Node][AddConnInterface] interface addr[%s] has existed.",
346 : interface->GetAddr().Describe().c_str());
347 169 : return;
348 : }
349 : }
350 :
351 818 : interfacesVec.emplace_back(interface);
352 : }
353 :
354 248 : void NetInstance::Node::AddConnInterfaces(u32 layer,
355 : const std::vector<std::shared_ptr<NetInstance::ConnInterface>>& interfaces)
356 : {
357 248 : if (interfaces.empty()) {
358 0 : return;
359 : }
360 496 : for (auto interface : interfaces) {
361 248 : AddConnInterface(layer, interface);
362 248 : }
363 : }
364 :
365 540 : NetInstance::Node::NodeType NetInstance::Node::GetType() const
366 : {
367 540 : return type_;
368 : }
369 :
370 52 : std::vector<std::shared_ptr<NetInstance::ConnInterface>> NetInstance::Node::GetIfacesByLayer(u32 layer) const
371 : {
372 52 : auto it = interfacesMap_.find(layer);
373 52 : if (it == interfacesMap_.end()) {
374 9 : HCCL_WARNING("[NetInstance][Node][GetIfacesByLayer] netLayer[%u] not exist.", layer);
375 3 : return std::vector<std::shared_ptr<NetInstance::ConnInterface>>{};
376 : }
377 49 : 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 85 : void NetInstance::Node::SetEndpointToIface(const CommAddr& commAddr, CommProtocol protocol,
393 : const std::shared_ptr<NetInstance::ConnInterface>& iface)
394 : {
395 85 : endpointToIfaceMap_[std::make_pair(commAddr, protocol)] = iface;
396 85 : }
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 6559 : NodeId NetInstance::Node::GetNodeId() const
404 : {
405 6559 : return nodeId_;
406 : }
407 :
408 1590 : LocalId NetInstance::Peer::GetLocalId() const
409 : {
410 1590 : return localId_;
411 : }
412 :
413 16 : LocalId NetInstance::Peer::GetReplacedLocalId() const
414 : {
415 16 : return replacedLocalId_;
416 : }
417 :
418 138 : DeviceId NetInstance::Peer::GetDeviceId() const
419 : {
420 138 : 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 2101 : RankId NetInstance::Peer::GetRankId() const
434 : {
435 2101 : return rankId_;
436 : }
437 :
438 126 : set<u32> NetInstance::Peer::GetLevels() const
439 : {
440 126 : return netLayers_;
441 : }
442 :
443 444 : const NetInstance *NetInstance::Peer::GetNetInstance(u32 netLayer) const
444 : {
445 444 : if (netLayer >= netInsts_.size() || netInsts_.at(netLayer) == nullptr) {
446 114 : HCCL_WARNING("[NetInstance][Peer][GetNetInstance] netLayer[%u] not exist.", netLayer);
447 38 : return nullptr;
448 : }
449 406 : return netInsts_[netLayer];
450 : }
451 :
452 764 : NodeId NetInstance::Peer::GenerateNodeId(RankId rankId)
453 : {
454 764 : return (static_cast<u64>(rankId) | static_cast<u64>(0) << 32); // 第32位为0 + rankId
455 : }
456 :
457 1269 : string NetInstance::Peer::Describe() const
458 : {
459 1269 : return StringFormat("NetInstance::Peer[rankId=%d, localId=%u, NodeId=%llu, netLayers_size=%zu]", rankId_, localId_, nodeId_,
460 1269 : netLayers_.size());
461 : }
462 :
463 488 : void NetInstance::Peer::AddNetInstance(const std::shared_ptr<NetInstance> &netInst)
464 : {
465 488 : u32 netLayer = netInst->GetNetLayer();
466 488 : if (netLayer >= netInsts_.size()) {
467 487 : netInsts_.resize(netLayer + 1);
468 : }
469 :
470 488 : 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 487 : netInsts_[netLayer] = netInst.get();
475 487 : netLayers_.insert(netInst->GetNetLayer());
476 487 : }
477 :
478 71 : void NetInstance::Peer::SetPortPortAddrMapLayer0(std::map<std::string, std::vector<IpAddress>> portAddrMap)
479 : {
480 71 : portAddrMapLayer0_ = std::move(portAddrMap);
481 71 : }
482 :
483 408 : std::map<std::string, std::vector<IpAddress>> NetInstance::Peer::GetPortAddrMapLayer0() const
484 : {
485 408 : return portAddrMapLayer0_;
486 : }
487 :
488 17 : PlaneId NetInstance::Fabric::GetPlaneId() const
489 : {
490 17 : return planeId_;
491 : }
492 :
493 77 : NodeId NetInstance::Fabric::GenerateNodeId(FabricId fabricId) const
494 : {
495 77 : return (static_cast<u64>(fabricId) | static_cast<u64>(1) << 32); // 第32位为1 + netplaneId
496 : }
497 :
498 347 : string NetInstance::Fabric::Describe() const
499 : {
500 347 : 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 129 : std::set<LinkProtocol> NetInstance::Link::GetLinkProtocols() const
509 : {
510 129 : return linkProtocols_;
511 : }
512 :
513 145 : LinkDirection NetInstance::Link::GetLinkDirection() const
514 : {
515 145 : return direction_;
516 : }
517 :
518 117 : u32 NetInstance::Link::GetHop() const
519 : {
520 117 : return hop_;
521 : }
522 :
523 1054 : shared_ptr<NetInstance::Node> NetInstance::Link::GetSourceNode() const
524 : {
525 1054 : return source_;
526 : }
527 :
528 1054 : shared_ptr<NetInstance::Node> NetInstance::Link::GetTargetNode() const
529 : {
530 1054 : return target_;
531 : }
532 :
533 296 : shared_ptr<NetInstance::ConnInterface> NetInstance::Link::GetSourceIface() const
534 : {
535 296 : return sourceIface_;
536 : }
537 :
538 296 : shared_ptr<NetInstance::ConnInterface> NetInstance::Link::GetTargetIface() const
539 : {
540 296 : return targetIface_;
541 : }
542 :
543 1382 : string NetInstance::Link::Describe() const
544 : {
545 1382 : stringstream iFace;
546 1382 : if (sourceIface_ != nullptr) {
547 992 : iFace << ", srcIface=" << sourceIface_->Describe();
548 : }
549 1382 : if (targetIface_ != nullptr) {
550 992 : iFace << ", dstIface=" << targetIface_->Describe();
551 : }
552 1382 : std::stringstream linkProtocolsStr;
553 2764 : for (auto protocol : linkProtocols_) {
554 1382 : if (!linkProtocolsStr.str().empty()) {
555 0 : linkProtocolsStr << ", ";
556 : }
557 1382 : linkProtocolsStr << protocol;
558 : }
559 : return StringFormat("NetInstance::Link[sourceId=%llu, targetId=%llu, type=%s, hop=%u, direction=%s, linkProtocol=%s%s]",
560 2764 : source_->GetNodeId(), target_->GetNodeId(), type_.Describe().c_str(), hop_,
561 5528 : direction_.Describe().c_str(), linkProtocolsStr.str().c_str(), iFace.str().c_str());
562 1382 : }
563 :
564 256 : bool NetInstance::Link::IsEmpty() const
565 : {
566 256 : return (source_ == nullptr) && (target_ == nullptr);
567 : }
568 :
569 125 : bool NetInstance::Link::operator==(const NetInstance::Link &rhs) const
570 : {
571 250 : return source_->GetNodeId() == rhs.source_->GetNodeId() && target_->GetNodeId() == rhs.target_->GetNodeId()
572 125 : && sourceIface_ == rhs.sourceIface_ && targetIface_ == rhs.targetIface_ && type_ == rhs.type_
573 250 : && 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 619 : IpAddress NetInstance::ConnInterface::GetAddr() const
582 : {
583 619 : return addr;
584 : }
585 :
586 288 : std::set<string> NetInstance::ConnInterface::GetPorts() const
587 : {
588 288 : return ports;
589 : }
590 :
591 475 : AddrPosition NetInstance::ConnInterface::GetPos() const
592 : {
593 475 : return pos;
594 : }
595 :
596 0 : LinkType NetInstance::ConnInterface::GetLinkType() const
597 : {
598 0 : return linkType;
599 : }
600 :
601 836 : std::set<LinkProtocol> NetInstance::ConnInterface::GetLinkProtocols() const
602 : {
603 836 : return linkProtocols;
604 : }
605 :
606 49 : void NetInstance::ConnInterface::SetLocalDieId(u32 dieId)
607 : {
608 49 : localDieId_ = dieId;
609 49 : }
610 :
611 42 : u32 NetInstance::ConnInterface::GetLocalDieId() const
612 : {
613 42 : 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 1984 : std::string NetInstance::ConnInterface::Describe() const
627 : {
628 1984 : return StringFormat("ConnIface[addr=%s, pos=%s, topoInstId=%u, topoType=%d, locallocalDieId=%u]", addr.Describe().c_str(), pos.Describe().c_str(), topoInstId, topoType, localDieId_);
629 : }
630 :
631 1052 : bool NetInstance::ConnInterface::operator==(const NetInstance::ConnInterface &rhs) const
632 : {
633 1245 : return addr == rhs.addr && pos == rhs.pos && linkType == rhs.linkType &&
634 1245 : 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
|