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_H
12 : #define HCOMM_CCU_KERNEL_H
13 :
14 : #include <cstdint>
15 : #include <functional>
16 : #include <array>
17 : #include <memory>
18 : #include <string>
19 : #include <vector>
20 : #include <unordered_map>
21 : #include <unordered_set>
22 :
23 : #include "ccu_task_arg_v1.h"
24 : #include "ccu_task_param_v1.h"
25 :
26 : #include "ccu_kernel_resource.h"
27 : #include "ccu_instr_info_v1.h"
28 : #include "ccu_rep_context_v1.h"
29 :
30 : #include "ccu_funccall_v1.h"
31 : #include "ccu_loopcall_v1.h"
32 :
33 : #include "hccl_types.h"
34 : #include "hcomm_primitives.h"
35 : #include "ccu_datatype_v1.h"
36 : #include "ccu_interface_assist_v1.h"
37 :
38 : #include "ccu_res_repo.h"
39 : #include "ccu_dev_mgr_imp.h" // CcuVersion
40 :
41 : // 暂时引用方便算法开发
42 : #include "ccu_repeat_v1.h"
43 : #include "ccu_condition_v1.h"
44 : #include "ccu_rep_funcblock_v1.h"
45 : #include "ccu_loopblock_v1.h"
46 : #include "ccu_loopcall_v1.h"
47 : #include "ccu_assist_pub.h"
48 :
49 : #ifndef CCU_PROFILING // 和hccl仓兼容性使用
50 : #define CCU_PROFILING
51 : #endif
52 : #include "ccu_types.h"
53 :
54 : namespace hcomm {
55 :
56 : struct GroupInfo {
57 : uint16_t loopParamId;
58 : uint16_t parallelParamId;
59 : uint16_t residualId;
60 : };
61 :
62 : struct GroupOpConfig {
63 : uint32_t msInterleave;
64 : uint32_t loopCount;
65 : uint64_t memSlice;
66 : };
67 :
68 : class CcuKernel : public CcuRep::CcuRepContext {
69 : public:
70 3150 : CcuKernel() = default;
71 : ~CcuKernel() override;
72 :
73 : HcclResult SetupProfilingInfo(const char *kernelFuncName);
74 : HcclResult ApplyDieFromChannels();
75 : HcclResult ValidateAndApplyDie(uint32_t targetDieId);
76 :
77 : CcuResReq GetResourceRequest();
78 : CcuResRepository &GetResRepository();
79 : CcuRepResource &GetResource();
80 : CcuSharedResource &GetExportedRes();
81 : CcuSharedResource &GetImportedRes();
82 :
83 : void SetResRepository(const CcuResRepository &resRepo);
84 : void SetInstrId(uint32_t instrId);
85 : uint32_t GetInstrId() const;
86 : uint32_t GetInstrCount();
87 : void SetCcuInstrInfo(const CcuRep::CcuInstrInfo &instrInfo);
88 :
89 : CcuResult GeneTaskParams(const uint64_t *taskArgs, uint32_t argsNum,
90 : std::vector<CcuTaskParam> &taskParams);
91 :
92 : void SetInsGenerater(CcuRep::CcuInsGeneratorBase* insGeneratorBase);
93 59 : void SetCcuVersion(CcuVersion version) { ccuVersion_ = version; }
94 : // 该友元函数用于在context类外创建Variable并被context内的资源管理器管理
95 : friend CcuRep::Variable CcuRep::CreateVariable(CcuRep::CcuRepContext *context);
96 :
97 : HcclResult AddProfilingInfo(const ChannelHandle *channels, uint32_t channelNum, HcclDataType dataType,
98 : HcclDataType outputDataType, HcclReduceOp opType, const std::string& opName);
99 :
100 : HcclResult AddCcuProfiling(GroupInfo groupInfo, const std::vector<ChannelHandle> channelHandle, HcclDataType dataType,
101 : HcclDataType outputDataType, HcclReduceOp opType, const std::string& opName);
102 : HcclResult AddCcuProfiling(const ChannelHandle *channels, uint32_t channelNum, HcclDataType dataType,
103 : HcclDataType outputDataType, HcclReduceOp opType, const std::string& opName);
104 : HcclResult GetCcuProfilingInfo(const uint64_t *taskArgs, uint32_t argSize,
105 : std::vector<CcuProfilingInfo> &allCcuProfilingInfo);
106 :
107 : const std::vector<CcuProfilingInfo> &GetAllCcuProfilingInfo() { return allCcuProfilingInfos_; };
108 :
109 : // process const values
110 114 : std::unordered_map<uint64_t, CcuRep::Variable>& GetConstValue2VarMap() { return constValue2VarMap; }
111 : HcclResult Add2ConstValue2VarMap(std::vector<uint64_t> &values);
112 :
113 8 : const std::unordered_set<ChannelHandle> &GetChannels() { return channels_; }
114 :
115 : public:
116 :
117 : //Alloc 相关接口
118 : CcuResult VariableAlloc(CcuVariableHandle *varHandle);
119 : CcuResult AddressAlloc(CcuAddressHandle *addrHandle);
120 : CcuResult EventAlloc(CcuEventHandle *eventHandle);
121 : CcuResult BufferAlloc(CcuBufferHandle *bufHandle);
122 : CcuResult LocalAddrAlloc(CcuLocalAddrHandle *localAddrHandle, CcuAddressHandle *addrHandle, CcuVariableHandle *tokenHandle);
123 : CcuResult RemoteAddrAlloc(CcuRemoteAddrHandle *remoteAddrHandle, CcuAddressHandle *addrHandle, CcuVariableHandle *tokenHandle);
124 : CcuResult BlockVariableAlloc(CcuVariableHandle *varHandles, uint32_t count);
125 : CcuResult BlockEventAlloc(CcuEventHandle *eventHandles, uint32_t count);
126 : CcuResult BlockBufferAlloc(CcuBufferHandle *bufHandles, uint32_t count);
127 : CcuResult VariableCreateByChannel(ChannelHandle channel, uint32_t varIndex, CcuVariableHandle *varHandle);
128 :
129 : //参数加载类 相关接口
130 : CcuResult LoadArg(CcuVariableHandle varHandle, uint32_t argId);
131 : CcuResult LoadVar(uint64_t addr, CcuVariableHandle varHandle, uint32_t num);
132 : CcuResult CcuLoadVarFromVarAddr(CcuVariableHandle addrHandle, CcuVariableHandle varHandle, uint32_t num);
133 : CcuResult StoreVar(uint64_t addr, CcuVariableHandle varHandle, uint32_t num);
134 : CcuResult CcuStoreVarToVarAddr(CcuVariableHandle addrHandle, CcuVariableHandle varHandle, uint32_t num);
135 :
136 : //Event信号同步类 相关接口
137 : // mask 由调用方独立传入(与 Event 句柄解耦),不再设独立的 SetEventMask 接口。
138 : CcuResult EventRecord(CcuEventHandle eventHandle, uint32_t mask);
139 : CcuResult EventWait(CcuEventHandle eventHandle, uint32_t mask);
140 : CcuResult NotifyRecord(const ChannelHandle channel, uint32_t remoteNotifyIdx, uint32_t mask);
141 : CcuResult NotifyWait(const ChannelHandle channel, uint32_t localNotifyIdx, uint32_t mask);
142 : CcuResult WriteVariableWithNotify(const ChannelHandle channel, CcuVariableHandle varHandle,uint32_t remoteVarIdx, uint32_t remoteNotifyIdx, uint32_t mask);
143 : // 本地(同 device 内跨 core)通知同步:用 notifyTag 字符串作为对端标识,
144 : // 由调用方约定生产者/消费者使用相同的 tag 字符串完成配对。
145 : // 与 NotifyRecord/Wait(用 ChannelHandle 标识跨 rank 通道)的对偶。
146 : // 必须 public:C API ccu_primitives_impl.cc 直接调用。
147 : CcuResult LocalNotifyRecord(const char *notifyTag, const uint32_t mask);
148 : CcuResult LocalNotifyWait(const char *notifyTag, const uint32_t mask);
149 : //本地数据拷贝 相关接口
150 : CcuResult LocalCopyMemToBuffer(CcuBufferHandle dstHandle, CcuLocalAddrHandle srcHandle,CcuVariableHandle lenHandle, CcuEventHandle eventHandle, uint32_t mask);
151 : CcuResult LocalCopyBufferToMem(CcuLocalAddrHandle dstHandle, CcuBufferHandle srcHandle,CcuVariableHandle lenHandle, CcuEventHandle eventHandle, uint32_t mask);
152 : CcuResult LocalCopyMemToMem(CcuLocalAddrHandle dstHandle, CcuLocalAddrHandle srcHandle,CcuVariableHandle lenHandle, CcuEventHandle eventHandle, uint32_t mask);
153 :
154 : //本地reduce 相关接口
155 : CcuResult LocalMemReduce(CcuLocalAddrHandle dstHandle, CcuLocalAddrHandle srcHandle, CcuVariableHandle lenHandle, HcclDataType dataType, HcclReduceOp opType, CcuEventHandle eventHandle, uint32_t mask);
156 : CcuResult LocalBufferReduce(CcuBufferHandle* bufHandles, uint32_t count, HcclDataType dataType, HcclDataType outputDataType, HcclReduceOp opType, CcuVariableHandle lenHandle, CcuEventHandle eventHandle, uint32_t mask);
157 :
158 : //运算重载 相关接口
159 : CcuResult VariableAssignImm(CcuVariableHandle var, uint64_t immediate);
160 : CcuResult VariableAssignVar(CcuVariableHandle var, CcuVariableHandle varA);
161 : CcuResult VariableAddVarToVar(CcuVariableHandle varHandle,CcuVariableHandle varAHandle, CcuVariableHandle varBHandle);
162 : CcuResult VariableSubVarToVar(CcuVariableHandle varHandle,CcuVariableHandle varAHandle, CcuVariableHandle varBHandle);
163 : CcuResult VariableMulVarToVar(CcuVariableHandle varHandle,CcuVariableHandle varAHandle, CcuVariableHandle varBHandle);
164 : CcuResult VariableAddImmToVar(CcuVariableHandle varHandle,CcuVariableHandle varAHandle, uint16_t immediate);
165 : CcuResult VariableSubImmToVar(CcuVariableHandle varHandle,CcuVariableHandle varAHandle, uint16_t immediate);
166 : CcuResult VariableMulImmToVar(CcuVariableHandle varHandle,CcuVariableHandle varAHandle, uint16_t immediate);
167 : CcuResult VariableAndVarToVar(CcuVariableHandle varHandle,CcuVariableHandle varAHandle, CcuVariableHandle varBHandle);
168 : CcuResult VariableOrVarToVar(CcuVariableHandle varHandle,CcuVariableHandle varAHandle, CcuVariableHandle varBHandle);
169 : CcuResult VariableXorVarToVar(CcuVariableHandle varHandle,CcuVariableHandle varAHandle, CcuVariableHandle varBHandle);
170 : CcuResult VariableNotVar(CcuVariableHandle varHandle,CcuVariableHandle varAHandle);
171 : CcuResult AddressAssignImm(CcuAddressHandle addr, uint64_t immediate);
172 : CcuResult AddressAssignVar(CcuAddressHandle addrHandle, CcuVariableHandle varHandle);
173 : CcuResult AddressAssignAddr(CcuAddressHandle dstAddrHandle, CcuAddressHandle srcAddrHandle);
174 : CcuResult AddressAddVarToAddr(CcuAddressHandle resAddr, CcuAddressHandle lhsAddr, CcuVariableHandle rhsVar);
175 : CcuResult AddressAddAddrToAddr(CcuAddressHandle resAddrHandle, CcuAddressHandle addrAHandle, CcuAddressHandle addrBHandle);
176 : CcuResult AddressAddAssignVar(CcuAddressHandle addr, CcuVariableHandle var);
177 : CcuResult AddressAddAssignAddr(CcuAddressHandle addrHandle, CcuAddressHandle otherHandle);
178 : CcuResult AddressAddImmToAddr(CcuAddressHandle resAddr, CcuAddressHandle addrA, uint16_t imm);
179 :
180 : // 远端数据传输操作
181 :
182 : CcuResult ReadMemToMem(ChannelHandle channel, CcuLocalAddrHandle localHandle, CcuRemoteAddrHandle remoteHandle, CcuVariableHandle lenHandle, CcuEventHandle eventHandle, uint32_t mask);
183 : CcuResult ReadMemToBuffer(ChannelHandle channel, CcuBufferHandle localHandle, CcuRemoteAddrHandle remoteHandle, CcuVariableHandle lenHandle, CcuEventHandle eventHandle, uint32_t mask);
184 : CcuResult ReadMemToMemReduce(ChannelHandle channel, CcuLocalAddrHandle localHandle, CcuRemoteAddrHandle remoteHandle, CcuVariableHandle lenHandle, HcclDataType dataType, HcclReduceOp opType, CcuEventHandle eventHandle, uint32_t mask);
185 : CcuResult WriteMemToMem(ChannelHandle channel, CcuRemoteAddrHandle remoteHandle, CcuLocalAddrHandle localHandle, CcuVariableHandle lenHandle, CcuEventHandle eventHandle, uint32_t mask);
186 : CcuResult WriteBufferToMem(ChannelHandle channel, CcuRemoteAddrHandle remoteHandle, CcuBufferHandle localHandle, CcuVariableHandle lenHandle, CcuEventHandle eventHandle, uint32_t mask);
187 : CcuResult WriteMemToMemReduce(ChannelHandle channel, CcuRemoteAddrHandle remoteHandle, CcuLocalAddrHandle localHandle, CcuVariableHandle lenHandle, HcclDataType dataType, HcclReduceOp opType, CcuEventHandle eventHandle, uint32_t mask);
188 :
189 :
190 : CcuResult IfBegin(CcuVariableHandle varHandle, uint64_t immediate,
191 : CcuConditionType condType, const char *label);
192 : CcuResult IfBeginVar(CcuVariableHandle lhsHandle, CcuVariableHandle rhsHandle,
193 : CcuConditionType condType, const char *label);
194 : CcuResult IfElse(const char *label);
195 : CcuResult IfEnd(const char *label);
196 :
197 : void IfLabelStackPush(const char *label);
198 : void IfLabelStackMarkBodyDone();
199 : const char *IfLabelStackPopForElse();
200 : bool IfLabelStackTopIsClosable();
201 : const char *IfLabelStackPop();
202 :
203 : void DoWhileLabelStackPush(const char *label);
204 : const char *DoWhileLabelStackPopForWhile();
205 :
206 : void FlushClosablePendingIfs();
207 : void Append(std::shared_ptr<CcuRep::CcuRepBase> rep) override;
208 : CcuResult WhileBegin(CcuVariableHandle varHandle, uint64_t immediate,
209 : CcuConditionType condType, const char *label);
210 : CcuResult WhileBeginVar(CcuVariableHandle lhsHandle, CcuVariableHandle rhsHandle,
211 : CcuConditionType condType, const char *label);
212 : CcuResult WhileEnd(const char *label);
213 : CcuResult DoWhileBegin(const char *label);
214 : CcuResult DoWhileEnd(CcuVariableHandle varHandle, uint64_t immediate,
215 : CcuConditionType condType, const char *label);
216 : CcuResult DoWhileEndVar(CcuVariableHandle lhsHandle, CcuVariableHandle rhsHandle,
217 : CcuConditionType condType, const char *label);
218 :
219 : CcuResult LoopCreate(CcuLoop *loop);
220 : CcuResult LoopBodyEnter(CcuLoop loop);
221 : CcuResult LoopBodyExit(CcuLoop loop);
222 : // 建组/加环统一以版本化 cfg 为准;旧 config 的转换在 C ABI 适配层完成。
223 : CcuResult LoopGroupCreate(CcuLoopGroup *group, uint32_t maxLoopNum,
224 : const CcuLoopGroupCfg *cfg);
225 : CcuResult LoopGroupCreateFromVar(CcuLoopGroup *group, uint32_t maxLoopNum,
226 : CcuVariableHandle parallelVarHandle, CcuVariableHandle offsetVarHandle);
227 : CcuResult LoopGroupCreateFromVarV2(CcuLoopGroup *group, uint32_t maxLoopNum,
228 : CcuVariableHandle parallelVarV2, CcuVariableHandle offsetVarV2, CcuVariableHandle varOffsetVar);
229 : CcuResult LoopGroupAddLoop(CcuLoopGroup group,
230 : CcuLoop loop, const CcuLoopCfg *cfg);
231 : CcuResult LoopGroupAddLoopFromVar(CcuLoopGroup group,
232 : CcuLoop loop, CcuVariableHandle loopParamVar);
233 : CcuResult LoopGroupAddLoopFromVarV2(CcuLoopGroup group,
234 : CcuLoop loop, CcuVariableHandle iterNumVar, CcuVariableHandle addrOffsetVar, CcuVariableHandle ctxIdVar);
235 :
236 : CcuResult FuncBlockLookup(const void *funcPtr, uint64_t *outHandle);
237 : CcuResult FuncBlockBegin(const void *funcPtr, uint64_t *outHandle);
238 : CcuResult FuncBlockEnd(uint64_t handle);
239 : CcuResult FuncDefineInArg(uint64_t handle, CcuVariableHandle formal);
240 : CcuResult FuncCall(uint64_t handle, const CcuVariableHandle *inArgs, uint32_t numIn);
241 :
242 :
243 : private:
244 : CcuResult GetVariableByHandle(CcuVariableHandle varHandle, CcuRep::Variable **variable);
245 : CcuResult GetEventByHandle(CcuEventHandle eventHandle, CcuRep::CompletedEvent **event);
246 : CcuResult LatchBodyError(CcuResult err);
247 : // 按需扩容 res_.blockExecutor[0]:不足 maxLoopNum 时补足,足够则不动;
248 : // 由 LoopGroupCreate / LoopGroupCreateFromVar 在 LoopGroup 创建时调用。
249 : CcuResult EnsureLoopEnginePool(uint32_t maxLoopNum);
250 :
251 : CcuResult ValidateTaskArgs(const uint64_t *taskArgs, uint32_t argsNum) const;
252 : void FillTaskParam(CcuTaskParam ¶m, uint32_t index, uint32_t seqNum,
253 : const uint64_t *taskArgs, uint32_t argsNum) const;
254 :
255 : CcuResult ResolveBufRemoteLenEvent(CcuBufferHandle bufHandle, CcuRemoteAddrHandle remoteHandle,
256 : CcuVariableHandle lenHandle, CcuEventHandle eventHandle,
257 : CcuRep::CcuBuf **buf, CcuRep::RemoteAddr **remote,
258 : CcuRep::Variable **len, CcuRep::CompletedEvent **event);
259 :
260 : CcuResult ResolveLocalRemoteLenEvent(CcuLocalAddrHandle localHandle, CcuRemoteAddrHandle remoteHandle,
261 : CcuVariableHandle lenHandle, CcuEventHandle eventHandle,
262 : CcuRep::LocalAddr **local, CcuRep::RemoteAddr **remote,
263 : CcuRep::Variable **len, CcuRep::CompletedEvent **event);
264 :
265 : CcuResult ResolveRemoteLocalLenEvent(CcuRemoteAddrHandle remoteHandle, CcuLocalAddrHandle localHandle,
266 : CcuVariableHandle lenHandle, CcuEventHandle eventHandle,
267 : CcuRep::RemoteAddr **remote, CcuRep::LocalAddr **local,
268 : CcuRep::Variable **len, CcuRep::CompletedEvent **event);
269 :
270 : // 校验从 varHandle 起的 num 个 Variable 句柄对应的内部变量 Id 连续递增,
271 : // 用于 LoadVar/StoreVar 等接口对“连续变量块”的前置校验。
272 : CcuResult CheckContinuousVariables(CcuVariableHandle varHandle, uint32_t num,
273 : const CcuRep::Variable &baseVar, const char *tag);
274 :
275 : // GetCcuProfilingInfo 的子步骤:处理 sqe & waitcke 类型的 profiling 信息,结果直接 push 到 allCcuProfilingInfos_ 中。
276 : HcclResult CollectSqeAndWaitCkeProfilingInfo();
277 : // GetCcuProfilingInfo 的子步骤:根据 LoopGroup 的 profiling 缓存构建
278 : // varId -> argIndex 与 varId -> varId 两个查找表,供 LoopGroup 段查询入参使用。
279 : HcclResult BuildLoopGroupVarIdMaps(std::unordered_map<uint16_t, uint32_t> &varId2ArgIndexMap,
280 : std::unordered_map<uint16_t, uint16_t> &varId2VarIdMap);
281 : // GetCcuProfilingInfo 的子步骤:处理 LoopGroup 的 profiling 信息,结果 push 到 allCcuProfilingInfos_ 中。
282 : HcclResult CollectLoopGroupProfilingInfo(const uint64_t *taskArgs, uint32_t argSize,
283 : const std::unordered_map<uint16_t, uint32_t> &varId2ArgIndexMap,
284 : const std::unordered_map<uint16_t, uint16_t> &varId2VarIdMap);
285 :
286 : struct IfLabelEntry {
287 : const char *label{nullptr};
288 : bool bodyDone{false};
289 : };
290 : struct DoWhileLabelEntry {
291 : const char *label{nullptr};
292 : std::shared_ptr<CcuRep::CcuRepBlock> snapshotBlock{nullptr};
293 : size_t snapshotRepCount{0};
294 : };
295 : std::vector<IfLabelEntry> iflabelStack_;
296 : std::vector<DoWhileLabelEntry> doWhileLabelStack_;
297 : bool isFlushing_ = false;
298 :
299 :
300 : struct PendingIfContext {
301 : std::shared_ptr<CcuRep::CcuRepJumpLabel> elseLabel;
302 : std::shared_ptr<CcuRep::CcuRepJumpLabel> endLabel;
303 : bool hasElse{false};
304 : };
305 :
306 : struct PendingWhileContext {
307 : std::shared_ptr<CcuRep::CcuRepJumpLabel> beginLabel;
308 : std::shared_ptr<CcuRep::CcuRepJumpLabel> endLabel;
309 : CcuVariableHandle varHandle;
310 : uint64_t immediate;
311 : CcuConditionType condType;
312 : };
313 :
314 : struct PendingDoWhileContext {
315 : std::shared_ptr<CcuRep::CcuRepJumpLabel> beginLabel;
316 : };
317 :
318 : std::unordered_map<CcuVariableHandle, CcuRep::Variable> ccuVarMap_{};
319 :
320 : std::unordered_map<std::string, PendingIfContext> pendingIfCtx_{};
321 : std::unordered_map<std::string, PendingWhileContext> pendingWhileCtx_{};
322 : std::unordered_map<std::string, PendingDoWhileContext> pendingDoWhileCtx_{};
323 :
324 : std::unordered_map<CcuEventHandle, CcuRep::CompletedEvent> ccuEventMap_{};
325 :
326 : CcuResult GetBufferByHandle(CcuBufferHandle bufferHandle, CcuRep::CcuBuf **buffer);
327 : std::unordered_map<CcuBufferHandle, CcuRep::CcuBuf> ccuBufferMap_{};
328 :
329 : CcuResult GetAddressByHandle(CcuAddressHandle addrHandle, CcuRep::Address **address);
330 : std::unordered_map<CcuAddressHandle, CcuRep::Address> ccuAddrMap_{};
331 :
332 : CcuResult GetLocalAddrByHandle(CcuLocalAddrHandle handle, CcuRep::LocalAddr **localAddr);
333 : std::unordered_map<CcuLocalAddrHandle, CcuRep::LocalAddr> ccuLocalAddrMap_{};
334 :
335 : CcuResult GetRemoteAddrByHandle(CcuRemoteAddrHandle handle, CcuRep::RemoteAddr **remoteAddr);
336 : std::unordered_map<CcuRemoteAddrHandle, CcuRep::RemoteAddr> ccuRemoteAddrMap_{};
337 :
338 : std::unordered_set<uint32_t> loadArgUsedSet_{};
339 :
340 : protected:
341 : // 使用channel中的Variable
342 : HcclResult CreateVariable(const ChannelHandle channel, uint32_t varIndex, CcuRep::Variable *var);
343 : CcuRep::Variable CreateVariable();
344 : CcuRep::Variable CreateExpectVar();
345 : CcuRep::Variable CreateContinuousVariable();
346 : CcuRep::LocalAddr CreateLocalAddr();
347 : CcuRep::RemoteAddr CreateRemoteAddr();
348 : CcuRep::RemoteAddr GetRemoteAddr(const ChannelHandle channel, const uint32_t index);
349 : CcuRep::LocalNotify CreateLocalNotify();
350 : CcuRep::CompletedEvent CreateCompletedEvent();
351 : CcuRep::CcuBuf CreateCcuBuf();
352 : CcuRep::Executor CreateExecutor();
353 :
354 : HcclResult CreateBlockCcuBuf(const uint32_t count, CcuRep::CcuBuf *ccuBufs);
355 : HcclResult CreateBlockExecutor(const uint32_t count, CcuRep::Executor *ccuExes);
356 : HcclResult CreateBlockCompletedEvent(const uint32_t count, CcuRep::CompletedEvent *ccuEvents);
357 :
358 : // 内部 *Nb / RecordEvent / WaitEvent 系列:mask 由调用方独立传入,
359 : // 不再从 CompletedEvent 上读取。
360 : HcclResult RecordEvent(CcuRep::CompletedEvent event, uint32_t mask);
361 : HcclResult WaitEvent(CcuRep::CompletedEvent event, uint32_t mask);
362 :
363 : // 数据操作
364 : HcclResult WriteNb(const ChannelHandle channel, const CcuRep::RemoteAddr &rem, const CcuRep::LocalAddr &loc,
365 : const CcuRep::Variable &len, CcuRep::CompletedEvent event, uint32_t mask);
366 : HcclResult WriteNb(const ChannelHandle channel, const CcuRep::RemoteAddr &rem, const CcuRep::CcuBuf &loc,
367 : const CcuRep::Variable &len, CcuRep::CompletedEvent event, uint32_t mask);
368 :
369 : HcclResult ReadNb(const ChannelHandle channel, const CcuRep::LocalAddr &loc, const CcuRep::RemoteAddr &rem,
370 : const CcuRep::Variable &len, CcuRep::CompletedEvent event, uint32_t mask);
371 : HcclResult ReadNb(const ChannelHandle channel, const CcuRep::CcuBuf &loc, const CcuRep::RemoteAddr &rem,
372 : const CcuRep::Variable &len, CcuRep::CompletedEvent event, uint32_t mask);
373 :
374 : HcclResult WriteReduceNb(const ChannelHandle channel, const CcuRep::RemoteAddr &rem, const CcuRep::LocalAddr &loc,
375 : const CcuRep::Variable &len, HcclDataType dataType, HcclReduceOp opType, CcuRep::CompletedEvent event, uint32_t mask);
376 : HcclResult ReadReduceNb(const ChannelHandle channel, const CcuRep::LocalAddr &loc, const CcuRep::RemoteAddr &rem,
377 : const CcuRep::Variable &len, HcclDataType dataType, HcclReduceOp opType, CcuRep::CompletedEvent event, uint32_t mask);
378 :
379 : HcclResult LocalCopyNb(const CcuRep::LocalAddr &dst, const CcuRep::LocalAddr &src, const CcuRep::Variable &len,
380 : CcuRep::CompletedEvent event, uint32_t mask);//dst和src是否都是local
381 : HcclResult LocalCopyNb(const CcuRep::CcuBuf &dst, const CcuRep::LocalAddr &src, const CcuRep::Variable &len,
382 : CcuRep::CompletedEvent event, uint32_t mask);
383 : HcclResult LocalCopyNb(const CcuRep::LocalAddr &dst, const CcuRep::CcuBuf &src, const CcuRep::Variable &len,
384 : CcuRep::CompletedEvent event, uint32_t mask);
385 :
386 : HcclResult LocalReduceNb(const CcuRep::LocalAddr &dst, const CcuRep::LocalAddr &src, const CcuRep::Variable &len,
387 : HcclDataType dataType, HcclReduceOp opType, CcuRep::CompletedEvent event, uint32_t mask);
388 : HcclResult LocalReduceNb(const CcuRep::CcuBuf *bufs, uint32_t count, HcclDataType dataType,
389 : HcclDataType outputDataType, HcclReduceOp opType,
390 : const CcuRep::Variable &len, CcuRep::CompletedEvent event, uint32_t mask);
391 :
392 : // 参数操作
393 : void Load(const CcuRep::Variable &var);
394 :
395 : // Variable src中存放内存地址,从地址中加载数据到Variable var中
396 : void LoadVariable(const CcuRep::Variable &src, const CcuRep::Variable &var);
397 :
398 : void StoreVariable(const CcuRep::Variable &var, uint64_t addr);
399 : // 控制逻辑
400 : // 宏定义IF、WHILE
401 : CcuRep::FuncCall Func(const std::string &label);
402 : CcuRep::FuncCall Func(const CcuRep::Variable &funcAddr);
403 : CcuRep::LoopCall Loop(const std::string &label);
404 :
405 : private:
406 : CcuRep::Address CreateAddress();
407 : CcuRep::LocalAddr CreateLocalAddr(const CcuRep::Variable &token);
408 :
409 : protected:
410 : GroupOpConfig moConfig_{0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFFFFFFFFFF};
411 :
412 : private:
413 : template <typename T> T CreateResAssist(std::array<std::vector<T>, CCU_MAX_IODIE_NUM> &resRecord);
414 : template <typename T> std::vector<T> CreateBlockResAssist(const uint32_t count,
415 : std::array<std::vector<T>, CCU_MAX_IODIE_NUM> &resRecord);
416 :
417 : private:
418 : CcuRepResource res_{};
419 : CcuResRepository resRepo_{};
420 :
421 : std::unordered_set<ChannelHandle> channels_{};
422 :
423 : CcuRep::CcuInstrInfo instrInfo_{};
424 :
425 : uint32_t loadArgIndex_{0};
426 :
427 : CcuVersion ccuVersion_{CcuVersion::CCU_INVALID};
428 :
429 : CcuSharedResource exportedRes_{};
430 : CcuSharedResource importedRes_{};
431 : std::vector<GroupInfo> groupOpSizeInfo_;
432 : std::vector<CcuProfilingInfo> allCcuProfilingInfos_;
433 :
434 : // 记录每个kernel所需常量,适用于A6场景
435 : std::unordered_map<uint64_t, CcuRep::Variable> constValue2VarMap;
436 :
437 : struct LoopDescriptor {
438 : std::string label;
439 : std::shared_ptr<CcuRep::CcuRepLoopBlock> repLoopBlock;
440 : std::shared_ptr<CcuRep::CcuRepBlock> prevActiveBlock;
441 : bool bodyDefined{false};
442 : };
443 :
444 : struct VersionV2LoopRecord {
445 : CcuRep::Variable iterNumVar;
446 : CcuRep::Variable addrOffsetVar;
447 : CcuRep::Variable ctxIdVar;
448 : };
449 :
450 : struct LoopGroupDescriptor {
451 : CcuLoopGroupCfg config;
452 : uint64_t totalLoopNum{0};
453 : uint32_t loopCount{0};
454 : CcuRep::Variable parallelVar;
455 : CcuRep::Variable offsetVar;
456 : CcuRep::Variable xnOffsetVar;
457 : std::shared_ptr<CcuRep::CcuRepBase> bundleRep;
458 : bool isVarBased{false};
459 : bool isVersionV2{false};
460 : std::vector<VersionV2LoopRecord> versionV2Loops;
461 : };
462 :
463 : struct FuncDescriptor {
464 : const void *funcPtr{nullptr};
465 : std::string label;
466 : std::shared_ptr<CcuRep::CcuRepFuncBlock> repFuncBlock;
467 : std::shared_ptr<CcuRep::CcuRepBlock> prevActiveBlock;
468 : bool bodyDefined{false};
469 : };
470 :
471 : std::unordered_map<CcuLoop, LoopDescriptor> loopMap_;
472 : std::unordered_map<CcuLoopGroup, LoopGroupDescriptor> loopGroupMap_;
473 : CcuResult LookupLoopGroupAndLoop(CcuLoopGroup group, CcuLoop loop, const char *fnName,
474 : const char *createFnName, LoopGroupDescriptor *&grpDesc,
475 : LoopDescriptor *&loopDesc, uint32_t &loopIdx);
476 : uint32_t loopHandleCounter_{0};
477 : uint32_t loopGroupHandleCounter_{0};
478 : uint32_t loopBodyDepth_{0};
479 :
480 : std::unordered_map<uint64_t, FuncDescriptor> funcMap_;
481 : std::unordered_map<const void *, uint64_t> funcInstanceMap_;
482 : uint64_t funcHandleCounter_{0};
483 : bool inFuncBody_{false};
484 : // loop/func body 内首个非法错误的粘性闩,退出 body 时上抛(void body 无法直接回传)。
485 : CcuResult bodyError_{CcuResult::CCU_SUCCESS};
486 :
487 : std::unordered_map<CcuLoopExecutors, std::vector<CcuRep::Executor>> loopEnginePools_;
488 : uint32_t loopEnginePoolCounter_{0};
489 :
490 : std::string name_{};
491 : };
492 :
493 : } // namespace hcomm
494 :
495 : #endif // HCOMM_CCU_KERNEL_H
|