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_res_pack.h"
12 :
13 : #include "ccu_log.h"
14 : #include "hcom_common.h"
15 :
16 : namespace hcomm {
17 :
18 105 : CcuResPack::~CcuResPack()
19 : {
20 53 : if (resHandle_ == 0) {
21 1 : return;
22 : }
23 :
24 52 : auto ret = CcuReleaseResHandle(devLogicId_, resHandle_);
25 52 : if (ret != HcclResult::HCCL_SUCCESS) {
26 0 : HCCL_ERROR("[CcuResPack][%s] failed, resHandle[%p] devLogicId[%d].",
27 : __func__, resHandle_, devLogicId_);
28 : }
29 52 : resHandle_ = 0;
30 53 : }
31 :
32 96 : CcuResult CcuResPack::Reset()
33 : {
34 96 : if (!resHandle_) {
35 0 : return CcuResult::CCU_SUCCESS;
36 : }
37 :
38 96 : CCU_CHK_RET(CcuCheckResource(devLogicId_, resHandle_, resRepo_));
39 96 : return CcuResult::CCU_SUCCESS;
40 : }
41 :
42 :
43 2 : CcuResult CcuResPack::InitByInsType(const CcuInstanceType insType)
44 : {
45 2 : devLogicId_ = HcclGetThreadDeviceId();
46 2 : if (insType == CcuInstanceType::CCU_UNUSED) {
47 0 : HCCL_ERROR("[CcuResPack][%s] failed, error ccu instance type[%d].",
48 : __func__, static_cast<int32_t>(insType));
49 0 : return CcuResult::CCU_E_PARA;
50 : }
51 :
52 : // 根据通信域算子展开模式申请资源
53 : // 如果资源不足,返回HCCL_E_UNAVAIL,表示需要回退
54 2 : auto ret = CcuAllocResHandleByInsType(devLogicId_, insType, resHandle_);
55 2 : if (ret == CcuResult::CCU_E_UNAVAIL) {
56 0 : HCCL_RUN_WARNING("[%s] failed but passed, resource is not enough, "
57 : "devLogicId[%d], ccuInsType[%d].", __func__, devLogicId_, insType);
58 0 : return ret;
59 : }
60 2 : CCU_CHK_RET(ret);
61 2 : CCU_CHK_RET(Reset());
62 2 : return CcuResult::CCU_SUCCESS;
63 : }
64 :
65 50 : CcuResult CcuResPack::InitByResDescs(const CcuResDesc *descs[], uint32_t descNum)
66 : {
67 50 : devLogicId_ = HcclGetThreadDeviceId();
68 :
69 : // 基于 resDesc 驱动的资源数量申请资源
70 : // 如果资源不足,返回CCU_E_UNAVAIL,表示需要回退
71 50 : auto ret = CcuAllocResHandleByResDescs(devLogicId_, descs, descNum, resHandle_);
72 50 : if (ret == CcuResult::CCU_E_UNAVAIL) {
73 0 : HCCL_RUN_WARNING("[%s] failed but passed, resource is not enough, "
74 : "devLogicId[%d], descNum[%u].", __func__, devLogicId_, descNum);
75 0 : return ret;
76 : }
77 50 : CCU_CHK_RET(ret);
78 50 : CCU_CHK_RET(Reset());
79 50 : return CcuResult::CCU_SUCCESS;
80 : }
81 :
82 118 : CcuResRepository &CcuResPack::GetCcuResRepo()
83 : {
84 118 : return resRepo_;
85 : }
86 :
87 : } // namespace hcomm
|