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