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 "acl_rt_impl.h"
12 : #include "runtime/rts/rts_kernel.h"
13 : #include "runtime/kernel.h"
14 : #include "runtime/base.h"
15 : #include "runtime/rts/rts_stream.h"
16 : #include "runtime/rts/rts_device.h"
17 : #include "common/log_inner.h"
18 : #include "common/error_codes_inner.h"
19 : #include "common/prof_reporter.h"
20 :
21 : namespace {
22 : constexpr uint32_t ACL_ERROR_INVALID_EXCEPTION_INFO = 0xFFFFFFFFU;
23 : }
24 :
25 4 : aclError aclrtSubscribeReportImpl(uint64_t threadId, aclrtStream stream)
26 : {
27 4 : ACL_LOG_INFO("start to execute aclrtSubscribeReport, threadId is %lu.", threadId);
28 4 : const rtError_t rtErr = rtSubscribeReport(threadId, static_cast<rtStream_t>(stream));
29 4 : if (rtErr != RT_ERROR_NONE) {
30 2 : ACL_LOG_CALL_ERROR("subscribe report failed, runtime errorCode = %d", static_cast<int32_t>(rtErr));
31 2 : return ACL_GET_ERRCODE_RTS(rtErr);
32 : }
33 2 : ACL_LOG_INFO("successfully execute aclrtSubscribeReport, threadId is %lu.", threadId);
34 2 : return ACL_SUCCESS;
35 : }
36 :
37 3 : aclError aclrtSetExceptionInfoCallbackImpl(aclrtExceptionInfoCallback callback)
38 : {
39 3 : ACL_LOG_INFO("start to execute aclrtSetExceptionInfoCallback.");
40 3 : const rtError_t rtErr = rtRegTaskFailCallbackByModule(acl::ACL_MODULE_NAME,
41 : static_cast<rtTaskFailCallback>(callback));
42 3 : if (rtErr != RT_ERROR_NONE) {
43 2 : ACL_LOG_CALL_ERROR("set callback of fail task failed, runtime errorCode = %d", static_cast<int32_t>(rtErr));
44 2 : return ACL_GET_ERRCODE_RTS(rtErr);
45 : }
46 1 : ACL_LOG_INFO("successfully execute aclrtSetExceptionInfoCallback");
47 1 : return ACL_SUCCESS;
48 : }
49 :
50 1 : uint32_t aclrtGetTaskIdFromExceptionInfoImpl(const aclrtExceptionInfo *info)
51 : {
52 1 : if (info == nullptr) {
53 1 : ACL_LOG_INNER_ERROR("exception information is null, get task id failed.");
54 1 : return ACL_ERROR_INVALID_EXCEPTION_INFO;
55 : }
56 0 : return info->taskid;
57 : }
58 :
59 1 : uint32_t aclrtGetStreamIdFromExceptionInfoImpl(const aclrtExceptionInfo *info)
60 : {
61 1 : if (info == nullptr) {
62 1 : ACL_LOG_INNER_ERROR("exception information is null, get stream id failed.");
63 1 : return ACL_ERROR_INVALID_EXCEPTION_INFO;
64 : }
65 0 : return info->streamid;
66 : }
67 :
68 1 : uint32_t aclrtGetThreadIdFromExceptionInfoImpl(const aclrtExceptionInfo *info)
69 : {
70 1 : if (info == nullptr) {
71 1 : ACL_LOG_INNER_ERROR("exception information is null, get thread id failed.");
72 1 : return ACL_ERROR_INVALID_EXCEPTION_INFO;
73 : }
74 0 : return info->tid;
75 : }
76 :
77 2 : uint32_t aclrtGetDeviceIdFromExceptionInfoImpl(const aclrtExceptionInfo *info)
78 : {
79 2 : if (info == nullptr) {
80 1 : ACL_LOG_INNER_ERROR("exception information is null, get device id failed.");
81 1 : return ACL_ERROR_INVALID_EXCEPTION_INFO;
82 : }
83 1 : return info->deviceid;
84 : }
85 :
86 2 : uint32_t aclrtGetErrorCodeFromExceptionInfoImpl(const aclrtExceptionInfo *info)
87 : {
88 2 : if (info == nullptr) {
89 1 : ACL_LOG_INNER_ERROR("exception information is null, get error code failed.");
90 1 : return ACL_ERROR_INVALID_EXCEPTION_INFO;
91 : }
92 1 : return info->retcode;
93 : }
94 :
95 3 : aclError aclrtGetArgsFromExceptionInfoImpl(const aclrtExceptionInfo *info, void **devArgsPtr, uint32_t *devArgsLen)
96 : {
97 3 : if (info == nullptr) {
98 1 : ACL_LOG_INNER_ERROR("exception information is null, get args failed.");
99 1 : return ACL_ERROR_INVALID_EXCEPTION_INFO;
100 : }
101 :
102 2 : if (devArgsPtr == nullptr || devArgsLen == nullptr) {
103 0 : ACL_LOG_INNER_ERROR("devArgsPtr or devArgsLen is null, get args failed.");
104 0 : return ACL_ERROR_INVALID_PARAM;
105 : }
106 :
107 2 : if (info->expandInfo.type == RT_EXCEPTION_AICORE) {
108 1 : *devArgsPtr = info->expandInfo.u.aicoreInfo.exceptionArgs.argAddr;
109 1 : *devArgsLen = info->expandInfo.u.aicoreInfo.exceptionArgs.argsize;
110 1 : } else if (info->expandInfo.type == RT_EXCEPTION_FUSION &&
111 1 : info->expandInfo.u.fusionInfo.type == RT_FUSION_AICORE_CCU) {
112 1 : *devArgsPtr = info->expandInfo.u.fusionInfo.u.aicoreCcuInfo.exceptionArgs.argAddr;
113 1 : *devArgsLen = info->expandInfo.u.fusionInfo.u.aicoreCcuInfo.exceptionArgs.argsize;
114 : } else {
115 0 : ACL_LOG_INNER_ERROR("exception information type = %d is invalid, get args failed.", info->expandInfo.type);
116 0 : return ACL_ERROR_INVALID_EXCEPTION_INFO;
117 : }
118 :
119 2 : return ACL_SUCCESS;
120 : }
121 :
122 3 : aclError aclrtGetFuncHandleFromExceptionInfoImpl(const aclrtExceptionInfo *info, aclrtFuncHandle *func)
123 : {
124 3 : if (info == nullptr) {
125 1 : ACL_LOG_INNER_ERROR("exception information is null, get func failed.");
126 1 : return ACL_ERROR_INVALID_EXCEPTION_INFO;
127 : }
128 :
129 2 : if (func == nullptr) {
130 0 : ACL_LOG_INNER_ERROR("func is null, get func failed.");
131 0 : return ACL_ERROR_INVALID_PARAM;
132 : }
133 :
134 2 : const rtError_t rtErr = rtGetFuncHandleFromExceptionInfo(info, func);
135 2 : if (rtErr != ACL_RT_SUCCESS) {
136 1 : ACL_LOG_CALL_ERROR("get func handle from exception info failed, runtime result = %d.", rtErr);
137 1 : return ACL_GET_ERRCODE_RTS(rtErr);
138 : }
139 :
140 1 : return ACL_SUCCESS;
141 : }
142 :
143 3 : aclError aclrtBinarySetExceptionCallbackImpl(aclrtBinHandle binHandle, aclrtOpExceptionCallback callback, void *userData)
144 : {
145 3 : ACL_LOG_INFO("start to execute aclrtBinarySetExceptionCallback.");
146 3 : if (binHandle == nullptr || callback == nullptr) {
147 1 : ACL_LOG_INNER_ERROR("binHandle or callback is null, set callback failed.");
148 1 : return ACL_ERROR_INVALID_PARAM;
149 : }
150 :
151 2 : const rtError_t rtErr = rtBinarySetExceptionCallback(binHandle, callback, userData);
152 2 : if (rtErr != ACL_RT_SUCCESS) {
153 1 : ACL_LOG_CALL_ERROR("binary set exception callback failed, runtime result = %d.", rtErr);
154 1 : return ACL_GET_ERRCODE_RTS(rtErr);
155 : }
156 :
157 1 : return ACL_SUCCESS;
158 : }
159 :
160 6 : aclError aclrtLaunchCallbackImpl(aclrtCallback fn, void *userData, aclrtCallbackBlockType blockType,
161 : aclrtStream stream)
162 : {
163 12 : ACL_PROFILING_REG(acl::AclProfType::AclrtLaunchCallback);
164 6 : ACL_LOG_INFO("start to execute aclrtLaunchCallback.");
165 6 : if ((blockType != ACL_CALLBACK_BLOCK) && (blockType != ACL_CALLBACK_NO_BLOCK)) {
166 2 : ACL_LOG_INNER_ERROR("invalid block type, the current blockType = %d", static_cast<int32_t>(blockType));
167 2 : return ACL_ERROR_INVALID_PARAM;
168 : }
169 4 : const bool isBlock = (blockType == ACL_CALLBACK_BLOCK);
170 4 : const rtError_t rtErr = rtCallbackLaunch(static_cast<rtCallback_t>(fn), userData,
171 : static_cast<rtStream_t>(stream), isBlock);
172 4 : if (rtErr != RT_ERROR_NONE) {
173 2 : ACL_LOG_CALL_ERROR("launch callback task failed, runtime errorCode = %d", static_cast<int32_t>(rtErr));
174 2 : return ACL_GET_ERRCODE_RTS(rtErr);
175 : }
176 2 : ACL_LOG_INFO("successfully execute aclrtLaunchCallback");
177 2 : return ACL_SUCCESS;
178 : }
179 :
180 2 : aclError aclrtLaunchHostFuncImpl(aclrtStream stream, aclrtHostFunc fn, void *args)
181 : {
182 4 : ACL_PROFILING_REG(acl::AclProfType::AclrtLaunchHostFunc);
183 2 : ACL_LOG_INFO("start to execute aclrtLaunchHostFunc.");
184 2 : const rtError_t rtErr = rtsLaunchHostFunc(static_cast<rtStream_t>(stream), static_cast<rtCallback_t>(fn), args);
185 2 : if (rtErr != RT_ERROR_NONE) {
186 1 : ACL_LOG_CALL_ERROR("launch callback task failed, runtime errorCode = %d", static_cast<int32_t>(rtErr));
187 1 : return ACL_GET_ERRCODE_RTS(rtErr);
188 : }
189 1 : ACL_LOG_INFO("successfully execute aclrtLaunchHostFunc");
190 1 : return ACL_SUCCESS;
191 : }
192 :
193 10 : aclError aclrtProcessReportImpl(int32_t timeout)
194 : {
195 20 : ACL_PROFILING_REG(acl::AclProfType::AclrtProcessReport);
196 10 : ACL_LOG_INFO("start to execute aclrtProcessReport, timeout is %dms.", timeout);
197 : // -1 represents infinite wait, timeout value greater than 0 represents waiting for a fixed time.
198 : // other value is invalid.
199 10 : if ((timeout < -1) || (timeout == 0)) {
200 2 : ACL_LOG_ERROR("invalid timeout value, timeout[%d]", timeout);
201 2 : const std::string timeoutStr = acl::AclErrorLogManager::FormatStr("%dms", timeout);
202 4 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_MSG, std::vector<const char *>({"param", "value",
203 4 : "reason"}), std::vector<const char *>({"timeout", timeoutStr.c_str(), "-1 represents infinite wait, "
204 : "timeout value greater than 0 represents waiting for a fixed time"}));
205 2 : return ACL_ERROR_INVALID_PARAM;
206 : }
207 8 : const rtError_t rtErr = rtProcessReport(timeout);
208 8 : if (rtErr != RT_ERROR_NONE) {
209 6 : if (rtErr == ACL_ERROR_RT_THREAD_SUBSCRIBE) {
210 2 : ACL_LOG_INFO("no subscribereport info, runtime errorCode = %d", static_cast<int32_t>(rtErr));
211 4 : } else if (rtErr == ACL_ERROR_RT_REPORT_TIMEOUT) {
212 2 : ACL_LOG_INFO("wait subscribereport timeout, runtime errorCode = %d", static_cast<int32_t>(rtErr));
213 : } else {
214 2 : ACL_LOG_CALL_ERROR("process report failed, runtime errorCode = %d", static_cast<int32_t>(rtErr));
215 : }
216 6 : return ACL_GET_ERRCODE_RTS(rtErr);
217 : }
218 2 : ACL_LOG_INFO("successfully aclrtProcessReport, timeout is %dms.", timeout);
219 2 : return ACL_SUCCESS;
220 : }
221 :
222 4 : aclError aclrtUnSubscribeReportImpl(uint64_t threadId, aclrtStream stream)
223 : {
224 4 : ACL_LOG_INFO("start to execute aclrtUnSubscribeReport, threadId is %lu.", threadId);
225 4 : const rtError_t rtErr = rtUnSubscribeReport(threadId, static_cast<rtStream_t>(stream));
226 4 : if (rtErr != RT_ERROR_NONE) {
227 2 : ACL_LOG_CALL_ERROR("unsubscribe report failed, runtime errorCode = %d", static_cast<int32_t>(rtErr));
228 2 : return ACL_GET_ERRCODE_RTS(rtErr);
229 : }
230 2 : ACL_LOG_INFO("successfully execute aclrtUnSubscribeReport, threadId is %lu.", threadId);
231 2 : return ACL_SUCCESS;
232 : }
233 :
234 4 : aclError aclrtRegStreamStateCallbackImpl(const char *regName, aclrtStreamStateCallback callback, void *args)
235 : {
236 8 : ACL_PROFILING_REG(acl::AclProfType::AclrtRegStreamStateCallback);
237 4 : ACL_LOG_INFO("start to execute aclrtRegStreamStateCallback");
238 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(regName);
239 3 : const rtError_t rtErr = rtsRegStreamStateCallback(regName, reinterpret_cast<rtsStreamStateCallback>(callback), args);
240 3 : if (rtErr != RT_ERROR_NONE) {
241 1 : ACL_LOG_CALL_ERROR("call rtsRegStreamStateCallback failed, runtime result = %d.", static_cast<int32_t>(rtErr));
242 1 : return ACL_GET_ERRCODE_RTS(rtErr);
243 : }
244 2 : ACL_LOG_INFO("successfully execute aclrtRegStreamStateCallback");
245 2 : return ACL_SUCCESS;
246 : }
247 :
248 4 : aclError aclrtRegDeviceStateCallbackImpl(const char *regName, aclrtDeviceStateCallback callback, void *args)
249 : {
250 8 : ACL_PROFILING_REG(acl::AclProfType::AclrtRegDeviceStateCallback);
251 4 : ACL_LOG_INFO("start to execute aclrtRegDeviceStateCallback");
252 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(regName);
253 :
254 3 : const rtError_t rtErr = rtsRegDeviceStateCallback(regName, reinterpret_cast<rtsDeviceStateCallback>(callback), args);
255 3 : if (rtErr != RT_ERROR_NONE) {
256 1 : ACL_LOG_CALL_ERROR("call rtsRegDeviceStateCallback failed, runtime result = %d.", static_cast<int32_t>(rtErr));
257 1 : return ACL_GET_ERRCODE_RTS(rtErr);
258 : }
259 2 : ACL_LOG_INFO("successfully execute aclrtRegDeviceStateCallback");
260 2 : return ACL_SUCCESS;
261 : }
262 :
263 4 : aclError aclrtSetDeviceTaskAbortCallbackImpl(const char *regName, aclrtDeviceTaskAbortCallback callback, void *args)
264 : {
265 8 : ACL_PROFILING_REG(acl::AclProfType::AclrtSetDeviceTaskAbortCallback);
266 4 : ACL_LOG_INFO("start to execute aclrtSetDeviceTaskAbortCallback");
267 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(regName);
268 :
269 3 : const rtError_t rtErr = rtsSetDeviceTaskAbortCallback(regName, reinterpret_cast<rtsDeviceTaskAbortCallback>(callback), args);
270 3 : if (rtErr != RT_ERROR_NONE) {
271 1 : ACL_LOG_CALL_ERROR("call rtsSetDeviceTaskAbortCallback failed, runtime result = %d.", static_cast<int32_t>(rtErr));
272 1 : return ACL_GET_ERRCODE_RTS(rtErr);
273 : }
274 2 : ACL_LOG_INFO("successfully execute aclrtSetDeviceTaskAbortCallback");
275 2 : return ACL_SUCCESS;
276 : }
|