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_generater_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(CcuResPack &resPack, const char *kernelFuncName,
37 : const void *kernelFunc, const void **kernelArgs, const uint32_t argNum,
38 : CcuKernelHandle &kernelHandle);
39 :
40 : CcuResult Translate(const std::vector<CcuKernelHandle> &kernelHandles);
41 :
42 : CcuKernel *GetKernel(CcuKernelHandle kernelHandle);
43 : CcuResult UnRegister(CcuKernelHandle kernelHandle);
44 :
45 : CcuKernel *GetCurrentKernel();
46 :
47 : private:
48 198 : explicit CcuKernelMgr() = default;
49 : ~CcuKernelMgr();
50 :
51 : CcuKernelMgr(const CcuKernelMgr &that) = delete;
52 : CcuKernelMgr &operator=(const CcuKernelMgr &that) = delete;
53 :
54 : private:
55 : struct CcuTranslatResPack {
56 : std::vector<CcuResHandle> handles{};
57 : };
58 :
59 : private:
60 : CcuResult PrepareConstValueResources();
61 : CcuResult AllocRes(CcuResPack &resPack);
62 :
63 : HcclResult InstantiationTranslator(const uint16_t dieId);
64 : HcclResult TransRepSequenceToMicrocode(const std::vector<CcuKernel *> &kernels,
65 : bool isFuncBlock);
66 : HcclResult LoadInstruction(const CcuRep::CcuInstrInfo &instrInfo, const uint32_t dieId);
67 :
68 : HcclResult GetResPackTotalResRepository(const CcuTranslatResPack &resPack,
69 : CcuResRepository &totalRes) const;
70 :
71 : private:
72 : bool initializedFlag_{false};
73 : int32_t devLogicId_{-1};
74 : std::mutex kernelMapMutex_{};
75 : std::mutex translateMutex_{};
76 : CcuKernelHandle kernelId_ = 0;
77 : std::unordered_map<CcuKernelHandle, std::unique_ptr<CcuKernel>> kernelMap_{};
78 : void *instructionLoadDevMem_{nullptr};
79 :
80 : std::unordered_map<uint16_t, std::unordered_map<uint16_t, std::shared_ptr<CcuRep::CcuRepTranslator>>> translators;
81 : std::unordered_map<uint16_t, std::unordered_map<uint16_t, std::shared_ptr<CcuRep::CcuRepReferenceManager>>> referenceMgrs;
82 : CcuTranslatResPack translatorResPack{};
83 : std::unique_ptr<CcuKernel> currKernel_{nullptr};
84 : std::shared_ptr<CcuInsGeneraterBase> insGenePtr;
85 : CcuVersion ccuVersion_{CcuVersion::CCU_INVALID};
86 : };
87 : }; // namespace hcomm
88 : #endif // HCOMM_CCU_KERNEL_MGR_IMP_H
|