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