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 HCOMM_CCU_KERNEL_MGR_H
12 : #define HCOMM_CCU_KERNEL_MGR_H
13 :
14 : #include <mutex>
15 : #include <unordered_map>
16 :
17 : #include "ccu_kernel.h"
18 : #include "../ccu_instance/ccu_res_pack.h"
19 : #include "../ccu_instance/ccu_instance.h"
20 :
21 : #include "ccu_dev_mgr_imp.h"
22 : #include "../ccu_representation/reps/translator/ccu_rep_translator_v1.h"
23 :
24 : #include "ccu_ins_generator_base.h"
25 :
26 : namespace hcomm {
27 :
28 : using namespace CcuRep;
29 :
30 : class CcuKernelMgr {
31 : public:
32 : static CcuKernelMgr& GetInstance(const s32 deviceLogicId);
33 :
34 : HcclResult Init();
35 : HcclResult Deinit();
36 :
37 : CcuResult Register(
38 : CcuResPack& resPack, const uint32_t dieId, const char* kernelFuncName, const void* kernelFunc,
39 : const void** kernelArgs, const uint32_t argNum, CcuInstance* ccuIns, CcuKernelHandle& kernelHandle);
40 :
41 : CcuResult GetKernelResourceRequest(
42 : const uint32_t dieId, const char* kernelFuncName, const void* kernelFunc, const void** kernelArgs,
43 : const uint32_t argNum, CcuResReq& resReq, uint32_t& instrCount);
44 :
45 : CcuResult Translate(const std::vector<CcuKernelHandle>& kernelHandles);
46 :
47 : CcuKernel* GetKernel(CcuKernelHandle kernelHandle);
48 : // 在锁内查找 kernel 并填充 CcuKernelInfo,裸指针不逃逸锁。
49 : CcuResult GetCcuKernelInfo(CcuKernelHandle kernelHandle, CcuKernelInfo& info);
50 : CcuResult UnRegister(CcuKernelHandle kernelHandle);
51 :
52 : CcuKernel* GetCurrentKernel();
53 :
54 : private:
55 198 : explicit CcuKernelMgr() = default;
56 : ~CcuKernelMgr();
57 :
58 : CcuKernelMgr(const CcuKernelMgr& that) = delete;
59 : CcuKernelMgr& operator=(const CcuKernelMgr& that) = delete;
60 :
61 : private:
62 : struct CcuTranslatResPack {
63 : std::vector<CcuResHandle> handles{};
64 : };
65 :
66 : private:
67 : CcuResult BuildKernel(
68 : uint32_t dieId, const char* kernelFuncName, const void* kernelFunc, const void** kernelArgs, uint32_t argNum,
69 : CcuInstance* ccuIns);
70 : CcuResult PrepareConstValueResources();
71 : CcuResult AllocRes(CcuResPack& resPack);
72 :
73 : HcclResult InstantiationTranslator(const uint16_t dieId);
74 : HcclResult TransRepSequenceToMicrocode(const std::vector<CcuKernel*>& kernels, bool isFuncBlock);
75 : HcclResult LoadInstruction(const CcuRep::CcuInstrInfo& instrInfo, const uint32_t dieId);
76 :
77 : HcclResult GetResPackTotalResRepository(const CcuTranslatResPack& resPack, CcuResRepository& totalRes) const;
78 :
79 : private:
80 : bool initializedFlag_{false};
81 : int32_t devLogicId_{-1};
82 : std::mutex kernelMapMutex_{};
83 : std::mutex translateMutex_{};
84 : CcuKernelHandle kernelId_ = 0;
85 : std::unordered_map<CcuKernelHandle, std::unique_ptr<CcuKernel>> kernelMap_{};
86 : void* instructionLoadDevMem_{nullptr};
87 :
88 : std::unordered_map<uint16_t, std::unordered_map<uint16_t, std::shared_ptr<CcuRep::CcuRepTranslator>>> translators;
89 : std::unordered_map<uint16_t, std::unordered_map<uint16_t, std::shared_ptr<CcuRep::CcuRepReferenceManager>>>
90 : referenceMgrs;
91 : CcuTranslatResPack translatorResPack{};
92 : std::unique_ptr<CcuKernel> currKernel_{nullptr};
93 : std::shared_ptr<CcuInsGeneratorBase> insGenePtr;
94 : CcuVersion ccuVersion_{CcuVersion::CCU_INVALID};
95 : };
96 : }; // namespace hcomm
97 : #endif // HCOMM_CCU_KERNEL_MGR_IMP_H
|