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 "ccu_transport_group_manager.h"
12 : #include "ccu_transport_manager.h"
13 : #include "ccu_transport_group.h"
14 : #include "exception_util.h"
15 : #include "internal_exception.h"
16 : #include "communicator_impl.h"
17 : #include "coll_service_device_mode.h"
18 :
19 : namespace Hccl {
20 :
21 274 : CcuTransportGroupMgr::CcuTransportGroupMgr(CommunicatorImpl& comm) : comm(&comm) { isDestroyed = false; }
22 :
23 274 : CcuTransportGroupMgr::~CcuTransportGroupMgr()
24 : {
25 274 : if (!isDestroyed) {
26 273 : DECTOR_TRY_CATCH("CcuTransportGroupMgr", Destroy());
27 : }
28 274 : }
29 :
30 4 : CcuTransportGroup* CcuTransportGroupMgr::Get(const LinkGroup& linkGrp)
31 : {
32 4 : auto linkGrpIter = linkGrp2TransportGrpMap.find(linkGrp);
33 4 : if (linkGrpIter != linkGrp2TransportGrpMap.end()) {
34 2 : return linkGrpIter->second.get();
35 : }
36 6 : HCCL_WARNING(
37 : "[CcuTransportGroupMgr::%s] CcuTransportGroup does not existed, "
38 : "errNo[0x%016llx], RankGroup size:%u",
39 : __func__, HCCL_ERROR_CODE(HcclResult::HCCL_E_PTR), linkGrp.GetLinks().size());
40 2 : return nullptr;
41 : }
42 :
43 2 : CcuTransportGroup* CcuTransportGroupMgr::PrepareCreate(const LinkGroup& linkGrp, u32 cntCkeNum)
44 : {
45 : // 如果linkGrp2TransportGrpMap中存在linkGrp对应的transportGroup,则直接返回
46 2 : auto ccuTransportGrp = Get(linkGrp);
47 2 : if (ccuTransportGrp != nullptr) {
48 1 : return ccuTransportGrp;
49 : }
50 :
51 1 : return CreateTransportGroupByLinkGrp(linkGrp, cntCkeNum);
52 : }
53 :
54 3 : CcuTransportGroup* CcuTransportGroupMgr::CreateTransportGroupByLinkGrp(const LinkGroup& linkGrp, u32 cntCkeNum)
55 : {
56 3 : CHECK_NULLPTR(comm, "[CcuTransportGroupMgr::CreateTransportGroupByLinkGrp] comm is nullptr!");
57 6 : CcuTransportMgr* ccuTransportMgr = dynamic_cast<CollServiceDeviceMode*>(comm->GetCollService())
58 : ->GetCcuInsPreprocessor()
59 : ->GetCcuComm()
60 6 : ->GetCcuTransportMgr();
61 3 : vector<CcuTransport*> ccuTransports;
62 6 : for (auto& linkInfo : linkGrp.GetLinks()) {
63 3 : const auto transportsPerRemoteRank = ccuTransportMgr->Get(linkInfo.rankId);
64 3 : if (transportsPerRemoteRank.size() != 0) {
65 3 : for (auto& transport : transportsPerRemoteRank) {
66 3 : if (transport->GetDieId() == linkInfo.dieId) {
67 3 : ccuTransports.emplace_back(transport);
68 : // 如果找到,先break(避免算法返回的linkGroup中的单个linkInfo对应多条transport)
69 3 : break;
70 : }
71 : }
72 : }
73 6 : }
74 :
75 : std::unique_ptr<CcuTransportGroup> newTransportGroup
76 3 : = std::make_unique<CcuTransportGroup>(ccuTransports, cntCkeNum);
77 :
78 : // TransportGroup如果创建失败,则返回nullptr,不抛异
79 3 : if (newTransportGroup->GetGrpStatus() != TransportGrpStatus::INIT) {
80 1 : auto msg = StringFormat("[CcuTransportGroupMgr::%s] Fail to create transportGroup", __func__);
81 3 : HCCL_WARNING(msg.c_str());
82 1 : return nullptr;
83 1 : }
84 2 : linkGrp2TransportGrpMap[linkGrp] = std::move(newTransportGroup);
85 2 : tempTransportGrp.emplace_back(linkGrp);
86 :
87 2 : return linkGrp2TransportGrpMap[linkGrp].get();
88 3 : }
89 :
90 4 : void CcuTransportGroupMgr::Confirm() { tempTransportGrp.clear(); }
91 :
92 1 : void CcuTransportGroupMgr::Clean()
93 : {
94 1 : for (auto& linkGrpTransPair : linkGrp2TransportGrpMap) {
95 0 : linkGrpTransPair.second = nullptr;
96 : }
97 1 : }
98 :
99 3 : void CcuTransportGroupMgr::ResumeAll(u32 cntCkeNum)
100 : {
101 3 : vector<LinkGroup> linkGroups;
102 5 : for (auto iter = linkGrp2TransportGrpMap.begin(); iter != linkGrp2TransportGrpMap.end(); ++iter) {
103 2 : linkGroups.push_back(iter->first);
104 : }
105 3 : if (linkGroups.size() == 0) {
106 3 : HCCL_WARNING("[CcuTransportGroupMgr][%s] used linkGroups vec is empty", __func__);
107 1 : return;
108 : }
109 :
110 3 : for (auto& linkGroup : linkGroups) {
111 2 : CcuTransportGroup* transportGrp = CreateTransportGroupByLinkGrp(linkGroup, cntCkeNum);
112 2 : if (transportGrp == nullptr) {
113 1 : THROW<InternalException>(
114 : "[CcuTransportGroupMgr][%s] transportGrp alloc resource fail, "
115 : "linkGroup size[%zu], cntCkeNum[%u]",
116 3 : __func__, linkGroup.GetLinks().size(), cntCkeNum);
117 : }
118 : }
119 3 : }
120 :
121 4 : void CcuTransportGroupMgr::Fallback()
122 : {
123 5 : for (auto& linkGrp : tempTransportGrp) {
124 1 : auto iterLinkGrp = linkGrp2TransportGrpMap.find(linkGrp);
125 1 : linkGrp2TransportGrpMap.erase(iterLinkGrp);
126 : }
127 4 : tempTransportGrp.clear();
128 4 : }
129 :
130 274 : void CcuTransportGroupMgr::Destroy()
131 : {
132 274 : isDestroyed = true;
133 274 : linkGrp2TransportGrpMap.clear();
134 274 : }
135 :
136 2 : vector<LinkGroup> CcuTransportGroupMgr::GetAllTransportGroups()
137 : {
138 2 : vector<LinkGroup> linkGroups;
139 3 : for (auto iter = linkGrp2TransportGrpMap.begin(); iter != linkGrp2TransportGrpMap.end(); ++iter) {
140 1 : linkGroups.push_back(iter->first);
141 : }
142 2 : if (linkGroups.size() == 0) {
143 1 : THROW<InternalException>("[CcuTransportGroupMgr][%s] used linkGroups vec is empty", __func__);
144 : }
145 1 : return linkGroups;
146 1 : }
147 :
148 : } // namespace Hccl
|