Line data Source code
1 : /**
2 : * Copyright (c) 2026 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_instance_mgr.h"
12 :
13 : #include "ccu_log.h"
14 : #include "hccl_common.h"
15 :
16 : namespace hcomm {
17 :
18 132 : CcuInstanceMgr::~CcuInstanceMgr()
19 : {
20 132 : if (!initializedFlag_) {
21 132 : return;
22 : }
23 :
24 0 : (void)Deinit();
25 264 : }
26 :
27 1721 : CcuInstanceMgr &CcuInstanceMgr::GetInstance(const int32_t deviceLogicId)
28 : {
29 1853 : static CcuInstanceMgr instanceMgrs[MAX_MODULE_DEVICE_NUM + 1];
30 :
31 1721 : int32_t devLogicId = deviceLogicId;
32 1721 : if (devLogicId < 0 || static_cast<uint32_t>(devLogicId) >= MAX_MODULE_DEVICE_NUM) {
33 0 : HCCL_WARNING("[CcuInstanceMgr][%s] use the backup device, devLogicId[%d] should be "
34 : "less than %u.", __func__, devLogicId, MAX_MODULE_DEVICE_NUM);
35 0 : devLogicId = MAX_MODULE_DEVICE_NUM; // 使用备份设备
36 : }
37 :
38 1721 : instanceMgrs[devLogicId].devLogicId_ = devLogicId;
39 1721 : return instanceMgrs[devLogicId];
40 : }
41 :
42 6 : CcuResult CcuInstanceMgr::Init()
43 : {
44 6 : std::unique_lock<std::shared_timed_mutex> lock(insMapMutex_);
45 6 : if (initializedFlag_) {
46 0 : return CcuResult::CCU_SUCCESS;
47 : }
48 :
49 6 : initializedFlag_ = true;
50 6 : insMap_.clear();
51 6 : return CcuResult::CCU_SUCCESS;
52 6 : }
53 :
54 117 : CcuResult CcuInstanceMgr::Deinit()
55 : {
56 117 : std::unique_lock<std::shared_timed_mutex> lock(insMapMutex_);
57 117 : insMap_.clear();
58 117 : (void)resDescMgr_.Deinit();
59 117 : initializedFlag_ = false;
60 117 : return CcuResult::CCU_SUCCESS;
61 117 : }
62 :
63 2 : CcuResult CcuInstanceMgr::CreateByInsType(const CcuInstanceType insType, CcuInsHandle &insHandle)
64 : {
65 2 : std::unique_lock<std::shared_timed_mutex> lock(insMapMutex_);
66 :
67 2 : std::unique_ptr<CcuInstance> instance{nullptr};
68 2 : EXCEPTION_CATCH(
69 : instance = std::make_unique<CcuInstance>(),
70 : return CcuResult::CCU_E_INTERNAL);
71 :
72 2 : CCU_CHK_RET(instance->InitByInsType(insType));
73 :
74 2 : instanceId_ += 1;
75 2 : EXCEPTION_CATCH(
76 : insMap_.emplace(instanceId_, std::move(instance)),
77 : return CcuResult::CCU_E_INTERNAL);
78 2 : insHandle = instanceId_;
79 2 : return CcuResult::CCU_SUCCESS;
80 2 : }
81 :
82 133 : CcuInstance *CcuInstanceMgr::Get(CcuInsHandle insHandle) const
83 : {
84 133 : std::shared_lock<std::shared_timed_mutex> lock(insMapMutex_);
85 133 : auto it = insMap_.find(insHandle);
86 133 : if (it == insMap_.end()) {
87 4 : HCCL_ERROR("[CcuInstanceMgr][%s] handle[%llx] is not existed.",
88 : __func__, insHandle);
89 4 : return nullptr;
90 : }
91 :
92 129 : return it->second.get();
93 133 : }
94 :
95 55 : CcuResult CcuInstanceMgr::Destroy(CcuInsHandle insHandle)
96 : {
97 55 : std::unique_lock<std::shared_timed_mutex> lock(insMapMutex_);
98 55 : auto it = insMap_.find(insHandle);
99 55 : if (it == insMap_.end()) {
100 4 : HCCL_ERROR("[CcuInstanceMgr][%s] handle[%llx] is not existed.",
101 : __func__, insHandle);
102 4 : return CcuResult::CCU_E_NOT_FOUND;
103 : }
104 :
105 51 : insMap_.erase(it);
106 51 : return CcuResult::CCU_SUCCESS;
107 55 : }
108 :
109 51 : CcuResult CcuInstanceMgr::CreateByResDescs(
110 : const CcuResDesc *descs[], uint32_t descNum, CcuInsHandle &insHandle)
111 : {
112 51 : std::unique_lock<std::shared_timed_mutex> lock(insMapMutex_);
113 :
114 51 : std::unique_ptr<CcuInstance> instance{nullptr};
115 51 : EXCEPTION_CATCH(
116 : instance = std::make_unique<CcuInstance>(),
117 : return CcuResult::CCU_E_INTERNAL);
118 :
119 51 : CCU_CHK_RET(instance->InitByResDescs(descs, descNum));
120 :
121 49 : instanceId_ += 1;
122 49 : EXCEPTION_CATCH(
123 : insMap_.emplace(instanceId_, std::move(instance)),
124 : return CcuResult::CCU_E_INTERNAL);
125 49 : insHandle = instanceId_;
126 49 : return CcuResult::CCU_SUCCESS;
127 51 : }
128 :
129 2 : CcuResult CcuInstanceMgr::CreateByAllRes(CcuInsHandle &insHandle)
130 : {
131 2 : std::unique_lock<std::shared_timed_mutex> lock(insMapMutex_);
132 :
133 2 : std::unique_ptr<CcuInstance> instance{nullptr};
134 2 : EXCEPTION_CATCH(
135 : instance = std::make_unique<CcuInstance>(),
136 : return CcuResult::CCU_E_INTERNAL);
137 :
138 2 : CCU_CHK_RET(instance->InitByAllRes());
139 :
140 1 : instanceId_ += 1;
141 1 : EXCEPTION_CATCH(
142 : insMap_.emplace(instanceId_, std::move(instance)),
143 : return CcuResult::CCU_E_INTERNAL);
144 1 : insHandle = instanceId_;
145 1 : return CcuResult::CCU_SUCCESS;
146 2 : }
147 :
148 2 : CcuResult CcuInstanceMgr::QueryInsResDesc(CcuInsHandle &ccuInsHandle, uint8_t dieId, HcommCcuResDescHandle &resDesc)
149 : {
150 2 : std::shared_lock<std::shared_timed_mutex> lock(insMapMutex_);
151 :
152 2 : auto it = insMap_.find(ccuInsHandle);
153 2 : if (it == insMap_.end()) {
154 0 : HCCL_ERROR("[CcuInstanceMgr][%s] handle[%llx] is not existed.",
155 : __func__, ccuInsHandle);
156 0 : return CcuResult::CCU_E_NOT_FOUND;
157 : }
158 :
159 : // 从 ccuIns 持有的 totalResDescs_ 取该 die 的资源描述符,逐项写入入参 resDesc
160 2 : const auto &totalDesc = it->second->GetTotalResDescs(dieId);
161 2 : uint32_t num = 0;
162 2 : CCU_CHK_RET(totalDesc.QueryResNum(ResType::LOOP, num));
163 2 : CCU_CHK_RET(resDescMgr_.SetResNum(resDesc, ResType::LOOP, num));
164 2 : CCU_CHK_RET(totalDesc.QueryResNum(ResType::MS, num));
165 2 : CCU_CHK_RET(resDescMgr_.SetResNum(resDesc, ResType::MS, num));
166 2 : CCU_CHK_RET(totalDesc.QueryResNum(ResType::CKE, num));
167 2 : CCU_CHK_RET(resDescMgr_.SetResNum(resDesc, ResType::CKE, num));
168 2 : CCU_CHK_RET(totalDesc.QueryResNum(ResType::XN, num));
169 2 : CCU_CHK_RET(resDescMgr_.SetResNum(resDesc, ResType::XN, num));
170 2 : CCU_CHK_RET(totalDesc.QueryResNum(ResType::GSA, num));
171 2 : CCU_CHK_RET(resDescMgr_.SetResNum(resDesc, ResType::GSA, num));
172 2 : CCU_CHK_RET(totalDesc.QueryResNum(ResType::MISSION, num));
173 2 : CCU_CHK_RET(resDescMgr_.SetResNum(resDesc, ResType::MISSION, num));
174 2 : CCU_CHK_RET(totalDesc.QueryResNum(ResType::INS, num));
175 2 : CCU_CHK_RET(resDescMgr_.SetResNum(resDesc, ResType::INS, num));
176 :
177 2 : return CcuResult::CCU_SUCCESS;
178 2 : }
179 :
180 1450 : CcuResDescMgr &CcuInstanceMgr::GetResDescMgr()
181 : {
182 1450 : return resDescMgr_;
183 : }
184 :
185 : } // namespace hcomm
|