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 __AICPU_HCCL_DEF_H__
12 : #define __AICPU_HCCL_DEF_H__
13 :
14 : #include <cstdint>
15 : #include "hccl_common.h"
16 : #include "aicpu_operator_pub.h"
17 : #include "aicpu_hccl_sqcq.h"
18 : #include "aicpu_hccl_sqcqv1.h"
19 : #include "aicpu_hccl_sqcqv2.h"
20 : #include "profiling_extend_info.h"
21 : #include "common.h"
22 :
23 : constexpr u32 AC_MAX_RANK_NUM = 32U;
24 : constexpr u32 HCCL_COMM_DOMAIN_KEY_MAX_LEN = 128;
25 : constexpr u32 TILING_TURN_MAX = 32;
26 : constexpr u32 AC_MAX_PROF_LOOP = 30U;
27 : constexpr u32 AC_MAX_PROF_COMM_CNT = 1024U;
28 :
29 : using HccCommResParamTask = HcclCombinOpParam;
30 :
31 : // 重执行状态
32 : enum class HcclOpExecFSM {
33 : HCCL_OP_EXEC_FSM_INIT = 0,
34 : HCCL_OP_EXEC_FSM_LAUNCH,
35 : HCCL_OP_EXEC_FSM_WAIT_END,
36 : HCCL_OP_EXEC_FSM_STOPPING,
37 : HCCL_OP_EXEC_FSM_STOPPED,
38 : HCCL_OP_EXEC_FSM_CHANGE_LINK,
39 : HCCL_OP_EXEC_FSM_WAIT_RETRY,
40 : HCCL_OP_EXEC_FSM_RETRY,
41 : HCCL_OP_EXEC_FSM_END,
42 : HCCL_OP_EXEC_FSM_ERROR,
43 : HCCL_OP_EXEC_STOP_LAUNCH,
44 : };
45 :
46 : enum AicpuCcOpFinishMode {
47 : AICPUSUSPENDING_ERROR = 301U,
48 : };
49 :
50 : struct KFCTaskComm {
51 : u64 context; // HCCL通信context
52 : u64 tilingData; // 通信
53 : };
54 :
55 : struct AicpuComProfCommLoop {
56 : // addr消息处理耗时
57 : u64 hccExecStartTime;
58 : u64 sendTaskStartTime;
59 : u64 sendSqeFinishTime;
60 :
61 : u64 dataLen;
62 : };
63 :
64 : enum AicpuCCExecOp {
65 : CC_EXE_TWO_SHOT_8_STREAM = 0, /**< two shot算法,适用于大数据量通信场景*/
66 : CC_EXE_ONE_SHOT_8_STREAM = 1, /**< one shot算法,适用于小数据量通信场景*/
67 : CC_EXE_ONE_SHOT_4_STREAM = 2, /**< one shot算法,4条流,需要执行两轮通信 */
68 : CC_EXE_ONE_SHOT_1_STREAM = 3, /**< one shot算法,1条流,需要执行三轮通信 */
69 : CC_EXE_TWO_SHOT_1_STREAM = 4,
70 : CC_EXE_ONE_SHOT_HD = 5,
71 : CC_EXE_ONE_SHOT_SINGLE_RING = 6,
72 : };
73 :
74 : struct AicpuComProf {
75 : // 初始化耗时
76 : u64 launchEntryTime;
77 : u64 commInitEndTime;
78 :
79 : // sqe填写发送统计数据
80 : u32 fillSqeCnt;
81 : u64 fillSqeTimes;
82 : u32 sendSqeBatch;
83 : u64 sendSqeTimes;
84 :
85 : // 记录具体每轮通信信息
86 : u32 workCnt;
87 : AicpuComProfCommLoop commLoop[AC_MAX_PROF_COMM_CNT];
88 : u64 tid;
89 : s32 clusterId;
90 : u32 rankId;
91 :
92 : // 记录trace上报耗时
93 : u64 traceSubmitTime;
94 : u64 traceCtxTime;
95 : u64 traceSqeTime;
96 :
97 : // 尾处理耗时
98 : u64 receiveFinalizeTime;
99 : u64 endTime;
100 : };
101 :
102 : struct AicpuComRankInfo {
103 : u32 rankId;
104 : u64 window; // default size 200*1024*1024
105 : u64 windowOut;
106 : };
107 :
108 : struct AicpuComSignalInfo {
109 : u64 address; // notify地址
110 : s32 actualNotifyId;
111 : };
112 :
113 : // preparePosition标记
114 : enum TASK_PREPARE_POSITION {
115 : TASK_PREPARE_HOST = 0, // host模式,通信由host下发
116 : TASK_PREPARE_KERNEL = 1, // kernel模式,通信由kernel下发
117 : TASK_PREPARE_RESERVED = 100
118 : };
119 :
120 : enum class DfxKfcStatus : int64_t {
121 : kDefault = 0,
122 : kOneStart,
123 : kOneFinished,
124 : kTimeOut,
125 : };
126 :
127 : enum class CommandToKfc : int64_t { kDefault = 0, kClear, kStop, kRestart };
128 :
129 : enum class CommandToBackGroud : int64_t {
130 : kDefault = 0,
131 : kStop,
132 : };
133 :
134 : struct KfcRestartConfig {
135 : uint32_t tryRestartTimes{0U};
136 : uint32_t maxRestartTimes{1U}; // 最多重执行一次
137 : };
138 :
139 : struct TaskExceptionCqe {
140 : uint8_t sqeType{0};
141 : uint32_t errorCode{0};
142 : };
143 :
144 : struct DfxExtendInfo {
145 : DfxKfcStatus kfcStatus = DfxKfcStatus::kDefault;
146 : CqeStatus cqeStatus = CqeStatus::kDefault;
147 : PollStatus pollStatus = PollStatus::kDefault;
148 : TaskExceptionCqe cqeException;
149 : CommandToKfc commandToKfc = CommandToKfc::kDefault;
150 : KfcRestartConfig kfcRestartConfig;
151 : CommandToBackGroud commandToBackGroud = CommandToBackGroud::kDefault;
152 : dfx::DfxTimeOutConfig dfxTimeOutConfig;
153 : };
154 :
155 : // record the communication global info.
156 : struct AicpuComContext {
157 : char hcomId[HCCL_COMM_DOMAIN_KEY_MAX_LEN];
158 : u32 devId;
159 : u32 ssid;
160 : u32 rankId; // self rank id
161 : u32 rankNum; // total rank, include self
162 :
163 : HcclCMDType commType; // AllReduce, scatter..
164 : HcclReduceOp reducekind; // ADD,MAX,MIN,EQUAL
165 :
166 : AicpuCCExecOp commOpType; // twoshot.onshot...
167 : u32 unitSize;
168 : u64 commLen;
169 : u64 totalCnt; // 发送总数据个数
170 : u64 windowSize;
171 :
172 : u64 workSpaceAddr;
173 : u32 notifyOff; // device notify write/read value偏移
174 : u16 notifyBeginCnt; // notify write value的使用个数
175 : u16 notifyEndCnt; // notify read value的使用个数
176 : u8 useBufferType; // 使用recvbuf类型
177 : u64 winOffset;
178 :
179 : u64 kfcNotifyId; // 用于主流全部任务最后添加record,激活kfc流(即内部创建用于launch
180 : // kfc算子的流)wait,kfc流开始执行下一个算子
181 : u32 eventIds[AC_MAX_RANK_NUM];
182 :
183 : u32 curTurnCnt; // 当前算法执行通信轮次
184 : u32 turnValue[TILING_TURN_MAX * AC_MAX_RANK_NUM];
185 : u32 totalTurnCnt; // 需要通信总轮次
186 :
187 : // profiling
188 : AicpuComProf acprof[AC_MAX_PROF_LOOP];
189 :
190 : AicpuComRankInfo rankInfo[AC_MAX_RANK_NUM];
191 : HcclComStreamInfo streamInfo[AC_MAX_RANK_NUM];
192 : int logLevel;
193 :
194 : AicpuComSignalInfo noIpcPreNotify[AC_MAX_RANK_NUM];
195 : AicpuComSignalInfo noIpcPostNotify[AC_MAX_RANK_NUM];
196 : AicpuComSignalInfo ipcPreRecordNotify[AC_MAX_RANK_NUM];
197 : AicpuComSignalInfo ipcPreWaitNotify[AC_MAX_RANK_NUM];
198 : AicpuComSignalInfo ipcPostRecordNotify[AC_MAX_RANK_NUM];
199 : AicpuComSignalInfo ipcPostWaitNotify[AC_MAX_RANK_NUM];
200 : AicpuComSignalInfo aicpuOpNotify[2]; // 集合通信AICPU展开资源
201 :
202 : uint8_t commAlg; // 指定通信算法
203 : bool directlySendMainSteramSqe; // 消除头开销,第一需要直接执行时先下发主流
204 : bool alreadyInit;
205 : bool determinism;
206 : bool retryEnable;
207 : s32 clusterId;
208 : DevType devType = DevType::DEV_TYPE_COUNT;
209 : uint8_t debugMode = 0;
210 : u64 overflowAddr;
211 : TASK_PREPARE_POSITION preparePosition; // mc2高阶api,记录当前的模式
212 : u32 msgPosForKernel; // mc2高阶api,记录当前处理的msg的位置
213 : u8 curTurnCntForKernel; // mc2高阶api,记录当前msg的通信轮次
214 : u8 onlyRead; // 只使用 SDMA 读进行拷贝
215 : DfxExtendInfo dfxExtendInfo;
216 :
217 : int sendCntRecord[4]; // 4 记录aicpu过程中的sendCnt,最多记录4次
218 : int recvCntRecord[4]; // 4 记录aicpu过程中的recvCnt,最多记录4次
219 : u32 retryHoldTime;
220 : u32 retryIntervalTime;
221 : u32 opIndex;
222 : bool isOpLaunch = false; // 每个算子下发记录 (kNull/KRunning)
223 : bool isStopLaunch = false; // MC2测试用例下,主线程是否实现停止算子展开
224 : bool endStopLaunch = false; // NsStopLaunch是否处理 (背景线程/主线程)
225 : bool commOpenStatus = false; // 通信域使用情况
226 : bool isRunning = false;
227 : std::shared_ptr<hccl::HDCommunicate> kfcControlTransferH2D{nullptr};
228 : std::shared_ptr<hccl::HDCommunicate> kfcStatusTransferD2H{nullptr};
229 : dfx::ProfilingExtendInfo profilingExtendInfo;
230 : u8 totalTurnCntForKernel; // mc2高阶api,记录当前msg的总通信轮次
231 : bool skipLocalDataCopy; // 通信算法是否拷贝本卡数据,根据isCommOut配置
232 : // isCommOut = 1时,该项为0;isCommOut = 0时,该项为1
233 : u64 gatherOut;
234 : std::vector<struct hccl::TransportDeviceNormalData> ibversData;
235 : bool multiServerFlag;
236 : int64_t chipId;
237 : };
238 :
239 : extern AicpuComContext* AicpuGetComContext();
240 : extern void AicpuGetAllComContext(AicpuComContext*& contextBase, uint32_t& contextNum);
241 :
242 : template <typename T>
243 229257807 : void AicpuUpdatComContextMumber(u64 offset, T value)
244 : {
245 229257807 : AicpuComContext* contextBase = nullptr;
246 229257807 : uint32_t contextNum = 0;
247 229257807 : AicpuGetAllComContext(contextBase, contextNum);
248 687773421 : for (uint32_t i = 0; i < contextNum; i++) {
249 458515614 : T* dst = reinterpret_cast<T*>(reinterpret_cast<u64>(&contextBase[i]) + offset);
250 458515614 : *dst = value;
251 : }
252 458515614 : return;
253 : };
254 :
255 : struct KfcState {
256 94 : KfcState() { AicpuUpdatComContextMumber(offsetof(AicpuComContext, isRunning), true); }
257 94 : ~KfcState() { AicpuUpdatComContextMumber(offsetof(AicpuComContext, isRunning), false); }
258 : };
259 : #endif
|