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 :
24 : namespace hcomm {
25 :
26 : struct BlockInfo {
27 : uint32_t id{0};
28 : uint32_t startId{0};
29 : uint32_t num{0};
30 : uintptr_t handle{0};
31 : bool allocated{false};
32 : };
33 :
34 : class CcuResBatchAllocator {
35 : public:
36 : static CcuResBatchAllocator& GetInstance(const int32_t deviceLogicId);
37 : HcclResult Init();
38 : HcclResult Deinit();
39 :
40 : HcclResult AllocResHandle(const CcuResReq& resReq, CcuResHandle &resHandle);
41 : HcclResult ReleaseResHandle(const CcuResHandle& handle);
42 : HcclResult GetResource(const CcuResHandle& handle, CcuResRepository &ccuResRepo);
43 :
44 : private:
45 : class CcuMissionMgr {
46 : public:
47 134 : CcuMissionMgr() = default;
48 134 : ~CcuMissionMgr() = default;
49 :
50 : void Reset();
51 : HcclResult PreAlloc(const int32_t devLogicId, const uint32_t blockSize,
52 : const std::array<bool, CCU_MAX_IODIE_NUM> &dieFlags);
53 : HcclResult Alloc(const uintptr_t handleKey, const MissionReq &missionReq,
54 : MissionResInfo &missionInfos);
55 : void Release(MissionResInfo &missionInfos);
56 :
57 : private:
58 : uint32_t stragtegy_{0};
59 : std::array<bool, CCU_MAX_IODIE_NUM> dieEnableFlags_;
60 : std::vector<BlockInfo> blocks_;
61 : };
62 :
63 : private:
64 536 : explicit CcuResBatchAllocator() = default;
65 : CcuResBatchAllocator(const CcuResBatchAllocator &that) = delete;
66 : CcuResBatchAllocator& operator=(const CcuResBatchAllocator &that) = delete;
67 134 : ~CcuResBatchAllocator() = default; // 不允许在析构中调用CcuComponent,会引起未定义行为
68 :
69 : HcclResult PreAllocBlockRes();
70 : HcclResult TryAllocResHandle(const uintptr_t handleKey, const CcuResReq& resReq,
71 : std::unique_ptr<CcuResRepository>& resRepoPtr);
72 : HcclResult AllocBlockRes(const uintptr_t handleKey, const CcuResReq& resReq,
73 : std::unique_ptr<CcuResRepository> &resRepoPtr);
74 : HcclResult AllocConsecutiveRes(const CcuResReq& resReq,
75 : std::unique_ptr<CcuResRepository>& resRepoPtr) const;
76 : HcclResult AllocDiscreteRes(const CcuResReq& resReq,
77 : std::unique_ptr<CcuResRepository>& resRepoPtr) const;
78 : HcclResult ReleaseResource(std::unique_ptr<CcuResRepository>& resRepoPtr);
79 : void ReleaseBlockResource(std::unique_ptr<CcuResRepository> &resRepoPtr);
80 : HcclResult ReleaseNonBlockTypeRes(std::unique_ptr<CcuResRepository>& resRepoPtr) const;
81 :
82 : private:
83 : std::mutex innerMutex_;
84 : int32_t devLogicId_{0};
85 : bool initFlag_{false};
86 : std::array<bool, CCU_MAX_IODIE_NUM> dieEnableFlags_{};
87 : std::array<CcuBlockResStrategy, CCU_MAX_IODIE_NUM> resStrategys_{};
88 : std::array<std::vector<std::vector<BlockInfo>>, CCU_MAX_IODIE_NUM> resBlocks_{};
89 : // 键值为CcuResRepository裸指针转换的uintptr_t
90 : std::unordered_map<uintptr_t, std::unique_ptr<CcuResRepository>> handleMap_{};
91 : CcuMissionMgr missionMgr_{};
92 : };
93 :
94 : }; // namespace hcomm
95 :
96 : #endif // CCU_RES_BATCH_ALLOCATOR_H
|