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_CTX_MGR_IMP_H
12 : #define HCCL_CCU_CTX_MGR_IMP_H
13 :
14 : #include "ccu_ctx_mgr.h"
15 :
16 : #include <mutex>
17 : #include <unordered_map>
18 : #include "ccu_device_manager.h"
19 : #include "ccu_rep_translator.h"
20 :
21 : namespace Hccl {
22 :
23 : using namespace CcuRep;
24 :
25 : class CtxMgrImp {
26 : public:
27 : ~CtxMgrImp();
28 :
29 : CtxMgrImp(const CtxMgrImp &that) = delete;
30 :
31 : CtxMgrImp &operator=(const CtxMgrImp &that) = delete;
32 :
33 : /**
34 : * @brief 获取CtxMgrImp的实例
35 : * @param deviceLogicId 设备逻辑ID
36 : * @return 返回对应deviceLogicId的CtxMgrImp实例
37 : */
38 : static CtxMgrImp &GetInstance(s32 deviceLogicId);
39 :
40 : /**
41 : * @brief 初始化CtxMgrImp的实例
42 : * @param deviceLogicId 设备逻辑ID
43 : * @return void
44 : */
45 : void Init();
46 :
47 : /**
48 : * @brief 解初始化CtxMgrImp的实例
49 : * @param deviceLogicId 设备逻辑ID
50 : * @return void
51 : */
52 : void Deinit();
53 :
54 : /**
55 : * @brief 分配资源
56 : * @param ctxGroup ccuContext上下文组
57 : * @param resPack 资源包
58 : * @return 分配资源的结果
59 : */
60 : HcclResult AllocRes(CcuCtxGroup &ctxGroup, CcuResPack &resPack);
61 :
62 : /**
63 : * @brief 释放资源
64 : * @param ctxGroup 资源句柄
65 : * @return HcclResult 返回释放资源的结果
66 : * @note 释放由CcuCtxGroup handle指定的资源
67 : */
68 : HcclResult ReleaseRes(CcuCtxGroup &ctxGroup) const;
69 :
70 : /**
71 : * @brief 注册CcuCtxGroup
72 : * @param ctxGroup 要注册的CCU上下文组
73 : * @return 返回一个64位无符号整型值,注册成功的标识的ctxGroup映射的id
74 : */
75 : uint64_t Register(CcuCtxGroup &ctxGroup, bool isFuncBlock = false);
76 :
77 : /**
78 : * @brief 注销执行器
79 : *
80 : * @param executorId 执行器ID
81 : * @return HcclResult 注销结果
82 : * @note 此函数用于注销指定ID的执行器
83 : */
84 : HcclResult UnRegister(const uint64_t executorId);
85 :
86 : /**
87 : * 获取任务参数
88 : * @param executorId 任务ID
89 : * @param args CCU参数
90 : * @return 返回任务参数:对外层vector对应多个ctx(mission), 内存vector对应一个ctx下的多个任务参数
91 : */
92 : std::vector<std::vector<CcuTaskParam>> GetTaskParam(CcuTaskArg &ccuTaskArg, const uint64_t executorId);
93 :
94 : /**
95 : * 获取任务Profiling信息
96 : * @param entityId 任务ID
97 : * @return 返回任务Profiling信息:对外层vector对应多个ctx(mission), 内层vector对应一个ctx下的多个任务Profiling信息
98 : */
99 : std::vector<std::vector<CcuProfilingInfo>> GetProfilingInfo(CcuTaskArg &ccuTaskArg, const uint64_t entityId);
100 :
101 : /**
102 : * 获取CcuContext
103 : * @param executorId 任务ID
104 : * @param dieId Die ID
105 : * @param missionId Mission ID
106 : * @return 返回匹配的CcuContext, 未找到则返回nullptr
107 : */
108 : CcuContext* GetCtx(uint64_t executorId, uint32_t dieId, uint32_t missionId);
109 :
110 : private:
111 : explicit CtxMgrImp();
112 :
113 : void CtxInit(CcuCtxGroup &ctxGroup) const;
114 :
115 : HcclResult AllocInstrRes(CcuCtxGroup &ctxGroup) const;
116 : HcclResult ReleaseInstrRes(CcuCtxGroup &ctxGroup) const;
117 :
118 : HcclResult GetResPackTotalResNum(const CcuResPack &resPack, CcuResReq &totalRes) const;
119 :
120 0 : inline int32_t GetResTotalNum(const vector<ResInfo> &resInfos) const
121 : {
122 0 : int32_t resNum = 0;
123 :
124 0 : for (ResInfo resInfo : resInfos) {
125 0 : resNum += static_cast<int32_t>(resInfo.num);
126 : }
127 0 : return resNum;
128 : }
129 :
130 : CcuResReq GetCtxGroupResReq(CcuCtxGroup &ctxGroup) const;
131 :
132 : void MergeCcuResReq(CcuResReq &resReqA, const CcuResReq &resReqB) const;
133 :
134 : HcclResult CompareResAndApplyAsNeeded(const CcuResReq &totalRes, const CcuResReq &resReq, CcuResPack &resPack) const;
135 :
136 66 : inline uint32_t GetReqResNum(uint32_t reqRes, uint32_t totalRes) const
137 : {
138 66 : return ((reqRes > totalRes) ? (reqRes - totalRes) : 0);
139 : }
140 :
141 : void SaveResPackToCtx(CcuCtxGroup &ctxGroup, CcuResPack &resPack) const;
142 :
143 : HcclResult InstantiationTranslator(uint16_t dieId);
144 :
145 : HcclResult TransRepResToPhyRes(CcuCtxGroup &ctxGroup) const;
146 :
147 : HcclResult GetResPackTotalResRepository(const CcuResPack &resPack, CcuResRepository &totalRes) const;
148 :
149 154 : inline void ExpandResInfo(vector<ResInfo> &expendResInfos, const vector<ResInfo> &resInfos) const
150 : {
151 : // 将resInfo中的资源信息扩展到megedRes中
152 250 : for (auto &resInfo : resInfos) {
153 40110 : for (uint32_t id = 0; id < resInfo.num; id++) {
154 40014 : expendResInfos.push_back({(resInfo.startId + id), {1}});
155 : }
156 : }
157 154 : }
158 :
159 : CcuRepResource GetTotalCcuRepResource(CcuCtxGroup &ctxGroup) const;
160 :
161 : void MergeCtxRepResource(CcuRepResource &repResourceA, CcuRepResource &repResourceB) const;
162 :
163 : template <typename T1, typename T2>
164 : void ResetRepResourceTemplate(std::vector<T1> &resource, const std::vector<T2> &repository) const;
165 :
166 : void ResetRepResourceToResRepository(CcuRepResource &totalRepRes, const CcuResRepository &totalResRepository) const;
167 :
168 : template <typename T>
169 : void ProcessSharedResources(std::unordered_map<std::string, T> &resources,
170 : std::vector<std::unordered_map<std::string, T>> &exportedResources, uint32_t i) const;
171 :
172 : void ProcessInterCtxRes(CcuCtxGroup &ctxGroup) const;
173 :
174 : HcclResult SaveCtxMissionInfo(CcuCtxGroup &ctxGroup, array<vector<ResInfo>, MAX_CCU_IODIE_NUM> &missionId) const;
175 :
176 : void TransRepSequenceToMicrocode(CcuCtxGroup &ctxGroup, bool isFuncBlock);
177 :
178 : void LoadInstruction(CcuRep::CcuInstrInfo &instrInfo, uint32_t dieId);
179 :
180 : void DumpResReqInfo(const CcuResReq &totalRes) const;
181 :
182 : void DumpResRepositoryInfo(const CcuResRepository &resRepo) const;
183 :
184 : private:
185 : bool initializedFlag_{false};
186 :
187 : // 创建一个无序的映射,键为uint64_t类型的executorId,值为CcuRepContext的智能指针
188 : std::unordered_map<uint64_t, CcuCtxGroup> ctxGroupMap_{};
189 :
190 : // 创建一个互斥量,用于保护contextMap_的并发访问
191 : std::mutex contextMapMutex_;
192 :
193 : uint64_t executorId_ = 0;
194 :
195 : s32 deviceLogicId_ = -1;
196 :
197 : // 指令模块加载使用的device的内存地址
198 : void *instructionLoadDevMem_ = nullptr;
199 : // translator实例
200 : std::unordered_map<uint16_t, std::unordered_map<uint16_t, std::shared_ptr<CcuRepTranslator>>> translators;
201 : std::unordered_map<uint16_t, std::unordered_map<uint16_t, std::shared_ptr<CcuRepReferenceManager>>> referenceMgrs;
202 : CcuResPack translatorResPack;
203 : };
204 : }; // namespace Hccl
205 :
206 : #endif // HCCL_CCU_CTX_MGR_IMP_H
|