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