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