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 : private:
58 : class CcuMissionMgr {
59 : public:
60 141 : CcuMissionMgr() = default;
61 141 : ~CcuMissionMgr() = default;
62 :
63 : void Reset();
64 : HcclResult PreAlloc(const int32_t devLogicId, const uint32_t blockSize,
65 : const std::array<bool, CCU_MAX_IODIE_NUM> &dieFlags);
66 : HcclResult Alloc(const uintptr_t handleKey, const MissionReq &missionReq,
67 : 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(const uintptr_t handleKey, const CcuResReq& resReq,
85 : std::unique_ptr<CcuResRepository>& resRepoPtr);
86 : HcclResult AllocBlockRes(const uintptr_t handleKey, const CcuResReq& resReq,
87 : std::unique_ptr<CcuResRepository> &resRepoPtr);
88 : HcclResult AllocConsecutiveRes(const CcuResReq& resReq,
89 : std::unique_ptr<CcuResRepository>& resRepoPtr) const;
90 : HcclResult AllocDiscreteRes(const CcuResReq& resReq,
91 : std::unique_ptr<CcuResRepository>& resRepoPtr) const;
92 : HcclResult ReleaseResource(std::unique_ptr<CcuResRepository>& resRepoPtr);
93 : void ReleaseBlockResource(std::unique_ptr<CcuResRepository> &resRepoPtr);
94 : HcclResult ReleaseNonBlockTypeRes(std::unique_ptr<CcuResRepository>& resRepoPtr) const;
95 : // 根据 resType 解析对应的 blocks 指针,用于 QueryRemainRes 降低圈复杂度
96 : HcclResult ResolveBlocksPtr(uint8_t dieId, ResType resType, const std::vector<BlockInfo>*& blocksPtr) const;
97 : private:
98 : mutable std::mutex innerMutex_;
99 : int32_t devLogicId_{0};
100 : bool initFlag_{false};
101 : std::array<bool, CCU_MAX_IODIE_NUM> dieEnableFlags_{};
102 : std::array<CcuBlockResStrategy, CCU_MAX_IODIE_NUM> resStrategies_{};
103 : std::array<std::unordered_map<ResType, std::vector<BlockInfo>, std::EnumClassHash>, CCU_MAX_IODIE_NUM> resBlocks_{};
104 : // 键值为CcuResRepository裸指针转换的uintptr_t
105 : std::unordered_map<uintptr_t, std::unique_ptr<CcuResRepository>> handleMap_{};
106 : CcuMissionMgr missionMgr_{};
107 : CcuResBlockNums maxResBlockNums_{};
108 : };
109 :
110 : }; // namespace hcomm
111 :
112 : #endif // CCU_RES_BATCH_ALLOCATOR_H
|