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.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 : class CcuResBatchAllocator {
32 : public:
33 : CcuResBatchAllocator(const CcuResBatchAllocator &that) = delete;
34 : CcuResBatchAllocator& operator=(const CcuResBatchAllocator &that) = delete;
35 71 : ~CcuResBatchAllocator() = default; // 不允许在析构中调用CcuComponent,会引起未定义行为
36 :
37 : static CcuResBatchAllocator& GetInstance(const int32_t deviceLogicId);
38 : void Init();
39 : void Deinit();
40 :
41 : HcclResult AllocResHandle(const CcuResReq& resReq, CcuResHandle &resHandle);
42 : HcclResult ReleaseResHandle(const CcuResHandle& handle);
43 : HcclResult GetResource(const CcuResHandle& handle, CcuResRepository &ccuResRepo);
44 : private:
45 : class CcuMissionMgr {
46 : public:
47 71 : CcuMissionMgr() = default;
48 71 : ~CcuMissionMgr() = default;
49 :
50 : void Reset();
51 : HcclResult PreAlloc(const int32_t devLogicId, const uint32_t blockSize,
52 : const std::array<bool, MAX_CCU_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, MAX_CCU_IODIE_NUM> dieEnableFlags;
60 : std::vector<BlockInfo> blocks;
61 : };
62 :
63 : // 键值为CcuResRepository裸指针转换的uintptr_t
64 : std::unordered_map<uintptr_t, std::unique_ptr<CcuResRepository>> handleMap;
65 : CcuMissionMgr missionMgr;
66 :
67 : int32_t devLogicId{0};
68 : std::array<bool, MAX_CCU_IODIE_NUM> dieEnableFlags;
69 : std::array<CcuBlockResStrategy, MAX_CCU_IODIE_NUM> resStrategys;
70 : bool preAllocated{false};
71 : std::array<std::vector<std::vector<BlockInfo>>, MAX_CCU_IODIE_NUM> resBlocks;
72 : std::mutex innerMutex;
73 :
74 71 : explicit CcuResBatchAllocator() = default;
75 :
76 : uint32_t GetPreAllocatedMaxBlockNum(const uint8_t dieId) const;
77 :
78 : HcclResult PreAllocBlockRes();
79 : HcclResult TryAllocResHandle(const uintptr_t handleKey, const CcuResReq& resReq,
80 : std::unique_ptr<CcuResRepository>& resRepoPtr);
81 : HcclResult AllocBlockRes(const uintptr_t handleKey, const CcuResReq& resReq,
82 : std::unique_ptr<CcuResRepository> &resRepoPtr);
83 : HcclResult AllocConsecutiveRes(const CcuResReq& resReq,
84 : std::unique_ptr<CcuResRepository>& resRepoPtr) const;
85 : HcclResult AllocDiscreteRes(const CcuResReq& resReq,
86 : std::unique_ptr<CcuResRepository>& resRepoPtr) const;
87 : HcclResult ReleaseResource(std::unique_ptr<CcuResRepository>& resRepoPtr);
88 : void ReleaseBlockResource(std::unique_ptr<CcuResRepository> &resRepoPtr);
89 : HcclResult ReleaseNonBlockTypeRes(std::unique_ptr<CcuResRepository>& resRepoPtr) const;
90 : };
91 :
92 : }; // namespace Hccl
93 :
94 : #endif
|