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_ALLOCATOR_H
12 : #define CCU_RES_ALLOCATOR_H
13 :
14 : #include <mutex>
15 : #include <vector>
16 : #include <string>
17 : #include <memory>
18 : #include <unordered_map>
19 :
20 : #include "ccu_dev_mgr_imp.h"
21 :
22 : namespace hcomm {
23 :
24 : class CcuResIdAllocator {
25 : public:
26 2040 : explicit CcuResIdAllocator(const uint32_t capacity) : capacity_(capacity) {};
27 : CcuResIdAllocator() = default;
28 :
29 : HcclResult Alloc(
30 : const uint32_t num, const bool consecutive, std::vector<ResInfo>& allocatedResInfos,
31 : const std::string& dfxInfo = "");
32 : HcclResult Release(const uint32_t startId, const uint32_t num);
33 : uint32_t GetConsecutiveRemainSize() const;
34 :
35 : std::string Describe() const;
36 :
37 : private:
38 : uint32_t capacity_{0};
39 : uint32_t allocatedSize_{0};
40 : std::vector<ResInfo> resInfos_{};
41 : std::mutex innerMutex_;
42 :
43 : size_t FindReleaseResIndex(const uint32_t startId) const;
44 : void AllocResInfo(std::vector<ResInfo> newResInfos);
45 : void ReleaseResInfo(const size_t resIndex, const uint32_t startId, const uint32_t num);
46 : };
47 :
48 : class CcuResAllocator {
49 : public:
50 210 : CcuResAllocator(const int32_t devLogicId, const uint8_t dieId) : devLogicId_(devLogicId), dieId_(dieId) {};
51 : CcuResAllocator() = default;
52 :
53 : HcclResult Init();
54 : HcclResult Alloc(const ResType resType, const uint32_t num, const bool consecutive, std::vector<ResInfo>& resInfos);
55 : HcclResult Release(const ResType resType, const uint32_t startId, const uint32_t num);
56 : uint32_t GetConsecutiveRemainSize(const ResType resType) const;
57 :
58 : // 0.5rtt专用
59 : HcclResult AllocCountXn(const uint32_t num, ResInfo& resInfo);
60 : HcclResult ReleaseCountXn(const uint32_t startId, const uint32_t num);
61 :
62 : std::string Describe() const;
63 :
64 : private:
65 : int32_t devLogicId_{0};
66 : uint8_t dieId_{0};
67 :
68 : // ccu v2新增countXn管理
69 : uint32_t xnSpecNum_{0};
70 : uint32_t countXnSpecNum_{0};
71 : std::unordered_map<uint8_t, std::unique_ptr<CcuResIdAllocator>> idAllocatorMap_;
72 : };
73 :
74 : } // namespace hcomm
75 :
76 : #endif // CCU_RES_ALLOCATOR_H
|