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_jetty_ctx_mgr_v1_legacy.h"
12 :
13 : namespace Hccl {
14 :
15 24 : CcuJettyCtxMgrV1::CcuJettyCtxMgrV1(const int32_t devLogicId, const uint8_t dieId, const uint32_t devPhyId)
16 24 : : CcuJettyCtxMgr(devLogicId, dieId, devPhyId)
17 24 : {}
18 :
19 28 : HcclResult CcuJettyCtxMgrV1::Alloc(
20 : const uint32_t feId, const uint32_t jettyNum, const uint32_t sqSize, std::vector<JettyInfo>& jettyInfos)
21 : {
22 28 : JettyAllocator* allocatorHandle = nullptr;
23 28 : auto ret = GetJettyAllocator(feId, allocatorHandle);
24 28 : CHK_PRT_RET(
25 : ret != HcclResult::HCCL_SUCCESS,
26 : HCCL_WARNING(
27 : "[CcuJettyCtxMgrV1][%s] failed, failed to get jetty allocator handle, "
28 : "devLogicId[%d], dieId[%u], feId[%u].",
29 : __func__, devLogicId, dieId, feId),
30 : HcclResult::HCCL_E_INTERNAL);
31 :
32 28 : vector<ResInfo> jettyResInfos; // jettys分配必须要求连续,故返回一个元素
33 56 : ret = allocatorHandle->idAllocator->Alloc(jettyNum, true, jettyResInfos, "ResType::Jetty");
34 28 : CHK_PRT_RET(
35 : ret != HcclResult::HCCL_SUCCESS,
36 : HCCL_WARNING(
37 : "[CcuJettyCtxMgrV1][%s] failed, allocator failed to allocate, "
38 : "devLogicId[%d], dieId[%u], feId[%u].",
39 : __func__, devLogicId, dieId, feId),
40 : ret);
41 :
42 28 : const auto& strategy = allocatorHandle->strategy;
43 28 : const uint32_t jettyResStartId = jettyResInfos[0].startId; // 资源分配器中的索引号
44 28 : const uint32_t jettyCtxStartId = strategy.startLocalJettyCtxId + jettyResStartId;
45 28 : const uint32_t taJettyStartId = strategy.startTaJettyId + jettyResStartId;
46 28 : CHK_PRT_RET(
47 : jettyCtxStartId > jettySpecNum - jettyNum,
48 : HCCL_WARNING(
49 : "[CcuJettyCtxMgrV1][%s] jetty resource is not enough, allocated "
50 : "jettyCtxId[%u] should be less than %u, devLogicId[%d], dieId[%u], feId[%u].",
51 : __func__, jettyCtxStartId, jettySpecNum, devLogicId, dieId, feId),
52 : HcclResult::HCCL_E_UNAVAIL);
53 :
54 28 : constexpr CcuJettyType type_ = CcuJettyType::CCUM_CACHED_JETTY;
55 28 : jettyInfos.resize(jettyNum);
56 28 : ret = TryAllocWqeBBResource(sqSize, jettyCtxStartId, taJettyStartId, type_, jettyInfos);
57 28 : if (ret != HcclResult::HCCL_SUCCESS) {
58 0 : HCCL_WARNING(
59 : "[CcuJettyCtxMgrV1][%s] failed, try to release temp resource, "
60 : "devLogicId[%d], dieId[%u], feId[%u].",
61 : __func__, devLogicId, dieId, feId);
62 0 : CHK_RET(ReleaseWqeBBResource(jettyInfos)); // jettyInfos已分配的wqeBB资源信息
63 0 : jettyInfos.clear(); // 清理wqebb资源信息
64 0 : CHK_RET(allocatorHandle->idAllocator->Release(jettyResStartId, jettyNum));
65 : }
66 28 : return ret;
67 28 : }
68 :
69 30 : HcclResult CcuJettyCtxMgrV1::GetJettyAllocator(uint32_t feId, JettyAllocator*& allocatorHandle)
70 : {
71 30 : if (allocator_ == nullptr) {
72 72 : HCCL_INFO("[CcuJettyCtxMgrV1][%s] allocator is null, create an allocator", __func__);
73 24 : PfeJettyStrategy strategy = {};
74 24 : CHK_RET(pfeMgr.GetPfeStrategy(feId, strategy)); // 如果strategy为0,后续按资源不足处理
75 24 : TRY_CATCH_RETURN(allocator_ = std::make_unique<JettyAllocator>(strategy));
76 : }
77 :
78 30 : allocatorHandle = allocator_.get();
79 30 : CHK_PTR_NULL(allocatorHandle);
80 30 : return HcclResult::HCCL_SUCCESS;
81 : }
82 :
83 28 : HcclResult CcuJettyCtxMgrV1::Config(
84 : const uint32_t feId, const std::vector<JettyInfo>& jettyInfos, const std::vector<JettyCfg>& jettyCfgs)
85 : {
86 34 : CHK_RET(CheckIfJettyCfgsValid(jettyInfos, jettyCfgs));
87 :
88 26 : std::vector<LocalJettyCtxData> jettyCtxData;
89 26 : const uint32_t jettyNum = jettyInfos.size();
90 54 : for (size_t i = 0; i < jettyNum; i++) {
91 28 : jettyCtxData.emplace_back(BuildJettyCtxData(dieId, feId, jettyInfos[i], jettyCfgs[i]));
92 : }
93 :
94 26 : const uint32_t startJettyCtxId = jettyInfos[0].jettyCtxId;
95 26 : TRY_CATCH_RETURN(ConfigJettyCtxData(devLogicId, dieId, startJettyCtxId, jettyCtxData));
96 26 : return HcclResult::HCCL_SUCCESS;
97 26 : }
98 :
99 2 : HcclResult CcuJettyCtxMgrV1::Release(const uint32_t feId, const std::vector<JettyInfo>& jettyInfos)
100 : {
101 2 : if (jettyInfos.empty()) {
102 0 : HCCL_INFO(
103 : "[CcuJettyCtxMgrV1][%s] passed, jettyInfos is empty, no need to release, ",
104 : "devLogicId[%d], dieId[%u], feId[%u].", __func__, devLogicId, dieId, feId);
105 0 : return HcclResult::HCCL_SUCCESS;
106 : }
107 2 : JettyAllocator* allocatorHandle = nullptr;
108 2 : CHK_RET(GetJettyAllocator(feId, allocatorHandle));
109 2 : CHK_RET(ReleaseWqeBBResource(jettyInfos));
110 :
111 2 : const uint32_t jettyStartCtxId = static_cast<uint32_t>(jettyInfos[0].jettyCtxId);
112 2 : const uint32_t jettyResStartId = jettyStartCtxId - allocatorHandle->strategy.startLocalJettyCtxId;
113 2 : CHK_RET(allocatorHandle->idAllocator->Release(jettyResStartId, jettyInfos.size()));
114 2 : return HcclResult::HCCL_SUCCESS;
115 : }
116 :
117 : }; // namespace Hccl
|