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 : #include "orion_adapter_rts.h"
12 : #include "runtime_api_exception.h"
13 : #include "exception_util.h"
14 : #include "invalid_params_exception.h"
15 : #include "log.h"
16 : #include "acl/acl_rt.h"
17 : #include "driver/ascend_hal.h"
18 : #include "not_support_exception.h"
19 : #include "adapter_error_manager_pub.h"
20 : #include "dlrts_function_v2.h"
21 :
22 : using namespace std;
23 : namespace Hccl {
24 :
25 : HcclResult HrtThreadExchangeCaptureMode(aclmdlRICaptureMode* mode);
26 : constexpr u32 TOKEN_ID_RIGHT_SHIF = 8; // URMA_TOKEN_ID_RIGHT_SHIF,因URMA配置原因需要右移8位
27 : namespace {
28 : constexpr char RT_SET_XPU_DEVICE[] = "rtSetXpuDevice";
29 : constexpr char RT_RESET_XPU_DEVICE[] = "rtResetXpuDevice";
30 : } // namespace
31 : const std::unordered_map<std::string, DevType> SOC_VER_CONVERT{
32 : {"Ascend310P1", DevType::DEV_TYPE_V51_310_P1},
33 : {"Ascend310P3", DevType::DEV_TYPE_V51_310_P3},
34 : {"Ascend910", DevType::DEV_TYPE_910A},
35 : {"Ascend910A", DevType::DEV_TYPE_910A},
36 : {"Ascend910B", DevType::DEV_TYPE_910A},
37 : {"Ascend910ProA", DevType::DEV_TYPE_910A},
38 : {"Ascend910ProB", DevType::DEV_TYPE_910A},
39 : {"Ascend910PremiumA", DevType::DEV_TYPE_910A},
40 : {"Ascend910B1", DevType::DEV_TYPE_910A2},
41 : {"Ascend910B2", DevType::DEV_TYPE_910A2},
42 : {"Ascend910B3", DevType::DEV_TYPE_910A2},
43 : {"Ascend910B4", DevType::DEV_TYPE_910A2},
44 : {"Ascend910B4-1", DevType::DEV_TYPE_910A2},
45 : {"Ascend910_939", DevType::DEV_TYPE_910A3},
46 : {"Ascend910_938", DevType::DEV_TYPE_910A3},
47 : {"Ascend910_937", DevType::DEV_TYPE_910A3},
48 : {"nosoc", DevType::DEV_TYPE_NOSOC}};
49 :
50 : // 添加编译宏,防止返回82类型芯片造成已有UT失效
51 64 : DevType HrtGetDeviceType()
52 : {
53 64 : std::string targetChipVerStr;
54 64 : HrtGetSocVer(targetChipVerStr);
55 :
56 192 : HCCL_INFO("[HrtGetDeviceType]targetChipVerStr = %s.", targetChipVerStr.c_str());
57 64 : if (targetChipVerStr.find("Ascend950") != std::string::npos) {
58 189 : HCCL_INFO("[HrtGetDeviceType]DeviceType = DevType::DEV_TYPE_950.");
59 63 : return DevType::DEV_TYPE_950;
60 : }
61 :
62 1 : if (targetChipVerStr.find("Ascend910_96") != std::string::npos
63 1 : || targetChipVerStr.find("Ascend960") != std::string::npos
64 2 : || targetChipVerStr.find("ascend960") != std::string::npos) {
65 0 : HCCL_INFO("[HrtGetDeviceType]DeviceType = DevType::DEV_TYPE_960.");
66 0 : return DevType::DEV_TYPE_960;
67 : }
68 :
69 1 : auto iter = SOC_VER_CONVERT.find(targetChipVerStr);
70 1 : if (iter == SOC_VER_CONVERT.end()) {
71 : string msg = StringFormat(
72 : "[Get][DeviceType]errNo[0x%016llx] rtGetSocVersion get "
73 : "illegal chipver, chip_ver[%s].",
74 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), targetChipVerStr.c_str());
75 4 : MACRO_THROW(RuntimeApiException, msg);
76 1 : }
77 0 : return iter->second;
78 64 : }
79 :
80 369 : DevId HrtGetDevicePhyIdByIndex(s32 deviceLogicId)
81 : {
82 369 : DevType deviceType = HrtGetDeviceType();
83 369 : if (deviceType == DevType::DEV_TYPE_NOSOC) {
84 1 : return 0;
85 : }
86 :
87 368 : s32 devicePhyId = 0;
88 368 : aclError ret = aclrtGetPhyDevIdByLogicDevId(deviceLogicId, &devicePhyId);
89 368 : if (ret != ACL_SUCCESS) {
90 : string msg = StringFormat(
91 : "[Get][DevicePhyId]errNo[0x%016llx] rtGet device PhyId by "
92 : "index failed. return[%d], "
93 : "para: devIndex[%d], phyId[%d].",
94 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_DRV), ret, deviceLogicId, devicePhyId);
95 4 : MACRO_THROW(RuntimeApiException, msg);
96 1 : }
97 1101 : HCCL_INFO("[HrtGetDevicePhyIdByIndex]deviceLogicId=%d, devicePhyId=%d.", deviceLogicId, devicePhyId);
98 367 : return static_cast<DevId>(devicePhyId);
99 : }
100 :
101 3 : s32 HrtDeviceGetBareTgid()
102 : {
103 3 : s32 pid = 0;
104 3 : aclError ret = aclrtDeviceGetBareTgid(&pid);
105 9 : HCCL_INFO("Call rtDeviceGetBareTgid, return value[%d], rtGet pid[%d].", ret, pid);
106 3 : if (ret != ACL_SUCCESS) {
107 : string msg = StringFormat(
108 : "[Get][BareTgid]errNo[0x%016llx] rtGet pid fail. "
109 : "return[%d], rtGet pid[%d].",
110 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, pid);
111 4 : MACRO_THROW(RuntimeApiException, msg);
112 1 : }
113 2 : return pid;
114 : }
115 :
116 64 : void HrtGetSocVer(std::string& socName)
117 : {
118 64 : const char* socNamePtr = aclrtGetSocName();
119 64 : if (socNamePtr == nullptr) {
120 : string msg = StringFormat(
121 1 : "[Get][SocVer]errNo[0x%016llx] rtGet deviceVer failed.", HCCL_ERROR_CODE((HcclResult::HCCL_E_RUNTIME)));
122 4 : MACRO_THROW(RuntimeApiException, msg);
123 1 : }
124 63 : socName = socNamePtr;
125 63 : }
126 :
127 1560 : s32 HrtGetDevice()
128 : {
129 1560 : s32 deviceLogicId = 0;
130 1560 : aclError ret = aclrtGetDevice(&deviceLogicId);
131 1560 : if (ret != ACL_SUCCESS) {
132 : string msg = StringFormat(
133 : "[Get][Device]errNo[0x%016llx] rtGet device fail, "
134 : "please make sure that device is set. return[%d], para:deviceLogicId[%d]",
135 2 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, deviceLogicId);
136 8 : MACRO_THROW(RuntimeApiException, msg);
137 2 : }
138 4674 : HCCL_INFO("[HrtGetDevice]deviceLogicId=%d.", deviceLogicId);
139 1558 : return deviceLogicId;
140 : }
141 :
142 14 : void HrtSetDevice(s32 deviceLogicId)
143 : {
144 14 : aclError ret = aclrtSetDevice(deviceLogicId);
145 42 : HCCL_INFO("Call rtSetDevice, return value[%d], para: device_id[%d].", ret, deviceLogicId);
146 14 : if (ret != ACL_SUCCESS) {
147 : string msg = StringFormat(
148 : "[Set][Device]errNo[0x%016llx] rtSet device fail. "
149 : "return[%d], para:deviceLogicId[%d].",
150 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, deviceLogicId);
151 4 : MACRO_THROW(RuntimeApiException, msg);
152 1 : }
153 13 : }
154 :
155 5 : void HrtResetDevice(s32 deviceLogicId)
156 : {
157 5 : aclError ret = aclrtResetDevice(deviceLogicId);
158 15 : HCCL_INFO("Call aclrtResetDevice, return value[%d], para: device_id[%d].", ret, deviceLogicId);
159 5 : if (ret != ACL_SUCCESS) {
160 : string msg = StringFormat(
161 : "[Reset][Device]errNo[0x%016llx] rtReset device fail. "
162 : "return[%d], para: deviceLogicId[%d].",
163 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, deviceLogicId);
164 4 : MACRO_THROW(RuntimeApiException, msg);
165 1 : }
166 4 : }
167 :
168 2 : u32 HrtGetDeviceCount()
169 : {
170 2 : u32 count = 0;
171 2 : aclError ret = aclrtGetDeviceCount(&count);
172 6 : HCCL_INFO("Call rtGetDeviceCount, return value[%d], para: count[%u].", ret, count);
173 2 : if (ret != ACL_SUCCESS) {
174 : string msg = StringFormat(
175 : "[Get][DeviceCount]errNo[0x%016llx] rtGet device count fail. "
176 : "return[%d], para:count[%u].",
177 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, count);
178 4 : MACRO_THROW(RuntimeApiException, msg);
179 1 : }
180 1 : return count;
181 : }
182 :
183 115 : HcclResult HrtGetLogicDevIdByUserDevId(s32 userDevId, s32& logicDevId)
184 : {
185 115 : DevType deviceType = HrtGetDeviceType();
186 115 : if (deviceType == DevType::DEV_TYPE_NOSOC) {
187 0 : logicDevId = 0;
188 0 : return HCCL_SUCCESS;
189 : }
190 :
191 115 : aclError aclRet = aclrtGetLogicDevIdByUserDevId(userDevId, &logicDevId); // userDevId 转 logicDevId
192 115 : if (aclRet != ACL_SUCCESS) {
193 0 : HCCL_ERROR("aclrtGetLogicDevIdByUserDevId failed, userDevId: %d, ret: %d", userDevId, aclRet);
194 0 : return HCCL_E_RUNTIME;
195 : }
196 :
197 115 : return HCCL_SUCCESS;
198 : }
199 :
200 : constexpr char RTS_SO_NAME[] = "libruntime.so";
201 : DlRtsFunctionV2<RTS_SO_NAME> g_dlRts;
202 3 : HcclResult HrtResetXpuDevice(uint32_t devType, const uint32_t devId)
203 : {
204 : static auto funcPtr
205 3 : = reinterpret_cast<rtError_t (*)(uint32_t, const uint32_t)>(g_dlRts.Handle<RT_RESET_XPU_DEVICE>());
206 12 : CHK_PTR_NULL(funcPtr);
207 0 : rtError_t ret = funcPtr(devType, devId);
208 0 : if (ret != RT_ERROR_NONE) {
209 0 : HCCL_ERROR("[%s] reset xpu device failed, devType[%u], devId[%u], return[%d].", __func__, devType, devId, ret);
210 0 : return HCCL_E_RUNTIME;
211 : }
212 0 : return HCCL_SUCCESS;
213 : }
214 :
215 0 : HcclResult HrtSetXpuDevice(uint32_t devType, const uint32_t devId)
216 : {
217 : static auto funcPtr
218 0 : = reinterpret_cast<rtError_t (*)(uint32_t, const uint32_t)>(g_dlRts.Handle<RT_SET_XPU_DEVICE>());
219 0 : CHK_PTR_NULL(funcPtr);
220 0 : rtError_t ret = funcPtr(devType, devId);
221 0 : if (ret != RT_ERROR_NONE) {
222 0 : HCCL_ERROR("[%s] set xpu device failed, devType[%u], devId[%u], return[%d].", __func__, devType, devId, ret);
223 0 : return HCCL_E_RUNTIME;
224 : }
225 0 : return HCCL_SUCCESS;
226 : }
227 :
228 78 : s32 HrtGetStreamId(aclrtStream ptr)
229 : {
230 : s32 streamId;
231 78 : aclError ret = aclrtStreamGetId(ptr, &streamId);
232 234 : HCCL_INFO("Call aclrtStreamGetId, ptr[%p] return value[%d] streamId[%d].", ptr, ret, streamId);
233 78 : if (ret != ACL_SUCCESS) {
234 : string msg = StringFormat(
235 : "[Get][StreamId]errNo[0x%016llx]. "
236 : "rt get stream ID fail. ptr[%p], return[%d].",
237 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ptr, ret);
238 4 : MACRO_THROW(RuntimeApiException, msg);
239 1 : }
240 :
241 77 : return streamId;
242 : }
243 :
244 16 : u64 HrtStreamGetMode(HcclRtStream const ptr)
245 : {
246 16 : if (ptr == nullptr) {
247 1 : throw RuntimeApiException(StringFormat("ptr is null, call aclrtGetStreamAttribute failed, ptr=%p", ptr));
248 : }
249 15 : u64 stmMode = 0;
250 15 : s32 streamId = -1;
251 15 : aclError ret = aclrtStreamGetId(ptr, &streamId);
252 45 : HCCL_DEBUG("[HrtStreamGetMode] ptr[%p], ret[%d].", ptr, ret);
253 : aclrtStreamAttrValue value;
254 15 : ret = aclrtGetStreamAttribute(ptr, ACL_STREAM_ATTR_FAILURE_MODE, &value);
255 15 : stmMode = value.failureMode;
256 45 : HCCL_INFO("Call rtStreamGetMode return value[%d]. stmMode[%llu].", ret, stmMode);
257 15 : if (ret != ACL_SUCCESS) {
258 : string msg = StringFormat(
259 : "[Stream][GetMode]errNo[0x%016llx] rtStreamGetMode error. "
260 : "ptr[%p], stmMode[%llu], streamId[%d], rtRet[%d].",
261 0 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ptr, stmMode, streamId, ret);
262 0 : MACRO_THROW(RuntimeApiException, msg);
263 0 : }
264 15 : return static_cast<u64>(stmMode);
265 : }
266 :
267 114 : void HrtStreamSetMode(HcclRtStream streamPtr, const uint64_t stmMode)
268 : {
269 114 : if (streamPtr == nullptr) {
270 1 : throw RuntimeApiException(StringFormat("ptr is null, call aclrtSetStreamAttribute failed, ptr=%p", streamPtr));
271 : }
272 113 : s32 streamId = -1;
273 113 : aclError ret = aclrtStreamGetId(streamPtr, &streamId);
274 339 : HCCL_DEBUG("Call aclrtStreamGetId, return value[%d].", ret);
275 : aclrtStreamAttrValue value;
276 113 : value.failureMode = stmMode;
277 113 : ret = aclrtSetStreamAttribute(streamPtr, ACL_STREAM_ATTR_FAILURE_MODE, &value);
278 339 : HCCL_INFO("[HrtStreamSetMode]streamPtr[%p], stmMode[%llu], ret[%d].", streamPtr, stmMode, ret);
279 113 : if (ret != ACL_SUCCESS) {
280 : string msg = StringFormat(
281 : "[Stream][SetMode]errNo[0x%016llx] rtStreamSetMode error. "
282 : "streamPtr[%p], rtRet[%d], stmMode[%llu].",
283 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), streamPtr, ret, stmMode);
284 4 : MACRO_THROW(RuntimeApiException, msg);
285 1 : }
286 112 : }
287 :
288 291 : HcclResult HrtGetDeviceInfo(uint32_t deviceLogicId, int32_t moduleType, aclrtDevAttr infoType, int64_t& val)
289 : {
290 291 : if (moduleType != DEV_MODULE_TYPE::MODULE_TYPE_SYSTEM) {
291 0 : THROW<NotSupportException>(StringFormat("[hrtGetDeviceInfo]Unsupported moduleType[%d].", moduleType));
292 : }
293 291 : aclError ret = aclrtGetDeviceInfo(deviceLogicId, infoType, reinterpret_cast<int64_t*>(&val));
294 873 : HCCL_INFO(
295 : "[HrtGetDeviceInfo]deviceLogicId[%u], moduleType[%d], infoType[%d], return[%d], val[%lld].", deviceLogicId,
296 : moduleType, infoType, ret, val);
297 291 : if (ret != ACL_SUCCESS) {
298 0 : HCCL_ERROR(
299 : "[HrtGetDeviceInfo]errNo[0x%016llx] rt get device info failed, "
300 : "deviceLogicId=%u, moduleType=%d, infoType=%d",
301 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), deviceLogicId, moduleType, infoType);
302 0 : return HcclResult::HCCL_E_RUNTIME;
303 : }
304 291 : return HcclResult::HCCL_SUCCESS;
305 : }
306 :
307 : constexpr uint64_t POD_MAINBOARD = 0x0;
308 : constexpr uint64_t A_K_SERVER_MAINBOARD = 0x1;
309 : constexpr uint64_t A_X_SERVER_MAINBOARD = 0x2;
310 : constexpr uint64_t PCIE_STD_MAINBOARD = 0x3;
311 : constexpr uint64_t RSV1_MAINBOARD = 0x4;
312 : constexpr uint64_t RSV2_MAINBOARD = 0x5;
313 : constexpr uint64_t EQUIP_MAINBOARD = 0x6;
314 : constexpr uint64_t EVB_MAINBOARD = 0x7;
315 :
316 : const std::unordered_map<uint64_t, HcclMainboardId> rtMainboardIdToHcclMainboardId
317 : = {{POD_MAINBOARD, HcclMainboardId::MAINBOARD_POD},
318 : {A_K_SERVER_MAINBOARD, HcclMainboardId::MAINBOARD_A_K_SERVER},
319 : {A_X_SERVER_MAINBOARD, HcclMainboardId::MAINBOARD_A_X_SERVER},
320 : {PCIE_STD_MAINBOARD, HcclMainboardId::MAINBOARD_PCIE_STD},
321 : {RSV1_MAINBOARD, HcclMainboardId::MAINBOARD_RSV},
322 : {RSV2_MAINBOARD, HcclMainboardId::MAINBOARD_RSV},
323 : {EQUIP_MAINBOARD, HcclMainboardId::MAINBOARD_EQUIPMENT},
324 : {EVB_MAINBOARD, HcclMainboardId::MAINBOARD_EVB}};
325 :
326 : /*
327 : * 获取Mainboard ID 5-7位,输出整机形态枚举值
328 : * Mainboard ID描述说明
329 : * Mainboard ID采用了16bit,区分形态,主从,以及端口配置
330 : * bit[7:5] 区分整机形态(当前POD和EVB没有区分A+X或A+K)
331 : * {
332 : * 000: 天成 POD
333 : * 001: A+K Server
334 : * 010: A+X Server
335 : * 011: PCIE标卡
336 : * 100-101: RSV
337 : * 110: 装备
338 : * 111: EVB
339 : * }
340 : * bit[4:1] 整机形态细分
341 : * {
342 : * 0000-1111
343 : * }
344 : * bit[0] 主从或池化
345 : * {
346 : * 0: 主从(NPU作为某个Host的从设备,Host主控)
347 : * 1: 池化(NPU作为资源池,其它Host对等访问)
348 : * }
349 : */
350 291 : HcclResult HrtGetMainboardId(uint32_t deviceLogicId, HcclMainboardId& hcclMainboardId)
351 : {
352 291 : constexpr int32_t moduleType = DEV_MODULE_TYPE::MODULE_TYPE_SYSTEM;
353 291 : constexpr aclrtDevAttr infoType = aclrtDevAttr::ACL_DEV_ATTR_MAINBOARD_ID;
354 291 : constexpr uint64_t BITS_5 = 5;
355 291 : constexpr uint64_t MASK_7 = 0x7;
356 291 : int64_t val = 0;
357 291 : CHK_RET(HrtGetDeviceInfo(deviceLogicId, moduleType, infoType, val));
358 873 : HCCL_INFO("[HrtGetMainboardId] deviceLogicId[%u] val[%lld].", deviceLogicId, val);
359 291 : CHK_PRT_RET(val < 0, HCCL_ERROR("[HrtGetMainboardId]val[%lld] < 0", val), HCCL_E_RUNTIME);
360 291 : uint64_t mainboardId = (static_cast<uint64_t>(val) >> BITS_5) & MASK_7; // 提取val的5-7位,判断整机形态
361 291 : auto it = rtMainboardIdToHcclMainboardId.find(mainboardId);
362 291 : if (it != rtMainboardIdToHcclMainboardId.end()) {
363 291 : hcclMainboardId = it->second;
364 : } else {
365 0 : hcclMainboardId = HcclMainboardId::MAINBOARD_OTHERS;
366 : }
367 873 : HCCL_INFO(
368 : "[HrtGetMainboardId] deviceLogicId[%u] mainboardId[%llu] hcclMainboardId[%s].", deviceLogicId, mainboardId,
369 : hcclMainboardId.Describe().c_str());
370 291 : return HcclResult::HCCL_SUCCESS;
371 : }
372 :
373 105 : aclrtStream HrtStreamCreateWithFlags(uint32_t priority, uint32_t flag)
374 : {
375 105 : aclrtStream ptr = nullptr;
376 105 : aclError ret = aclrtCreateStreamWithConfig(&ptr, priority, flag);
377 315 : HCCL_INFO("[HrtStreamCreateWithFlags] priority[%u], flags[%u], ptr[%p], ret[%d].", priority, flag, ptr, ret);
378 :
379 105 : if (ret != ACL_SUCCESS) {
380 : string msg = StringFormat(
381 : "[Stream][CreateWithFlags]errNo[0x%016llx] rtStreamCreate error, "
382 : "rtRet[%d], flags[%u].",
383 0 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, flag);
384 0 : MACRO_THROW(RuntimeApiException, msg);
385 0 : }
386 :
387 105 : return ptr;
388 : }
389 :
390 95 : void HrtStreamDestroy(aclrtStream ptr)
391 : {
392 95 : aclError ret = aclrtDestroyStreamForce(ptr);
393 285 : HCCL_INFO("[HrtStreamDestroy] ptr[%p], ret[%d].", ptr, ret);
394 95 : if (ret != ACL_SUCCESS) {
395 : string msg = StringFormat(
396 : "[Stream][Destroy]errNo[0x%016llx] rt stream Destroy fail. "
397 : "return[%d], ptr[%p].",
398 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, ptr);
399 4 : MACRO_THROW(RuntimeApiException, msg);
400 1 : }
401 94 : }
402 :
403 2 : void HrtStreamActive(aclrtStream activeStream, aclrtStream stream)
404 : {
405 2 : aclError ret = aclrtActiveStream(activeStream, stream);
406 6 : HCCL_INFO("[HrtStreamActive] activeStream[%p], stream[%p], ret[%d].", activeStream, stream, ret);
407 2 : if (ret != ACL_SUCCESS) {
408 : string msg = StringFormat(
409 : "[Activate][Stream]errNo[0x%016llx] "
410 : "rt stream active fail. return[%d], active_stream=%p, stream=%p.",
411 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, activeStream, stream);
412 4 : MACRO_THROW(RuntimeApiException, msg);
413 1 : }
414 1 : }
415 :
416 1 : inline s32 GetMsTimeFromExecTimeout()
417 : {
418 1 : constexpr s32 HCCL_EXEC_TIME_OUT_OFFSET_S = 5; // 避免与notifywait timeout时间冲突,增加5s的偏移值
419 1 : constexpr u32 TIME_S_TO_MS = 1000;
420 1 : s32 execTimeOut = 3000; // 从环境变量中获取超时时间
421 1 : s64 timeOutMs = (execTimeOut + HCCL_EXEC_TIME_OUT_OFFSET_S) * TIME_S_TO_MS;
422 1 : timeOutMs = (timeOutMs > 0x7FFFFFFF) ? 0x7FFFFFFF : timeOutMs;
423 1 : return static_cast<s32>(static_cast<u64>(timeOutMs) & (0x7FFFFFFF));
424 : }
425 :
426 2 : void HcclStreamSynchronize(HcclRtStream ptr)
427 : {
428 2 : if (ptr == nullptr) {
429 1 : string msg = StringFormat("ptr is null, call aclrtSynchronizeStreamWithTimeout failed, ptr=%p", ptr);
430 4 : MACRO_THROW(RuntimeApiException, msg);
431 1 : }
432 3 : HCCL_INFO("[HcclStreamSynchronize] ptr[%p].", ptr);
433 1 : s32 timeout = GetMsTimeFromExecTimeout();
434 1 : aclError ret = aclrtSynchronizeStreamWithTimeout(ptr, timeout);
435 1 : if (ret != ACL_SUCCESS) {
436 : string msg = StringFormat(
437 : "[Synchronize][Stream]errNo[0x%016llx] rt "
438 : "streamsynchronizewithtimeout fail. return[%d], stream[%p], timeout[%d ms].",
439 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, ptr, timeout);
440 4 : MACRO_THROW(RuntimeApiException, msg);
441 1 : }
442 0 : }
443 :
444 : /*
445 : *RT_MEMORY_TS:aclrtMallocForTaskScheduler
446 : *RT_MEMORY_DDR:ACL_MEM_TYPE_LOW_BAND_WIDTH
447 : *RT_MEMORY_HBM:ACL_MEM_TYPE_HIGH_BAND_WIDTH
448 : */
449 754 : void* HrtMalloc(u64 size, aclrtMemType_t memType)
450 : {
451 754 : aclError ret = ACL_SUCCESS;
452 754 : void* devPtr = nullptr;
453 : aclrtMallocAttrValue moduleIdValue;
454 754 : moduleIdValue.moduleId = HCCL;
455 754 : aclrtMallocAttribute attrs{.attr = ACL_RT_MEM_ATTR_MODULE_ID, .value = moduleIdValue};
456 754 : aclrtMallocConfig cfg{.attrs = &attrs, .numAttrs = 1};
457 754 : ret = aclrtMallocWithCfg(&devPtr, size, static_cast<aclrtMemMallocPolicy>(memType), &cfg);
458 2262 : HCCL_INFO("[HrtMalloc] ret[%d] size[%llu], memType[%d], devPtr[%p], moudleId: HCCL.", ret, size, memType, devPtr);
459 754 : if (ret == ACL_ERROR_RT_MEMORY_ALLOCATION) {
460 0 : RPT_INPUT_ERR(
461 : true, "EI0007", std::vector<std::string>({"resource_type", "resource_info"}),
462 : std::vector<std::string>(
463 : {"DeviceMemory", std::string("MallocWithCfg, size:") + std::to_string(size) + " bytes"}));
464 : string msg = StringFormat(
465 : "[Malloc][Mem]errNo[0x%016llx] aclrtMallocWithCfg failed, "
466 : "Reason: out of memory, return[%d], para: devPtrAddr[%p], size[%llu]",
467 0 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, devPtr, size);
468 0 : MACRO_THROW(RuntimeApiException, msg);
469 0 : }
470 754 : if (ret != ACL_SUCCESS) {
471 11 : RPT_INPUT_ERR(
472 : true, "EI0007", std::vector<std::string>({"resource_type", "resource_info"}),
473 : std::vector<std::string>(
474 : {"DeviceMemory", std::string("MallocWithCfg, size:") + std::to_string(size) + " bytes"}));
475 : string msg = StringFormat(
476 : "[Malloc][Mem]errNo[0x%016llx] aclrtMallocWithCfg failed, "
477 : "return[%d], para: devPtrAddr[%p], size[%llu]",
478 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, devPtr, size);
479 4 : MACRO_THROW(RuntimeApiException, msg);
480 1 : }
481 753 : return devPtr;
482 3 : }
483 :
484 1062 : void HrtFree(void* devPtr)
485 : {
486 1062 : aclError ret = aclrtFree(devPtr);
487 3186 : HCCL_INFO("[HrtFree] ret[%d], para: dev_ptr[%p].", ret, devPtr);
488 1062 : if (ret != RT_ERROR_NONE) {
489 : string msg = StringFormat(
490 : "[Free][Mem]errNo[0x%016llx] aclrtFree failed. "
491 : "return[%d], para: devPtrAddr[%p].",
492 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, devPtr);
493 4 : MACRO_THROW(RuntimeApiException, msg);
494 1 : }
495 1061 : }
496 :
497 1939 : HcclResult MemcpyKindTranslate(rtMemcpyKind_t kind, aclrtMemcpyKind* rtKind)
498 : {
499 1939 : switch (kind) {
500 5 : case rtMemcpyKind_t::RT_MEMCPY_HOST_TO_HOST: {
501 5 : *rtKind = ACL_MEMCPY_HOST_TO_HOST;
502 5 : return HCCL_SUCCESS;
503 : }
504 1190 : case rtMemcpyKind_t::RT_MEMCPY_HOST_TO_DEVICE: {
505 1190 : *rtKind = ACL_MEMCPY_HOST_TO_DEVICE;
506 1190 : return HCCL_SUCCESS;
507 : }
508 742 : case rtMemcpyKind_t::RT_MEMCPY_DEVICE_TO_HOST: {
509 742 : *rtKind = ACL_MEMCPY_DEVICE_TO_HOST;
510 742 : return HCCL_SUCCESS;
511 : }
512 1 : case rtMemcpyKind_t::RT_MEMCPY_DEVICE_TO_DEVICE: {
513 1 : *rtKind = ACL_MEMCPY_DEVICE_TO_DEVICE;
514 1 : return HCCL_SUCCESS;
515 : }
516 1 : default: {
517 3 : HCCL_ERROR("[MemcpyKindTranslate]Not support the memory copy type[%d].", kind);
518 1 : return HCCL_E_PARA;
519 : }
520 : }
521 : }
522 :
523 1939 : void HrtMemcpy(void* dst, uint64_t destMax, const void* src, uint64_t count, rtMemcpyKind_t kind)
524 : {
525 1939 : aclmdlRICaptureMode mode = aclmdlRICaptureMode::ACL_MODEL_RI_CAPTURE_MODE_RELAXED;
526 1939 : HcclResult hcclRet = HrtThreadExchangeCaptureMode(&mode);
527 1939 : CHK_PRT_CONT(
528 : hcclRet != HCCL_SUCCESS && hcclRet != HCCL_E_NOT_SUPPORT,
529 : HCCL_WARNING("[hrtMemcpy] HrtThreadExchangeCaptureMode return [%d].", hcclRet));
530 1939 : aclrtMemcpyKind rtKind = ACL_MEMCPY_DEFAULT;
531 1939 : hcclRet = MemcpyKindTranslate(kind, &rtKind);
532 1939 : aclError ret = aclrtMemcpy(dst, destMax, src, count, rtKind);
533 5817 : HCCL_INFO("[HrtMemcpy] dst[%p], destMax[%llu], src[%p], count[%llu], ret[%d].", dst, destMax, src, count, ret);
534 1939 : if (ret != ACL_SUCCESS) {
535 : string msg = StringFormat(
536 : "[SyncCopy][Mem]errNo[0x%016llx] rtMemcpy failed. "
537 : "return[%d], para: dstAddr[%p], destMax[%llu], srcAddr[%p], count[%llu], kind[%d].",
538 4 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, dst, destMax, src, count, kind);
539 16 : MACRO_THROW(RuntimeApiException, msg);
540 4 : }
541 1935 : hcclRet = HrtThreadExchangeCaptureMode(&mode);
542 1935 : CHK_PRT_CONT(
543 : hcclRet != HCCL_SUCCESS && hcclRet != HCCL_E_NOT_SUPPORT,
544 : HCCL_WARNING("[hrtMemcpy] HrtThreadExchangeCaptureMode return [%d].", hcclRet));
545 1935 : }
546 :
547 249 : void HrtMemset(void* dst, uint64_t destMax, uint64_t count)
548 : {
549 747 : HCCL_INFO("[HrtMemset] dst[%p], destMax[%llu], count[%llu].", dst, destMax, count);
550 249 : aclmdlRICaptureMode mode = aclmdlRICaptureMode::ACL_MODEL_RI_CAPTURE_MODE_RELAXED;
551 249 : HcclResult hcclRet = HrtThreadExchangeCaptureMode(&mode);
552 249 : CHK_PRT_CONT(
553 : hcclRet != HCCL_SUCCESS && hcclRet != HCCL_E_NOT_SUPPORT,
554 : HCCL_WARNING("[hrtMemSet] HrtThreadExchangeCaptureMode return [%d].", hcclRet));
555 249 : aclError ret = aclrtMemset(dst, destMax, 0, count);
556 249 : if (ret != ACL_SUCCESS) {
557 : string msg = StringFormat(
558 : "[SyncSet][Mem]errNo[0x%016llx] aclrtMemset failed. "
559 : "return[%d], para: dstAddr[%p], destMax[%llu], count[%llu].",
560 0 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, dst, destMax, count);
561 0 : MACRO_THROW(RuntimeApiException, msg);
562 0 : }
563 249 : hcclRet = HrtThreadExchangeCaptureMode(&mode);
564 249 : CHK_PRT_CONT(
565 : hcclRet != HCCL_SUCCESS && hcclRet != HCCL_E_NOT_SUPPORT,
566 : HCCL_WARNING("[hrtMemSet] HrtThreadExchangeCaptureMode return [%d].", hcclRet));
567 249 : }
568 :
569 6 : void HrtIpcSetMemoryName(void* ptr, char_t* name, u64 ptrMaxLen, u32 nameMaxLen)
570 : {
571 6 : aclError ret = aclrtIpcMemGetExportKey(ptr, ptrMaxLen, name, nameMaxLen, 1UL);
572 18 : HCCL_INFO(
573 : "Call aclrtIpcMemGetExportKey, return value[%d], para: ptr[%p], name[%s], byteCount[%llu], nameLen[%u]", ret,
574 : ptr, name, ptrMaxLen, nameMaxLen);
575 6 : if (ret != ACL_SUCCESS) {
576 : string msg = StringFormat(
577 : "[Set][IpcMemoryName]errNo[0x%016llx] rtSet Ipc Memory Name. "
578 : "return[%d], para: ptr[%p], byteCount[%llu], name[%s].",
579 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, ptr, ptrMaxLen, name);
580 4 : MACRO_THROW(RuntimeApiException, msg);
581 1 : }
582 5 : }
583 :
584 6 : void HrtIpcDestroyMemoryName(const char_t* name)
585 : {
586 6 : aclError ret = aclrtIpcMemClose(reinterpret_cast<const char*>(name));
587 18 : HCCL_INFO("Call aclrtIpcMemClose, return[%d], para: name[%s]", ret, reinterpret_cast<const char*>(name));
588 6 : if (ret != ACL_SUCCESS) {
589 : string msg = StringFormat(
590 : "[Destroy][IpcMemoryName]errNo[0x%016llx] "
591 : "rtDestroy Ipc memory name fail. return[%d], para: name[%s].",
592 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, name);
593 4 : MACRO_THROW(RuntimeApiException, msg);
594 1 : }
595 5 : }
596 :
597 3 : void* HrtIpcOpenMemory(const char_t* name)
598 : {
599 9 : HCCL_INFO("[HrtIpcOpenMemory] name[%s].", name);
600 3 : void* ptr = nullptr;
601 3 : aclError ret = aclrtIpcMemImportByKey(&ptr, name, 0UL);
602 3 : if (ret != ACL_SUCCESS) {
603 : string msg = StringFormat(
604 : "[Open][IpcMemory]errNo[0x%016llx] "
605 : "rtOpen ipc memory fail. return[%d], para: ptr[%p], name[%s].",
606 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, ptr, name);
607 4 : MACRO_THROW(RuntimeApiException, msg);
608 1 : }
609 2 : return ptr;
610 : }
611 :
612 2 : void HrtIpcCloseMemory(const void* ptr)
613 : {
614 2 : aclError ret = aclrtIpcMemClose(reinterpret_cast<const char*>(ptr));
615 6 : HCCL_INFO("Call aclrtIpcMemClose, return[%d], para: name[%s].", ret, reinterpret_cast<const char*>(ptr));
616 2 : if (ret != ACL_SUCCESS) {
617 : string msg = StringFormat(
618 : "[Close][IpcMemory]errNo[0x%016llx] "
619 : "rtClose ipc memory failed. return[%d], para: ptr[%p].",
620 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, ptr);
621 4 : MACRO_THROW(RuntimeApiException, msg);
622 1 : }
623 1 : }
624 :
625 1 : void HrtIpcSetMemoryPid(const char_t* name, int pid)
626 : {
627 1 : aclError ret = aclrtIpcMemSetImportPid(name, &pid, 1);
628 3 : HCCL_INFO("Call aclrtIpcMemSetImportPid, return value[%d], pid[%d], name[%s].", ret, pid, name);
629 1 : if (ret != ACL_SUCCESS) {
630 : string msg = StringFormat(
631 : "[Set][IpcMemoryPid]errNo[0x%016llx] "
632 : "rtSet ipc memory pid fail. return[%d], pid[%d], name[%s].",
633 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, pid, name);
634 4 : MACRO_THROW(RuntimeApiException, msg);
635 1 : }
636 0 : }
637 :
638 8 : aclrtPtrAttributes HrtPointerGetAttributes(const void* ptr)
639 : {
640 24 : HCCL_INFO("[HrtPointerGetAttributes] ptr[%p].", ptr);
641 : aclrtPtrAttributes ptrAttr;
642 8 : aclError ret = aclrtPointerGetAttributes(ptr, reinterpret_cast<aclrtPtrAttributes*>(&ptrAttr));
643 8 : if (ret != ACL_SUCCESS) {
644 : string msg = StringFormat(
645 : "[Get][PointAttr]errNo[0x%016llx] rt get point attr failed, "
646 : "return[%d], para: ptrAddr[%p].",
647 2 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, ptr);
648 8 : MACRO_THROW(RuntimeApiException, msg);
649 2 : }
650 6 : return ptrAttr;
651 : }
652 :
653 1 : void PrintMemoryAttr(const void* memAddr)
654 : {
655 1 : if (LIKELY(!CheckInfoLogLevel())) {
656 0 : return;
657 : }
658 1 : aclrtPtrAttributes memAttr = HrtPointerGetAttributes(memAddr);
659 3 : HCCL_INFO(
660 : "[PrintMemoryAttr] address[%p], page size[%u], type[%d]", memAddr, memAttr.pageSize, memAttr.location.type);
661 : }
662 :
663 8 : void HrtDevMemAlignWithPage(void* ptr, u64 size, void*& ipcPtr, u64& ipcSize, u64& ipcOff)
664 : {
665 8 : aclrtPtrAttributes memAttr = HrtPointerGetAttributes(ptr);
666 :
667 21 : HCCL_INFO(
668 : "[HrtDevMemAlignWithPage] ptr[%p], size[%llu], ipcPtr[%p], ipcSize[%llu], ipcOff[%llu], pageSize[%u].", ptr,
669 : size, ipcPtr, ipcSize, ipcOff, memAttr.pageSize);
670 7 : if (memAttr.pageSize == 0) {
671 1 : ipcPtr = ptr;
672 1 : ipcSize = size;
673 1 : ipcOff = 0;
674 1 : return;
675 : }
676 :
677 6 : u64 tmpPtr = reinterpret_cast<u64>(ptr);
678 6 : ipcPtr = reinterpret_cast<void*>((reinterpret_cast<u64>(ptr)) & (~(static_cast<u64>(memAttr.pageSize) - 1)));
679 6 : ipcOff = tmpPtr - reinterpret_cast<u64>(ipcPtr);
680 6 : ipcSize = size + ipcOff;
681 : }
682 :
683 141 : void* HrtMallocHost(u64 size)
684 : {
685 141 : void* hostPtr = nullptr;
686 : aclrtMallocAttrValue moduleIdValue;
687 141 : moduleIdValue.moduleId = HCCL;
688 141 : aclrtMallocAttribute attrs{.attr = ACL_RT_MEM_ATTR_MODULE_ID, .value = moduleIdValue};
689 141 : aclrtMallocConfig cfg{.attrs = &attrs, .numAttrs = 1};
690 141 : aclError ret = aclrtMallocHostWithCfg(&hostPtr, size, &cfg);
691 423 : HCCL_INFO(
692 : "Call aclrtMallocHostWithCfg. return value[%d], para: hostPtr[%p], size[%llu], moudleId: HCCL.", ret, hostPtr,
693 : size);
694 141 : if (ret != ACL_SUCCESS) {
695 22 : RPT_INPUT_ERR(
696 : true, "EI0007", std::vector<std::string>({"resource_type", "resource_info"}),
697 : std::vector<std::string>(
698 : {"HostMemory", std::string("MallocHostWithCfg, size:") + std::to_string(size) + " bytes"}));
699 : string msg = StringFormat(
700 : "[Malloc][Host]errNo[0x%016llx] rt malloc host fail. return[%d], "
701 : "para: size[%llu].",
702 2 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, size);
703 8 : MACRO_THROW(RuntimeApiException, msg);
704 2 : }
705 139 : return hostPtr;
706 6 : }
707 :
708 181 : void HrtFreeHost(void* hostPtr)
709 : {
710 543 : HCCL_INFO("[HrtFreeHost] hostPtr[%p].", hostPtr);
711 181 : aclError ret = aclrtFreeHost(hostPtr);
712 181 : if (ret != ACL_SUCCESS) {
713 : string msg = StringFormat(
714 : "[Free][Host]errNo[0x%016llx] rt free host fail. return[%d], "
715 : "para: hostPtr[%p].",
716 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, hostPtr);
717 4 : MACRO_THROW(RuntimeApiException, msg);
718 1 : }
719 180 : }
720 :
721 124 : aclrtNotify HrtNotifyCreate(s32 deviceLogicId)
722 : {
723 124 : aclrtNotify ptr = nullptr;
724 : // aclrtCreateNotify 中通过 aclrtGetDevice 获取 deviceId,所以要求当前线程设置过 setDevice
725 124 : aclError ret = aclrtCreateNotify(&ptr, ACL_NOTIFY_DEFAULT);
726 124 : if (ret != ACL_SUCCESS) {
727 : string msg = StringFormat(
728 : "[Notify][Create]errNo[0x%016llx] rtNotifyCreate error. "
729 : "return[%d], deviceId[%d], ptr[%p].",
730 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, deviceLogicId, ptr);
731 4 : MACRO_THROW(RuntimeApiException, msg);
732 1 : }
733 369 : HCCL_INFO("[HrtNotifyCreate] deviceId[%d], ptr[%p].", deviceLogicId, ptr);
734 123 : return ptr;
735 : }
736 :
737 872 : void HrtNotifyDestroy(RtNotify_t ptr)
738 : {
739 2616 : HCCL_INFO("[HrtNotifyDestroy] ptr[%p].", ptr);
740 872 : aclError ret = aclrtDestroyNotify(ptr);
741 872 : if (ret != ACL_SUCCESS) {
742 : string msg = StringFormat(
743 : "[Notify][Destroy]errNo[0x%016llx] aclrtDestroyNotify error. "
744 : "ptr[%p], return[%d].",
745 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ptr, ret);
746 4 : MACRO_THROW(RuntimeApiException, msg);
747 1 : }
748 871 : }
749 :
750 2 : void HrtIpcSetNotifyName(RtNotify_t ptr, char_t* name, uint32_t len)
751 : {
752 2 : aclError ret = aclrtNotifyGetExportKey(ptr, name, len, 2UL);
753 6 : HCCL_INFO("[HrtIpcSetNotifyName] ptr[%p], name[%s], len[%u], ret[%d].", ptr, name, len, ret);
754 2 : if (ret != ACL_SUCCESS) {
755 : string msg = StringFormat(
756 : "[Set][IPCNotify]errNo[0x%016llx] IPC set notify name fail. "
757 : "ptr[%p], name[%s], len[%u], return[%d].",
758 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ptr, name, len, ret);
759 4 : MACRO_THROW(RuntimeApiException, msg);
760 1 : }
761 1 : }
762 :
763 124 : u32 HrtGetNotifyID(RtNotify_t notifyHandle)
764 : {
765 124 : u32 notifyID = 0;
766 124 : aclError ret = aclrtGetNotifyId(notifyHandle, ¬ifyID);
767 124 : if (ret != ACL_SUCCESS) {
768 : string msg = StringFormat(
769 : "[HrtGetNotifyID]rt get notify id failed. "
770 : "notifyHandle[%p], notifyID[%u], return[%d].",
771 1 : notifyHandle, notifyID, ret);
772 4 : MACRO_THROW(RuntimeApiException, msg);
773 1 : }
774 369 : HCCL_INFO("[HrtGetNotifyID] notifyHandle[%p], notifyID[%u].", notifyHandle, notifyID);
775 123 : return notifyID;
776 : }
777 :
778 2 : u64 HrtNotifyGetAddr(RtNotify_t notifyHandle)
779 : {
780 6 : HCCL_INFO("[HrtNotifyGetAddr] notifyHandle[%p].", notifyHandle);
781 : uint64_t addr;
782 2 : rtError_t ret = rtGetNotifyAddress(notifyHandle, &addr);
783 2 : if (ret != RT_ERROR_NONE) {
784 : string msg = StringFormat(
785 : "[rtGetNotifyAddress]rt get notify address failed. "
786 : "notifyHandle[%p], addr[%llu], return[%d].",
787 1 : notifyHandle, addr, ret);
788 4 : MACRO_THROW(RuntimeApiException, msg);
789 1 : }
790 1 : return addr;
791 : }
792 :
793 1 : void HrtSetIpcNotifyPid(aclrtNotify notify, int32_t pid)
794 : {
795 1 : aclError ret = aclrtNotifySetImportPid(notify, &pid, 1);
796 3 : HCCL_INFO("[HrtSetIpcNotifyPid] notify[%p], pid[%d], ret[%d].", notify, pid, ret);
797 1 : if (ret != ACL_SUCCESS) {
798 : string msg = StringFormat(
799 : "[Set][IpcNotifyPid]errNo[0x%016llx] "
800 : "rtSet ipc Notify pid fail. notify[%p], return[%d], pid[%d].",
801 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), notify, ret, pid);
802 4 : MACRO_THROW(RuntimeApiException, msg);
803 1 : }
804 0 : }
805 :
806 2 : RtNotify_t HrtIpcOpenNotify(const char_t* name)
807 : {
808 2 : uint64_t flags = 0;
809 2 : aclrtNotify* notify = nullptr;
810 2 : aclError ret = aclrtNotifyImportByKey(notify, name, static_cast<uint64_t>(flags));
811 6 : HCCL_INFO("[HrtIpcOpenNotify] ret[%d], para: notify[%p], name[%s].", ret, notify, name);
812 2 : if (ret != ACL_SUCCESS) {
813 : string msg = StringFormat(
814 : "[rt][IpcOpenNotify]errNo[0x%016llx] rt ipc notify open fail. "
815 : "return[%d]. para: notify[%p], name[%s], flags[%llu].",
816 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, notify, name, flags);
817 4 : MACRO_THROW(RuntimeApiException, msg);
818 1 : }
819 1 : return notify;
820 : }
821 :
822 2 : u32 HrtNotifyGetOffset(RtNotify_t ptr)
823 : {
824 2 : uint32_t offset = 0;
825 2 : aclError ret = aclrtGetNotifyId(ptr, &offset);
826 2 : if (ret != ACL_SUCCESS) {
827 : string msg = StringFormat(
828 : "[rt][NotifyGetOffset]errNo[0x%016llx] rt ipc notify open fail. "
829 : "return[%d], ptr[%p], offset[%u].",
830 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, ptr, offset);
831 4 : MACRO_THROW(RuntimeApiException, msg);
832 1 : }
833 3 : HCCL_INFO("[HrtNotifyGetOffset] ptr[%p], offset[%u].", ptr, offset);
834 1 : return offset;
835 : }
836 :
837 14 : void HrtNotifyWaitWithTimeOut(RtNotify_t notifyPtr, aclrtStream streamPtr, uint32_t timeOut)
838 : {
839 14 : aclError ret = aclrtWaitAndResetNotify(notifyPtr, streamPtr, timeOut);
840 42 : HCCL_INFO(
841 : "[HrtNotifyWaitWithTimeOut] notifyPtr[%p], streamPtr[%p], timeOut[%u ms], ret[%d].", notifyPtr, streamPtr,
842 : timeOut, ret);
843 14 : if (ret != ACL_SUCCESS) {
844 : string msg = StringFormat(
845 : "call aclrtWaitAndResetNotify failed. notifyPtr=[%p], streamPtr=[%p], timeout=[%u ms], return=[%d].",
846 1 : notifyPtr, streamPtr, timeOut, ret);
847 4 : MACRO_THROW(RuntimeApiException, msg);
848 1 : }
849 13 : }
850 :
851 11 : void HrtNotifyRecord(RtNotify_t notifyPtr, aclrtStream streamPtr)
852 : {
853 11 : aclError ret = aclrtRecordNotify(notifyPtr, streamPtr);
854 33 : HCCL_INFO("[HrtNotifyRecord] notifyPtr[%p], streamPtr[%p], ret[%d].", notifyPtr, streamPtr, ret);
855 11 : if (ret != ACL_SUCCESS) {
856 : string msg = StringFormat(
857 1 : "call HrtNotifyRecord failed. notifyPtr=%p, streamPtr=%p, return=%d.", notifyPtr, streamPtr, ret);
858 4 : MACRO_THROW(RuntimeApiException, msg);
859 1 : }
860 10 : }
861 :
862 1 : void HrtMemAsyncCopy(
863 : void* dst, uint64_t destMax, const void* src, uint64_t count, aclrtMemcpyKind kind, aclrtStream streamPtr)
864 : {
865 1 : aclError ret = aclrtMemcpyAsync(dst, destMax, src, count, kind, streamPtr);
866 3 : HCCL_DEBUG(
867 : "[HrtMemAsyncCopy] ret[%d], para: dstAddr[%p], destMax[%llu], "
868 : "srcAddr[%p], count[%llu], rtKind[%d], streamPtr[%p].",
869 : ret, dst, destMax, src, count, kind, streamPtr);
870 1 : if (ret != ACL_SUCCESS) {
871 : string msg = StringFormat(
872 : "[AsyncCopy][Mem]errNo[0x%016llx] rt memory async copy failed. "
873 : "return[%d], para: dstAddr[%p], destMax[%llu], srcAddr[%p], count[%llu], kind[%d], stream[%p].",
874 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, dst, destMax, src, count, kind, streamPtr);
875 4 : MACRO_THROW(RuntimeApiException, msg);
876 1 : }
877 0 : }
878 :
879 4 : void HrtReduceAsync(
880 : void* dst, uint64_t destMax, const void* src, uint64_t count, aclrtReduceKind kind, aclDataType type,
881 : aclrtStream streamPtr)
882 : {
883 : // reserve 预留字段填 nullptr
884 4 : aclError ret = aclrtReduceAsync(dst, src, count, kind, type, streamPtr, nullptr);
885 12 : HCCL_INFO(
886 : "Call rtReduceAsync, return value[%d], para: dst[%p], destMax[%llu], src[%p], count[%llu], rtReduceOp[%d], "
887 : "rtDataType[%d], streamPtr[%p].",
888 : ret, dst, destMax, src, count, kind, type, streamPtr);
889 4 : if (ret != ACL_SUCCESS) {
890 : string msg = StringFormat(
891 : "[rt][ReduceAsync]errNo[0x%016llx] rt reduce async fail. "
892 : "return[%d], para: dst[%p], destMax[%llu], src[%p], count[%llu], rtReduceOp[%d], rtDataType[%d], "
893 : "streamPtr[%p].",
894 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, dst, destMax, src, count, kind, type, streamPtr);
895 4 : MACRO_THROW(RuntimeApiException, msg);
896 1 : }
897 3 : }
898 :
899 1 : void HrtRDMASend(u32 qpn, u32 wqeIndex, aclrtStream streamPtr)
900 : {
901 1 : rtError_t ret = rtRDMASend(qpn, wqeIndex, streamPtr);
902 3 : HCCL_INFO(
903 : "Call rtRDMASend, return value[%d]. Params: qpn[%u], wqeIndex[%u], streamPtr[%p].", ret, qpn, wqeIndex,
904 : streamPtr);
905 1 : if (ret != RT_ERROR_NONE) {
906 : string msg = StringFormat(
907 : "[rt][RdmaSend]errNo[0x%016llx] rt rdma send fail. "
908 : "return[%d]. para: qpn[%u], wqeIndex[%u], streamPtr[%p].",
909 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, qpn, wqeIndex, streamPtr);
910 4 : MACRO_THROW(RuntimeApiException, msg);
911 1 : }
912 0 : }
913 :
914 1 : void HrtRDMADBSend(uint32_t dbindex, uint64_t dbinfo, aclrtStream streamPtr)
915 : {
916 3 : HCCL_INFO("[HrtRDMADBSend] dbindex[%u], dbinfo[%llu], streamPtr[%p].", dbindex, dbinfo, streamPtr);
917 1 : rtError_t ret = rtRDMADBSend(dbindex, dbinfo, streamPtr);
918 1 : if (ret != RT_ERROR_NONE) {
919 : string msg = StringFormat(
920 : "[rtRDMADBSend]errNo[0x%016llx] rt rdma send fail. "
921 : "return[%d]. para: dbindex[%u], dbinfo[%llu], streamPtr[%p].",
922 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, dbindex, dbinfo, streamPtr);
923 4 : MACRO_THROW(RuntimeApiException, msg);
924 1 : }
925 0 : }
926 :
927 41 : void HrtGetTaskIdAndStreamID(u32& taskId, u32& streamId)
928 : {
929 41 : rtError_t ret = rtGetTaskIdAndStreamID(&taskId, &streamId);
930 123 : HCCL_INFO("[HrtGetTaskIdAndStreamID] ret[%d], para: taskId[%u], streamId[%u].", ret, taskId, streamId);
931 41 : if (ret != RT_ERROR_NONE) {
932 : string msg = StringFormat(
933 : "[Get][TaskIdAndStreamID]errNo[0x%016llx] "
934 : "rt get task ID and stream ID fail. return[%d], taskId[%u], streamId[%u].",
935 1 : HCCL_ERROR_CODE(HcclResult::HCCL_E_RUNTIME), ret, taskId, streamId);
936 4 : MACRO_THROW(RuntimeApiException, msg);
937 1 : }
938 40 : }
939 :
940 6 : void HrtUbDbSend([[maybe_unused]] const HrtUbDbInfo& info, [[maybe_unused]] aclrtStream streamPtr)
941 : {
942 6 : THROW<NotSupportException>(StringFormat("Unsupported rtUbDbSend"));
943 : }
944 :
945 2 : void HrtUbDirectSend([[maybe_unused]] const HrtUbWqeInfo& info, [[maybe_unused]] aclrtStream streamPtr)
946 : {
947 2 : THROW<NotSupportException>(StringFormat("Unsupported rtUbDirectSend"));
948 : }
949 :
950 68 : aclrtCntNotify HrtCntNotifyCreate(u32 deviceId)
951 : {
952 : aclrtCntNotify handle;
953 68 : aclError ret = aclrtCntNotifyCreate(&handle, RT_NOTIFY_FLAG_DEFAULT);
954 204 : HCCL_INFO("Call aclrtCntNotifyCreate, return value[%d] devId[%u].", ret, deviceId);
955 68 : if (ret != ACL_SUCCESS) {
956 0 : string msg = StringFormat("Call aclrtCntNotifyCreate failed");
957 0 : THROW<RuntimeApiException>(msg);
958 0 : }
959 68 : return handle;
960 : }
961 :
962 68 : u32 HrtGetCntNotifyId(const aclrtCntNotify inCntNotify)
963 : {
964 68 : u32 notifyId = 0; // 待接口rtGetCntNotifyId(inCntNotify, notifyId)上库,目前打桩;
965 68 : aclError ret = aclrtCntNotifyGetId(inCntNotify, ¬ifyId);
966 204 : HCCL_INFO("[HrtGetCntNotifyId] ret[%d], inCntNotify[%p], notifyId[%u]", ret, inCntNotify, notifyId);
967 68 : if (ret != ACL_SUCCESS) {
968 : string msg = StringFormat(
969 0 : "Call rtGetCntNotifyId failed. return[%d], inCntNotify[%p], notifyId[%u].", ret, inCntNotify, notifyId);
970 0 : THROW<RuntimeApiException>(msg);
971 0 : }
972 68 : return notifyId;
973 : }
974 :
975 86 : void HrtCntNotifyDestroy(const aclrtCntNotify inCntNotify)
976 : {
977 86 : aclError ret = aclrtCntNotifyDestroy(inCntNotify);
978 258 : HCCL_INFO("[HrtCntNotifyDestroy] ret[%d], inCntNotify[%p].", ret, inCntNotify);
979 86 : if (ret != ACL_SUCCESS) {
980 0 : string msg = StringFormat("Call aclrtCntNotifyDestroy failed");
981 0 : THROW<RuntimeApiException>(msg);
982 0 : }
983 86 : }
984 :
985 : const std::map<HrtCntNotifyRecordMode, aclrtCntNotifyRecordMode> HRT_CNT_NOTIFY_RECORD_MODE_MAP
986 : = {{HrtCntNotifyRecordMode::WRITE_BIT, aclrtCntNotifyRecordMode::ACL_RT_CNT_NOTIFY_RECORD_BIT_OR_MODE},
987 : {HrtCntNotifyRecordMode::STORE, aclrtCntNotifyRecordMode::ACL_RT_CNT_NOTIFY_RECORD_SET_VALUE_MODE}};
988 19 : void HrtCntNotifyRecord(
989 : const aclrtCntNotify inCntNotify, const aclrtStream streamPtr, HrtCntNotifyRecordMode mode, u32 value)
990 : {
991 19 : aclrtCntNotifyRecordInfo recordInfo{};
992 19 : recordInfo.mode = HRT_CNT_NOTIFY_RECORD_MODE_MAP.at(mode);
993 19 : recordInfo.value = value;
994 19 : aclError ret = aclrtCntNotifyRecord(inCntNotify, streamPtr, &recordInfo);
995 57 : HCCL_INFO(
996 : "[HrtCntNotifyRecord] inCntNotify[%p], streamPtr[%p], mode[%d], value[%u], ret[%d].", inCntNotify, streamPtr,
997 : mode, value, ret);
998 19 : if (ret != ACL_SUCCESS) {
999 0 : string msg = StringFormat("Call aclrtCntNotifyRecord failed");
1000 0 : THROW<RuntimeApiException>(msg);
1001 0 : }
1002 19 : }
1003 : // david接口 包间接口
1004 : const std::map<HrtCntNotifyWaitMode, aclrtCntNotifyWaitMode> HRT_CNT_NOTIFY_WAIT_MODE_MAP
1005 : = {{HrtCntNotifyWaitMode::EQUAL, aclrtCntNotifyWaitMode::ACL_RT_CNT_NOTIFY_WAIT_EQUAL_MODE},
1006 : {HrtCntNotifyWaitMode::BITMAP, aclrtCntNotifyWaitMode::ACL_RT_CNT_NOTIFY_WAIT_EQUAL_WITH_BITMASK_MODE}};
1007 21 : void HrtCntNotifyWaitWithTimeOut(
1008 : const aclrtCntNotify inCntNotify, const aclrtStream streamPtr, HrtCntNotifyWaitMode mode, u32 value, u32 timeout,
1009 : bool isClear)
1010 : {
1011 21 : aclrtCntNotifyWaitInfo waitInfo{};
1012 21 : waitInfo.mode = HRT_CNT_NOTIFY_WAIT_MODE_MAP.at(mode);
1013 21 : waitInfo.value = value;
1014 21 : waitInfo.isClear = isClear;
1015 21 : waitInfo.timeout = timeout;
1016 21 : aclError ret = aclrtCntNotifyWaitWithTimeout(inCntNotify, streamPtr, &waitInfo);
1017 63 : HCCL_INFO(
1018 : "[HrtCntNotifyWaitWithTimeOut] inCntNotify[%p], streamPtr[%p], mode[%d], "
1019 : "value[%u], timeout[%u ms], isClear[%d], ret[%d].",
1020 : inCntNotify, streamPtr, mode, value, timeout, isClear, ret);
1021 21 : if (ret != ACL_SUCCESS) {
1022 : string msg = StringFormat(
1023 : "Call rtCntNotifyWaitWithTimeout failed. return[%d], inCntNotify[%p], "
1024 : "streamPtr[%p], mode[%d], value[%u], timeout[%u ms], isClear[%d].",
1025 1 : ret, inCntNotify, streamPtr, mode, value, timeout, isClear);
1026 1 : THROW<RuntimeApiException>(msg);
1027 1 : }
1028 20 : }
1029 :
1030 3 : aclrtNotify HrtNotifyCreateWithFlag(u32 devId, u32 flag)
1031 : {
1032 3 : aclrtNotify ptr = nullptr;
1033 3 : aclError ret = aclrtCreateNotify(&ptr, flag);
1034 9 : HCCL_INFO("Call HrtNotifyCreateWithFlag, return value[%d], flag[%u] devid[%u].", ret, flag, devId);
1035 3 : if (ret != ACL_SUCCESS) {
1036 : string msg
1037 1 : = StringFormat("Call rtNotifyCreateWithFlag failed. return[%d], devId[%u], flag[%u].", ret, devId, flag);
1038 1 : THROW<RuntimeApiException>(msg);
1039 1 : }
1040 2 : return ptr;
1041 : }
1042 :
1043 2 : RtNotify_t HrtIpcOpenNotifyWithFlag(const char_t* name, uint32_t flags)
1044 : {
1045 6 : HCCL_INFO("[HrtIpcOpenNotifyWithFlag] name[%s], flags[%u].", name, flags);
1046 2 : RtNotify_t ptr = nullptr;
1047 2 : aclError ret = aclrtNotifyImportByKey(&ptr, name, static_cast<uint64_t>(flags));
1048 2 : if (ret != ACL_SUCCESS) {
1049 : string msg = StringFormat(
1050 1 : "Call rtIpcOpenNotifyWithFlag failed. return[%d], name[%p], flags[%u], ptr[%p].", ret, name, flags, ptr);
1051 1 : THROW<RuntimeApiException>(msg);
1052 1 : }
1053 1 : return ptr;
1054 : }
1055 :
1056 : // 兜底extern形式
1057 8 : void HrtAicpuLaunchKernelWithHostArgs(
1058 : aclrtFuncHandle funcHandle, uint32_t numBlocks, aclrtStream stream, aclrtLaunchKernelCfg* cfg, void* hostArgs,
1059 : size_t argsSize, aclrtPlaceHolderInfo* placeHolderArray, size_t placeHolderNum)
1060 : {
1061 8 : rtError_t ret = aclrtLaunchKernelWithHostArgs(
1062 : funcHandle, numBlocks, stream, cfg, hostArgs, argsSize, placeHolderArray, placeHolderNum);
1063 8 : if (ret != RT_ERROR_NONE) {
1064 1 : THROW<RuntimeApiException>(StringFormat("Call aclrtLaunchKernelWithHostArgs failed, with ret[%d]", ret));
1065 : }
1066 7 : }
1067 :
1068 7 : void HrtRegTaskFailCallbackByModule(aclrtExceptionInfoCallback callback)
1069 : {
1070 21 : HCCL_INFO("[HrtRegTaskFailCallbackByModule] callback[%p].", callback);
1071 7 : aclError ret = aclrtExceptionInfoCallbackRegister(callback);
1072 7 : if (ret != ACL_SUCCESS) {
1073 : string msg
1074 0 : = StringFormat("Call aclrtExceptionInfoCallbackRegister failed. return[%d], callback[%p].", ret, callback);
1075 0 : THROW<RuntimeApiException>(msg);
1076 0 : }
1077 7 : }
1078 :
1079 6 : void HrtUnregTaskFailCallbackByModule(aclrtExceptionInfoCallback callback)
1080 : {
1081 18 : HCCL_INFO("[HrtUnregTaskFailCallbackByModule] callback[%p].", callback);
1082 6 : aclError ret = aclrtExceptionInfoCallbackUnregister(callback);
1083 6 : if (ret != ACL_SUCCESS) {
1084 : string msg = StringFormat(
1085 0 : "Call aclrtExceptionInfoCallbackUnregister failed. return[%d], callback[%p].", ret, callback);
1086 0 : THROW<RuntimeApiException>(msg);
1087 0 : }
1088 6 : }
1089 :
1090 6 : u32 HrtStreamGetSqId(const aclrtStream ptr)
1091 : {
1092 18 : HCCL_INFO("[HrtStreamGetSqId] ptr[%p].", ptr);
1093 : u32 sqId;
1094 6 : rtError_t ret = rtStreamGetSqid(ptr, &sqId);
1095 6 : if (ret != RT_ERROR_NONE) {
1096 1 : string msg = StringFormat("Call rtStreamGetSqid failed. return[%d], ptr[%p], sqId[%u].", ret, ptr, sqId);
1097 1 : THROW<RuntimeApiException>(msg);
1098 1 : }
1099 5 : return sqId;
1100 : }
1101 :
1102 11 : u32 HrtStreamGetCqId(const aclrtStream ptr)
1103 : {
1104 33 : HCCL_INFO("[HrtStreamGetCqId] ptr[%p].", ptr);
1105 : u32 cqId;
1106 : u32 logicCqId;
1107 11 : rtError_t ret = rtStreamGetCqid(ptr, &cqId, &logicCqId);
1108 11 : if (ret != RT_ERROR_NONE) {
1109 0 : THROW<RuntimeApiException>(StringFormat("Call rtStreamGetCqid failed, with ret[%d]", ret));
1110 : }
1111 11 : return logicCqId;
1112 : }
1113 :
1114 20 : void HrtCcuLaunch(rtCcuTaskInfo_t& taskInfo, aclrtStream const streamPtr)
1115 : {
1116 60 : HCCL_INFO("[HrtCcuLaunch] taskInfo[%p], streamPtr[%p].", &taskInfo, streamPtr);
1117 20 : auto ret = rtCCULaunch(&taskInfo, streamPtr);
1118 20 : if (ret != RT_ERROR_NONE) {
1119 2 : std::string msg;
1120 2 : if (ret == ACL_ERROR_RT_STREAM_TASK_FULL) {
1121 : // task 队列满
1122 2 : msg = StringFormat(
1123 : "[%s] rtCCULaunch failed because the task queue on the stream is full. "
1124 : "ret[%d], param: taskInfo[%p], streamPtr[%p]. "
1125 : "Possible causes: 1) too many tasks have been submitted to the stream; "
1126 : "2) the HCCL buffer is configured too small.",
1127 1 : __func__, ret, &taskInfo, streamPtr);
1128 : } else {
1129 2 : msg = StringFormat(
1130 1 : "Call rtCCULaunch failed. return[%d], taskInfo[%p], streamPtr[%p].", ret, &taskInfo, streamPtr);
1131 : }
1132 2 : THROW<RuntimeApiException>(msg);
1133 2 : }
1134 18 : }
1135 :
1136 37 : void HrtUbDevQueryInfo(rtUbDevQueryCmd cmd, void* devInfo)
1137 : {
1138 111 : HCCL_INFO("[HrtUbDevQueryInfo] cmd[%d], devInfo[%p].", cmd, devInfo);
1139 37 : auto ret = rtUbDevQueryInfo(cmd, devInfo);
1140 37 : if (ret != RT_ERROR_NONE) {
1141 1 : string msg = StringFormat("Call rtUbDevQueryInfo failed. return[%d], cmd[%d], devInfo[%p].", ret, cmd, devInfo);
1142 1 : THROW<RuntimeApiException>(msg);
1143 1 : }
1144 36 : if (cmd == QUERY_PROCESS_TOKEN) {
1145 36 : rtMemUbTokenInfo* info = static_cast<rtMemUbTokenInfo*>(devInfo);
1146 36 : info->tokenId = info->tokenId >> TOKEN_ID_RIGHT_SHIF;
1147 : }
1148 36 : }
1149 : // pair<tokendId, tokenValue>
1150 9 : std::pair<u32, u32> HrtUbDevQueryToken(u64 addr, u64 size)
1151 : {
1152 27 : HCCL_INFO("[HrtUbDevQueryToken] addr[%llu], size[%llu].", addr, size);
1153 : rtMemUbTokenInfo info;
1154 9 : info.va = addr;
1155 9 : info.size = size;
1156 9 : auto ret = rtUbDevQueryInfo(QUERY_PROCESS_TOKEN, &info);
1157 9 : if (ret != RT_ERROR_NONE) {
1158 3 : HCCL_WARNING("query(va=0x%llx, size=0x%llx) token failed, ret=%d", addr, size, ret);
1159 1 : return std::make_pair(0, 0);
1160 : }
1161 :
1162 8 : return {info.tokenId >> TOKEN_ID_RIGHT_SHIF, info.tokenValue};
1163 : }
1164 :
1165 : const std::map<HrtDevResProcType, rtDevResProcType_t> HRT_DEV_RES_PROC_TYPE_MAP
1166 : = {{HrtDevResProcType::PROCESS_CP1, RT_PROCESS_CP1}, {HrtDevResProcType::PROCESS_HCCP, RT_PROCESS_HCCP}};
1167 :
1168 : const std::map<HrtDevResType, rtDevResType_t> HRT_DEV_RES_TYPE_MAP
1169 : = {{HrtDevResType::RES_TYPE_STARS_NOTIFY_RECORD, RT_RES_TYPE_STARS_NOTIFY_RECORD},
1170 : {HrtDevResType::RES_TYPE_CCU_CKE, RT_RES_TYPE_CCU_CKE},
1171 : {HrtDevResType::RES_TYPE_CCU_XN, RT_RES_TYPE_CCU_XN},
1172 : {HrtDevResType::RES_TYPE_STARS_CNT_NOTIFY_BIT_WR, RT_RES_TYPE_STARS_CNT_NOTIFY_BIT_WR}};
1173 125 : HrtDevResAddrInfo HrtGetDevResAddress(const HrtDevResInfo& devResInfo)
1174 : {
1175 375 : HCCL_INFO(
1176 : "[HrtGetDevResAddress] devResInfo.dieId[%u], devResInfo.procType[%u], "
1177 : "devResInfo.resType[%u], devResInfo.flag[%u], devResInfo.resId[%u].",
1178 : devResInfo.dieId, devResInfo.procType, devResInfo.resType, devResInfo.flag, devResInfo.resId);
1179 : rtDevResInfo resInfo;
1180 125 : resInfo.dieId = devResInfo.dieId;
1181 125 : resInfo.procType = HRT_DEV_RES_PROC_TYPE_MAP.at(devResInfo.procType);
1182 125 : resInfo.resType = HRT_DEV_RES_TYPE_MAP.at(devResInfo.resType);
1183 125 : resInfo.flag = devResInfo.flag;
1184 125 : resInfo.resId = devResInfo.resId;
1185 :
1186 125 : uint64_t addr = 0;
1187 125 : u32 len = 0;
1188 : rtDevResAddrInfo addrInfo;
1189 125 : addrInfo.resAddress = &addr;
1190 125 : addrInfo.len = &len;
1191 125 : auto ret = rtGetDevResAddress(&resInfo, &addrInfo);
1192 125 : if (ret != RT_ERROR_NONE) {
1193 : string msg = StringFormat(
1194 : "Call rtGetDevResAddress failed. return[%d], devResInfo.dieId[%u], "
1195 : "devResInfo.procType[%u], devResInfo.resType[%u], devResInfo.flag[%u], "
1196 : "devResInfo.resId[%u].",
1197 1 : ret, devResInfo.dieId, devResInfo.procType, devResInfo.resType, devResInfo.flag, devResInfo.resId);
1198 1 : THROW<RuntimeApiException>(msg);
1199 1 : }
1200 124 : HrtDevResAddrInfo devResAddrInfo;
1201 124 : devResAddrInfo.address = addr;
1202 124 : devResAddrInfo.len = len;
1203 372 : HCCL_INFO("devResAddrInfo.address[%llu], devResAddrInfo.len[%u].", devResAddrInfo.address, devResAddrInfo.len);
1204 124 : return devResAddrInfo;
1205 : }
1206 :
1207 125 : void HrtReleaseDevResAddress(const HrtDevResInfo& devResInfo)
1208 : {
1209 375 : HCCL_INFO(
1210 : "[HrtReleaseDevResAddress] devResInfo.dieId[%u], devResInfo.procType[%u], "
1211 : "devResInfo.resType[%u], devResInfo.flag[%u], devResInfo.resId[%u].",
1212 : devResInfo.dieId, devResInfo.procType, devResInfo.resType, devResInfo.flag, devResInfo.resId);
1213 : rtDevResInfo resInfo;
1214 125 : resInfo.dieId = devResInfo.dieId;
1215 125 : resInfo.procType = HRT_DEV_RES_PROC_TYPE_MAP.at(devResInfo.procType);
1216 125 : resInfo.resType = HRT_DEV_RES_TYPE_MAP.at(devResInfo.resType);
1217 125 : resInfo.flag = devResInfo.flag;
1218 125 : resInfo.resId = devResInfo.resId;
1219 :
1220 125 : rtError_t ret = rtReleaseDevResAddress(&resInfo);
1221 125 : if (ret != RT_ERROR_NONE) {
1222 : string msg = StringFormat(
1223 : "Call rtReleaseDevResAddress failed. return[%d], devResInfo.dieId[%u], "
1224 : "devResInfo.procType[%u],devResInfo.resType[%u], devResInfo.flag[%u], "
1225 : "devResInfo.resId[%u].",
1226 1 : ret, devResInfo.dieId, devResInfo.procType, devResInfo.resType, devResInfo.flag, devResInfo.resId);
1227 1 : THROW<RuntimeApiException>(msg);
1228 1 : }
1229 124 : }
1230 :
1231 5 : aclrtEvent HrtEventCreateWithFlag(u32 flag)
1232 : {
1233 15 : HCCL_INFO("[HrtEventCreateWithFlag] flag[%u].", flag);
1234 5 : aclrtEvent ptr = nullptr;
1235 5 : aclError ret = aclrtCreateEventWithFlag(&ptr, flag);
1236 5 : if (ret != ACL_SUCCESS) {
1237 1 : string msg = StringFormat("Call rtEventCreateWithFlag failed. return[%d], flag[%u], ptr[%p].", ret, flag, ptr);
1238 1 : THROW<RuntimeApiException>(msg);
1239 1 : }
1240 4 : return ptr;
1241 : }
1242 :
1243 7 : void HrtEventDestroy(RtEvent_t eventPtr)
1244 : {
1245 21 : HCCL_INFO("[HrtEventDestroy] eventPtr[%p].", eventPtr);
1246 7 : aclError ret = aclrtDestroyEvent(eventPtr);
1247 7 : if (ret != ACL_SUCCESS) {
1248 1 : string msg = StringFormat("Call aclrtDestroyEvent failed. return[%d], eventPtr[%p].", ret, eventPtr);
1249 1 : THROW<RuntimeApiException>(msg);
1250 1 : }
1251 6 : }
1252 :
1253 4 : void HrtEventRecord(RtEvent_t eventPtr, aclrtStream streamPtr)
1254 : {
1255 12 : HCCL_INFO("[HrtEventRecord] eventPtr[%p], streamPtr[%p].", eventPtr, streamPtr);
1256 4 : aclError ret = aclrtRecordEvent(eventPtr, streamPtr);
1257 4 : if (ret != ACL_SUCCESS) {
1258 : string msg = StringFormat(
1259 1 : "Call aclrtRecordEvent failed. return[%d], eventPtr[%p], streamPtr[%p].", ret, eventPtr, streamPtr);
1260 1 : THROW<RuntimeApiException>(msg);
1261 1 : }
1262 3 : }
1263 :
1264 : const std::map<aclrtEventWaitStatus, HrtEventStatus> HRT_EVENT_STATUS_MAP{
1265 : {ACL_EVENT_WAIT_STATUS_NOT_READY, HrtEventStatus::EVENT_INIT},
1266 : {ACL_EVENT_WAIT_STATUS_COMPLETE, HrtEventStatus::EVENT_RECORDED},
1267 : };
1268 :
1269 4 : HrtEventStatus HrtEventQueryStatus(RtEvent_t eventPtr)
1270 : {
1271 12 : HCCL_INFO("[HrtEventQueryStatus] eventPtr[%p].", eventPtr);
1272 4 : aclrtEventWaitStatus status = ACL_EVENT_WAIT_STATUS_NOT_READY;
1273 4 : aclError ret = aclrtQueryEventWaitStatus(eventPtr, &status);
1274 4 : if (ret != ACL_SUCCESS) {
1275 1 : string msg = StringFormat("Call aclrtQueryEventWaitStatus failed. return[%d], eventPtr[%p].", ret, eventPtr);
1276 1 : THROW<RuntimeApiException>(msg);
1277 1 : }
1278 3 : if (HRT_EVENT_STATUS_MAP.find(status) == HRT_EVENT_STATUS_MAP.end()) {
1279 0 : THROW<InvalidParamsException>(
1280 0 : StringFormat("event status[%u] not in HRT_EVENT_STATUS_MAP.", static_cast<u32>(status)));
1281 : }
1282 6 : return HRT_EVENT_STATUS_MAP.at(status);
1283 : }
1284 :
1285 6 : void HrtWriteValue([[maybe_unused]] u64 addr, [[maybe_unused]] u32 piVal, [[maybe_unused]] const aclrtStream streamPtr)
1286 : {
1287 6 : THROW<NotSupportException>(StringFormat("Unsupported rtWriteValue"));
1288 : }
1289 :
1290 1 : void HrtDeviceAbortRegCallBack(aclrtDeviceTaskAbortCallback callback, void* args, const std::string& name)
1291 : {
1292 1 : aclError ret = aclrtSetDeviceTaskAbortCallback(name.c_str(), callback, args);
1293 1 : if (ret != ACL_SUCCESS) {
1294 0 : string msg = StringFormat("call rtSetTaskAbortCallBack failed. ret=[%d].", ret);
1295 0 : THROW<RuntimeApiException>(msg);
1296 0 : }
1297 1 : }
1298 :
1299 4372 : HcclResult HrtThreadExchangeCaptureMode(aclmdlRICaptureMode* mode)
1300 : {
1301 13116 : HCCL_INFO("[HrtThreadExchangeCaptureMode] mode[%p].", mode);
1302 4372 : aclError ret = aclmdlRICaptureThreadExchangeMode(mode);
1303 4372 : if (ret == ACL_ERROR_RT_FEATURE_NOT_SUPPORT) {
1304 0 : HCCL_WARNING(
1305 : "[HrtThreadExchangeCaptureMode] rtThreadExchangeCaptureMode not support!, ret=%d, mode=%p.", ret, mode);
1306 0 : return HCCL_E_NOT_SUPPORT;
1307 : } else {
1308 4372 : CHK_PRT_RET(
1309 : ret != ACL_SUCCESS,
1310 : HCCL_ERROR(
1311 : "[HrtThreadExchangeCaptureMode]rtThreadExchangeCaptureMode "
1312 : "failed mode:%d, return value[%d].",
1313 : *mode, ret),
1314 : HCCL_E_RUNTIME);
1315 : }
1316 4372 : return HCCL_SUCCESS;
1317 : }
1318 :
1319 0 : HcclResult HrtMemPrefetchToDevice(void* devPtr, uint64_t len)
1320 : {
1321 0 : CHK_PRT_RET(
1322 : aclrtMemP2PMap == nullptr,
1323 : HCCL_ERROR("aclrtMemP2PMap is nullptr, "
1324 : "Does not support this interface."),
1325 : HCCL_E_RUNTIME);
1326 0 : aclError ret = aclrtMemP2PMap(devPtr, static_cast<size_t>(len), HrtGetDevice(), 0);
1327 0 : HCCL_INFO("[HrtMemPrefetchToDevice] devPtr[%p], len[%llu], ret[%d].", devPtr, len, ret);
1328 0 : if (ret != ACL_SUCCESS) {
1329 0 : HCCL_ERROR("aclrtMemP2PMap fail ret = %d", ret);
1330 0 : return HCCL_E_RUNTIME;
1331 : }
1332 0 : return HCCL_SUCCESS;
1333 : }
1334 :
1335 1 : HcclResult HrtEnableP2P(u32 deviceLogicId, u32 devicePhyId)
1336 : {
1337 1 : rtError_t ret = rtEnableP2P(deviceLogicId, devicePhyId, 0);
1338 :
1339 3 : HCCL_INFO("rt enableP2P deviceLogicId[%u] and devicePhyId[%u] ret[%d]", deviceLogicId, devicePhyId, ret);
1340 :
1341 1 : CHK_PRT_RET(
1342 : ret != RT_ERROR_NONE,
1343 : HCCL_ERROR(
1344 : "[Enable][P2P]errNo[0x%016llx] rt enableP2P deviceLogicId[%u] and "
1345 : "devicePhyId[%u] fail[%d]",
1346 : HCCL_ERROR_CODE(HCCL_E_RUNTIME), deviceLogicId, devicePhyId, ret),
1347 : HCCL_E_RUNTIME);
1348 :
1349 1 : return HCCL_SUCCESS;
1350 : }
1351 :
1352 1 : HcclResult HrtDisableP2P(u32 deviceLogicId, u32 devicePhyId)
1353 : {
1354 1 : rtError_t ret = rtDisableP2P(deviceLogicId, devicePhyId);
1355 :
1356 3 : HCCL_INFO("rt disableP2P deviceLogicId[%u] and devicePhyId[%u] ret[%d]", deviceLogicId, devicePhyId, ret);
1357 :
1358 1 : CHK_PRT_RET(
1359 : ret != RT_ERROR_NONE,
1360 : HCCL_ERROR(
1361 : "[Disable][P2P]errNo[0x%016llx] rt disableP2P deviceLogicId[%u] and "
1362 : "devicePhyId[%u] fail[%d]",
1363 : HCCL_ERROR_CODE(HCCL_E_RUNTIME), deviceLogicId, devicePhyId, ret),
1364 : HCCL_E_RUNTIME);
1365 1 : return HCCL_SUCCESS;
1366 : }
1367 :
1368 0 : HcclResult HrtGetP2PStatus(u32 deviceLogicId, u32 devicePhyId, uint32_t* status)
1369 : {
1370 0 : rtError_t ret = rtGetP2PStatus(deviceLogicId, devicePhyId, status);
1371 :
1372 0 : HCCL_DEBUG(
1373 : "rt getp2pstatus deviceLogicId[%u] and devicePhyId[%u] ret[%d], status[%u]", deviceLogicId, devicePhyId, ret,
1374 : *status);
1375 0 : CHK_PRT_RET(
1376 : ret != RT_ERROR_NONE,
1377 : HCCL_ERROR(
1378 : "[Get][P2PStatus]errNo[0x%016llx]Call rtGetP2PStatus failed, "
1379 : "ret[%d], deviceLogicId[%u], devicePhyId[%u]",
1380 : HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, deviceLogicId, devicePhyId),
1381 : HCCL_E_RUNTIME);
1382 0 : return HCCL_SUCCESS;
1383 : }
1384 : } // namespace Hccl
|