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 : #include "acl/acl_rt.h"
11 : #include "log_inner.h"
12 : #include "runtime/rt.h"
13 :
14 : #ifdef __cplusplus
15 : extern "C" {
16 : #endif
17 :
18 3 : aclError aclrtCreateContext(aclrtContext* context, int32_t deviceId)
19 : {
20 3 : if (context == NULL) {
21 1 : ACL_LOG_ERROR("context is NULL");
22 1 : return ACL_ERROR_INVALID_PARAM;
23 : }
24 :
25 2 : rtContext_t rtCtx = NULL;
26 2 : const rtError_t rtErr = rtCtxCreateEx(&rtCtx, (uint32_t)(RT_CTX_NORMAL_MODE), deviceId);
27 2 : if (rtErr != RT_ERROR_NONE) {
28 1 : ACL_LOG_CALL_ERROR("create context failed, device is %d, ret=%d", deviceId, (int32_t)(rtErr));
29 1 : return rtErr;
30 : }
31 :
32 1 : *context = (aclrtContext)(rtCtx);
33 1 : return ACL_SUCCESS;
34 : }
35 :
36 3 : aclError aclrtDestroyContext(aclrtContext context)
37 : {
38 3 : if (context == NULL) {
39 1 : ACL_LOG_ERROR("context is NULL");
40 1 : return ACL_ERROR_INVALID_PARAM;
41 : }
42 2 : const rtError_t rtErr = rtCtxDestroyEx((rtContext_t)(context));
43 2 : if (rtErr != RT_ERROR_NONE) {
44 1 : ACL_LOG_CALL_ERROR("destroy context failed, ret=%d", (int32_t)(rtErr));
45 1 : return rtErr;
46 : }
47 1 : return ACL_SUCCESS;
48 : }
49 :
50 3 : aclError aclrtSetCurrentContext(aclrtContext context)
51 : {
52 3 : if (context == NULL) {
53 1 : ACL_LOG_ERROR("context is NULL");
54 1 : return ACL_ERROR_INVALID_PARAM;
55 : }
56 2 : const rtError_t rtErr = rtCtxSetCurrent((rtContext_t)(context));
57 2 : if (rtErr != RT_ERROR_NONE) {
58 1 : ACL_LOG_CALL_ERROR("set current context failed, ret=%d", (int32_t)(rtErr));
59 1 : return rtErr;
60 : }
61 1 : return ACL_SUCCESS;
62 : }
63 :
64 3 : aclError aclrtGetCurrentContext(aclrtContext* context)
65 : {
66 3 : if (context == NULL) {
67 1 : ACL_LOG_ERROR("context is NULL");
68 1 : return ACL_ERROR_INVALID_PARAM;
69 : }
70 :
71 2 : rtContext_t rtCtx = NULL;
72 2 : const rtError_t rtErr = rtCtxGetCurrent(&rtCtx);
73 2 : if (rtErr != RT_ERROR_NONE) {
74 1 : ACL_LOG_INFO("can not get current context, ret=%d", (int32_t)(rtErr));
75 1 : return rtErr;
76 : }
77 :
78 1 : *context = rtCtx;
79 1 : return ACL_SUCCESS;
80 : }
81 :
82 : #if defined(__cplusplus)
83 : }
84 : #endif
|