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