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 : #ifndef CCU_RES_BATCH_ALLOCATOR_H
12 : #define CCU_RES_BATCH_ALLOCATOR_H
13 :
14 : #include <array>
15 : #include <mutex>
16 : #include <memory>
17 : #include <vector>
18 : #include <cstdint>
19 : #include <unordered_map>
20 :
21 : #include "ccu_res_specs.h"
22 : #include "ccu_dev_mgr_imp.h"
23 : #include "ccu_device_res.h"
24 : #include "ccu_res_desc.h"
25 :
26 : namespace hcomm {
27 :
28 : struct BlockInfo {
29 : uint32_t id{0};
30 : uint32_t startId{0};
31 : uint32_t num{0};
32 : uintptr_t handle{0};
33 : bool allocated{false};
34 : };
35 :
36 : struct CcuResBlockNums {
37 : uint32_t loopNum{0};
38 : uint32_t msNum{0};
39 : uint32_t ckeNum{0};
40 : uint32_t xnNum{0};
41 : uint32_t gsaNum{0};
42 : };
43 :
44 : class CcuResBatchAllocator {
45 : public:
46 : static CcuResBatchAllocator& GetInstance(const int32_t deviceLogicId);
47 : HcclResult Init();
48 : HcclResult Deinit();
49 :
50 : HcclResult AllocResHandle(const CcuResReq& resReq, CcuResHandle& resHandle);
51 : HcclResult ReleaseResHandle(const CcuResHandle& handle);
52 : HcclResult GetResource(const CcuResHandle& handle, CcuResRepository& ccuResRepo);
53 : HcclResult QueryRemainRes(uint8_t dieId, ResType resType, uint32_t& remainNum) const;
54 : // 获取指定 die 上可分配的块类型资源数量 = 预分配块数量 × CcuBlockResStrategy 对应块大小
55 : // 仅支持 ResType::LOOP / MS / CKE / XN / GSA
56 : HcclResult GetAllocatableMaxBlockResNum(ResType resType, uint8_t dieId, uint32_t& num) const;
57 :
58 : private:
59 : class CcuMissionMgr {
60 : public:
61 141 : CcuMissionMgr() = default;
62 141 : ~CcuMissionMgr() = default;
63 :
64 : void Reset();
65 : HcclResult PreAlloc(
66 : const int32_t devLogicId, const uint32_t blockSize, const std::array<bool, CCU_MAX_IODIE_NUM>& dieFlags);
67 : HcclResult Alloc(const uintptr_t handleKey, const MissionReq& missionReq, MissionResInfo& missionInfos);
68 : void Release(MissionResInfo& missionInfos);
69 1 : const std::vector<BlockInfo>& GetBlocks() const { return blocks_; }
70 :
71 : private:
72 : uint32_t stragtegy_{0};
73 : std::array<bool, CCU_MAX_IODIE_NUM> dieEnableFlags_;
74 : std::vector<BlockInfo> blocks_;
75 : };
76 :
77 : private:
78 564 : explicit CcuResBatchAllocator() = default;
79 : CcuResBatchAllocator(const CcuResBatchAllocator& that) = delete;
80 : CcuResBatchAllocator& operator=(const CcuResBatchAllocator& that) = delete;
81 141 : ~CcuResBatchAllocator() = default; // 不允许在析构中调用CcuComponent,会引起未定义行为
82 :
83 : HcclResult PreAllocBlockRes();
84 : HcclResult TryAllocResHandle(
85 : const uintptr_t handleKey, const CcuResReq& resReq, std::unique_ptr<CcuResRepository>& resRepoPtr);
86 : HcclResult
87 : AllocBlockRes(const uintptr_t handleKey, const CcuResReq& resReq, std::unique_ptr<CcuResRepository>& resRepoPtr);
88 : HcclResult AllocConsecutiveRes(const CcuResReq& resReq, std::unique_ptr<CcuResRepository>& resRepoPtr) const;
89 : HcclResult AllocDiscreteRes(const CcuResReq& resReq, std::unique_ptr<CcuResRepository>& resRepoPtr) const;
90 : HcclResult ReleaseResource(std::unique_ptr<CcuResRepository>& resRepoPtr);
91 : void ReleaseBlockResource(std::unique_ptr<CcuResRepository>& resRepoPtr);
92 : HcclResult ReleaseNonBlockTypeRes(std::unique_ptr<CcuResRepository>& resRepoPtr) const;
93 : // 根据 resType 解析对应的 blocks 指针,用于 QueryRemainRes 降低圈复杂度
94 : HcclResult ResolveBlocksPtr(uint8_t dieId, ResType resType, const std::vector<BlockInfo>*& blocksPtr) const;
95 :
96 : private:
97 : mutable std::mutex innerMutex_;
98 : int32_t devLogicId_{0};
99 : bool initFlag_{false};
100 : std::array<bool, CCU_MAX_IODIE_NUM> dieEnableFlags_{};
101 : std::array<CcuBlockResStrategy, CCU_MAX_IODIE_NUM> resStrategies_{};
102 : std::array<std::unordered_map<ResType, std::vector<BlockInfo>, std::EnumClassHash>, CCU_MAX_IODIE_NUM> resBlocks_{};
103 : // 键值为CcuResRepository裸指针转换的uintptr_t
104 : std::unordered_map<uintptr_t, std::unique_ptr<CcuResRepository>> handleMap_{};
105 : CcuMissionMgr missionMgr_{};
106 : CcuResBlockNums maxResBlockNums_{};
107 : };
108 :
109 : }; // namespace hcomm
110 :
111 : #endif // CCU_RES_BATCH_ALLOCATOR_H
|