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_wqebb_mgr_v1.h"
12 :
13 : #include <vector>
14 :
15 : #include "ccu_res_specs.h"
16 :
17 : namespace hcomm {
18 :
19 : // 计算 num 的以 2 为底的对数后移位,相当于向上最接近的 2 的整数次幂
20 183 : static uint32_t RoundUpToNextPowerOfTwo(uint32_t num)
21 : {
22 183 : if (num == 0) {
23 0 : return 1;
24 : }
25 :
26 183 : num--;
27 183 : num |= num >> 1;
28 183 : num |= num >> Hccl::SHIFT_2BITS;
29 183 : num |= num >> Hccl::SHIFT_4BITS;
30 183 : num |= num >> Hccl::SHIFT_8BITS;
31 183 : num |= num >> Hccl::SHIFT_16BITS;
32 183 : return num + 1;
33 : }
34 :
35 183 : static uint32_t GetWqeBBReqSizeBySqSize(uint32_t sqSize)
36 : {
37 183 : if (sqSize < CCU_MIN_SQ_DEPTH) {
38 0 : HCCL_WARNING(
39 : "[CcuJettyCtxMgr][%s] sqSize[%u] is too small, reset to [%u].", __func__, sqSize, CCU_MIN_SQ_DEPTH);
40 0 : return CCU_MIN_SQ_DEPTH;
41 : }
42 :
43 : // WQE basic block 的 size 必须是 2 的整数次幂,向上取整
44 183 : uint32_t wqeBBReqNum = RoundUpToNextPowerOfTwo(sqSize);
45 183 : if (wqeBBReqNum != sqSize) {
46 0 : HCCL_WARNING(
47 : "[CcuJettyCtxMgr][%s] sqSize[%u] is not power of 2, reset to [%u].", __func__, sqSize, wqeBBReqNum);
48 : }
49 :
50 183 : if (sqSize > CCU_MAX_SQ_DEPTH) {
51 0 : HCCL_WARNING(
52 : "[CcuJettyCtxMgr][%s] sqSize[%u] is too large, reset to [%u].", __func__, sqSize, CCU_MAX_SQ_DEPTH);
53 0 : return CCU_MAX_SQ_DEPTH;
54 : }
55 :
56 183 : return wqeBBReqNum;
57 : }
58 :
59 170 : HcclResult CcuWqeBBMgrV1::Init()
60 : {
61 170 : uint32_t wqeBBNum = 0; // 获取失败或为0场景,分配将按资源不足操作
62 170 : (void)CcuResSpecifications::GetInstance(devLogicId_).GetWqeBBNum(dieId_, wqeBBNum);
63 170 : idAllocator_.reset(new (std::nothrow) CcuResIdAllocator(wqeBBNum));
64 170 : CHK_PTR_NULL(idAllocator_);
65 170 : return HcclResult::HCCL_SUCCESS;
66 : }
67 :
68 183 : HcclResult CcuWqeBBMgrV1::Alloc(const WqeBBReq& wqeBBReq, ResInfo& wqeBBInfo)
69 : {
70 183 : uint32_t wqeBBReqSize = GetWqeBBReqSizeBySqSize(wqeBBReq.sqSize);
71 183 : std::vector<ResInfo> resInfo;
72 366 : auto ret = idAllocator_->Alloc(wqeBBReqSize, true, resInfo, "ResType::WqeBB"); // wqebb资源要求连续
73 183 : if (ret == HcclResult::HCCL_E_UNAVAIL) {
74 0 : HCCL_WARNING(
75 : "[CcuWqeBBMgrV1][%s] failed, left resources are not enough, "
76 : "wqeBBReq.sqSize[%u], wqeBBSize[%u]",
77 : __func__, wqeBBReq.sqSize, wqeBBReqSize);
78 0 : return ret;
79 : }
80 183 : CHK_RET(ret);
81 :
82 183 : wqeBBInfo = resInfo[0]; // 分配连续资源包含1个元素
83 183 : return ret;
84 183 : }
85 :
86 13 : HcclResult CcuWqeBBMgrV1::Release(const ResInfo& wqeBBInfo)
87 : {
88 13 : auto ret = idAllocator_->Release(wqeBBInfo.startId, wqeBBInfo.num);
89 13 : CHK_PRT_RET(
90 : ret != HcclResult::HCCL_SUCCESS,
91 : HCCL_ERROR(
92 : "[CcuWqeBBMgrV1][%s] failed, wqe basic block resource info[%s]", __func__, wqeBBInfo.Describe().c_str()),
93 : ret);
94 :
95 13 : return HcclResult::HCCL_SUCCESS;
96 : }
97 : }; // namespace hcomm
|