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.h"
12 : #include "exception_util.h"
13 : #include "ccu_device_manager.h"
14 :
15 : namespace Hccl {
16 :
17 28 : bool CcuTransportGroup::CheckTransports(const vector<CcuTransport*>& transports)
18 : {
19 28 : if (transports.size() == 0) {
20 6 : HCCL_ERROR("[CcuTransportGroup::%s] Transports size is 0, please check.", __func__);
21 2 : return false;
22 : }
23 78 : HCCL_INFO("[CheckTransports] size[%llu].", transports.size());
24 :
25 : // 校验transports中所有的DieId是否相等,如果不相等,则构建失败,如果相等,则将transports传入transportsGrp中
26 184 : for (unsigned int i = 0; i < transports.size(); i++) {
27 158 : if (transports[0]->GetDieId() != transports[i]->GetDieId()) {
28 0 : HCCL_ERROR("[CcuTransportGroup::%s] Transports dieId is not equal, please check.", __func__);
29 0 : return false;
30 : }
31 : }
32 26 : return true;
33 : }
34 :
35 27 : HcclResult CcuTransportGroup::CheckTransportCntCke()
36 : {
37 27 : HcclResult allocResHandleReturnValue = HcclResult::HCCL_SUCCESS;
38 :
39 27 : TRY_CATCH_RETURN(
40 : allocResHandleReturnValue = CcuDeviceManager::AllocCke(
41 : HrtGetDevice(), cntCkesGroupDieId, cntCkeNumTransportGroupUse, ckeInfoTransportGroupUse));
42 :
43 27 : if (allocResHandleReturnValue != HCCL_SUCCESS) {
44 72 : HCCL_ERROR("[CcuTransportGroup::%s] Failed to allocate cntCke resource, please check.", __func__);
45 24 : return HcclResult::HCCL_E_INTERNAL;
46 : }
47 :
48 3 : for (u32 i = 0; i < ckeInfoTransportGroupUse.size(); i++) {
49 0 : u32 ckeNum = ckeInfoTransportGroupUse[i].num;
50 0 : u32 ckesStartId = ckeInfoTransportGroupUse[i].startId;
51 0 : for (unsigned int j = 0; j < ckeNum; j++) {
52 0 : cntCkesGroup.push_back(ckesStartId + j);
53 : }
54 : }
55 :
56 6 : for (auto& transport : transportsGrp) {
57 3 : transport->SetCntCke(cntCkesGroup);
58 : }
59 3 : return HcclResult::HCCL_SUCCESS;
60 : }
61 :
62 45 : CcuTransportGroup::CcuTransportGroup(const vector<CcuTransport*>& transports, u32 cntCkeNum) : isDestroyed(false)
63 : {
64 135 : HCCL_INFO("[CcuTransportGroup] cntCkeNum[%u].", cntCkeNum);
65 45 : if (!CheckTransports(transports)) {
66 1 : grpStatus = TransportGrpStatus::FAIL;
67 3 : HCCL_ERROR("[CcuTransportGroup::%s] Func CheckTransports failed, please check.", __func__);
68 1 : return;
69 : }
70 :
71 44 : transportsGrp = transports;
72 44 : cntCkesGroupDieId = transports[0]->GetDieId();
73 44 : cntCkeNumTransportGroupUse = cntCkeNum;
74 :
75 44 : HcclResult ret = CheckTransportCntCke();
76 44 : if (ret != HcclResult::HCCL_SUCCESS) {
77 24 : grpStatus = TransportGrpStatus::FAIL;
78 72 : HCCL_ERROR("[CcuTransportGroup::%s] Func CheckTransportCntCke failed, ret=[%d], please check.", __func__, ret);
79 24 : return;
80 : }
81 :
82 20 : grpStatus = TransportGrpStatus::INIT;
83 0 : }
84 :
85 4 : TransportGrpStatus CcuTransportGroup::GetGrpStatus() const
86 : {
87 12 : HCCL_INFO("[CcuTransportGroup] current group status [%s].", grpStatus.Describe().c_str());
88 4 : return grpStatus;
89 : }
90 :
91 52 : CcuTransportGroup::~CcuTransportGroup()
92 : {
93 45 : if (!isDestroyed) {
94 44 : Destroy();
95 : }
96 :
97 : // 调用ReleaseResHandle接口,用来释放cntResHandleTransportGroupUse
98 45 : auto ret = CcuDeviceManager::ReleaseCke(HrtGetDevice(), cntCkesGroupDieId, ckeInfoTransportGroupUse);
99 45 : if (ret != HcclResult::HCCL_SUCCESS) {
100 66 : HCCL_ERROR(
101 : "[CcuTransportGroup::%s] Release ckesRes failed, ret[%d], ckeInfo size is [%u]", __func__, ret,
102 : ckeInfoTransportGroupUse.size());
103 22 : for (auto& ckeInfo : ckeInfoTransportGroupUse) {
104 0 : HCCL_ERROR(
105 : "[CcuTransportGroup::%s] Release ckesRes failed, ckeInfo.startId[%u], ckeInfo.num[%u]", __func__,
106 : ckeInfo.startId, ckeInfo.num);
107 : }
108 : } else {
109 69 : HCCL_INFO("[CcuTransportGroup::%s] CcuTransportGroup Destructor success.", __func__);
110 : }
111 52 : }
112 :
113 37 : void CcuTransportGroup::Destroy()
114 : {
115 37 : isDestroyed = true;
116 37 : transportsGrp.clear();
117 37 : }
118 :
119 68 : HcclResult CcuTransportGroup::GetCntCkeId(u32 index, u32& cntCkeId) const
120 : {
121 204 : HCCL_INFO("[GetCntCkeId] index[%u].", index);
122 68 : if (index >= cntCkesGroup.size()) {
123 0 : HCCL_ERROR(
124 : "[GetCntCkeId] err[%s], index[%u] is bigger than cntCkesGroup size[%u], please check.", __func__, index,
125 : cntCkesGroup.size());
126 0 : return HcclResult::HCCL_E_PARA;
127 : }
128 :
129 68 : cntCkeId = cntCkesGroup[index];
130 68 : return HcclResult::HCCL_SUCCESS;
131 : }
132 :
133 49 : const vector<CcuTransport*>& CcuTransportGroup::GetTransports() const { return transportsGrp; }
134 :
135 : } // namespace Hccl
|